1c6603c74SDaniel Vetter // SPDX-License-Identifier: GPL-2.0
2c6603c74SDaniel Vetter
3c6603c74SDaniel Vetter #ifndef _DRM_MANAGED_H_
4c6603c74SDaniel Vetter #define _DRM_MANAGED_H_
5c6603c74SDaniel Vetter
6c6603c74SDaniel Vetter #include <linux/gfp.h>
7c23d686fSDaniel Vetter #include <linux/overflow.h>
8c6603c74SDaniel Vetter #include <linux/types.h>
9c6603c74SDaniel Vetter
10c6603c74SDaniel Vetter struct drm_device;
11e13f13e0SThomas Zimmermann struct mutex;
12c6603c74SDaniel Vetter
13c6603c74SDaniel Vetter typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
14c6603c74SDaniel Vetter
159e1ed9fbSDaniel Vetter /**
169e1ed9fbSDaniel Vetter * drmm_add_action - add a managed release action to a &drm_device
179e1ed9fbSDaniel Vetter * @dev: DRM device
189e1ed9fbSDaniel Vetter * @action: function which should be called when @dev is released
199e1ed9fbSDaniel Vetter * @data: opaque pointer, passed to @action
209e1ed9fbSDaniel Vetter *
219e1ed9fbSDaniel Vetter * This function adds the @release action with optional parameter @data to the
229e1ed9fbSDaniel Vetter * list of cleanup actions for @dev. The cleanup actions will be run in reverse
239e1ed9fbSDaniel Vetter * order in the final drm_dev_put() call for @dev.
249e1ed9fbSDaniel Vetter */
25c6603c74SDaniel Vetter #define drmm_add_action(dev, action, data) \
26c6603c74SDaniel Vetter __drmm_add_action(dev, action, data, #action)
27c6603c74SDaniel Vetter
28c6603c74SDaniel Vetter int __must_check __drmm_add_action(struct drm_device *dev,
29c6603c74SDaniel Vetter drmres_release_t action,
30c6603c74SDaniel Vetter void *data, const char *name);
31c6603c74SDaniel Vetter
329e1ed9fbSDaniel Vetter /**
339e1ed9fbSDaniel Vetter * drmm_add_action_or_reset - add a managed release action to a &drm_device
349e1ed9fbSDaniel Vetter * @dev: DRM device
359e1ed9fbSDaniel Vetter * @action: function which should be called when @dev is released
369e1ed9fbSDaniel Vetter * @data: opaque pointer, passed to @action
379e1ed9fbSDaniel Vetter *
389e1ed9fbSDaniel Vetter * Similar to drmm_add_action(), with the only difference that upon failure
399e1ed9fbSDaniel Vetter * @action is directly called for any cleanup work necessary on failures.
409e1ed9fbSDaniel Vetter */
41f96306f9SDaniel Vetter #define drmm_add_action_or_reset(dev, action, data) \
42f96306f9SDaniel Vetter __drmm_add_action_or_reset(dev, action, data, #action)
43f96306f9SDaniel Vetter
44f96306f9SDaniel Vetter int __must_check __drmm_add_action_or_reset(struct drm_device *dev,
45f96306f9SDaniel Vetter drmres_release_t action,
46f96306f9SDaniel Vetter void *data, const char *name);
47f96306f9SDaniel Vetter
48*42d6196fSMichał Winiarski void drmm_release_action(struct drm_device *dev,
49*42d6196fSMichał Winiarski drmres_release_t action,
50*42d6196fSMichał Winiarski void *data);
51*42d6196fSMichał Winiarski
52c6603c74SDaniel Vetter void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp) __malloc;
539e1ed9fbSDaniel Vetter
549e1ed9fbSDaniel Vetter /**
559e1ed9fbSDaniel Vetter * drmm_kzalloc - &drm_device managed kzalloc()
569e1ed9fbSDaniel Vetter * @dev: DRM device
579e1ed9fbSDaniel Vetter * @size: size of the memory allocation
589e1ed9fbSDaniel Vetter * @gfp: GFP allocation flags
599e1ed9fbSDaniel Vetter *
609e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kzalloc(). The allocated memory is
619e1ed9fbSDaniel Vetter * automatically freed on the final drm_dev_put(). Memory can also be freed
629e1ed9fbSDaniel Vetter * before the final drm_dev_put() by calling drmm_kfree().
639e1ed9fbSDaniel Vetter */
drmm_kzalloc(struct drm_device * dev,size_t size,gfp_t gfp)64c6603c74SDaniel Vetter static inline void *drmm_kzalloc(struct drm_device *dev, size_t size, gfp_t gfp)
65c6603c74SDaniel Vetter {
66c6603c74SDaniel Vetter return drmm_kmalloc(dev, size, gfp | __GFP_ZERO);
67c6603c74SDaniel Vetter }
689e1ed9fbSDaniel Vetter
699e1ed9fbSDaniel Vetter /**
709e1ed9fbSDaniel Vetter * drmm_kmalloc_array - &drm_device managed kmalloc_array()
719e1ed9fbSDaniel Vetter * @dev: DRM device
729e1ed9fbSDaniel Vetter * @n: number of array elements to allocate
739e1ed9fbSDaniel Vetter * @size: size of array member
749e1ed9fbSDaniel Vetter * @flags: GFP allocation flags
759e1ed9fbSDaniel Vetter *
769e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kmalloc_array(). The allocated
779e1ed9fbSDaniel Vetter * memory is automatically freed on the final drm_dev_put() and works exactly
789e1ed9fbSDaniel Vetter * like a memory allocation obtained by drmm_kmalloc().
799e1ed9fbSDaniel Vetter */
drmm_kmalloc_array(struct drm_device * dev,size_t n,size_t size,gfp_t flags)80c23d686fSDaniel Vetter static inline void *drmm_kmalloc_array(struct drm_device *dev,
81c23d686fSDaniel Vetter size_t n, size_t size, gfp_t flags)
82c23d686fSDaniel Vetter {
83c23d686fSDaniel Vetter size_t bytes;
84c23d686fSDaniel Vetter
85c23d686fSDaniel Vetter if (unlikely(check_mul_overflow(n, size, &bytes)))
86c23d686fSDaniel Vetter return NULL;
87c23d686fSDaniel Vetter
88c23d686fSDaniel Vetter return drmm_kmalloc(dev, bytes, flags);
89c23d686fSDaniel Vetter }
909e1ed9fbSDaniel Vetter
919e1ed9fbSDaniel Vetter /**
929e1ed9fbSDaniel Vetter * drmm_kcalloc - &drm_device managed kcalloc()
939e1ed9fbSDaniel Vetter * @dev: DRM device
949e1ed9fbSDaniel Vetter * @n: number of array elements to allocate
959e1ed9fbSDaniel Vetter * @size: size of array member
969e1ed9fbSDaniel Vetter * @flags: GFP allocation flags
979e1ed9fbSDaniel Vetter *
989e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kcalloc(). The allocated memory is
999e1ed9fbSDaniel Vetter * automatically freed on the final drm_dev_put() and works exactly like a
1009e1ed9fbSDaniel Vetter * memory allocation obtained by drmm_kmalloc().
1019e1ed9fbSDaniel Vetter */
drmm_kcalloc(struct drm_device * dev,size_t n,size_t size,gfp_t flags)102c23d686fSDaniel Vetter static inline void *drmm_kcalloc(struct drm_device *dev,
103c23d686fSDaniel Vetter size_t n, size_t size, gfp_t flags)
104c23d686fSDaniel Vetter {
105c23d686fSDaniel Vetter return drmm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
106c23d686fSDaniel Vetter }
1079e1ed9fbSDaniel Vetter
108a5c71fdbSDaniel Vetter char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
109c6603c74SDaniel Vetter
110c6603c74SDaniel Vetter void drmm_kfree(struct drm_device *dev, void *data);
111c6603c74SDaniel Vetter
112c21f11d1SMatthew Auld void __drmm_mutex_release(struct drm_device *dev, void *res);
113c21f11d1SMatthew Auld
114c21f11d1SMatthew Auld /**
115c21f11d1SMatthew Auld * drmm_mutex_init - &drm_device-managed mutex_init()
116c21f11d1SMatthew Auld * @dev: DRM device
117c21f11d1SMatthew Auld * @lock: lock to be initialized
118c21f11d1SMatthew Auld *
119c21f11d1SMatthew Auld * Returns:
120c21f11d1SMatthew Auld * 0 on success, or a negative errno code otherwise.
121c21f11d1SMatthew Auld *
122c21f11d1SMatthew Auld * This is a &drm_device-managed version of mutex_init(). The initialized
123c21f11d1SMatthew Auld * lock is automatically destroyed on the final drm_dev_put().
124c21f11d1SMatthew Auld */
125c21f11d1SMatthew Auld #define drmm_mutex_init(dev, lock) ({ \
126c21f11d1SMatthew Auld mutex_init(lock); \
127c21f11d1SMatthew Auld drmm_add_action_or_reset(dev, __drmm_mutex_release, lock); \
128c21f11d1SMatthew Auld }) \
129e13f13e0SThomas Zimmermann
130c6603c74SDaniel Vetter #endif
131