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 13 struct device; 14 struct device_node; 15 struct resource; 16 17 /* device resource management */ 18 typedef void (*dr_release_t)(struct device *dev, void *res); 19 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); 20 21 void * __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 28 void 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); 32 void devres_free(void *res); 33 void devres_add(struct device *dev, void *res); 34 void *devres_find(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); 35 void *devres_get(struct device *dev, void *new_res, dr_match_t match, void *match_data); 36 void *devres_remove(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); 37 int devres_destroy(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); 38 int devres_release(struct device *dev, dr_release_t release, dr_match_t match, void *match_data); 39 40 /* devres group */ 41 void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp); 42 void devres_close_group(struct device *dev, void *id); 43 void devres_remove_group(struct device *dev, void *id); 44 int devres_release_group(struct device *dev, void *id); 45 46 /* managed devm_k.alloc/kfree for device drivers */ 47 void * __alloc_size(2) 48 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp); 49 void * __must_check __realloc_size(3) 50 devm_krealloc(struct device *dev, void *ptr, size_t size, gfp_t gfp); 51 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) 52 { 53 return devm_kmalloc(dev, size, gfp | __GFP_ZERO); 54 } 55 static 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, bytes, flags); 63 } 64 static 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 | __GFP_ZERO); 67 } 68 static inline __realloc_size(3, 4) void * __must_check 69 devm_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, p, bytes, flags); 77 } 78 79 void devm_kfree(struct device *dev, const void *p); 80 81 void * __realloc_size(3) 82 devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp); 83 const void * 84 devm_kmemdup_const(struct device *dev, const void *src, size_t len, gfp_t gfp); 85 static 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, size_mul(size, n), flags); 89 } 90 91 char * __malloc 92 devm_kstrdup(struct device *dev, const char *s, gfp_t gfp); 93 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); 94 char * __printf(3, 0) __malloc 95 devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap); 96 char * __printf(3, 4) __malloc 97 devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...); 98 99 unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order); 100 void devm_free_pages(struct device *dev, unsigned long addr); 101 102 #ifdef CONFIG_HAS_IOMEM 103 104 void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res); 105 void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res); 106 107 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index, 108 resource_size_t *size); 109 #else 110 111 static inline 112 void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res) 113 { 114 return IOMEM_ERR_PTR(-EINVAL); 115 } 116 117 static inline 118 void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res) 119 { 120 return IOMEM_ERR_PTR(-EINVAL); 121 } 122 123 static inline 124 void __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 */ 133 int 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 */ 144 static inline 145 void devm_remove_action(struct device *dev, void (*action)(void *), void *data) 146 { 147 WARN_ON(devm_remove_action_nowarn(dev, action, data)); 148 } 149 150 void devm_release_action(struct device *dev, void (*action)(void *), void *data); 151 152 int __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 156 static 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 170 bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data); 171 172 #endif /* _DEVICE_DEVRES_H_ */ 173