Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
14struct device;
15struct device_node;
16struct resource;
17
18/* device resource management */
19typedef void (*dr_release_t)(struct device *dev, void *res);
20typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
21
22void * __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
29void devres_free(void *res);
30void devres_add(struct device *dev, void *res);
31void *devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
32void *devres_get(struct device *dev, void *new_res, dr_match_t match, void *match_data);
33void *devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
34int devres_destroy(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
35int devres_release(struct device *dev, dr_release_t release, dr_match_t match, void *match_data);
36
37/* devres group */
38void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
39void devres_close_group(struct device *dev, void *id);
40void devres_remove_group(struct device *dev, void *id);
41int devres_release_group(struct device *dev, void *id);
42
43/* managed devm_k.alloc/kfree for device drivers */
44void * __alloc_size(2)
45devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
46void * __must_check __realloc_size(3)
47devm_krealloc(struct device *dev, void *ptr, size_t size, gfp_t gfp);
48static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
49{
50 return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
51}
52static 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}
61static 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}
65static inline __realloc_size(3, 4) void * __must_check
66devm_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
76void devm_kfree(struct device *dev, const void *p);
77
78void * __realloc_size(3)
79devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
80const void *
81devm_kmemdup_const(struct device *dev, const void *src, size_t len, gfp_t gfp);
82static 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
88char * __malloc
89devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
90const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
91char * __printf(3, 0) __malloc
92devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap);
93char * __printf(3, 4) __malloc
94devm_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
110void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, size_t align);
111
112unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order);
113void devm_free_pages(struct device *dev, unsigned long addr);
114
115#ifdef CONFIG_HAS_IOMEM
116
117void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res);
118void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res);
119
120void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
121 resource_size_t *size);
122#else
123
124static inline
125void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res)
126{
127 return IOMEM_ERR_PTR(-EINVAL);
128}
129
130static inline
131void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res)
132{
133 return IOMEM_ERR_PTR(-EINVAL);
134}
135
136static inline
137void __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 */
146int 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 */
157static inline
158void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
159{
160 WARN_ON(devm_remove_action_nowarn(dev, action, data));
161}
162
163void devm_release_action(struct device *dev, void (*action)(void *), void *data);
164
165int __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
169static 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
183bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data);
184
185#endif /* _DEVICE_DEVRES_H_ */