1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 
3 #include <drm/drm_exec.h>
4 #include <drm/drm_gem.h>
5 #include <linux/dma-resv.h>
6 
7 /**
8  * DOC: Overview
9  *
10  * This component mainly abstracts the retry loop necessary for locking
11  * multiple GEM objects while preparing hardware operations (e.g. command
12  * submissions, page table updates etc..).
13  *
14  * If a contention is detected while locking a GEM object the cleanup procedure
15  * unlocks all previously locked GEM objects and locks the contended one first
16  * before locking any further objects.
17  *
18  * After an object is locked fences slots can optionally be reserved on the
19  * dma_resv object inside the GEM object.
20  *
21  * A typical usage pattern should look like this::
22  *
23  *	struct drm_gem_object *obj;
24  *	struct drm_exec exec;
25  *	unsigned long index;
26  *	int ret;
27  *
28  *	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT);
29  *	drm_exec_until_all_locked(&exec) {
30  *		ret = drm_exec_prepare_obj(&exec, boA, 1);
31  *		drm_exec_retry_on_contention(&exec);
32  *		if (ret)
33  *			goto error;
34  *
35  *		ret = drm_exec_prepare_obj(&exec, boB, 1);
36  *		drm_exec_retry_on_contention(&exec);
37  *		if (ret)
38  *			goto error;
39  *	}
40  *
41  *	drm_exec_for_each_locked_object(&exec, index, obj) {
42  *		dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
43  *		...
44  *	}
45  *	drm_exec_fini(&exec);
46  *
47  * See struct dma_exec for more details.
48  */
49 
50 /* Dummy value used to initially enter the retry loop */
51 #define DRM_EXEC_DUMMY ((void *)~0)
52 
53 /* Unlock all objects and drop references */
drm_exec_unlock_all(struct drm_exec * exec)54 static void drm_exec_unlock_all(struct drm_exec *exec)
55 {
56 	struct drm_gem_object *obj;
57 	unsigned long index;
58 
59 	drm_exec_for_each_locked_object_reverse(exec, index, obj) {
60 		dma_resv_unlock(obj->resv);
61 		drm_gem_object_put(obj);
62 	}
63 
64 	drm_gem_object_put(exec->prelocked);
65 	exec->prelocked = NULL;
66 }
67 
68 /**
69  * drm_exec_init - initialize a drm_exec object
70  * @exec: the drm_exec object to initialize
71  * @flags: controls locking behavior, see DRM_EXEC_* defines
72  * @nr: the initial # of objects
73  *
74  * Initialize the object and make sure that we can track locked objects.
75  *
76  * If nr is non-zero then it is used as the initial objects table size.
77  * In either case, the table will grow (be re-allocated) on demand.
78  */
drm_exec_init(struct drm_exec * exec,u32 flags,unsigned nr)79 void drm_exec_init(struct drm_exec *exec, u32 flags, unsigned nr)
80 {
81 	if (!nr)
82 		nr = PAGE_SIZE / sizeof(void *);
83 
84 	exec->flags = flags;
85 	exec->objects = kvmalloc_array(nr, sizeof(void *), GFP_KERNEL);
86 
87 	/* If allocation here fails, just delay that till the first use */
88 	exec->max_objects = exec->objects ? nr : 0;
89 	exec->num_objects = 0;
90 	exec->contended = DRM_EXEC_DUMMY;
91 	exec->prelocked = NULL;
92 }
93 EXPORT_SYMBOL(drm_exec_init);
94 
95 /**
96  * drm_exec_fini - finalize a drm_exec object
97  * @exec: the drm_exec object to finalize
98  *
99  * Unlock all locked objects, drop the references to objects and free all memory
100  * used for tracking the state.
101  */
drm_exec_fini(struct drm_exec * exec)102 void drm_exec_fini(struct drm_exec *exec)
103 {
104 	drm_exec_unlock_all(exec);
105 	kvfree(exec->objects);
106 	if (exec->contended != DRM_EXEC_DUMMY) {
107 		drm_gem_object_put(exec->contended);
108 		ww_acquire_fini(&exec->ticket);
109 	}
110 }
111 EXPORT_SYMBOL(drm_exec_fini);
112 
113 /**
114  * drm_exec_cleanup - cleanup when contention is detected
115  * @exec: the drm_exec object to cleanup
116  *
117  * Cleanup the current state and return true if we should stay inside the retry
118  * loop, false if there wasn't any contention detected and we can keep the
119  * objects locked.
120  */
drm_exec_cleanup(struct drm_exec * exec)121 bool drm_exec_cleanup(struct drm_exec *exec)
122 {
123 	if (likely(!exec->contended)) {
124 		ww_acquire_done(&exec->ticket);
125 		return false;
126 	}
127 
128 	if (likely(exec->contended == DRM_EXEC_DUMMY)) {
129 		exec->contended = NULL;
130 		ww_acquire_init(&exec->ticket, &reservation_ww_class);
131 		return true;
132 	}
133 
134 	drm_exec_unlock_all(exec);
135 	exec->num_objects = 0;
136 	return true;
137 }
138 EXPORT_SYMBOL(drm_exec_cleanup);
139 
140 /* Track the locked object in the array */
drm_exec_obj_locked(struct drm_exec * exec,struct drm_gem_object * obj)141 static int drm_exec_obj_locked(struct drm_exec *exec,
142 			       struct drm_gem_object *obj)
143 {
144 	if (unlikely(exec->num_objects == exec->max_objects)) {
145 		size_t size = exec->max_objects * sizeof(void *);
146 		void *tmp;
147 
148 #ifdef __linux__
149 		tmp = kvrealloc(exec->objects, size + PAGE_SIZE, GFP_KERNEL);
150 		if (!tmp)
151 			return -ENOMEM;
152 #else
153 		tmp = kvmalloc(size + PAGE_SIZE, GFP_KERNEL);
154 		if (!tmp)
155 			return -ENOMEM;
156 		memcpy(tmp, exec->objects, size);
157 		kvfree(exec->objects);
158 #endif
159 
160 		exec->objects = tmp;
161 		exec->max_objects += PAGE_SIZE / sizeof(void *);
162 	}
163 	drm_gem_object_get(obj);
164 	exec->objects[exec->num_objects++] = obj;
165 
166 	return 0;
167 }
168 
169 /* Make sure the contended object is locked first */
drm_exec_lock_contended(struct drm_exec * exec)170 static int drm_exec_lock_contended(struct drm_exec *exec)
171 {
172 	struct drm_gem_object *obj = exec->contended;
173 	int ret;
174 
175 	if (likely(!obj))
176 		return 0;
177 
178 	/* Always cleanup the contention so that error handling can kick in */
179 	exec->contended = NULL;
180 	if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
181 		ret = dma_resv_lock_slow_interruptible(obj->resv,
182 						       &exec->ticket);
183 		if (unlikely(ret))
184 			goto error_dropref;
185 	} else {
186 		dma_resv_lock_slow(obj->resv, &exec->ticket);
187 	}
188 
189 	ret = drm_exec_obj_locked(exec, obj);
190 	if (unlikely(ret))
191 		goto error_unlock;
192 
193 	exec->prelocked = obj;
194 	return 0;
195 
196 error_unlock:
197 	dma_resv_unlock(obj->resv);
198 
199 error_dropref:
200 	drm_gem_object_put(obj);
201 	return ret;
202 }
203 
204 /**
205  * drm_exec_lock_obj - lock a GEM object for use
206  * @exec: the drm_exec object with the state
207  * @obj: the GEM object to lock
208  *
209  * Lock a GEM object for use and grab a reference to it.
210  *
211  * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
212  * already locked (can be suppressed by setting the DRM_EXEC_IGNORE_DUPLICATES
213  * flag), -ENOMEM when memory allocation failed and zero for success.
214  */
drm_exec_lock_obj(struct drm_exec * exec,struct drm_gem_object * obj)215 int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj)
216 {
217 	int ret;
218 
219 	ret = drm_exec_lock_contended(exec);
220 	if (unlikely(ret))
221 		return ret;
222 
223 	if (exec->prelocked == obj) {
224 		drm_gem_object_put(exec->prelocked);
225 		exec->prelocked = NULL;
226 		return 0;
227 	}
228 
229 	if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT)
230 		ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
231 	else
232 		ret = dma_resv_lock(obj->resv, &exec->ticket);
233 
234 	if (unlikely(ret == -EDEADLK)) {
235 		drm_gem_object_get(obj);
236 		exec->contended = obj;
237 		return -EDEADLK;
238 	}
239 
240 	if (unlikely(ret == -EALREADY) &&
241 	    exec->flags & DRM_EXEC_IGNORE_DUPLICATES)
242 		return 0;
243 
244 	if (unlikely(ret))
245 		return ret;
246 
247 	ret = drm_exec_obj_locked(exec, obj);
248 	if (ret)
249 		goto error_unlock;
250 
251 	return 0;
252 
253 error_unlock:
254 	dma_resv_unlock(obj->resv);
255 	return ret;
256 }
257 EXPORT_SYMBOL(drm_exec_lock_obj);
258 
259 /**
260  * drm_exec_unlock_obj - unlock a GEM object in this exec context
261  * @exec: the drm_exec object with the state
262  * @obj: the GEM object to unlock
263  *
264  * Unlock the GEM object and remove it from the collection of locked objects.
265  * Should only be used to unlock the most recently locked objects. It's not time
266  * efficient to unlock objects locked long ago.
267  */
drm_exec_unlock_obj(struct drm_exec * exec,struct drm_gem_object * obj)268 void drm_exec_unlock_obj(struct drm_exec *exec, struct drm_gem_object *obj)
269 {
270 	unsigned int i;
271 
272 	for (i = exec->num_objects; i--;) {
273 		if (exec->objects[i] == obj) {
274 			dma_resv_unlock(obj->resv);
275 			for (++i; i < exec->num_objects; ++i)
276 				exec->objects[i - 1] = exec->objects[i];
277 			--exec->num_objects;
278 			drm_gem_object_put(obj);
279 			return;
280 		}
281 
282 	}
283 }
284 EXPORT_SYMBOL(drm_exec_unlock_obj);
285 
286 /**
287  * drm_exec_prepare_obj - prepare a GEM object for use
288  * @exec: the drm_exec object with the state
289  * @obj: the GEM object to prepare
290  * @num_fences: how many fences to reserve
291  *
292  * Prepare a GEM object for use by locking it and reserving fence slots.
293  *
294  * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
295  * already locked, -ENOMEM when memory allocation failed and zero for success.
296  */
drm_exec_prepare_obj(struct drm_exec * exec,struct drm_gem_object * obj,unsigned int num_fences)297 int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
298 			 unsigned int num_fences)
299 {
300 	int ret;
301 
302 	ret = drm_exec_lock_obj(exec, obj);
303 	if (ret)
304 		return ret;
305 
306 	ret = dma_resv_reserve_fences(obj->resv, num_fences);
307 	if (ret) {
308 		drm_exec_unlock_obj(exec, obj);
309 		return ret;
310 	}
311 
312 	return 0;
313 }
314 EXPORT_SYMBOL(drm_exec_prepare_obj);
315 
316 /**
317  * drm_exec_prepare_array - helper to prepare an array of objects
318  * @exec: the drm_exec object with the state
319  * @objects: array of GEM object to prepare
320  * @num_objects: number of GEM objects in the array
321  * @num_fences: number of fences to reserve on each GEM object
322  *
323  * Prepares all GEM objects in an array, aborts on first error.
324  * Reserves @num_fences on each GEM object after locking it.
325  *
326  * Returns: -EDEADLOCK on contention, -EALREADY when object is already locked,
327  * -ENOMEM when memory allocation failed and zero for success.
328  */
drm_exec_prepare_array(struct drm_exec * exec,struct drm_gem_object ** objects,unsigned int num_objects,unsigned int num_fences)329 int drm_exec_prepare_array(struct drm_exec *exec,
330 			   struct drm_gem_object **objects,
331 			   unsigned int num_objects,
332 			   unsigned int num_fences)
333 {
334 	int ret;
335 
336 	for (unsigned int i = 0; i < num_objects; ++i) {
337 		ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
338 		if (unlikely(ret))
339 			return ret;
340 	}
341 
342 	return 0;
343 }
344 EXPORT_SYMBOL(drm_exec_prepare_array);
345 
346 MODULE_DESCRIPTION("DRM execution context");
347 MODULE_LICENSE("Dual MIT/GPL");
348