xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c (revision fcab107abe1ab5be9dbe874baa722372da8f4f73)
1 /*
2  * Copyright 2008 Jerome Glisse.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Jerome Glisse <glisse@freedesktop.org>
26  */
27 
28 #include <linux/file.h>
29 #include <linux/pagemap.h>
30 #include <linux/sync_file.h>
31 #include <linux/dma-buf.h>
32 
33 #include <drm/amdgpu_drm.h>
34 #include <drm/drm_syncobj.h>
35 #include <drm/ttm/ttm_tt.h>
36 
37 #include "amdgpu_cs.h"
38 #include "amdgpu.h"
39 #include "amdgpu_trace.h"
40 #include "amdgpu_gmc.h"
41 #include "amdgpu_gem.h"
42 #include "amdgpu_ras.h"
43 
44 static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p,
45 				 struct amdgpu_device *adev,
46 				 struct drm_file *filp,
47 				 union drm_amdgpu_cs *cs)
48 {
49 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
50 
51 	if (cs->in.num_chunks == 0)
52 		return -EINVAL;
53 
54 	memset(p, 0, sizeof(*p));
55 	p->adev = adev;
56 	p->filp = filp;
57 
58 	p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
59 	if (!p->ctx)
60 		return -EINVAL;
61 
62 	if (atomic_read(&p->ctx->guilty)) {
63 		amdgpu_ctx_put(p->ctx);
64 		return -ECANCELED;
65 	}
66 
67 	amdgpu_sync_create(&p->sync);
68 	drm_exec_init(&p->exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
69 		      DRM_EXEC_IGNORE_DUPLICATES, 0);
70 	return 0;
71 }
72 
73 static int amdgpu_cs_job_idx(struct amdgpu_cs_parser *p,
74 			     struct drm_amdgpu_cs_chunk_ib *chunk_ib)
75 {
76 	struct drm_sched_entity *entity;
77 	unsigned int i;
78 	int r;
79 
80 	r = amdgpu_ctx_get_entity(p->ctx, chunk_ib->ip_type,
81 				  chunk_ib->ip_instance,
82 				  chunk_ib->ring, &entity);
83 	if (r)
84 		return r;
85 
86 	/*
87 	 * Abort if there is no run queue associated with this entity.
88 	 * Possibly because of disabled HW IP.
89 	 */
90 	if (entity->rq == NULL)
91 		return -EINVAL;
92 
93 	/* Check if we can add this IB to some existing job */
94 	for (i = 0; i < p->gang_size; ++i)
95 		if (p->entities[i] == entity)
96 			return i;
97 
98 	/* If not increase the gang size if possible */
99 	if (i == AMDGPU_CS_GANG_SIZE)
100 		return -EINVAL;
101 
102 	p->entities[i] = entity;
103 	p->gang_size = i + 1;
104 	return i;
105 }
106 
107 static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
108 			   struct drm_amdgpu_cs_chunk_ib *chunk_ib,
109 			   unsigned int *num_ibs)
110 {
111 	int r;
112 
113 	r = amdgpu_cs_job_idx(p, chunk_ib);
114 	if (r < 0)
115 		return r;
116 
117 	if (num_ibs[r] >= amdgpu_ring_max_ibs(chunk_ib->ip_type))
118 		return -EINVAL;
119 
120 	++(num_ibs[r]);
121 	p->gang_leader_idx = r;
122 	return 0;
123 }
124 
125 static int amdgpu_cs_p1_user_fence(struct amdgpu_cs_parser *p,
126 				   struct drm_amdgpu_cs_chunk_fence *data,
127 				   uint32_t *offset)
128 {
129 	struct drm_gem_object *gobj;
130 	unsigned long size;
131 
132 	gobj = drm_gem_object_lookup(p->filp, data->handle);
133 	if (gobj == NULL)
134 		return -EINVAL;
135 
136 	p->uf_bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
137 	drm_gem_object_put(gobj);
138 
139 	size = amdgpu_bo_size(p->uf_bo);
140 	if (size != PAGE_SIZE || data->offset > (size - 8))
141 		return -EINVAL;
142 
143 	if (amdgpu_ttm_tt_get_usermm(p->uf_bo->tbo.ttm))
144 		return -EINVAL;
145 
146 	*offset = data->offset;
147 	return 0;
148 }
149 
150 static int amdgpu_cs_p1_bo_handles(struct amdgpu_cs_parser *p,
151 				   struct drm_amdgpu_bo_list_in *data)
152 {
153 	struct drm_amdgpu_bo_list_entry *info;
154 	int r;
155 
156 	r = amdgpu_bo_create_list_entry_array(data, &info);
157 	if (r)
158 		return r;
159 
160 	r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
161 				  &p->bo_list);
162 	if (r)
163 		goto error_free;
164 
165 	kvfree(info);
166 	return 0;
167 
168 error_free:
169 	kvfree(info);
170 
171 	return r;
172 }
173 
174 /* Copy the data from userspace and go over it the first time */
175 static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
176 			   union drm_amdgpu_cs *cs)
177 {
178 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
179 	unsigned int num_ibs[AMDGPU_CS_GANG_SIZE] = { };
180 	struct amdgpu_vm *vm = &fpriv->vm;
181 	uint64_t *chunk_array_user;
182 	uint64_t *chunk_array;
183 	uint32_t uf_offset = 0;
184 	size_t size;
185 	int ret;
186 	int i;
187 
188 	chunk_array = kvmalloc_array(cs->in.num_chunks, sizeof(uint64_t),
189 				     GFP_KERNEL);
190 	if (!chunk_array)
191 		return -ENOMEM;
192 
193 	/* get chunks */
194 	chunk_array_user = u64_to_user_ptr(cs->in.chunks);
195 	if (copy_from_user(chunk_array, chunk_array_user,
196 			   sizeof(uint64_t)*cs->in.num_chunks)) {
197 		ret = -EFAULT;
198 		goto free_chunk;
199 	}
200 
201 	p->nchunks = cs->in.num_chunks;
202 	p->chunks = kvmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
203 			    GFP_KERNEL);
204 	if (!p->chunks) {
205 		ret = -ENOMEM;
206 		goto free_chunk;
207 	}
208 
209 	for (i = 0; i < p->nchunks; i++) {
210 		struct drm_amdgpu_cs_chunk __user *chunk_ptr = NULL;
211 		struct drm_amdgpu_cs_chunk user_chunk;
212 		uint32_t __user *cdata;
213 
214 		chunk_ptr = u64_to_user_ptr(chunk_array[i]);
215 		if (copy_from_user(&user_chunk, chunk_ptr,
216 				       sizeof(struct drm_amdgpu_cs_chunk))) {
217 			ret = -EFAULT;
218 			i--;
219 			goto free_partial_kdata;
220 		}
221 		p->chunks[i].chunk_id = user_chunk.chunk_id;
222 		p->chunks[i].length_dw = user_chunk.length_dw;
223 
224 		size = p->chunks[i].length_dw;
225 		cdata = u64_to_user_ptr(user_chunk.chunk_data);
226 
227 		p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t),
228 						    GFP_KERNEL);
229 		if (p->chunks[i].kdata == NULL) {
230 			ret = -ENOMEM;
231 			i--;
232 			goto free_partial_kdata;
233 		}
234 		size *= sizeof(uint32_t);
235 		if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
236 			ret = -EFAULT;
237 			goto free_partial_kdata;
238 		}
239 
240 		/* Assume the worst on the following checks */
241 		ret = -EINVAL;
242 		switch (p->chunks[i].chunk_id) {
243 		case AMDGPU_CHUNK_ID_IB:
244 			if (size < sizeof(struct drm_amdgpu_cs_chunk_ib))
245 				goto free_partial_kdata;
246 
247 			ret = amdgpu_cs_p1_ib(p, p->chunks[i].kdata, num_ibs);
248 			if (ret)
249 				goto free_partial_kdata;
250 			break;
251 
252 		case AMDGPU_CHUNK_ID_FENCE:
253 			if (size < sizeof(struct drm_amdgpu_cs_chunk_fence))
254 				goto free_partial_kdata;
255 
256 			ret = amdgpu_cs_p1_user_fence(p, p->chunks[i].kdata,
257 						      &uf_offset);
258 			if (ret)
259 				goto free_partial_kdata;
260 			break;
261 
262 		case AMDGPU_CHUNK_ID_BO_HANDLES:
263 			if (size < sizeof(struct drm_amdgpu_bo_list_in))
264 				goto free_partial_kdata;
265 
266 			/* Only a single BO list is allowed to simplify handling. */
267 			if (p->bo_list)
268 				goto free_partial_kdata;
269 
270 			ret = amdgpu_cs_p1_bo_handles(p, p->chunks[i].kdata);
271 			if (ret)
272 				goto free_partial_kdata;
273 			break;
274 
275 		case AMDGPU_CHUNK_ID_DEPENDENCIES:
276 		case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
277 		case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
278 		case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES:
279 		case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT:
280 		case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL:
281 		case AMDGPU_CHUNK_ID_CP_GFX_SHADOW:
282 			break;
283 
284 		default:
285 			goto free_partial_kdata;
286 		}
287 	}
288 
289 	if (!p->gang_size) {
290 		ret = -EINVAL;
291 		goto free_all_kdata;
292 	}
293 
294 	for (i = 0; i < p->gang_size; ++i) {
295 		ret = amdgpu_job_alloc(p->adev, vm, p->entities[i], vm,
296 				       num_ibs[i], &p->jobs[i]);
297 		if (ret)
298 			goto free_all_kdata;
299 		switch (p->adev->enforce_isolation[fpriv->xcp_id]) {
300 		case AMDGPU_ENFORCE_ISOLATION_DISABLE:
301 		default:
302 			p->jobs[i]->enforce_isolation = false;
303 			p->jobs[i]->run_cleaner_shader = false;
304 			break;
305 		case AMDGPU_ENFORCE_ISOLATION_ENABLE:
306 			p->jobs[i]->enforce_isolation = true;
307 			p->jobs[i]->run_cleaner_shader = true;
308 			break;
309 		case AMDGPU_ENFORCE_ISOLATION_ENABLE_LEGACY:
310 			p->jobs[i]->enforce_isolation = true;
311 			p->jobs[i]->run_cleaner_shader = false;
312 			break;
313 		case AMDGPU_ENFORCE_ISOLATION_NO_CLEANER_SHADER:
314 			p->jobs[i]->enforce_isolation = true;
315 			p->jobs[i]->run_cleaner_shader = false;
316 			break;
317 		}
318 	}
319 	p->gang_leader = p->jobs[p->gang_leader_idx];
320 
321 	if (p->ctx->generation != p->gang_leader->generation) {
322 		ret = -ECANCELED;
323 		goto free_all_kdata;
324 	}
325 
326 	if (p->uf_bo)
327 		p->gang_leader->uf_addr = uf_offset;
328 	kvfree(chunk_array);
329 
330 	/* Use this opportunity to fill in task info for the vm */
331 	amdgpu_vm_set_task_info(vm);
332 
333 	return 0;
334 
335 free_all_kdata:
336 	i = p->nchunks - 1;
337 free_partial_kdata:
338 	for (; i >= 0; i--)
339 		kvfree(p->chunks[i].kdata);
340 	kvfree(p->chunks);
341 	p->chunks = NULL;
342 	p->nchunks = 0;
343 free_chunk:
344 	kvfree(chunk_array);
345 
346 	return ret;
347 }
348 
349 static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p,
350 			   struct amdgpu_cs_chunk *chunk,
351 			   unsigned int *ce_preempt,
352 			   unsigned int *de_preempt)
353 {
354 	struct drm_amdgpu_cs_chunk_ib *chunk_ib = chunk->kdata;
355 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
356 	struct amdgpu_vm *vm = &fpriv->vm;
357 	struct amdgpu_ring *ring;
358 	struct amdgpu_job *job;
359 	struct amdgpu_ib *ib;
360 	int r;
361 
362 	r = amdgpu_cs_job_idx(p, chunk_ib);
363 	if (r < 0)
364 		return r;
365 
366 	job = p->jobs[r];
367 	ring = amdgpu_job_ring(job);
368 	ib = &job->ibs[job->num_ibs++];
369 
370 	/* submissions to kernel queues are disabled */
371 	if (ring->no_user_submission)
372 		return -EINVAL;
373 
374 	/* MM engine doesn't support user fences */
375 	if (p->uf_bo && ring->funcs->no_user_fence)
376 		return -EINVAL;
377 
378 	if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX &&
379 	    chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
380 		if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
381 			(*ce_preempt)++;
382 		else
383 			(*de_preempt)++;
384 
385 		/* Each GFX command submit allows only 1 IB max
386 		 * preemptible for CE & DE */
387 		if (*ce_preempt > 1 || *de_preempt > 1)
388 			return -EINVAL;
389 	}
390 
391 	if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
392 		job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
393 
394 	r =  amdgpu_ib_get(p->adev, vm, ring->funcs->parse_cs ?
395 			   chunk_ib->ib_bytes : 0,
396 			   AMDGPU_IB_POOL_DELAYED, ib);
397 	if (r) {
398 		DRM_ERROR("Failed to get ib !\n");
399 		return r;
400 	}
401 
402 	ib->gpu_addr = chunk_ib->va_start;
403 	ib->length_dw = chunk_ib->ib_bytes / 4;
404 	ib->flags = chunk_ib->flags;
405 	return 0;
406 }
407 
408 static int amdgpu_cs_p2_dependencies(struct amdgpu_cs_parser *p,
409 				     struct amdgpu_cs_chunk *chunk)
410 {
411 	struct drm_amdgpu_cs_chunk_dep *deps = chunk->kdata;
412 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
413 	unsigned int num_deps;
414 	int i, r;
415 
416 	num_deps = chunk->length_dw * 4 /
417 		sizeof(struct drm_amdgpu_cs_chunk_dep);
418 
419 	for (i = 0; i < num_deps; ++i) {
420 		struct amdgpu_ctx *ctx;
421 		struct drm_sched_entity *entity;
422 		struct dma_fence *fence;
423 
424 		ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
425 		if (ctx == NULL)
426 			return -EINVAL;
427 
428 		r = amdgpu_ctx_get_entity(ctx, deps[i].ip_type,
429 					  deps[i].ip_instance,
430 					  deps[i].ring, &entity);
431 		if (r) {
432 			amdgpu_ctx_put(ctx);
433 			return r;
434 		}
435 
436 		fence = amdgpu_ctx_get_fence(ctx, entity, deps[i].handle);
437 		amdgpu_ctx_put(ctx);
438 
439 		if (IS_ERR(fence))
440 			return PTR_ERR(fence);
441 		else if (!fence)
442 			continue;
443 
444 		if (chunk->chunk_id == AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES) {
445 			struct drm_sched_fence *s_fence;
446 			struct dma_fence *old = fence;
447 
448 			s_fence = to_drm_sched_fence(fence);
449 			fence = dma_fence_get(&s_fence->scheduled);
450 			dma_fence_put(old);
451 		}
452 
453 		r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL);
454 		dma_fence_put(fence);
455 		if (r)
456 			return r;
457 	}
458 	return 0;
459 }
460 
461 static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,
462 					 uint32_t handle, u64 point,
463 					 u64 flags)
464 {
465 	struct dma_fence *fence;
466 	int r;
467 
468 	r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);
469 	if (r) {
470 		DRM_ERROR("syncobj %u failed to find fence @ %llu (%d)!\n",
471 			  handle, point, r);
472 		return r;
473 	}
474 
475 	r = amdgpu_sync_fence(&p->sync, fence, GFP_KERNEL);
476 	dma_fence_put(fence);
477 	return r;
478 }
479 
480 static int amdgpu_cs_p2_syncobj_in(struct amdgpu_cs_parser *p,
481 				   struct amdgpu_cs_chunk *chunk)
482 {
483 	struct drm_amdgpu_cs_chunk_sem *deps = chunk->kdata;
484 	unsigned int num_deps;
485 	int i, r;
486 
487 	num_deps = chunk->length_dw * 4 /
488 		sizeof(struct drm_amdgpu_cs_chunk_sem);
489 	for (i = 0; i < num_deps; ++i) {
490 		r = amdgpu_syncobj_lookup_and_add(p, deps[i].handle, 0, 0);
491 		if (r)
492 			return r;
493 	}
494 
495 	return 0;
496 }
497 
498 static int amdgpu_cs_p2_syncobj_timeline_wait(struct amdgpu_cs_parser *p,
499 					      struct amdgpu_cs_chunk *chunk)
500 {
501 	struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps = chunk->kdata;
502 	unsigned int num_deps;
503 	int i, r;
504 
505 	num_deps = chunk->length_dw * 4 /
506 		sizeof(struct drm_amdgpu_cs_chunk_syncobj);
507 	for (i = 0; i < num_deps; ++i) {
508 		r = amdgpu_syncobj_lookup_and_add(p, syncobj_deps[i].handle,
509 						  syncobj_deps[i].point,
510 						  syncobj_deps[i].flags);
511 		if (r)
512 			return r;
513 	}
514 
515 	return 0;
516 }
517 
518 static int amdgpu_cs_p2_syncobj_out(struct amdgpu_cs_parser *p,
519 				    struct amdgpu_cs_chunk *chunk)
520 {
521 	struct drm_amdgpu_cs_chunk_sem *deps = chunk->kdata;
522 	unsigned int num_deps;
523 	int i;
524 
525 	num_deps = chunk->length_dw * 4 /
526 		sizeof(struct drm_amdgpu_cs_chunk_sem);
527 
528 	if (p->post_deps)
529 		return -EINVAL;
530 
531 	p->post_deps = kmalloc_array(num_deps, sizeof(*p->post_deps),
532 				     GFP_KERNEL);
533 	p->num_post_deps = 0;
534 
535 	if (!p->post_deps)
536 		return -ENOMEM;
537 
538 
539 	for (i = 0; i < num_deps; ++i) {
540 		p->post_deps[i].syncobj =
541 			drm_syncobj_find(p->filp, deps[i].handle);
542 		if (!p->post_deps[i].syncobj)
543 			return -EINVAL;
544 		p->post_deps[i].chain = NULL;
545 		p->post_deps[i].point = 0;
546 		p->num_post_deps++;
547 	}
548 
549 	return 0;
550 }
551 
552 static int amdgpu_cs_p2_syncobj_timeline_signal(struct amdgpu_cs_parser *p,
553 						struct amdgpu_cs_chunk *chunk)
554 {
555 	struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps = chunk->kdata;
556 	unsigned int num_deps;
557 	int i;
558 
559 	num_deps = chunk->length_dw * 4 /
560 		sizeof(struct drm_amdgpu_cs_chunk_syncobj);
561 
562 	if (p->post_deps)
563 		return -EINVAL;
564 
565 	p->post_deps = kmalloc_array(num_deps, sizeof(*p->post_deps),
566 				     GFP_KERNEL);
567 	p->num_post_deps = 0;
568 
569 	if (!p->post_deps)
570 		return -ENOMEM;
571 
572 	for (i = 0; i < num_deps; ++i) {
573 		struct amdgpu_cs_post_dep *dep = &p->post_deps[i];
574 
575 		dep->chain = NULL;
576 		if (syncobj_deps[i].point) {
577 			dep->chain = dma_fence_chain_alloc();
578 			if (!dep->chain)
579 				return -ENOMEM;
580 		}
581 
582 		dep->syncobj = drm_syncobj_find(p->filp,
583 						syncobj_deps[i].handle);
584 		if (!dep->syncobj) {
585 			dma_fence_chain_free(dep->chain);
586 			return -EINVAL;
587 		}
588 		dep->point = syncobj_deps[i].point;
589 		p->num_post_deps++;
590 	}
591 
592 	return 0;
593 }
594 
595 static int amdgpu_cs_p2_shadow(struct amdgpu_cs_parser *p,
596 			       struct amdgpu_cs_chunk *chunk)
597 {
598 	struct drm_amdgpu_cs_chunk_cp_gfx_shadow *shadow = chunk->kdata;
599 	int i;
600 
601 	if (shadow->flags & ~AMDGPU_CS_CHUNK_CP_GFX_SHADOW_FLAGS_INIT_SHADOW)
602 		return -EINVAL;
603 
604 	for (i = 0; i < p->gang_size; ++i) {
605 		p->jobs[i]->shadow_va = shadow->shadow_va;
606 		p->jobs[i]->csa_va = shadow->csa_va;
607 		p->jobs[i]->gds_va = shadow->gds_va;
608 		p->jobs[i]->init_shadow =
609 			shadow->flags & AMDGPU_CS_CHUNK_CP_GFX_SHADOW_FLAGS_INIT_SHADOW;
610 	}
611 
612 	return 0;
613 }
614 
615 static int amdgpu_cs_pass2(struct amdgpu_cs_parser *p)
616 {
617 	unsigned int ce_preempt = 0, de_preempt = 0;
618 	int i, r;
619 
620 	for (i = 0; i < p->nchunks; ++i) {
621 		struct amdgpu_cs_chunk *chunk;
622 
623 		chunk = &p->chunks[i];
624 
625 		switch (chunk->chunk_id) {
626 		case AMDGPU_CHUNK_ID_IB:
627 			r = amdgpu_cs_p2_ib(p, chunk, &ce_preempt, &de_preempt);
628 			if (r)
629 				return r;
630 			break;
631 		case AMDGPU_CHUNK_ID_DEPENDENCIES:
632 		case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES:
633 			r = amdgpu_cs_p2_dependencies(p, chunk);
634 			if (r)
635 				return r;
636 			break;
637 		case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
638 			r = amdgpu_cs_p2_syncobj_in(p, chunk);
639 			if (r)
640 				return r;
641 			break;
642 		case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
643 			r = amdgpu_cs_p2_syncobj_out(p, chunk);
644 			if (r)
645 				return r;
646 			break;
647 		case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT:
648 			r = amdgpu_cs_p2_syncobj_timeline_wait(p, chunk);
649 			if (r)
650 				return r;
651 			break;
652 		case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL:
653 			r = amdgpu_cs_p2_syncobj_timeline_signal(p, chunk);
654 			if (r)
655 				return r;
656 			break;
657 		case AMDGPU_CHUNK_ID_CP_GFX_SHADOW:
658 			r = amdgpu_cs_p2_shadow(p, chunk);
659 			if (r)
660 				return r;
661 			break;
662 		}
663 	}
664 
665 	return 0;
666 }
667 
668 /* Convert microseconds to bytes. */
669 static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
670 {
671 	if (us <= 0 || !adev->mm_stats.log2_max_MBps)
672 		return 0;
673 
674 	/* Since accum_us is incremented by a million per second, just
675 	 * multiply it by the number of MB/s to get the number of bytes.
676 	 */
677 	return us << adev->mm_stats.log2_max_MBps;
678 }
679 
680 static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
681 {
682 	if (!adev->mm_stats.log2_max_MBps)
683 		return 0;
684 
685 	return bytes >> adev->mm_stats.log2_max_MBps;
686 }
687 
688 /* Returns how many bytes TTM can move right now. If no bytes can be moved,
689  * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
690  * which means it can go over the threshold once. If that happens, the driver
691  * will be in debt and no other buffer migrations can be done until that debt
692  * is repaid.
693  *
694  * This approach allows moving a buffer of any size (it's important to allow
695  * that).
696  *
697  * The currency is simply time in microseconds and it increases as the clock
698  * ticks. The accumulated microseconds (us) are converted to bytes and
699  * returned.
700  */
701 static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
702 					      u64 *max_bytes,
703 					      u64 *max_vis_bytes)
704 {
705 	s64 time_us, increment_us;
706 	u64 free_vram, total_vram, used_vram;
707 	/* Allow a maximum of 200 accumulated ms. This is basically per-IB
708 	 * throttling.
709 	 *
710 	 * It means that in order to get full max MBps, at least 5 IBs per
711 	 * second must be submitted and not more than 200ms apart from each
712 	 * other.
713 	 */
714 	const s64 us_upper_bound = 200000;
715 
716 	if (!adev->mm_stats.log2_max_MBps) {
717 		*max_bytes = 0;
718 		*max_vis_bytes = 0;
719 		return;
720 	}
721 
722 	total_vram = adev->gmc.real_vram_size - atomic64_read(&adev->vram_pin_size);
723 	used_vram = ttm_resource_manager_usage(&adev->mman.vram_mgr.manager);
724 	free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
725 
726 	spin_lock(&adev->mm_stats.lock);
727 
728 	/* Increase the amount of accumulated us. */
729 	time_us = ktime_to_us(ktime_get());
730 	increment_us = time_us - adev->mm_stats.last_update_us;
731 	adev->mm_stats.last_update_us = time_us;
732 	adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
733 				      us_upper_bound);
734 
735 	/* This prevents the short period of low performance when the VRAM
736 	 * usage is low and the driver is in debt or doesn't have enough
737 	 * accumulated us to fill VRAM quickly.
738 	 *
739 	 * The situation can occur in these cases:
740 	 * - a lot of VRAM is freed by userspace
741 	 * - the presence of a big buffer causes a lot of evictions
742 	 *   (solution: split buffers into smaller ones)
743 	 *
744 	 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
745 	 * accum_us to a positive number.
746 	 */
747 	if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
748 		s64 min_us;
749 
750 		/* Be more aggressive on dGPUs. Try to fill a portion of free
751 		 * VRAM now.
752 		 */
753 		if (!(adev->flags & AMD_IS_APU))
754 			min_us = bytes_to_us(adev, free_vram / 4);
755 		else
756 			min_us = 0; /* Reset accum_us on APUs. */
757 
758 		adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
759 	}
760 
761 	/* This is set to 0 if the driver is in debt to disallow (optional)
762 	 * buffer moves.
763 	 */
764 	*max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
765 
766 	/* Do the same for visible VRAM if half of it is free */
767 	if (!amdgpu_gmc_vram_full_visible(&adev->gmc)) {
768 		u64 total_vis_vram = adev->gmc.visible_vram_size;
769 		u64 used_vis_vram =
770 		  amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr);
771 
772 		if (used_vis_vram < total_vis_vram) {
773 			u64 free_vis_vram = total_vis_vram - used_vis_vram;
774 
775 			adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
776 							  increment_us, us_upper_bound);
777 
778 			if (free_vis_vram >= total_vis_vram / 2)
779 				adev->mm_stats.accum_us_vis =
780 					max(bytes_to_us(adev, free_vis_vram / 2),
781 					    adev->mm_stats.accum_us_vis);
782 		}
783 
784 		*max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
785 	} else {
786 		*max_vis_bytes = 0;
787 	}
788 
789 	spin_unlock(&adev->mm_stats.lock);
790 }
791 
792 /* Report how many bytes have really been moved for the last command
793  * submission. This can result in a debt that can stop buffer migrations
794  * temporarily.
795  */
796 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
797 				  u64 num_vis_bytes)
798 {
799 	spin_lock(&adev->mm_stats.lock);
800 	adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
801 	adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
802 	spin_unlock(&adev->mm_stats.lock);
803 }
804 
805 static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo)
806 {
807 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
808 	struct amdgpu_cs_parser *p = param;
809 	struct ttm_operation_ctx ctx = {
810 		.interruptible = true,
811 		.no_wait_gpu = false,
812 		.resv = bo->tbo.base.resv
813 	};
814 	uint32_t domain;
815 	int r;
816 
817 	if (bo->tbo.pin_count)
818 		return 0;
819 
820 	/* Don't move this buffer if we have depleted our allowance
821 	 * to move it. Don't move anything if the threshold is zero.
822 	 */
823 	if (p->bytes_moved < p->bytes_moved_threshold &&
824 	    (!bo->tbo.base.dma_buf ||
825 	    list_empty(&bo->tbo.base.dma_buf->attachments))) {
826 		if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
827 		    (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
828 			/* And don't move a CPU_ACCESS_REQUIRED BO to limited
829 			 * visible VRAM if we've depleted our allowance to do
830 			 * that.
831 			 */
832 			if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
833 				domain = bo->preferred_domains;
834 			else
835 				domain = bo->allowed_domains;
836 		} else {
837 			domain = bo->preferred_domains;
838 		}
839 	} else {
840 		domain = bo->allowed_domains;
841 	}
842 
843 retry:
844 	amdgpu_bo_placement_from_domain(bo, domain);
845 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
846 
847 	p->bytes_moved += ctx.bytes_moved;
848 	if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
849 	    amdgpu_res_cpu_visible(adev, bo->tbo.resource))
850 		p->bytes_moved_vis += ctx.bytes_moved;
851 
852 	if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
853 		domain = bo->allowed_domains;
854 		goto retry;
855 	}
856 
857 	return r;
858 }
859 
860 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
861 				union drm_amdgpu_cs *cs)
862 {
863 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
864 	struct ttm_operation_ctx ctx = { true, false };
865 	struct amdgpu_vm *vm = &fpriv->vm;
866 	struct amdgpu_bo_list_entry *e;
867 	struct drm_gem_object *obj;
868 	unsigned long index;
869 	unsigned int i;
870 	int r;
871 
872 	/* p->bo_list could already be assigned if AMDGPU_CHUNK_ID_BO_HANDLES is present */
873 	if (cs->in.bo_list_handle) {
874 		if (p->bo_list)
875 			return -EINVAL;
876 
877 		r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
878 				       &p->bo_list);
879 		if (r)
880 			return r;
881 	} else if (!p->bo_list) {
882 		/* Create a empty bo_list when no handle is provided */
883 		r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
884 					  &p->bo_list);
885 		if (r)
886 			return r;
887 	}
888 
889 	mutex_lock(&p->bo_list->bo_list_mutex);
890 
891 	/* Get userptr backing pages. If pages are updated after registered
892 	 * in amdgpu_gem_userptr_ioctl(), amdgpu_cs_list_validate() will do
893 	 * amdgpu_ttm_backend_bind() to flush and invalidate new pages
894 	 */
895 	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
896 		bool userpage_invalidated = false;
897 		struct amdgpu_bo *bo = e->bo;
898 		int i;
899 
900 		e->user_pages = kvcalloc(bo->tbo.ttm->num_pages,
901 					 sizeof(struct page *),
902 					 GFP_KERNEL);
903 		if (!e->user_pages) {
904 			DRM_ERROR("kvmalloc_array failure\n");
905 			r = -ENOMEM;
906 			goto out_free_user_pages;
907 		}
908 
909 		r = amdgpu_ttm_tt_get_user_pages(bo, e->user_pages, &e->range);
910 		if (r) {
911 			kvfree(e->user_pages);
912 			e->user_pages = NULL;
913 			goto out_free_user_pages;
914 		}
915 
916 		for (i = 0; i < bo->tbo.ttm->num_pages; i++) {
917 			if (bo->tbo.ttm->pages[i] != e->user_pages[i]) {
918 				userpage_invalidated = true;
919 				break;
920 			}
921 		}
922 		e->user_invalidated = userpage_invalidated;
923 	}
924 
925 	drm_exec_until_all_locked(&p->exec) {
926 		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec, 1 + p->gang_size);
927 		drm_exec_retry_on_contention(&p->exec);
928 		if (unlikely(r))
929 			goto out_free_user_pages;
930 
931 		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
932 			/* One fence for TTM and one for each CS job */
933 			r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base,
934 						 1 + p->gang_size);
935 			drm_exec_retry_on_contention(&p->exec);
936 			if (unlikely(r))
937 				goto out_free_user_pages;
938 
939 			e->bo_va = amdgpu_vm_bo_find(vm, e->bo);
940 		}
941 
942 		if (p->uf_bo) {
943 			r = drm_exec_prepare_obj(&p->exec, &p->uf_bo->tbo.base,
944 						 1 + p->gang_size);
945 			drm_exec_retry_on_contention(&p->exec);
946 			if (unlikely(r))
947 				goto out_free_user_pages;
948 		}
949 	}
950 
951 	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
952 		struct mm_struct *usermm;
953 
954 		usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm);
955 		if (usermm && usermm != current->mm) {
956 			r = -EPERM;
957 			goto out_free_user_pages;
958 		}
959 
960 		if (amdgpu_ttm_tt_is_userptr(e->bo->tbo.ttm) &&
961 		    e->user_invalidated && e->user_pages) {
962 			amdgpu_bo_placement_from_domain(e->bo,
963 							AMDGPU_GEM_DOMAIN_CPU);
964 			r = ttm_bo_validate(&e->bo->tbo, &e->bo->placement,
965 					    &ctx);
966 			if (r)
967 				goto out_free_user_pages;
968 
969 			amdgpu_ttm_tt_set_user_pages(e->bo->tbo.ttm,
970 						     e->user_pages);
971 		}
972 
973 		kvfree(e->user_pages);
974 		e->user_pages = NULL;
975 	}
976 
977 	amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
978 					  &p->bytes_moved_vis_threshold);
979 	p->bytes_moved = 0;
980 	p->bytes_moved_vis = 0;
981 
982 	r = amdgpu_vm_validate(p->adev, &fpriv->vm, NULL,
983 			       amdgpu_cs_bo_validate, p);
984 	if (r) {
985 		DRM_ERROR("amdgpu_vm_validate() failed.\n");
986 		goto out_free_user_pages;
987 	}
988 
989 	drm_exec_for_each_locked_object(&p->exec, index, obj) {
990 		r = amdgpu_cs_bo_validate(p, gem_to_amdgpu_bo(obj));
991 		if (unlikely(r))
992 			goto out_free_user_pages;
993 	}
994 
995 	if (p->uf_bo) {
996 		r = amdgpu_ttm_alloc_gart(&p->uf_bo->tbo);
997 		if (unlikely(r))
998 			goto out_free_user_pages;
999 
1000 		p->gang_leader->uf_addr += amdgpu_bo_gpu_offset(p->uf_bo);
1001 	}
1002 
1003 	amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
1004 				     p->bytes_moved_vis);
1005 
1006 	for (i = 0; i < p->gang_size; ++i)
1007 		amdgpu_job_set_resources(p->jobs[i], p->bo_list->gds_obj,
1008 					 p->bo_list->gws_obj,
1009 					 p->bo_list->oa_obj);
1010 	return 0;
1011 
1012 out_free_user_pages:
1013 	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
1014 		struct amdgpu_bo *bo = e->bo;
1015 
1016 		if (!e->user_pages)
1017 			continue;
1018 		amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, e->range);
1019 		kvfree(e->user_pages);
1020 		e->user_pages = NULL;
1021 		e->range = NULL;
1022 	}
1023 	mutex_unlock(&p->bo_list->bo_list_mutex);
1024 	return r;
1025 }
1026 
1027 static void trace_amdgpu_cs_ibs(struct amdgpu_cs_parser *p)
1028 {
1029 	int i, j;
1030 
1031 	if (!trace_amdgpu_cs_enabled())
1032 		return;
1033 
1034 	for (i = 0; i < p->gang_size; ++i) {
1035 		struct amdgpu_job *job = p->jobs[i];
1036 
1037 		for (j = 0; j < job->num_ibs; ++j)
1038 			trace_amdgpu_cs(p, job, &job->ibs[j]);
1039 	}
1040 }
1041 
1042 static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
1043 			       struct amdgpu_job *job)
1044 {
1045 	struct amdgpu_ring *ring = amdgpu_job_ring(job);
1046 	unsigned int i;
1047 	int r;
1048 
1049 	/* Only for UVD/VCE VM emulation */
1050 	if (!ring->funcs->parse_cs && !ring->funcs->patch_cs_in_place)
1051 		return 0;
1052 
1053 	for (i = 0; i < job->num_ibs; ++i) {
1054 		struct amdgpu_ib *ib = &job->ibs[i];
1055 		struct amdgpu_bo_va_mapping *m;
1056 		struct amdgpu_bo *aobj;
1057 		uint64_t va_start;
1058 		uint8_t *kptr;
1059 
1060 		va_start = ib->gpu_addr & AMDGPU_GMC_HOLE_MASK;
1061 		r = amdgpu_cs_find_mapping(p, va_start, &aobj, &m);
1062 		if (r) {
1063 			DRM_ERROR("IB va_start is invalid\n");
1064 			return r;
1065 		}
1066 
1067 		if ((va_start + ib->length_dw * 4) >
1068 		    (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
1069 			DRM_ERROR("IB va_start+ib_bytes is invalid\n");
1070 			return -EINVAL;
1071 		}
1072 
1073 		/* the IB should be reserved at this point */
1074 		r = amdgpu_bo_kmap(aobj, (void **)&kptr);
1075 		if (r)
1076 			return r;
1077 
1078 		kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE);
1079 
1080 		if (ring->funcs->parse_cs) {
1081 			memcpy(ib->ptr, kptr, ib->length_dw * 4);
1082 			amdgpu_bo_kunmap(aobj);
1083 
1084 			r = amdgpu_ring_parse_cs(ring, p, job, ib);
1085 			if (r)
1086 				return r;
1087 
1088 			if (ib->sa_bo)
1089 				ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
1090 		} else {
1091 			ib->ptr = (uint32_t *)kptr;
1092 			r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
1093 			amdgpu_bo_kunmap(aobj);
1094 			if (r)
1095 				return r;
1096 		}
1097 	}
1098 
1099 	return 0;
1100 }
1101 
1102 static int amdgpu_cs_patch_jobs(struct amdgpu_cs_parser *p)
1103 {
1104 	unsigned int i;
1105 	int r;
1106 
1107 	for (i = 0; i < p->gang_size; ++i) {
1108 		r = amdgpu_cs_patch_ibs(p, p->jobs[i]);
1109 		if (r)
1110 			return r;
1111 	}
1112 	return 0;
1113 }
1114 
1115 static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
1116 {
1117 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1118 	struct amdgpu_job *job = p->gang_leader;
1119 	struct amdgpu_device *adev = p->adev;
1120 	struct amdgpu_vm *vm = &fpriv->vm;
1121 	struct amdgpu_bo_list_entry *e;
1122 	struct amdgpu_bo_va *bo_va;
1123 	unsigned int i;
1124 	int r;
1125 
1126 	/*
1127 	 * We can't use gang submit on with reserved VMIDs when the VM changes
1128 	 * can't be invalidated by more than one engine at the same time.
1129 	 */
1130 	if (p->gang_size > 1 && !adev->vm_manager.concurrent_flush) {
1131 		for (i = 0; i < p->gang_size; ++i) {
1132 			struct drm_sched_entity *entity = p->entities[i];
1133 			struct drm_gpu_scheduler *sched = entity->rq->sched;
1134 			struct amdgpu_ring *ring = to_amdgpu_ring(sched);
1135 
1136 			if (amdgpu_vmid_uses_reserved(vm, ring->vm_hub))
1137 				return -EINVAL;
1138 		}
1139 	}
1140 
1141 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
1142 	if (r)
1143 		return r;
1144 
1145 	r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
1146 	if (r)
1147 		return r;
1148 
1149 	r = amdgpu_sync_fence(&p->sync, fpriv->prt_va->last_pt_update,
1150 			      GFP_KERNEL);
1151 	if (r)
1152 		return r;
1153 
1154 	if (fpriv->csa_va) {
1155 		bo_va = fpriv->csa_va;
1156 		BUG_ON(!bo_va);
1157 		r = amdgpu_vm_bo_update(adev, bo_va, false);
1158 		if (r)
1159 			return r;
1160 
1161 		r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update,
1162 				      GFP_KERNEL);
1163 		if (r)
1164 			return r;
1165 	}
1166 
1167 	/* FIXME: In theory this loop shouldn't be needed any more when
1168 	 * amdgpu_vm_handle_moved handles all moved BOs that are reserved
1169 	 * with p->ticket. But removing it caused test regressions, so I'm
1170 	 * leaving it here for now.
1171 	 */
1172 	amdgpu_bo_list_for_each_entry(e, p->bo_list) {
1173 		bo_va = e->bo_va;
1174 		if (bo_va == NULL)
1175 			continue;
1176 
1177 		r = amdgpu_vm_bo_update(adev, bo_va, false);
1178 		if (r)
1179 			return r;
1180 
1181 		r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update,
1182 				      GFP_KERNEL);
1183 		if (r)
1184 			return r;
1185 	}
1186 
1187 	r = amdgpu_vm_handle_moved(adev, vm, &p->exec.ticket);
1188 	if (r)
1189 		return r;
1190 
1191 	r = amdgpu_vm_update_pdes(adev, vm, false);
1192 	if (r)
1193 		return r;
1194 
1195 	r = amdgpu_sync_fence(&p->sync, vm->last_update, GFP_KERNEL);
1196 	if (r)
1197 		return r;
1198 
1199 	for (i = 0; i < p->gang_size; ++i) {
1200 		job = p->jobs[i];
1201 
1202 		if (!job->vm)
1203 			continue;
1204 
1205 		job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
1206 	}
1207 
1208 	if (adev->debug_vm) {
1209 		/* Invalidate all BOs to test for userspace bugs */
1210 		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
1211 			struct amdgpu_bo *bo = e->bo;
1212 
1213 			/* ignore duplicates */
1214 			if (!bo)
1215 				continue;
1216 
1217 			amdgpu_vm_bo_invalidate(bo, false);
1218 		}
1219 	}
1220 
1221 	return 0;
1222 }
1223 
1224 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
1225 {
1226 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1227 	struct drm_gpu_scheduler *sched;
1228 	struct drm_gem_object *obj;
1229 	struct dma_fence *fence;
1230 	unsigned long index;
1231 	unsigned int i;
1232 	int r;
1233 
1234 	r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
1235 	if (r) {
1236 		if (r != -ERESTARTSYS)
1237 			DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
1238 		return r;
1239 	}
1240 
1241 	drm_exec_for_each_locked_object(&p->exec, index, obj) {
1242 		struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
1243 
1244 		struct dma_resv *resv = bo->tbo.base.resv;
1245 		enum amdgpu_sync_mode sync_mode;
1246 
1247 		sync_mode = amdgpu_bo_explicit_sync(bo) ?
1248 			AMDGPU_SYNC_EXPLICIT : AMDGPU_SYNC_NE_OWNER;
1249 		r = amdgpu_sync_resv(p->adev, &p->sync, resv, sync_mode,
1250 				     &fpriv->vm);
1251 		if (r)
1252 			return r;
1253 	}
1254 
1255 	for (i = 0; i < p->gang_size; ++i) {
1256 		r = amdgpu_sync_push_to_job(&p->sync, p->jobs[i]);
1257 		if (r)
1258 			return r;
1259 	}
1260 
1261 	sched = p->gang_leader->base.entity->rq->sched;
1262 	while ((fence = amdgpu_sync_get_fence(&p->sync))) {
1263 		struct drm_sched_fence *s_fence = to_drm_sched_fence(fence);
1264 
1265 		/*
1266 		 * When we have an dependency it might be necessary to insert a
1267 		 * pipeline sync to make sure that all caches etc are flushed and the
1268 		 * next job actually sees the results from the previous one
1269 		 * before we start executing on the same scheduler ring.
1270 		 */
1271 		if (!s_fence || s_fence->sched != sched) {
1272 			dma_fence_put(fence);
1273 			continue;
1274 		}
1275 
1276 		r = amdgpu_sync_fence(&p->gang_leader->explicit_sync, fence,
1277 				      GFP_KERNEL);
1278 		dma_fence_put(fence);
1279 		if (r)
1280 			return r;
1281 	}
1282 	return 0;
1283 }
1284 
1285 static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1286 {
1287 	int i;
1288 
1289 	for (i = 0; i < p->num_post_deps; ++i) {
1290 		if (p->post_deps[i].chain && p->post_deps[i].point) {
1291 			drm_syncobj_add_point(p->post_deps[i].syncobj,
1292 					      p->post_deps[i].chain,
1293 					      p->fence, p->post_deps[i].point);
1294 			p->post_deps[i].chain = NULL;
1295 		} else {
1296 			drm_syncobj_replace_fence(p->post_deps[i].syncobj,
1297 						  p->fence);
1298 		}
1299 	}
1300 }
1301 
1302 static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1303 			    union drm_amdgpu_cs *cs)
1304 {
1305 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1306 	struct amdgpu_job *leader = p->gang_leader;
1307 	struct amdgpu_bo_list_entry *e;
1308 	struct drm_gem_object *gobj;
1309 	unsigned long index;
1310 	unsigned int i;
1311 	uint64_t seq;
1312 	int r;
1313 
1314 	for (i = 0; i < p->gang_size; ++i)
1315 		drm_sched_job_arm(&p->jobs[i]->base);
1316 
1317 	for (i = 0; i < p->gang_size; ++i) {
1318 		struct dma_fence *fence;
1319 
1320 		if (p->jobs[i] == leader)
1321 			continue;
1322 
1323 		fence = &p->jobs[i]->base.s_fence->scheduled;
1324 		dma_fence_get(fence);
1325 		r = drm_sched_job_add_dependency(&leader->base, fence);
1326 		if (r) {
1327 			dma_fence_put(fence);
1328 			return r;
1329 		}
1330 	}
1331 
1332 	if (p->gang_size > 1) {
1333 		for (i = 0; i < p->gang_size; ++i)
1334 			amdgpu_job_set_gang_leader(p->jobs[i], leader);
1335 	}
1336 
1337 	/* No memory allocation is allowed while holding the notifier lock.
1338 	 * The lock is held until amdgpu_cs_submit is finished and fence is
1339 	 * added to BOs.
1340 	 */
1341 	mutex_lock(&p->adev->notifier_lock);
1342 
1343 	/* If userptr are invalidated after amdgpu_cs_parser_bos(), return
1344 	 * -EAGAIN, drmIoctl in libdrm will restart the amdgpu_cs_ioctl.
1345 	 */
1346 	r = 0;
1347 	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
1348 		r |= !amdgpu_ttm_tt_get_user_pages_done(e->bo->tbo.ttm,
1349 							e->range);
1350 		e->range = NULL;
1351 	}
1352 	if (r) {
1353 		r = -EAGAIN;
1354 		mutex_unlock(&p->adev->notifier_lock);
1355 		return r;
1356 	}
1357 
1358 	p->fence = dma_fence_get(&leader->base.s_fence->finished);
1359 	drm_exec_for_each_locked_object(&p->exec, index, gobj) {
1360 
1361 		ttm_bo_move_to_lru_tail_unlocked(&gem_to_amdgpu_bo(gobj)->tbo);
1362 
1363 		/* Everybody except for the gang leader uses READ */
1364 		for (i = 0; i < p->gang_size; ++i) {
1365 			if (p->jobs[i] == leader)
1366 				continue;
1367 
1368 			dma_resv_add_fence(gobj->resv,
1369 					   &p->jobs[i]->base.s_fence->finished,
1370 					   DMA_RESV_USAGE_READ);
1371 		}
1372 
1373 		/* The gang leader as remembered as writer */
1374 		dma_resv_add_fence(gobj->resv, p->fence, DMA_RESV_USAGE_WRITE);
1375 	}
1376 
1377 	seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
1378 				   p->fence);
1379 	amdgpu_cs_post_dependencies(p);
1380 
1381 	if ((leader->preamble_status & AMDGPU_PREAMBLE_IB_PRESENT) &&
1382 	    !p->ctx->preamble_presented) {
1383 		leader->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
1384 		p->ctx->preamble_presented = true;
1385 	}
1386 
1387 	cs->out.handle = seq;
1388 	leader->uf_sequence = seq;
1389 
1390 	amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->exec.ticket);
1391 	for (i = 0; i < p->gang_size; ++i) {
1392 		amdgpu_job_free_resources(p->jobs[i]);
1393 		trace_amdgpu_cs_ioctl(p->jobs[i]);
1394 		drm_sched_entity_push_job(&p->jobs[i]->base);
1395 		p->jobs[i] = NULL;
1396 	}
1397 
1398 	amdgpu_vm_move_to_lru_tail(p->adev, &fpriv->vm);
1399 
1400 	mutex_unlock(&p->adev->notifier_lock);
1401 	mutex_unlock(&p->bo_list->bo_list_mutex);
1402 	return 0;
1403 }
1404 
1405 /* Cleanup the parser structure */
1406 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
1407 {
1408 	unsigned int i;
1409 
1410 	amdgpu_sync_free(&parser->sync);
1411 	drm_exec_fini(&parser->exec);
1412 
1413 	for (i = 0; i < parser->num_post_deps; i++) {
1414 		drm_syncobj_put(parser->post_deps[i].syncobj);
1415 		kfree(parser->post_deps[i].chain);
1416 	}
1417 	kfree(parser->post_deps);
1418 
1419 	dma_fence_put(parser->fence);
1420 
1421 	if (parser->ctx)
1422 		amdgpu_ctx_put(parser->ctx);
1423 	if (parser->bo_list)
1424 		amdgpu_bo_list_put(parser->bo_list);
1425 
1426 	for (i = 0; i < parser->nchunks; i++)
1427 		kvfree(parser->chunks[i].kdata);
1428 	kvfree(parser->chunks);
1429 	for (i = 0; i < parser->gang_size; ++i) {
1430 		if (parser->jobs[i])
1431 			amdgpu_job_free(parser->jobs[i]);
1432 	}
1433 	amdgpu_bo_unref(&parser->uf_bo);
1434 }
1435 
1436 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1437 {
1438 	struct amdgpu_device *adev = drm_to_adev(dev);
1439 	struct amdgpu_cs_parser parser;
1440 	int r;
1441 
1442 	if (amdgpu_ras_intr_triggered())
1443 		return -EHWPOISON;
1444 
1445 	if (!adev->accel_working)
1446 		return -EBUSY;
1447 
1448 	r = amdgpu_cs_parser_init(&parser, adev, filp, data);
1449 	if (r) {
1450 		DRM_ERROR_RATELIMITED("Failed to initialize parser %d!\n", r);
1451 		return r;
1452 	}
1453 
1454 	r = amdgpu_cs_pass1(&parser, data);
1455 	if (r)
1456 		goto error_fini;
1457 
1458 	r = amdgpu_cs_pass2(&parser);
1459 	if (r)
1460 		goto error_fini;
1461 
1462 	r = amdgpu_cs_parser_bos(&parser, data);
1463 	if (r) {
1464 		if (r == -ENOMEM)
1465 			DRM_ERROR("Not enough memory for command submission!\n");
1466 		else if (r != -ERESTARTSYS && r != -EAGAIN)
1467 			DRM_DEBUG("Failed to process the buffer list %d!\n", r);
1468 		goto error_fini;
1469 	}
1470 
1471 	r = amdgpu_cs_patch_jobs(&parser);
1472 	if (r)
1473 		goto error_backoff;
1474 
1475 	r = amdgpu_cs_vm_handling(&parser);
1476 	if (r)
1477 		goto error_backoff;
1478 
1479 	r = amdgpu_cs_sync_rings(&parser);
1480 	if (r)
1481 		goto error_backoff;
1482 
1483 	trace_amdgpu_cs_ibs(&parser);
1484 
1485 	r = amdgpu_cs_submit(&parser, data);
1486 	if (r)
1487 		goto error_backoff;
1488 
1489 	amdgpu_cs_parser_fini(&parser);
1490 	return 0;
1491 
1492 error_backoff:
1493 	mutex_unlock(&parser.bo_list->bo_list_mutex);
1494 
1495 error_fini:
1496 	amdgpu_cs_parser_fini(&parser);
1497 	return r;
1498 }
1499 
1500 /**
1501  * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1502  *
1503  * @dev: drm device
1504  * @data: data from userspace
1505  * @filp: file private
1506  *
1507  * Wait for the command submission identified by handle to finish.
1508  */
1509 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1510 			 struct drm_file *filp)
1511 {
1512 	union drm_amdgpu_wait_cs *wait = data;
1513 	unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
1514 	struct drm_sched_entity *entity;
1515 	struct amdgpu_ctx *ctx;
1516 	struct dma_fence *fence;
1517 	long r;
1518 
1519 	ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1520 	if (ctx == NULL)
1521 		return -EINVAL;
1522 
1523 	r = amdgpu_ctx_get_entity(ctx, wait->in.ip_type, wait->in.ip_instance,
1524 				  wait->in.ring, &entity);
1525 	if (r) {
1526 		amdgpu_ctx_put(ctx);
1527 		return r;
1528 	}
1529 
1530 	fence = amdgpu_ctx_get_fence(ctx, entity, wait->in.handle);
1531 	if (IS_ERR(fence))
1532 		r = PTR_ERR(fence);
1533 	else if (fence) {
1534 		r = dma_fence_wait_timeout(fence, true, timeout);
1535 		if (r > 0 && fence->error)
1536 			r = fence->error;
1537 		dma_fence_put(fence);
1538 	} else
1539 		r = 1;
1540 
1541 	amdgpu_ctx_put(ctx);
1542 	if (r < 0)
1543 		return r;
1544 
1545 	memset(wait, 0, sizeof(*wait));
1546 	wait->out.status = (r == 0);
1547 
1548 	return 0;
1549 }
1550 
1551 /**
1552  * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1553  *
1554  * @adev: amdgpu device
1555  * @filp: file private
1556  * @user: drm_amdgpu_fence copied from user space
1557  */
1558 static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1559 					     struct drm_file *filp,
1560 					     struct drm_amdgpu_fence *user)
1561 {
1562 	struct drm_sched_entity *entity;
1563 	struct amdgpu_ctx *ctx;
1564 	struct dma_fence *fence;
1565 	int r;
1566 
1567 	ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1568 	if (ctx == NULL)
1569 		return ERR_PTR(-EINVAL);
1570 
1571 	r = amdgpu_ctx_get_entity(ctx, user->ip_type, user->ip_instance,
1572 				  user->ring, &entity);
1573 	if (r) {
1574 		amdgpu_ctx_put(ctx);
1575 		return ERR_PTR(r);
1576 	}
1577 
1578 	fence = amdgpu_ctx_get_fence(ctx, entity, user->seq_no);
1579 	amdgpu_ctx_put(ctx);
1580 
1581 	return fence;
1582 }
1583 
1584 int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1585 				    struct drm_file *filp)
1586 {
1587 	struct amdgpu_device *adev = drm_to_adev(dev);
1588 	union drm_amdgpu_fence_to_handle *info = data;
1589 	struct dma_fence *fence;
1590 	struct drm_syncobj *syncobj;
1591 	struct sync_file *sync_file;
1592 	int fd, r;
1593 
1594 	fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1595 	if (IS_ERR(fence))
1596 		return PTR_ERR(fence);
1597 
1598 	if (!fence)
1599 		fence = dma_fence_get_stub();
1600 
1601 	switch (info->in.what) {
1602 	case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1603 		r = drm_syncobj_create(&syncobj, 0, fence);
1604 		dma_fence_put(fence);
1605 		if (r)
1606 			return r;
1607 		r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1608 		drm_syncobj_put(syncobj);
1609 		return r;
1610 
1611 	case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1612 		r = drm_syncobj_create(&syncobj, 0, fence);
1613 		dma_fence_put(fence);
1614 		if (r)
1615 			return r;
1616 		r = drm_syncobj_get_fd(syncobj, (int *)&info->out.handle);
1617 		drm_syncobj_put(syncobj);
1618 		return r;
1619 
1620 	case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1621 		fd = get_unused_fd_flags(O_CLOEXEC);
1622 		if (fd < 0) {
1623 			dma_fence_put(fence);
1624 			return fd;
1625 		}
1626 
1627 		sync_file = sync_file_create(fence);
1628 		dma_fence_put(fence);
1629 		if (!sync_file) {
1630 			put_unused_fd(fd);
1631 			return -ENOMEM;
1632 		}
1633 
1634 		fd_install(fd, sync_file->file);
1635 		info->out.handle = fd;
1636 		return 0;
1637 
1638 	default:
1639 		dma_fence_put(fence);
1640 		return -EINVAL;
1641 	}
1642 }
1643 
1644 /**
1645  * amdgpu_cs_wait_all_fences - wait on all fences to signal
1646  *
1647  * @adev: amdgpu device
1648  * @filp: file private
1649  * @wait: wait parameters
1650  * @fences: array of drm_amdgpu_fence
1651  */
1652 static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1653 				     struct drm_file *filp,
1654 				     union drm_amdgpu_wait_fences *wait,
1655 				     struct drm_amdgpu_fence *fences)
1656 {
1657 	uint32_t fence_count = wait->in.fence_count;
1658 	unsigned int i;
1659 	long r = 1;
1660 
1661 	for (i = 0; i < fence_count; i++) {
1662 		struct dma_fence *fence;
1663 		unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1664 
1665 		fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1666 		if (IS_ERR(fence))
1667 			return PTR_ERR(fence);
1668 		else if (!fence)
1669 			continue;
1670 
1671 		r = dma_fence_wait_timeout(fence, true, timeout);
1672 		if (r > 0 && fence->error)
1673 			r = fence->error;
1674 
1675 		dma_fence_put(fence);
1676 		if (r < 0)
1677 			return r;
1678 
1679 		if (r == 0)
1680 			break;
1681 	}
1682 
1683 	memset(wait, 0, sizeof(*wait));
1684 	wait->out.status = (r > 0);
1685 
1686 	return 0;
1687 }
1688 
1689 /**
1690  * amdgpu_cs_wait_any_fence - wait on any fence to signal
1691  *
1692  * @adev: amdgpu device
1693  * @filp: file private
1694  * @wait: wait parameters
1695  * @fences: array of drm_amdgpu_fence
1696  */
1697 static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1698 				    struct drm_file *filp,
1699 				    union drm_amdgpu_wait_fences *wait,
1700 				    struct drm_amdgpu_fence *fences)
1701 {
1702 	unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1703 	uint32_t fence_count = wait->in.fence_count;
1704 	uint32_t first = ~0;
1705 	struct dma_fence **array;
1706 	unsigned int i;
1707 	long r;
1708 
1709 	/* Prepare the fence array */
1710 	array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1711 
1712 	if (array == NULL)
1713 		return -ENOMEM;
1714 
1715 	for (i = 0; i < fence_count; i++) {
1716 		struct dma_fence *fence;
1717 
1718 		fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1719 		if (IS_ERR(fence)) {
1720 			r = PTR_ERR(fence);
1721 			goto err_free_fence_array;
1722 		} else if (fence) {
1723 			array[i] = fence;
1724 		} else { /* NULL, the fence has been already signaled */
1725 			r = 1;
1726 			first = i;
1727 			goto out;
1728 		}
1729 	}
1730 
1731 	r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1732 				       &first);
1733 	if (r < 0)
1734 		goto err_free_fence_array;
1735 
1736 out:
1737 	memset(wait, 0, sizeof(*wait));
1738 	wait->out.status = (r > 0);
1739 	wait->out.first_signaled = first;
1740 
1741 	if (first < fence_count && array[first])
1742 		r = array[first]->error;
1743 	else
1744 		r = 0;
1745 
1746 err_free_fence_array:
1747 	for (i = 0; i < fence_count; i++)
1748 		dma_fence_put(array[i]);
1749 	kfree(array);
1750 
1751 	return r;
1752 }
1753 
1754 /**
1755  * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1756  *
1757  * @dev: drm device
1758  * @data: data from userspace
1759  * @filp: file private
1760  */
1761 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1762 				struct drm_file *filp)
1763 {
1764 	struct amdgpu_device *adev = drm_to_adev(dev);
1765 	union drm_amdgpu_wait_fences *wait = data;
1766 	uint32_t fence_count = wait->in.fence_count;
1767 	struct drm_amdgpu_fence *fences_user;
1768 	struct drm_amdgpu_fence *fences;
1769 	int r;
1770 
1771 	/* Get the fences from userspace */
1772 	fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1773 			GFP_KERNEL);
1774 	if (fences == NULL)
1775 		return -ENOMEM;
1776 
1777 	fences_user = u64_to_user_ptr(wait->in.fences);
1778 	if (copy_from_user(fences, fences_user,
1779 		sizeof(struct drm_amdgpu_fence) * fence_count)) {
1780 		r = -EFAULT;
1781 		goto err_free_fences;
1782 	}
1783 
1784 	if (wait->in.wait_all)
1785 		r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1786 	else
1787 		r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1788 
1789 err_free_fences:
1790 	kfree(fences);
1791 
1792 	return r;
1793 }
1794 
1795 /**
1796  * amdgpu_cs_find_mapping - find bo_va for VM address
1797  *
1798  * @parser: command submission parser context
1799  * @addr: VM address
1800  * @bo: resulting BO of the mapping found
1801  * @map: Placeholder to return found BO mapping
1802  *
1803  * Search the buffer objects in the command submission context for a certain
1804  * virtual memory address. Returns allocation structure when found, NULL
1805  * otherwise.
1806  */
1807 int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1808 			   uint64_t addr, struct amdgpu_bo **bo,
1809 			   struct amdgpu_bo_va_mapping **map)
1810 {
1811 	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1812 	struct ttm_operation_ctx ctx = { false, false };
1813 	struct amdgpu_vm *vm = &fpriv->vm;
1814 	struct amdgpu_bo_va_mapping *mapping;
1815 	int i, r;
1816 
1817 	addr /= AMDGPU_GPU_PAGE_SIZE;
1818 
1819 	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1820 	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1821 		return -EINVAL;
1822 
1823 	*bo = mapping->bo_va->base.bo;
1824 	*map = mapping;
1825 
1826 	/* Double check that the BO is reserved by this CS */
1827 	if (dma_resv_locking_ctx((*bo)->tbo.base.resv) != &parser->exec.ticket)
1828 		return -EINVAL;
1829 
1830 	/* Make sure VRAM is allocated contigiously */
1831 	(*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1832 	if ((*bo)->tbo.resource->mem_type == TTM_PL_VRAM &&
1833 	    !((*bo)->tbo.resource->placement & TTM_PL_FLAG_CONTIGUOUS)) {
1834 
1835 		amdgpu_bo_placement_from_domain(*bo, (*bo)->allowed_domains);
1836 		for (i = 0; i < (*bo)->placement.num_placement; i++)
1837 			(*bo)->placements[i].flags |= TTM_PL_FLAG_CONTIGUOUS;
1838 		r = ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, &ctx);
1839 		if (r)
1840 			return r;
1841 	}
1842 
1843 	return amdgpu_ttm_alloc_gart(&(*bo)->tbo);
1844 }
1845