]>
Commit | Line | Data |
---|---|---|
9ac7849e TH |
1 | /* |
2 | * drivers/base/devres.c - device resource management | |
3 | * | |
4 | * Copyright (c) 2006 SUSE Linux Products GmbH | |
5 | * Copyright (c) 2006 Tejun Heo <teheo@suse.de> | |
6 | * | |
7 | * This file is released under the GPLv2. | |
8 | */ | |
9 | ||
10 | #include <linux/device.h> | |
11 | #include <linux/module.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
9ac7849e | 13 | |
2a013455 AB |
14 | #include "base.h" |
15 | ||
9ac7849e TH |
16 | struct devres_node { |
17 | struct list_head entry; | |
18 | dr_release_t release; | |
19 | #ifdef CONFIG_DEBUG_DEVRES | |
20 | const char *name; | |
21 | size_t size; | |
22 | #endif | |
23 | }; | |
24 | ||
25 | struct devres { | |
26 | struct devres_node node; | |
27 | /* -- 3 pointers */ | |
28 | unsigned long long data[]; /* guarantee ull alignment */ | |
29 | }; | |
30 | ||
31 | struct devres_group { | |
32 | struct devres_node node[2]; | |
33 | void *id; | |
34 | int color; | |
35 | /* -- 8 pointers */ | |
36 | }; | |
37 | ||
38 | #ifdef CONFIG_DEBUG_DEVRES | |
39 | static int log_devres = 0; | |
40 | module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR); | |
41 | ||
42 | static void set_node_dbginfo(struct devres_node *node, const char *name, | |
43 | size_t size) | |
44 | { | |
45 | node->name = name; | |
46 | node->size = size; | |
47 | } | |
48 | ||
49 | static void devres_log(struct device *dev, struct devres_node *node, | |
50 | const char *op) | |
51 | { | |
52 | if (unlikely(log_devres)) | |
a369a7eb JP |
53 | dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n", |
54 | op, node, node->name, (unsigned long)node->size); | |
9ac7849e TH |
55 | } |
56 | #else /* CONFIG_DEBUG_DEVRES */ | |
57 | #define set_node_dbginfo(node, n, s) do {} while (0) | |
58 | #define devres_log(dev, node, op) do {} while (0) | |
59 | #endif /* CONFIG_DEBUG_DEVRES */ | |
60 | ||
61 | /* | |
62 | * Release functions for devres group. These callbacks are used only | |
63 | * for identification. | |
64 | */ | |
65 | static void group_open_release(struct device *dev, void *res) | |
66 | { | |
67 | /* noop */ | |
68 | } | |
69 | ||
70 | static void group_close_release(struct device *dev, void *res) | |
71 | { | |
72 | /* noop */ | |
73 | } | |
74 | ||
75 | static struct devres_group * node_to_group(struct devres_node *node) | |
76 | { | |
77 | if (node->release == &group_open_release) | |
78 | return container_of(node, struct devres_group, node[0]); | |
79 | if (node->release == &group_close_release) | |
80 | return container_of(node, struct devres_group, node[1]); | |
81 | return NULL; | |
82 | } | |
83 | ||
84 | static __always_inline struct devres * alloc_dr(dr_release_t release, | |
85 | size_t size, gfp_t gfp) | |
86 | { | |
87 | size_t tot_size = sizeof(struct devres) + size; | |
88 | struct devres *dr; | |
89 | ||
90 | dr = kmalloc_track_caller(tot_size, gfp); | |
91 | if (unlikely(!dr)) | |
92 | return NULL; | |
93 | ||
94 | memset(dr, 0, tot_size); | |
95 | INIT_LIST_HEAD(&dr->node.entry); | |
96 | dr->node.release = release; | |
97 | return dr; | |
98 | } | |
99 | ||
100 | static void add_dr(struct device *dev, struct devres_node *node) | |
101 | { | |
102 | devres_log(dev, node, "ADD"); | |
103 | BUG_ON(!list_empty(&node->entry)); | |
104 | list_add_tail(&node->entry, &dev->devres_head); | |
105 | } | |
106 | ||
9ac7849e TH |
107 | #ifdef CONFIG_DEBUG_DEVRES |
108 | void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp, | |
109 | const char *name) | |
110 | { | |
111 | struct devres *dr; | |
112 | ||
113 | dr = alloc_dr(release, size, gfp); | |
114 | if (unlikely(!dr)) | |
115 | return NULL; | |
116 | set_node_dbginfo(&dr->node, name, size); | |
117 | return dr->data; | |
118 | } | |
119 | EXPORT_SYMBOL_GPL(__devres_alloc); | |
120 | #else | |
d3e6975e RD |
121 | /** |
122 | * devres_alloc - Allocate device resource data | |
123 | * @release: Release function devres will be associated with | |
124 | * @size: Allocation size | |
125 | * @gfp: Allocation flags | |
126 | * | |
127 | * Allocate devres of @size bytes. The allocated area is zeroed, then | |
128 | * associated with @release. The returned pointer can be passed to | |
129 | * other devres_*() functions. | |
130 | * | |
131 | * RETURNS: | |
132 | * Pointer to allocated devres on success, NULL on failure. | |
133 | */ | |
9ac7849e TH |
134 | void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp) |
135 | { | |
136 | struct devres *dr; | |
137 | ||
138 | dr = alloc_dr(release, size, gfp); | |
139 | if (unlikely(!dr)) | |
140 | return NULL; | |
141 | return dr->data; | |
142 | } | |
143 | EXPORT_SYMBOL_GPL(devres_alloc); | |
144 | #endif | |
145 | ||
bddb1b90 ML |
146 | /** |
147 | * devres_for_each_res - Resource iterator | |
148 | * @dev: Device to iterate resource from | |
149 | * @release: Look for resources associated with this release function | |
150 | * @match: Match function (optional) | |
151 | * @match_data: Data for the match function | |
152 | * @fn: Function to be called for each matched resource. | |
153 | * @data: Data for @fn, the 3rd parameter of @fn | |
154 | * | |
155 | * Call @fn for each devres of @dev which is associated with @release | |
156 | * and for which @match returns 1. | |
157 | * | |
158 | * RETURNS: | |
159 | * void | |
160 | */ | |
161 | void devres_for_each_res(struct device *dev, dr_release_t release, | |
162 | dr_match_t match, void *match_data, | |
163 | void (*fn)(struct device *, void *, void *), | |
164 | void *data) | |
165 | { | |
166 | struct devres_node *node; | |
167 | struct devres_node *tmp; | |
168 | unsigned long flags; | |
169 | ||
170 | if (!fn) | |
171 | return; | |
172 | ||
173 | spin_lock_irqsave(&dev->devres_lock, flags); | |
174 | list_for_each_entry_safe_reverse(node, tmp, | |
175 | &dev->devres_head, entry) { | |
176 | struct devres *dr = container_of(node, struct devres, node); | |
177 | ||
178 | if (node->release != release) | |
179 | continue; | |
180 | if (match && !match(dev, dr->data, match_data)) | |
181 | continue; | |
182 | fn(dev, dr->data, data); | |
183 | } | |
184 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
185 | } | |
186 | EXPORT_SYMBOL_GPL(devres_for_each_res); | |
187 | ||
9ac7849e TH |
188 | /** |
189 | * devres_free - Free device resource data | |
190 | * @res: Pointer to devres data to free | |
191 | * | |
192 | * Free devres created with devres_alloc(). | |
193 | */ | |
194 | void devres_free(void *res) | |
195 | { | |
196 | if (res) { | |
197 | struct devres *dr = container_of(res, struct devres, data); | |
198 | ||
199 | BUG_ON(!list_empty(&dr->node.entry)); | |
200 | kfree(dr); | |
201 | } | |
202 | } | |
203 | EXPORT_SYMBOL_GPL(devres_free); | |
204 | ||
205 | /** | |
206 | * devres_add - Register device resource | |
207 | * @dev: Device to add resource to | |
208 | * @res: Resource to register | |
209 | * | |
210 | * Register devres @res to @dev. @res should have been allocated | |
211 | * using devres_alloc(). On driver detach, the associated release | |
212 | * function will be invoked and devres will be freed automatically. | |
213 | */ | |
214 | void devres_add(struct device *dev, void *res) | |
215 | { | |
216 | struct devres *dr = container_of(res, struct devres, data); | |
217 | unsigned long flags; | |
218 | ||
219 | spin_lock_irqsave(&dev->devres_lock, flags); | |
220 | add_dr(dev, &dr->node); | |
221 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
222 | } | |
223 | EXPORT_SYMBOL_GPL(devres_add); | |
224 | ||
225 | static struct devres *find_dr(struct device *dev, dr_release_t release, | |
226 | dr_match_t match, void *match_data) | |
227 | { | |
228 | struct devres_node *node; | |
229 | ||
230 | list_for_each_entry_reverse(node, &dev->devres_head, entry) { | |
231 | struct devres *dr = container_of(node, struct devres, node); | |
232 | ||
233 | if (node->release != release) | |
234 | continue; | |
235 | if (match && !match(dev, dr->data, match_data)) | |
236 | continue; | |
237 | return dr; | |
238 | } | |
239 | ||
240 | return NULL; | |
241 | } | |
242 | ||
243 | /** | |
244 | * devres_find - Find device resource | |
245 | * @dev: Device to lookup resource from | |
246 | * @release: Look for resources associated with this release function | |
247 | * @match: Match function (optional) | |
248 | * @match_data: Data for the match function | |
249 | * | |
250 | * Find the latest devres of @dev which is associated with @release | |
251 | * and for which @match returns 1. If @match is NULL, it's considered | |
252 | * to match all. | |
253 | * | |
254 | * RETURNS: | |
255 | * Pointer to found devres, NULL if not found. | |
256 | */ | |
257 | void * devres_find(struct device *dev, dr_release_t release, | |
258 | dr_match_t match, void *match_data) | |
259 | { | |
260 | struct devres *dr; | |
261 | unsigned long flags; | |
262 | ||
263 | spin_lock_irqsave(&dev->devres_lock, flags); | |
264 | dr = find_dr(dev, release, match, match_data); | |
265 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
266 | ||
267 | if (dr) | |
268 | return dr->data; | |
269 | return NULL; | |
270 | } | |
271 | EXPORT_SYMBOL_GPL(devres_find); | |
272 | ||
273 | /** | |
274 | * devres_get - Find devres, if non-existent, add one atomically | |
275 | * @dev: Device to lookup or add devres for | |
276 | * @new_res: Pointer to new initialized devres to add if not found | |
277 | * @match: Match function (optional) | |
278 | * @match_data: Data for the match function | |
279 | * | |
280 | * Find the latest devres of @dev which has the same release function | |
281 | * as @new_res and for which @match return 1. If found, @new_res is | |
282 | * freed; otherwise, @new_res is added atomically. | |
283 | * | |
284 | * RETURNS: | |
285 | * Pointer to found or added devres. | |
286 | */ | |
287 | void * devres_get(struct device *dev, void *new_res, | |
288 | dr_match_t match, void *match_data) | |
289 | { | |
290 | struct devres *new_dr = container_of(new_res, struct devres, data); | |
291 | struct devres *dr; | |
292 | unsigned long flags; | |
293 | ||
294 | spin_lock_irqsave(&dev->devres_lock, flags); | |
295 | dr = find_dr(dev, new_dr->node.release, match, match_data); | |
296 | if (!dr) { | |
297 | add_dr(dev, &new_dr->node); | |
298 | dr = new_dr; | |
299 | new_dr = NULL; | |
300 | } | |
301 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
302 | devres_free(new_dr); | |
303 | ||
304 | return dr->data; | |
305 | } | |
306 | EXPORT_SYMBOL_GPL(devres_get); | |
307 | ||
308 | /** | |
309 | * devres_remove - Find a device resource and remove it | |
310 | * @dev: Device to find resource from | |
311 | * @release: Look for resources associated with this release function | |
312 | * @match: Match function (optional) | |
313 | * @match_data: Data for the match function | |
314 | * | |
315 | * Find the latest devres of @dev associated with @release and for | |
316 | * which @match returns 1. If @match is NULL, it's considered to | |
317 | * match all. If found, the resource is removed atomically and | |
318 | * returned. | |
319 | * | |
320 | * RETURNS: | |
321 | * Pointer to removed devres on success, NULL if not found. | |
322 | */ | |
323 | void * devres_remove(struct device *dev, dr_release_t release, | |
324 | dr_match_t match, void *match_data) | |
325 | { | |
326 | struct devres *dr; | |
327 | unsigned long flags; | |
328 | ||
329 | spin_lock_irqsave(&dev->devres_lock, flags); | |
330 | dr = find_dr(dev, release, match, match_data); | |
331 | if (dr) { | |
332 | list_del_init(&dr->node.entry); | |
333 | devres_log(dev, &dr->node, "REM"); | |
334 | } | |
335 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
336 | ||
337 | if (dr) | |
338 | return dr->data; | |
339 | return NULL; | |
340 | } | |
341 | EXPORT_SYMBOL_GPL(devres_remove); | |
342 | ||
343 | /** | |
344 | * devres_destroy - Find a device resource and destroy it | |
345 | * @dev: Device to find resource from | |
346 | * @release: Look for resources associated with this release function | |
347 | * @match: Match function (optional) | |
348 | * @match_data: Data for the match function | |
349 | * | |
350 | * Find the latest devres of @dev associated with @release and for | |
351 | * which @match returns 1. If @match is NULL, it's considered to | |
352 | * match all. If found, the resource is removed atomically and freed. | |
353 | * | |
698cd2dd MB |
354 | * Note that the release function for the resource will not be called, |
355 | * only the devres-allocated data will be freed. The caller becomes | |
356 | * responsible for freeing any other data. | |
357 | * | |
9ac7849e TH |
358 | * RETURNS: |
359 | * 0 if devres is found and freed, -ENOENT if not found. | |
360 | */ | |
361 | int devres_destroy(struct device *dev, dr_release_t release, | |
362 | dr_match_t match, void *match_data) | |
363 | { | |
364 | void *res; | |
365 | ||
366 | res = devres_remove(dev, release, match, match_data); | |
367 | if (unlikely(!res)) | |
368 | return -ENOENT; | |
369 | ||
370 | devres_free(res); | |
371 | return 0; | |
372 | } | |
373 | EXPORT_SYMBOL_GPL(devres_destroy); | |
374 | ||
d926d0e4 MB |
375 | |
376 | /** | |
377 | * devres_release - Find a device resource and destroy it, calling release | |
378 | * @dev: Device to find resource from | |
379 | * @release: Look for resources associated with this release function | |
380 | * @match: Match function (optional) | |
381 | * @match_data: Data for the match function | |
382 | * | |
383 | * Find the latest devres of @dev associated with @release and for | |
384 | * which @match returns 1. If @match is NULL, it's considered to | |
385 | * match all. If found, the resource is removed atomically, the | |
386 | * release function called and the resource freed. | |
387 | * | |
388 | * RETURNS: | |
389 | * 0 if devres is found and freed, -ENOENT if not found. | |
390 | */ | |
391 | int devres_release(struct device *dev, dr_release_t release, | |
392 | dr_match_t match, void *match_data) | |
393 | { | |
394 | void *res; | |
395 | ||
396 | res = devres_remove(dev, release, match, match_data); | |
397 | if (unlikely(!res)) | |
398 | return -ENOENT; | |
399 | ||
400 | (*release)(dev, res); | |
401 | devres_free(res); | |
402 | return 0; | |
403 | } | |
404 | EXPORT_SYMBOL_GPL(devres_release); | |
405 | ||
9ac7849e TH |
406 | static int remove_nodes(struct device *dev, |
407 | struct list_head *first, struct list_head *end, | |
408 | struct list_head *todo) | |
409 | { | |
410 | int cnt = 0, nr_groups = 0; | |
411 | struct list_head *cur; | |
412 | ||
413 | /* First pass - move normal devres entries to @todo and clear | |
414 | * devres_group colors. | |
415 | */ | |
416 | cur = first; | |
417 | while (cur != end) { | |
418 | struct devres_node *node; | |
419 | struct devres_group *grp; | |
420 | ||
421 | node = list_entry(cur, struct devres_node, entry); | |
422 | cur = cur->next; | |
423 | ||
424 | grp = node_to_group(node); | |
425 | if (grp) { | |
426 | /* clear color of group markers in the first pass */ | |
427 | grp->color = 0; | |
428 | nr_groups++; | |
429 | } else { | |
430 | /* regular devres entry */ | |
431 | if (&node->entry == first) | |
432 | first = first->next; | |
433 | list_move_tail(&node->entry, todo); | |
434 | cnt++; | |
435 | } | |
436 | } | |
437 | ||
438 | if (!nr_groups) | |
439 | return cnt; | |
440 | ||
441 | /* Second pass - Scan groups and color them. A group gets | |
442 | * color value of two iff the group is wholly contained in | |
443 | * [cur, end). That is, for a closed group, both opening and | |
444 | * closing markers should be in the range, while just the | |
445 | * opening marker is enough for an open group. | |
446 | */ | |
447 | cur = first; | |
448 | while (cur != end) { | |
449 | struct devres_node *node; | |
450 | struct devres_group *grp; | |
451 | ||
452 | node = list_entry(cur, struct devres_node, entry); | |
453 | cur = cur->next; | |
454 | ||
455 | grp = node_to_group(node); | |
456 | BUG_ON(!grp || list_empty(&grp->node[0].entry)); | |
457 | ||
458 | grp->color++; | |
459 | if (list_empty(&grp->node[1].entry)) | |
460 | grp->color++; | |
461 | ||
462 | BUG_ON(grp->color <= 0 || grp->color > 2); | |
463 | if (grp->color == 2) { | |
464 | /* No need to update cur or end. The removed | |
465 | * nodes are always before both. | |
466 | */ | |
467 | list_move_tail(&grp->node[0].entry, todo); | |
468 | list_del_init(&grp->node[1].entry); | |
469 | } | |
470 | } | |
471 | ||
472 | return cnt; | |
473 | } | |
474 | ||
475 | static int release_nodes(struct device *dev, struct list_head *first, | |
476 | struct list_head *end, unsigned long flags) | |
86ec67fd | 477 | __releases(&dev->devres_lock) |
9ac7849e TH |
478 | { |
479 | LIST_HEAD(todo); | |
480 | int cnt; | |
481 | struct devres *dr, *tmp; | |
482 | ||
483 | cnt = remove_nodes(dev, first, end, &todo); | |
484 | ||
485 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
486 | ||
487 | /* Release. Note that both devres and devres_group are | |
488 | * handled as devres in the following loop. This is safe. | |
489 | */ | |
490 | list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) { | |
491 | devres_log(dev, &dr->node, "REL"); | |
492 | dr->node.release(dev, dr->data); | |
493 | kfree(dr); | |
494 | } | |
495 | ||
496 | return cnt; | |
497 | } | |
498 | ||
499 | /** | |
d3e6975e | 500 | * devres_release_all - Release all managed resources |
9ac7849e TH |
501 | * @dev: Device to release resources for |
502 | * | |
503 | * Release all resources associated with @dev. This function is | |
504 | * called on driver detach. | |
505 | */ | |
506 | int devres_release_all(struct device *dev) | |
507 | { | |
508 | unsigned long flags; | |
509 | ||
eb8d3c60 BH |
510 | /* Looks like an uninitialized device structure */ |
511 | if (WARN_ON(dev->devres_head.next == NULL)) | |
512 | return -ENODEV; | |
9ac7849e TH |
513 | spin_lock_irqsave(&dev->devres_lock, flags); |
514 | return release_nodes(dev, dev->devres_head.next, &dev->devres_head, | |
515 | flags); | |
516 | } | |
517 | ||
518 | /** | |
519 | * devres_open_group - Open a new devres group | |
520 | * @dev: Device to open devres group for | |
521 | * @id: Separator ID | |
522 | * @gfp: Allocation flags | |
523 | * | |
524 | * Open a new devres group for @dev with @id. For @id, using a | |
525 | * pointer to an object which won't be used for another group is | |
526 | * recommended. If @id is NULL, address-wise unique ID is created. | |
527 | * | |
528 | * RETURNS: | |
529 | * ID of the new group, NULL on failure. | |
530 | */ | |
531 | void * devres_open_group(struct device *dev, void *id, gfp_t gfp) | |
532 | { | |
533 | struct devres_group *grp; | |
534 | unsigned long flags; | |
535 | ||
536 | grp = kmalloc(sizeof(*grp), gfp); | |
537 | if (unlikely(!grp)) | |
538 | return NULL; | |
539 | ||
540 | grp->node[0].release = &group_open_release; | |
541 | grp->node[1].release = &group_close_release; | |
542 | INIT_LIST_HEAD(&grp->node[0].entry); | |
543 | INIT_LIST_HEAD(&grp->node[1].entry); | |
544 | set_node_dbginfo(&grp->node[0], "grp<", 0); | |
545 | set_node_dbginfo(&grp->node[1], "grp>", 0); | |
546 | grp->id = grp; | |
547 | if (id) | |
548 | grp->id = id; | |
549 | ||
550 | spin_lock_irqsave(&dev->devres_lock, flags); | |
551 | add_dr(dev, &grp->node[0]); | |
552 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
553 | return grp->id; | |
554 | } | |
555 | EXPORT_SYMBOL_GPL(devres_open_group); | |
556 | ||
557 | /* Find devres group with ID @id. If @id is NULL, look for the latest. */ | |
558 | static struct devres_group * find_group(struct device *dev, void *id) | |
559 | { | |
560 | struct devres_node *node; | |
561 | ||
562 | list_for_each_entry_reverse(node, &dev->devres_head, entry) { | |
563 | struct devres_group *grp; | |
564 | ||
565 | if (node->release != &group_open_release) | |
566 | continue; | |
567 | ||
568 | grp = container_of(node, struct devres_group, node[0]); | |
569 | ||
570 | if (id) { | |
571 | if (grp->id == id) | |
572 | return grp; | |
573 | } else if (list_empty(&grp->node[1].entry)) | |
574 | return grp; | |
575 | } | |
576 | ||
577 | return NULL; | |
578 | } | |
579 | ||
580 | /** | |
581 | * devres_close_group - Close a devres group | |
582 | * @dev: Device to close devres group for | |
583 | * @id: ID of target group, can be NULL | |
584 | * | |
585 | * Close the group identified by @id. If @id is NULL, the latest open | |
586 | * group is selected. | |
587 | */ | |
588 | void devres_close_group(struct device *dev, void *id) | |
589 | { | |
590 | struct devres_group *grp; | |
591 | unsigned long flags; | |
592 | ||
593 | spin_lock_irqsave(&dev->devres_lock, flags); | |
594 | ||
595 | grp = find_group(dev, id); | |
596 | if (grp) | |
597 | add_dr(dev, &grp->node[1]); | |
598 | else | |
599 | WARN_ON(1); | |
600 | ||
601 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
602 | } | |
603 | EXPORT_SYMBOL_GPL(devres_close_group); | |
604 | ||
605 | /** | |
606 | * devres_remove_group - Remove a devres group | |
607 | * @dev: Device to remove group for | |
608 | * @id: ID of target group, can be NULL | |
609 | * | |
610 | * Remove the group identified by @id. If @id is NULL, the latest | |
611 | * open group is selected. Note that removing a group doesn't affect | |
612 | * any other resources. | |
613 | */ | |
614 | void devres_remove_group(struct device *dev, void *id) | |
615 | { | |
616 | struct devres_group *grp; | |
617 | unsigned long flags; | |
618 | ||
619 | spin_lock_irqsave(&dev->devres_lock, flags); | |
620 | ||
621 | grp = find_group(dev, id); | |
622 | if (grp) { | |
623 | list_del_init(&grp->node[0].entry); | |
624 | list_del_init(&grp->node[1].entry); | |
625 | devres_log(dev, &grp->node[0], "REM"); | |
626 | } else | |
627 | WARN_ON(1); | |
628 | ||
629 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
630 | ||
631 | kfree(grp); | |
632 | } | |
633 | EXPORT_SYMBOL_GPL(devres_remove_group); | |
634 | ||
635 | /** | |
636 | * devres_release_group - Release resources in a devres group | |
637 | * @dev: Device to release group for | |
638 | * @id: ID of target group, can be NULL | |
639 | * | |
640 | * Release all resources in the group identified by @id. If @id is | |
641 | * NULL, the latest open group is selected. The selected group and | |
642 | * groups properly nested inside the selected group are removed. | |
643 | * | |
644 | * RETURNS: | |
645 | * The number of released non-group resources. | |
646 | */ | |
647 | int devres_release_group(struct device *dev, void *id) | |
648 | { | |
649 | struct devres_group *grp; | |
650 | unsigned long flags; | |
651 | int cnt = 0; | |
652 | ||
653 | spin_lock_irqsave(&dev->devres_lock, flags); | |
654 | ||
655 | grp = find_group(dev, id); | |
656 | if (grp) { | |
657 | struct list_head *first = &grp->node[0].entry; | |
658 | struct list_head *end = &dev->devres_head; | |
659 | ||
660 | if (!list_empty(&grp->node[1].entry)) | |
661 | end = grp->node[1].entry.next; | |
662 | ||
663 | cnt = release_nodes(dev, first, end, flags); | |
664 | } else { | |
665 | WARN_ON(1); | |
666 | spin_unlock_irqrestore(&dev->devres_lock, flags); | |
667 | } | |
668 | ||
669 | return cnt; | |
670 | } | |
671 | EXPORT_SYMBOL_GPL(devres_release_group); | |
672 | ||
d6b0c580 DT |
673 | /* |
674 | * Custom devres actions allow inserting a simple function call | |
675 | * into the teadown sequence. | |
676 | */ | |
677 | ||
678 | struct action_devres { | |
679 | void *data; | |
680 | void (*action)(void *); | |
681 | }; | |
682 | ||
683 | static int devm_action_match(struct device *dev, void *res, void *p) | |
684 | { | |
685 | struct action_devres *devres = res; | |
686 | struct action_devres *target = p; | |
687 | ||
688 | return devres->action == target->action && | |
689 | devres->data == target->data; | |
690 | } | |
691 | ||
692 | static void devm_action_release(struct device *dev, void *res) | |
693 | { | |
694 | struct action_devres *devres = res; | |
695 | ||
696 | devres->action(devres->data); | |
697 | } | |
698 | ||
699 | /** | |
700 | * devm_add_action() - add a custom action to list of managed resources | |
701 | * @dev: Device that owns the action | |
702 | * @action: Function that should be called | |
703 | * @data: Pointer to data passed to @action implementation | |
704 | * | |
705 | * This adds a custom action to the list of managed resources so that | |
706 | * it gets executed as part of standard resource unwinding. | |
707 | */ | |
708 | int devm_add_action(struct device *dev, void (*action)(void *), void *data) | |
709 | { | |
710 | struct action_devres *devres; | |
711 | ||
712 | devres = devres_alloc(devm_action_release, | |
713 | sizeof(struct action_devres), GFP_KERNEL); | |
714 | if (!devres) | |
715 | return -ENOMEM; | |
716 | ||
717 | devres->data = data; | |
718 | devres->action = action; | |
719 | ||
720 | devres_add(dev, devres); | |
721 | return 0; | |
722 | } | |
723 | EXPORT_SYMBOL_GPL(devm_add_action); | |
724 | ||
725 | /** | |
726 | * devm_remove_action() - removes previously added custom action | |
727 | * @dev: Device that owns the action | |
728 | * @action: Function implementing the action | |
729 | * @data: Pointer to data passed to @action implementation | |
730 | * | |
731 | * Removes instance of @action previously added by devm_add_action(). | |
732 | * Both action and data should match one of the existing entries. | |
733 | */ | |
734 | void devm_remove_action(struct device *dev, void (*action)(void *), void *data) | |
735 | { | |
736 | struct action_devres devres = { | |
737 | .data = data, | |
738 | .action = action, | |
739 | }; | |
740 | ||
741 | WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match, | |
742 | &devres)); | |
743 | ||
744 | } | |
745 | EXPORT_SYMBOL_GPL(devm_remove_action); | |
746 | ||
9ac7849e TH |
747 | /* |
748 | * Managed kzalloc/kfree | |
749 | */ | |
750 | static void devm_kzalloc_release(struct device *dev, void *res) | |
751 | { | |
752 | /* noop */ | |
753 | } | |
754 | ||
755 | static int devm_kzalloc_match(struct device *dev, void *res, void *data) | |
756 | { | |
757 | return res == data; | |
758 | } | |
759 | ||
760 | /** | |
d3e6975e | 761 | * devm_kzalloc - Resource-managed kzalloc |
9ac7849e TH |
762 | * @dev: Device to allocate memory for |
763 | * @size: Allocation size | |
764 | * @gfp: Allocation gfp flags | |
765 | * | |
766 | * Managed kzalloc. Memory allocated with this function is | |
767 | * automatically freed on driver detach. Like all other devres | |
768 | * resources, guaranteed alignment is unsigned long long. | |
769 | * | |
770 | * RETURNS: | |
771 | * Pointer to allocated memory on success, NULL on failure. | |
772 | */ | |
773 | void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) | |
774 | { | |
775 | struct devres *dr; | |
776 | ||
777 | /* use raw alloc_dr for kmalloc caller tracing */ | |
778 | dr = alloc_dr(devm_kzalloc_release, size, gfp); | |
779 | if (unlikely(!dr)) | |
780 | return NULL; | |
781 | ||
782 | set_node_dbginfo(&dr->node, "devm_kzalloc_release", size); | |
783 | devres_add(dev, dr->data); | |
784 | return dr->data; | |
785 | } | |
786 | EXPORT_SYMBOL_GPL(devm_kzalloc); | |
787 | ||
788 | /** | |
d3e6975e | 789 | * devm_kfree - Resource-managed kfree |
9ac7849e TH |
790 | * @dev: Device this memory belongs to |
791 | * @p: Memory to free | |
792 | * | |
92f1b851 | 793 | * Free memory allocated with devm_kzalloc(). |
9ac7849e TH |
794 | */ |
795 | void devm_kfree(struct device *dev, void *p) | |
796 | { | |
797 | int rc; | |
798 | ||
799 | rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p); | |
800 | WARN_ON(rc); | |
801 | } | |
802 | EXPORT_SYMBOL_GPL(devm_kfree); |