devres.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * Based on the original work in Linux by
  6. * Copyright (c) 2006 SUSE Linux Products GmbH
  7. * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
  8. * Copyright 2019 Google LLC
  9. */
  10. #ifndef _DM_DEVRES_H
  11. #define _DM_DEVRES_H
  12. #include <linux/compat.h>
  13. struct udevice;
  14. /* device resource management */
  15. typedef void (*dr_release_t)(struct udevice *dev, void *res);
  16. typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
  17. /**
  18. * struct devres_stats - Information about devres allocations for a device
  19. *
  20. * @allocs: Number of allocations
  21. * @total_size: Total size of allocations in bytes
  22. */
  23. struct devres_stats {
  24. int allocs;
  25. int total_size;
  26. };
  27. #ifdef CONFIG_DEVRES
  28. #ifdef CONFIG_DEBUG_DEVRES
  29. void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  30. const char *name);
  31. #define _devres_alloc(release, size, gfp) \
  32. __devres_alloc(release, size, gfp, #release)
  33. #else
  34. void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  35. #endif
  36. /**
  37. * devres_alloc() - Allocate device resource data
  38. * @release: Release function devres will be associated with
  39. * @size: Allocation size
  40. * @gfp: Allocation flags
  41. *
  42. * Allocate devres of @size bytes. The allocated area is associated
  43. * with @release. The returned pointer can be passed to
  44. * other devres_*() functions.
  45. *
  46. * RETURNS:
  47. * Pointer to allocated devres on success, NULL on failure.
  48. */
  49. #define devres_alloc(release, size, gfp) \
  50. _devres_alloc(release, size, (gfp) | __GFP_ZERO)
  51. /**
  52. * devres_free() - Free device resource data
  53. * @res: Pointer to devres data to free
  54. *
  55. * Free devres created with devres_alloc().
  56. */
  57. void devres_free(void *res);
  58. /**
  59. * devres_add() - Register device resource
  60. * @dev: Device to add resource to
  61. * @res: Resource to register
  62. *
  63. * Register devres @res to @dev. @res should have been allocated
  64. * using devres_alloc(). On driver detach, the associated release
  65. * function will be invoked and devres will be freed automatically.
  66. */
  67. void devres_add(struct udevice *dev, void *res);
  68. /**
  69. * devres_find() - Find device resource
  70. * @dev: Device to lookup resource from
  71. * @release: Look for resources associated with this release function
  72. * @match: Match function (optional)
  73. * @match_data: Data for the match function
  74. *
  75. * Find the latest devres of @dev which is associated with @release
  76. * and for which @match returns 1. If @match is NULL, it's considered
  77. * to match all.
  78. *
  79. * @return pointer to found devres, NULL if not found.
  80. */
  81. void *devres_find(struct udevice *dev, dr_release_t release,
  82. dr_match_t match, void *match_data);
  83. /**
  84. * devres_get() - Find devres, if non-existent, add one atomically
  85. * @dev: Device to lookup or add devres for
  86. * @new_res: Pointer to new initialized devres to add if not found
  87. * @match: Match function (optional)
  88. * @match_data: Data for the match function
  89. *
  90. * Find the latest devres of @dev which has the same release function
  91. * as @new_res and for which @match return 1. If found, @new_res is
  92. * freed; otherwise, @new_res is added atomically.
  93. *
  94. * @return ointer to found or added devres.
  95. */
  96. void *devres_get(struct udevice *dev, void *new_res,
  97. dr_match_t match, void *match_data);
  98. /**
  99. * devres_remove() - Find a device resource and remove it
  100. * @dev: Device to find resource from
  101. * @release: Look for resources associated with this release function
  102. * @match: Match function (optional)
  103. * @match_data: Data for the match function
  104. *
  105. * Find the latest devres of @dev associated with @release and for
  106. * which @match returns 1. If @match is NULL, it's considered to
  107. * match all. If found, the resource is removed atomically and
  108. * returned.
  109. *
  110. * @return ointer to removed devres on success, NULL if not found.
  111. */
  112. void *devres_remove(struct udevice *dev, dr_release_t release,
  113. dr_match_t match, void *match_data);
  114. /**
  115. * devres_destroy() - Find a device resource and destroy it
  116. * @dev: Device to find resource from
  117. * @release: Look for resources associated with this release function
  118. * @match: Match function (optional)
  119. * @match_data: Data for the match function
  120. *
  121. * Find the latest devres of @dev associated with @release and for
  122. * which @match returns 1. If @match is NULL, it's considered to
  123. * match all. If found, the resource is removed atomically and freed.
  124. *
  125. * Note that the release function for the resource will not be called,
  126. * only the devres-allocated data will be freed. The caller becomes
  127. * responsible for freeing any other data.
  128. *
  129. * @return 0 if devres is found and freed, -ENOENT if not found.
  130. */
  131. int devres_destroy(struct udevice *dev, dr_release_t release,
  132. dr_match_t match, void *match_data);
  133. /**
  134. * devres_release() - Find a device resource and destroy it, calling release
  135. * @dev: Device to find resource from
  136. * @release: Look for resources associated with this release function
  137. * @match: Match function (optional)
  138. * @match_data: Data for the match function
  139. *
  140. * Find the latest devres of @dev associated with @release and for
  141. * which @match returns 1. If @match is NULL, it's considered to
  142. * match all. If found, the resource is removed atomically, the
  143. * release function called and the resource freed.
  144. *
  145. * @return 0 if devres is found and freed, -ENOENT if not found.
  146. */
  147. int devres_release(struct udevice *dev, dr_release_t release,
  148. dr_match_t match, void *match_data);
  149. /* managed devm_k.alloc/kfree for device drivers */
  150. /**
  151. * devm_kmalloc() - Resource-managed kmalloc
  152. * @dev: Device to allocate memory for
  153. * @size: Allocation size
  154. * @gfp: Allocation gfp flags
  155. *
  156. * Managed kmalloc. Memory allocated with this function is
  157. * automatically freed on driver detach. Like all other devres
  158. * resources, guaranteed alignment is unsigned long long.
  159. *
  160. * @return pointer to allocated memory on success, NULL on failure.
  161. */
  162. void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
  163. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  164. {
  165. return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
  166. }
  167. static inline void *devm_kmalloc_array(struct udevice *dev,
  168. size_t n, size_t size, gfp_t flags)
  169. {
  170. if (size != 0 && n > SIZE_MAX / size)
  171. return NULL;
  172. return devm_kmalloc(dev, n * size, flags);
  173. }
  174. static inline void *devm_kcalloc(struct udevice *dev,
  175. size_t n, size_t size, gfp_t flags)
  176. {
  177. return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
  178. }
  179. /**
  180. * devm_kfree() - Resource-managed kfree
  181. * @dev: Device this memory belongs to
  182. * @ptr: Memory to free
  183. *
  184. * Free memory allocated with devm_kmalloc().
  185. */
  186. void devm_kfree(struct udevice *dev, void *ptr);
  187. /* Get basic stats on allocations */
  188. void devres_get_stats(const struct udevice *dev, struct devres_stats *stats);
  189. #else /* ! CONFIG_DEVRES */
  190. static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
  191. {
  192. return kzalloc(size, gfp);
  193. }
  194. static inline void devres_free(void *res)
  195. {
  196. kfree(res);
  197. }
  198. static inline void devres_add(struct udevice *dev, void *res)
  199. {
  200. }
  201. static inline void *devres_find(struct udevice *dev, dr_release_t release,
  202. dr_match_t match, void *match_data)
  203. {
  204. return NULL;
  205. }
  206. static inline void *devres_get(struct udevice *dev, void *new_res,
  207. dr_match_t match, void *match_data)
  208. {
  209. return NULL;
  210. }
  211. static inline void *devres_remove(struct udevice *dev, dr_release_t release,
  212. dr_match_t match, void *match_data)
  213. {
  214. return NULL;
  215. }
  216. static inline int devres_destroy(struct udevice *dev, dr_release_t release,
  217. dr_match_t match, void *match_data)
  218. {
  219. return 0;
  220. }
  221. static inline int devres_release(struct udevice *dev, dr_release_t release,
  222. dr_match_t match, void *match_data)
  223. {
  224. return 0;
  225. }
  226. static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
  227. {
  228. return kmalloc(size, gfp);
  229. }
  230. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  231. {
  232. return kzalloc(size, gfp);
  233. }
  234. static inline void *devm_kmalloc_array(struct udevice *dev,
  235. size_t n, size_t size, gfp_t flags)
  236. {
  237. /* TODO: add kmalloc_array() to linux/compat.h */
  238. if (size != 0 && n > SIZE_MAX / size)
  239. return NULL;
  240. return kmalloc(n * size, flags);
  241. }
  242. static inline void *devm_kcalloc(struct udevice *dev,
  243. size_t n, size_t size, gfp_t flags)
  244. {
  245. /* TODO: add kcalloc() to linux/compat.h */
  246. return kmalloc(n * size, flags | __GFP_ZERO);
  247. }
  248. static inline void devm_kfree(struct udevice *dev, void *ptr)
  249. {
  250. kfree(ptr);
  251. }
  252. static inline void devres_get_stats(const struct udevice *dev,
  253. struct devres_stats *stats)
  254. {
  255. }
  256. #endif /* DEVRES */
  257. #endif /* _DM_DEVRES_H */