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