at v6.17-rc4 5.9 kB view raw
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_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, bytes, 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 | __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, p, bytes, 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); 83static inline void *devm_kmemdup_array(struct device *dev, const void *src, 84 size_t n, size_t size, gfp_t flags) 85{ 86 return devm_kmemdup(dev, src, size_mul(size, n), flags); 87} 88 89char * __malloc 90devm_kstrdup(struct device *dev, const char *s, gfp_t gfp); 91const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); 92char * __printf(3, 0) __malloc 93devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap); 94char * __printf(3, 4) __malloc 95devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...); 96 97unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order); 98void devm_free_pages(struct device *dev, unsigned long addr); 99 100#ifdef CONFIG_HAS_IOMEM 101 102void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res); 103void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res); 104 105void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index, 106 resource_size_t *size); 107#else 108 109static inline 110void __iomem *devm_ioremap_resource(struct device *dev, const struct resource *res) 111{ 112 return IOMEM_ERR_PTR(-EINVAL); 113} 114 115static inline 116void __iomem *devm_ioremap_resource_wc(struct device *dev, const struct resource *res) 117{ 118 return IOMEM_ERR_PTR(-EINVAL); 119} 120 121static inline 122void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index, 123 resource_size_t *size) 124{ 125 return IOMEM_ERR_PTR(-EINVAL); 126} 127 128#endif 129 130/* allows to add/remove a custom action to devres stack */ 131int devm_remove_action_nowarn(struct device *dev, void (*action)(void *), void *data); 132 133/** 134 * devm_remove_action() - removes previously added custom action 135 * @dev: Device that owns the action 136 * @action: Function implementing the action 137 * @data: Pointer to data passed to @action implementation 138 * 139 * Removes instance of @action previously added by devm_add_action(). 140 * Both action and data should match one of the existing entries. 141 */ 142static inline 143void devm_remove_action(struct device *dev, void (*action)(void *), void *data) 144{ 145 WARN_ON(devm_remove_action_nowarn(dev, action, data)); 146} 147 148void devm_release_action(struct device *dev, void (*action)(void *), void *data); 149 150int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name); 151#define devm_add_action(dev, action, data) \ 152 __devm_add_action(dev, action, data, #action) 153 154static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *), 155 void *data, const char *name) 156{ 157 int ret; 158 159 ret = __devm_add_action(dev, action, data, name); 160 if (ret) 161 action(data); 162 163 return ret; 164} 165#define devm_add_action_or_reset(dev, action, data) \ 166 __devm_add_action_or_reset(dev, action, data, #action) 167 168bool devm_is_action_added(struct device *dev, void (*action)(void *), void *data); 169 170#endif /* _DEVICE_DEVRES_H_ */