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