blob: 5986da26a49270b89b4195fcf7ca9464bb723e57 [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:
Alex Deucherc113ea12015-10-08 16:30:37 -0400107 if (ring < adev->sdma.num_instances) {
108 *out_ring = &adev->sdma.instance[ring].ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 } else {
Alex Deucherc113ea12015-10-08 16:30:37 -0400110 DRM_ERROR("only %d SDMA rings are supported\n",
111 adev->sdma.num_instances);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112 return -EINVAL;
113 }
114 break;
115 case AMDGPU_HW_IP_UVD:
116 *out_ring = &adev->uvd.ring;
117 break;
118 case AMDGPU_HW_IP_VCE:
119 if (ring < 2){
120 *out_ring = &adev->vce.ring[ring];
121 } else {
122 DRM_ERROR("only two VCE rings are supported\n");
123 return -EINVAL;
124 }
125 break;
126 }
127 return 0;
128}
129
Christian König91acbeb2015-12-14 16:42:31 +0100130static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
131 struct drm_amdgpu_cs_chunk_fence *fence_data)
132{
133 struct drm_gem_object *gobj;
134 uint32_t handle;
135
136 handle = fence_data->handle;
137 gobj = drm_gem_object_lookup(p->adev->ddev, p->filp,
138 fence_data->handle);
139 if (gobj == NULL)
140 return -EINVAL;
141
142 p->uf.bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
143 p->uf.offset = fence_data->offset;
144
145 if (amdgpu_ttm_tt_has_userptr(p->uf.bo->tbo.ttm)) {
146 drm_gem_object_unreference_unlocked(gobj);
147 return -EINVAL;
148 }
149
150 p->uf_entry.robj = amdgpu_bo_ref(p->uf.bo);
151 p->uf_entry.prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
152 p->uf_entry.allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
153 p->uf_entry.priority = 0;
154 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
155 p->uf_entry.tv.shared = true;
156
157 drm_gem_object_unreference_unlocked(gobj);
158 return 0;
159}
160
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
162{
163 union drm_amdgpu_cs *cs = data;
164 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +0300165 uint64_t *chunk_array;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Dan Carpenter54313502015-09-25 14:36:55 +0300167 unsigned size;
168 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +0300169 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170
Dan Carpenter1d263472015-09-23 13:59:28 +0300171 if (cs->in.num_chunks == 0)
172 return 0;
173
174 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
175 if (!chunk_array)
176 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177
Christian König3cb485f2015-05-11 15:34:59 +0200178 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
179 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300180 ret = -EINVAL;
181 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +0200182 }
Dan Carpenter1d263472015-09-23 13:59:28 +0300183
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184 /* get chunks */
Arnd Bergmann028423b2015-10-07 09:41:27 +0200185 chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186 if (copy_from_user(chunk_array, chunk_array_user,
187 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300188 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +0100189 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190 }
191
192 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800193 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300195 if (!p->chunks) {
196 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100197 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 }
199
200 for (i = 0; i < p->nchunks; i++) {
201 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
202 struct drm_amdgpu_cs_chunk user_chunk;
203 uint32_t __user *cdata;
204
Arnd Bergmann028423b2015-10-07 09:41:27 +0200205 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 if (copy_from_user(&user_chunk, chunk_ptr,
207 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300208 ret = -EFAULT;
209 i--;
210 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 }
212 p->chunks[i].chunk_id = user_chunk.chunk_id;
213 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214
215 size = p->chunks[i].length_dw;
Arnd Bergmann028423b2015-10-07 09:41:27 +0200216 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400217
218 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
219 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300220 ret = -ENOMEM;
221 i--;
222 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223 }
224 size *= sizeof(uint32_t);
225 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300226 ret = -EFAULT;
227 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228 }
229
Christian König9a5e8fb2015-06-23 17:07:03 +0200230 switch (p->chunks[i].chunk_id) {
231 case AMDGPU_CHUNK_ID_IB:
232 p->num_ibs++;
233 break;
234
235 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100237 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300238 ret = -EINVAL;
239 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 }
Christian König91acbeb2015-12-14 16:42:31 +0100241
242 ret = amdgpu_cs_user_fence_chunk(p, (void *)p->chunks[i].kdata);
243 if (ret)
244 goto free_partial_kdata;
245
Christian König9a5e8fb2015-06-23 17:07:03 +0200246 break;
247
Christian König2b48d322015-06-19 17:31:29 +0200248 case AMDGPU_CHUNK_ID_DEPENDENCIES:
249 break;
250
Christian König9a5e8fb2015-06-23 17:07:03 +0200251 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300252 ret = -EINVAL;
253 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 }
255 }
256
monk.liue60b3442015-07-17 18:39:25 +0800257
Christian Königb203dd92015-08-18 18:23:16 +0200258 p->ibs = kcalloc(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300259 if (!p->ibs) {
260 ret = -ENOMEM;
261 goto free_all_kdata;
262 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400264 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300265 return 0;
266
267free_all_kdata:
268 i = p->nchunks - 1;
269free_partial_kdata:
270 for (; i >= 0; i--)
271 drm_free_large(p->chunks[i].kdata);
272 kfree(p->chunks);
Christian König2a7d9bd2015-12-18 20:33:52 +0100273put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300274 amdgpu_ctx_put(p->ctx);
275free_chunk:
276 kfree(chunk_array);
277
278 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279}
280
281/* Returns how many bytes TTM can move per IB.
282 */
283static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
284{
285 u64 real_vram_size = adev->mc.real_vram_size;
286 u64 vram_usage = atomic64_read(&adev->vram_usage);
287
288 /* This function is based on the current VRAM usage.
289 *
290 * - If all of VRAM is free, allow relocating the number of bytes that
291 * is equal to 1/4 of the size of VRAM for this IB.
292
293 * - If more than one half of VRAM is occupied, only allow relocating
294 * 1 MB of data for this IB.
295 *
296 * - From 0 to one half of used VRAM, the threshold decreases
297 * linearly.
298 * __________________
299 * 1/4 of -|\ |
300 * VRAM | \ |
301 * | \ |
302 * | \ |
303 * | \ |
304 * | \ |
305 * | \ |
306 * | \________|1 MB
307 * |----------------|
308 * VRAM 0 % 100 %
309 * used used
310 *
311 * Note: It's a threshold, not a limit. The threshold must be crossed
312 * for buffer relocations to stop, so any buffer of an arbitrary size
313 * can be moved as long as the threshold isn't crossed before
314 * the relocation takes place. We don't want to disable buffer
315 * relocations completely.
316 *
317 * The idea is that buffers should be placed in VRAM at creation time
318 * and TTM should only do a minimum number of relocations during
319 * command submission. In practice, you need to submit at least
320 * a dozen IBs to move all buffers to VRAM if they are in GTT.
321 *
322 * Also, things can get pretty crazy under memory pressure and actual
323 * VRAM usage can change a lot, so playing safe even at 50% does
324 * consistently increase performance.
325 */
326
327 u64 half_vram = real_vram_size >> 1;
328 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
329 u64 bytes_moved_threshold = half_free_vram >> 1;
330 return max(bytes_moved_threshold, 1024*1024ull);
331}
332
Christian Königf69f90a12015-12-21 19:47:42 +0100333int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200334 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400335{
Christian Königf69f90a12015-12-21 19:47:42 +0100336 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
337 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400338 struct amdgpu_bo_list_entry *lobj;
Christian Königf69f90a12015-12-21 19:47:42 +0100339 u64 initial_bytes_moved;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400340 int r;
341
Christian Königa5b75052015-09-03 16:40:39 +0200342 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100343 struct amdgpu_bo *bo = lobj->robj;
344 uint32_t domain;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
Christian König36409d122015-12-21 20:31:35 +0100347 if (bo->pin_count)
348 continue;
349
350 /* Avoid moving this one if we have moved too many buffers
351 * for this IB already.
352 *
353 * Note that this allows moving at least one buffer of
354 * any size, because it doesn't take the current "bo"
355 * into account. We don't want to disallow buffer moves
356 * completely.
357 */
358 if (p->bytes_moved <= p->bytes_moved_threshold)
359 domain = lobj->prefered_domains;
360 else
361 domain = lobj->allowed_domains;
362
363 retry:
364 amdgpu_ttm_placement_from_domain(bo, domain);
365 initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
366 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
367 p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
368 initial_bytes_moved;
369
370 if (unlikely(r)) {
371 if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
372 domain = lobj->allowed_domains;
373 goto retry;
374 }
375 return r;
376 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377 }
378 return 0;
379}
380
Christian König2a7d9bd2015-12-18 20:33:52 +0100381static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
382 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383{
384 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
385 struct amdgpu_cs_buckets buckets;
Christian Königa5b75052015-09-03 16:40:39 +0200386 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800387 bool need_mmap_lock = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388 int i, r;
389
Christian König2a7d9bd2015-12-18 20:33:52 +0100390 INIT_LIST_HEAD(&p->validated);
391
392 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800393 if (p->bo_list) {
394 need_mmap_lock = p->bo_list->has_userptr;
395 amdgpu_cs_buckets_init(&buckets);
396 for (i = 0; i < p->bo_list->num_entries; i++)
397 amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
Christian König2a7d9bd2015-12-18 20:33:52 +0100398 p->bo_list->array[i].priority);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400399
monk.liu840d5142015-04-27 15:19:20 +0800400 amdgpu_cs_buckets_get_list(&buckets, &p->validated);
401 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400402
Christian König3c0eea62015-12-11 14:39:05 +0100403 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100404 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405
Christian König91acbeb2015-12-14 16:42:31 +0100406 if (p->uf.bo)
407 list_add(&p->uf_entry.tv.head, &p->validated);
408
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409 if (need_mmap_lock)
410 down_read(&current->mm->mmap_sem);
411
Christian Königa5b75052015-09-03 16:40:39 +0200412 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
413 if (unlikely(r != 0))
414 goto error_reserve;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415
Christian Königee1782c2015-12-11 21:01:23 +0100416 amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100417
Christian Königf69f90a12015-12-21 19:47:42 +0100418 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
419 p->bytes_moved = 0;
420
421 r = amdgpu_cs_list_validate(p, &duplicates);
Christian Königa5b75052015-09-03 16:40:39 +0200422 if (r)
423 goto error_validate;
424
Christian Königf69f90a12015-12-21 19:47:42 +0100425 r = amdgpu_cs_list_validate(p, &p->validated);
Christian Königa5b75052015-09-03 16:40:39 +0200426
427error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100428 if (r) {
429 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200430 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100431 }
Christian Königa5b75052015-09-03 16:40:39 +0200432
433error_reserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 if (need_mmap_lock)
435 up_read(&current->mm->mmap_sem);
436
437 return r;
438}
439
440static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
441{
442 struct amdgpu_bo_list_entry *e;
443 int r;
444
445 list_for_each_entry(e, &p->validated, tv.head) {
446 struct reservation_object *resv = e->robj->tbo.resv;
447 r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
448
449 if (r)
450 return r;
451 }
452 return 0;
453}
454
455static int cmp_size_smaller_first(void *priv, struct list_head *a,
456 struct list_head *b)
457{
458 struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
459 struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
460
461 /* Sort A before B if A is smaller. */
462 return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
463}
464
Christian König984810f2015-11-14 21:05:35 +0100465/**
466 * cs_parser_fini() - clean parser states
467 * @parser: parser structure holding parsing context.
468 * @error: error number
469 *
470 * If error is set than unvalidate buffer, otherwise just free memory
471 * used by parsing context.
472 **/
473static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800474{
Christian Königeceb8a12016-01-11 15:35:21 +0100475 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100476 unsigned i;
477
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500479 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
480
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 /* Sort the buffer list from the smallest to largest buffer,
482 * which affects the order of buffers in the LRU list.
483 * This assures that the smallest buffers are added first
484 * to the LRU list, so they are likely to be later evicted
485 * first, instead of large buffers whose eviction is more
486 * expensive.
487 *
488 * This slightly lowers the number of bytes moved by TTM
489 * per frame under memory pressure.
490 */
491 list_sort(NULL, &parser->validated, cmp_size_smaller_first);
492
493 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100494 &parser->validated,
495 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496 } else if (backoff) {
497 ttm_eu_backoff_reservation(&parser->ticket,
498 &parser->validated);
499 }
Christian König984810f2015-11-14 21:05:35 +0100500 fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100501
Christian König3cb485f2015-05-11 15:34:59 +0200502 if (parser->ctx)
503 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800504 if (parser->bo_list)
505 amdgpu_bo_list_put(parser->bo_list);
506
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 for (i = 0; i < parser->nchunks; i++)
508 drm_free_large(parser->chunks[i].kdata);
509 kfree(parser->chunks);
Christian Könige4a58a22015-11-05 17:00:25 +0100510 if (parser->ibs)
511 for (i = 0; i < parser->num_ibs; i++)
512 amdgpu_ib_free(parser->adev, &parser->ibs[i]);
513 kfree(parser->ibs);
Christian König91acbeb2015-12-14 16:42:31 +0100514 amdgpu_bo_unref(&parser->uf.bo);
515 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400516}
517
518static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
519 struct amdgpu_vm *vm)
520{
521 struct amdgpu_device *adev = p->adev;
522 struct amdgpu_bo_va *bo_va;
523 struct amdgpu_bo *bo;
524 int i, r;
525
526 r = amdgpu_vm_update_page_directory(adev, vm);
527 if (r)
528 return r;
529
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200530 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, vm->page_directory_fence);
531 if (r)
532 return r;
533
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 r = amdgpu_vm_clear_freed(adev, vm);
535 if (r)
536 return r;
537
538 if (p->bo_list) {
539 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200540 struct fence *f;
541
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 /* ignore duplicates */
543 bo = p->bo_list->array[i].robj;
544 if (!bo)
545 continue;
546
547 bo_va = p->bo_list->array[i].bo_va;
548 if (bo_va == NULL)
549 continue;
550
551 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
552 if (r)
553 return r;
554
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800555 f = bo_va->last_pt_update;
Christian König91e1a522015-07-06 22:06:40 +0200556 r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
557 if (r)
558 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 }
Christian Königb495bd32015-09-10 14:00:35 +0200560
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 }
562
Christian Königb495bd32015-09-10 14:00:35 +0200563 r = amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
564
565 if (amdgpu_vm_debug && p->bo_list) {
566 /* Invalidate all BOs to test for userspace bugs */
567 for (i = 0; i < p->bo_list->num_entries; i++) {
568 /* ignore duplicates */
569 bo = p->bo_list->array[i].robj;
570 if (!bo)
571 continue;
572
573 amdgpu_vm_bo_invalidate(adev, bo);
574 }
575 }
576
577 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578}
579
580static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
581 struct amdgpu_cs_parser *parser)
582{
583 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
584 struct amdgpu_vm *vm = &fpriv->vm;
585 struct amdgpu_ring *ring;
586 int i, r;
587
588 if (parser->num_ibs == 0)
589 return 0;
590
591 /* Only for UVD/VCE VM emulation */
592 for (i = 0; i < parser->num_ibs; i++) {
593 ring = parser->ibs[i].ring;
594 if (ring->funcs->parse_cs) {
595 r = amdgpu_ring_parse_cs(ring, parser, i);
596 if (r)
597 return r;
598 }
599 }
600
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 r = amdgpu_bo_vm_update_pte(parser, vm);
Christian König984810f2015-11-14 21:05:35 +0100602 if (!r)
603 amdgpu_cs_sync_rings(parser);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605 return r;
606}
607
608static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
609{
610 if (r == -EDEADLK) {
611 r = amdgpu_gpu_reset(adev);
612 if (!r)
613 r = -EAGAIN;
614 }
615 return r;
616}
617
618static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
619 struct amdgpu_cs_parser *parser)
620{
621 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
622 struct amdgpu_vm *vm = &fpriv->vm;
623 int i, j;
624 int r;
625
626 for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
627 struct amdgpu_cs_chunk *chunk;
628 struct amdgpu_ib *ib;
629 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631
632 chunk = &parser->chunks[i];
633 ib = &parser->ibs[j];
634 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
635
636 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
637 continue;
638
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
640 chunk_ib->ip_instance, chunk_ib->ring,
641 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200642 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644
645 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200646 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200647 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200648 uint64_t offset;
649 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200650
Christian König4802ce12015-06-10 17:20:11 +0200651 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
652 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200653 if (!aobj) {
654 DRM_ERROR("IB va_start is invalid\n");
655 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 }
657
Christian König4802ce12015-06-10 17:20:11 +0200658 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
659 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
660 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
661 return -EINVAL;
662 }
663
Marek Olšák3ccec532015-06-02 17:44:49 +0200664 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200665 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 return r;
668 }
669
Christian König4802ce12015-06-10 17:20:11 +0200670 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
671 kptr += chunk_ib->va_start - offset;
672
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
674 if (r) {
675 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 return r;
677 }
678
679 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
680 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 } else {
682 r = amdgpu_ib_get(ring, vm, 0, ib);
683 if (r) {
684 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 return r;
686 }
687
688 ib->gpu_addr = chunk_ib->va_start;
689 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690
Marek Olšák3ccec532015-06-02 17:44:49 +0200691 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800692 ib->flags = chunk_ib->flags;
Christian König3cb485f2015-05-11 15:34:59 +0200693 ib->ctx = parser->ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 j++;
695 }
696
697 if (!parser->num_ibs)
698 return 0;
699
700 /* add GDS resources to first IB */
701 if (parser->bo_list) {
702 struct amdgpu_bo *gds = parser->bo_list->gds_obj;
703 struct amdgpu_bo *gws = parser->bo_list->gws_obj;
704 struct amdgpu_bo *oa = parser->bo_list->oa_obj;
705 struct amdgpu_ib *ib = &parser->ibs[0];
706
707 if (gds) {
708 ib->gds_base = amdgpu_bo_gpu_offset(gds);
709 ib->gds_size = amdgpu_bo_size(gds);
710 }
711 if (gws) {
712 ib->gws_base = amdgpu_bo_gpu_offset(gws);
713 ib->gws_size = amdgpu_bo_size(gws);
714 }
715 if (oa) {
716 ib->oa_base = amdgpu_bo_gpu_offset(oa);
717 ib->oa_size = amdgpu_bo_size(oa);
718 }
719 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 /* wrap the last IB with user fence */
721 if (parser->uf.bo) {
722 struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
723
724 /* UVD & VCE fw doesn't support user fences */
725 if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
726 ib->ring->type == AMDGPU_RING_TYPE_VCE)
727 return -EINVAL;
728
729 ib->user = &parser->uf;
730 }
731
732 return 0;
733}
734
Christian König2b48d322015-06-19 17:31:29 +0200735static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
736 struct amdgpu_cs_parser *p)
737{
Christian König76a1ea62015-07-06 19:42:10 +0200738 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200739 struct amdgpu_ib *ib;
740 int i, j, r;
741
742 if (!p->num_ibs)
743 return 0;
744
745 /* Add dependencies to first IB */
746 ib = &p->ibs[0];
747 for (i = 0; i < p->nchunks; ++i) {
748 struct drm_amdgpu_cs_chunk_dep *deps;
749 struct amdgpu_cs_chunk *chunk;
750 unsigned num_deps;
751
752 chunk = &p->chunks[i];
753
754 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
755 continue;
756
757 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
758 num_deps = chunk->length_dw * 4 /
759 sizeof(struct drm_amdgpu_cs_chunk_dep);
760
761 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200762 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200763 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200764 struct fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200765
766 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
767 deps[j].ip_instance,
768 deps[j].ring, &ring);
769 if (r)
770 return r;
771
Christian König76a1ea62015-07-06 19:42:10 +0200772 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
773 if (ctx == NULL)
774 return -EINVAL;
775
Christian König21c16bf2015-07-07 17:24:49 +0200776 fence = amdgpu_ctx_get_fence(ctx, ring,
777 deps[j].handle);
778 if (IS_ERR(fence)) {
779 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +0200780 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +0200781 return r;
Christian König21c16bf2015-07-07 17:24:49 +0200782
783 } else if (fence) {
784 r = amdgpu_sync_fence(adev, &ib->sync, fence);
785 fence_put(fence);
786 amdgpu_ctx_put(ctx);
787 if (r)
788 return r;
Christian König76a1ea62015-07-06 19:42:10 +0200789 }
Christian König2b48d322015-06-19 17:31:29 +0200790 }
791 }
792
793 return 0;
794}
795
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800796static int amdgpu_cs_free_job(struct amdgpu_job *job)
Chunming Zhoubb977d32015-08-18 15:16:40 +0800797{
798 int i;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800799 if (job->ibs)
800 for (i = 0; i < job->num_ibs; i++)
801 amdgpu_ib_free(job->adev, &job->ibs[i]);
802 kfree(job->ibs);
803 if (job->uf.bo)
Christian Königf3f17692015-12-03 19:55:52 +0100804 amdgpu_bo_unref(&job->uf.bo);
Chunming Zhoubb977d32015-08-18 15:16:40 +0800805 return 0;
806}
807
Chunming Zhou049fc522015-07-21 14:36:51 +0800808int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
809{
810 struct amdgpu_device *adev = dev->dev_private;
811 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +0100812 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +0200813 bool reserved_buffers = false;
814 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +0800815
Christian König0c418f12015-09-01 15:13:53 +0200816 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +0800817 return -EBUSY;
Chunming Zhou049fc522015-07-21 14:36:51 +0800818
Christian König7e52a812015-11-04 15:44:39 +0100819 parser.adev = adev;
820 parser.filp = filp;
821
822 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800824 DRM_ERROR("Failed to initialize parser !\n");
Christian König7e52a812015-11-04 15:44:39 +0100825 amdgpu_cs_parser_fini(&parser, r, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826 r = amdgpu_cs_handle_lockup(adev, r);
827 return r;
828 }
Christian König2a7d9bd2015-12-18 20:33:52 +0100829 r = amdgpu_cs_parser_bos(&parser, data);
Christian König26a69802015-08-18 21:09:33 +0200830 if (r == -ENOMEM)
831 DRM_ERROR("Not enough memory for command submission!\n");
832 else if (r && r != -ERESTARTSYS)
833 DRM_ERROR("Failed to process the buffer list %d!\n", r);
834 else if (!r) {
835 reserved_buffers = true;
Christian König7e52a812015-11-04 15:44:39 +0100836 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +0200837 }
838
839 if (!r) {
Christian König7e52a812015-11-04 15:44:39 +0100840 r = amdgpu_cs_dependencies(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +0200841 if (r)
842 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
843 }
844
845 if (r)
846 goto out;
847
Christian König7e52a812015-11-04 15:44:39 +0100848 for (i = 0; i < parser.num_ibs; i++)
849 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +0200850
Christian König7e52a812015-11-04 15:44:39 +0100851 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +0800852 if (r)
853 goto out;
854
Christian König7e52a812015-11-04 15:44:39 +0100855 if (amdgpu_enable_scheduler && parser.num_ibs) {
Christian König7e52a812015-11-04 15:44:39 +0100856 struct amdgpu_ring * ring = parser.ibs->ring;
Christian Könige2840222015-11-05 19:49:48 +0100857 struct amd_sched_fence *fence;
858 struct amdgpu_job *job;
Christian König7e52a812015-11-04 15:44:39 +0100859
Chunming Zhoubb977d32015-08-18 15:16:40 +0800860 job = kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
Dan Carpenter4cfdcd92015-11-04 16:25:09 +0300861 if (!job) {
862 r = -ENOMEM;
863 goto out;
864 }
Christian König7e52a812015-11-04 15:44:39 +0100865
Christian König4f839a22015-09-08 20:22:31 +0200866 job->base.sched = &ring->sched;
Christian König7e52a812015-11-04 15:44:39 +0100867 job->base.s_entity = &parser.ctx->rings[ring->idx].entity;
868 job->adev = parser.adev;
Christian Könige2840222015-11-05 19:49:48 +0100869 job->owner = parser.filp;
870 job->free_job = amdgpu_cs_free_job;
871
Christian König5d827302015-11-13 13:04:50 +0100872 job->ibs = parser.ibs;
873 job->num_ibs = parser.num_ibs;
874 parser.ibs = NULL;
875 parser.num_ibs = 0;
876
Chunming Zhoubb977d32015-08-18 15:16:40 +0800877 if (job->ibs[job->num_ibs - 1].user) {
Christian König7e52a812015-11-04 15:44:39 +0100878 job->uf = parser.uf;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800879 job->ibs[job->num_ibs - 1].user = &job->uf;
Christian König7e52a812015-11-04 15:44:39 +0100880 parser.uf.bo = NULL;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800881 }
882
Christian Könige2840222015-11-05 19:49:48 +0100883 fence = amd_sched_fence_create(job->base.s_entity,
884 parser.filp);
885 if (!fence) {
886 r = -ENOMEM;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800887 amdgpu_cs_free_job(job);
888 kfree(job);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800889 goto out;
890 }
Christian Könige2840222015-11-05 19:49:48 +0100891 job->base.s_fence = fence;
Christian König984810f2015-11-14 21:05:35 +0100892 parser.fence = fence_get(&fence->base);
Christian Könige2840222015-11-05 19:49:48 +0100893
894 cs->out.handle = amdgpu_ctx_add_fence(parser.ctx, ring,
895 &fence->base);
Christian Könige4a58a22015-11-05 17:00:25 +0100896 job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
Christian Königeb98d1c2015-08-20 17:28:36 +0200897
Chunming Zhou7034dec2015-11-11 14:56:00 +0800898 trace_amdgpu_cs_ioctl(job);
Christian Könige2840222015-11-05 19:49:48 +0100899 amd_sched_entity_push_job(&job->base);
900
Christian König984810f2015-11-14 21:05:35 +0100901 } else {
902 struct amdgpu_fence *fence;
Christian Könige2840222015-11-05 19:49:48 +0100903
Christian König984810f2015-11-14 21:05:35 +0100904 r = amdgpu_ib_schedule(adev, parser.num_ibs, parser.ibs,
905 parser.filp);
906 fence = parser.ibs[parser.num_ibs - 1].fence;
907 parser.fence = fence_get(&fence->base);
908 cs->out.handle = parser.ibs[parser.num_ibs - 1].sequence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400909 }
910
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400911out:
Christian König7e52a812015-11-04 15:44:39 +0100912 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 r = amdgpu_cs_handle_lockup(adev, r);
914 return r;
915}
916
917/**
918 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
919 *
920 * @dev: drm device
921 * @data: data from userspace
922 * @filp: file private
923 *
924 * Wait for the command submission identified by handle to finish.
925 */
926int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
927 struct drm_file *filp)
928{
929 union drm_amdgpu_wait_cs *wait = data;
930 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +0200932 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800933 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200934 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 long r;
936
Christian König21c16bf2015-07-07 17:24:49 +0200937 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
938 wait->in.ring, &ring);
939 if (r)
940 return r;
941
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800942 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
943 if (ctx == NULL)
944 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +0800945
946 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
947 if (IS_ERR(fence))
948 r = PTR_ERR(fence);
949 else if (fence) {
950 r = fence_wait_timeout(fence, true, timeout);
951 fence_put(fence);
952 } else
Christian König21c16bf2015-07-07 17:24:49 +0200953 r = 1;
954
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800955 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 if (r < 0)
957 return r;
958
959 memset(wait, 0, sizeof(*wait));
960 wait->out.status = (r == 0);
961
962 return 0;
963}
964
965/**
966 * amdgpu_cs_find_bo_va - find bo_va for VM address
967 *
968 * @parser: command submission parser context
969 * @addr: VM address
970 * @bo: resulting BO of the mapping found
971 *
972 * Search the buffer objects in the command submission context for a certain
973 * virtual memory address. Returns allocation structure when found, NULL
974 * otherwise.
975 */
976struct amdgpu_bo_va_mapping *
977amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
978 uint64_t addr, struct amdgpu_bo **bo)
979{
980 struct amdgpu_bo_list_entry *reloc;
981 struct amdgpu_bo_va_mapping *mapping;
982
983 addr /= AMDGPU_GPU_PAGE_SIZE;
984
985 list_for_each_entry(reloc, &parser->validated, tv.head) {
986 if (!reloc->bo_va)
987 continue;
988
Christian König7fc11952015-07-30 11:53:42 +0200989 list_for_each_entry(mapping, &reloc->bo_va->valids, list) {
990 if (mapping->it.start > addr ||
991 addr > mapping->it.last)
992 continue;
993
994 *bo = reloc->bo_va->bo;
995 return mapping;
996 }
997
998 list_for_each_entry(mapping, &reloc->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999 if (mapping->it.start > addr ||
1000 addr > mapping->it.last)
1001 continue;
1002
1003 *bo = reloc->bo_va->bo;
1004 return mapping;
1005 }
1006 }
1007
1008 return NULL;
1009}