xref: /linux/include/drm/drm_exec.h (revision 460ea8980511c01c1551012b9a6ec6a06d02da59)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 
3 #ifndef __DRM_EXEC_H__
4 #define __DRM_EXEC_H__
5 
6 #include <linux/ww_mutex.h>
7 
8 #define DRM_EXEC_INTERRUPTIBLE_WAIT	BIT(0)
9 #define DRM_EXEC_IGNORE_DUPLICATES	BIT(1)
10 
11 struct drm_gem_object;
12 
13 /**
14  * struct drm_exec - Execution context
15  */
16 struct drm_exec {
17 	/**
18 	 * @flags: Flags to control locking behavior
19 	 */
20 	uint32_t		flags;
21 
22 	/**
23 	 * @ticket: WW ticket used for acquiring locks
24 	 */
25 	struct ww_acquire_ctx	ticket;
26 
27 	/**
28 	 * @num_objects: number of objects locked
29 	 */
30 	unsigned int		num_objects;
31 
32 	/**
33 	 * @max_objects: maximum objects in array
34 	 */
35 	unsigned int		max_objects;
36 
37 	/**
38 	 * @objects: array of the locked objects
39 	 */
40 	struct drm_gem_object	**objects;
41 
42 	/**
43 	 * @contended: contended GEM object we backed off for
44 	 */
45 	struct drm_gem_object	*contended;
46 
47 	/**
48 	 * @prelocked: already locked GEM object due to contention
49 	 */
50 	struct drm_gem_object *prelocked;
51 };
52 
53 /**
54  * drm_exec_for_each_locked_object - iterate over all the locked objects
55  * @exec: drm_exec object
56  * @index: unsigned long index for the iteration
57  * @obj: the current GEM object
58  *
59  * Iterate over all the locked GEM objects inside the drm_exec object.
60  */
61 #define drm_exec_for_each_locked_object(exec, index, obj)	\
62 	for (index = 0, obj = (exec)->objects[0];		\
63 	     index < (exec)->num_objects;			\
64 	     ++index, obj = (exec)->objects[index])
65 
66 /**
67  * drm_exec_until_all_locked - loop until all GEM objects are locked
68  * @exec: drm_exec object
69  *
70  * Core functionality of the drm_exec object. Loops until all GEM objects are
71  * locked and no more contention exists. At the beginning of the loop it is
72  * guaranteed that no GEM object is locked.
73  *
74  * Since labels can't be defined local to the loops body we use a jump pointer
75  * to make sure that the retry is only used from within the loops body.
76  */
77 #define drm_exec_until_all_locked(exec)				\
78 	for (void *__drm_exec_retry_ptr; ({			\
79 		__label__ __drm_exec_retry;			\
80 __drm_exec_retry:						\
81 		__drm_exec_retry_ptr = &&__drm_exec_retry;	\
82 		(void)__drm_exec_retry_ptr;			\
83 		drm_exec_cleanup(exec);				\
84 	});)
85 
86 /**
87  * drm_exec_retry_on_contention - restart the loop to grap all locks
88  * @exec: drm_exec object
89  *
90  * Control flow helper to continue when a contention was detected and we need to
91  * clean up and re-start the loop to prepare all GEM objects.
92  */
93 #define drm_exec_retry_on_contention(exec)			\
94 	do {							\
95 		if (unlikely(drm_exec_is_contended(exec)))	\
96 			goto *__drm_exec_retry_ptr;		\
97 	} while (0)
98 
99 /**
100  * drm_exec_is_contended - check for contention
101  * @exec: drm_exec object
102  *
103  * Returns true if the drm_exec object has run into some contention while
104  * locking a GEM object and needs to clean up.
105  */
106 static inline bool drm_exec_is_contended(struct drm_exec *exec)
107 {
108 	return !!exec->contended;
109 }
110 
111 void drm_exec_init(struct drm_exec *exec, uint32_t flags);
112 void drm_exec_fini(struct drm_exec *exec);
113 bool drm_exec_cleanup(struct drm_exec *exec);
114 int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj);
115 void drm_exec_unlock_obj(struct drm_exec *exec, struct drm_gem_object *obj);
116 int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
117 			 unsigned int num_fences);
118 int drm_exec_prepare_array(struct drm_exec *exec,
119 			   struct drm_gem_object **objects,
120 			   unsigned int num_objects,
121 			   unsigned int num_fences);
122 
123 #endif
124