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
13struct device;
14struct device_node;
15struct resource;
16
17/* device resource management */
18typedef void (*dr_release_t)(struct device *dev, void *res);
19typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
20
21void * __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
28void 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);
32void devres_free(void *res);
33void devres_add(struct device *dev, void *res);
34void *devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
35void *devres_get(struct device *dev, void *new_res, dr_match_t match, void *match_data);
36void *devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
37int devres_destroy(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
38int devres_release(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
39
40/* devres group */
41void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
42void devres_close_group(struct device *dev, void *id);
43void devres_remove_group(struct device *dev, void *id);
44int devres_release_group(struct device *dev, void *id);
45
46/* managed devm_k.alloc/kfree for device drivers */
47void * __alloc_size(2)
48devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
49void * __must_check __realloc_size(3)
50devm_krealloc(struct device *dev, void *ptr, size_t size, gfp_t gfp);
51static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
52{
53 return devm_kmalloc(dev, size, gfp: gfp | __GFP_ZERO);
54}
55static 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, size: bytes, gfp: flags);
63}
64static 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: flags | __GFP_ZERO);
67}
68static inline __realloc_size(3, 4) void * __must_check
69devm_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, ptr: p, size: bytes, gfp: flags);
77}
78
79void devm_kfree(struct device *dev, const void *p);
80
81void * __realloc_size(3)
82devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
83const void *
84devm_kmemdup_const(struct device *dev, const void *src, size_t len, gfp_t gfp);
85static inline void *devm_kmemdup_array(struct device *dev, const void *src,
86 size_t n, size_t size, gfp_t flags)
87{
88 return devm_kmemdup(dev, src, len: size_mul(factor1: size, factor2: n), gfp: flags);
89}
90
91char * __malloc
92devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
93const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
94char * __printf(3, 0) __malloc
95devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap);
96char * __printf(3, 4) __malloc
97devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...);
98
99unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order);
100void devm_free_pages(struct device *dev, unsigned long addr);
101
102#ifdef CONFIG_HAS_IOMEM
103
104void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res);
105void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res);
106
107void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
108 resource_size_t *size);
109#else
110
111static inline
112void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res)
113{
114 return IOMEM_ERR_PTR(-EINVAL);
115}
116
117static inline
118void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res)
119{
120 return IOMEM_ERR_PTR(-EINVAL);
121}
122
123static inline
124void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
125 resource_size_t *size)
126{
127 return IOMEM_ERR_PTR(-EINVAL);
128}
129
130#endif
131
132/* allows to add/remove a custom action to devres stack */
133int devm_remove_action_nowarn(struct device *dev, void (*action)(void *), void *data);
134
135/**
136 * devm_remove_action() - removes previously added custom action
137 * @dev: Device that owns the action
138 * @action: Function implementing the action
139 * @data: Pointer to data passed to @action implementation
140 *
141 * Removes instance of @action previously added by devm_add_action().
142 * Both action and data should match one of the existing entries.
143 */
144static inline
145void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
146{
147 WARN_ON(devm_remove_action_nowarn(dev, action, data));
148}
149
150void devm_release_action(struct device *dev, void (*action)(void *), void *data);
151
152int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
153#define devm_add_action(dev, action, data) \
154 __devm_add_action(dev, action, data, #action)
155
156static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
157 void *data, const char *name)
158{
159 int ret;
160
161 ret = __devm_add_action(dev, action, data, name);
162 if (ret)
163 action(data);
164
165 return ret;
166}
167#define devm_add_action_or_reset(dev, action, data) \
168 __devm_add_action_or_reset(dev, action, data, #action)
169
170bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data);
171
172#endif /* _DEVICE_DEVRES_H_ */
173