2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * Based on the original work in Linux by
5 * Copyright (c) 2006 SUSE Linux Products GmbH
6 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/compat.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <dm/device.h>
20 * struct devres - Bookkeeping info for managed device resource
21 * @entry: List to associate this structure with a device
22 * @release: Callback invoked when this resource is released
23 * @probe: Flag to show when this resource was allocated
24 (true = probe, false = bind)
25 * @name: Name of release function
26 * @size: Size of resource data
27 * @data: Resource data
30 struct list_head entry;
33 #ifdef CONFIG_DEBUG_DEVRES
37 unsigned long long data[];
40 #ifdef CONFIG_DEBUG_DEVRES
41 static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
47 static void devres_log(struct udevice *dev, struct devres *dr,
50 printf("%s: DEVRES %3s %p %s (%lu bytes)\n",
51 dev->name, op, dr, dr->name, (unsigned long)dr->size);
53 #else /* CONFIG_DEBUG_DEVRES */
54 #define set_node_dbginfo(dr, n, s) do {} while (0)
55 #define devres_log(dev, dr, op) do {} while (0)
58 #if CONFIG_DEBUG_DEVRES
59 void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
62 void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
65 size_t tot_size = sizeof(struct devres) + size;
68 dr = kmalloc(tot_size, gfp);
72 INIT_LIST_HEAD(&dr->entry);
73 dr->release = release;
74 set_node_dbginfo(dr, name, size);
79 void devres_free(void *res)
82 struct devres *dr = container_of(res, struct devres, data);
84 BUG_ON(!list_empty(&dr->entry));
89 void devres_add(struct udevice *dev, void *res)
91 struct devres *dr = container_of(res, struct devres, data);
93 devres_log(dev, dr, "ADD");
94 BUG_ON(!list_empty(&dr->entry));
95 dr->probe = dev->flags & DM_FLAG_BOUND ? true : false;
96 list_add_tail(&dr->entry, &dev->devres_head);
99 void *devres_find(struct udevice *dev, dr_release_t release,
100 dr_match_t match, void *match_data)
104 list_for_each_entry_reverse(dr, &dev->devres_head, entry) {
105 if (dr->release != release)
107 if (match && !match(dev, dr->data, match_data))
115 void *devres_get(struct udevice *dev, void *new_res,
116 dr_match_t match, void *match_data)
118 struct devres *new_dr = container_of(new_res, struct devres, data);
121 res = devres_find(dev, new_dr->release, match, match_data);
123 devres_add(dev, new_res);
127 devres_free(new_res);
132 void *devres_remove(struct udevice *dev, dr_release_t release,
133 dr_match_t match, void *match_data)
137 res = devres_find(dev, release, match, match_data);
139 struct devres *dr = container_of(res, struct devres, data);
141 list_del_init(&dr->entry);
142 devres_log(dev, dr, "REM");
148 int devres_destroy(struct udevice *dev, dr_release_t release,
149 dr_match_t match, void *match_data)
153 res = devres_remove(dev, release, match, match_data);
161 int devres_release(struct udevice *dev, dr_release_t release,
162 dr_match_t match, void *match_data)
166 res = devres_remove(dev, release, match, match_data);
170 (*release)(dev, res);
175 static void release_nodes(struct udevice *dev, struct list_head *head,
178 struct devres *dr, *tmp;
180 list_for_each_entry_safe_reverse(dr, tmp, head, entry) {
181 if (probe_only && !dr->probe)
183 devres_log(dev, dr, "REL");
184 dr->release(dev, dr->data);
185 list_del(&dr->entry);
190 void devres_release_probe(struct udevice *dev)
192 release_nodes(dev, &dev->devres_head, true);
195 void devres_release_all(struct udevice *dev)
197 release_nodes(dev, &dev->devres_head, false);
200 #ifdef CONFIG_DEBUG_DEVRES
201 static void dump_resources(struct udevice *dev, int depth)
204 struct udevice *child;
206 printf("- %s\n", dev->name);
208 list_for_each_entry(dr, &dev->devres_head, entry)
209 printf(" %p (%lu byte) %s %s\n", dr,
210 (unsigned long)dr->size, dr->name,
211 dr->probe ? "PROBE" : "BIND");
213 list_for_each_entry(child, &dev->child_head, sibling_node)
214 dump_resources(child, depth + 1);
217 void dm_dump_devres(void)
219 struct udevice *root;
223 dump_resources(root, 0);
228 * Managed kmalloc/kfree
230 static void devm_kmalloc_release(struct udevice *dev, void *res)
235 static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
240 void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
244 data = _devres_alloc(devm_kmalloc_release, size, gfp);
248 devres_add(dev, data);
253 void devm_kfree(struct udevice *dev, void *p)
257 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);