1 // SPDX-License-Identifier: GPL-2.0
3 #ifndef _DRM_MANAGED_H_
4 #define _DRM_MANAGED_H_
7 #include <linux/overflow.h>
8 #include <linux/types.h>
13 typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
16 * drmm_add_action - add a managed release action to a &drm_device
18 * @action: function which should be called when @dev is released
19 * @data: opaque pointer, passed to @action
21 * This function adds the @release action with optional parameter @data to the
22 * list of cleanup actions for @dev. The cleanup actions will be run in reverse
23 * order in the final drm_dev_put() call for @dev.
25 #define drmm_add_action(dev, action, data) \
26 __drmm_add_action(dev, action, data, #action)
28 int __must_check __drmm_add_action(struct drm_device *dev,
29 drmres_release_t action,
30 void *data, const char *name);
33 * drmm_add_action_or_reset - add a managed release action to a &drm_device
35 * @action: function which should be called when @dev is released
36 * @data: opaque pointer, passed to @action
38 * Similar to drmm_add_action(), with the only difference that upon failure
39 * @action is directly called for any cleanup work necessary on failures.
41 #define drmm_add_action_or_reset(dev, action, data) \
42 __drmm_add_action_or_reset(dev, action, data, #action)
44 int __must_check __drmm_add_action_or_reset(struct drm_device *dev,
45 drmres_release_t action,
46 void *data, const char *name);
48 void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp) __malloc;
51 * drmm_kzalloc - &drm_device managed kzalloc()
53 * @size: size of the memory allocation
54 * @gfp: GFP allocation flags
56 * This is a &drm_device managed version of kzalloc(). The allocated memory is
57 * automatically freed on the final drm_dev_put(). Memory can also be freed
58 * before the final drm_dev_put() by calling drmm_kfree().
60 static inline void *drmm_kzalloc(struct drm_device *dev, size_t size, gfp_t gfp)
62 return drmm_kmalloc(dev, size, gfp | __GFP_ZERO);
66 * drmm_kmalloc_array - &drm_device managed kmalloc_array()
68 * @n: number of array elements to allocate
69 * @size: size of array member
70 * @flags: GFP allocation flags
72 * This is a &drm_device managed version of kmalloc_array(). The allocated
73 * memory is automatically freed on the final drm_dev_put() and works exactly
74 * like a memory allocation obtained by drmm_kmalloc().
76 static inline void *drmm_kmalloc_array(struct drm_device *dev,
77 size_t n, size_t size, gfp_t flags)
81 if (unlikely(check_mul_overflow(n, size, &bytes)))
84 return drmm_kmalloc(dev, bytes, flags);
88 * drmm_kcalloc - &drm_device managed kcalloc()
90 * @n: number of array elements to allocate
91 * @size: size of array member
92 * @flags: GFP allocation flags
94 * This is a &drm_device managed version of kcalloc(). The allocated memory is
95 * automatically freed on the final drm_dev_put() and works exactly like a
96 * memory allocation obtained by drmm_kmalloc().
98 static inline void *drmm_kcalloc(struct drm_device *dev,
99 size_t n, size_t size, gfp_t flags)
101 return drmm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
104 char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
106 void drmm_kfree(struct drm_device *dev, void *data);
108 void __drmm_mutex_release(struct drm_device *dev, void *res);
111 * drmm_mutex_init - &drm_device-managed mutex_init()
113 * @lock: lock to be initialized
116 * 0 on success, or a negative errno code otherwise.
118 * This is a &drm_device-managed version of mutex_init(). The initialized
119 * lock is automatically destroyed on the final drm_dev_put().
121 #define drmm_mutex_init(dev, lock) ({ \
123 drmm_add_action_or_reset(dev, __drmm_mutex_release, lock); \