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>
12
13struct 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
20};
21
22struct devres {
23	struct devres_node		node;
24	/* -- 3 pointers */
25	unsigned long long		data[];	/* guarantee ull alignment */
26};
27
28struct devres_group {
29	struct devres_node		node[2];
30	void				*id;
31	int				color;
32	/* -- 8 pointers */
33};
34
35#ifdef CONFIG_DEBUG_DEVRES
36static int log_devres = 0;
37module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
38
39static void set_node_dbginfo(struct devres_node *node, const char *name,
40			     size_t size)
41{
42	node->name = name;
43	node->size = size;
44}
45
46static void devres_log(struct device *dev, struct devres_node *node,
47		       const char *op)
48{
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);
52}
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 */
57
58/*
59 * Release functions for devres group.  These callbacks are used only
60 * for identification.
61 */
62static void group_open_release(struct device *dev, void *res)
63{
64	/* noop */
65}
66
67static void group_close_release(struct device *dev, void *res)
68{
69	/* noop */
70}
71
72static struct devres_group * node_to_group(struct devres_node *node)
73{
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;
79}
80
81static __always_inline struct devres * alloc_dr(dr_release_t release,
82						size_t size, gfp_t gfp)
83{
84	size_t tot_size = sizeof(struct devres) + size;
85	struct devres *dr;
86
87	dr = kmalloc_track_caller(tot_size, gfp);
88	if (unlikely(!dr))
89		return NULL;
90
91	memset(dr, 0, tot_size);
92	INIT_LIST_HEAD(&dr->node.entry);
93	dr->node.release = release;
94	return dr;
95}
96
97static void add_dr(struct device *dev, struct devres_node *node)
98{
99	devres_log(dev, node, "ADD");
100	BUG_ON(!list_empty(&node->entry));
101	list_add_tail(&node->entry, &dev->devres_head);
102}
103
104#ifdef CONFIG_DEBUG_DEVRES
105void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
106		      const char *name)
107{
108	struct devres *dr;
109
110	dr = alloc_dr(release, size, gfp);
111	if (unlikely(!dr))
112		return NULL;
113	set_node_dbginfo(&dr->node, name, size);
114	return dr->data;
115}
116EXPORT_SYMBOL_GPL(__devres_alloc);
117#else
118/**
119 * devres_alloc - Allocate device resource data
120 * @release: Release function devres will be associated with
121 * @size: Allocation size
122 * @gfp: Allocation flags
123 *
124 * Allocate devres of @size bytes.  The allocated area is zeroed, then
125 * associated with @release.  The returned pointer can be passed to
126 * other devres_*() functions.
127 *
128 * RETURNS:
129 * Pointer to allocated devres on success, NULL on failure.
130 */
131void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
132{
133	struct devres *dr;
134
135	dr = alloc_dr(release, size, gfp);
136	if (unlikely(!dr))
137		return NULL;
138	return dr->data;
139}
140EXPORT_SYMBOL_GPL(devres_alloc);
141#endif
142
143/**
144 * devres_free - Free device resource data
145 * @res: Pointer to devres data to free
146 *
147 * Free devres created with devres_alloc().
148 */
149void devres_free(void *res)
150{
151	if (res) {
152		struct devres *dr = container_of(res, struct devres, data);
153
154		BUG_ON(!list_empty(&dr->node.entry));
155		kfree(dr);
156	}
157}
158EXPORT_SYMBOL_GPL(devres_free);
159
160/**
161 * devres_add - Register device resource
162 * @dev: Device to add resource to
163 * @res: Resource to register
164 *
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.
168 */
169void devres_add(struct device *dev, void *res)
170{
171	struct devres *dr = container_of(res, struct devres, data);
172	unsigned long flags;
173
174	spin_lock_irqsave(&dev->devres_lock, flags);
175	add_dr(dev, &dr->node);
176	spin_unlock_irqrestore(&dev->devres_lock, flags);
177}
178EXPORT_SYMBOL_GPL(devres_add);
179
180static struct devres *find_dr(struct device *dev, dr_release_t release,
181			      dr_match_t match, void *match_data)
182{
183	struct devres_node *node;
184
185	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
186		struct devres *dr = container_of(node, struct devres, node);
187
188		if (node->release != release)
189			continue;
190		if (match && !match(dev, dr->data, match_data))
191			continue;
192		return dr;
193	}
194
195	return NULL;
196}
197
198/**
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
204 *
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.
208 *
209 * RETURNS:
210 * Pointer to found devres, NULL if not found.
211 */
212void * devres_find(struct device *dev, dr_release_t release,
213		   dr_match_t match, void *match_data)
214{
215	struct devres *dr;
216	unsigned long flags;
217
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);
221
222	if (dr)
223		return dr->data;
224	return NULL;
225}
226EXPORT_SYMBOL_GPL(devres_find);
227
228/**
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
234 *
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.
238 *
239 * RETURNS:
240 * Pointer to found or added devres.
241 */
242void * devres_get(struct device *dev, void *new_res,
243		  dr_match_t match, void *match_data)
244{
245	struct devres *new_dr = container_of(new_res, struct devres, data);
246	struct devres *dr;
247	unsigned long flags;
248
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;
255	}
256	spin_unlock_irqrestore(&dev->devres_lock, flags);
257	devres_free(new_dr);
258
259	return dr->data;
260}
261EXPORT_SYMBOL_GPL(devres_get);
262
263/**
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
269 *
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.
274 *
275 * RETURNS:
276 * Pointer to removed devres on success, NULL if not found.
277 */
278void * devres_remove(struct device *dev, dr_release_t release,
279		     dr_match_t match, void *match_data)
280{
281	struct devres *dr;
282	unsigned long flags;
283
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");
289	}
290	spin_unlock_irqrestore(&dev->devres_lock, flags);
291
292	if (dr)
293		return dr->data;
294	return NULL;
295}
296EXPORT_SYMBOL_GPL(devres_remove);
297
298/**
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
304 *
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.
308 *
309 * RETURNS:
310 * 0 if devres is found and freed, -ENOENT if not found.
311 */
312int devres_destroy(struct device *dev, dr_release_t release,
313		   dr_match_t match, void *match_data)
314{
315	void *res;
316
317	res = devres_remove(dev, release, match, match_data);
318	if (unlikely(!res))
319		return -ENOENT;
320
321	devres_free(res);
322	return 0;
323}
324EXPORT_SYMBOL_GPL(devres_destroy);
325
326static int remove_nodes(struct device *dev,
327			struct list_head *first, struct list_head *end,
328			struct list_head *todo)
329{
330	int cnt = 0, nr_groups = 0;
331	struct list_head *cur;
332
333	/* First pass - move normal devres entries to @todo and clear
334	 * devres_group colors.
335	 */
336	cur = first;
337	while (cur != end) {
338		struct devres_node *node;
339		struct devres_group *grp;
340
341		node = list_entry(cur, struct devres_node, entry);
342		cur = cur->next;
343
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++;
355		}
356	}
357
358	if (!nr_groups)
359		return cnt;
360
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.
366	 */
367	cur = first;
368	while (cur != end) {
369		struct devres_node *node;
370		struct devres_group *grp;
371
372		node = list_entry(cur, struct devres_node, entry);
373		cur = cur->next;
374
375		grp = node_to_group(node);
376		BUG_ON(!grp || list_empty(&grp->node[0].entry));
377
378		grp->color++;
379		if (list_empty(&grp->node[1].entry))
380			grp->color++;
381
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.
386			 */
387			list_move_tail(&grp->node[0].entry, todo);
388			list_del_init(&grp->node[1].entry);
389		}
390	}
391
392	return cnt;
393}
394
395static int release_nodes(struct device *dev, struct list_head *first,
396			 struct list_head *end, unsigned long flags)
397{
398	LIST_HEAD(todo);
399	int cnt;
400	struct devres *dr, *tmp;
401
402	cnt = remove_nodes(dev, first, end, &todo);
403
404	spin_unlock_irqrestore(&dev->devres_lock, flags);
405
406	/* Release.  Note that both devres and devres_group are
407	 * handled as devres in the following loop.  This is safe.
408	 */
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);
413	}
414
415	return cnt;
416}
417
418/**
419 * devres_release_all - Release all managed resources
420 * @dev: Device to release resources for
421 *
422 * Release all resources associated with @dev.  This function is
423 * called on driver detach.
424 */
425int devres_release_all(struct device *dev)
426{
427	unsigned long flags;
428
429	spin_lock_irqsave(&dev->devres_lock, flags);
430	return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
431			     flags);
432}
433
434/**
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
439 *
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.
443 *
444 * RETURNS:
445 * ID of the new group, NULL on failure.
446 */
447void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
448{
449	struct devres_group *grp;
450	unsigned long flags;
451
452	grp = kmalloc(sizeof(*grp), gfp);
453	if (unlikely(!grp))
454		return NULL;
455
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;
465
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;
470}
471EXPORT_SYMBOL_GPL(devres_open_group);
472
473/* Find devres group with ID @id.  If @id is NULL, look for the latest. */
474static struct devres_group * find_group(struct device *dev, void *id)
475{
476	struct devres_node *node;
477
478	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
479		struct devres_group *grp;
480
481		if (node->release != &group_open_release)
482			continue;
483
484		grp = container_of(node, struct devres_group, node[0]);
485
486		if (id) {
487			if (grp->id == id)
488				return grp;
489		} else if (list_empty(&grp->node[1].entry))
490			return grp;
491	}
492
493	return NULL;
494}
495
496/**
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
500 *
501 * Close the group identified by @id.  If @id is NULL, the latest open
502 * group is selected.
503 */
504void devres_close_group(struct device *dev, void *id)
505{
506	struct devres_group *grp;
507	unsigned long flags;
508
509	spin_lock_irqsave(&dev->devres_lock, flags);
510
511	grp = find_group(dev, id);
512	if (grp)
513		add_dr(dev, &grp->node[1]);
514	else
515		WARN_ON(1);
516
517	spin_unlock_irqrestore(&dev->devres_lock, flags);
518}
519EXPORT_SYMBOL_GPL(devres_close_group);
520
521/**
522 * devres_remove_group - Remove a devres group
523 * @dev: Device to remove group for
524 * @id: ID of target group, can be NULL
525 *
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.
529 */
530void devres_remove_group(struct device *dev, void *id)
531{
532	struct devres_group *grp;
533	unsigned long flags;
534
535	spin_lock_irqsave(&dev->devres_lock, flags);
536
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);
544
545	spin_unlock_irqrestore(&dev->devres_lock, flags);
546
547	kfree(grp);
548}
549EXPORT_SYMBOL_GPL(devres_remove_group);
550
551/**
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
555 *
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.
559 *
560 * RETURNS:
561 * The number of released non-group resources.
562 */
563int devres_release_group(struct device *dev, void *id)
564{
565	struct devres_group *grp;
566	unsigned long flags;
567	int cnt = 0;
568
569	spin_lock_irqsave(&dev->devres_lock, flags);
570
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;
575
576		if (!list_empty(&grp->node[1].entry))
577			end = grp->node[1].entry.next;
578
579		cnt = release_nodes(dev, first, end, flags);
580	} else {
581		WARN_ON(1);
582		spin_unlock_irqrestore(&dev->devres_lock, flags);
583	}
584
585	return cnt;
586}
587EXPORT_SYMBOL_GPL(devres_release_group);
588
589/*
590 * Managed kzalloc/kfree
591 */
592static void devm_kzalloc_release(struct device *dev, void *res)
593{
594	/* noop */
595}
596
597static int devm_kzalloc_match(struct device *dev, void *res, void *data)
598{
599	return res == data;
600}
601
602/**
603 * devm_kzalloc - Resource-managed kzalloc
604 * @dev: Device to allocate memory for
605 * @size: Allocation size
606 * @gfp: Allocation gfp flags
607 *
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.
611 *
612 * RETURNS:
613 * Pointer to allocated memory on success, NULL on failure.
614 */
615void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
616{
617	struct devres *dr;
618
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;
623
624	set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
625	devres_add(dev, dr->data);
626	return dr->data;
627}
628EXPORT_SYMBOL_GPL(devm_kzalloc);
629
630/**
631 * devm_kfree - Resource-managed kfree
632 * @dev: Device this memory belongs to
633 * @p: Memory to free
634 *
635 * Free memory allocated with dev_kzalloc().
636 */
637void devm_kfree(struct device *dev, void *p)
638{
639	int rc;
640
641	rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p);
642	WARN_ON(rc);
643}
644EXPORT_SYMBOL_GPL(devm_kfree);
645