]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
drm/amdgpu: fix leaking the IBs on error
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_cs.c
CommitLineData
d38ceaf9
AD
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#include <linux/list_sort.h>
28#include <drm/drmP.h>
29#include <drm/amdgpu_drm.h>
30#include "amdgpu.h"
31#include "amdgpu_trace.h"
32
33#define AMDGPU_CS_MAX_PRIORITY 32u
34#define AMDGPU_CS_NUM_BUCKETS (AMDGPU_CS_MAX_PRIORITY + 1)
35
36/* This is based on the bucket sort with O(n) time complexity.
37 * An item with priority "i" is added to bucket[i]. The lists are then
38 * concatenated in descending order.
39 */
40struct amdgpu_cs_buckets {
41 struct list_head bucket[AMDGPU_CS_NUM_BUCKETS];
42};
43
44static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets *b)
45{
46 unsigned i;
47
48 for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++)
49 INIT_LIST_HEAD(&b->bucket[i]);
50}
51
52static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets *b,
53 struct list_head *item, unsigned priority)
54{
55 /* Since buffers which appear sooner in the relocation list are
56 * likely to be used more often than buffers which appear later
57 * in the list, the sort mustn't change the ordering of buffers
58 * with the same priority, i.e. it must be stable.
59 */
60 list_add_tail(item, &b->bucket[min(priority, AMDGPU_CS_MAX_PRIORITY)]);
61}
62
63static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets *b,
64 struct list_head *out_list)
65{
66 unsigned i;
67
68 /* Connect the sorted buckets in the output list. */
69 for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) {
70 list_splice(&b->bucket[i], out_list);
71 }
72}
73
74int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
75 u32 ip_instance, u32 ring,
76 struct amdgpu_ring **out_ring)
77{
78 /* Right now all IPs have only one instance - multiple rings. */
79 if (ip_instance != 0) {
80 DRM_ERROR("invalid ip instance: %d\n", ip_instance);
81 return -EINVAL;
82 }
83
84 switch (ip_type) {
85 default:
86 DRM_ERROR("unknown ip type: %d\n", ip_type);
87 return -EINVAL;
88 case AMDGPU_HW_IP_GFX:
89 if (ring < adev->gfx.num_gfx_rings) {
90 *out_ring = &adev->gfx.gfx_ring[ring];
91 } else {
92 DRM_ERROR("only %d gfx rings are supported now\n",
93 adev->gfx.num_gfx_rings);
94 return -EINVAL;
95 }
96 break;
97 case AMDGPU_HW_IP_COMPUTE:
98 if (ring < adev->gfx.num_compute_rings) {
99 *out_ring = &adev->gfx.compute_ring[ring];
100 } else {
101 DRM_ERROR("only %d compute rings are supported now\n",
102 adev->gfx.num_compute_rings);
103 return -EINVAL;
104 }
105 break;
106 case AMDGPU_HW_IP_DMA:
c113ea1c
AD
107 if (ring < adev->sdma.num_instances) {
108 *out_ring = &adev->sdma.instance[ring].ring;
d38ceaf9 109 } else {
c113ea1c
AD
110 DRM_ERROR("only %d SDMA rings are supported\n",
111 adev->sdma.num_instances);
d38ceaf9
AD
112 return -EINVAL;
113 }
114 break;
115 case AMDGPU_HW_IP_UVD:
116 *out_ring = &adev->uvd.ring;
117 break;
118 case AMDGPU_HW_IP_VCE:
119 if (ring < 2){
120 *out_ring = &adev->vce.ring[ring];
121 } else {
122 DRM_ERROR("only two VCE rings are supported\n");
123 return -EINVAL;
124 }
125 break;
126 }
127 return 0;
128}
129
049fc527
CZ
130struct amdgpu_cs_parser *amdgpu_cs_parser_create(struct amdgpu_device *adev,
131 struct drm_file *filp,
132 struct amdgpu_ctx *ctx,
133 struct amdgpu_ib *ibs,
134 uint32_t num_ibs)
135{
136 struct amdgpu_cs_parser *parser;
137 int i;
138
139 parser = kzalloc(sizeof(struct amdgpu_cs_parser), GFP_KERNEL);
140 if (!parser)
141 return NULL;
142
143 parser->adev = adev;
144 parser->filp = filp;
145 parser->ctx = ctx;
146 parser->ibs = ibs;
147 parser->num_ibs = num_ibs;
049fc527
CZ
148 for (i = 0; i < num_ibs; i++)
149 ibs[i].ctx = ctx;
150
151 return parser;
152}
153
d38ceaf9
AD
154int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
155{
156 union drm_amdgpu_cs *cs = data;
157 uint64_t *chunk_array_user;
1d263474 158 uint64_t *chunk_array;
d38ceaf9 159 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
54313503
DC
160 unsigned size;
161 int i;
1d263474 162 int ret;
d38ceaf9 163
1d263474
DC
164 if (cs->in.num_chunks == 0)
165 return 0;
166
167 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
168 if (!chunk_array)
169 return -ENOMEM;
d38ceaf9 170
3cb485f3
CK
171 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
172 if (!p->ctx) {
1d263474
DC
173 ret = -EINVAL;
174 goto free_chunk;
3cb485f3 175 }
1d263474 176
a3348bb8 177 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
d38ceaf9
AD
178
179 /* get chunks */
180 INIT_LIST_HEAD(&p->validated);
028423b0 181 chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
d38ceaf9
AD
182 if (copy_from_user(chunk_array, chunk_array_user,
183 sizeof(uint64_t)*cs->in.num_chunks)) {
1d263474
DC
184 ret = -EFAULT;
185 goto put_bo_list;
d38ceaf9
AD
186 }
187
188 p->nchunks = cs->in.num_chunks;
e60b344f 189 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
d38ceaf9 190 GFP_KERNEL);
1d263474
DC
191 if (!p->chunks) {
192 ret = -ENOMEM;
193 goto put_bo_list;
d38ceaf9
AD
194 }
195
196 for (i = 0; i < p->nchunks; i++) {
197 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
198 struct drm_amdgpu_cs_chunk user_chunk;
199 uint32_t __user *cdata;
200
028423b0 201 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
d38ceaf9
AD
202 if (copy_from_user(&user_chunk, chunk_ptr,
203 sizeof(struct drm_amdgpu_cs_chunk))) {
1d263474
DC
204 ret = -EFAULT;
205 i--;
206 goto free_partial_kdata;
d38ceaf9
AD
207 }
208 p->chunks[i].chunk_id = user_chunk.chunk_id;
209 p->chunks[i].length_dw = user_chunk.length_dw;
d38ceaf9
AD
210
211 size = p->chunks[i].length_dw;
028423b0 212 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
d38ceaf9
AD
213 p->chunks[i].user_ptr = cdata;
214
215 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
216 if (p->chunks[i].kdata == NULL) {
1d263474
DC
217 ret = -ENOMEM;
218 i--;
219 goto free_partial_kdata;
d38ceaf9
AD
220 }
221 size *= sizeof(uint32_t);
222 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
1d263474
DC
223 ret = -EFAULT;
224 goto free_partial_kdata;
d38ceaf9
AD
225 }
226
9a5e8fb1
CK
227 switch (p->chunks[i].chunk_id) {
228 case AMDGPU_CHUNK_ID_IB:
229 p->num_ibs++;
230 break;
231
232 case AMDGPU_CHUNK_ID_FENCE:
d38ceaf9
AD
233 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
234 if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
235 uint32_t handle;
236 struct drm_gem_object *gobj;
237 struct drm_amdgpu_cs_chunk_fence *fence_data;
238
239 fence_data = (void *)p->chunks[i].kdata;
240 handle = fence_data->handle;
241 gobj = drm_gem_object_lookup(p->adev->ddev,
242 p->filp, handle);
243 if (gobj == NULL) {
1d263474
DC
244 ret = -EINVAL;
245 goto free_partial_kdata;
d38ceaf9
AD
246 }
247
248 p->uf.bo = gem_to_amdgpu_bo(gobj);
249 p->uf.offset = fence_data->offset;
250 } else {
1d263474
DC
251 ret = -EINVAL;
252 goto free_partial_kdata;
d38ceaf9 253 }
9a5e8fb1
CK
254 break;
255
2b48d323
CK
256 case AMDGPU_CHUNK_ID_DEPENDENCIES:
257 break;
258
9a5e8fb1 259 default:
1d263474
DC
260 ret = -EINVAL;
261 goto free_partial_kdata;
d38ceaf9
AD
262 }
263 }
264
e60b344f 265
b203dd95 266 p->ibs = kcalloc(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
1d263474
DC
267 if (!p->ibs) {
268 ret = -ENOMEM;
269 goto free_all_kdata;
270 }
d38ceaf9 271
d38ceaf9 272 kfree(chunk_array);
1d263474
DC
273 return 0;
274
275free_all_kdata:
276 i = p->nchunks - 1;
277free_partial_kdata:
278 for (; i >= 0; i--)
279 drm_free_large(p->chunks[i].kdata);
280 kfree(p->chunks);
281put_bo_list:
282 if (p->bo_list)
283 amdgpu_bo_list_put(p->bo_list);
284 amdgpu_ctx_put(p->ctx);
285free_chunk:
286 kfree(chunk_array);
287
288 return ret;
d38ceaf9
AD
289}
290
291/* Returns how many bytes TTM can move per IB.
292 */
293static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
294{
295 u64 real_vram_size = adev->mc.real_vram_size;
296 u64 vram_usage = atomic64_read(&adev->vram_usage);
297
298 /* This function is based on the current VRAM usage.
299 *
300 * - If all of VRAM is free, allow relocating the number of bytes that
301 * is equal to 1/4 of the size of VRAM for this IB.
302
303 * - If more than one half of VRAM is occupied, only allow relocating
304 * 1 MB of data for this IB.
305 *
306 * - From 0 to one half of used VRAM, the threshold decreases
307 * linearly.
308 * __________________
309 * 1/4 of -|\ |
310 * VRAM | \ |
311 * | \ |
312 * | \ |
313 * | \ |
314 * | \ |
315 * | \ |
316 * | \________|1 MB
317 * |----------------|
318 * VRAM 0 % 100 %
319 * used used
320 *
321 * Note: It's a threshold, not a limit. The threshold must be crossed
322 * for buffer relocations to stop, so any buffer of an arbitrary size
323 * can be moved as long as the threshold isn't crossed before
324 * the relocation takes place. We don't want to disable buffer
325 * relocations completely.
326 *
327 * The idea is that buffers should be placed in VRAM at creation time
328 * and TTM should only do a minimum number of relocations during
329 * command submission. In practice, you need to submit at least
330 * a dozen IBs to move all buffers to VRAM if they are in GTT.
331 *
332 * Also, things can get pretty crazy under memory pressure and actual
333 * VRAM usage can change a lot, so playing safe even at 50% does
334 * consistently increase performance.
335 */
336
337 u64 half_vram = real_vram_size >> 1;
338 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
339 u64 bytes_moved_threshold = half_free_vram >> 1;
340 return max(bytes_moved_threshold, 1024*1024ull);
341}
342
a5b75058
CK
343int amdgpu_cs_list_validate(struct amdgpu_device *adev,
344 struct amdgpu_vm *vm,
345 struct list_head *validated)
d38ceaf9 346{
d38ceaf9 347 struct amdgpu_bo_list_entry *lobj;
d38ceaf9
AD
348 struct amdgpu_bo *bo;
349 u64 bytes_moved = 0, initial_bytes_moved;
350 u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
351 int r;
352
a5b75058 353 list_for_each_entry(lobj, validated, tv.head) {
d38ceaf9
AD
354 bo = lobj->robj;
355 if (!bo->pin_count) {
356 u32 domain = lobj->prefered_domains;
357 u32 current_domain =
358 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
359
360 /* Check if this buffer will be moved and don't move it
361 * if we have moved too many buffers for this IB already.
362 *
363 * Note that this allows moving at least one buffer of
364 * any size, because it doesn't take the current "bo"
365 * into account. We don't want to disallow buffer moves
366 * completely.
367 */
270e869d 368 if ((lobj->allowed_domains & current_domain) != 0 &&
d38ceaf9
AD
369 (domain & current_domain) == 0 && /* will be moved */
370 bytes_moved > bytes_moved_threshold) {
371 /* don't move it */
372 domain = current_domain;
373 }
374
375 retry:
376 amdgpu_ttm_placement_from_domain(bo, domain);
377 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
378 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
379 bytes_moved += atomic64_read(&adev->num_bytes_moved) -
380 initial_bytes_moved;
381
382 if (unlikely(r)) {
383 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
384 domain = lobj->allowed_domains;
385 goto retry;
386 }
d38ceaf9
AD
387 return r;
388 }
389 }
390 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
391 }
392 return 0;
393}
394
395static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
396{
397 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
398 struct amdgpu_cs_buckets buckets;
a5b75058 399 struct list_head duplicates;
840d5144 400 bool need_mmap_lock = false;
d38ceaf9
AD
401 int i, r;
402
840d5144 403 if (p->bo_list) {
404 need_mmap_lock = p->bo_list->has_userptr;
405 amdgpu_cs_buckets_init(&buckets);
406 for (i = 0; i < p->bo_list->num_entries; i++)
407 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
408 p->bo_list->array[i].priority);
d38ceaf9 409
840d5144 410 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
411 }
d38ceaf9 412
d38ceaf9
AD
413 p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
414 &p->validated);
415
d38ceaf9
AD
416 if (need_mmap_lock)
417 down_read(&current->mm->mmap_sem);
418
a5b75058
CK
419 INIT_LIST_HEAD(&duplicates);
420 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
421 if (unlikely(r != 0))
422 goto error_reserve;
423
424 r = amdgpu_cs_list_validate(p->adev, &fpriv->vm, &p->validated);
425 if (r)
426 goto error_validate;
427
428 r = amdgpu_cs_list_validate(p->adev, &fpriv->vm, &duplicates);
429
430error_validate:
431 if (r)
432 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
d38ceaf9 433
a5b75058 434error_reserve:
d38ceaf9
AD
435 if (need_mmap_lock)
436 up_read(&current->mm->mmap_sem);
437
438 return r;
439}
440
441static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
442{
443 struct amdgpu_bo_list_entry *e;
444 int r;
445
446 list_for_each_entry(e, &p->validated, tv.head) {
447 struct reservation_object *resv = e->robj->tbo.resv;
448 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
449
450 if (r)
451 return r;
452 }
453 return 0;
454}
455
456static int cmp_size_smaller_first(void *priv, struct list_head *a,
457 struct list_head *b)
458{
459 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
460 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
461
462 /* Sort A before B if A is smaller. */
463 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
464}
465
049fc527
CZ
466static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser *parser, int error, bool backoff)
467{
d38ceaf9
AD
468 if (!error) {
469 /* Sort the buffer list from the smallest to largest buffer,
470 * which affects the order of buffers in the LRU list.
471 * This assures that the smallest buffers are added first
472 * to the LRU list, so they are likely to be later evicted
473 * first, instead of large buffers whose eviction is more
474 * expensive.
475 *
476 * This slightly lowers the number of bytes moved by TTM
477 * per frame under memory pressure.
478 */
479 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
480
481 ttm_eu_fence_buffer_objects(&parser->ticket,
482 &parser->validated,
483 &parser->ibs[parser->num_ibs-1].fence->base);
484 } else if (backoff) {
485 ttm_eu_backoff_reservation(&parser->ticket,
486 &parser->validated);
487 }
049fc527 488}
d38ceaf9 489
049fc527
CZ
490static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser *parser)
491{
492 unsigned i;
3cb485f3
CK
493 if (parser->ctx)
494 amdgpu_ctx_put(parser->ctx);
a3348bb8
CZ
495 if (parser->bo_list)
496 amdgpu_bo_list_put(parser->bo_list);
497
d38ceaf9
AD
498 drm_free_large(parser->vm_bos);
499 for (i = 0; i < parser->nchunks; i++)
500 drm_free_large(parser->chunks[i].kdata);
501 kfree(parser->chunks);
e4a58a28
CK
502 if (parser->ibs)
503 for (i = 0; i < parser->num_ibs; i++)
504 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
505 kfree(parser->ibs);
506 if (parser->uf.bo)
507 drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
bb977d37 508 kfree(parser);
d38ceaf9
AD
509}
510
351dba73
CK
511/**
512 * cs_parser_fini() - clean parser states
513 * @parser: parser structure holding parsing context.
514 * @error: error number
515 *
516 * If error is set than unvalidate buffer, otherwise just free memory
517 * used by parsing context.
518 **/
519static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
520{
521 amdgpu_cs_parser_fini_early(parser, error, backoff);
522 amdgpu_cs_parser_fini_late(parser);
523}
524
d38ceaf9
AD
525static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
526 struct amdgpu_vm *vm)
527{
528 struct amdgpu_device *adev = p->adev;
529 struct amdgpu_bo_va *bo_va;
530 struct amdgpu_bo *bo;
531 int i, r;
532
533 r = amdgpu_vm_update_page_directory(adev, vm);
534 if (r)
535 return r;
536
05906dec
BN
537 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, vm->page_directory_fence);
538 if (r)
539 return r;
540
d38ceaf9
AD
541 r = amdgpu_vm_clear_freed(adev, vm);
542 if (r)
543 return r;
544
545 if (p->bo_list) {
546 for (i = 0; i < p->bo_list->num_entries; i++) {
91e1a520
CK
547 struct fence *f;
548
d38ceaf9
AD
549 /* ignore duplicates */
550 bo = p->bo_list->array[i].robj;
551 if (!bo)
552 continue;
553
554 bo_va = p->bo_list->array[i].bo_va;
555 if (bo_va == NULL)
556 continue;
557
558 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
559 if (r)
560 return r;
561
bb1e38a4 562 f = bo_va->last_pt_update;
91e1a520
CK
563 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
564 if (r)
565 return r;
d38ceaf9 566 }
b495bd3a
CK
567
568 }
569
570 r = amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
571
572 if (amdgpu_vm_debug && p->bo_list) {
573 /* Invalidate all BOs to test for userspace bugs */
574 for (i = 0; i < p->bo_list->num_entries; i++) {
575 /* ignore duplicates */
576 bo = p->bo_list->array[i].robj;
577 if (!bo)
578 continue;
579
580 amdgpu_vm_bo_invalidate(adev, bo);
581 }
d38ceaf9
AD
582 }
583
b495bd3a 584 return r;
d38ceaf9
AD
585}
586
587static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
588 struct amdgpu_cs_parser *parser)
589{
590 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
591 struct amdgpu_vm *vm = &fpriv->vm;
592 struct amdgpu_ring *ring;
593 int i, r;
594
595 if (parser->num_ibs == 0)
596 return 0;
597
598 /* Only for UVD/VCE VM emulation */
599 for (i = 0; i < parser->num_ibs; i++) {
600 ring = parser->ibs[i].ring;
601 if (ring->funcs->parse_cs) {
602 r = amdgpu_ring_parse_cs(ring, parser, i);
603 if (r)
604 return r;
605 }
606 }
607
d38ceaf9
AD
608 r = amdgpu_bo_vm_update_pte(parser, vm);
609 if (r) {
610 goto out;
611 }
612 amdgpu_cs_sync_rings(parser);
049fc527
CZ
613 if (!amdgpu_enable_scheduler)
614 r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
615 parser->filp);
d38ceaf9
AD
616
617out:
d38ceaf9
AD
618 return r;
619}
620
621static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
622{
623 if (r == -EDEADLK) {
624 r = amdgpu_gpu_reset(adev);
625 if (!r)
626 r = -EAGAIN;
627 }
628 return r;
629}
630
631static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
632 struct amdgpu_cs_parser *parser)
633{
634 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
635 struct amdgpu_vm *vm = &fpriv->vm;
636 int i, j;
637 int r;
638
639 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
640 struct amdgpu_cs_chunk *chunk;
641 struct amdgpu_ib *ib;
642 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
d38ceaf9 643 struct amdgpu_ring *ring;
d38ceaf9
AD
644
645 chunk = &parser->chunks[i];
646 ib = &parser->ibs[j];
647 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
648
649 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
650 continue;
651
d38ceaf9
AD
652 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
653 chunk_ib->ip_instance, chunk_ib->ring,
654 &ring);
3ccec53c 655 if (r)
d38ceaf9 656 return r;
d38ceaf9
AD
657
658 if (ring->funcs->parse_cs) {
4802ce11 659 struct amdgpu_bo_va_mapping *m;
3ccec53c 660 struct amdgpu_bo *aobj = NULL;
4802ce11
CK
661 uint64_t offset;
662 uint8_t *kptr;
3ccec53c 663
4802ce11
CK
664 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
665 &aobj);
3ccec53c
MO
666 if (!aobj) {
667 DRM_ERROR("IB va_start is invalid\n");
668 return -EINVAL;
d38ceaf9
AD
669 }
670
4802ce11
CK
671 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
672 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
673 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
674 return -EINVAL;
675 }
676
3ccec53c 677 /* the IB should be reserved at this point */
4802ce11 678 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
d38ceaf9 679 if (r) {
d38ceaf9
AD
680 return r;
681 }
682
4802ce11
CK
683 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
684 kptr += chunk_ib->va_start - offset;
685
d38ceaf9
AD
686 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
687 if (r) {
688 DRM_ERROR("Failed to get ib !\n");
d38ceaf9
AD
689 return r;
690 }
691
692 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
693 amdgpu_bo_kunmap(aobj);
d38ceaf9
AD
694 } else {
695 r = amdgpu_ib_get(ring, vm, 0, ib);
696 if (r) {
697 DRM_ERROR("Failed to get ib !\n");
d38ceaf9
AD
698 return r;
699 }
700
701 ib->gpu_addr = chunk_ib->va_start;
702 }
d38ceaf9 703
3ccec53c 704 ib->length_dw = chunk_ib->ib_bytes / 4;
de807f81 705 ib->flags = chunk_ib->flags;
3cb485f3 706 ib->ctx = parser->ctx;
d38ceaf9
AD
707 j++;
708 }
709
710 if (!parser->num_ibs)
711 return 0;
712
713 /* add GDS resources to first IB */
714 if (parser->bo_list) {
715 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
716 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
717 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
718 struct amdgpu_ib *ib = &parser->ibs[0];
719
720 if (gds) {
721 ib->gds_base = amdgpu_bo_gpu_offset(gds);
722 ib->gds_size = amdgpu_bo_size(gds);
723 }
724 if (gws) {
725 ib->gws_base = amdgpu_bo_gpu_offset(gws);
726 ib->gws_size = amdgpu_bo_size(gws);
727 }
728 if (oa) {
729 ib->oa_base = amdgpu_bo_gpu_offset(oa);
730 ib->oa_size = amdgpu_bo_size(oa);
731 }
732 }
d38ceaf9
AD
733 /* wrap the last IB with user fence */
734 if (parser->uf.bo) {
735 struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
736
737 /* UVD & VCE fw doesn't support user fences */
738 if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
739 ib->ring->type == AMDGPU_RING_TYPE_VCE)
740 return -EINVAL;
741
742 ib->user = &parser->uf;
743 }
744
745 return 0;
746}
747
2b48d323
CK
748static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
749 struct amdgpu_cs_parser *p)
750{
76a1ea61 751 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
2b48d323
CK
752 struct amdgpu_ib *ib;
753 int i, j, r;
754
755 if (!p->num_ibs)
756 return 0;
757
758 /* Add dependencies to first IB */
759 ib = &p->ibs[0];
760 for (i = 0; i < p->nchunks; ++i) {
761 struct drm_amdgpu_cs_chunk_dep *deps;
762 struct amdgpu_cs_chunk *chunk;
763 unsigned num_deps;
764
765 chunk = &p->chunks[i];
766
767 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
768 continue;
769
770 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
771 num_deps = chunk->length_dw * 4 /
772 sizeof(struct drm_amdgpu_cs_chunk_dep);
773
774 for (j = 0; j < num_deps; ++j) {
2b48d323 775 struct amdgpu_ring *ring;
76a1ea61 776 struct amdgpu_ctx *ctx;
21c16bf6 777 struct fence *fence;
2b48d323
CK
778
779 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
780 deps[j].ip_instance,
781 deps[j].ring, &ring);
782 if (r)
783 return r;
784
76a1ea61
CK
785 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
786 if (ctx == NULL)
787 return -EINVAL;
788
21c16bf6
CK
789 fence = amdgpu_ctx_get_fence(ctx, ring,
790 deps[j].handle);
791 if (IS_ERR(fence)) {
792 r = PTR_ERR(fence);
76a1ea61 793 amdgpu_ctx_put(ctx);
2b48d323 794 return r;
91e1a520 795
21c16bf6
CK
796 } else if (fence) {
797 r = amdgpu_sync_fence(adev, &ib->sync, fence);
798 fence_put(fence);
799 amdgpu_ctx_put(ctx);
800 if (r)
801 return r;
802 }
2b48d323
CK
803 }
804 }
805
806 return 0;
807}
808
4c7eb91c 809static int amdgpu_cs_free_job(struct amdgpu_job *job)
bb977d37
CZ
810{
811 int i;
4c7eb91c
JZ
812 if (job->ibs)
813 for (i = 0; i < job->num_ibs; i++)
814 amdgpu_ib_free(job->adev, &job->ibs[i]);
815 kfree(job->ibs);
816 if (job->uf.bo)
817 drm_gem_object_unreference_unlocked(&job->uf.bo->gem_base);
bb977d37
CZ
818 return 0;
819}
820
049fc527
CZ
821int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
822{
823 struct amdgpu_device *adev = dev->dev_private;
824 union drm_amdgpu_cs *cs = data;
f48b2659
CZ
825 struct amdgpu_fpriv *fpriv = filp->driver_priv;
826 struct amdgpu_vm *vm = &fpriv->vm;
049fc527 827 struct amdgpu_cs_parser *parser;
26a6980c
CK
828 bool reserved_buffers = false;
829 int i, r;
049fc527 830
0c418f10 831 if (!adev->accel_working)
049fc527 832 return -EBUSY;
2b48d323 833
049fc527
CZ
834 parser = amdgpu_cs_parser_create(adev, filp, NULL, NULL, 0);
835 if (!parser)
836 return -ENOMEM;
837 r = amdgpu_cs_parser_init(parser, data);
d38ceaf9 838 if (r) {
049fc527 839 DRM_ERROR("Failed to initialize parser !\n");
0c418f10 840 amdgpu_cs_parser_fini(parser, r, false);
d38ceaf9
AD
841 r = amdgpu_cs_handle_lockup(adev, r);
842 return r;
843 }
f48b2659 844 mutex_lock(&vm->mutex);
26a6980c
CK
845 r = amdgpu_cs_parser_relocs(parser);
846 if (r == -ENOMEM)
847 DRM_ERROR("Not enough memory for command submission!\n");
848 else if (r && r != -ERESTARTSYS)
849 DRM_ERROR("Failed to process the buffer list %d!\n", r);
850 else if (!r) {
851 reserved_buffers = true;
852 r = amdgpu_cs_ib_fill(adev, parser);
853 }
854
855 if (!r) {
856 r = amdgpu_cs_dependencies(adev, parser);
857 if (r)
858 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
859 }
860
861 if (r)
862 goto out;
863
864 for (i = 0; i < parser->num_ibs; i++)
865 trace_amdgpu_cs(parser, i);
866
867 r = amdgpu_cs_ib_vm_chunk(adev, parser);
4fe63117
CZ
868 if (r)
869 goto out;
870
049fc527 871 if (amdgpu_enable_scheduler && parser->num_ibs) {
bb977d37 872 struct amdgpu_job *job;
3c4adead 873 struct amdgpu_ring * ring = parser->ibs->ring;
bb977d37 874 job = kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
4cfdcd9c
DC
875 if (!job) {
876 r = -ENOMEM;
877 goto out;
878 }
4f839a24 879 job->base.sched = &ring->sched;
bb977d37
CZ
880 job->base.s_entity = &parser->ctx->rings[ring->idx].entity;
881 job->adev = parser->adev;
882 job->ibs = parser->ibs;
883 job->num_ibs = parser->num_ibs;
84f76ea6 884 job->base.owner = parser->filp;
bb977d37
CZ
885 mutex_init(&job->job_lock);
886 if (job->ibs[job->num_ibs - 1].user) {
e4a58a28 887 job->uf = parser->uf;
bb977d37 888 job->ibs[job->num_ibs - 1].user = &job->uf;
e4a58a28 889 parser->uf.bo = NULL;
bb977d37
CZ
890 }
891
e4a58a28
CK
892 parser->ibs = NULL;
893 parser->num_ibs = 0;
894
bb977d37
CZ
895 job->free_job = amdgpu_cs_free_job;
896 mutex_lock(&job->job_lock);
a6db8a33 897 r = amd_sched_entity_push_job(&job->base);
f556cb0c 898 if (r) {
bb977d37
CZ
899 mutex_unlock(&job->job_lock);
900 amdgpu_cs_free_job(job);
901 kfree(job);
f556cb0c
CZ
902 goto out;
903 }
ce882e6d 904 cs->out.handle =
3a185a33 905 amdgpu_ctx_add_fence(parser->ctx, ring,
ce882e6d 906 &job->base.s_fence->base);
e4a58a28 907 job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
eb98d1c5 908
c3b95d4f
CZ
909 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
910 ttm_eu_fence_buffer_objects(&parser->ticket,
911 &parser->validated,
bb977d37 912 &job->base.s_fence->base);
c3b95d4f 913
bb977d37
CZ
914 mutex_unlock(&job->job_lock);
915 amdgpu_cs_parser_fini_late(parser);
f48b2659 916 mutex_unlock(&vm->mutex);
049fc527 917 return 0;
d38ceaf9
AD
918 }
919
049fc527 920 cs->out.handle = parser->ibs[parser->num_ibs - 1].sequence;
d38ceaf9 921out:
26a6980c 922 amdgpu_cs_parser_fini(parser, r, reserved_buffers);
f48b2659 923 mutex_unlock(&vm->mutex);
d38ceaf9
AD
924 r = amdgpu_cs_handle_lockup(adev, r);
925 return r;
926}
927
928/**
929 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
930 *
931 * @dev: drm device
932 * @data: data from userspace
933 * @filp: file private
934 *
935 * Wait for the command submission identified by handle to finish.
936 */
937int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
938 struct drm_file *filp)
939{
940 union drm_amdgpu_wait_cs *wait = data;
941 struct amdgpu_device *adev = dev->dev_private;
d38ceaf9 942 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
03507c4f 943 struct amdgpu_ring *ring = NULL;
66b3cf2a 944 struct amdgpu_ctx *ctx;
21c16bf6 945 struct fence *fence;
d38ceaf9
AD
946 long r;
947
21c16bf6
CK
948 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
949 wait->in.ring, &ring);
950 if (r)
951 return r;
952
66b3cf2a
JZ
953 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
954 if (ctx == NULL)
955 return -EINVAL;
d38ceaf9 956
4b559c90
CZ
957 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
958 if (IS_ERR(fence))
959 r = PTR_ERR(fence);
960 else if (fence) {
961 r = fence_wait_timeout(fence, true, timeout);
962 fence_put(fence);
963 } else
964 r = 1;
049fc527 965
66b3cf2a 966 amdgpu_ctx_put(ctx);
d38ceaf9
AD
967 if (r < 0)
968 return r;
969
970 memset(wait, 0, sizeof(*wait));
971 wait->out.status = (r == 0);
972
973 return 0;
974}
975
976/**
977 * amdgpu_cs_find_bo_va - find bo_va for VM address
978 *
979 * @parser: command submission parser context
980 * @addr: VM address
981 * @bo: resulting BO of the mapping found
982 *
983 * Search the buffer objects in the command submission context for a certain
984 * virtual memory address. Returns allocation structure when found, NULL
985 * otherwise.
986 */
987struct amdgpu_bo_va_mapping *
988amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
989 uint64_t addr, struct amdgpu_bo **bo)
990{
991 struct amdgpu_bo_list_entry *reloc;
992 struct amdgpu_bo_va_mapping *mapping;
993
994 addr /= AMDGPU_GPU_PAGE_SIZE;
995
996 list_for_each_entry(reloc, &parser->validated, tv.head) {
997 if (!reloc->bo_va)
998 continue;
999
7fc11959
CK
1000 list_for_each_entry(mapping, &reloc->bo_va->valids, list) {
1001 if (mapping->it.start > addr ||
1002 addr > mapping->it.last)
1003 continue;
1004
1005 *bo = reloc->bo_va->bo;
1006 return mapping;
1007 }
1008
1009 list_for_each_entry(mapping, &reloc->bo_va->invalids, list) {
d38ceaf9
AD
1010 if (mapping->it.start > addr ||
1011 addr > mapping->it.last)
1012 continue;
1013
1014 *bo = reloc->bo_va->bo;
1015 return mapping;
1016 }
1017 }
1018
1019 return NULL;
1020}