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