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