blob: cef8360698bef0ffdfed426202c7d051046d679f [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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:
107 if (ring < 2) {
108 *out_ring = &adev->sdma[ring].ring;
109 } else {
110 DRM_ERROR("only two SDMA rings are supported\n");
111 return -EINVAL;
112 }
113 break;
114 case AMDGPU_HW_IP_UVD:
115 *out_ring = &adev->uvd.ring;
116 break;
117 case AMDGPU_HW_IP_VCE:
118 if (ring < 2){
119 *out_ring = &adev->vce.ring[ring];
120 } else {
121 DRM_ERROR("only two VCE rings are supported\n");
122 return -EINVAL;
123 }
124 break;
125 }
126 return 0;
127}
128
129int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
130{
131 union drm_amdgpu_cs *cs = data;
132 uint64_t *chunk_array_user;
133 uint64_t *chunk_array = NULL;
134 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
135 unsigned size, i;
136 int r = 0;
137
138 if (!cs->in.num_chunks)
139 goto out;
140
Christian König3cb485f2015-05-11 15:34:59 +0200141 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
142 if (!p->ctx) {
143 r = -EINVAL;
144 goto out;
145 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
147
148 /* get chunks */
149 INIT_LIST_HEAD(&p->validated);
150 chunk_array = kcalloc(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
151 if (chunk_array == NULL) {
152 r = -ENOMEM;
153 goto out;
154 }
155
156 chunk_array_user = (uint64_t *)(unsigned long)(cs->in.chunks);
157 if (copy_from_user(chunk_array, chunk_array_user,
158 sizeof(uint64_t)*cs->in.num_chunks)) {
159 r = -EFAULT;
160 goto out;
161 }
162
163 p->nchunks = cs->in.num_chunks;
164 p->chunks = kcalloc(p->nchunks, sizeof(struct amdgpu_cs_chunk),
165 GFP_KERNEL);
166 if (p->chunks == NULL) {
167 r = -ENOMEM;
168 goto out;
169 }
170
171 for (i = 0; i < p->nchunks; i++) {
172 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
173 struct drm_amdgpu_cs_chunk user_chunk;
174 uint32_t __user *cdata;
175
176 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
177 if (copy_from_user(&user_chunk, chunk_ptr,
178 sizeof(struct drm_amdgpu_cs_chunk))) {
179 r = -EFAULT;
180 goto out;
181 }
182 p->chunks[i].chunk_id = user_chunk.chunk_id;
183 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184
185 size = p->chunks[i].length_dw;
186 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
187 p->chunks[i].user_ptr = cdata;
188
189 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
190 if (p->chunks[i].kdata == NULL) {
191 r = -ENOMEM;
192 goto out;
193 }
194 size *= sizeof(uint32_t);
195 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
196 r = -EFAULT;
197 goto out;
198 }
199
Christian König9a5e8fb2015-06-23 17:07:03 +0200200 switch (p->chunks[i].chunk_id) {
201 case AMDGPU_CHUNK_ID_IB:
202 p->num_ibs++;
203 break;
204
205 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
207 if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
208 uint32_t handle;
209 struct drm_gem_object *gobj;
210 struct drm_amdgpu_cs_chunk_fence *fence_data;
211
212 fence_data = (void *)p->chunks[i].kdata;
213 handle = fence_data->handle;
214 gobj = drm_gem_object_lookup(p->adev->ddev,
215 p->filp, handle);
216 if (gobj == NULL) {
217 r = -EINVAL;
218 goto out;
219 }
220
221 p->uf.bo = gem_to_amdgpu_bo(gobj);
222 p->uf.offset = fence_data->offset;
223 } else {
224 r = -EINVAL;
225 goto out;
226 }
Christian König9a5e8fb2015-06-23 17:07:03 +0200227 break;
228
Christian König2b48d322015-06-19 17:31:29 +0200229 case AMDGPU_CHUNK_ID_DEPENDENCIES:
230 break;
231
Christian König9a5e8fb2015-06-23 17:07:03 +0200232 default:
233 r = -EINVAL;
234 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235 }
236 }
237
238 p->ibs = kcalloc(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
239 if (!p->ibs) {
240 r = -ENOMEM;
241 goto out;
242 }
243
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400244out:
245 kfree(chunk_array);
246 return r;
247}
248
249/* Returns how many bytes TTM can move per IB.
250 */
251static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
252{
253 u64 real_vram_size = adev->mc.real_vram_size;
254 u64 vram_usage = atomic64_read(&adev->vram_usage);
255
256 /* This function is based on the current VRAM usage.
257 *
258 * - If all of VRAM is free, allow relocating the number of bytes that
259 * is equal to 1/4 of the size of VRAM for this IB.
260
261 * - If more than one half of VRAM is occupied, only allow relocating
262 * 1 MB of data for this IB.
263 *
264 * - From 0 to one half of used VRAM, the threshold decreases
265 * linearly.
266 * __________________
267 * 1/4 of -|\ |
268 * VRAM | \ |
269 * | \ |
270 * | \ |
271 * | \ |
272 * | \ |
273 * | \ |
274 * | \________|1 MB
275 * |----------------|
276 * VRAM 0 % 100 %
277 * used used
278 *
279 * Note: It's a threshold, not a limit. The threshold must be crossed
280 * for buffer relocations to stop, so any buffer of an arbitrary size
281 * can be moved as long as the threshold isn't crossed before
282 * the relocation takes place. We don't want to disable buffer
283 * relocations completely.
284 *
285 * The idea is that buffers should be placed in VRAM at creation time
286 * and TTM should only do a minimum number of relocations during
287 * command submission. In practice, you need to submit at least
288 * a dozen IBs to move all buffers to VRAM if they are in GTT.
289 *
290 * Also, things can get pretty crazy under memory pressure and actual
291 * VRAM usage can change a lot, so playing safe even at 50% does
292 * consistently increase performance.
293 */
294
295 u64 half_vram = real_vram_size >> 1;
296 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
297 u64 bytes_moved_threshold = half_free_vram >> 1;
298 return max(bytes_moved_threshold, 1024*1024ull);
299}
300
301int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p)
302{
303 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
304 struct amdgpu_vm *vm = &fpriv->vm;
305 struct amdgpu_device *adev = p->adev;
306 struct amdgpu_bo_list_entry *lobj;
307 struct list_head duplicates;
308 struct amdgpu_bo *bo;
309 u64 bytes_moved = 0, initial_bytes_moved;
310 u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
311 int r;
312
313 INIT_LIST_HEAD(&duplicates);
314 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
315 if (unlikely(r != 0)) {
316 return r;
317 }
318
319 list_for_each_entry(lobj, &p->validated, tv.head) {
320 bo = lobj->robj;
321 if (!bo->pin_count) {
322 u32 domain = lobj->prefered_domains;
323 u32 current_domain =
324 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
325
326 /* Check if this buffer will be moved and don't move it
327 * if we have moved too many buffers for this IB already.
328 *
329 * Note that this allows moving at least one buffer of
330 * any size, because it doesn't take the current "bo"
331 * into account. We don't want to disallow buffer moves
332 * completely.
333 */
334 if (current_domain != AMDGPU_GEM_DOMAIN_CPU &&
335 (domain & current_domain) == 0 && /* will be moved */
336 bytes_moved > bytes_moved_threshold) {
337 /* don't move it */
338 domain = current_domain;
339 }
340
341 retry:
342 amdgpu_ttm_placement_from_domain(bo, domain);
343 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
344 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
345 bytes_moved += atomic64_read(&adev->num_bytes_moved) -
346 initial_bytes_moved;
347
348 if (unlikely(r)) {
349 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
350 domain = lobj->allowed_domains;
351 goto retry;
352 }
353 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
354 return r;
355 }
356 }
357 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
358 }
359 return 0;
360}
361
362static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
363{
364 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
365 struct amdgpu_cs_buckets buckets;
monk.liu840d5142015-04-27 15:19:20 +0800366 bool need_mmap_lock = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367 int i, r;
368
monk.liu840d5142015-04-27 15:19:20 +0800369 if (p->bo_list) {
370 need_mmap_lock = p->bo_list->has_userptr;
371 amdgpu_cs_buckets_init(&buckets);
372 for (i = 0; i < p->bo_list->num_entries; i++)
373 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
374 p->bo_list->array[i].priority);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400375
monk.liu840d5142015-04-27 15:19:20 +0800376 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
377 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379 p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
380 &p->validated);
381
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382 if (need_mmap_lock)
383 down_read(&current->mm->mmap_sem);
384
385 r = amdgpu_cs_list_validate(p);
386
387 if (need_mmap_lock)
388 up_read(&current->mm->mmap_sem);
389
390 return r;
391}
392
393static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
394{
395 struct amdgpu_bo_list_entry *e;
396 int r;
397
398 list_for_each_entry(e, &p->validated, tv.head) {
399 struct reservation_object *resv = e->robj->tbo.resv;
400 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
401
402 if (r)
403 return r;
404 }
405 return 0;
406}
407
408static int cmp_size_smaller_first(void *priv, struct list_head *a,
409 struct list_head *b)
410{
411 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
412 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
413
414 /* Sort A before B if A is smaller. */
415 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
416}
417
418/**
419 * cs_parser_fini() - clean parser states
420 * @parser: parser structure holding parsing context.
421 * @error: error number
422 *
423 * If error is set than unvalidate buffer, otherwise just free memory
424 * used by parsing context.
425 **/
426static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
427{
428 unsigned i;
429
430 if (!error) {
431 /* Sort the buffer list from the smallest to largest buffer,
432 * which affects the order of buffers in the LRU list.
433 * This assures that the smallest buffers are added first
434 * to the LRU list, so they are likely to be later evicted
435 * first, instead of large buffers whose eviction is more
436 * expensive.
437 *
438 * This slightly lowers the number of bytes moved by TTM
439 * per frame under memory pressure.
440 */
441 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
442
443 ttm_eu_fence_buffer_objects(&parser->ticket,
444 &parser->validated,
445 &parser->ibs[parser->num_ibs-1].fence->base);
446 } else if (backoff) {
447 ttm_eu_backoff_reservation(&parser->ticket,
448 &parser->validated);
449 }
450
Christian König3cb485f2015-05-11 15:34:59 +0200451 if (parser->ctx)
452 amdgpu_ctx_put(parser->ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 if (parser->bo_list)
454 amdgpu_bo_list_put(parser->bo_list);
455 drm_free_large(parser->vm_bos);
456 for (i = 0; i < parser->nchunks; i++)
457 drm_free_large(parser->chunks[i].kdata);
458 kfree(parser->chunks);
Christian Königb8682ac2015-06-22 14:54:32 +0200459 if (parser->ibs)
460 for (i = 0; i < parser->num_ibs; i++)
461 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462 kfree(parser->ibs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 if (parser->uf.bo)
464 drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
465}
466
467static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
468 struct amdgpu_vm *vm)
469{
470 struct amdgpu_device *adev = p->adev;
471 struct amdgpu_bo_va *bo_va;
472 struct amdgpu_bo *bo;
473 int i, r;
474
475 r = amdgpu_vm_update_page_directory(adev, vm);
476 if (r)
477 return r;
478
479 r = amdgpu_vm_clear_freed(adev, vm);
480 if (r)
481 return r;
482
483 if (p->bo_list) {
484 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200485 struct fence *f;
486
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400487 /* ignore duplicates */
488 bo = p->bo_list->array[i].robj;
489 if (!bo)
490 continue;
491
492 bo_va = p->bo_list->array[i].bo_va;
493 if (bo_va == NULL)
494 continue;
495
496 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
497 if (r)
498 return r;
499
Christian König91e1a522015-07-06 22:06:40 +0200500 f = &bo_va->last_pt_update->base;
501 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
502 if (r)
503 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 }
505 }
506
monk.liucfe2c972015-05-26 15:01:54 +0800507 return amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508}
509
510static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
511 struct amdgpu_cs_parser *parser)
512{
513 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
514 struct amdgpu_vm *vm = &fpriv->vm;
515 struct amdgpu_ring *ring;
516 int i, r;
517
518 if (parser->num_ibs == 0)
519 return 0;
520
521 /* Only for UVD/VCE VM emulation */
522 for (i = 0; i < parser->num_ibs; i++) {
523 ring = parser->ibs[i].ring;
524 if (ring->funcs->parse_cs) {
525 r = amdgpu_ring_parse_cs(ring, parser, i);
526 if (r)
527 return r;
528 }
529 }
530
531 mutex_lock(&vm->mutex);
532 r = amdgpu_bo_vm_update_pte(parser, vm);
533 if (r) {
534 goto out;
535 }
536 amdgpu_cs_sync_rings(parser);
537
538 r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
539 parser->filp);
540
541out:
542 mutex_unlock(&vm->mutex);
543 return r;
544}
545
546static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
547{
548 if (r == -EDEADLK) {
549 r = amdgpu_gpu_reset(adev);
550 if (!r)
551 r = -EAGAIN;
552 }
553 return r;
554}
555
556static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
557 struct amdgpu_cs_parser *parser)
558{
559 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
560 struct amdgpu_vm *vm = &fpriv->vm;
561 int i, j;
562 int r;
563
564 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
565 struct amdgpu_cs_chunk *chunk;
566 struct amdgpu_ib *ib;
567 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400568 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569
570 chunk = &parser->chunks[i];
571 ib = &parser->ibs[j];
572 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
573
574 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
575 continue;
576
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
578 chunk_ib->ip_instance, chunk_ib->ring,
579 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200580 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582
583 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200584 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200585 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200586 uint64_t offset;
587 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200588
Christian König4802ce12015-06-10 17:20:11 +0200589 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
590 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200591 if (!aobj) {
592 DRM_ERROR("IB va_start is invalid\n");
593 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 }
595
Christian König4802ce12015-06-10 17:20:11 +0200596 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
597 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
598 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
599 return -EINVAL;
600 }
601
Marek Olšák3ccec532015-06-02 17:44:49 +0200602 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200603 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605 return r;
606 }
607
Christian König4802ce12015-06-10 17:20:11 +0200608 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
609 kptr += chunk_ib->va_start - offset;
610
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
612 if (r) {
613 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 return r;
615 }
616
617 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
618 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 } else {
620 r = amdgpu_ib_get(ring, vm, 0, ib);
621 if (r) {
622 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 return r;
624 }
625
626 ib->gpu_addr = chunk_ib->va_start;
627 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628
Marek Olšák3ccec532015-06-02 17:44:49 +0200629 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800630 ib->flags = chunk_ib->flags;
Christian König3cb485f2015-05-11 15:34:59 +0200631 ib->ctx = parser->ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400632 j++;
633 }
634
635 if (!parser->num_ibs)
636 return 0;
637
638 /* add GDS resources to first IB */
639 if (parser->bo_list) {
640 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
641 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
642 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
643 struct amdgpu_ib *ib = &parser->ibs[0];
644
645 if (gds) {
646 ib->gds_base = amdgpu_bo_gpu_offset(gds);
647 ib->gds_size = amdgpu_bo_size(gds);
648 }
649 if (gws) {
650 ib->gws_base = amdgpu_bo_gpu_offset(gws);
651 ib->gws_size = amdgpu_bo_size(gws);
652 }
653 if (oa) {
654 ib->oa_base = amdgpu_bo_gpu_offset(oa);
655 ib->oa_size = amdgpu_bo_size(oa);
656 }
657 }
658
659 /* wrap the last IB with user fence */
660 if (parser->uf.bo) {
661 struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
662
663 /* UVD & VCE fw doesn't support user fences */
664 if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
665 ib->ring->type == AMDGPU_RING_TYPE_VCE)
666 return -EINVAL;
667
668 ib->user = &parser->uf;
669 }
670
671 return 0;
672}
673
Christian König2b48d322015-06-19 17:31:29 +0200674static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
675 struct amdgpu_cs_parser *p)
676{
Christian König76a1ea62015-07-06 19:42:10 +0200677 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200678 struct amdgpu_ib *ib;
679 int i, j, r;
680
681 if (!p->num_ibs)
682 return 0;
683
684 /* Add dependencies to first IB */
685 ib = &p->ibs[0];
686 for (i = 0; i < p->nchunks; ++i) {
687 struct drm_amdgpu_cs_chunk_dep *deps;
688 struct amdgpu_cs_chunk *chunk;
689 unsigned num_deps;
690
691 chunk = &p->chunks[i];
692
693 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
694 continue;
695
696 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
697 num_deps = chunk->length_dw * 4 /
698 sizeof(struct drm_amdgpu_cs_chunk_dep);
699
700 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200701 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200702 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200703 struct fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200704
705 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
706 deps[j].ip_instance,
707 deps[j].ring, &ring);
708 if (r)
709 return r;
710
Christian König76a1ea62015-07-06 19:42:10 +0200711 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
712 if (ctx == NULL)
713 return -EINVAL;
714
Christian König21c16bf2015-07-07 17:24:49 +0200715 fence = amdgpu_ctx_get_fence(ctx, ring,
716 deps[j].handle);
717 if (IS_ERR(fence)) {
718 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +0200719 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +0200720 return r;
Christian König21c16bf2015-07-07 17:24:49 +0200721
722 } else if (fence) {
723 r = amdgpu_sync_fence(adev, &ib->sync, fence);
724 fence_put(fence);
725 amdgpu_ctx_put(ctx);
726 if (r)
727 return r;
Christian König76a1ea62015-07-06 19:42:10 +0200728 }
Christian König2b48d322015-06-19 17:31:29 +0200729 }
730 }
731
732 return 0;
733}
734
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
736{
737 struct amdgpu_device *adev = dev->dev_private;
738 union drm_amdgpu_cs *cs = data;
739 struct amdgpu_cs_parser parser;
740 int r, i;
Marek Olšák3ccec532015-06-02 17:44:49 +0200741 bool reserved_buffers = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742
743 down_read(&adev->exclusive_lock);
744 if (!adev->accel_working) {
745 up_read(&adev->exclusive_lock);
746 return -EBUSY;
747 }
748 /* initialize parser */
749 memset(&parser, 0, sizeof(struct amdgpu_cs_parser));
750 parser.filp = filp;
751 parser.adev = adev;
752 r = amdgpu_cs_parser_init(&parser, data);
753 if (r) {
754 DRM_ERROR("Failed to initialize parser !\n");
755 amdgpu_cs_parser_fini(&parser, r, false);
756 up_read(&adev->exclusive_lock);
757 r = amdgpu_cs_handle_lockup(adev, r);
758 return r;
759 }
760
Marek Olšák3ccec532015-06-02 17:44:49 +0200761 r = amdgpu_cs_parser_relocs(&parser);
762 if (r) {
763 if (r != -ERESTARTSYS) {
764 if (r == -ENOMEM)
765 DRM_ERROR("Not enough memory for command submission!\n");
766 else
767 DRM_ERROR("Failed to process the buffer list %d!\n", r);
768 }
Christian König2b48d322015-06-19 17:31:29 +0200769 }
770
771 if (!r) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200772 reserved_buffers = true;
773 r = amdgpu_cs_ib_fill(adev, &parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400774 }
775
Christian König21c16bf2015-07-07 17:24:49 +0200776 if (!r) {
Christian König2b48d322015-06-19 17:31:29 +0200777 r = amdgpu_cs_dependencies(adev, &parser);
Christian König21c16bf2015-07-07 17:24:49 +0200778 if (r)
779 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
780 }
Christian König2b48d322015-06-19 17:31:29 +0200781
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400782 if (r) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200783 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784 up_read(&adev->exclusive_lock);
785 r = amdgpu_cs_handle_lockup(adev, r);
786 return r;
787 }
788
789 for (i = 0; i < parser.num_ibs; i++)
790 trace_amdgpu_cs(&parser, i);
791
792 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
793 if (r) {
794 goto out;
795 }
796
Christian König21c16bf2015-07-07 17:24:49 +0200797 cs->out.handle = parser.uf.sequence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798out:
799 amdgpu_cs_parser_fini(&parser, r, true);
800 up_read(&adev->exclusive_lock);
801 r = amdgpu_cs_handle_lockup(adev, r);
802 return r;
803}
804
805/**
806 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
807 *
808 * @dev: drm device
809 * @data: data from userspace
810 * @filp: file private
811 *
812 * Wait for the command submission identified by handle to finish.
813 */
814int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
815 struct drm_file *filp)
816{
817 union drm_amdgpu_wait_cs *wait = data;
818 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +0200820 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800821 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200822 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823 long r;
824
Christian König21c16bf2015-07-07 17:24:49 +0200825 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
826 wait->in.ring, &ring);
827 if (r)
828 return r;
829
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800830 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
831 if (ctx == NULL)
832 return -EINVAL;
833
Christian König21c16bf2015-07-07 17:24:49 +0200834 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
835 if (IS_ERR(fence))
836 r = PTR_ERR(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837
Christian König21c16bf2015-07-07 17:24:49 +0200838 else if (fence) {
839 r = fence_wait_timeout(fence, true, timeout);
840 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841
Christian König21c16bf2015-07-07 17:24:49 +0200842 } else
843 r = 1;
844
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800845 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 if (r < 0)
847 return r;
848
849 memset(wait, 0, sizeof(*wait));
850 wait->out.status = (r == 0);
851
852 return 0;
853}
854
855/**
856 * amdgpu_cs_find_bo_va - find bo_va for VM address
857 *
858 * @parser: command submission parser context
859 * @addr: VM address
860 * @bo: resulting BO of the mapping found
861 *
862 * Search the buffer objects in the command submission context for a certain
863 * virtual memory address. Returns allocation structure when found, NULL
864 * otherwise.
865 */
866struct amdgpu_bo_va_mapping *
867amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
868 uint64_t addr, struct amdgpu_bo **bo)
869{
870 struct amdgpu_bo_list_entry *reloc;
871 struct amdgpu_bo_va_mapping *mapping;
872
873 addr /= AMDGPU_GPU_PAGE_SIZE;
874
875 list_for_each_entry(reloc, &parser->validated, tv.head) {
876 if (!reloc->bo_va)
877 continue;
878
879 list_for_each_entry(mapping, &reloc->bo_va->mappings, list) {
880 if (mapping->it.start > addr ||
881 addr > mapping->it.last)
882 continue;
883
884 *bo = reloc->bo_va->bo;
885 return mapping;
886 }
887 }
888
889 return NULL;
890}