1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
6 #include <linux/ww_mutex.h>
8 #define DRM_EXEC_INTERRUPTIBLE_WAIT BIT(0)
9 #define DRM_EXEC_IGNORE_DUPLICATES BIT(1)
11 struct drm_gem_object;
14 * struct drm_exec - Execution context
18 * @flags: Flags to control locking behavior
23 * @ticket: WW ticket used for acquiring locks
25 struct ww_acquire_ctx ticket;
28 * @num_objects: number of objects locked
30 unsigned int num_objects;
33 * @max_objects: maximum objects in array
35 unsigned int max_objects;
38 * @objects: array of the locked objects
40 struct drm_gem_object **objects;
43 * @contended: contended GEM object we backed off for
45 struct drm_gem_object *contended;
48 * @prelocked: already locked GEM object due to contention
50 struct drm_gem_object *prelocked;
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
59 * Iterate over all the locked GEM objects inside the drm_exec object.
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])
67 * drm_exec_until_all_locked - loop until all GEM objects are locked
68 * @exec: drm_exec object
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.
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.
77 #define drm_exec_until_all_locked(exec) \
78 for (void *__drm_exec_retry_ptr; ({ \
79 __label__ __drm_exec_retry; \
81 __drm_exec_retry_ptr = &&__drm_exec_retry; \
82 (void)__drm_exec_retry_ptr; \
83 drm_exec_cleanup(exec); \
87 * drm_exec_retry_on_contention - restart the loop to grap all locks
88 * @exec: drm_exec object
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.
93 #define drm_exec_retry_on_contention(exec) \
95 if (unlikely(drm_exec_is_contended(exec))) \
96 goto *__drm_exec_retry_ptr; \
100 * drm_exec_is_contended - check for contention
101 * @exec: drm_exec object
103 * Returns true if the drm_exec object has run into some contention while
104 * locking a GEM object and needs to clean up.
106 static inline bool drm_exec_is_contended(struct drm_exec *exec)
108 return !!exec->contended;
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);