| 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_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); |
| 33 | void devres_free(void *res); |
| 34 | void devres_add(struct device *dev, void *res); |
| 35 | void *devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); |
| 36 | void *devres_get(struct device *dev, void *new_res, dr_match_t match, void *match_data); |
| 37 | void *devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); |
| 38 | int devres_destroy(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); |
| 39 | int devres_release(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); |
| 40 | |
| 41 | /* devres group */ |
| 42 | void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp); |
| 43 | void devres_close_group(struct device *dev, void *id); |
| 44 | void devres_remove_group(struct device *dev, void *id); |
| 45 | int devres_release_group(struct device *dev, void *id); |
| 46 | |
| 47 | /* managed devm_k.alloc/kfree for device drivers */ |
| 48 | void * __alloc_size(2) |
| 49 | devm_kmalloc(struct device *dev, size_t size, gfp_t gfp); |
| 50 | void * __must_check __realloc_size(3) |
| 51 | devm_krealloc(struct device *dev, void *ptr, size_t size, gfp_t gfp); |
| 52 | static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) |
| 53 | { |
| 54 | return devm_kmalloc(dev, size, gfp: gfp | __GFP_ZERO); |
| 55 | } |
| 56 | static 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, size: bytes, gfp: flags); |
| 64 | } |
| 65 | static 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: flags | __GFP_ZERO); |
| 68 | } |
| 69 | static inline __realloc_size(3, 4) void * __must_check |
| 70 | devm_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, ptr: p, size: bytes, gfp: flags); |
| 78 | } |
| 79 | |
| 80 | void devm_kfree(struct device *dev, const void *p); |
| 81 | |
| 82 | void * __realloc_size(3) |
| 83 | devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp); |
| 84 | const void * |
| 85 | devm_kmemdup_const(struct device *dev, const void *src, size_t len, gfp_t gfp); |
| 86 | static 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, len: size_mul(factor1: size, factor2: n), gfp: flags); |
| 90 | } |
| 91 | |
| 92 | char * __malloc |
| 93 | devm_kstrdup(struct device *dev, const char *s, gfp_t gfp); |
| 94 | const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); |
| 95 | char * __printf(3, 0) __malloc |
| 96 | devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap); |
| 97 | char * __printf(3, 4) __malloc |
| 98 | devm_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 | |
| 114 | void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, size_t align); |
| 115 | |
| 116 | unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order); |
| 117 | void devm_free_pages(struct device *dev, unsigned long addr); |
| 118 | |
| 119 | #ifdef CONFIG_HAS_IOMEM |
| 120 | |
| 121 | void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res); |
| 122 | void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res); |
| 123 | |
| 124 | void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index, |
| 125 | resource_size_t *size); |
| 126 | #else |
| 127 | |
| 128 | static inline |
| 129 | void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res) |
| 130 | { |
| 131 | return IOMEM_ERR_PTR(-EINVAL); |
| 132 | } |
| 133 | |
| 134 | static inline |
| 135 | void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res) |
| 136 | { |
| 137 | return IOMEM_ERR_PTR(-EINVAL); |
| 138 | } |
| 139 | |
| 140 | static inline |
| 141 | void __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 */ |
| 150 | int 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 | */ |
| 161 | static inline |
| 162 | void devm_remove_action(struct device *dev, void (*action)(void *), void *data) |
| 163 | { |
| 164 | WARN_ON(devm_remove_action_nowarn(dev, action, data)); |
| 165 | } |
| 166 | |
| 167 | void devm_release_action(struct device *dev, void (*action)(void *), void *data); |
| 168 | |
| 169 | int __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 | |
| 173 | static 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 | |
| 187 | bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data); |
| 188 | |
| 189 | #endif /* _DEVICE_DEVRES_H_ */ |
| 190 | |