xref: /linux/include/linux/device/devres.h (revision aacc73ceeb8bf664426f0e53db2778a59325bd9f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _DEVICE_DEVRES_H_
3 #define _DEVICE_DEVRES_H_
4 
5 #include <linux/err.h>
6 #include <linux/gfp_types.h>
7 #include <linux/numa.h>
8 #include <linux/overflow.h>
9 #include <linux/stdarg.h>
10 #include <linux/types.h>
11 #include <asm/bug.h>
12 
13 struct device;
14 struct device_node;
15 struct resource;
16 
17 /* device resource management */
18 typedef void (*dr_release_t)(struct device *dev, void *res);
19 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
20 
21 void * __malloc
22 __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid, const char *name);
23 #define devres_alloc(release, size, gfp) \
24 	__devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
25 #define devres_alloc_node(release, size, gfp, nid) \
26 	__devres_alloc_node(release, size, gfp, nid, #release)
27 
28 void devres_for_each_res(struct device *dev, dr_release_t release,
29 			 dr_match_t match, void *match_data,
30 			 void (*fn)(struct device *, void *, void *),
31 			 void *data);
32 void devres_free(void *res);
33 void devres_add(struct device *dev, void *res);
34 void *devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
35 void *devres_get(struct device *dev, void *new_res, dr_match_t match, void *match_data);
36 void *devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
37 int devres_destroy(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
38 int devres_release(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
39 
40 /* devres group */
41 void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
42 void devres_close_group(struct device *dev, void *id);
43 void devres_remove_group(struct device *dev, void *id);
44 int devres_release_group(struct device *dev, void *id);
45 
46 /* managed devm_k.alloc/kfree for device drivers */
47 void * __alloc_size(2)
48 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
49 void * __must_check __realloc_size(3)
50 devm_krealloc(struct device *dev, void *ptr, size_t size, gfp_t gfp);
devm_kzalloc(struct device * dev,size_t size,gfp_t gfp)51 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
52 {
53 	return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
54 }
devm_kmalloc_array(struct device * dev,size_t n,size_t size,gfp_t flags)55 static inline void *devm_kmalloc_array(struct device *dev, size_t n, size_t size, gfp_t flags)
56 {
57 	size_t bytes;
58 
59 	if (unlikely(check_mul_overflow(n, size, &bytes)))
60 		return NULL;
61 
62 	return devm_kmalloc(dev, bytes, flags);
63 }
devm_kcalloc(struct device * dev,size_t n,size_t size,gfp_t flags)64 static inline void *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_t flags)
65 {
66 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
67 }
68 static inline __realloc_size(3, 4) void * __must_check
devm_krealloc_array(struct device * dev,void * p,size_t new_n,size_t new_size,gfp_t flags)69 devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
70 {
71 	size_t bytes;
72 
73 	if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
74 		return NULL;
75 
76 	return devm_krealloc(dev, p, bytes, flags);
77 }
78 
79 void devm_kfree(struct device *dev, const void *p);
80 
81 void * __realloc_size(3)
82 devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
devm_kmemdup_array(struct device * dev,const void * src,size_t n,size_t size,gfp_t flags)83 static inline void *devm_kmemdup_array(struct device *dev, const void *src,
84 				       size_t n, size_t size, gfp_t flags)
85 {
86 	return devm_kmemdup(dev, src, size_mul(size, n), flags);
87 }
88 
89 char * __malloc
90 devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
91 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
92 char * __printf(3, 0) __malloc
93 devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap);
94 char * __printf(3, 4) __malloc
95 devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...);
96 
97 unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order);
98 void devm_free_pages(struct device *dev, unsigned long addr);
99 
100 #ifdef CONFIG_HAS_IOMEM
101 
102 void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res);
103 void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res);
104 
105 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
106 			    resource_size_t *size);
107 #else
108 
109 static inline
devm_ioremap_resource(struct device * dev,const struct resource * res)110 void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res)
111 {
112 	return IOMEM_ERR_PTR(-EINVAL);
113 }
114 
115 static inline
devm_ioremap_resource_wc(struct device * dev,const struct resource * res)116 void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res)
117 {
118 	return IOMEM_ERR_PTR(-EINVAL);
119 }
120 
121 static inline
devm_of_iomap(struct device * dev,struct device_node * node,int index,resource_size_t * size)122 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
123 			    resource_size_t *size)
124 {
125 	return IOMEM_ERR_PTR(-EINVAL);
126 }
127 
128 #endif
129 
130 /* allows to add/remove a custom action to devres stack */
131 int devm_remove_action_nowarn(struct device *dev, void (*action)(void *), void *data);
132 
133 /**
134  * devm_remove_action() - removes previously added custom action
135  * @dev: Device that owns the action
136  * @action: Function implementing the action
137  * @data: Pointer to data passed to @action implementation
138  *
139  * Removes instance of @action previously added by devm_add_action().
140  * Both action and data should match one of the existing entries.
141  */
142 static inline
devm_remove_action(struct device * dev,void (* action)(void *),void * data)143 void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
144 {
145 	WARN_ON(devm_remove_action_nowarn(dev, action, data));
146 }
147 
148 void devm_release_action(struct device *dev, void (*action)(void *), void *data);
149 
150 int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
151 #define devm_add_action(dev, action, data) \
152 	__devm_add_action(dev, action, data, #action)
153 
__devm_add_action_or_reset(struct device * dev,void (* action)(void *),void * data,const char * name)154 static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
155 					     void *data, const char *name)
156 {
157 	int ret;
158 
159 	ret = __devm_add_action(dev, action, data, name);
160 	if (ret)
161 		action(data);
162 
163 	return ret;
164 }
165 #define devm_add_action_or_reset(dev, action, data) \
166 	__devm_add_action_or_reset(dev, action, data, #action)
167 
168 bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data);
169 
170 #endif /* _DEVICE_DEVRES_H_ */
171