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