[MIPS] RTLX: Protect rtlx_{read,write} with mutex.
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / base / devres.c
blobe177c9533b6cc6654e2c8afd3567021672d0c51a
1 /*
2 * drivers/base/devres.c - device resource management
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
7 * This file is released under the GPLv2.
8 */
10 #include <linux/device.h>
11 #include <linux/module.h>
13 struct devres_node {
14 struct list_head entry;
15 dr_release_t release;
16 #ifdef CONFIG_DEBUG_DEVRES
17 const char *name;
18 size_t size;
19 #endif
22 struct devres {
23 struct devres_node node;
24 /* -- 3 pointers */
25 unsigned long long data[]; /* guarantee ull alignment */
28 struct devres_group {
29 struct devres_node node[2];
30 void *id;
31 int color;
32 /* -- 8 pointers */
35 #ifdef CONFIG_DEBUG_DEVRES
36 static int log_devres = 0;
37 module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
39 static void set_node_dbginfo(struct devres_node *node, const char *name,
40 size_t size)
42 node->name = name;
43 node->size = size;
46 static void devres_log(struct device *dev, struct devres_node *node,
47 const char *op)
49 if (unlikely(log_devres))
50 dev_printk(KERN_ERR, dev, "DEVRES %3s %p %s (%lu bytes)\n",
51 op, node, node->name, (unsigned long)node->size);
53 #else /* CONFIG_DEBUG_DEVRES */
54 #define set_node_dbginfo(node, n, s) do {} while (0)
55 #define devres_log(dev, node, op) do {} while (0)
56 #endif /* CONFIG_DEBUG_DEVRES */
59 * Release functions for devres group. These callbacks are used only
60 * for identification.
62 static void group_open_release(struct device *dev, void *res)
64 /* noop */
67 static void group_close_release(struct device *dev, void *res)
69 /* noop */
72 static struct devres_group * node_to_group(struct devres_node *node)
74 if (node->release == &group_open_release)
75 return container_of(node, struct devres_group, node[0]);
76 if (node->release == &group_close_release)
77 return container_of(node, struct devres_group, node[1]);
78 return NULL;
81 static __always_inline struct devres * alloc_dr(dr_release_t release,
82 size_t size, gfp_t gfp)
84 size_t tot_size = sizeof(struct devres) + size;
85 struct devres *dr;
87 dr = kmalloc_track_caller(tot_size, gfp);
88 if (unlikely(!dr))
89 return NULL;
91 memset(dr, 0, tot_size);
92 INIT_LIST_HEAD(&dr->node.entry);
93 dr->node.release = release;
94 return dr;
97 static void add_dr(struct device *dev, struct devres_node *node)
99 devres_log(dev, node, "ADD");
100 BUG_ON(!list_empty(&node->entry));
101 list_add_tail(&node->entry, &dev->devres_head);
105 * devres_alloc - Allocate device resource data
106 * @release: Release function devres will be associated with
107 * @size: Allocation size
108 * @gfp: Allocation flags
110 * allocate devres of @size bytes. The allocated area is zeroed, then
111 * associated with @release. The returned pointer can be passed to
112 * other devres_*() functions.
114 * RETURNS:
115 * Pointer to allocated devres on success, NULL on failure.
117 #ifdef CONFIG_DEBUG_DEVRES
118 void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
119 const char *name)
121 struct devres *dr;
123 dr = alloc_dr(release, size, gfp);
124 if (unlikely(!dr))
125 return NULL;
126 set_node_dbginfo(&dr->node, name, size);
127 return dr->data;
129 EXPORT_SYMBOL_GPL(__devres_alloc);
130 #else
131 void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
133 struct devres *dr;
135 dr = alloc_dr(release, size, gfp);
136 if (unlikely(!dr))
137 return NULL;
138 return dr->data;
140 EXPORT_SYMBOL_GPL(devres_alloc);
141 #endif
144 * devres_free - Free device resource data
145 * @res: Pointer to devres data to free
147 * Free devres created with devres_alloc().
149 void devres_free(void *res)
151 if (res) {
152 struct devres *dr = container_of(res, struct devres, data);
154 BUG_ON(!list_empty(&dr->node.entry));
155 kfree(dr);
158 EXPORT_SYMBOL_GPL(devres_free);
161 * devres_add - Register device resource
162 * @dev: Device to add resource to
163 * @res: Resource to register
165 * Register devres @res to @dev. @res should have been allocated
166 * using devres_alloc(). On driver detach, the associated release
167 * function will be invoked and devres will be freed automatically.
169 void devres_add(struct device *dev, void *res)
171 struct devres *dr = container_of(res, struct devres, data);
172 unsigned long flags;
174 spin_lock_irqsave(&dev->devres_lock, flags);
175 add_dr(dev, &dr->node);
176 spin_unlock_irqrestore(&dev->devres_lock, flags);
178 EXPORT_SYMBOL_GPL(devres_add);
180 static struct devres *find_dr(struct device *dev, dr_release_t release,
181 dr_match_t match, void *match_data)
183 struct devres_node *node;
185 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
186 struct devres *dr = container_of(node, struct devres, node);
188 if (node->release != release)
189 continue;
190 if (match && !match(dev, dr->data, match_data))
191 continue;
192 return dr;
195 return NULL;
199 * devres_find - Find device resource
200 * @dev: Device to lookup resource from
201 * @release: Look for resources associated with this release function
202 * @match: Match function (optional)
203 * @match_data: Data for the match function
205 * Find the latest devres of @dev which is associated with @release
206 * and for which @match returns 1. If @match is NULL, it's considered
207 * to match all.
209 * RETURNS:
210 * Pointer to found devres, NULL if not found.
212 void * devres_find(struct device *dev, dr_release_t release,
213 dr_match_t match, void *match_data)
215 struct devres *dr;
216 unsigned long flags;
218 spin_lock_irqsave(&dev->devres_lock, flags);
219 dr = find_dr(dev, release, match, match_data);
220 spin_unlock_irqrestore(&dev->devres_lock, flags);
222 if (dr)
223 return dr->data;
224 return NULL;
226 EXPORT_SYMBOL_GPL(devres_find);
229 * devres_get - Find devres, if non-existent, add one atomically
230 * @dev: Device to lookup or add devres for
231 * @new_res: Pointer to new initialized devres to add if not found
232 * @match: Match function (optional)
233 * @match_data: Data for the match function
235 * Find the latest devres of @dev which has the same release function
236 * as @new_res and for which @match return 1. If found, @new_res is
237 * freed; otherwise, @new_res is added atomically.
239 * RETURNS:
240 * Pointer to found or added devres.
242 void * devres_get(struct device *dev, void *new_res,
243 dr_match_t match, void *match_data)
245 struct devres *new_dr = container_of(new_res, struct devres, data);
246 struct devres *dr;
247 unsigned long flags;
249 spin_lock_irqsave(&dev->devres_lock, flags);
250 dr = find_dr(dev, new_dr->node.release, match, match_data);
251 if (!dr) {
252 add_dr(dev, &new_dr->node);
253 dr = new_dr;
254 new_dr = NULL;
256 spin_unlock_irqrestore(&dev->devres_lock, flags);
257 devres_free(new_dr);
259 return dr->data;
261 EXPORT_SYMBOL_GPL(devres_get);
264 * devres_remove - Find a device resource and remove it
265 * @dev: Device to find resource from
266 * @release: Look for resources associated with this release function
267 * @match: Match function (optional)
268 * @match_data: Data for the match function
270 * Find the latest devres of @dev associated with @release and for
271 * which @match returns 1. If @match is NULL, it's considered to
272 * match all. If found, the resource is removed atomically and
273 * returned.
275 * RETURNS:
276 * Pointer to removed devres on success, NULL if not found.
278 void * devres_remove(struct device *dev, dr_release_t release,
279 dr_match_t match, void *match_data)
281 struct devres *dr;
282 unsigned long flags;
284 spin_lock_irqsave(&dev->devres_lock, flags);
285 dr = find_dr(dev, release, match, match_data);
286 if (dr) {
287 list_del_init(&dr->node.entry);
288 devres_log(dev, &dr->node, "REM");
290 spin_unlock_irqrestore(&dev->devres_lock, flags);
292 if (dr)
293 return dr->data;
294 return NULL;
296 EXPORT_SYMBOL_GPL(devres_remove);
299 * devres_destroy - Find a device resource and destroy it
300 * @dev: Device to find resource from
301 * @release: Look for resources associated with this release function
302 * @match: Match function (optional)
303 * @match_data: Data for the match function
305 * Find the latest devres of @dev associated with @release and for
306 * which @match returns 1. If @match is NULL, it's considered to
307 * match all. If found, the resource is removed atomically and freed.
309 * RETURNS:
310 * 0 if devres is found and freed, -ENOENT if not found.
312 int devres_destroy(struct device *dev, dr_release_t release,
313 dr_match_t match, void *match_data)
315 void *res;
317 res = devres_remove(dev, release, match, match_data);
318 if (unlikely(!res))
319 return -ENOENT;
321 devres_free(res);
322 return 0;
324 EXPORT_SYMBOL_GPL(devres_destroy);
326 static int remove_nodes(struct device *dev,
327 struct list_head *first, struct list_head *end,
328 struct list_head *todo)
330 int cnt = 0, nr_groups = 0;
331 struct list_head *cur;
333 /* First pass - move normal devres entries to @todo and clear
334 * devres_group colors.
336 cur = first;
337 while (cur != end) {
338 struct devres_node *node;
339 struct devres_group *grp;
341 node = list_entry(cur, struct devres_node, entry);
342 cur = cur->next;
344 grp = node_to_group(node);
345 if (grp) {
346 /* clear color of group markers in the first pass */
347 grp->color = 0;
348 nr_groups++;
349 } else {
350 /* regular devres entry */
351 if (&node->entry == first)
352 first = first->next;
353 list_move_tail(&node->entry, todo);
354 cnt++;
358 if (!nr_groups)
359 return cnt;
361 /* Second pass - Scan groups and color them. A group gets
362 * color value of two iff the group is wholly contained in
363 * [cur, end). That is, for a closed group, both opening and
364 * closing markers should be in the range, while just the
365 * opening marker is enough for an open group.
367 cur = first;
368 while (cur != end) {
369 struct devres_node *node;
370 struct devres_group *grp;
372 node = list_entry(cur, struct devres_node, entry);
373 cur = cur->next;
375 grp = node_to_group(node);
376 BUG_ON(!grp || list_empty(&grp->node[0].entry));
378 grp->color++;
379 if (list_empty(&grp->node[1].entry))
380 grp->color++;
382 BUG_ON(grp->color <= 0 || grp->color > 2);
383 if (grp->color == 2) {
384 /* No need to update cur or end. The removed
385 * nodes are always before both.
387 list_move_tail(&grp->node[0].entry, todo);
388 list_del_init(&grp->node[1].entry);
392 return cnt;
395 static int release_nodes(struct device *dev, struct list_head *first,
396 struct list_head *end, unsigned long flags)
398 LIST_HEAD(todo);
399 int cnt;
400 struct devres *dr, *tmp;
402 cnt = remove_nodes(dev, first, end, &todo);
404 spin_unlock_irqrestore(&dev->devres_lock, flags);
406 /* Release. Note that both devres and devres_group are
407 * handled as devres in the following loop. This is safe.
409 list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
410 devres_log(dev, &dr->node, "REL");
411 dr->node.release(dev, dr->data);
412 kfree(dr);
415 return cnt;
419 * devres_release_all - Release all resources
420 * @dev: Device to release resources for
422 * Release all resources associated with @dev. This function is
423 * called on driver detach.
425 int devres_release_all(struct device *dev)
427 unsigned long flags;
429 spin_lock_irqsave(&dev->devres_lock, flags);
430 return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
431 flags);
435 * devres_open_group - Open a new devres group
436 * @dev: Device to open devres group for
437 * @id: Separator ID
438 * @gfp: Allocation flags
440 * Open a new devres group for @dev with @id. For @id, using a
441 * pointer to an object which won't be used for another group is
442 * recommended. If @id is NULL, address-wise unique ID is created.
444 * RETURNS:
445 * ID of the new group, NULL on failure.
447 void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
449 struct devres_group *grp;
450 unsigned long flags;
452 grp = kmalloc(sizeof(*grp), gfp);
453 if (unlikely(!grp))
454 return NULL;
456 grp->node[0].release = &group_open_release;
457 grp->node[1].release = &group_close_release;
458 INIT_LIST_HEAD(&grp->node[0].entry);
459 INIT_LIST_HEAD(&grp->node[1].entry);
460 set_node_dbginfo(&grp->node[0], "grp<", 0);
461 set_node_dbginfo(&grp->node[1], "grp>", 0);
462 grp->id = grp;
463 if (id)
464 grp->id = id;
466 spin_lock_irqsave(&dev->devres_lock, flags);
467 add_dr(dev, &grp->node[0]);
468 spin_unlock_irqrestore(&dev->devres_lock, flags);
469 return grp->id;
471 EXPORT_SYMBOL_GPL(devres_open_group);
473 /* Find devres group with ID @id. If @id is NULL, look for the latest. */
474 static struct devres_group * find_group(struct device *dev, void *id)
476 struct devres_node *node;
478 list_for_each_entry_reverse(node, &dev->devres_head, entry) {
479 struct devres_group *grp;
481 if (node->release != &group_open_release)
482 continue;
484 grp = container_of(node, struct devres_group, node[0]);
486 if (id) {
487 if (grp->id == id)
488 return grp;
489 } else if (list_empty(&grp->node[1].entry))
490 return grp;
493 return NULL;
497 * devres_close_group - Close a devres group
498 * @dev: Device to close devres group for
499 * @id: ID of target group, can be NULL
501 * Close the group identified by @id. If @id is NULL, the latest open
502 * group is selected.
504 void devres_close_group(struct device *dev, void *id)
506 struct devres_group *grp;
507 unsigned long flags;
509 spin_lock_irqsave(&dev->devres_lock, flags);
511 grp = find_group(dev, id);
512 if (grp)
513 add_dr(dev, &grp->node[1]);
514 else
515 WARN_ON(1);
517 spin_unlock_irqrestore(&dev->devres_lock, flags);
519 EXPORT_SYMBOL_GPL(devres_close_group);
522 * devres_remove_group - Remove a devres group
523 * @dev: Device to remove group for
524 * @id: ID of target group, can be NULL
526 * Remove the group identified by @id. If @id is NULL, the latest
527 * open group is selected. Note that removing a group doesn't affect
528 * any other resources.
530 void devres_remove_group(struct device *dev, void *id)
532 struct devres_group *grp;
533 unsigned long flags;
535 spin_lock_irqsave(&dev->devres_lock, flags);
537 grp = find_group(dev, id);
538 if (grp) {
539 list_del_init(&grp->node[0].entry);
540 list_del_init(&grp->node[1].entry);
541 devres_log(dev, &grp->node[0], "REM");
542 } else
543 WARN_ON(1);
545 spin_unlock_irqrestore(&dev->devres_lock, flags);
547 kfree(grp);
549 EXPORT_SYMBOL_GPL(devres_remove_group);
552 * devres_release_group - Release resources in a devres group
553 * @dev: Device to release group for
554 * @id: ID of target group, can be NULL
556 * Release all resources in the group identified by @id. If @id is
557 * NULL, the latest open group is selected. The selected group and
558 * groups properly nested inside the selected group are removed.
560 * RETURNS:
561 * The number of released non-group resources.
563 int devres_release_group(struct device *dev, void *id)
565 struct devres_group *grp;
566 unsigned long flags;
567 int cnt = 0;
569 spin_lock_irqsave(&dev->devres_lock, flags);
571 grp = find_group(dev, id);
572 if (grp) {
573 struct list_head *first = &grp->node[0].entry;
574 struct list_head *end = &dev->devres_head;
576 if (!list_empty(&grp->node[1].entry))
577 end = grp->node[1].entry.next;
579 cnt = release_nodes(dev, first, end, flags);
580 } else {
581 WARN_ON(1);
582 spin_unlock_irqrestore(&dev->devres_lock, flags);
585 return cnt;
587 EXPORT_SYMBOL_GPL(devres_release_group);
590 * Managed kzalloc/kfree
592 static void devm_kzalloc_release(struct device *dev, void *res)
594 /* noop */
597 static int devm_kzalloc_match(struct device *dev, void *res, void *data)
599 return res == data;
603 * devm_kzalloc - Managed kzalloc
604 * @dev: Device to allocate memory for
605 * @size: Allocation size
606 * @gfp: Allocation gfp flags
608 * Managed kzalloc. Memory allocated with this function is
609 * automatically freed on driver detach. Like all other devres
610 * resources, guaranteed alignment is unsigned long long.
612 * RETURNS:
613 * Pointer to allocated memory on success, NULL on failure.
615 void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
617 struct devres *dr;
619 /* use raw alloc_dr for kmalloc caller tracing */
620 dr = alloc_dr(devm_kzalloc_release, size, gfp);
621 if (unlikely(!dr))
622 return NULL;
624 set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
625 devres_add(dev, dr->data);
626 return dr->data;
628 EXPORT_SYMBOL_GPL(devm_kzalloc);
631 * devm_kfree - Managed kfree
632 * @dev: Device this memory belongs to
633 * @p: Memory to free
635 * Free memory allocated with dev_kzalloc().
637 void devm_kfree(struct device *dev, void *p)
639 int rc;
641 rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
642 WARN_ON(rc);
644 EXPORT_SYMBOL_GPL(devm_kfree);