Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
| 4 | * |
| 5 | * Based on the original work in Linux by |
| 6 | * Copyright (c) 2006 SUSE Linux Products GmbH |
| 7 | * Copyright (c) 2006 Tejun Heo <teheo@suse.de> |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
Simon Glass | 8a8fe1b | 2019-12-29 21:19:24 -0700 | [diff] [blame] | 10 | #define LOG_CATEGORY LOGC_DEVRES |
| 11 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 14 | #include <linux/compat.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <dm/device.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <dm/devres.h> |
Masahiro Yamada | f929c0b | 2015-07-25 21:52:38 +0900 | [diff] [blame] | 19 | #include <dm/root.h> |
| 20 | #include <dm/util.h> |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 21 | |
Simon Glass | 76a64ab | 2019-12-29 21:19:27 -0700 | [diff] [blame] | 22 | /** enum devres_phase - Shows where resource was allocated |
| 23 | * |
| 24 | * DEVRES_PHASE_BIND: In the bind() method |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 25 | * DEVRES_PHASE_OFDATA: In the of_to_plat() method |
Simon Glass | 76a64ab | 2019-12-29 21:19:27 -0700 | [diff] [blame] | 26 | * DEVRES_PHASE_PROBE: In the probe() method |
| 27 | */ |
| 28 | enum devres_phase { |
| 29 | DEVRES_PHASE_BIND, |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 30 | DEVRES_PHASE_OFDATA, |
Simon Glass | 76a64ab | 2019-12-29 21:19:27 -0700 | [diff] [blame] | 31 | DEVRES_PHASE_PROBE, |
| 32 | }; |
| 33 | |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 34 | /** |
| 35 | * struct devres - Bookkeeping info for managed device resource |
| 36 | * @entry: List to associate this structure with a device |
| 37 | * @release: Callback invoked when this resource is released |
Simon Glass | 76a64ab | 2019-12-29 21:19:27 -0700 | [diff] [blame] | 38 | * @probe: Show where this resource was allocated |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 39 | * @name: Name of release function |
| 40 | * @size: Size of resource data |
| 41 | * @data: Resource data |
| 42 | */ |
| 43 | struct devres { |
| 44 | struct list_head entry; |
| 45 | dr_release_t release; |
Simon Glass | 76a64ab | 2019-12-29 21:19:27 -0700 | [diff] [blame] | 46 | enum devres_phase phase; |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 47 | #ifdef CONFIG_DEBUG_DEVRES |
| 48 | const char *name; |
| 49 | size_t size; |
| 50 | #endif |
| 51 | unsigned long long data[]; |
| 52 | }; |
| 53 | |
| 54 | #ifdef CONFIG_DEBUG_DEVRES |
| 55 | static void set_node_dbginfo(struct devres *dr, const char *name, size_t size) |
| 56 | { |
| 57 | dr->name = name; |
| 58 | dr->size = size; |
| 59 | } |
| 60 | |
| 61 | static void devres_log(struct udevice *dev, struct devres *dr, |
| 62 | const char *op) |
| 63 | { |
Simon Glass | 8a8fe1b | 2019-12-29 21:19:24 -0700 | [diff] [blame] | 64 | log_debug("%s: DEVRES %3s %p %s (%lu bytes)\n", dev->name, op, dr, |
| 65 | dr->name, (unsigned long)dr->size); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 66 | } |
| 67 | #else /* CONFIG_DEBUG_DEVRES */ |
| 68 | #define set_node_dbginfo(dr, n, s) do {} while (0) |
| 69 | #define devres_log(dev, dr, op) do {} while (0) |
| 70 | #endif |
| 71 | |
| 72 | #if CONFIG_DEBUG_DEVRES |
| 73 | void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp, |
| 74 | const char *name) |
| 75 | #else |
| 76 | void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp) |
| 77 | #endif |
| 78 | { |
| 79 | size_t tot_size = sizeof(struct devres) + size; |
| 80 | struct devres *dr; |
| 81 | |
| 82 | dr = kmalloc(tot_size, gfp); |
| 83 | if (unlikely(!dr)) |
| 84 | return NULL; |
| 85 | |
| 86 | INIT_LIST_HEAD(&dr->entry); |
| 87 | dr->release = release; |
| 88 | set_node_dbginfo(dr, name, size); |
| 89 | |
| 90 | return dr->data; |
| 91 | } |
| 92 | |
| 93 | void devres_free(void *res) |
| 94 | { |
| 95 | if (res) { |
| 96 | struct devres *dr = container_of(res, struct devres, data); |
| 97 | |
Simon Glass | 4da551b | 2019-12-29 21:19:11 -0700 | [diff] [blame] | 98 | assert_noisy(list_empty(&dr->entry)); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 99 | kfree(dr); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void devres_add(struct udevice *dev, void *res) |
| 104 | { |
| 105 | struct devres *dr = container_of(res, struct devres, data); |
| 106 | |
| 107 | devres_log(dev, dr, "ADD"); |
Simon Glass | 4da551b | 2019-12-29 21:19:11 -0700 | [diff] [blame] | 108 | assert_noisy(list_empty(&dr->entry)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 109 | if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID) |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 110 | dr->phase = DEVRES_PHASE_PROBE; |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 111 | else if (dev_get_flags(dev) & DM_FLAG_BOUND) |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 112 | dr->phase = DEVRES_PHASE_OFDATA; |
| 113 | else |
| 114 | dr->phase = DEVRES_PHASE_BIND; |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 115 | list_add_tail(&dr->entry, &dev->devres_head); |
| 116 | } |
| 117 | |
| 118 | void *devres_find(struct udevice *dev, dr_release_t release, |
| 119 | dr_match_t match, void *match_data) |
| 120 | { |
| 121 | struct devres *dr; |
| 122 | |
| 123 | list_for_each_entry_reverse(dr, &dev->devres_head, entry) { |
| 124 | if (dr->release != release) |
| 125 | continue; |
| 126 | if (match && !match(dev, dr->data, match_data)) |
| 127 | continue; |
| 128 | return dr->data; |
| 129 | } |
| 130 | |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | void *devres_get(struct udevice *dev, void *new_res, |
| 135 | dr_match_t match, void *match_data) |
| 136 | { |
| 137 | struct devres *new_dr = container_of(new_res, struct devres, data); |
| 138 | void *res; |
| 139 | |
| 140 | res = devres_find(dev, new_dr->release, match, match_data); |
| 141 | if (!res) { |
| 142 | devres_add(dev, new_res); |
| 143 | res = new_res; |
| 144 | new_res = NULL; |
| 145 | } |
| 146 | devres_free(new_res); |
| 147 | |
| 148 | return res; |
| 149 | } |
| 150 | |
| 151 | void *devres_remove(struct udevice *dev, dr_release_t release, |
| 152 | dr_match_t match, void *match_data) |
| 153 | { |
| 154 | void *res; |
| 155 | |
| 156 | res = devres_find(dev, release, match, match_data); |
| 157 | if (res) { |
| 158 | struct devres *dr = container_of(res, struct devres, data); |
| 159 | |
| 160 | list_del_init(&dr->entry); |
| 161 | devres_log(dev, dr, "REM"); |
| 162 | } |
| 163 | |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | int devres_destroy(struct udevice *dev, dr_release_t release, |
| 168 | dr_match_t match, void *match_data) |
| 169 | { |
| 170 | void *res; |
| 171 | |
| 172 | res = devres_remove(dev, release, match, match_data); |
| 173 | if (unlikely(!res)) |
| 174 | return -ENOENT; |
| 175 | |
| 176 | devres_free(res); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int devres_release(struct udevice *dev, dr_release_t release, |
| 181 | dr_match_t match, void *match_data) |
| 182 | { |
| 183 | void *res; |
| 184 | |
| 185 | res = devres_remove(dev, release, match, match_data); |
| 186 | if (unlikely(!res)) |
| 187 | return -ENOENT; |
| 188 | |
| 189 | (*release)(dev, res); |
| 190 | devres_free(res); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void release_nodes(struct udevice *dev, struct list_head *head, |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 195 | bool probe_and_ofdata_only) |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 196 | { |
| 197 | struct devres *dr, *tmp; |
| 198 | |
| 199 | list_for_each_entry_safe_reverse(dr, tmp, head, entry) { |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 200 | if (probe_and_ofdata_only && dr->phase == DEVRES_PHASE_BIND) |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 201 | break; |
| 202 | devres_log(dev, dr, "REL"); |
| 203 | dr->release(dev, dr->data); |
| 204 | list_del(&dr->entry); |
| 205 | kfree(dr); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void devres_release_probe(struct udevice *dev) |
| 210 | { |
| 211 | release_nodes(dev, &dev->devres_head, true); |
| 212 | } |
| 213 | |
| 214 | void devres_release_all(struct udevice *dev) |
| 215 | { |
| 216 | release_nodes(dev, &dev->devres_head, false); |
| 217 | } |
Masahiro Yamada | b475e1f | 2015-07-25 21:52:36 +0900 | [diff] [blame] | 218 | |
Masahiro Yamada | f929c0b | 2015-07-25 21:52:38 +0900 | [diff] [blame] | 219 | #ifdef CONFIG_DEBUG_DEVRES |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 220 | static char *const devres_phase_name[] = {"BIND", "OFDATA", "PROBE"}; |
| 221 | |
Masahiro Yamada | f929c0b | 2015-07-25 21:52:38 +0900 | [diff] [blame] | 222 | static void dump_resources(struct udevice *dev, int depth) |
| 223 | { |
| 224 | struct devres *dr; |
| 225 | struct udevice *child; |
| 226 | |
| 227 | printf("- %s\n", dev->name); |
| 228 | |
| 229 | list_for_each_entry(dr, &dev->devres_head, entry) |
| 230 | printf(" %p (%lu byte) %s %s\n", dr, |
| 231 | (unsigned long)dr->size, dr->name, |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 232 | devres_phase_name[dr->phase]); |
Masahiro Yamada | f929c0b | 2015-07-25 21:52:38 +0900 | [diff] [blame] | 233 | |
Sean Anderson | f69a330 | 2022-03-30 12:26:40 -0400 | [diff] [blame] | 234 | device_foreach_child(child, dev) |
Masahiro Yamada | f929c0b | 2015-07-25 21:52:38 +0900 | [diff] [blame] | 235 | dump_resources(child, depth + 1); |
| 236 | } |
| 237 | |
| 238 | void dm_dump_devres(void) |
| 239 | { |
| 240 | struct udevice *root; |
| 241 | |
| 242 | root = dm_root(); |
| 243 | if (root) |
| 244 | dump_resources(root, 0); |
| 245 | } |
Simon Glass | 323ff9f | 2019-12-29 21:19:26 -0700 | [diff] [blame] | 246 | |
| 247 | void devres_get_stats(const struct udevice *dev, struct devres_stats *stats) |
| 248 | { |
| 249 | struct devres *dr; |
| 250 | |
| 251 | stats->allocs = 0; |
| 252 | stats->total_size = 0; |
| 253 | list_for_each_entry(dr, &dev->devres_head, entry) { |
| 254 | stats->allocs++; |
| 255 | stats->total_size += dr->size; |
| 256 | } |
| 257 | } |
| 258 | |
Masahiro Yamada | f929c0b | 2015-07-25 21:52:38 +0900 | [diff] [blame] | 259 | #endif |
| 260 | |
Masahiro Yamada | b475e1f | 2015-07-25 21:52:36 +0900 | [diff] [blame] | 261 | /* |
| 262 | * Managed kmalloc/kfree |
| 263 | */ |
| 264 | static void devm_kmalloc_release(struct udevice *dev, void *res) |
| 265 | { |
| 266 | /* noop */ |
| 267 | } |
| 268 | |
| 269 | static int devm_kmalloc_match(struct udevice *dev, void *res, void *data) |
| 270 | { |
| 271 | return res == data; |
| 272 | } |
| 273 | |
| 274 | void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp) |
| 275 | { |
| 276 | void *data; |
| 277 | |
| 278 | data = _devres_alloc(devm_kmalloc_release, size, gfp); |
| 279 | if (unlikely(!data)) |
| 280 | return NULL; |
| 281 | |
| 282 | devres_add(dev, data); |
| 283 | |
| 284 | return data; |
| 285 | } |
| 286 | |
| 287 | void devm_kfree(struct udevice *dev, void *p) |
| 288 | { |
| 289 | int rc; |
| 290 | |
| 291 | rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p); |
Simon Glass | 4da551b | 2019-12-29 21:19:11 -0700 | [diff] [blame] | 292 | assert_noisy(!rc); |
Masahiro Yamada | b475e1f | 2015-07-25 21:52:36 +0900 | [diff] [blame] | 293 | } |