blob: 5f2403898b06c945cd8b4ddd71e898df56c05cb1 [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
Chunming Zhou049fc522015-07-21 14:36:51 +080044static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser,
45 int error, bool backoff);
46static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser *parser, int error, bool backoff);
47static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser *parser);
48
Alex Deucherd38ceaf2015-04-20 16:55:21 -040049static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets *b)
50{
51 unsigned i;
52
53 for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++)
54 INIT_LIST_HEAD(&b->bucket[i]);
55}
56
57static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets *b,
58 struct list_head *item, unsigned priority)
59{
60 /* Since buffers which appear sooner in the relocation list are
61 * likely to be used more often than buffers which appear later
62 * in the list, the sort mustn't change the ordering of buffers
63 * with the same priority, i.e. it must be stable.
64 */
65 list_add_tail(item, &b->bucket[min(priority, AMDGPU_CS_MAX_PRIORITY)]);
66}
67
68static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets *b,
69 struct list_head *out_list)
70{
71 unsigned i;
72
73 /* Connect the sorted buckets in the output list. */
74 for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) {
75 list_splice(&b->bucket[i], out_list);
76 }
77}
78
79int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
80 u32 ip_instance, u32 ring,
81 struct amdgpu_ring **out_ring)
82{
83 /* Right now all IPs have only one instance - multiple rings. */
84 if (ip_instance != 0) {
85 DRM_ERROR("invalid ip instance: %d\n", ip_instance);
86 return -EINVAL;
87 }
88
89 switch (ip_type) {
90 default:
91 DRM_ERROR("unknown ip type: %d\n", ip_type);
92 return -EINVAL;
93 case AMDGPU_HW_IP_GFX:
94 if (ring < adev->gfx.num_gfx_rings) {
95 *out_ring = &adev->gfx.gfx_ring[ring];
96 } else {
97 DRM_ERROR("only %d gfx rings are supported now\n",
98 adev->gfx.num_gfx_rings);
99 return -EINVAL;
100 }
101 break;
102 case AMDGPU_HW_IP_COMPUTE:
103 if (ring < adev->gfx.num_compute_rings) {
104 *out_ring = &adev->gfx.compute_ring[ring];
105 } else {
106 DRM_ERROR("only %d compute rings are supported now\n",
107 adev->gfx.num_compute_rings);
108 return -EINVAL;
109 }
110 break;
111 case AMDGPU_HW_IP_DMA:
112 if (ring < 2) {
113 *out_ring = &adev->sdma[ring].ring;
114 } else {
115 DRM_ERROR("only two SDMA rings are supported\n");
116 return -EINVAL;
117 }
118 break;
119 case AMDGPU_HW_IP_UVD:
120 *out_ring = &adev->uvd.ring;
121 break;
122 case AMDGPU_HW_IP_VCE:
123 if (ring < 2){
124 *out_ring = &adev->vce.ring[ring];
125 } else {
126 DRM_ERROR("only two VCE rings are supported\n");
127 return -EINVAL;
128 }
129 break;
130 }
131 return 0;
132}
133
Chunming Zhou049fc522015-07-21 14:36:51 +0800134static void amdgpu_job_work_func(struct work_struct *work)
135{
136 struct amdgpu_cs_parser *sched_job =
137 container_of(work, struct amdgpu_cs_parser,
138 job_work);
139 mutex_lock(&sched_job->job_lock);
140 sched_job->free_job(sched_job);
141 mutex_unlock(&sched_job->job_lock);
142 /* after processing job, free memory */
143 kfree(sched_job);
144}
145struct amdgpu_cs_parser *amdgpu_cs_parser_create(struct amdgpu_device *adev,
146 struct drm_file *filp,
147 struct amdgpu_ctx *ctx,
148 struct amdgpu_ib *ibs,
149 uint32_t num_ibs)
150{
151 struct amdgpu_cs_parser *parser;
152 int i;
153
154 parser = kzalloc(sizeof(struct amdgpu_cs_parser), GFP_KERNEL);
155 if (!parser)
156 return NULL;
157
158 parser->adev = adev;
159 parser->filp = filp;
160 parser->ctx = ctx;
161 parser->ibs = ibs;
162 parser->num_ibs = num_ibs;
163 if (amdgpu_enable_scheduler) {
164 mutex_init(&parser->job_lock);
165 INIT_WORK(&parser->job_work, amdgpu_job_work_func);
166 }
167 for (i = 0; i < num_ibs; i++)
168 ibs[i].ctx = ctx;
169
170 return parser;
171}
172
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
174{
175 union drm_amdgpu_cs *cs = data;
176 uint64_t *chunk_array_user;
177 uint64_t *chunk_array = NULL;
178 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +0800179 struct amdgpu_bo_list *bo_list = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 unsigned size, i;
181 int r = 0;
182
183 if (!cs->in.num_chunks)
184 goto out;
185
Christian König3cb485f2015-05-11 15:34:59 +0200186 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
187 if (!p->ctx) {
188 r = -EINVAL;
189 goto out;
190 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800191 bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
192 if (bo_list && !bo_list->has_userptr) {
193 p->bo_list = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
194 if (!p->bo_list)
195 return -ENOMEM;
196 amdgpu_bo_list_copy(p->adev, p->bo_list, bo_list);
197 amdgpu_bo_list_put(bo_list);
198 } else if (bo_list && bo_list->has_userptr)
199 p->bo_list = bo_list;
200 else
201 p->bo_list = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202
203 /* get chunks */
204 INIT_LIST_HEAD(&p->validated);
monk.liue60b3442015-07-17 18:39:25 +0800205 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 if (chunk_array == NULL) {
207 r = -ENOMEM;
208 goto out;
209 }
210
monk.liue60b3442015-07-17 18:39:25 +0800211 chunk_array_user = (uint64_t __user *)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 if (copy_from_user(chunk_array, chunk_array_user,
213 sizeof(uint64_t)*cs->in.num_chunks)) {
214 r = -EFAULT;
215 goto out;
216 }
217
218 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800219 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220 GFP_KERNEL);
221 if (p->chunks == NULL) {
222 r = -ENOMEM;
223 goto out;
224 }
225
226 for (i = 0; i < p->nchunks; i++) {
227 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
228 struct drm_amdgpu_cs_chunk user_chunk;
229 uint32_t __user *cdata;
230
monk.liue60b3442015-07-17 18:39:25 +0800231 chunk_ptr = (void __user *)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232 if (copy_from_user(&user_chunk, chunk_ptr,
233 sizeof(struct drm_amdgpu_cs_chunk))) {
234 r = -EFAULT;
235 goto out;
236 }
237 p->chunks[i].chunk_id = user_chunk.chunk_id;
238 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239
240 size = p->chunks[i].length_dw;
monk.liue60b3442015-07-17 18:39:25 +0800241 cdata = (void __user *)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242 p->chunks[i].user_ptr = cdata;
243
244 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
245 if (p->chunks[i].kdata == NULL) {
246 r = -ENOMEM;
247 goto out;
248 }
249 size *= sizeof(uint32_t);
250 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
251 r = -EFAULT;
252 goto out;
253 }
254
Christian König9a5e8fb2015-06-23 17:07:03 +0200255 switch (p->chunks[i].chunk_id) {
256 case AMDGPU_CHUNK_ID_IB:
257 p->num_ibs++;
258 break;
259
260 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400261 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
262 if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
263 uint32_t handle;
264 struct drm_gem_object *gobj;
265 struct drm_amdgpu_cs_chunk_fence *fence_data;
266
267 fence_data = (void *)p->chunks[i].kdata;
268 handle = fence_data->handle;
269 gobj = drm_gem_object_lookup(p->adev->ddev,
270 p->filp, handle);
271 if (gobj == NULL) {
272 r = -EINVAL;
273 goto out;
274 }
275
276 p->uf.bo = gem_to_amdgpu_bo(gobj);
277 p->uf.offset = fence_data->offset;
278 } else {
279 r = -EINVAL;
280 goto out;
281 }
Christian König9a5e8fb2015-06-23 17:07:03 +0200282 break;
283
Christian König2b48d322015-06-19 17:31:29 +0200284 case AMDGPU_CHUNK_ID_DEPENDENCIES:
285 break;
286
Christian König9a5e8fb2015-06-23 17:07:03 +0200287 default:
288 r = -EINVAL;
289 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290 }
291 }
292
monk.liue60b3442015-07-17 18:39:25 +0800293
294 p->ibs = kmalloc_array(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
295 if (!p->ibs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 r = -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298out:
299 kfree(chunk_array);
300 return r;
301}
302
303/* Returns how many bytes TTM can move per IB.
304 */
305static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
306{
307 u64 real_vram_size = adev->mc.real_vram_size;
308 u64 vram_usage = atomic64_read(&adev->vram_usage);
309
310 /* This function is based on the current VRAM usage.
311 *
312 * - If all of VRAM is free, allow relocating the number of bytes that
313 * is equal to 1/4 of the size of VRAM for this IB.
314
315 * - If more than one half of VRAM is occupied, only allow relocating
316 * 1 MB of data for this IB.
317 *
318 * - From 0 to one half of used VRAM, the threshold decreases
319 * linearly.
320 * __________________
321 * 1/4 of -|\ |
322 * VRAM | \ |
323 * | \ |
324 * | \ |
325 * | \ |
326 * | \ |
327 * | \ |
328 * | \________|1 MB
329 * |----------------|
330 * VRAM 0 % 100 %
331 * used used
332 *
333 * Note: It's a threshold, not a limit. The threshold must be crossed
334 * for buffer relocations to stop, so any buffer of an arbitrary size
335 * can be moved as long as the threshold isn't crossed before
336 * the relocation takes place. We don't want to disable buffer
337 * relocations completely.
338 *
339 * The idea is that buffers should be placed in VRAM at creation time
340 * and TTM should only do a minimum number of relocations during
341 * command submission. In practice, you need to submit at least
342 * a dozen IBs to move all buffers to VRAM if they are in GTT.
343 *
344 * Also, things can get pretty crazy under memory pressure and actual
345 * VRAM usage can change a lot, so playing safe even at 50% does
346 * consistently increase performance.
347 */
348
349 u64 half_vram = real_vram_size >> 1;
350 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
351 u64 bytes_moved_threshold = half_free_vram >> 1;
352 return max(bytes_moved_threshold, 1024*1024ull);
353}
354
355int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p)
356{
357 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
358 struct amdgpu_vm *vm = &fpriv->vm;
359 struct amdgpu_device *adev = p->adev;
360 struct amdgpu_bo_list_entry *lobj;
361 struct list_head duplicates;
362 struct amdgpu_bo *bo;
363 u64 bytes_moved = 0, initial_bytes_moved;
364 u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
365 int r;
366
367 INIT_LIST_HEAD(&duplicates);
368 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
369 if (unlikely(r != 0)) {
370 return r;
371 }
372
373 list_for_each_entry(lobj, &p->validated, tv.head) {
374 bo = lobj->robj;
375 if (!bo->pin_count) {
376 u32 domain = lobj->prefered_domains;
377 u32 current_domain =
378 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
379
380 /* Check if this buffer will be moved and don't move it
381 * if we have moved too many buffers for this IB already.
382 *
383 * Note that this allows moving at least one buffer of
384 * any size, because it doesn't take the current "bo"
385 * into account. We don't want to disallow buffer moves
386 * completely.
387 */
388 if (current_domain != AMDGPU_GEM_DOMAIN_CPU &&
389 (domain & current_domain) == 0 && /* will be moved */
390 bytes_moved > bytes_moved_threshold) {
391 /* don't move it */
392 domain = current_domain;
393 }
394
395 retry:
396 amdgpu_ttm_placement_from_domain(bo, domain);
397 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
398 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
399 bytes_moved += atomic64_read(&adev->num_bytes_moved) -
400 initial_bytes_moved;
401
402 if (unlikely(r)) {
403 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
404 domain = lobj->allowed_domains;
405 goto retry;
406 }
407 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
408 return r;
409 }
410 }
411 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
412 }
413 return 0;
414}
415
416static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
417{
418 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
419 struct amdgpu_cs_buckets buckets;
monk.liu840d5142015-04-27 15:19:20 +0800420 bool need_mmap_lock = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421 int i, r;
422
monk.liu840d5142015-04-27 15:19:20 +0800423 if (p->bo_list) {
424 need_mmap_lock = p->bo_list->has_userptr;
425 amdgpu_cs_buckets_init(&buckets);
426 for (i = 0; i < p->bo_list->num_entries; i++)
427 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
428 p->bo_list->array[i].priority);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429
monk.liu840d5142015-04-27 15:19:20 +0800430 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
431 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433 p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
434 &p->validated);
435
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400436 if (need_mmap_lock)
437 down_read(&current->mm->mmap_sem);
438
439 r = amdgpu_cs_list_validate(p);
440
441 if (need_mmap_lock)
442 up_read(&current->mm->mmap_sem);
443
444 return r;
445}
446
447static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
448{
449 struct amdgpu_bo_list_entry *e;
450 int r;
451
452 list_for_each_entry(e, &p->validated, tv.head) {
453 struct reservation_object *resv = e->robj->tbo.resv;
454 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
455
456 if (r)
457 return r;
458 }
459 return 0;
460}
461
462static int cmp_size_smaller_first(void *priv, struct list_head *a,
463 struct list_head *b)
464{
465 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
466 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
467
468 /* Sort A before B if A is smaller. */
469 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
470}
471
472/**
473 * cs_parser_fini() - clean parser states
474 * @parser: parser structure holding parsing context.
475 * @error: error number
476 *
477 * If error is set than unvalidate buffer, otherwise just free memory
478 * used by parsing context.
479 **/
480static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
481{
Chunming Zhou049fc522015-07-21 14:36:51 +0800482 amdgpu_cs_parser_fini_early(parser, error, backoff);
483 amdgpu_cs_parser_fini_late(parser);
484}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400485
Chunming Zhou049fc522015-07-21 14:36:51 +0800486static int amdgpu_cs_parser_run_job(
487 struct amdgpu_cs_parser *sched_job)
488{
489 amdgpu_cs_parser_fini_early(sched_job, 0, true);
490 return 0;
491}
492
493static int amdgpu_cs_parser_free_job(
494 struct amdgpu_cs_parser *sched_job)
495{
496 amdgpu_cs_parser_fini_late(sched_job);
497 return 0;
498}
499
500static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser *parser, int error, bool backoff)
501{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 if (!error) {
503 /* Sort the buffer list from the smallest to largest buffer,
504 * which affects the order of buffers in the LRU list.
505 * This assures that the smallest buffers are added first
506 * to the LRU list, so they are likely to be later evicted
507 * first, instead of large buffers whose eviction is more
508 * expensive.
509 *
510 * This slightly lowers the number of bytes moved by TTM
511 * per frame under memory pressure.
512 */
513 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
514
515 ttm_eu_fence_buffer_objects(&parser->ticket,
516 &parser->validated,
517 &parser->ibs[parser->num_ibs-1].fence->base);
518 } else if (backoff) {
519 ttm_eu_backoff_reservation(&parser->ticket,
520 &parser->validated);
521 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800522}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523
Chunming Zhou049fc522015-07-21 14:36:51 +0800524static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser *parser)
525{
526 unsigned i;
Christian König3cb485f2015-05-11 15:34:59 +0200527 if (parser->ctx)
528 amdgpu_ctx_put(parser->ctx);
Chunming Zhou049fc522015-07-21 14:36:51 +0800529 if (parser->bo_list) {
530 if (!parser->bo_list->has_userptr)
531 amdgpu_bo_list_free(parser->bo_list);
532 else
533 amdgpu_bo_list_put(parser->bo_list);
534 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 drm_free_large(parser->vm_bos);
536 for (i = 0; i < parser->nchunks; i++)
537 drm_free_large(parser->chunks[i].kdata);
538 kfree(parser->chunks);
Christian Königb8682ac2015-06-22 14:54:32 +0200539 if (parser->ibs)
540 for (i = 0; i < parser->num_ibs; i++)
541 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 kfree(parser->ibs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 if (parser->uf.bo)
544 drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
Chunming Zhou049fc522015-07-21 14:36:51 +0800545
546 if (!amdgpu_enable_scheduler)
547 kfree(parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548}
549
550static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
551 struct amdgpu_vm *vm)
552{
553 struct amdgpu_device *adev = p->adev;
554 struct amdgpu_bo_va *bo_va;
555 struct amdgpu_bo *bo;
556 int i, r;
557
558 r = amdgpu_vm_update_page_directory(adev, vm);
559 if (r)
560 return r;
561
562 r = amdgpu_vm_clear_freed(adev, vm);
563 if (r)
564 return r;
565
566 if (p->bo_list) {
567 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200568 struct fence *f;
569
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 /* ignore duplicates */
571 bo = p->bo_list->array[i].robj;
572 if (!bo)
573 continue;
574
575 bo_va = p->bo_list->array[i].bo_va;
576 if (bo_va == NULL)
577 continue;
578
579 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
580 if (r)
581 return r;
582
Christian König91e1a522015-07-06 22:06:40 +0200583 f = &bo_va->last_pt_update->base;
584 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
585 if (r)
586 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 }
588 }
589
monk.liucfe2c972015-05-26 15:01:54 +0800590 return amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591}
592
593static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
594 struct amdgpu_cs_parser *parser)
595{
596 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
597 struct amdgpu_vm *vm = &fpriv->vm;
598 struct amdgpu_ring *ring;
599 int i, r;
600
601 if (parser->num_ibs == 0)
602 return 0;
603
604 /* Only for UVD/VCE VM emulation */
605 for (i = 0; i < parser->num_ibs; i++) {
606 ring = parser->ibs[i].ring;
607 if (ring->funcs->parse_cs) {
608 r = amdgpu_ring_parse_cs(ring, parser, i);
609 if (r)
610 return r;
611 }
612 }
613
614 mutex_lock(&vm->mutex);
615 r = amdgpu_bo_vm_update_pte(parser, vm);
616 if (r) {
617 goto out;
618 }
619 amdgpu_cs_sync_rings(parser);
Chunming Zhou049fc522015-07-21 14:36:51 +0800620 if (!amdgpu_enable_scheduler)
621 r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
622 parser->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623
624out:
625 mutex_unlock(&vm->mutex);
626 return r;
627}
628
629static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
630{
631 if (r == -EDEADLK) {
632 r = amdgpu_gpu_reset(adev);
633 if (!r)
634 r = -EAGAIN;
635 }
636 return r;
637}
638
639static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
640 struct amdgpu_cs_parser *parser)
641{
642 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
643 struct amdgpu_vm *vm = &fpriv->vm;
644 int i, j;
645 int r;
646
647 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
648 struct amdgpu_cs_chunk *chunk;
649 struct amdgpu_ib *ib;
650 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652
653 chunk = &parser->chunks[i];
654 ib = &parser->ibs[j];
655 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
656
657 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
658 continue;
659
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
661 chunk_ib->ip_instance, chunk_ib->ring,
662 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200663 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665
666 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200667 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200668 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200669 uint64_t offset;
670 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200671
Christian König4802ce12015-06-10 17:20:11 +0200672 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
673 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200674 if (!aobj) {
675 DRM_ERROR("IB va_start is invalid\n");
676 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 }
678
Christian König4802ce12015-06-10 17:20:11 +0200679 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
680 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
681 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
682 return -EINVAL;
683 }
684
Marek Olšák3ccec532015-06-02 17:44:49 +0200685 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200686 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 return r;
689 }
690
Christian König4802ce12015-06-10 17:20:11 +0200691 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
692 kptr += chunk_ib->va_start - offset;
693
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
695 if (r) {
696 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 return r;
698 }
699
700 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
701 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 } else {
703 r = amdgpu_ib_get(ring, vm, 0, ib);
704 if (r) {
705 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 return r;
707 }
708
709 ib->gpu_addr = chunk_ib->va_start;
710 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711
Marek Olšák3ccec532015-06-02 17:44:49 +0200712 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800713 ib->flags = chunk_ib->flags;
Christian König3cb485f2015-05-11 15:34:59 +0200714 ib->ctx = parser->ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 j++;
716 }
717
718 if (!parser->num_ibs)
719 return 0;
720
721 /* add GDS resources to first IB */
722 if (parser->bo_list) {
723 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
724 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
725 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
726 struct amdgpu_ib *ib = &parser->ibs[0];
727
728 if (gds) {
729 ib->gds_base = amdgpu_bo_gpu_offset(gds);
730 ib->gds_size = amdgpu_bo_size(gds);
731 }
732 if (gws) {
733 ib->gws_base = amdgpu_bo_gpu_offset(gws);
734 ib->gws_size = amdgpu_bo_size(gws);
735 }
736 if (oa) {
737 ib->oa_base = amdgpu_bo_gpu_offset(oa);
738 ib->oa_size = amdgpu_bo_size(oa);
739 }
740 }
741
742 /* wrap the last IB with user fence */
743 if (parser->uf.bo) {
744 struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
745
746 /* UVD & VCE fw doesn't support user fences */
747 if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
748 ib->ring->type == AMDGPU_RING_TYPE_VCE)
749 return -EINVAL;
750
751 ib->user = &parser->uf;
752 }
753
754 return 0;
755}
756
Christian König2b48d322015-06-19 17:31:29 +0200757static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
758 struct amdgpu_cs_parser *p)
759{
Christian König76a1ea62015-07-06 19:42:10 +0200760 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200761 struct amdgpu_ib *ib;
762 int i, j, r;
763
764 if (!p->num_ibs)
765 return 0;
766
767 /* Add dependencies to first IB */
768 ib = &p->ibs[0];
769 for (i = 0; i < p->nchunks; ++i) {
770 struct drm_amdgpu_cs_chunk_dep *deps;
771 struct amdgpu_cs_chunk *chunk;
772 unsigned num_deps;
773
774 chunk = &p->chunks[i];
775
776 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
777 continue;
778
779 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
780 num_deps = chunk->length_dw * 4 /
781 sizeof(struct drm_amdgpu_cs_chunk_dep);
782
783 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200784 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200785 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200786 struct fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200787
788 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
789 deps[j].ip_instance,
790 deps[j].ring, &ring);
791 if (r)
792 return r;
793
Christian König76a1ea62015-07-06 19:42:10 +0200794 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
795 if (ctx == NULL)
796 return -EINVAL;
797
Christian König21c16bf2015-07-07 17:24:49 +0200798 fence = amdgpu_ctx_get_fence(ctx, ring,
799 deps[j].handle);
800 if (IS_ERR(fence)) {
801 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +0200802 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +0200803 return r;
Christian König21c16bf2015-07-07 17:24:49 +0200804
805 } else if (fence) {
806 r = amdgpu_sync_fence(adev, &ib->sync, fence);
807 fence_put(fence);
808 amdgpu_ctx_put(ctx);
809 if (r)
810 return r;
Christian König76a1ea62015-07-06 19:42:10 +0200811 }
Christian König2b48d322015-06-19 17:31:29 +0200812 }
813 }
814
815 return 0;
816}
817
Chunming Zhou049fc522015-07-21 14:36:51 +0800818static int amdgpu_cs_parser_prepare_job(struct amdgpu_cs_parser *sched_job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819{
Chunming Zhou049fc522015-07-21 14:36:51 +0800820 int r, i;
821 struct amdgpu_cs_parser *parser = sched_job;
822 struct amdgpu_device *adev = sched_job->adev;
823 bool reserved_buffers = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400824
Chunming Zhou049fc522015-07-21 14:36:51 +0800825 r = amdgpu_cs_parser_relocs(parser);
826 if (r) {
827 if (r != -ERESTARTSYS) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200828 if (r == -ENOMEM)
829 DRM_ERROR("Not enough memory for command submission!\n");
830 else
831 DRM_ERROR("Failed to process the buffer list %d!\n", r);
832 }
Christian König2b48d322015-06-19 17:31:29 +0200833 }
834
835 if (!r) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200836 reserved_buffers = true;
Chunming Zhou049fc522015-07-21 14:36:51 +0800837 r = amdgpu_cs_ib_fill(adev, parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838 }
Christian König21c16bf2015-07-07 17:24:49 +0200839 if (!r) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800840 r = amdgpu_cs_dependencies(adev, parser);
Christian König21c16bf2015-07-07 17:24:49 +0200841 if (r)
842 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
843 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800844 if (r) {
845 amdgpu_cs_parser_fini(parser, r, reserved_buffers);
846 return r;
847 }
Christian König2b48d322015-06-19 17:31:29 +0200848
Chunming Zhou049fc522015-07-21 14:36:51 +0800849 for (i = 0; i < parser->num_ibs; i++)
850 trace_amdgpu_cs(parser, i);
851
852 r = amdgpu_cs_ib_vm_chunk(adev, parser);
853 return r;
854}
855
856static struct amdgpu_ring *amdgpu_cs_parser_get_ring(
857 struct amdgpu_device *adev,
858 struct amdgpu_cs_parser *parser)
859{
860 int i, r;
861
862 struct amdgpu_cs_chunk *chunk;
863 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
864 struct amdgpu_ring *ring;
865 for (i = 0; i < parser->nchunks; i++) {
866 chunk = &parser->chunks[i];
867 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
868
869 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
870 continue;
871
872 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
873 chunk_ib->ip_instance, chunk_ib->ring,
874 &ring);
875 if (r)
876 return NULL;
877 break;
878 }
879 return ring;
880}
881
882int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
883{
884 struct amdgpu_device *adev = dev->dev_private;
885 union drm_amdgpu_cs *cs = data;
886 struct amdgpu_cs_parser *parser;
887 int r;
888
889 down_read(&adev->exclusive_lock);
890 if (!adev->accel_working) {
891 up_read(&adev->exclusive_lock);
892 return -EBUSY;
893 }
894
895 parser = amdgpu_cs_parser_create(adev, filp, NULL, NULL, 0);
896 if (!parser)
897 return -ENOMEM;
898 r = amdgpu_cs_parser_init(parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800900 DRM_ERROR("Failed to initialize parser !\n");
901 amdgpu_cs_parser_fini(parser, r, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400902 up_read(&adev->exclusive_lock);
903 r = amdgpu_cs_handle_lockup(adev, r);
904 return r;
905 }
906
Chunming Zhou049fc522015-07-21 14:36:51 +0800907 if (amdgpu_enable_scheduler && parser->num_ibs) {
908 struct amdgpu_ring * ring =
909 amdgpu_cs_parser_get_ring(adev, parser);
910 parser->uf.sequence = atomic64_inc_return(
911 &parser->ctx->rings[ring->idx].c_entity.last_queued_v_seq);
912 if ((parser->bo_list && parser->bo_list->has_userptr)) {
913 r = amdgpu_cs_parser_prepare_job(parser);
914 if (r)
915 goto out;
916 } else
917 parser->prepare_job = amdgpu_cs_parser_prepare_job;
Chunming Zhou4b559c92015-07-21 15:53:04 +0800918 parser->ring = ring;
Chunming Zhou049fc522015-07-21 14:36:51 +0800919 parser->run_job = amdgpu_cs_parser_run_job;
920 parser->free_job = amdgpu_cs_parser_free_job;
921 amd_sched_push_job(ring->scheduler,
922 &parser->ctx->rings[ring->idx].c_entity,
923 parser);
924 cs->out.handle = parser->uf.sequence;
925 up_read(&adev->exclusive_lock);
926 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800928 r = amdgpu_cs_parser_prepare_job(parser);
929 if (r)
930 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931
Chunming Zhou049fc522015-07-21 14:36:51 +0800932 cs->out.handle = parser->ibs[parser->num_ibs - 1].sequence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933out:
Chunming Zhou049fc522015-07-21 14:36:51 +0800934 amdgpu_cs_parser_fini(parser, r, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 up_read(&adev->exclusive_lock);
936 r = amdgpu_cs_handle_lockup(adev, r);
937 return r;
938}
939
940/**
941 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
942 *
943 * @dev: drm device
944 * @data: data from userspace
945 * @filp: file private
946 *
947 * Wait for the command submission identified by handle to finish.
948 */
949int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
950 struct drm_file *filp)
951{
952 union drm_amdgpu_wait_cs *wait = data;
953 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +0200955 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800956 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200957 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958 long r;
959
Christian König21c16bf2015-07-07 17:24:49 +0200960 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
961 wait->in.ring, &ring);
962 if (r)
963 return r;
964
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800965 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
966 if (ctx == NULL)
967 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +0800968
969 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
970 if (IS_ERR(fence))
971 r = PTR_ERR(fence);
972 else if (fence) {
973 r = fence_wait_timeout(fence, true, timeout);
974 fence_put(fence);
975 } else
Christian König21c16bf2015-07-07 17:24:49 +0200976 r = 1;
977
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800978 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 if (r < 0)
980 return r;
981
982 memset(wait, 0, sizeof(*wait));
983 wait->out.status = (r == 0);
984
985 return 0;
986}
987
988/**
989 * amdgpu_cs_find_bo_va - find bo_va for VM address
990 *
991 * @parser: command submission parser context
992 * @addr: VM address
993 * @bo: resulting BO of the mapping found
994 *
995 * Search the buffer objects in the command submission context for a certain
996 * virtual memory address. Returns allocation structure when found, NULL
997 * otherwise.
998 */
999struct amdgpu_bo_va_mapping *
1000amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1001 uint64_t addr, struct amdgpu_bo **bo)
1002{
1003 struct amdgpu_bo_list_entry *reloc;
1004 struct amdgpu_bo_va_mapping *mapping;
1005
1006 addr /= AMDGPU_GPU_PAGE_SIZE;
1007
1008 list_for_each_entry(reloc, &parser->validated, tv.head) {
1009 if (!reloc->bo_va)
1010 continue;
1011
1012 list_for_each_entry(mapping, &reloc->bo_va->mappings, list) {
1013 if (mapping->it.start > addr ||
1014 addr > mapping->it.last)
1015 continue;
1016
1017 *bo = reloc->bo_va->bo;
1018 return mapping;
1019 }
1020 }
1021
1022 return NULL;
1023}