drm_managed.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. struct mutex;
  9. typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
  10. /**
  11. * drmm_add_action - add a managed release action to a &drm_device
  12. * @dev: DRM device
  13. * @action: function which should be called when @dev is released
  14. * @data: opaque pointer, passed to @action
  15. *
  16. * This function adds the @release action with optional parameter @data to the
  17. * list of cleanup actions for @dev. The cleanup actions will be run in reverse
  18. * order in the final drm_dev_put() call for @dev.
  19. */
  20. #define drmm_add_action(dev, action, data) \
  21. __drmm_add_action(dev, action, data, #action)
  22. int __must_check __drmm_add_action(struct drm_device *dev,
  23. drmres_release_t action,
  24. void *data, const char *name);
  25. /**
  26. * drmm_add_action_or_reset - add a managed release action to a &drm_device
  27. * @dev: DRM device
  28. * @action: function which should be called when @dev is released
  29. * @data: opaque pointer, passed to @action
  30. *
  31. * Similar to drmm_add_action(), with the only difference that upon failure
  32. * @action is directly called for any cleanup work necessary on failures.
  33. */
  34. #define drmm_add_action_or_reset(dev, action, data) \
  35. __drmm_add_action_or_reset(dev, action, data, #action)
  36. int __must_check __drmm_add_action_or_reset(struct drm_device *dev,
  37. drmres_release_t action,
  38. void *data, const char *name);
  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. void __drmm_mutex_release(struct drm_device *dev, void *res);
  92. /**
  93. * drmm_mutex_init - &drm_device-managed mutex_init()
  94. * @dev: DRM device
  95. * @lock: lock to be initialized
  96. *
  97. * Returns:
  98. * 0 on success, or a negative errno code otherwise.
  99. *
  100. * This is a &drm_device-managed version of mutex_init(). The initialized
  101. * lock is automatically destroyed on the final drm_dev_put().
  102. */
  103. #define drmm_mutex_init(dev, lock) ({ \
  104. mutex_init(lock); \
  105. drmm_add_action_or_reset(dev, __drmm_mutex_release, lock); \
  106. }) \
  107. #endif