blob: 9c04499c6d5b0bd6c0042d69a47b6c2d6e1bba7a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada8b15b162015-07-25 21:52:35 +09002/*
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 Yamada8b15b162015-07-25 21:52:35 +09008 */
9
Simon Glass8a8fe1b2019-12-29 21:19:24 -070010#define LOG_CATEGORY LOGC_DEVRES
11
Masahiro Yamada8b15b162015-07-25 21:52:35 +090012#include <common.h>
13#include <linux/compat.h>
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <dm/device.h>
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +090017#include <dm/root.h>
18#include <dm/util.h>
Masahiro Yamada8b15b162015-07-25 21:52:35 +090019
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 */
30struct 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
42static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
43{
44 dr->name = name;
45 dr->size = size;
46}
47
48static void devres_log(struct udevice *dev, struct devres *dr,
49 const char *op)
50{
Simon Glass8a8fe1b2019-12-29 21:19:24 -070051 log_debug("%s: DEVRES %3s %p %s (%lu bytes)\n", dev->name, op, dr,
52 dr->name, (unsigned long)dr->size);
Masahiro Yamada8b15b162015-07-25 21:52:35 +090053}
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
60void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
61 const char *name)
62#else
63void *_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
80void devres_free(void *res)
81{
82 if (res) {
83 struct devres *dr = container_of(res, struct devres, data);
84
Simon Glass4da551b2019-12-29 21:19:11 -070085 assert_noisy(list_empty(&dr->entry));
Masahiro Yamada8b15b162015-07-25 21:52:35 +090086 kfree(dr);
87 }
88}
89
90void 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 Glass4da551b2019-12-29 21:19:11 -070095 assert_noisy(list_empty(&dr->entry));
Masahiro Yamada8b15b162015-07-25 21:52:35 +090096 dr->probe = dev->flags & DM_FLAG_BOUND ? true : false;
97 list_add_tail(&dr->entry, &dev->devres_head);
98}
99
100void *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
116void *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
133void *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
149int 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
162int 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
176static 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
191void devres_release_probe(struct udevice *dev)
192{
193 release_nodes(dev, &dev->devres_head, true);
194}
195
196void devres_release_all(struct udevice *dev)
197{
198 release_nodes(dev, &dev->devres_head, false);
199}
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900200
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900201#ifdef CONFIG_DEBUG_DEVRES
202static 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
218void 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 Yamadab475e1f2015-07-25 21:52:36 +0900228/*
229 * Managed kmalloc/kfree
230 */
231static void devm_kmalloc_release(struct udevice *dev, void *res)
232{
233 /* noop */
234}
235
236static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
237{
238 return res == data;
239}
240
241void *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
254void 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 Glass4da551b2019-12-29 21:19:11 -0700259 assert_noisy(!rc);
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900260}