blob: 5376118f12478c353763ee7753fe5d0693ceebab [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
Simon Glass76a64ab2019-12-29 21:19:27 -070020/** enum devres_phase - Shows where resource was allocated
21 *
22 * DEVRES_PHASE_BIND: In the bind() method
23 * DEVRES_PHASE_PROBE: In the probe() method
24 */
25enum devres_phase {
26 DEVRES_PHASE_BIND,
27 DEVRES_PHASE_PROBE,
28};
29
Masahiro Yamada8b15b162015-07-25 21:52:35 +090030/**
31 * struct devres - Bookkeeping info for managed device resource
32 * @entry: List to associate this structure with a device
33 * @release: Callback invoked when this resource is released
Simon Glass76a64ab2019-12-29 21:19:27 -070034 * @probe: Show where this resource was allocated
Masahiro Yamada8b15b162015-07-25 21:52:35 +090035 * @name: Name of release function
36 * @size: Size of resource data
37 * @data: Resource data
38 */
39struct devres {
40 struct list_head entry;
41 dr_release_t release;
Simon Glass76a64ab2019-12-29 21:19:27 -070042 enum devres_phase phase;
Masahiro Yamada8b15b162015-07-25 21:52:35 +090043#ifdef CONFIG_DEBUG_DEVRES
44 const char *name;
45 size_t size;
46#endif
47 unsigned long long data[];
48};
49
50#ifdef CONFIG_DEBUG_DEVRES
51static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
52{
53 dr->name = name;
54 dr->size = size;
55}
56
57static void devres_log(struct udevice *dev, struct devres *dr,
58 const char *op)
59{
Simon Glass8a8fe1b2019-12-29 21:19:24 -070060 log_debug("%s: DEVRES %3s %p %s (%lu bytes)\n", dev->name, op, dr,
61 dr->name, (unsigned long)dr->size);
Masahiro Yamada8b15b162015-07-25 21:52:35 +090062}
63#else /* CONFIG_DEBUG_DEVRES */
64#define set_node_dbginfo(dr, n, s) do {} while (0)
65#define devres_log(dev, dr, op) do {} while (0)
66#endif
67
68#if CONFIG_DEBUG_DEVRES
69void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
70 const char *name)
71#else
72void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
73#endif
74{
75 size_t tot_size = sizeof(struct devres) + size;
76 struct devres *dr;
77
78 dr = kmalloc(tot_size, gfp);
79 if (unlikely(!dr))
80 return NULL;
81
82 INIT_LIST_HEAD(&dr->entry);
83 dr->release = release;
84 set_node_dbginfo(dr, name, size);
85
86 return dr->data;
87}
88
89void devres_free(void *res)
90{
91 if (res) {
92 struct devres *dr = container_of(res, struct devres, data);
93
Simon Glass4da551b2019-12-29 21:19:11 -070094 assert_noisy(list_empty(&dr->entry));
Masahiro Yamada8b15b162015-07-25 21:52:35 +090095 kfree(dr);
96 }
97}
98
99void devres_add(struct udevice *dev, void *res)
100{
101 struct devres *dr = container_of(res, struct devres, data);
102
103 devres_log(dev, dr, "ADD");
Simon Glass4da551b2019-12-29 21:19:11 -0700104 assert_noisy(list_empty(&dr->entry));
Simon Glass76a64ab2019-12-29 21:19:27 -0700105 dr->phase = dev->flags & DM_FLAG_BOUND ? DEVRES_PHASE_PROBE :
106 DEVRES_PHASE_BIND;
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900107 list_add_tail(&dr->entry, &dev->devres_head);
108}
109
110void *devres_find(struct udevice *dev, dr_release_t release,
111 dr_match_t match, void *match_data)
112{
113 struct devres *dr;
114
115 list_for_each_entry_reverse(dr, &dev->devres_head, entry) {
116 if (dr->release != release)
117 continue;
118 if (match && !match(dev, dr->data, match_data))
119 continue;
120 return dr->data;
121 }
122
123 return NULL;
124}
125
126void *devres_get(struct udevice *dev, void *new_res,
127 dr_match_t match, void *match_data)
128{
129 struct devres *new_dr = container_of(new_res, struct devres, data);
130 void *res;
131
132 res = devres_find(dev, new_dr->release, match, match_data);
133 if (!res) {
134 devres_add(dev, new_res);
135 res = new_res;
136 new_res = NULL;
137 }
138 devres_free(new_res);
139
140 return res;
141}
142
143void *devres_remove(struct udevice *dev, dr_release_t release,
144 dr_match_t match, void *match_data)
145{
146 void *res;
147
148 res = devres_find(dev, release, match, match_data);
149 if (res) {
150 struct devres *dr = container_of(res, struct devres, data);
151
152 list_del_init(&dr->entry);
153 devres_log(dev, dr, "REM");
154 }
155
156 return res;
157}
158
159int devres_destroy(struct udevice *dev, dr_release_t release,
160 dr_match_t match, void *match_data)
161{
162 void *res;
163
164 res = devres_remove(dev, release, match, match_data);
165 if (unlikely(!res))
166 return -ENOENT;
167
168 devres_free(res);
169 return 0;
170}
171
172int devres_release(struct udevice *dev, dr_release_t release,
173 dr_match_t match, void *match_data)
174{
175 void *res;
176
177 res = devres_remove(dev, release, match, match_data);
178 if (unlikely(!res))
179 return -ENOENT;
180
181 (*release)(dev, res);
182 devres_free(res);
183 return 0;
184}
185
186static void release_nodes(struct udevice *dev, struct list_head *head,
187 bool probe_only)
188{
189 struct devres *dr, *tmp;
190
191 list_for_each_entry_safe_reverse(dr, tmp, head, entry) {
Simon Glass76a64ab2019-12-29 21:19:27 -0700192 if (probe_only && dr->phase != DEVRES_PHASE_PROBE)
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900193 break;
194 devres_log(dev, dr, "REL");
195 dr->release(dev, dr->data);
196 list_del(&dr->entry);
197 kfree(dr);
198 }
199}
200
201void devres_release_probe(struct udevice *dev)
202{
203 release_nodes(dev, &dev->devres_head, true);
204}
205
206void devres_release_all(struct udevice *dev)
207{
208 release_nodes(dev, &dev->devres_head, false);
209}
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900210
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900211#ifdef CONFIG_DEBUG_DEVRES
212static void dump_resources(struct udevice *dev, int depth)
213{
214 struct devres *dr;
215 struct udevice *child;
216
217 printf("- %s\n", dev->name);
218
219 list_for_each_entry(dr, &dev->devres_head, entry)
220 printf(" %p (%lu byte) %s %s\n", dr,
221 (unsigned long)dr->size, dr->name,
Simon Glass76a64ab2019-12-29 21:19:27 -0700222 dr->phase == DEVRES_PHASE_PROBE ? "PROBE" : "BIND");
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900223
224 list_for_each_entry(child, &dev->child_head, sibling_node)
225 dump_resources(child, depth + 1);
226}
227
228void dm_dump_devres(void)
229{
230 struct udevice *root;
231
232 root = dm_root();
233 if (root)
234 dump_resources(root, 0);
235}
Simon Glass323ff9f2019-12-29 21:19:26 -0700236
237void devres_get_stats(const struct udevice *dev, struct devres_stats *stats)
238{
239 struct devres *dr;
240
241 stats->allocs = 0;
242 stats->total_size = 0;
243 list_for_each_entry(dr, &dev->devres_head, entry) {
244 stats->allocs++;
245 stats->total_size += dr->size;
246 }
247}
248
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900249#endif
250
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900251/*
252 * Managed kmalloc/kfree
253 */
254static void devm_kmalloc_release(struct udevice *dev, void *res)
255{
256 /* noop */
257}
258
259static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
260{
261 return res == data;
262}
263
264void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
265{
266 void *data;
267
268 data = _devres_alloc(devm_kmalloc_release, size, gfp);
269 if (unlikely(!data))
270 return NULL;
271
272 devres_add(dev, data);
273
274 return data;
275}
276
277void devm_kfree(struct udevice *dev, void *p)
278{
279 int rc;
280
281 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
Simon Glass4da551b2019-12-29 21:19:11 -0700282 assert_noisy(!rc);
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900283}