xref: /OK3568_Linux_fs/kernel/drivers/base/devres.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * drivers/base/devres.c - device resource management
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2006  SUSE Linux Products GmbH
6*4882a593Smuzhiyun  * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/percpu.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <asm/sections.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "base.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct devres_node {
19*4882a593Smuzhiyun 	struct list_head		entry;
20*4882a593Smuzhiyun 	dr_release_t			release;
21*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_DEVRES
22*4882a593Smuzhiyun 	const char			*name;
23*4882a593Smuzhiyun 	size_t				size;
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct devres {
28*4882a593Smuzhiyun 	struct devres_node		node;
29*4882a593Smuzhiyun 	/*
30*4882a593Smuzhiyun 	 * Some archs want to perform DMA into kmalloc caches
31*4882a593Smuzhiyun 	 * and need a guaranteed alignment larger than
32*4882a593Smuzhiyun 	 * the alignment of a 64-bit integer.
33*4882a593Smuzhiyun 	 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
34*4882a593Smuzhiyun 	 * buffer alignment as if it was allocated by plain kmalloc().
35*4882a593Smuzhiyun 	 */
36*4882a593Smuzhiyun 	u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun struct devres_group {
40*4882a593Smuzhiyun 	struct devres_node		node[2];
41*4882a593Smuzhiyun 	void				*id;
42*4882a593Smuzhiyun 	int				color;
43*4882a593Smuzhiyun 	/* -- 8 pointers */
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_DEVRES
47*4882a593Smuzhiyun static int log_devres = 0;
48*4882a593Smuzhiyun module_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
49*4882a593Smuzhiyun 
set_node_dbginfo(struct devres_node * node,const char * name,size_t size)50*4882a593Smuzhiyun static void set_node_dbginfo(struct devres_node *node, const char *name,
51*4882a593Smuzhiyun 			     size_t size)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	node->name = name;
54*4882a593Smuzhiyun 	node->size = size;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
devres_log(struct device * dev,struct devres_node * node,const char * op)57*4882a593Smuzhiyun static void devres_log(struct device *dev, struct devres_node *node,
58*4882a593Smuzhiyun 		       const char *op)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	if (unlikely(log_devres))
61*4882a593Smuzhiyun 		dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
62*4882a593Smuzhiyun 			op, node, node->name, (unsigned long)node->size);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun #else /* CONFIG_DEBUG_DEVRES */
65*4882a593Smuzhiyun #define set_node_dbginfo(node, n, s)	do {} while (0)
66*4882a593Smuzhiyun #define devres_log(dev, node, op)	do {} while (0)
67*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_DEVRES */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * Release functions for devres group.  These callbacks are used only
71*4882a593Smuzhiyun  * for identification.
72*4882a593Smuzhiyun  */
group_open_release(struct device * dev,void * res)73*4882a593Smuzhiyun static void group_open_release(struct device *dev, void *res)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	/* noop */
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
group_close_release(struct device * dev,void * res)78*4882a593Smuzhiyun static void group_close_release(struct device *dev, void *res)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	/* noop */
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
node_to_group(struct devres_node * node)83*4882a593Smuzhiyun static struct devres_group * node_to_group(struct devres_node *node)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	if (node->release == &group_open_release)
86*4882a593Smuzhiyun 		return container_of(node, struct devres_group, node[0]);
87*4882a593Smuzhiyun 	if (node->release == &group_close_release)
88*4882a593Smuzhiyun 		return container_of(node, struct devres_group, node[1]);
89*4882a593Smuzhiyun 	return NULL;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
check_dr_size(size_t size,size_t * tot_size)92*4882a593Smuzhiyun static bool check_dr_size(size_t size, size_t *tot_size)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	/* We must catch any near-SIZE_MAX cases that could overflow. */
95*4882a593Smuzhiyun 	if (unlikely(check_add_overflow(sizeof(struct devres),
96*4882a593Smuzhiyun 					size, tot_size)))
97*4882a593Smuzhiyun 		return false;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	return true;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
alloc_dr(dr_release_t release,size_t size,gfp_t gfp,int nid)102*4882a593Smuzhiyun static __always_inline struct devres * alloc_dr(dr_release_t release,
103*4882a593Smuzhiyun 						size_t size, gfp_t gfp, int nid)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	size_t tot_size;
106*4882a593Smuzhiyun 	struct devres *dr;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (!check_dr_size(size, &tot_size))
109*4882a593Smuzhiyun 		return NULL;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	dr = kmalloc_node_track_caller(tot_size, gfp, nid);
112*4882a593Smuzhiyun 	if (unlikely(!dr))
113*4882a593Smuzhiyun 		return NULL;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	memset(dr, 0, offsetof(struct devres, data));
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dr->node.entry);
118*4882a593Smuzhiyun 	dr->node.release = release;
119*4882a593Smuzhiyun 	return dr;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
add_dr(struct device * dev,struct devres_node * node)122*4882a593Smuzhiyun static void add_dr(struct device *dev, struct devres_node *node)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	devres_log(dev, node, "ADD");
125*4882a593Smuzhiyun 	BUG_ON(!list_empty(&node->entry));
126*4882a593Smuzhiyun 	list_add_tail(&node->entry, &dev->devres_head);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
replace_dr(struct device * dev,struct devres_node * old,struct devres_node * new)129*4882a593Smuzhiyun static void replace_dr(struct device *dev,
130*4882a593Smuzhiyun 		       struct devres_node *old, struct devres_node *new)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	devres_log(dev, old, "REPLACE");
133*4882a593Smuzhiyun 	BUG_ON(!list_empty(&new->entry));
134*4882a593Smuzhiyun 	list_replace(&old->entry, &new->entry);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_DEVRES
__devres_alloc_node(dr_release_t release,size_t size,gfp_t gfp,int nid,const char * name)138*4882a593Smuzhiyun void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
139*4882a593Smuzhiyun 		      const char *name)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	struct devres *dr;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
144*4882a593Smuzhiyun 	if (unlikely(!dr))
145*4882a593Smuzhiyun 		return NULL;
146*4882a593Smuzhiyun 	set_node_dbginfo(&dr->node, name, size);
147*4882a593Smuzhiyun 	return dr->data;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__devres_alloc_node);
150*4882a593Smuzhiyun #else
151*4882a593Smuzhiyun /**
152*4882a593Smuzhiyun  * devres_alloc - Allocate device resource data
153*4882a593Smuzhiyun  * @release: Release function devres will be associated with
154*4882a593Smuzhiyun  * @size: Allocation size
155*4882a593Smuzhiyun  * @gfp: Allocation flags
156*4882a593Smuzhiyun  * @nid: NUMA node
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * Allocate devres of @size bytes.  The allocated area is zeroed, then
159*4882a593Smuzhiyun  * associated with @release.  The returned pointer can be passed to
160*4882a593Smuzhiyun  * other devres_*() functions.
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  * RETURNS:
163*4882a593Smuzhiyun  * Pointer to allocated devres on success, NULL on failure.
164*4882a593Smuzhiyun  */
devres_alloc_node(dr_release_t release,size_t size,gfp_t gfp,int nid)165*4882a593Smuzhiyun void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct devres *dr;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
170*4882a593Smuzhiyun 	if (unlikely(!dr))
171*4882a593Smuzhiyun 		return NULL;
172*4882a593Smuzhiyun 	return dr->data;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_alloc_node);
175*4882a593Smuzhiyun #endif
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /**
178*4882a593Smuzhiyun  * devres_for_each_res - Resource iterator
179*4882a593Smuzhiyun  * @dev: Device to iterate resource from
180*4882a593Smuzhiyun  * @release: Look for resources associated with this release function
181*4882a593Smuzhiyun  * @match: Match function (optional)
182*4882a593Smuzhiyun  * @match_data: Data for the match function
183*4882a593Smuzhiyun  * @fn: Function to be called for each matched resource.
184*4882a593Smuzhiyun  * @data: Data for @fn, the 3rd parameter of @fn
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * Call @fn for each devres of @dev which is associated with @release
187*4882a593Smuzhiyun  * and for which @match returns 1.
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * RETURNS:
190*4882a593Smuzhiyun  * 	void
191*4882a593Smuzhiyun  */
devres_for_each_res(struct device * dev,dr_release_t release,dr_match_t match,void * match_data,void (* fn)(struct device *,void *,void *),void * data)192*4882a593Smuzhiyun void devres_for_each_res(struct device *dev, dr_release_t release,
193*4882a593Smuzhiyun 			dr_match_t match, void *match_data,
194*4882a593Smuzhiyun 			void (*fn)(struct device *, void *, void *),
195*4882a593Smuzhiyun 			void *data)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct devres_node *node;
198*4882a593Smuzhiyun 	struct devres_node *tmp;
199*4882a593Smuzhiyun 	unsigned long flags;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (!fn)
202*4882a593Smuzhiyun 		return;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
205*4882a593Smuzhiyun 	list_for_each_entry_safe_reverse(node, tmp,
206*4882a593Smuzhiyun 			&dev->devres_head, entry) {
207*4882a593Smuzhiyun 		struct devres *dr = container_of(node, struct devres, node);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		if (node->release != release)
210*4882a593Smuzhiyun 			continue;
211*4882a593Smuzhiyun 		if (match && !match(dev, dr->data, match_data))
212*4882a593Smuzhiyun 			continue;
213*4882a593Smuzhiyun 		fn(dev, dr->data, data);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_for_each_res);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun /**
220*4882a593Smuzhiyun  * devres_free - Free device resource data
221*4882a593Smuzhiyun  * @res: Pointer to devres data to free
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * Free devres created with devres_alloc().
224*4882a593Smuzhiyun  */
devres_free(void * res)225*4882a593Smuzhiyun void devres_free(void *res)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	if (res) {
228*4882a593Smuzhiyun 		struct devres *dr = container_of(res, struct devres, data);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 		BUG_ON(!list_empty(&dr->node.entry));
231*4882a593Smuzhiyun 		kfree(dr);
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_free);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /**
237*4882a593Smuzhiyun  * devres_add - Register device resource
238*4882a593Smuzhiyun  * @dev: Device to add resource to
239*4882a593Smuzhiyun  * @res: Resource to register
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  * Register devres @res to @dev.  @res should have been allocated
242*4882a593Smuzhiyun  * using devres_alloc().  On driver detach, the associated release
243*4882a593Smuzhiyun  * function will be invoked and devres will be freed automatically.
244*4882a593Smuzhiyun  */
devres_add(struct device * dev,void * res)245*4882a593Smuzhiyun void devres_add(struct device *dev, void *res)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	struct devres *dr = container_of(res, struct devres, data);
248*4882a593Smuzhiyun 	unsigned long flags;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
251*4882a593Smuzhiyun 	add_dr(dev, &dr->node);
252*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_add);
255*4882a593Smuzhiyun 
find_dr(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)256*4882a593Smuzhiyun static struct devres *find_dr(struct device *dev, dr_release_t release,
257*4882a593Smuzhiyun 			      dr_match_t match, void *match_data)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct devres_node *node;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
262*4882a593Smuzhiyun 		struct devres *dr = container_of(node, struct devres, node);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		if (node->release != release)
265*4882a593Smuzhiyun 			continue;
266*4882a593Smuzhiyun 		if (match && !match(dev, dr->data, match_data))
267*4882a593Smuzhiyun 			continue;
268*4882a593Smuzhiyun 		return dr;
269*4882a593Smuzhiyun 	}
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	return NULL;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun /**
275*4882a593Smuzhiyun  * devres_find - Find device resource
276*4882a593Smuzhiyun  * @dev: Device to lookup resource from
277*4882a593Smuzhiyun  * @release: Look for resources associated with this release function
278*4882a593Smuzhiyun  * @match: Match function (optional)
279*4882a593Smuzhiyun  * @match_data: Data for the match function
280*4882a593Smuzhiyun  *
281*4882a593Smuzhiyun  * Find the latest devres of @dev which is associated with @release
282*4882a593Smuzhiyun  * and for which @match returns 1.  If @match is NULL, it's considered
283*4882a593Smuzhiyun  * to match all.
284*4882a593Smuzhiyun  *
285*4882a593Smuzhiyun  * RETURNS:
286*4882a593Smuzhiyun  * Pointer to found devres, NULL if not found.
287*4882a593Smuzhiyun  */
devres_find(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)288*4882a593Smuzhiyun void * devres_find(struct device *dev, dr_release_t release,
289*4882a593Smuzhiyun 		   dr_match_t match, void *match_data)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	struct devres *dr;
292*4882a593Smuzhiyun 	unsigned long flags;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
295*4882a593Smuzhiyun 	dr = find_dr(dev, release, match, match_data);
296*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (dr)
299*4882a593Smuzhiyun 		return dr->data;
300*4882a593Smuzhiyun 	return NULL;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_find);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun /**
305*4882a593Smuzhiyun  * devres_get - Find devres, if non-existent, add one atomically
306*4882a593Smuzhiyun  * @dev: Device to lookup or add devres for
307*4882a593Smuzhiyun  * @new_res: Pointer to new initialized devres to add if not found
308*4882a593Smuzhiyun  * @match: Match function (optional)
309*4882a593Smuzhiyun  * @match_data: Data for the match function
310*4882a593Smuzhiyun  *
311*4882a593Smuzhiyun  * Find the latest devres of @dev which has the same release function
312*4882a593Smuzhiyun  * as @new_res and for which @match return 1.  If found, @new_res is
313*4882a593Smuzhiyun  * freed; otherwise, @new_res is added atomically.
314*4882a593Smuzhiyun  *
315*4882a593Smuzhiyun  * RETURNS:
316*4882a593Smuzhiyun  * Pointer to found or added devres.
317*4882a593Smuzhiyun  */
devres_get(struct device * dev,void * new_res,dr_match_t match,void * match_data)318*4882a593Smuzhiyun void * devres_get(struct device *dev, void *new_res,
319*4882a593Smuzhiyun 		  dr_match_t match, void *match_data)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	struct devres *new_dr = container_of(new_res, struct devres, data);
322*4882a593Smuzhiyun 	struct devres *dr;
323*4882a593Smuzhiyun 	unsigned long flags;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
326*4882a593Smuzhiyun 	dr = find_dr(dev, new_dr->node.release, match, match_data);
327*4882a593Smuzhiyun 	if (!dr) {
328*4882a593Smuzhiyun 		add_dr(dev, &new_dr->node);
329*4882a593Smuzhiyun 		dr = new_dr;
330*4882a593Smuzhiyun 		new_res = NULL;
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
333*4882a593Smuzhiyun 	devres_free(new_res);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	return dr->data;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_get);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun /**
340*4882a593Smuzhiyun  * devres_remove - Find a device resource and remove it
341*4882a593Smuzhiyun  * @dev: Device to find resource from
342*4882a593Smuzhiyun  * @release: Look for resources associated with this release function
343*4882a593Smuzhiyun  * @match: Match function (optional)
344*4882a593Smuzhiyun  * @match_data: Data for the match function
345*4882a593Smuzhiyun  *
346*4882a593Smuzhiyun  * Find the latest devres of @dev associated with @release and for
347*4882a593Smuzhiyun  * which @match returns 1.  If @match is NULL, it's considered to
348*4882a593Smuzhiyun  * match all.  If found, the resource is removed atomically and
349*4882a593Smuzhiyun  * returned.
350*4882a593Smuzhiyun  *
351*4882a593Smuzhiyun  * RETURNS:
352*4882a593Smuzhiyun  * Pointer to removed devres on success, NULL if not found.
353*4882a593Smuzhiyun  */
devres_remove(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)354*4882a593Smuzhiyun void * devres_remove(struct device *dev, dr_release_t release,
355*4882a593Smuzhiyun 		     dr_match_t match, void *match_data)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	struct devres *dr;
358*4882a593Smuzhiyun 	unsigned long flags;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
361*4882a593Smuzhiyun 	dr = find_dr(dev, release, match, match_data);
362*4882a593Smuzhiyun 	if (dr) {
363*4882a593Smuzhiyun 		list_del_init(&dr->node.entry);
364*4882a593Smuzhiyun 		devres_log(dev, &dr->node, "REM");
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (dr)
369*4882a593Smuzhiyun 		return dr->data;
370*4882a593Smuzhiyun 	return NULL;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_remove);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun /**
375*4882a593Smuzhiyun  * devres_destroy - Find a device resource and destroy it
376*4882a593Smuzhiyun  * @dev: Device to find resource from
377*4882a593Smuzhiyun  * @release: Look for resources associated with this release function
378*4882a593Smuzhiyun  * @match: Match function (optional)
379*4882a593Smuzhiyun  * @match_data: Data for the match function
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * Find the latest devres of @dev associated with @release and for
382*4882a593Smuzhiyun  * which @match returns 1.  If @match is NULL, it's considered to
383*4882a593Smuzhiyun  * match all.  If found, the resource is removed atomically and freed.
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * Note that the release function for the resource will not be called,
386*4882a593Smuzhiyun  * only the devres-allocated data will be freed.  The caller becomes
387*4882a593Smuzhiyun  * responsible for freeing any other data.
388*4882a593Smuzhiyun  *
389*4882a593Smuzhiyun  * RETURNS:
390*4882a593Smuzhiyun  * 0 if devres is found and freed, -ENOENT if not found.
391*4882a593Smuzhiyun  */
devres_destroy(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)392*4882a593Smuzhiyun int devres_destroy(struct device *dev, dr_release_t release,
393*4882a593Smuzhiyun 		   dr_match_t match, void *match_data)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	void *res;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	res = devres_remove(dev, release, match, match_data);
398*4882a593Smuzhiyun 	if (unlikely(!res))
399*4882a593Smuzhiyun 		return -ENOENT;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	devres_free(res);
402*4882a593Smuzhiyun 	return 0;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_destroy);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun /**
408*4882a593Smuzhiyun  * devres_release - Find a device resource and destroy it, calling release
409*4882a593Smuzhiyun  * @dev: Device to find resource from
410*4882a593Smuzhiyun  * @release: Look for resources associated with this release function
411*4882a593Smuzhiyun  * @match: Match function (optional)
412*4882a593Smuzhiyun  * @match_data: Data for the match function
413*4882a593Smuzhiyun  *
414*4882a593Smuzhiyun  * Find the latest devres of @dev associated with @release and for
415*4882a593Smuzhiyun  * which @match returns 1.  If @match is NULL, it's considered to
416*4882a593Smuzhiyun  * match all.  If found, the resource is removed atomically, the
417*4882a593Smuzhiyun  * release function called and the resource freed.
418*4882a593Smuzhiyun  *
419*4882a593Smuzhiyun  * RETURNS:
420*4882a593Smuzhiyun  * 0 if devres is found and freed, -ENOENT if not found.
421*4882a593Smuzhiyun  */
devres_release(struct device * dev,dr_release_t release,dr_match_t match,void * match_data)422*4882a593Smuzhiyun int devres_release(struct device *dev, dr_release_t release,
423*4882a593Smuzhiyun 		   dr_match_t match, void *match_data)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	void *res;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	res = devres_remove(dev, release, match, match_data);
428*4882a593Smuzhiyun 	if (unlikely(!res))
429*4882a593Smuzhiyun 		return -ENOENT;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	(*release)(dev, res);
432*4882a593Smuzhiyun 	devres_free(res);
433*4882a593Smuzhiyun 	return 0;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_release);
436*4882a593Smuzhiyun 
remove_nodes(struct device * dev,struct list_head * first,struct list_head * end,struct list_head * todo)437*4882a593Smuzhiyun static int remove_nodes(struct device *dev,
438*4882a593Smuzhiyun 			struct list_head *first, struct list_head *end,
439*4882a593Smuzhiyun 			struct list_head *todo)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	int cnt = 0, nr_groups = 0;
442*4882a593Smuzhiyun 	struct list_head *cur;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* First pass - move normal devres entries to @todo and clear
445*4882a593Smuzhiyun 	 * devres_group colors.
446*4882a593Smuzhiyun 	 */
447*4882a593Smuzhiyun 	cur = first;
448*4882a593Smuzhiyun 	while (cur != end) {
449*4882a593Smuzhiyun 		struct devres_node *node;
450*4882a593Smuzhiyun 		struct devres_group *grp;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 		node = list_entry(cur, struct devres_node, entry);
453*4882a593Smuzhiyun 		cur = cur->next;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 		grp = node_to_group(node);
456*4882a593Smuzhiyun 		if (grp) {
457*4882a593Smuzhiyun 			/* clear color of group markers in the first pass */
458*4882a593Smuzhiyun 			grp->color = 0;
459*4882a593Smuzhiyun 			nr_groups++;
460*4882a593Smuzhiyun 		} else {
461*4882a593Smuzhiyun 			/* regular devres entry */
462*4882a593Smuzhiyun 			if (&node->entry == first)
463*4882a593Smuzhiyun 				first = first->next;
464*4882a593Smuzhiyun 			list_move_tail(&node->entry, todo);
465*4882a593Smuzhiyun 			cnt++;
466*4882a593Smuzhiyun 		}
467*4882a593Smuzhiyun 	}
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	if (!nr_groups)
470*4882a593Smuzhiyun 		return cnt;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	/* Second pass - Scan groups and color them.  A group gets
473*4882a593Smuzhiyun 	 * color value of two iff the group is wholly contained in
474*4882a593Smuzhiyun 	 * [cur, end).  That is, for a closed group, both opening and
475*4882a593Smuzhiyun 	 * closing markers should be in the range, while just the
476*4882a593Smuzhiyun 	 * opening marker is enough for an open group.
477*4882a593Smuzhiyun 	 */
478*4882a593Smuzhiyun 	cur = first;
479*4882a593Smuzhiyun 	while (cur != end) {
480*4882a593Smuzhiyun 		struct devres_node *node;
481*4882a593Smuzhiyun 		struct devres_group *grp;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 		node = list_entry(cur, struct devres_node, entry);
484*4882a593Smuzhiyun 		cur = cur->next;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 		grp = node_to_group(node);
487*4882a593Smuzhiyun 		BUG_ON(!grp || list_empty(&grp->node[0].entry));
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 		grp->color++;
490*4882a593Smuzhiyun 		if (list_empty(&grp->node[1].entry))
491*4882a593Smuzhiyun 			grp->color++;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 		BUG_ON(grp->color <= 0 || grp->color > 2);
494*4882a593Smuzhiyun 		if (grp->color == 2) {
495*4882a593Smuzhiyun 			/* No need to update cur or end.  The removed
496*4882a593Smuzhiyun 			 * nodes are always before both.
497*4882a593Smuzhiyun 			 */
498*4882a593Smuzhiyun 			list_move_tail(&grp->node[0].entry, todo);
499*4882a593Smuzhiyun 			list_del_init(&grp->node[1].entry);
500*4882a593Smuzhiyun 		}
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	return cnt;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun 
release_nodes(struct device * dev,struct list_head * first,struct list_head * end,unsigned long flags)506*4882a593Smuzhiyun static int release_nodes(struct device *dev, struct list_head *first,
507*4882a593Smuzhiyun 			 struct list_head *end, unsigned long flags)
508*4882a593Smuzhiyun 	__releases(&dev->devres_lock)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	LIST_HEAD(todo);
511*4882a593Smuzhiyun 	int cnt;
512*4882a593Smuzhiyun 	struct devres *dr, *tmp;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	cnt = remove_nodes(dev, first, end, &todo);
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* Release.  Note that both devres and devres_group are
519*4882a593Smuzhiyun 	 * handled as devres in the following loop.  This is safe.
520*4882a593Smuzhiyun 	 */
521*4882a593Smuzhiyun 	list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
522*4882a593Smuzhiyun 		devres_log(dev, &dr->node, "REL");
523*4882a593Smuzhiyun 		dr->node.release(dev, dr->data);
524*4882a593Smuzhiyun 		kfree(dr);
525*4882a593Smuzhiyun 	}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	return cnt;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /**
531*4882a593Smuzhiyun  * devres_release_all - Release all managed resources
532*4882a593Smuzhiyun  * @dev: Device to release resources for
533*4882a593Smuzhiyun  *
534*4882a593Smuzhiyun  * Release all resources associated with @dev.  This function is
535*4882a593Smuzhiyun  * called on driver detach.
536*4882a593Smuzhiyun  */
devres_release_all(struct device * dev)537*4882a593Smuzhiyun int devres_release_all(struct device *dev)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	unsigned long flags;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	/* Looks like an uninitialized device structure */
542*4882a593Smuzhiyun 	if (WARN_ON(dev->devres_head.next == NULL))
543*4882a593Smuzhiyun 		return -ENODEV;
544*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
545*4882a593Smuzhiyun 	return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
546*4882a593Smuzhiyun 			     flags);
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun /**
550*4882a593Smuzhiyun  * devres_open_group - Open a new devres group
551*4882a593Smuzhiyun  * @dev: Device to open devres group for
552*4882a593Smuzhiyun  * @id: Separator ID
553*4882a593Smuzhiyun  * @gfp: Allocation flags
554*4882a593Smuzhiyun  *
555*4882a593Smuzhiyun  * Open a new devres group for @dev with @id.  For @id, using a
556*4882a593Smuzhiyun  * pointer to an object which won't be used for another group is
557*4882a593Smuzhiyun  * recommended.  If @id is NULL, address-wise unique ID is created.
558*4882a593Smuzhiyun  *
559*4882a593Smuzhiyun  * RETURNS:
560*4882a593Smuzhiyun  * ID of the new group, NULL on failure.
561*4882a593Smuzhiyun  */
devres_open_group(struct device * dev,void * id,gfp_t gfp)562*4882a593Smuzhiyun void * devres_open_group(struct device *dev, void *id, gfp_t gfp)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	struct devres_group *grp;
565*4882a593Smuzhiyun 	unsigned long flags;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	grp = kmalloc(sizeof(*grp), gfp);
568*4882a593Smuzhiyun 	if (unlikely(!grp))
569*4882a593Smuzhiyun 		return NULL;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	grp->node[0].release = &group_open_release;
572*4882a593Smuzhiyun 	grp->node[1].release = &group_close_release;
573*4882a593Smuzhiyun 	INIT_LIST_HEAD(&grp->node[0].entry);
574*4882a593Smuzhiyun 	INIT_LIST_HEAD(&grp->node[1].entry);
575*4882a593Smuzhiyun 	set_node_dbginfo(&grp->node[0], "grp<", 0);
576*4882a593Smuzhiyun 	set_node_dbginfo(&grp->node[1], "grp>", 0);
577*4882a593Smuzhiyun 	grp->id = grp;
578*4882a593Smuzhiyun 	if (id)
579*4882a593Smuzhiyun 		grp->id = id;
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
582*4882a593Smuzhiyun 	add_dr(dev, &grp->node[0]);
583*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
584*4882a593Smuzhiyun 	return grp->id;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_open_group);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun /* Find devres group with ID @id.  If @id is NULL, look for the latest. */
find_group(struct device * dev,void * id)589*4882a593Smuzhiyun static struct devres_group * find_group(struct device *dev, void *id)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	struct devres_node *node;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
594*4882a593Smuzhiyun 		struct devres_group *grp;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 		if (node->release != &group_open_release)
597*4882a593Smuzhiyun 			continue;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		grp = container_of(node, struct devres_group, node[0]);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		if (id) {
602*4882a593Smuzhiyun 			if (grp->id == id)
603*4882a593Smuzhiyun 				return grp;
604*4882a593Smuzhiyun 		} else if (list_empty(&grp->node[1].entry))
605*4882a593Smuzhiyun 			return grp;
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return NULL;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun /**
612*4882a593Smuzhiyun  * devres_close_group - Close a devres group
613*4882a593Smuzhiyun  * @dev: Device to close devres group for
614*4882a593Smuzhiyun  * @id: ID of target group, can be NULL
615*4882a593Smuzhiyun  *
616*4882a593Smuzhiyun  * Close the group identified by @id.  If @id is NULL, the latest open
617*4882a593Smuzhiyun  * group is selected.
618*4882a593Smuzhiyun  */
devres_close_group(struct device * dev,void * id)619*4882a593Smuzhiyun void devres_close_group(struct device *dev, void *id)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun 	struct devres_group *grp;
622*4882a593Smuzhiyun 	unsigned long flags;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	grp = find_group(dev, id);
627*4882a593Smuzhiyun 	if (grp)
628*4882a593Smuzhiyun 		add_dr(dev, &grp->node[1]);
629*4882a593Smuzhiyun 	else
630*4882a593Smuzhiyun 		WARN_ON(1);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_close_group);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun /**
637*4882a593Smuzhiyun  * devres_remove_group - Remove a devres group
638*4882a593Smuzhiyun  * @dev: Device to remove group for
639*4882a593Smuzhiyun  * @id: ID of target group, can be NULL
640*4882a593Smuzhiyun  *
641*4882a593Smuzhiyun  * Remove the group identified by @id.  If @id is NULL, the latest
642*4882a593Smuzhiyun  * open group is selected.  Note that removing a group doesn't affect
643*4882a593Smuzhiyun  * any other resources.
644*4882a593Smuzhiyun  */
devres_remove_group(struct device * dev,void * id)645*4882a593Smuzhiyun void devres_remove_group(struct device *dev, void *id)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun 	struct devres_group *grp;
648*4882a593Smuzhiyun 	unsigned long flags;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	grp = find_group(dev, id);
653*4882a593Smuzhiyun 	if (grp) {
654*4882a593Smuzhiyun 		list_del_init(&grp->node[0].entry);
655*4882a593Smuzhiyun 		list_del_init(&grp->node[1].entry);
656*4882a593Smuzhiyun 		devres_log(dev, &grp->node[0], "REM");
657*4882a593Smuzhiyun 	} else
658*4882a593Smuzhiyun 		WARN_ON(1);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	kfree(grp);
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_remove_group);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun /**
667*4882a593Smuzhiyun  * devres_release_group - Release resources in a devres group
668*4882a593Smuzhiyun  * @dev: Device to release group for
669*4882a593Smuzhiyun  * @id: ID of target group, can be NULL
670*4882a593Smuzhiyun  *
671*4882a593Smuzhiyun  * Release all resources in the group identified by @id.  If @id is
672*4882a593Smuzhiyun  * NULL, the latest open group is selected.  The selected group and
673*4882a593Smuzhiyun  * groups properly nested inside the selected group are removed.
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  * RETURNS:
676*4882a593Smuzhiyun  * The number of released non-group resources.
677*4882a593Smuzhiyun  */
devres_release_group(struct device * dev,void * id)678*4882a593Smuzhiyun int devres_release_group(struct device *dev, void *id)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	struct devres_group *grp;
681*4882a593Smuzhiyun 	unsigned long flags;
682*4882a593Smuzhiyun 	int cnt = 0;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	grp = find_group(dev, id);
687*4882a593Smuzhiyun 	if (grp) {
688*4882a593Smuzhiyun 		struct list_head *first = &grp->node[0].entry;
689*4882a593Smuzhiyun 		struct list_head *end = &dev->devres_head;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 		if (!list_empty(&grp->node[1].entry))
692*4882a593Smuzhiyun 			end = grp->node[1].entry.next;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 		cnt = release_nodes(dev, first, end, flags);
695*4882a593Smuzhiyun 	} else {
696*4882a593Smuzhiyun 		WARN_ON(1);
697*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dev->devres_lock, flags);
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	return cnt;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devres_release_group);
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun /*
705*4882a593Smuzhiyun  * Custom devres actions allow inserting a simple function call
706*4882a593Smuzhiyun  * into the teadown sequence.
707*4882a593Smuzhiyun  */
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun struct action_devres {
710*4882a593Smuzhiyun 	void *data;
711*4882a593Smuzhiyun 	void (*action)(void *);
712*4882a593Smuzhiyun };
713*4882a593Smuzhiyun 
devm_action_match(struct device * dev,void * res,void * p)714*4882a593Smuzhiyun static int devm_action_match(struct device *dev, void *res, void *p)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	struct action_devres *devres = res;
717*4882a593Smuzhiyun 	struct action_devres *target = p;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	return devres->action == target->action &&
720*4882a593Smuzhiyun 	       devres->data == target->data;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
devm_action_release(struct device * dev,void * res)723*4882a593Smuzhiyun static void devm_action_release(struct device *dev, void *res)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	struct action_devres *devres = res;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	devres->action(devres->data);
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun /**
731*4882a593Smuzhiyun  * devm_add_action() - add a custom action to list of managed resources
732*4882a593Smuzhiyun  * @dev: Device that owns the action
733*4882a593Smuzhiyun  * @action: Function that should be called
734*4882a593Smuzhiyun  * @data: Pointer to data passed to @action implementation
735*4882a593Smuzhiyun  *
736*4882a593Smuzhiyun  * This adds a custom action to the list of managed resources so that
737*4882a593Smuzhiyun  * it gets executed as part of standard resource unwinding.
738*4882a593Smuzhiyun  */
devm_add_action(struct device * dev,void (* action)(void *),void * data)739*4882a593Smuzhiyun int devm_add_action(struct device *dev, void (*action)(void *), void *data)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun 	struct action_devres *devres;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	devres = devres_alloc(devm_action_release,
744*4882a593Smuzhiyun 			      sizeof(struct action_devres), GFP_KERNEL);
745*4882a593Smuzhiyun 	if (!devres)
746*4882a593Smuzhiyun 		return -ENOMEM;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	devres->data = data;
749*4882a593Smuzhiyun 	devres->action = action;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	devres_add(dev, devres);
752*4882a593Smuzhiyun 	return 0;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_add_action);
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun /**
757*4882a593Smuzhiyun  * devm_remove_action() - removes previously added custom action
758*4882a593Smuzhiyun  * @dev: Device that owns the action
759*4882a593Smuzhiyun  * @action: Function implementing the action
760*4882a593Smuzhiyun  * @data: Pointer to data passed to @action implementation
761*4882a593Smuzhiyun  *
762*4882a593Smuzhiyun  * Removes instance of @action previously added by devm_add_action().
763*4882a593Smuzhiyun  * Both action and data should match one of the existing entries.
764*4882a593Smuzhiyun  */
devm_remove_action(struct device * dev,void (* action)(void *),void * data)765*4882a593Smuzhiyun void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	struct action_devres devres = {
768*4882a593Smuzhiyun 		.data = data,
769*4882a593Smuzhiyun 		.action = action,
770*4882a593Smuzhiyun 	};
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
773*4882a593Smuzhiyun 			       &devres));
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_remove_action);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun /**
778*4882a593Smuzhiyun  * devm_release_action() - release previously added custom action
779*4882a593Smuzhiyun  * @dev: Device that owns the action
780*4882a593Smuzhiyun  * @action: Function implementing the action
781*4882a593Smuzhiyun  * @data: Pointer to data passed to @action implementation
782*4882a593Smuzhiyun  *
783*4882a593Smuzhiyun  * Releases and removes instance of @action previously added by
784*4882a593Smuzhiyun  * devm_add_action().  Both action and data should match one of the
785*4882a593Smuzhiyun  * existing entries.
786*4882a593Smuzhiyun  */
devm_release_action(struct device * dev,void (* action)(void *),void * data)787*4882a593Smuzhiyun void devm_release_action(struct device *dev, void (*action)(void *), void *data)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun 	struct action_devres devres = {
790*4882a593Smuzhiyun 		.data = data,
791*4882a593Smuzhiyun 		.action = action,
792*4882a593Smuzhiyun 	};
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	WARN_ON(devres_release(dev, devm_action_release, devm_action_match,
795*4882a593Smuzhiyun 			       &devres));
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_release_action);
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun  * Managed kmalloc/kfree
802*4882a593Smuzhiyun  */
devm_kmalloc_release(struct device * dev,void * res)803*4882a593Smuzhiyun static void devm_kmalloc_release(struct device *dev, void *res)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	/* noop */
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
devm_kmalloc_match(struct device * dev,void * res,void * data)808*4882a593Smuzhiyun static int devm_kmalloc_match(struct device *dev, void *res, void *data)
809*4882a593Smuzhiyun {
810*4882a593Smuzhiyun 	return res == data;
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun /**
814*4882a593Smuzhiyun  * devm_kmalloc - Resource-managed kmalloc
815*4882a593Smuzhiyun  * @dev: Device to allocate memory for
816*4882a593Smuzhiyun  * @size: Allocation size
817*4882a593Smuzhiyun  * @gfp: Allocation gfp flags
818*4882a593Smuzhiyun  *
819*4882a593Smuzhiyun  * Managed kmalloc.  Memory allocated with this function is
820*4882a593Smuzhiyun  * automatically freed on driver detach.  Like all other devres
821*4882a593Smuzhiyun  * resources, guaranteed alignment is unsigned long long.
822*4882a593Smuzhiyun  *
823*4882a593Smuzhiyun  * RETURNS:
824*4882a593Smuzhiyun  * Pointer to allocated memory on success, NULL on failure.
825*4882a593Smuzhiyun  */
devm_kmalloc(struct device * dev,size_t size,gfp_t gfp)826*4882a593Smuzhiyun void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun 	struct devres *dr;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	if (unlikely(!size))
831*4882a593Smuzhiyun 		return ZERO_SIZE_PTR;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	/* use raw alloc_dr for kmalloc caller tracing */
834*4882a593Smuzhiyun 	dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
835*4882a593Smuzhiyun 	if (unlikely(!dr))
836*4882a593Smuzhiyun 		return NULL;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	/*
839*4882a593Smuzhiyun 	 * This is named devm_kzalloc_release for historical reasons
840*4882a593Smuzhiyun 	 * The initial implementation did not support kmalloc, only kzalloc
841*4882a593Smuzhiyun 	 */
842*4882a593Smuzhiyun 	set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
843*4882a593Smuzhiyun 	devres_add(dev, dr->data);
844*4882a593Smuzhiyun 	return dr->data;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_kmalloc);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun /**
849*4882a593Smuzhiyun  * devm_krealloc - Resource-managed krealloc()
850*4882a593Smuzhiyun  * @dev: Device to re-allocate memory for
851*4882a593Smuzhiyun  * @ptr: Pointer to the memory chunk to re-allocate
852*4882a593Smuzhiyun  * @new_size: New allocation size
853*4882a593Smuzhiyun  * @gfp: Allocation gfp flags
854*4882a593Smuzhiyun  *
855*4882a593Smuzhiyun  * Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc().
856*4882a593Smuzhiyun  * Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR,
857*4882a593Smuzhiyun  * it's the equivalent of devm_kmalloc(). If new_size is zero, it frees the
858*4882a593Smuzhiyun  * previously allocated memory and returns ZERO_SIZE_PTR. This function doesn't
859*4882a593Smuzhiyun  * change the order in which the release callback for the re-alloc'ed devres
860*4882a593Smuzhiyun  * will be called (except when falling back to devm_kmalloc() or when freeing
861*4882a593Smuzhiyun  * resources when new_size is zero). The contents of the memory are preserved
862*4882a593Smuzhiyun  * up to the lesser of new and old sizes.
863*4882a593Smuzhiyun  */
devm_krealloc(struct device * dev,void * ptr,size_t new_size,gfp_t gfp)864*4882a593Smuzhiyun void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	size_t total_new_size, total_old_size;
867*4882a593Smuzhiyun 	struct devres *old_dr, *new_dr;
868*4882a593Smuzhiyun 	unsigned long flags;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	if (unlikely(!new_size)) {
871*4882a593Smuzhiyun 		devm_kfree(dev, ptr);
872*4882a593Smuzhiyun 		return ZERO_SIZE_PTR;
873*4882a593Smuzhiyun 	}
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	if (unlikely(ZERO_OR_NULL_PTR(ptr)))
876*4882a593Smuzhiyun 		return devm_kmalloc(dev, new_size, gfp);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	if (WARN_ON(is_kernel_rodata((unsigned long)ptr)))
879*4882a593Smuzhiyun 		/*
880*4882a593Smuzhiyun 		 * We cannot reliably realloc a const string returned by
881*4882a593Smuzhiyun 		 * devm_kstrdup_const().
882*4882a593Smuzhiyun 		 */
883*4882a593Smuzhiyun 		return NULL;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	if (!check_dr_size(new_size, &total_new_size))
886*4882a593Smuzhiyun 		return NULL;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	total_old_size = ksize(container_of(ptr, struct devres, data));
889*4882a593Smuzhiyun 	if (total_old_size == 0) {
890*4882a593Smuzhiyun 		WARN(1, "Pointer doesn't point to dynamically allocated memory.");
891*4882a593Smuzhiyun 		return NULL;
892*4882a593Smuzhiyun 	}
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	/*
895*4882a593Smuzhiyun 	 * If new size is smaller or equal to the actual number of bytes
896*4882a593Smuzhiyun 	 * allocated previously - just return the same pointer.
897*4882a593Smuzhiyun 	 */
898*4882a593Smuzhiyun 	if (total_new_size <= total_old_size)
899*4882a593Smuzhiyun 		return ptr;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/*
902*4882a593Smuzhiyun 	 * Otherwise: allocate new, larger chunk. We need to allocate before
903*4882a593Smuzhiyun 	 * taking the lock as most probably the caller uses GFP_KERNEL.
904*4882a593Smuzhiyun 	 */
905*4882a593Smuzhiyun 	new_dr = alloc_dr(devm_kmalloc_release,
906*4882a593Smuzhiyun 			  total_new_size, gfp, dev_to_node(dev));
907*4882a593Smuzhiyun 	if (!new_dr)
908*4882a593Smuzhiyun 		return NULL;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	/*
911*4882a593Smuzhiyun 	 * The spinlock protects the linked list against concurrent
912*4882a593Smuzhiyun 	 * modifications but not the resource itself.
913*4882a593Smuzhiyun 	 */
914*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->devres_lock, flags);
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr);
917*4882a593Smuzhiyun 	if (!old_dr) {
918*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dev->devres_lock, flags);
919*4882a593Smuzhiyun 		kfree(new_dr);
920*4882a593Smuzhiyun 		WARN(1, "Memory chunk not managed or managed by a different device.");
921*4882a593Smuzhiyun 		return NULL;
922*4882a593Smuzhiyun 	}
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	replace_dr(dev, &old_dr->node, &new_dr->node);
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->devres_lock, flags);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/*
929*4882a593Smuzhiyun 	 * We can copy the memory contents after releasing the lock as we're
930*4882a593Smuzhiyun 	 * no longer modyfing the list links.
931*4882a593Smuzhiyun 	 */
932*4882a593Smuzhiyun 	memcpy(new_dr->data, old_dr->data,
933*4882a593Smuzhiyun 	       total_old_size - offsetof(struct devres, data));
934*4882a593Smuzhiyun 	/*
935*4882a593Smuzhiyun 	 * Same for releasing the old devres - it's now been removed from the
936*4882a593Smuzhiyun 	 * list. This is also the reason why we must not use devm_kfree() - the
937*4882a593Smuzhiyun 	 * links are no longer valid.
938*4882a593Smuzhiyun 	 */
939*4882a593Smuzhiyun 	kfree(old_dr);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	return new_dr->data;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_krealloc);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun /**
946*4882a593Smuzhiyun  * devm_kstrdup - Allocate resource managed space and
947*4882a593Smuzhiyun  *                copy an existing string into that.
948*4882a593Smuzhiyun  * @dev: Device to allocate memory for
949*4882a593Smuzhiyun  * @s: the string to duplicate
950*4882a593Smuzhiyun  * @gfp: the GFP mask used in the devm_kmalloc() call when
951*4882a593Smuzhiyun  *       allocating memory
952*4882a593Smuzhiyun  * RETURNS:
953*4882a593Smuzhiyun  * Pointer to allocated string on success, NULL on failure.
954*4882a593Smuzhiyun  */
devm_kstrdup(struct device * dev,const char * s,gfp_t gfp)955*4882a593Smuzhiyun char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
956*4882a593Smuzhiyun {
957*4882a593Smuzhiyun 	size_t size;
958*4882a593Smuzhiyun 	char *buf;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	if (!s)
961*4882a593Smuzhiyun 		return NULL;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	size = strlen(s) + 1;
964*4882a593Smuzhiyun 	buf = devm_kmalloc(dev, size, gfp);
965*4882a593Smuzhiyun 	if (buf)
966*4882a593Smuzhiyun 		memcpy(buf, s, size);
967*4882a593Smuzhiyun 	return buf;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_kstrdup);
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun /**
972*4882a593Smuzhiyun  * devm_kstrdup_const - resource managed conditional string duplication
973*4882a593Smuzhiyun  * @dev: device for which to duplicate the string
974*4882a593Smuzhiyun  * @s: the string to duplicate
975*4882a593Smuzhiyun  * @gfp: the GFP mask used in the kmalloc() call when allocating memory
976*4882a593Smuzhiyun  *
977*4882a593Smuzhiyun  * Strings allocated by devm_kstrdup_const will be automatically freed when
978*4882a593Smuzhiyun  * the associated device is detached.
979*4882a593Smuzhiyun  *
980*4882a593Smuzhiyun  * RETURNS:
981*4882a593Smuzhiyun  * Source string if it is in .rodata section otherwise it falls back to
982*4882a593Smuzhiyun  * devm_kstrdup.
983*4882a593Smuzhiyun  */
devm_kstrdup_const(struct device * dev,const char * s,gfp_t gfp)984*4882a593Smuzhiyun const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
985*4882a593Smuzhiyun {
986*4882a593Smuzhiyun 	if (is_kernel_rodata((unsigned long)s))
987*4882a593Smuzhiyun 		return s;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	return devm_kstrdup(dev, s, gfp);
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_kstrdup_const);
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun /**
994*4882a593Smuzhiyun  * devm_kvasprintf - Allocate resource managed space and format a string
995*4882a593Smuzhiyun  *		     into that.
996*4882a593Smuzhiyun  * @dev: Device to allocate memory for
997*4882a593Smuzhiyun  * @gfp: the GFP mask used in the devm_kmalloc() call when
998*4882a593Smuzhiyun  *       allocating memory
999*4882a593Smuzhiyun  * @fmt: The printf()-style format string
1000*4882a593Smuzhiyun  * @ap: Arguments for the format string
1001*4882a593Smuzhiyun  * RETURNS:
1002*4882a593Smuzhiyun  * Pointer to allocated string on success, NULL on failure.
1003*4882a593Smuzhiyun  */
devm_kvasprintf(struct device * dev,gfp_t gfp,const char * fmt,va_list ap)1004*4882a593Smuzhiyun char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
1005*4882a593Smuzhiyun 		      va_list ap)
1006*4882a593Smuzhiyun {
1007*4882a593Smuzhiyun 	unsigned int len;
1008*4882a593Smuzhiyun 	char *p;
1009*4882a593Smuzhiyun 	va_list aq;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	va_copy(aq, ap);
1012*4882a593Smuzhiyun 	len = vsnprintf(NULL, 0, fmt, aq);
1013*4882a593Smuzhiyun 	va_end(aq);
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	p = devm_kmalloc(dev, len+1, gfp);
1016*4882a593Smuzhiyun 	if (!p)
1017*4882a593Smuzhiyun 		return NULL;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	vsnprintf(p, len+1, fmt, ap);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	return p;
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun EXPORT_SYMBOL(devm_kvasprintf);
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun /**
1026*4882a593Smuzhiyun  * devm_kasprintf - Allocate resource managed space and format a string
1027*4882a593Smuzhiyun  *		    into that.
1028*4882a593Smuzhiyun  * @dev: Device to allocate memory for
1029*4882a593Smuzhiyun  * @gfp: the GFP mask used in the devm_kmalloc() call when
1030*4882a593Smuzhiyun  *       allocating memory
1031*4882a593Smuzhiyun  * @fmt: The printf()-style format string
1032*4882a593Smuzhiyun  * @...: Arguments for the format string
1033*4882a593Smuzhiyun  * RETURNS:
1034*4882a593Smuzhiyun  * Pointer to allocated string on success, NULL on failure.
1035*4882a593Smuzhiyun  */
devm_kasprintf(struct device * dev,gfp_t gfp,const char * fmt,...)1036*4882a593Smuzhiyun char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun 	va_list ap;
1039*4882a593Smuzhiyun 	char *p;
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	va_start(ap, fmt);
1042*4882a593Smuzhiyun 	p = devm_kvasprintf(dev, gfp, fmt, ap);
1043*4882a593Smuzhiyun 	va_end(ap);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	return p;
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_kasprintf);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun /**
1050*4882a593Smuzhiyun  * devm_kfree - Resource-managed kfree
1051*4882a593Smuzhiyun  * @dev: Device this memory belongs to
1052*4882a593Smuzhiyun  * @p: Memory to free
1053*4882a593Smuzhiyun  *
1054*4882a593Smuzhiyun  * Free memory allocated with devm_kmalloc().
1055*4882a593Smuzhiyun  */
devm_kfree(struct device * dev,const void * p)1056*4882a593Smuzhiyun void devm_kfree(struct device *dev, const void *p)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun 	int rc;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	/*
1061*4882a593Smuzhiyun 	 * Special cases: pointer to a string in .rodata returned by
1062*4882a593Smuzhiyun 	 * devm_kstrdup_const() or NULL/ZERO ptr.
1063*4882a593Smuzhiyun 	 */
1064*4882a593Smuzhiyun 	if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p)))
1065*4882a593Smuzhiyun 		return;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	rc = devres_destroy(dev, devm_kmalloc_release,
1068*4882a593Smuzhiyun 			    devm_kmalloc_match, (void *)p);
1069*4882a593Smuzhiyun 	WARN_ON(rc);
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_kfree);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun /**
1074*4882a593Smuzhiyun  * devm_kmemdup - Resource-managed kmemdup
1075*4882a593Smuzhiyun  * @dev: Device this memory belongs to
1076*4882a593Smuzhiyun  * @src: Memory region to duplicate
1077*4882a593Smuzhiyun  * @len: Memory region length
1078*4882a593Smuzhiyun  * @gfp: GFP mask to use
1079*4882a593Smuzhiyun  *
1080*4882a593Smuzhiyun  * Duplicate region of a memory using resource managed kmalloc
1081*4882a593Smuzhiyun  */
devm_kmemdup(struct device * dev,const void * src,size_t len,gfp_t gfp)1082*4882a593Smuzhiyun void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	void *p;
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	p = devm_kmalloc(dev, len, gfp);
1087*4882a593Smuzhiyun 	if (p)
1088*4882a593Smuzhiyun 		memcpy(p, src, len);
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	return p;
1091*4882a593Smuzhiyun }
1092*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_kmemdup);
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun struct pages_devres {
1095*4882a593Smuzhiyun 	unsigned long addr;
1096*4882a593Smuzhiyun 	unsigned int order;
1097*4882a593Smuzhiyun };
1098*4882a593Smuzhiyun 
devm_pages_match(struct device * dev,void * res,void * p)1099*4882a593Smuzhiyun static int devm_pages_match(struct device *dev, void *res, void *p)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun 	struct pages_devres *devres = res;
1102*4882a593Smuzhiyun 	struct pages_devres *target = p;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	return devres->addr == target->addr;
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun 
devm_pages_release(struct device * dev,void * res)1107*4882a593Smuzhiyun static void devm_pages_release(struct device *dev, void *res)
1108*4882a593Smuzhiyun {
1109*4882a593Smuzhiyun 	struct pages_devres *devres = res;
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	free_pages(devres->addr, devres->order);
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /**
1115*4882a593Smuzhiyun  * devm_get_free_pages - Resource-managed __get_free_pages
1116*4882a593Smuzhiyun  * @dev: Device to allocate memory for
1117*4882a593Smuzhiyun  * @gfp_mask: Allocation gfp flags
1118*4882a593Smuzhiyun  * @order: Allocation size is (1 << order) pages
1119*4882a593Smuzhiyun  *
1120*4882a593Smuzhiyun  * Managed get_free_pages.  Memory allocated with this function is
1121*4882a593Smuzhiyun  * automatically freed on driver detach.
1122*4882a593Smuzhiyun  *
1123*4882a593Smuzhiyun  * RETURNS:
1124*4882a593Smuzhiyun  * Address of allocated memory on success, 0 on failure.
1125*4882a593Smuzhiyun  */
1126*4882a593Smuzhiyun 
devm_get_free_pages(struct device * dev,gfp_t gfp_mask,unsigned int order)1127*4882a593Smuzhiyun unsigned long devm_get_free_pages(struct device *dev,
1128*4882a593Smuzhiyun 				  gfp_t gfp_mask, unsigned int order)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	struct pages_devres *devres;
1131*4882a593Smuzhiyun 	unsigned long addr;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	addr = __get_free_pages(gfp_mask, order);
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	if (unlikely(!addr))
1136*4882a593Smuzhiyun 		return 0;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	devres = devres_alloc(devm_pages_release,
1139*4882a593Smuzhiyun 			      sizeof(struct pages_devres), GFP_KERNEL);
1140*4882a593Smuzhiyun 	if (unlikely(!devres)) {
1141*4882a593Smuzhiyun 		free_pages(addr, order);
1142*4882a593Smuzhiyun 		return 0;
1143*4882a593Smuzhiyun 	}
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	devres->addr = addr;
1146*4882a593Smuzhiyun 	devres->order = order;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	devres_add(dev, devres);
1149*4882a593Smuzhiyun 	return addr;
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_get_free_pages);
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun /**
1154*4882a593Smuzhiyun  * devm_free_pages - Resource-managed free_pages
1155*4882a593Smuzhiyun  * @dev: Device this memory belongs to
1156*4882a593Smuzhiyun  * @addr: Memory to free
1157*4882a593Smuzhiyun  *
1158*4882a593Smuzhiyun  * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
1159*4882a593Smuzhiyun  * there is no need to supply the @order.
1160*4882a593Smuzhiyun  */
devm_free_pages(struct device * dev,unsigned long addr)1161*4882a593Smuzhiyun void devm_free_pages(struct device *dev, unsigned long addr)
1162*4882a593Smuzhiyun {
1163*4882a593Smuzhiyun 	struct pages_devres devres = { .addr = addr };
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
1166*4882a593Smuzhiyun 			       &devres));
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_free_pages);
1169*4882a593Smuzhiyun 
devm_percpu_release(struct device * dev,void * pdata)1170*4882a593Smuzhiyun static void devm_percpu_release(struct device *dev, void *pdata)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun 	void __percpu *p;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	p = *(void __percpu **)pdata;
1175*4882a593Smuzhiyun 	free_percpu(p);
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun 
devm_percpu_match(struct device * dev,void * data,void * p)1178*4882a593Smuzhiyun static int devm_percpu_match(struct device *dev, void *data, void *p)
1179*4882a593Smuzhiyun {
1180*4882a593Smuzhiyun 	struct devres *devr = container_of(data, struct devres, data);
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	return *(void **)devr->data == p;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun /**
1186*4882a593Smuzhiyun  * __devm_alloc_percpu - Resource-managed alloc_percpu
1187*4882a593Smuzhiyun  * @dev: Device to allocate per-cpu memory for
1188*4882a593Smuzhiyun  * @size: Size of per-cpu memory to allocate
1189*4882a593Smuzhiyun  * @align: Alignment of per-cpu memory to allocate
1190*4882a593Smuzhiyun  *
1191*4882a593Smuzhiyun  * Managed alloc_percpu. Per-cpu memory allocated with this function is
1192*4882a593Smuzhiyun  * automatically freed on driver detach.
1193*4882a593Smuzhiyun  *
1194*4882a593Smuzhiyun  * RETURNS:
1195*4882a593Smuzhiyun  * Pointer to allocated memory on success, NULL on failure.
1196*4882a593Smuzhiyun  */
__devm_alloc_percpu(struct device * dev,size_t size,size_t align)1197*4882a593Smuzhiyun void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
1198*4882a593Smuzhiyun 		size_t align)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun 	void *p;
1201*4882a593Smuzhiyun 	void __percpu *pcpu;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	pcpu = __alloc_percpu(size, align);
1204*4882a593Smuzhiyun 	if (!pcpu)
1205*4882a593Smuzhiyun 		return NULL;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
1208*4882a593Smuzhiyun 	if (!p) {
1209*4882a593Smuzhiyun 		free_percpu(pcpu);
1210*4882a593Smuzhiyun 		return NULL;
1211*4882a593Smuzhiyun 	}
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 	*(void __percpu **)p = pcpu;
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	devres_add(dev, p);
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	return pcpu;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__devm_alloc_percpu);
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun /**
1222*4882a593Smuzhiyun  * devm_free_percpu - Resource-managed free_percpu
1223*4882a593Smuzhiyun  * @dev: Device this memory belongs to
1224*4882a593Smuzhiyun  * @pdata: Per-cpu memory to free
1225*4882a593Smuzhiyun  *
1226*4882a593Smuzhiyun  * Free memory allocated with devm_alloc_percpu().
1227*4882a593Smuzhiyun  */
devm_free_percpu(struct device * dev,void __percpu * pdata)1228*4882a593Smuzhiyun void devm_free_percpu(struct device *dev, void __percpu *pdata)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun 	WARN_ON(devres_destroy(dev, devm_percpu_release, devm_percpu_match,
1231*4882a593Smuzhiyun 			       (void *)pdata));
1232*4882a593Smuzhiyun }
1233*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_free_percpu);
1234