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
14 #include <linux/compat.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <dm/device.h>
18 #include <dm/devres.h>
22 /** enum devres_phase - Shows where resource was allocated
24 * DEVRES_PHASE_BIND: In the bind() method
25 * DEVRES_PHASE_OFDATA: In the of_to_plat() method
26 * DEVRES_PHASE_PROBE: In the probe() method
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
38 * @probe: Show where this resource was allocated
39 * @name: Name of release function
40 * @size: Size of resource data
41 * @data: Resource data
44 struct list_head entry;
46 enum devres_phase phase;
47 #ifdef CONFIG_DEBUG_DEVRES
51 unsigned long long data[];
54 #ifdef CONFIG_DEBUG_DEVRES
55 static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
61 static void devres_log(struct udevice *dev, struct devres *dr,
64 log_debug("%s: DEVRES %3s %p %s (%lu bytes)\n", dev->name, op, dr,
65 dr->name, (unsigned long)dr->size);
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)
72 #if CONFIG_DEBUG_DEVRES
73 void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
76 void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
79 size_t tot_size = sizeof(struct devres) + size;
82 dr = kmalloc(tot_size, gfp);
86 INIT_LIST_HEAD(&dr->entry);
87 dr->release = release;
88 set_node_dbginfo(dr, name, size);
93 void devres_free(void *res)
96 struct devres *dr = container_of(res, struct devres, data);
98 assert_noisy(list_empty(&dr->entry));
103 void devres_add(struct udevice *dev, void *res)
105 struct devres *dr = container_of(res, struct devres, data);
107 devres_log(dev, dr, "ADD");
108 assert_noisy(list_empty(&dr->entry));
109 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
110 dr->phase = DEVRES_PHASE_PROBE;
111 else if (dev_get_flags(dev) & DM_FLAG_BOUND)
112 dr->phase = DEVRES_PHASE_OFDATA;
114 dr->phase = DEVRES_PHASE_BIND;
115 list_add_tail(&dr->entry, &dev->devres_head);
118 void *devres_find(struct udevice *dev, dr_release_t release,
119 dr_match_t match, void *match_data)
123 list_for_each_entry_reverse(dr, &dev->devres_head, entry) {
124 if (dr->release != release)
126 if (match && !match(dev, dr->data, match_data))
134 void *devres_get(struct udevice *dev, void *new_res,
135 dr_match_t match, void *match_data)
137 struct devres *new_dr = container_of(new_res, struct devres, data);
140 res = devres_find(dev, new_dr->release, match, match_data);
142 devres_add(dev, new_res);
146 devres_free(new_res);
151 void *devres_remove(struct udevice *dev, dr_release_t release,
152 dr_match_t match, void *match_data)
156 res = devres_find(dev, release, match, match_data);
158 struct devres *dr = container_of(res, struct devres, data);
160 list_del_init(&dr->entry);
161 devres_log(dev, dr, "REM");
167 int devres_destroy(struct udevice *dev, dr_release_t release,
168 dr_match_t match, void *match_data)
172 res = devres_remove(dev, release, match, match_data);
180 int devres_release(struct udevice *dev, dr_release_t release,
181 dr_match_t match, void *match_data)
185 res = devres_remove(dev, release, match, match_data);
189 (*release)(dev, res);
194 static void release_nodes(struct udevice *dev, struct list_head *head,
195 bool probe_and_ofdata_only)
197 struct devres *dr, *tmp;
199 list_for_each_entry_safe_reverse(dr, tmp, head, entry) {
200 if (probe_and_ofdata_only && dr->phase == DEVRES_PHASE_BIND)
202 devres_log(dev, dr, "REL");
203 dr->release(dev, dr->data);
204 list_del(&dr->entry);
209 void devres_release_probe(struct udevice *dev)
211 release_nodes(dev, &dev->devres_head, true);
214 void devres_release_all(struct udevice *dev)
216 release_nodes(dev, &dev->devres_head, false);
219 #ifdef CONFIG_DEBUG_DEVRES
220 static char *const devres_phase_name[] = {"BIND", "OFDATA", "PROBE"};
222 static void dump_resources(struct udevice *dev, int depth)
225 struct udevice *child;
227 printf("- %s\n", dev->name);
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,
232 devres_phase_name[dr->phase]);
234 device_foreach_child(child, dev)
235 dump_resources(child, depth + 1);
238 void dm_dump_devres(void)
240 struct udevice *root;
244 dump_resources(root, 0);
247 void devres_get_stats(const struct udevice *dev, struct devres_stats *stats)
252 stats->total_size = 0;
253 list_for_each_entry(dr, &dev->devres_head, entry) {
255 stats->total_size += dr->size;
262 * Managed kmalloc/kfree
264 static void devm_kmalloc_release(struct udevice *dev, void *res)
269 static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
274 void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
278 data = _devres_alloc(devm_kmalloc_release, size, gfp);
282 devres_add(dev, data);
287 void devm_kfree(struct udevice *dev, void *p)
291 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);