blob: 457e1309c54e43fac2737a92b4c98ece84b03c75 [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>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Masahiro Yamada8b15b162015-07-25 21:52:35 +090014#include <linux/compat.h>
15#include <linux/kernel.h>
16#include <linux/list.h>
17#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <dm/devres.h>
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +090019#include <dm/root.h>
20#include <dm/util.h>
Masahiro Yamada8b15b162015-07-25 21:52:35 +090021
Simon Glass76a64ab2019-12-29 21:19:27 -070022/** enum devres_phase - Shows where resource was allocated
23 *
24 * DEVRES_PHASE_BIND: In the bind() method
Simon Glass0fe15372019-12-29 21:19:28 -070025 * DEVRES_PHASE_OFDATA: In the ofdata_to_platdata() method
Simon Glass76a64ab2019-12-29 21:19:27 -070026 * DEVRES_PHASE_PROBE: In the probe() method
27 */
28enum devres_phase {
29 DEVRES_PHASE_BIND,
Simon Glass0fe15372019-12-29 21:19:28 -070030 DEVRES_PHASE_OFDATA,
Simon Glass76a64ab2019-12-29 21:19:27 -070031 DEVRES_PHASE_PROBE,
32};
33
Masahiro Yamada8b15b162015-07-25 21:52:35 +090034/**
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
Simon Glass76a64ab2019-12-29 21:19:27 -070038 * @probe: Show where this resource was allocated
Masahiro Yamada8b15b162015-07-25 21:52:35 +090039 * @name: Name of release function
40 * @size: Size of resource data
41 * @data: Resource data
42 */
43struct devres {
44 struct list_head entry;
45 dr_release_t release;
Simon Glass76a64ab2019-12-29 21:19:27 -070046 enum devres_phase phase;
Masahiro Yamada8b15b162015-07-25 21:52:35 +090047#ifdef CONFIG_DEBUG_DEVRES
48 const char *name;
49 size_t size;
50#endif
51 unsigned long long data[];
52};
53
54#ifdef CONFIG_DEBUG_DEVRES
55static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)
56{
57 dr->name = name;
58 dr->size = size;
59}
60
61static void devres_log(struct udevice *dev, struct devres *dr,
62 const char *op)
63{
Simon Glass8a8fe1b2019-12-29 21:19:24 -070064 log_debug("%s: DEVRES %3s %p %s (%lu bytes)\n", dev->name, op, dr,
65 dr->name, (unsigned long)dr->size);
Masahiro Yamada8b15b162015-07-25 21:52:35 +090066}
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)
70#endif
71
72#if CONFIG_DEBUG_DEVRES
73void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
74 const char *name)
75#else
76void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
77#endif
78{
79 size_t tot_size = sizeof(struct devres) + size;
80 struct devres *dr;
81
82 dr = kmalloc(tot_size, gfp);
83 if (unlikely(!dr))
84 return NULL;
85
86 INIT_LIST_HEAD(&dr->entry);
87 dr->release = release;
88 set_node_dbginfo(dr, name, size);
89
90 return dr->data;
91}
92
93void devres_free(void *res)
94{
95 if (res) {
96 struct devres *dr = container_of(res, struct devres, data);
97
Simon Glass4da551b2019-12-29 21:19:11 -070098 assert_noisy(list_empty(&dr->entry));
Masahiro Yamada8b15b162015-07-25 21:52:35 +090099 kfree(dr);
100 }
101}
102
103void devres_add(struct udevice *dev, void *res)
104{
105 struct devres *dr = container_of(res, struct devres, data);
106
107 devres_log(dev, dr, "ADD");
Simon Glass4da551b2019-12-29 21:19:11 -0700108 assert_noisy(list_empty(&dr->entry));
Simon Glass0fe15372019-12-29 21:19:28 -0700109 if (dev->flags & DM_FLAG_PLATDATA_VALID)
110 dr->phase = DEVRES_PHASE_PROBE;
111 else if (dev->flags & DM_FLAG_BOUND)
112 dr->phase = DEVRES_PHASE_OFDATA;
113 else
114 dr->phase = DEVRES_PHASE_BIND;
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900115 list_add_tail(&dr->entry, &dev->devres_head);
116}
117
118void *devres_find(struct udevice *dev, dr_release_t release,
119 dr_match_t match, void *match_data)
120{
121 struct devres *dr;
122
123 list_for_each_entry_reverse(dr, &dev->devres_head, entry) {
124 if (dr->release != release)
125 continue;
126 if (match && !match(dev, dr->data, match_data))
127 continue;
128 return dr->data;
129 }
130
131 return NULL;
132}
133
134void *devres_get(struct udevice *dev, void *new_res,
135 dr_match_t match, void *match_data)
136{
137 struct devres *new_dr = container_of(new_res, struct devres, data);
138 void *res;
139
140 res = devres_find(dev, new_dr->release, match, match_data);
141 if (!res) {
142 devres_add(dev, new_res);
143 res = new_res;
144 new_res = NULL;
145 }
146 devres_free(new_res);
147
148 return res;
149}
150
151void *devres_remove(struct udevice *dev, dr_release_t release,
152 dr_match_t match, void *match_data)
153{
154 void *res;
155
156 res = devres_find(dev, release, match, match_data);
157 if (res) {
158 struct devres *dr = container_of(res, struct devres, data);
159
160 list_del_init(&dr->entry);
161 devres_log(dev, dr, "REM");
162 }
163
164 return res;
165}
166
167int devres_destroy(struct udevice *dev, dr_release_t release,
168 dr_match_t match, void *match_data)
169{
170 void *res;
171
172 res = devres_remove(dev, release, match, match_data);
173 if (unlikely(!res))
174 return -ENOENT;
175
176 devres_free(res);
177 return 0;
178}
179
180int devres_release(struct udevice *dev, dr_release_t release,
181 dr_match_t match, void *match_data)
182{
183 void *res;
184
185 res = devres_remove(dev, release, match, match_data);
186 if (unlikely(!res))
187 return -ENOENT;
188
189 (*release)(dev, res);
190 devres_free(res);
191 return 0;
192}
193
194static void release_nodes(struct udevice *dev, struct list_head *head,
Simon Glass0fe15372019-12-29 21:19:28 -0700195 bool probe_and_ofdata_only)
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900196{
197 struct devres *dr, *tmp;
198
199 list_for_each_entry_safe_reverse(dr, tmp, head, entry) {
Simon Glass0fe15372019-12-29 21:19:28 -0700200 if (probe_and_ofdata_only && dr->phase == DEVRES_PHASE_BIND)
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900201 break;
202 devres_log(dev, dr, "REL");
203 dr->release(dev, dr->data);
204 list_del(&dr->entry);
205 kfree(dr);
206 }
207}
208
209void devres_release_probe(struct udevice *dev)
210{
211 release_nodes(dev, &dev->devres_head, true);
212}
213
214void devres_release_all(struct udevice *dev)
215{
216 release_nodes(dev, &dev->devres_head, false);
217}
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900218
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900219#ifdef CONFIG_DEBUG_DEVRES
Simon Glass0fe15372019-12-29 21:19:28 -0700220static char *const devres_phase_name[] = {"BIND", "OFDATA", "PROBE"};
221
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900222static void dump_resources(struct udevice *dev, int depth)
223{
224 struct devres *dr;
225 struct udevice *child;
226
227 printf("- %s\n", dev->name);
228
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,
Simon Glass0fe15372019-12-29 21:19:28 -0700232 devres_phase_name[dr->phase]);
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900233
234 list_for_each_entry(child, &dev->child_head, sibling_node)
235 dump_resources(child, depth + 1);
236}
237
238void dm_dump_devres(void)
239{
240 struct udevice *root;
241
242 root = dm_root();
243 if (root)
244 dump_resources(root, 0);
245}
Simon Glass323ff9f2019-12-29 21:19:26 -0700246
247void devres_get_stats(const struct udevice *dev, struct devres_stats *stats)
248{
249 struct devres *dr;
250
251 stats->allocs = 0;
252 stats->total_size = 0;
253 list_for_each_entry(dr, &dev->devres_head, entry) {
254 stats->allocs++;
255 stats->total_size += dr->size;
256 }
257}
258
Masahiro Yamadaf929c0b2015-07-25 21:52:38 +0900259#endif
260
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900261/*
262 * Managed kmalloc/kfree
263 */
264static void devm_kmalloc_release(struct udevice *dev, void *res)
265{
266 /* noop */
267}
268
269static int devm_kmalloc_match(struct udevice *dev, void *res, void *data)
270{
271 return res == data;
272}
273
274void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
275{
276 void *data;
277
278 data = _devres_alloc(devm_kmalloc_release, size, gfp);
279 if (unlikely(!data))
280 return NULL;
281
282 devres_add(dev, data);
283
284 return data;
285}
286
287void devm_kfree(struct udevice *dev, void *p)
288{
289 int rc;
290
291 rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p);
Simon Glass4da551b2019-12-29 21:19:11 -0700292 assert_noisy(!rc);
Masahiro Yamadab475e1f2015-07-25 21:52:36 +0900293}