1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
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>
11 #include <linux/compat.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <dm/device.h>
19 * struct devres - Bookkeeping info for managed device resource
20 * @entry: List to associate this structure with a device
21 * @release: Callback invoked when this resource is released
22 * @probe: Flag to show when this resource was allocated
23 (true = probe, false = bind)
24 * @name: Name of release function
25 * @size: Size of resource data
26 * @data: Resource data
29 struct list_head entry;
32 #ifdef CONFIG_DEBUG_DEVRES
36 unsigned long long data[];
39 #ifdef CONFIG_DEBUG_DEVRES
40 static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
46 static void devres_log(struct udevice *dev, struct devres *dr,
49 printf("%s: DEVRES %3s %p %s (%lu bytes)\n",
50 dev->name, op, dr, dr->name, (unsigned long)dr->size);
52 #else /* CONFIG_DEBUG_DEVRES */
53 #define set_node_dbginfo(dr, n, s) do {} while (0)
54 #define devres_log(dev, dr, op) do {} while (0)
57 #if CONFIG_DEBUG_DEVRES
58 void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
61 void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
64 size_t tot_size = sizeof(struct devres) + size;
67 dr = kmalloc(tot_size, gfp);
71 INIT_LIST_HEAD(&dr->entry);
72 dr->release = release;
73 set_node_dbginfo(dr, name, size);
78 void devres_free(void *res)
81 struct devres *dr = container_of(res, struct devres, data);
83 BUG_ON(!list_empty(&dr->entry));
88 void devres_add(struct udevice *dev, void *res)
90 struct devres *dr = container_of(res, struct devres, data);
92 devres_log(dev, dr, "ADD");
93 BUG_ON(!list_empty(&dr->entry));
94 dr->probe = dev->flags & DM_FLAG_BOUND ? true : false;
95 list_add_tail(&dr->entry, &dev->devres_head);
98 void *devres_find(struct udevice *dev, dr_release_t release,
99 dr_match_t match, void *match_data)
103 list_for_each_entry_reverse(dr, &dev->devres_head, entry) {
104 if (dr->release != release)
106 if (match && !match(dev, dr->data, match_data))
114 void *devres_get(struct udevice *dev, void *new_res,
115 dr_match_t match, void *match_data)
117 struct devres *new_dr = container_of(new_res, struct devres, data);
120 res = devres_find(dev, new_dr->release, match, match_data);
122 devres_add(dev, new_res);
126 devres_free(new_res);
131 void *devres_remove(struct udevice *dev, dr_release_t release,
132 dr_match_t match, void *match_data)
136 res = devres_find(dev, release, match, match_data);
138 struct devres *dr = container_of(res, struct devres, data);
140 list_del_init(&dr->entry);
141 devres_log(dev, dr, "REM");
147 int devres_destroy(struct udevice *dev, dr_release_t release,
148 dr_match_t match, void *match_data)
152 res = devres_remove(dev, release, match, match_data);
160 int devres_release(struct udevice *dev, dr_release_t release,
161 dr_match_t match, void *match_data)
165 res = devres_remove(dev, release, match, match_data);
169 (*release)(dev, res);
174 static void release_nodes(struct udevice *dev, struct list_head *head,
177 struct devres *dr, *tmp;
179 list_for_each_entry_safe_reverse(dr, tmp, head, entry) {
180 if (probe_only && !dr->probe)
182 devres_log(dev, dr, "REL");
183 dr->release(dev, dr->data);
184 list_del(&dr->entry);
189 void devres_release_probe(struct udevice *dev)
191 release_nodes(dev, &dev->devres_head, true);
194 void devres_release_all(struct udevice *dev)
196 release_nodes(dev, &dev->devres_head, false);
199 #ifdef CONFIG_DEBUG_DEVRES
200 static void dump_resources(struct udevice *dev, int depth)
203 struct udevice *child;
205 printf("- %s\n", dev->name);
207 list_for_each_entry(dr, &dev->devres_head, entry)
208 printf(" %p (%lu byte) %s %s\n", dr,
209 (unsigned long)dr->size, dr->name,
210 dr->probe ? "PROBE" : "BIND");
212 list_for_each_entry(child, &dev->child_head, sibling_node)
213 dump_resources(child, depth + 1);
216 void dm_dump_devres(void)
218 struct udevice *root;
222 dump_resources(root, 0);
227 * Managed kmalloc/kfree
229 static void devm_kmalloc_release(struct udevice *dev, void *res)
234 static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
239 void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
243 data = _devres_alloc(devm_kmalloc_release, size, gfp);
247 devres_add(dev, data);
252 void devm_kfree(struct udevice *dev, void *p)
256 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);