blob: fe81b46266d9c6d3029f41159820524828e45b36 [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);
Chunming Zhouafe10082015-07-28 16:11:52 +0800140 if (sched_job->free_job)
141 sched_job->free_job(sched_job);
Chunming Zhou049fc522015-07-21 14:36:51 +0800142 mutex_unlock(&sched_job->job_lock);
143 /* after processing job, free memory */
144 kfree(sched_job);
145}
146struct amdgpu_cs_parser *amdgpu_cs_parser_create(struct amdgpu_device *adev,
147 struct drm_file *filp,
148 struct amdgpu_ctx *ctx,
149 struct amdgpu_ib *ibs,
150 uint32_t num_ibs)
151{
152 struct amdgpu_cs_parser *parser;
153 int i;
154
155 parser = kzalloc(sizeof(struct amdgpu_cs_parser), GFP_KERNEL);
156 if (!parser)
157 return NULL;
158
159 parser->adev = adev;
160 parser->filp = filp;
161 parser->ctx = ctx;
162 parser->ibs = ibs;
163 parser->num_ibs = num_ibs;
164 if (amdgpu_enable_scheduler) {
165 mutex_init(&parser->job_lock);
166 INIT_WORK(&parser->job_work, amdgpu_job_work_func);
167 }
168 for (i = 0; i < num_ibs; i++)
169 ibs[i].ctx = ctx;
170
171 return parser;
172}
173
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
175{
176 union drm_amdgpu_cs *cs = data;
177 uint64_t *chunk_array_user;
178 uint64_t *chunk_array = NULL;
179 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +0800180 struct amdgpu_bo_list *bo_list = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181 unsigned size, i;
182 int r = 0;
183
184 if (!cs->in.num_chunks)
185 goto out;
186
Christian König3cb485f2015-05-11 15:34:59 +0200187 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
188 if (!p->ctx) {
189 r = -EINVAL;
190 goto out;
191 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800192 bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
193 if (bo_list && !bo_list->has_userptr) {
194 p->bo_list = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
195 if (!p->bo_list)
196 return -ENOMEM;
197 amdgpu_bo_list_copy(p->adev, p->bo_list, bo_list);
198 amdgpu_bo_list_put(bo_list);
199 } else if (bo_list && bo_list->has_userptr)
200 p->bo_list = bo_list;
201 else
202 p->bo_list = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400203
204 /* get chunks */
205 INIT_LIST_HEAD(&p->validated);
monk.liue60b3442015-07-17 18:39:25 +0800206 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 if (chunk_array == NULL) {
208 r = -ENOMEM;
209 goto out;
210 }
211
monk.liue60b3442015-07-17 18:39:25 +0800212 chunk_array_user = (uint64_t __user *)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400213 if (copy_from_user(chunk_array, chunk_array_user,
214 sizeof(uint64_t)*cs->in.num_chunks)) {
215 r = -EFAULT;
216 goto out;
217 }
218
219 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800220 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221 GFP_KERNEL);
222 if (p->chunks == NULL) {
223 r = -ENOMEM;
224 goto out;
225 }
226
227 for (i = 0; i < p->nchunks; i++) {
228 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
229 struct drm_amdgpu_cs_chunk user_chunk;
230 uint32_t __user *cdata;
231
monk.liue60b3442015-07-17 18:39:25 +0800232 chunk_ptr = (void __user *)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233 if (copy_from_user(&user_chunk, chunk_ptr,
234 sizeof(struct drm_amdgpu_cs_chunk))) {
235 r = -EFAULT;
236 goto out;
237 }
238 p->chunks[i].chunk_id = user_chunk.chunk_id;
239 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240
241 size = p->chunks[i].length_dw;
monk.liue60b3442015-07-17 18:39:25 +0800242 cdata = (void __user *)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243 p->chunks[i].user_ptr = cdata;
244
245 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
246 if (p->chunks[i].kdata == NULL) {
247 r = -ENOMEM;
248 goto out;
249 }
250 size *= sizeof(uint32_t);
251 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
252 r = -EFAULT;
253 goto out;
254 }
255
Christian König9a5e8fb2015-06-23 17:07:03 +0200256 switch (p->chunks[i].chunk_id) {
257 case AMDGPU_CHUNK_ID_IB:
258 p->num_ibs++;
259 break;
260
261 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
263 if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) {
264 uint32_t handle;
265 struct drm_gem_object *gobj;
266 struct drm_amdgpu_cs_chunk_fence *fence_data;
267
268 fence_data = (void *)p->chunks[i].kdata;
269 handle = fence_data->handle;
270 gobj = drm_gem_object_lookup(p->adev->ddev,
271 p->filp, handle);
272 if (gobj == NULL) {
273 r = -EINVAL;
274 goto out;
275 }
276
277 p->uf.bo = gem_to_amdgpu_bo(gobj);
278 p->uf.offset = fence_data->offset;
279 } else {
280 r = -EINVAL;
281 goto out;
282 }
Christian König9a5e8fb2015-06-23 17:07:03 +0200283 break;
284
Christian König2b48d322015-06-19 17:31:29 +0200285 case AMDGPU_CHUNK_ID_DEPENDENCIES:
286 break;
287
Christian König9a5e8fb2015-06-23 17:07:03 +0200288 default:
289 r = -EINVAL;
290 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400291 }
292 }
293
monk.liue60b3442015-07-17 18:39:25 +0800294
295 p->ibs = kmalloc_array(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
296 if (!p->ibs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297 r = -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299out:
300 kfree(chunk_array);
301 return r;
302}
303
304/* Returns how many bytes TTM can move per IB.
305 */
306static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
307{
308 u64 real_vram_size = adev->mc.real_vram_size;
309 u64 vram_usage = atomic64_read(&adev->vram_usage);
310
311 /* This function is based on the current VRAM usage.
312 *
313 * - If all of VRAM is free, allow relocating the number of bytes that
314 * is equal to 1/4 of the size of VRAM for this IB.
315
316 * - If more than one half of VRAM is occupied, only allow relocating
317 * 1 MB of data for this IB.
318 *
319 * - From 0 to one half of used VRAM, the threshold decreases
320 * linearly.
321 * __________________
322 * 1/4 of -|\ |
323 * VRAM | \ |
324 * | \ |
325 * | \ |
326 * | \ |
327 * | \ |
328 * | \ |
329 * | \________|1 MB
330 * |----------------|
331 * VRAM 0 % 100 %
332 * used used
333 *
334 * Note: It's a threshold, not a limit. The threshold must be crossed
335 * for buffer relocations to stop, so any buffer of an arbitrary size
336 * can be moved as long as the threshold isn't crossed before
337 * the relocation takes place. We don't want to disable buffer
338 * relocations completely.
339 *
340 * The idea is that buffers should be placed in VRAM at creation time
341 * and TTM should only do a minimum number of relocations during
342 * command submission. In practice, you need to submit at least
343 * a dozen IBs to move all buffers to VRAM if they are in GTT.
344 *
345 * Also, things can get pretty crazy under memory pressure and actual
346 * VRAM usage can change a lot, so playing safe even at 50% does
347 * consistently increase performance.
348 */
349
350 u64 half_vram = real_vram_size >> 1;
351 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
352 u64 bytes_moved_threshold = half_free_vram >> 1;
353 return max(bytes_moved_threshold, 1024*1024ull);
354}
355
356int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p)
357{
358 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
359 struct amdgpu_vm *vm = &fpriv->vm;
360 struct amdgpu_device *adev = p->adev;
361 struct amdgpu_bo_list_entry *lobj;
362 struct list_head duplicates;
363 struct amdgpu_bo *bo;
364 u64 bytes_moved = 0, initial_bytes_moved;
365 u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
366 int r;
367
368 INIT_LIST_HEAD(&duplicates);
369 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
370 if (unlikely(r != 0)) {
371 return r;
372 }
373
374 list_for_each_entry(lobj, &p->validated, tv.head) {
375 bo = lobj->robj;
376 if (!bo->pin_count) {
377 u32 domain = lobj->prefered_domains;
378 u32 current_domain =
379 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
380
381 /* Check if this buffer will be moved and don't move it
382 * if we have moved too many buffers for this IB already.
383 *
384 * Note that this allows moving at least one buffer of
385 * any size, because it doesn't take the current "bo"
386 * into account. We don't want to disallow buffer moves
387 * completely.
388 */
389 if (current_domain != AMDGPU_GEM_DOMAIN_CPU &&
390 (domain & current_domain) == 0 && /* will be moved */
391 bytes_moved > bytes_moved_threshold) {
392 /* don't move it */
393 domain = current_domain;
394 }
395
396 retry:
397 amdgpu_ttm_placement_from_domain(bo, domain);
398 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
399 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
400 bytes_moved += atomic64_read(&adev->num_bytes_moved) -
401 initial_bytes_moved;
402
403 if (unlikely(r)) {
404 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
405 domain = lobj->allowed_domains;
406 goto retry;
407 }
408 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
409 return r;
410 }
411 }
412 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
413 }
414 return 0;
415}
416
417static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
418{
419 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
420 struct amdgpu_cs_buckets buckets;
monk.liu840d5142015-04-27 15:19:20 +0800421 bool need_mmap_lock = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422 int i, r;
423
monk.liu840d5142015-04-27 15:19:20 +0800424 if (p->bo_list) {
425 need_mmap_lock = p->bo_list->has_userptr;
426 amdgpu_cs_buckets_init(&buckets);
427 for (i = 0; i < p->bo_list->num_entries; i++)
428 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
429 p->bo_list->array[i].priority);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430
monk.liu840d5142015-04-27 15:19:20 +0800431 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
432 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm,
435 &p->validated);
436
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 if (need_mmap_lock)
438 down_read(&current->mm->mmap_sem);
439
440 r = amdgpu_cs_list_validate(p);
441
442 if (need_mmap_lock)
443 up_read(&current->mm->mmap_sem);
444
445 return r;
446}
447
448static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
449{
450 struct amdgpu_bo_list_entry *e;
451 int r;
452
453 list_for_each_entry(e, &p->validated, tv.head) {
454 struct reservation_object *resv = e->robj->tbo.resv;
455 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
456
457 if (r)
458 return r;
459 }
460 return 0;
461}
462
463static int cmp_size_smaller_first(void *priv, struct list_head *a,
464 struct list_head *b)
465{
466 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
467 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
468
469 /* Sort A before B if A is smaller. */
470 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
471}
472
473/**
474 * cs_parser_fini() - clean parser states
475 * @parser: parser structure holding parsing context.
476 * @error: error number
477 *
478 * If error is set than unvalidate buffer, otherwise just free memory
479 * used by parsing context.
480 **/
481static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
482{
Chunming Zhou049fc522015-07-21 14:36:51 +0800483 amdgpu_cs_parser_fini_early(parser, error, backoff);
484 amdgpu_cs_parser_fini_late(parser);
485}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486
Chunming Zhou049fc522015-07-21 14:36:51 +0800487static int amdgpu_cs_parser_run_job(
488 struct amdgpu_cs_parser *sched_job)
489{
490 amdgpu_cs_parser_fini_early(sched_job, 0, true);
491 return 0;
492}
493
494static int amdgpu_cs_parser_free_job(
495 struct amdgpu_cs_parser *sched_job)
496{
497 amdgpu_cs_parser_fini_late(sched_job);
498 return 0;
499}
500
501static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser *parser, int error, bool backoff)
502{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 if (!error) {
504 /* Sort the buffer list from the smallest to largest buffer,
505 * which affects the order of buffers in the LRU list.
506 * This assures that the smallest buffers are added first
507 * to the LRU list, so they are likely to be later evicted
508 * first, instead of large buffers whose eviction is more
509 * expensive.
510 *
511 * This slightly lowers the number of bytes moved by TTM
512 * per frame under memory pressure.
513 */
514 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
515
516 ttm_eu_fence_buffer_objects(&parser->ticket,
517 &parser->validated,
518 &parser->ibs[parser->num_ibs-1].fence->base);
519 } else if (backoff) {
520 ttm_eu_backoff_reservation(&parser->ticket,
521 &parser->validated);
522 }
Chunming Zhou049fc522015-07-21 14:36:51 +0800523}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
Chunming Zhou049fc522015-07-21 14:36:51 +0800525static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser *parser)
526{
527 unsigned i;
Christian König3cb485f2015-05-11 15:34:59 +0200528 if (parser->ctx)
529 amdgpu_ctx_put(parser->ctx);
Chunming Zhou049fc522015-07-21 14:36:51 +0800530 if (parser->bo_list) {
531 if (!parser->bo_list->has_userptr)
532 amdgpu_bo_list_free(parser->bo_list);
533 else
534 amdgpu_bo_list_put(parser->bo_list);
535 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 drm_free_large(parser->vm_bos);
537 for (i = 0; i < parser->nchunks; i++)
538 drm_free_large(parser->chunks[i].kdata);
539 kfree(parser->chunks);
Christian Königb8682ac2015-06-22 14:54:32 +0200540 if (parser->ibs)
541 for (i = 0; i < parser->num_ibs; i++)
542 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 kfree(parser->ibs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 if (parser->uf.bo)
545 drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base);
Chunming Zhou049fc522015-07-21 14:36:51 +0800546
547 if (!amdgpu_enable_scheduler)
548 kfree(parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549}
550
551static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
552 struct amdgpu_vm *vm)
553{
554 struct amdgpu_device *adev = p->adev;
555 struct amdgpu_bo_va *bo_va;
556 struct amdgpu_bo *bo;
557 int i, r;
558
559 r = amdgpu_vm_update_page_directory(adev, vm);
560 if (r)
561 return r;
562
563 r = amdgpu_vm_clear_freed(adev, vm);
564 if (r)
565 return r;
566
567 if (p->bo_list) {
568 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200569 struct fence *f;
570
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571 /* ignore duplicates */
572 bo = p->bo_list->array[i].robj;
573 if (!bo)
574 continue;
575
576 bo_va = p->bo_list->array[i].bo_va;
577 if (bo_va == NULL)
578 continue;
579
580 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
581 if (r)
582 return r;
583
Christian König91e1a522015-07-06 22:06:40 +0200584 f = &bo_va->last_pt_update->base;
585 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
586 if (r)
587 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 }
589 }
590
monk.liucfe2c972015-05-26 15:01:54 +0800591 return amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592}
593
594static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
595 struct amdgpu_cs_parser *parser)
596{
597 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
598 struct amdgpu_vm *vm = &fpriv->vm;
599 struct amdgpu_ring *ring;
600 int i, r;
601
602 if (parser->num_ibs == 0)
603 return 0;
604
605 /* Only for UVD/VCE VM emulation */
606 for (i = 0; i < parser->num_ibs; i++) {
607 ring = parser->ibs[i].ring;
608 if (ring->funcs->parse_cs) {
609 r = amdgpu_ring_parse_cs(ring, parser, i);
610 if (r)
611 return r;
612 }
613 }
614
615 mutex_lock(&vm->mutex);
616 r = amdgpu_bo_vm_update_pte(parser, vm);
617 if (r) {
618 goto out;
619 }
620 amdgpu_cs_sync_rings(parser);
Chunming Zhou049fc522015-07-21 14:36:51 +0800621 if (!amdgpu_enable_scheduler)
622 r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs,
623 parser->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
625out:
626 mutex_unlock(&vm->mutex);
627 return r;
628}
629
630static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
631{
632 if (r == -EDEADLK) {
633 r = amdgpu_gpu_reset(adev);
634 if (!r)
635 r = -EAGAIN;
636 }
637 return r;
638}
639
640static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
641 struct amdgpu_cs_parser *parser)
642{
643 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
644 struct amdgpu_vm *vm = &fpriv->vm;
645 int i, j;
646 int r;
647
648 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
649 struct amdgpu_cs_chunk *chunk;
650 struct amdgpu_ib *ib;
651 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653
654 chunk = &parser->chunks[i];
655 ib = &parser->ibs[j];
656 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
657
658 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
659 continue;
660
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
662 chunk_ib->ip_instance, chunk_ib->ring,
663 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200664 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666
667 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200668 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200669 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200670 uint64_t offset;
671 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200672
Christian König4802ce12015-06-10 17:20:11 +0200673 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
674 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200675 if (!aobj) {
676 DRM_ERROR("IB va_start is invalid\n");
677 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678 }
679
Christian König4802ce12015-06-10 17:20:11 +0200680 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
681 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
682 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
683 return -EINVAL;
684 }
685
Marek Olšák3ccec532015-06-02 17:44:49 +0200686 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200687 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 return r;
690 }
691
Christian König4802ce12015-06-10 17:20:11 +0200692 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
693 kptr += chunk_ib->va_start - offset;
694
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
696 if (r) {
697 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 return r;
699 }
700
701 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
702 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 } else {
704 r = amdgpu_ib_get(ring, vm, 0, ib);
705 if (r) {
706 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 return r;
708 }
709
710 ib->gpu_addr = chunk_ib->va_start;
711 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712
Marek Olšák3ccec532015-06-02 17:44:49 +0200713 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800714 ib->flags = chunk_ib->flags;
Christian König3cb485f2015-05-11 15:34:59 +0200715 ib->ctx = parser->ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716 j++;
717 }
718
719 if (!parser->num_ibs)
720 return 0;
721
722 /* add GDS resources to first IB */
723 if (parser->bo_list) {
724 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
725 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
726 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
727 struct amdgpu_ib *ib = &parser->ibs[0];
728
729 if (gds) {
730 ib->gds_base = amdgpu_bo_gpu_offset(gds);
731 ib->gds_size = amdgpu_bo_size(gds);
732 }
733 if (gws) {
734 ib->gws_base = amdgpu_bo_gpu_offset(gws);
735 ib->gws_size = amdgpu_bo_size(gws);
736 }
737 if (oa) {
738 ib->oa_base = amdgpu_bo_gpu_offset(oa);
739 ib->oa_size = amdgpu_bo_size(oa);
740 }
741 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742 /* 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);
Chunming Zhoud1ff9082015-07-30 17:59:43 +0800910 parser->ibs[parser->num_ibs - 1].sequence = atomic64_inc_return(
Chunming Zhou049fc522015-07-21 14:36:51 +0800911 &parser->ctx->rings[ring->idx].c_entity.last_queued_v_seq);
Chunming Zhou4274f5d2015-07-21 16:04:39 +0800912 if (ring->is_pte_ring || (parser->bo_list && parser->bo_list->has_userptr)) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800913 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);
Chunming Zhoud1ff9082015-07-30 17:59:43 +0800924 cs->out.handle = parser->ibs[parser->num_ibs - 1].sequence;
Chunming Zhou049fc522015-07-21 14:36:51 +0800925 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
Christian König7fc11952015-07-30 11:53:42 +02001012 list_for_each_entry(mapping, &reloc->bo_va->valids, 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 list_for_each_entry(mapping, &reloc->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001022 if (mapping->it.start > addr ||
1023 addr > mapping->it.last)
1024 continue;
1025
1026 *bo = reloc->bo_va->bo;
1027 return mapping;
1028 }
1029 }
1030
1031 return NULL;
1032}