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