xref: /linux/drivers/gpu/drm/xe/xe_sync.c (revision b50ecc5aca4d18f1f0c4942f5c797bc85edef144)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_sync.h"
7 
8 #include <linux/dma-fence-array.h>
9 #include <linux/kthread.h>
10 #include <linux/sched/mm.h>
11 #include <linux/uaccess.h>
12 
13 #include <drm/drm_print.h>
14 #include <drm/drm_syncobj.h>
15 #include <uapi/drm/xe_drm.h>
16 
17 #include "xe_device_types.h"
18 #include "xe_exec_queue.h"
19 #include "xe_macros.h"
20 #include "xe_sched_job_types.h"
21 
22 struct xe_user_fence {
23 	struct xe_device *xe;
24 	struct kref refcount;
25 	struct dma_fence_cb cb;
26 	struct work_struct worker;
27 	struct mm_struct *mm;
28 	u64 __user *addr;
29 	u64 value;
30 	int signalled;
31 };
32 
33 static void user_fence_destroy(struct kref *kref)
34 {
35 	struct xe_user_fence *ufence = container_of(kref, struct xe_user_fence,
36 						 refcount);
37 
38 	mmdrop(ufence->mm);
39 	kfree(ufence);
40 }
41 
42 static void user_fence_get(struct xe_user_fence *ufence)
43 {
44 	kref_get(&ufence->refcount);
45 }
46 
47 static void user_fence_put(struct xe_user_fence *ufence)
48 {
49 	kref_put(&ufence->refcount, user_fence_destroy);
50 }
51 
52 static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 addr,
53 					       u64 value)
54 {
55 	struct xe_user_fence *ufence;
56 	u64 __user *ptr = u64_to_user_ptr(addr);
57 	u64 __maybe_unused prefetch_val;
58 
59 	if (get_user(prefetch_val, ptr))
60 		return ERR_PTR(-EFAULT);
61 
62 	ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
63 	if (!ufence)
64 		return ERR_PTR(-ENOMEM);
65 
66 	ufence->xe = xe;
67 	kref_init(&ufence->refcount);
68 	ufence->addr = ptr;
69 	ufence->value = value;
70 	ufence->mm = current->mm;
71 	mmgrab(ufence->mm);
72 
73 	return ufence;
74 }
75 
76 static void user_fence_worker(struct work_struct *w)
77 {
78 	struct xe_user_fence *ufence = container_of(w, struct xe_user_fence, worker);
79 
80 	if (mmget_not_zero(ufence->mm)) {
81 		kthread_use_mm(ufence->mm);
82 		if (copy_to_user(ufence->addr, &ufence->value, sizeof(ufence->value)))
83 			XE_WARN_ON("Copy to user failed");
84 		kthread_unuse_mm(ufence->mm);
85 		mmput(ufence->mm);
86 	} else {
87 		drm_dbg(&ufence->xe->drm, "mmget_not_zero() failed, ufence wasn't signaled\n");
88 	}
89 
90 	wake_up_all(&ufence->xe->ufence_wq);
91 	WRITE_ONCE(ufence->signalled, 1);
92 	user_fence_put(ufence);
93 }
94 
95 static void kick_ufence(struct xe_user_fence *ufence, struct dma_fence *fence)
96 {
97 	INIT_WORK(&ufence->worker, user_fence_worker);
98 	queue_work(ufence->xe->ordered_wq, &ufence->worker);
99 	dma_fence_put(fence);
100 }
101 
102 static void user_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
103 {
104 	struct xe_user_fence *ufence = container_of(cb, struct xe_user_fence, cb);
105 
106 	kick_ufence(ufence, fence);
107 }
108 
109 int xe_sync_entry_parse(struct xe_device *xe, struct xe_file *xef,
110 			struct xe_sync_entry *sync,
111 			struct drm_xe_sync __user *sync_user,
112 			unsigned int flags)
113 {
114 	struct drm_xe_sync sync_in;
115 	int err;
116 	bool exec = flags & SYNC_PARSE_FLAG_EXEC;
117 	bool in_lr_mode = flags & SYNC_PARSE_FLAG_LR_MODE;
118 	bool disallow_user_fence = flags & SYNC_PARSE_FLAG_DISALLOW_USER_FENCE;
119 	bool signal;
120 
121 	if (copy_from_user(&sync_in, sync_user, sizeof(*sync_user)))
122 		return -EFAULT;
123 
124 	if (XE_IOCTL_DBG(xe, sync_in.flags & ~DRM_XE_SYNC_FLAG_SIGNAL) ||
125 	    XE_IOCTL_DBG(xe, sync_in.reserved[0] || sync_in.reserved[1]))
126 		return -EINVAL;
127 
128 	signal = sync_in.flags & DRM_XE_SYNC_FLAG_SIGNAL;
129 	switch (sync_in.type) {
130 	case DRM_XE_SYNC_TYPE_SYNCOBJ:
131 		if (XE_IOCTL_DBG(xe, in_lr_mode && signal))
132 			return -EOPNOTSUPP;
133 
134 		if (XE_IOCTL_DBG(xe, upper_32_bits(sync_in.addr)))
135 			return -EINVAL;
136 
137 		sync->syncobj = drm_syncobj_find(xef->drm, sync_in.handle);
138 		if (XE_IOCTL_DBG(xe, !sync->syncobj))
139 			return -ENOENT;
140 
141 		if (!signal) {
142 			sync->fence = drm_syncobj_fence_get(sync->syncobj);
143 			if (XE_IOCTL_DBG(xe, !sync->fence))
144 				return -EINVAL;
145 		}
146 		break;
147 
148 	case DRM_XE_SYNC_TYPE_TIMELINE_SYNCOBJ:
149 		if (XE_IOCTL_DBG(xe, in_lr_mode && signal))
150 			return -EOPNOTSUPP;
151 
152 		if (XE_IOCTL_DBG(xe, upper_32_bits(sync_in.addr)))
153 			return -EINVAL;
154 
155 		if (XE_IOCTL_DBG(xe, sync_in.timeline_value == 0))
156 			return -EINVAL;
157 
158 		sync->syncobj = drm_syncobj_find(xef->drm, sync_in.handle);
159 		if (XE_IOCTL_DBG(xe, !sync->syncobj))
160 			return -ENOENT;
161 
162 		if (signal) {
163 			sync->chain_fence = dma_fence_chain_alloc();
164 			if (!sync->chain_fence)
165 				return -ENOMEM;
166 		} else {
167 			sync->fence = drm_syncobj_fence_get(sync->syncobj);
168 			if (XE_IOCTL_DBG(xe, !sync->fence))
169 				return -EINVAL;
170 
171 			err = dma_fence_chain_find_seqno(&sync->fence,
172 							 sync_in.timeline_value);
173 			if (err)
174 				return err;
175 		}
176 		break;
177 
178 	case DRM_XE_SYNC_TYPE_USER_FENCE:
179 		if (XE_IOCTL_DBG(xe, disallow_user_fence))
180 			return -EOPNOTSUPP;
181 
182 		if (XE_IOCTL_DBG(xe, !signal))
183 			return -EOPNOTSUPP;
184 
185 		if (XE_IOCTL_DBG(xe, sync_in.addr & 0x7))
186 			return -EINVAL;
187 
188 		if (exec) {
189 			sync->addr = sync_in.addr;
190 		} else {
191 			sync->ufence = user_fence_create(xe, sync_in.addr,
192 							 sync_in.timeline_value);
193 			if (XE_IOCTL_DBG(xe, IS_ERR(sync->ufence)))
194 				return PTR_ERR(sync->ufence);
195 		}
196 
197 		break;
198 
199 	default:
200 		return -EINVAL;
201 	}
202 
203 	sync->type = sync_in.type;
204 	sync->flags = sync_in.flags;
205 	sync->timeline_value = sync_in.timeline_value;
206 
207 	return 0;
208 }
209 
210 int xe_sync_entry_add_deps(struct xe_sync_entry *sync, struct xe_sched_job *job)
211 {
212 	if (sync->fence)
213 		return  drm_sched_job_add_dependency(&job->drm,
214 						     dma_fence_get(sync->fence));
215 
216 	return 0;
217 }
218 
219 void xe_sync_entry_signal(struct xe_sync_entry *sync, struct dma_fence *fence)
220 {
221 	if (!(sync->flags & DRM_XE_SYNC_FLAG_SIGNAL))
222 		return;
223 
224 	if (sync->chain_fence) {
225 		drm_syncobj_add_point(sync->syncobj, sync->chain_fence,
226 				      fence, sync->timeline_value);
227 		/*
228 		 * The chain's ownership is transferred to the
229 		 * timeline.
230 		 */
231 		sync->chain_fence = NULL;
232 	} else if (sync->syncobj) {
233 		drm_syncobj_replace_fence(sync->syncobj, fence);
234 	} else if (sync->ufence) {
235 		int err;
236 
237 		dma_fence_get(fence);
238 		user_fence_get(sync->ufence);
239 		err = dma_fence_add_callback(fence, &sync->ufence->cb,
240 					     user_fence_cb);
241 		if (err == -ENOENT) {
242 			kick_ufence(sync->ufence, fence);
243 		} else if (err) {
244 			XE_WARN_ON("failed to add user fence");
245 			user_fence_put(sync->ufence);
246 			dma_fence_put(fence);
247 		}
248 	}
249 }
250 
251 void xe_sync_entry_cleanup(struct xe_sync_entry *sync)
252 {
253 	if (sync->syncobj)
254 		drm_syncobj_put(sync->syncobj);
255 	dma_fence_put(sync->fence);
256 	dma_fence_chain_free(sync->chain_fence);
257 	if (sync->ufence)
258 		user_fence_put(sync->ufence);
259 }
260 
261 /**
262  * xe_sync_in_fence_get() - Get a fence from syncs, exec queue, and VM
263  * @sync: input syncs
264  * @num_sync: number of syncs
265  * @q: exec queue
266  * @vm: VM
267  *
268  * Get a fence from syncs, exec queue, and VM. If syncs contain in-fences create
269  * and return a composite fence of all in-fences + last fence. If no in-fences
270  * return last fence on  input exec queue. Caller must drop reference to
271  * returned fence.
272  *
273  * Return: fence on success, ERR_PTR(-ENOMEM) on failure
274  */
275 struct dma_fence *
276 xe_sync_in_fence_get(struct xe_sync_entry *sync, int num_sync,
277 		     struct xe_exec_queue *q, struct xe_vm *vm)
278 {
279 	struct dma_fence **fences = NULL;
280 	struct dma_fence_array *cf = NULL;
281 	struct dma_fence *fence;
282 	int i, num_in_fence = 0, current_fence = 0;
283 
284 	lockdep_assert_held(&vm->lock);
285 
286 	/* Count in-fences */
287 	for (i = 0; i < num_sync; ++i) {
288 		if (sync[i].fence) {
289 			++num_in_fence;
290 			fence = sync[i].fence;
291 		}
292 	}
293 
294 	/* Easy case... */
295 	if (!num_in_fence) {
296 		fence = xe_exec_queue_last_fence_get(q, vm);
297 		return fence;
298 	}
299 
300 	/* Create composite fence */
301 	fences = kmalloc_array(num_in_fence + 1, sizeof(*fences), GFP_KERNEL);
302 	if (!fences)
303 		return ERR_PTR(-ENOMEM);
304 	for (i = 0; i < num_sync; ++i) {
305 		if (sync[i].fence) {
306 			dma_fence_get(sync[i].fence);
307 			fences[current_fence++] = sync[i].fence;
308 		}
309 	}
310 	fences[current_fence++] = xe_exec_queue_last_fence_get(q, vm);
311 	cf = dma_fence_array_create(num_in_fence, fences,
312 				    vm->composite_fence_ctx,
313 				    vm->composite_fence_seqno++,
314 				    false);
315 	if (!cf) {
316 		--vm->composite_fence_seqno;
317 		goto err_out;
318 	}
319 
320 	return &cf->base;
321 
322 err_out:
323 	while (current_fence)
324 		dma_fence_put(fences[--current_fence]);
325 	kfree(fences);
326 	kfree(cf);
327 
328 	return ERR_PTR(-ENOMEM);
329 }
330 
331 /**
332  * __xe_sync_ufence_get() - Get user fence from user fence
333  * @ufence: input user fence
334  *
335  * Get a user fence reference from user fence
336  *
337  * Return: xe_user_fence pointer with reference
338  */
339 struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence)
340 {
341 	user_fence_get(ufence);
342 
343 	return ufence;
344 }
345 
346 /**
347  * xe_sync_ufence_get() - Get user fence from sync
348  * @sync: input sync
349  *
350  * Get a user fence reference from sync.
351  *
352  * Return: xe_user_fence pointer with reference
353  */
354 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync)
355 {
356 	user_fence_get(sync->ufence);
357 
358 	return sync->ufence;
359 }
360 
361 /**
362  * xe_sync_ufence_put() - Put user fence reference
363  * @ufence: user fence reference
364  *
365  */
366 void xe_sync_ufence_put(struct xe_user_fence *ufence)
367 {
368 	user_fence_put(ufence);
369 }
370 
371 /**
372  * xe_sync_ufence_get_status() - Get user fence status
373  * @ufence: user fence
374  *
375  * Return: 1 if signalled, 0 not signalled, <0 on error
376  */
377 int xe_sync_ufence_get_status(struct xe_user_fence *ufence)
378 {
379 	return READ_ONCE(ufence->signalled);
380 }
381