blob: d4cc232ccff3cc9549e69149b6840e6a1d3562cf [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
Chunming Zhou049fc522015-07-21 14:36:51 +0800129static void amdgpu_job_work_func(struct work_struct *work)
130{
131 struct amdgpu_cs_parser *sched_job =
132 container_of(work, struct amdgpu_cs_parser,
133 job_work);
134 mutex_lock(&sched_job->job_lock);
Chunming Zhouafe10082015-07-28 16:11:52 +0800135 if (sched_job->free_job)
136 sched_job->free_job(sched_job);
Chunming Zhou049fc522015-07-21 14:36:51 +0800137 mutex_unlock(&sched_job->job_lock);
138 /* after processing job, free memory */
139 kfree(sched_job);
140}
141struct amdgpu_cs_parser *amdgpu_cs_parser_create(struct amdgpu_device *adev,
142 struct drm_file *filp,
143 struct amdgpu_ctx *ctx,
144 struct amdgpu_ib *ibs,
145 uint32_t num_ibs)
146{
147 struct amdgpu_cs_parser *parser;
148 int i;
149
150 parser = kzalloc(sizeof(struct amdgpu_cs_parser), GFP_KERNEL);
151 if (!parser)
152 return NULL;
153
154 parser->adev = adev;
155 parser->filp = filp;
156 parser->ctx = ctx;
157 parser->ibs = ibs;
158 parser->num_ibs = num_ibs;
159 if (amdgpu_enable_scheduler) {
160 mutex_init(&parser->job_lock);
161 INIT_WORK(&parser->job_work, amdgpu_job_work_func);
162 }
163 for (i = 0; i < num_ibs; i++)
164 ibs[i].ctx = ctx;
165
166 return parser;
167}
168
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
170{
171 union drm_amdgpu_cs *cs = data;
172 uint64_t *chunk_array_user;
173 uint64_t *chunk_array = NULL;
174 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +0800175 struct amdgpu_bo_list *bo_list = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 unsigned size, i;
177 int r = 0;
178
179 if (!cs->in.num_chunks)
180 goto out;
181
Christian König3cb485f2015-05-11 15:34:59 +0200182 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
183 if (!p->ctx) {
184 r = -EINVAL;
185 goto out;
186 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800187 bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
188 if (bo_list && !bo_list->has_userptr) {
189 p->bo_list = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
190 if (!p->bo_list)
191 return -ENOMEM;
192 amdgpu_bo_list_copy(p->adev, p->bo_list, bo_list);
193 amdgpu_bo_list_put(bo_list);
194 } else if (bo_list && bo_list->has_userptr)
195 p->bo_list = bo_list;
196 else
197 p->bo_list = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198
199 /* get chunks */
200 INIT_LIST_HEAD(&p->validated);
monk.liue60b3442015-07-17 18:39:25 +0800201 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202 if (chunk_array == NULL) {
203 r = -ENOMEM;
204 goto out;
205 }
206
monk.liue60b3442015-07-17 18:39:25 +0800207 chunk_array_user = (uint64_t __user *)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208 if (copy_from_user(chunk_array, chunk_array_user,
209 sizeof(uint64_t)*cs->in.num_chunks)) {
210 r = -EFAULT;
211 goto out;
212 }
213
214 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800215 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216 GFP_KERNEL);
217 if (p->chunks == NULL) {
218 r = -ENOMEM;
219 goto out;
220 }
221
222 for (i = 0; i < p->nchunks; i++) {
223 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
224 struct drm_amdgpu_cs_chunk user_chunk;
225 uint32_t __user *cdata;
226
monk.liue60b3442015-07-17 18:39:25 +0800227 chunk_ptr = (void __user *)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228 if (copy_from_user(&user_chunk, chunk_ptr,
229 sizeof(struct drm_amdgpu_cs_chunk))) {
230 r = -EFAULT;
231 goto out;
232 }
233 p->chunks[i].chunk_id = user_chunk.chunk_id;
234 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235
236 size = p->chunks[i].length_dw;
monk.liue60b3442015-07-17 18:39:25 +0800237 cdata = (void __user *)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238 p->chunks[i].user_ptr = cdata;
239
240 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
241 if (p->chunks[i].kdata == NULL) {
242 r = -ENOMEM;
243 goto out;
244 }
245 size *= sizeof(uint32_t);
246 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
247 r = -EFAULT;
248 goto out;
249 }
250
Christian König9a5e8fb2015-06-23 17:07:03 +0200251 switch (p->chunks[i].chunk_id) {
252 case AMDGPU_CHUNK_ID_IB:
253 p->num_ibs++;
254 break;
255
256 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
258 if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
259 uint32_t handle;
260 struct drm_gem_object *gobj;
261 struct drm_amdgpu_cs_chunk_fence *fence_data;
262
263 fence_data = (void *)p->chunks[i].kdata;
264 handle = fence_data->handle;
265 gobj = drm_gem_object_lookup(p->adev->ddev,
266 p->filp, handle);
267 if (gobj == NULL) {
268 r = -EINVAL;
269 goto out;
270 }
271
272 p->uf.bo = gem_to_amdgpu_bo(gobj);
273 p->uf.offset = fence_data->offset;
274 } else {
275 r = -EINVAL;
276 goto out;
277 }
Christian König9a5e8fb2015-06-23 17:07:03 +0200278 break;
279
Christian König2b48d322015-06-19 17:31:29 +0200280 case AMDGPU_CHUNK_ID_DEPENDENCIES:
281 break;
282
Christian König9a5e8fb2015-06-23 17:07:03 +0200283 default:
284 r = -EINVAL;
285 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286 }
287 }
288
monk.liue60b3442015-07-17 18:39:25 +0800289
290 p->ibs = kmalloc_array(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
291 if (!p->ibs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400292 r = -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400293
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294out:
295 kfree(chunk_array);
296 return r;
297}
298
299/* Returns how many bytes TTM can move per IB.
300 */
301static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
302{
303 u64 real_vram_size = adev->mc.real_vram_size;
304 u64 vram_usage = atomic64_read(&adev->vram_usage);
305
306 /* This function is based on the current VRAM usage.
307 *
308 * - If all of VRAM is free, allow relocating the number of bytes that
309 * is equal to 1/4 of the size of VRAM for this IB.
310
311 * - If more than one half of VRAM is occupied, only allow relocating
312 * 1 MB of data for this IB.
313 *
314 * - From 0 to one half of used VRAM, the threshold decreases
315 * linearly.
316 * __________________
317 * 1/4 of -|\ |
318 * VRAM | \ |
319 * | \ |
320 * | \ |
321 * | \ |
322 * | \ |
323 * | \ |
324 * | \________|1 MB
325 * |----------------|
326 * VRAM 0 % 100 %
327 * used used
328 *
329 * Note: It's a threshold, not a limit. The threshold must be crossed
330 * for buffer relocations to stop, so any buffer of an arbitrary size
331 * can be moved as long as the threshold isn't crossed before
332 * the relocation takes place. We don't want to disable buffer
333 * relocations completely.
334 *
335 * The idea is that buffers should be placed in VRAM at creation time
336 * and TTM should only do a minimum number of relocations during
337 * command submission. In practice, you need to submit at least
338 * a dozen IBs to move all buffers to VRAM if they are in GTT.
339 *
340 * Also, things can get pretty crazy under memory pressure and actual
341 * VRAM usage can change a lot, so playing safe even at 50% does
342 * consistently increase performance.
343 */
344
345 u64 half_vram = real_vram_size >> 1;
346 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
347 u64 bytes_moved_threshold = half_free_vram >> 1;
348 return max(bytes_moved_threshold, 1024*1024ull);
349}
350
351int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p)
352{
353 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
354 struct amdgpu_vm *vm = &fpriv->vm;
355 struct amdgpu_device *adev = p->adev;
356 struct amdgpu_bo_list_entry *lobj;
357 struct list_head duplicates;
358 struct amdgpu_bo *bo;
359 u64 bytes_moved = 0, initial_bytes_moved;
360 u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
361 int r;
362
363 INIT_LIST_HEAD(&duplicates);
364 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
365 if (unlikely(r != 0)) {
366 return r;
367 }
368
369 list_for_each_entry(lobj, &p->validated, tv.head) {
370 bo = lobj->robj;
371 if (!bo->pin_count) {
372 u32 domain = lobj->prefered_domains;
373 u32 current_domain =
374 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
375
376 /* Check if this buffer will be moved and don't move it
377 * if we have moved too many buffers for this IB already.
378 *
379 * Note that this allows moving at least one buffer of
380 * any size, because it doesn't take the current "bo"
381 * into account. We don't want to disallow buffer moves
382 * completely.
383 */
384 if (current_domain != AMDGPU_GEM_DOMAIN_CPU &&
385 (domain & current_domain) == 0 && /* will be moved */
386 bytes_moved > bytes_moved_threshold) {
387 /* don't move it */
388 domain = current_domain;
389 }
390
391 retry:
392 amdgpu_ttm_placement_from_domain(bo, domain);
393 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
394 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
395 bytes_moved += atomic64_read(&adev->num_bytes_moved) -
396 initial_bytes_moved;
397
398 if (unlikely(r)) {
399 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
400 domain = lobj->allowed_domains;
401 goto retry;
402 }
403 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
404 return r;
405 }
406 }
407 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
408 }
409 return 0;
410}
411
412static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
413{
414 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
415 struct amdgpu_cs_buckets buckets;
monk.liu840d5142015-04-27 15:19:20 +0800416 bool need_mmap_lock = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417 int i, r;
418
monk.liu840d5142015-04-27 15:19:20 +0800419 if (p->bo_list) {
420 need_mmap_lock = p->bo_list->has_userptr;
421 amdgpu_cs_buckets_init(&buckets);
422 for (i = 0; i < p->bo_list->num_entries; i++)
423 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
424 p->bo_list->array[i].priority);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425
monk.liu840d5142015-04-27 15:19:20 +0800426 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
427 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429 p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
430 &p->validated);
431
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 if (need_mmap_lock)
433 down_read(&current->mm->mmap_sem);
434
435 r = amdgpu_cs_list_validate(p);
436
437 if (need_mmap_lock)
438 up_read(&current->mm->mmap_sem);
439
440 return r;
441}
442
443static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
444{
445 struct amdgpu_bo_list_entry *e;
446 int r;
447
448 list_for_each_entry(e, &p->validated, tv.head) {
449 struct reservation_object *resv = e->robj->tbo.resv;
450 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
451
452 if (r)
453 return r;
454 }
455 return 0;
456}
457
458static int cmp_size_smaller_first(void *priv, struct list_head *a,
459 struct list_head *b)
460{
461 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
462 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
463
464 /* Sort A before B if A is smaller. */
465 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
466}
467
Chunming Zhou049fc522015-07-21 14:36:51 +0800468static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser *parser, int error, bool backoff)
469{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470 if (!error) {
471 /* Sort the buffer list from the smallest to largest buffer,
472 * which affects the order of buffers in the LRU list.
473 * This assures that the smallest buffers are added first
474 * to the LRU list, so they are likely to be later evicted
475 * first, instead of large buffers whose eviction is more
476 * expensive.
477 *
478 * This slightly lowers the number of bytes moved by TTM
479 * per frame under memory pressure.
480 */
481 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
482
483 ttm_eu_fence_buffer_objects(&parser->ticket,
484 &parser->validated,
485 &parser->ibs[parser->num_ibs-1].fence->base);
486 } else if (backoff) {
487 ttm_eu_backoff_reservation(&parser->ticket,
488 &parser->validated);
489 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800490}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491
Chunming Zhou049fc522015-07-21 14:36:51 +0800492static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser *parser)
493{
494 unsigned i;
Christian König3cb485f2015-05-11 15:34:59 +0200495 if (parser->ctx)
496 amdgpu_ctx_put(parser->ctx);
Chunming Zhou049fc522015-07-21 14:36:51 +0800497 if (parser->bo_list) {
498 if (!parser->bo_list->has_userptr)
499 amdgpu_bo_list_free(parser->bo_list);
500 else
501 amdgpu_bo_list_put(parser->bo_list);
502 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 drm_free_large(parser->vm_bos);
504 for (i = 0; i < parser->nchunks; i++)
505 drm_free_large(parser->chunks[i].kdata);
506 kfree(parser->chunks);
Christian Königb8682ac2015-06-22 14:54:32 +0200507 if (parser->ibs)
508 for (i = 0; i < parser->num_ibs; i++)
509 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510 kfree(parser->ibs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 if (parser->uf.bo)
512 drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
Chunming Zhou049fc522015-07-21 14:36:51 +0800513
514 if (!amdgpu_enable_scheduler)
515 kfree(parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400516}
517
Christian König351dba72015-08-03 20:39:12 +0200518/**
519 * cs_parser_fini() - clean parser states
520 * @parser: parser structure holding parsing context.
521 * @error: error number
522 *
523 * If error is set than unvalidate buffer, otherwise just free memory
524 * used by parsing context.
525 **/
526static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
527{
528 amdgpu_cs_parser_fini_early(parser, error, backoff);
529 amdgpu_cs_parser_fini_late(parser);
530}
531
532static int amdgpu_cs_parser_run_job(
533 struct amdgpu_cs_parser *sched_job)
534{
535 amdgpu_cs_parser_fini_early(sched_job, 0, true);
536 return 0;
537}
538
539static int amdgpu_cs_parser_free_job(
540 struct amdgpu_cs_parser *sched_job)
541{
542 amdgpu_cs_parser_fini_late(sched_job);
543 return 0;
544}
545
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
547 struct amdgpu_vm *vm)
548{
549 struct amdgpu_device *adev = p->adev;
550 struct amdgpu_bo_va *bo_va;
551 struct amdgpu_bo *bo;
552 int i, r;
553
554 r = amdgpu_vm_update_page_directory(adev, vm);
555 if (r)
556 return r;
557
558 r = amdgpu_vm_clear_freed(adev, vm);
559 if (r)
560 return r;
561
562 if (p->bo_list) {
563 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200564 struct fence *f;
565
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566 /* ignore duplicates */
567 bo = p->bo_list->array[i].robj;
568 if (!bo)
569 continue;
570
571 bo_va = p->bo_list->array[i].bo_va;
572 if (bo_va == NULL)
573 continue;
574
575 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
576 if (r)
577 return r;
578
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800579 f = bo_va->last_pt_update;
Christian König91e1a522015-07-06 22:06:40 +0200580 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
581 if (r)
582 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 }
584 }
585
monk.liucfe2c972015-05-26 15:01:54 +0800586 return amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587}
588
589static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
590 struct amdgpu_cs_parser *parser)
591{
592 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
593 struct amdgpu_vm *vm = &fpriv->vm;
594 struct amdgpu_ring *ring;
595 int i, r;
596
597 if (parser->num_ibs == 0)
598 return 0;
599
600 /* Only for UVD/VCE VM emulation */
601 for (i = 0; i < parser->num_ibs; i++) {
602 ring = parser->ibs[i].ring;
603 if (ring->funcs->parse_cs) {
604 r = amdgpu_ring_parse_cs(ring, parser, i);
605 if (r)
606 return r;
607 }
608 }
609
610 mutex_lock(&vm->mutex);
611 r = amdgpu_bo_vm_update_pte(parser, vm);
612 if (r) {
613 goto out;
614 }
615 amdgpu_cs_sync_rings(parser);
Chunming Zhou049fc522015-07-21 14:36:51 +0800616 if (!amdgpu_enable_scheduler)
617 r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
618 parser->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619
620out:
621 mutex_unlock(&vm->mutex);
622 return r;
623}
624
625static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
626{
627 if (r == -EDEADLK) {
628 r = amdgpu_gpu_reset(adev);
629 if (!r)
630 r = -EAGAIN;
631 }
632 return r;
633}
634
635static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
636 struct amdgpu_cs_parser *parser)
637{
638 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
639 struct amdgpu_vm *vm = &fpriv->vm;
640 int i, j;
641 int r;
642
643 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
644 struct amdgpu_cs_chunk *chunk;
645 struct amdgpu_ib *ib;
646 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648
649 chunk = &parser->chunks[i];
650 ib = &parser->ibs[j];
651 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
652
653 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
654 continue;
655
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
657 chunk_ib->ip_instance, chunk_ib->ring,
658 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200659 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661
662 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200663 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200664 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200665 uint64_t offset;
666 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200667
Christian König4802ce12015-06-10 17:20:11 +0200668 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
669 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200670 if (!aobj) {
671 DRM_ERROR("IB va_start is invalid\n");
672 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 }
674
Christian König4802ce12015-06-10 17:20:11 +0200675 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
676 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
677 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
678 return -EINVAL;
679 }
680
Marek Olšák3ccec532015-06-02 17:44:49 +0200681 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200682 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400683 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 return r;
685 }
686
Christian König4802ce12015-06-10 17:20:11 +0200687 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
688 kptr += chunk_ib->va_start - offset;
689
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
691 if (r) {
692 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 return r;
694 }
695
696 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
697 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 } else {
699 r = amdgpu_ib_get(ring, vm, 0, ib);
700 if (r) {
701 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 return r;
703 }
704
705 ib->gpu_addr = chunk_ib->va_start;
706 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707
Marek Olšák3ccec532015-06-02 17:44:49 +0200708 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800709 ib->flags = chunk_ib->flags;
Christian König3cb485f2015-05-11 15:34:59 +0200710 ib->ctx = parser->ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 j++;
712 }
713
714 if (!parser->num_ibs)
715 return 0;
716
717 /* add GDS resources to first IB */
718 if (parser->bo_list) {
719 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
720 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
721 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
722 struct amdgpu_ib *ib = &parser->ibs[0];
723
724 if (gds) {
725 ib->gds_base = amdgpu_bo_gpu_offset(gds);
726 ib->gds_size = amdgpu_bo_size(gds);
727 }
728 if (gws) {
729 ib->gws_base = amdgpu_bo_gpu_offset(gws);
730 ib->gws_size = amdgpu_bo_size(gws);
731 }
732 if (oa) {
733 ib->oa_base = amdgpu_bo_gpu_offset(oa);
734 ib->oa_size = amdgpu_bo_size(oa);
735 }
736 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 /* wrap the last IB with user fence */
738 if (parser->uf.bo) {
739 struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
740
741 /* UVD & VCE fw doesn't support user fences */
742 if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
743 ib->ring->type == AMDGPU_RING_TYPE_VCE)
744 return -EINVAL;
745
746 ib->user = &parser->uf;
747 }
748
749 return 0;
750}
751
Christian König2b48d322015-06-19 17:31:29 +0200752static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
753 struct amdgpu_cs_parser *p)
754{
Christian König76a1ea62015-07-06 19:42:10 +0200755 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200756 struct amdgpu_ib *ib;
757 int i, j, r;
758
759 if (!p->num_ibs)
760 return 0;
761
762 /* Add dependencies to first IB */
763 ib = &p->ibs[0];
764 for (i = 0; i < p->nchunks; ++i) {
765 struct drm_amdgpu_cs_chunk_dep *deps;
766 struct amdgpu_cs_chunk *chunk;
767 unsigned num_deps;
768
769 chunk = &p->chunks[i];
770
771 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
772 continue;
773
774 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
775 num_deps = chunk->length_dw * 4 /
776 sizeof(struct drm_amdgpu_cs_chunk_dep);
777
778 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200779 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200780 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200781 struct fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200782
783 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
784 deps[j].ip_instance,
785 deps[j].ring, &ring);
786 if (r)
787 return r;
788
Christian König76a1ea62015-07-06 19:42:10 +0200789 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
790 if (ctx == NULL)
791 return -EINVAL;
792
Christian König21c16bf2015-07-07 17:24:49 +0200793 fence = amdgpu_ctx_get_fence(ctx, ring,
794 deps[j].handle);
795 if (IS_ERR(fence)) {
796 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +0200797 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +0200798 return r;
Christian König21c16bf2015-07-07 17:24:49 +0200799
800 } else if (fence) {
801 r = amdgpu_sync_fence(adev, &ib->sync, fence);
802 fence_put(fence);
803 amdgpu_ctx_put(ctx);
804 if (r)
805 return r;
Christian König76a1ea62015-07-06 19:42:10 +0200806 }
Christian König2b48d322015-06-19 17:31:29 +0200807 }
808 }
809
810 return 0;
811}
812
Chunming Zhou049fc522015-07-21 14:36:51 +0800813static int amdgpu_cs_parser_prepare_job(struct amdgpu_cs_parser *sched_job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814{
Jammy Zhoudd01d072015-07-30 17:19:52 +0800815 int r, i;
Chunming Zhou049fc522015-07-21 14:36:51 +0800816 struct amdgpu_cs_parser *parser = sched_job;
817 struct amdgpu_device *adev = sched_job->adev;
Jammy Zhoudd01d072015-07-30 17:19:52 +0800818 bool reserved_buffers = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819
Jammy Zhoudd01d072015-07-30 17:19:52 +0800820 r = amdgpu_cs_parser_relocs(parser);
821 if (r) {
822 if (r != -ERESTARTSYS) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200823 if (r == -ENOMEM)
824 DRM_ERROR("Not enough memory for command submission!\n");
825 else
826 DRM_ERROR("Failed to process the buffer list %d!\n", r);
827 }
Christian König2b48d322015-06-19 17:31:29 +0200828 }
829
830 if (!r) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200831 reserved_buffers = true;
Chunming Zhou049fc522015-07-21 14:36:51 +0800832 r = amdgpu_cs_ib_fill(adev, parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400833 }
Christian König21c16bf2015-07-07 17:24:49 +0200834 if (!r) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800835 r = amdgpu_cs_dependencies(adev, parser);
Christian König21c16bf2015-07-07 17:24:49 +0200836 if (r)
837 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
838 }
Jammy Zhoudd01d072015-07-30 17:19:52 +0800839 if (r) {
840 amdgpu_cs_parser_fini(parser, r, reserved_buffers);
841 return r;
842 }
Christian König2b48d322015-06-19 17:31:29 +0200843
Jammy Zhoudd01d072015-07-30 17:19:52 +0800844 for (i = 0; i < parser->num_ibs; i++)
845 trace_amdgpu_cs(parser, i);
Chunming Zhou049fc522015-07-21 14:36:51 +0800846
Jammy Zhoudd01d072015-07-30 17:19:52 +0800847 r = amdgpu_cs_ib_vm_chunk(adev, parser);
848 return r;
Chunming Zhou049fc522015-07-21 14:36:51 +0800849}
850
851static struct amdgpu_ring *amdgpu_cs_parser_get_ring(
852 struct amdgpu_device *adev,
853 struct amdgpu_cs_parser *parser)
854{
855 int i, r;
856
857 struct amdgpu_cs_chunk *chunk;
858 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
859 struct amdgpu_ring *ring;
860 for (i = 0; i < parser->nchunks; i++) {
861 chunk = &parser->chunks[i];
862 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
863
864 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
865 continue;
866
867 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
868 chunk_ib->ip_instance, chunk_ib->ring,
869 &ring);
870 if (r)
871 return NULL;
872 break;
873 }
874 return ring;
875}
876
877int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
878{
879 struct amdgpu_device *adev = dev->dev_private;
880 union drm_amdgpu_cs *cs = data;
881 struct amdgpu_cs_parser *parser;
882 int r;
883
884 down_read(&adev->exclusive_lock);
885 if (!adev->accel_working) {
886 up_read(&adev->exclusive_lock);
887 return -EBUSY;
888 }
889
890 parser = amdgpu_cs_parser_create(adev, filp, NULL, NULL, 0);
891 if (!parser)
892 return -ENOMEM;
893 r = amdgpu_cs_parser_init(parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800895 DRM_ERROR("Failed to initialize parser !\n");
896 amdgpu_cs_parser_fini(parser, r, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400897 up_read(&adev->exclusive_lock);
898 r = amdgpu_cs_handle_lockup(adev, r);
899 return r;
900 }
901
Chunming Zhou049fc522015-07-21 14:36:51 +0800902 if (amdgpu_enable_scheduler && parser->num_ibs) {
903 struct amdgpu_ring * ring =
904 amdgpu_cs_parser_get_ring(adev, parser);
Chunming Zhou4274f5d2015-07-21 16:04:39 +0800905 if (ring->is_pte_ring || (parser->bo_list && parser->bo_list->has_userptr)) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800906 r = amdgpu_cs_parser_prepare_job(parser);
907 if (r)
908 goto out;
909 } else
910 parser->prepare_job = amdgpu_cs_parser_prepare_job;
Chunming Zhou4b559c92015-07-21 15:53:04 +0800911 parser->ring = ring;
Chunming Zhou049fc522015-07-21 14:36:51 +0800912 parser->run_job = amdgpu_cs_parser_run_job;
913 parser->free_job = amdgpu_cs_parser_free_job;
Jammy Zhouea199cc2015-07-31 16:47:28 +0800914 parser->ibs[parser->num_ibs - 1].sequence =
915 amd_sched_push_job(ring->scheduler,
Chunming Zhou049fc522015-07-21 14:36:51 +0800916 &parser->ctx->rings[ring->idx].c_entity,
917 parser);
Chunming Zhoud1ff9082015-07-30 17:59:43 +0800918 cs->out.handle = parser->ibs[parser->num_ibs - 1].sequence;
Chunming Zhou049fc522015-07-21 14:36:51 +0800919 up_read(&adev->exclusive_lock);
920 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800922 r = amdgpu_cs_parser_prepare_job(parser);
923 if (r)
924 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925
Chunming Zhou049fc522015-07-21 14:36:51 +0800926 cs->out.handle = parser->ibs[parser->num_ibs - 1].sequence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927out:
Chunming Zhou049fc522015-07-21 14:36:51 +0800928 amdgpu_cs_parser_fini(parser, r, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 up_read(&adev->exclusive_lock);
930 r = amdgpu_cs_handle_lockup(adev, r);
931 return r;
932}
933
934/**
935 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
936 *
937 * @dev: drm device
938 * @data: data from userspace
939 * @filp: file private
940 *
941 * Wait for the command submission identified by handle to finish.
942 */
943int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
944 struct drm_file *filp)
945{
946 union drm_amdgpu_wait_cs *wait = data;
947 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +0200949 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800950 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200951 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 long r;
953
Christian König21c16bf2015-07-07 17:24:49 +0200954 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
955 wait->in.ring, &ring);
956 if (r)
957 return r;
958
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800959 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
960 if (ctx == NULL)
961 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +0800962
963 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
964 if (IS_ERR(fence))
965 r = PTR_ERR(fence);
966 else if (fence) {
967 r = fence_wait_timeout(fence, true, timeout);
968 fence_put(fence);
969 } else
Christian König21c16bf2015-07-07 17:24:49 +0200970 r = 1;
971
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800972 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973 if (r < 0)
974 return r;
975
976 memset(wait, 0, sizeof(*wait));
977 wait->out.status = (r == 0);
978
979 return 0;
980}
981
982/**
983 * amdgpu_cs_find_bo_va - find bo_va for VM address
984 *
985 * @parser: command submission parser context
986 * @addr: VM address
987 * @bo: resulting BO of the mapping found
988 *
989 * Search the buffer objects in the command submission context for a certain
990 * virtual memory address. Returns allocation structure when found, NULL
991 * otherwise.
992 */
993struct amdgpu_bo_va_mapping *
994amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
995 uint64_t addr, struct amdgpu_bo **bo)
996{
997 struct amdgpu_bo_list_entry *reloc;
998 struct amdgpu_bo_va_mapping *mapping;
999
1000 addr /= AMDGPU_GPU_PAGE_SIZE;
1001
1002 list_for_each_entry(reloc, &parser->validated, tv.head) {
1003 if (!reloc->bo_va)
1004 continue;
1005
Christian König7fc11952015-07-30 11:53:42 +02001006 list_for_each_entry(mapping, &reloc->bo_va->valids, list) {
1007 if (mapping->it.start > addr ||
1008 addr > mapping->it.last)
1009 continue;
1010
1011 *bo = reloc->bo_va->bo;
1012 return mapping;
1013 }
1014
1015 list_for_each_entry(mapping, &reloc->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016 if (mapping->it.start > addr ||
1017 addr > mapping->it.last)
1018 continue;
1019
1020 *bo = reloc->bo_va->bo;
1021 return mapping;
1022 }
1023 }
1024
1025 return NULL;
1026}