1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 #ifndef _DRM_MANAGED_H_ 3 #ifndef _DRM_MANAGED_H_ 4 #define _DRM_MANAGED_H_ 4 #define _DRM_MANAGED_H_ 5 5 6 #include <linux/gfp.h> 6 #include <linux/gfp.h> 7 #include <linux/overflow.h> 7 #include <linux/overflow.h> 8 #include <linux/types.h> 8 #include <linux/types.h> 9 9 10 struct drm_device; 10 struct drm_device; 11 struct mutex; 11 struct mutex; 12 12 13 typedef void (*drmres_release_t)(struct drm_de 13 typedef void (*drmres_release_t)(struct drm_device *dev, void *res); 14 14 15 /** 15 /** 16 * drmm_add_action - add a managed release act 16 * drmm_add_action - add a managed release action to a &drm_device 17 * @dev: DRM device 17 * @dev: DRM device 18 * @action: function which should be called wh 18 * @action: function which should be called when @dev is released 19 * @data: opaque pointer, passed to @action 19 * @data: opaque pointer, passed to @action 20 * 20 * 21 * This function adds the @release action with 21 * This function adds the @release action with optional parameter @data to the 22 * list of cleanup actions for @dev. The clean 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 @ 23 * order in the final drm_dev_put() call for @dev. 24 */ 24 */ 25 #define drmm_add_action(dev, action, data) \ 25 #define drmm_add_action(dev, action, data) \ 26 __drmm_add_action(dev, action, data, # 26 __drmm_add_action(dev, action, data, #action) 27 27 28 int __must_check __drmm_add_action(struct drm_ 28 int __must_check __drmm_add_action(struct drm_device *dev, 29 drmres_rele 29 drmres_release_t action, 30 void *data, 30 void *data, const char *name); 31 31 32 /** 32 /** 33 * drmm_add_action_or_reset - add a managed re 33 * drmm_add_action_or_reset - add a managed release action to a &drm_device 34 * @dev: DRM device 34 * @dev: DRM device 35 * @action: function which should be called wh 35 * @action: function which should be called when @dev is released 36 * @data: opaque pointer, passed to @action 36 * @data: opaque pointer, passed to @action 37 * 37 * 38 * Similar to drmm_add_action(), with the only 38 * Similar to drmm_add_action(), with the only difference that upon failure 39 * @action is directly called for any cleanup 39 * @action is directly called for any cleanup work necessary on failures. 40 */ 40 */ 41 #define drmm_add_action_or_reset(dev, action, 41 #define drmm_add_action_or_reset(dev, action, data) \ 42 __drmm_add_action_or_reset(dev, action 42 __drmm_add_action_or_reset(dev, action, data, #action) 43 43 44 int __must_check __drmm_add_action_or_reset(st 44 int __must_check __drmm_add_action_or_reset(struct drm_device *dev, 45 dr 45 drmres_release_t action, 46 vo 46 void *data, const char *name); 47 47 48 void drmm_release_action(struct drm_device *de << 49 drmres_release_t acti << 50 void *data); << 51 << 52 void *drmm_kmalloc(struct drm_device *dev, siz 48 void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp) __malloc; 53 49 54 /** 50 /** 55 * drmm_kzalloc - &drm_device managed kzalloc( 51 * drmm_kzalloc - &drm_device managed kzalloc() 56 * @dev: DRM device 52 * @dev: DRM device 57 * @size: size of the memory allocation 53 * @size: size of the memory allocation 58 * @gfp: GFP allocation flags 54 * @gfp: GFP allocation flags 59 * 55 * 60 * This is a &drm_device managed version of kz 56 * This is a &drm_device managed version of kzalloc(). The allocated memory is 61 * automatically freed on the final drm_dev_pu 57 * automatically freed on the final drm_dev_put(). Memory can also be freed 62 * before the final drm_dev_put() by calling d 58 * before the final drm_dev_put() by calling drmm_kfree(). 63 */ 59 */ 64 static inline void *drmm_kzalloc(struct drm_de 60 static inline void *drmm_kzalloc(struct drm_device *dev, size_t size, gfp_t gfp) 65 { 61 { 66 return drmm_kmalloc(dev, size, gfp | _ 62 return drmm_kmalloc(dev, size, gfp | __GFP_ZERO); 67 } 63 } 68 64 69 /** 65 /** 70 * drmm_kmalloc_array - &drm_device managed km 66 * drmm_kmalloc_array - &drm_device managed kmalloc_array() 71 * @dev: DRM device 67 * @dev: DRM device 72 * @n: number of array elements to allocate 68 * @n: number of array elements to allocate 73 * @size: size of array member 69 * @size: size of array member 74 * @flags: GFP allocation flags 70 * @flags: GFP allocation flags 75 * 71 * 76 * This is a &drm_device managed version of km 72 * This is a &drm_device managed version of kmalloc_array(). The allocated 77 * memory is automatically freed on the final 73 * memory is automatically freed on the final drm_dev_put() and works exactly 78 * like a memory allocation obtained by drmm_k 74 * like a memory allocation obtained by drmm_kmalloc(). 79 */ 75 */ 80 static inline void *drmm_kmalloc_array(struct 76 static inline void *drmm_kmalloc_array(struct drm_device *dev, 81 size_t 77 size_t n, size_t size, gfp_t flags) 82 { 78 { 83 size_t bytes; 79 size_t bytes; 84 80 85 if (unlikely(check_mul_overflow(n, siz 81 if (unlikely(check_mul_overflow(n, size, &bytes))) 86 return NULL; 82 return NULL; 87 83 88 return drmm_kmalloc(dev, bytes, flags) 84 return drmm_kmalloc(dev, bytes, flags); 89 } 85 } 90 86 91 /** 87 /** 92 * drmm_kcalloc - &drm_device managed kcalloc( 88 * drmm_kcalloc - &drm_device managed kcalloc() 93 * @dev: DRM device 89 * @dev: DRM device 94 * @n: number of array elements to allocate 90 * @n: number of array elements to allocate 95 * @size: size of array member 91 * @size: size of array member 96 * @flags: GFP allocation flags 92 * @flags: GFP allocation flags 97 * 93 * 98 * This is a &drm_device managed version of kc 94 * This is a &drm_device managed version of kcalloc(). The allocated memory is 99 * automatically freed on the final drm_dev_pu 95 * automatically freed on the final drm_dev_put() and works exactly like a 100 * memory allocation obtained by drmm_kmalloc( 96 * memory allocation obtained by drmm_kmalloc(). 101 */ 97 */ 102 static inline void *drmm_kcalloc(struct drm_de 98 static inline void *drmm_kcalloc(struct drm_device *dev, 103 size_t n, siz 99 size_t n, size_t size, gfp_t flags) 104 { 100 { 105 return drmm_kmalloc_array(dev, n, size 101 return drmm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); 106 } 102 } 107 103 108 char *drmm_kstrdup(struct drm_device *dev, con 104 char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp); 109 105 110 void drmm_kfree(struct drm_device *dev, void * 106 void drmm_kfree(struct drm_device *dev, void *data); 111 107 112 void __drmm_mutex_release(struct drm_device *d !! 108 int drmm_mutex_init(struct drm_device *dev, struct mutex *lock); 113 << 114 /** << 115 * drmm_mutex_init - &drm_device-managed mutex << 116 * @dev: DRM device << 117 * @lock: lock to be initialized << 118 * << 119 * Returns: << 120 * 0 on success, or a negative errno code othe << 121 * << 122 * This is a &drm_device-managed version of mu << 123 * lock is automatically destroyed on the fina << 124 */ << 125 #define drmm_mutex_init(dev, lock) ({ << 126 mutex_init(lock); << 127 drmm_add_action_or_reset(dev, __drmm_m << 128 }) << 129 109 130 #endif 110 #endif 131 111
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.