1 // SPDX-License-Identifier: GPL-2.0+
5 * Based on the original work in Linux by
6 * Copyright (c) 2006 SUSE Linux Products GmbH
10 #define LOG_CATEGORY LOGC_DEVRES
15 #include <linux/compat.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <dm/device.h>
19 #include <dm/devres.h>
23 /** enum devres_phase - Shows where resource was allocated
25 * DEVRES_PHASE_BIND: In the bind() method
26 * DEVRES_PHASE_OFDATA: In the of_to_plat() method
27 * DEVRES_PHASE_PROBE: In the probe() method
36 * struct devres - Bookkeeping info for managed device resource
37 * @entry: List to associate this structure with a device
38 * @release: Callback invoked when this resource is released
39 * @probe: Show where this resource was allocated
40 * @name: Name of release function
41 * @size: Size of resource data
42 * @data: Resource data
45 struct list_head entry;
47 enum devres_phase phase;
48 #ifdef CONFIG_DEBUG_DEVRES
52 unsigned long long data[];
55 #ifdef CONFIG_DEBUG_DEVRES
56 static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
62 static void devres_log(struct udevice *dev, struct devres *dr,
65 log_debug("%s: DEVRES %3s %p %s (%lu bytes)\n", dev->name, op, dr,
66 dr->name, (unsigned long)dr->size);
68 #else /* CONFIG_DEBUG_DEVRES */
69 #define set_node_dbginfo(dr, n, s) do {} while (0)
70 #define devres_log(dev, dr, op) do {} while (0)
73 #if CONFIG_DEBUG_DEVRES
74 void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
77 void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
80 size_t tot_size = sizeof(struct devres) + size;
83 dr = kmalloc(tot_size, gfp);
87 INIT_LIST_HEAD(&dr->entry);
88 dr->release = release;
89 set_node_dbginfo(dr, name, size);
94 void devres_free(void *res)
97 struct devres *dr = container_of(res, struct devres, data);
99 assert_noisy(list_empty(&dr->entry));
104 void devres_add(struct udevice *dev, void *res)
106 struct devres *dr = container_of(res, struct devres, data);
108 devres_log(dev, dr, "ADD");
109 assert_noisy(list_empty(&dr->entry));
110 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
111 dr->phase = DEVRES_PHASE_PROBE;
112 else if (dev_get_flags(dev) & DM_FLAG_BOUND)
113 dr->phase = DEVRES_PHASE_OFDATA;
115 dr->phase = DEVRES_PHASE_BIND;
116 list_add_tail(&dr->entry, &dev->devres_head);
119 void *devres_find(struct udevice *dev, dr_release_t release,
120 dr_match_t match, void *match_data)
124 list_for_each_entry_reverse(dr, &dev->devres_head, entry) {
125 if (dr->release != release)
127 if (match && !match(dev, dr->data, match_data))
135 void *devres_get(struct udevice *dev, void *new_res,
136 dr_match_t match, void *match_data)
138 struct devres *new_dr = container_of(new_res, struct devres, data);
141 res = devres_find(dev, new_dr->release, match, match_data);
143 devres_add(dev, new_res);
147 devres_free(new_res);
152 void *devres_remove(struct udevice *dev, dr_release_t release,
153 dr_match_t match, void *match_data)
157 res = devres_find(dev, release, match, match_data);
159 struct devres *dr = container_of(res, struct devres, data);
161 list_del_init(&dr->entry);
162 devres_log(dev, dr, "REM");
168 int devres_destroy(struct udevice *dev, dr_release_t release,
169 dr_match_t match, void *match_data)
173 res = devres_remove(dev, release, match, match_data);
181 int devres_release(struct udevice *dev, dr_release_t release,
182 dr_match_t match, void *match_data)
186 res = devres_remove(dev, release, match, match_data);
190 (*release)(dev, res);
195 static void release_nodes(struct udevice *dev, struct list_head *head,
196 bool probe_and_ofdata_only)
198 struct devres *dr, *tmp;
200 list_for_each_entry_safe_reverse(dr, tmp, head, entry) {
201 if (probe_and_ofdata_only && dr->phase == DEVRES_PHASE_BIND)
203 devres_log(dev, dr, "REL");
204 dr->release(dev, dr->data);
205 list_del(&dr->entry);
210 void devres_release_probe(struct udevice *dev)
212 release_nodes(dev, &dev->devres_head, true);
215 void devres_release_all(struct udevice *dev)
217 release_nodes(dev, &dev->devres_head, false);
220 #ifdef CONFIG_DEBUG_DEVRES
221 static char *const devres_phase_name[] = {"BIND", "OFDATA", "PROBE"};
223 static void dump_resources(struct udevice *dev, int depth)
226 struct udevice *child;
228 printf("- %s\n", dev->name);
230 list_for_each_entry(dr, &dev->devres_head, entry)
231 printf(" %p (%lu byte) %s %s\n", dr,
232 (unsigned long)dr->size, dr->name,
233 devres_phase_name[dr->phase]);
235 device_foreach_child(child, dev)
236 dump_resources(child, depth + 1);
239 void dm_dump_devres(void)
241 struct udevice *root;
245 dump_resources(root, 0);
248 void devres_get_stats(const struct udevice *dev, struct devres_stats *stats)
253 stats->total_size = 0;
254 list_for_each_entry(dr, &dev->devres_head, entry) {
256 stats->total_size += dr->size;
263 * Managed kmalloc/kfree
265 static void devm_kmalloc_release(struct udevice *dev, void *res)
270 static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
275 void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
279 data = _devres_alloc(devm_kmalloc_release, size, gfp);
283 devres_add(dev, data);
288 void devm_kfree(struct udevice *dev, void *p)
292 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);