drm_managed.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef _DRM_MANAGED_H_
  3. #define _DRM_MANAGED_H_
  4. #include <linux/gfp.h>
  5. #include <linux/overflow.h>
  6. #include <linux/types.h>
  7. struct drm_device;
  8. typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
  9. /**
  10. * drmm_add_action - add a managed release action to a &drm_device
  11. * @dev: DRM device
  12. * @action: function which should be called when @dev is released
  13. * @data: opaque pointer, passed to @action
  14. *
  15. * This function adds the @release action with optional parameter @data to the
  16. * list of cleanup actions for @dev. The cleanup actions will be run in reverse
  17. * order in the final drm_dev_put() call for @dev.
  18. */
  19. #define drmm_add_action(dev, action, data) \
  20. __drmm_add_action(dev, action, data, #action)
  21. int __must_check __drmm_add_action(struct drm_device *dev,
  22. drmres_release_t action,
  23. void *data, const char *name);
  24. /**
  25. * drmm_add_action_or_reset - add a managed release action to a &drm_device
  26. * @dev: DRM device
  27. * @action: function which should be called when @dev is released
  28. * @data: opaque pointer, passed to @action
  29. *
  30. * Similar to drmm_add_action(), with the only difference that upon failure
  31. * @action is directly called for any cleanup work necessary on failures.
  32. */
  33. #define drmm_add_action_or_reset(dev, action, data) \
  34. __drmm_add_action_or_reset(dev, action, data, #action)
  35. int __must_check __drmm_add_action_or_reset(struct drm_device *dev,
  36. drmres_release_t action,
  37. void *data, const char *name);
  38. void drmm_add_final_kfree(struct drm_device *dev, void *container);
  39. void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp) __malloc;
  40. /**
  41. * drmm_kzalloc - &drm_device managed kzalloc()
  42. * @dev: DRM device
  43. * @size: size of the memory allocation
  44. * @gfp: GFP allocation flags
  45. *
  46. * This is a &drm_device managed version of kzalloc(). The allocated memory is
  47. * automatically freed on the final drm_dev_put(). Memory can also be freed
  48. * before the final drm_dev_put() by calling drmm_kfree().
  49. */
  50. static inline void *drmm_kzalloc(struct drm_device *dev, size_t size, gfp_t gfp)
  51. {
  52. return drmm_kmalloc(dev, size, gfp | __GFP_ZERO);
  53. }
  54. /**
  55. * drmm_kmalloc_array - &drm_device managed kmalloc_array()
  56. * @dev: DRM device
  57. * @n: number of array elements to allocate
  58. * @size: size of array member
  59. * @flags: GFP allocation flags
  60. *
  61. * This is a &drm_device managed version of kmalloc_array(). The allocated
  62. * memory is automatically freed on the final drm_dev_put() and works exactly
  63. * like a memory allocation obtained by drmm_kmalloc().
  64. */
  65. static inline void *drmm_kmalloc_array(struct drm_device *dev,
  66. size_t n, size_t size, gfp_t flags)
  67. {
  68. size_t bytes;
  69. if (unlikely(check_mul_overflow(n, size, &bytes)))
  70. return NULL;
  71. return drmm_kmalloc(dev, bytes, flags);
  72. }
  73. /**
  74. * drmm_kcalloc - &drm_device managed kcalloc()
  75. * @dev: DRM device
  76. * @n: number of array elements to allocate
  77. * @size: size of array member
  78. * @flags: GFP allocation flags
  79. *
  80. * This is a &drm_device managed version of kcalloc(). The allocated memory is
  81. * automatically freed on the final drm_dev_put() and works exactly like a
  82. * memory allocation obtained by drmm_kmalloc().
  83. */
  84. static inline void *drmm_kcalloc(struct drm_device *dev,
  85. size_t n, size_t size, gfp_t flags)
  86. {
  87. return drmm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
  88. }
  89. char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
  90. void drmm_kfree(struct drm_device *dev, void *data);
  91. #endif