blob: 2bbeeb07c187e0977ed88feb405106aad178c939 [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 */
Stephen Rothwell568d7c72016-03-17 15:30:49 +110027#include <linux/pagemap.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040028#include <drm/drmP.h>
29#include <drm/amdgpu_drm.h>
30#include "amdgpu.h"
31#include "amdgpu_trace.h"
32
Alex Deucherd38ceaf2015-04-20 16:55:21 -040033int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
34 u32 ip_instance, u32 ring,
35 struct amdgpu_ring **out_ring)
36{
37 /* Right now all IPs have only one instance - multiple rings. */
38 if (ip_instance != 0) {
39 DRM_ERROR("invalid ip instance: %d\n", ip_instance);
40 return -EINVAL;
41 }
42
43 switch (ip_type) {
44 default:
45 DRM_ERROR("unknown ip type: %d\n", ip_type);
46 return -EINVAL;
47 case AMDGPU_HW_IP_GFX:
48 if (ring < adev->gfx.num_gfx_rings) {
49 *out_ring = &adev->gfx.gfx_ring[ring];
50 } else {
51 DRM_ERROR("only %d gfx rings are supported now\n",
52 adev->gfx.num_gfx_rings);
53 return -EINVAL;
54 }
55 break;
56 case AMDGPU_HW_IP_COMPUTE:
57 if (ring < adev->gfx.num_compute_rings) {
58 *out_ring = &adev->gfx.compute_ring[ring];
59 } else {
60 DRM_ERROR("only %d compute rings are supported now\n",
61 adev->gfx.num_compute_rings);
62 return -EINVAL;
63 }
64 break;
65 case AMDGPU_HW_IP_DMA:
Alex Deucherc113ea12015-10-08 16:30:37 -040066 if (ring < adev->sdma.num_instances) {
67 *out_ring = &adev->sdma.instance[ring].ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068 } else {
Alex Deucherc113ea12015-10-08 16:30:37 -040069 DRM_ERROR("only %d SDMA rings are supported\n",
70 adev->sdma.num_instances);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040071 return -EINVAL;
72 }
73 break;
74 case AMDGPU_HW_IP_UVD:
75 *out_ring = &adev->uvd.ring;
76 break;
77 case AMDGPU_HW_IP_VCE:
78 if (ring < 2){
79 *out_ring = &adev->vce.ring[ring];
80 } else {
81 DRM_ERROR("only two VCE rings are supported\n");
82 return -EINVAL;
83 }
84 break;
85 }
86 return 0;
87}
88
Christian König91acbeb2015-12-14 16:42:31 +010089static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +020090 struct drm_amdgpu_cs_chunk_fence *data,
91 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +010092{
93 struct drm_gem_object *gobj;
Christian König91acbeb2015-12-14 16:42:31 +010094
Christian König91acbeb2015-12-14 16:42:31 +010095 gobj = drm_gem_object_lookup(p->adev->ddev, p->filp,
Christian König758ac172016-05-06 22:14:00 +020096 data->handle);
Christian König91acbeb2015-12-14 16:42:31 +010097 if (gobj == NULL)
98 return -EINVAL;
99
Christian König758ac172016-05-06 22:14:00 +0200100 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +0100101 p->uf_entry.priority = 0;
102 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
103 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100104 p->uf_entry.user_pages = NULL;
Christian König758ac172016-05-06 22:14:00 +0200105 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +0100106
107 drm_gem_object_unreference_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +0200108
109 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
110 amdgpu_bo_unref(&p->uf_entry.robj);
111 return -EINVAL;
112 }
113
Christian König91acbeb2015-12-14 16:42:31 +0100114 return 0;
115}
116
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
118{
Christian König4c0b2422016-02-01 11:20:37 +0100119 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +0800120 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121 union drm_amdgpu_cs *cs = data;
122 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +0300123 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +0100124 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +0200125 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +0300126 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +0300127 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128
Dan Carpenter1d263472015-09-23 13:59:28 +0300129 if (cs->in.num_chunks == 0)
130 return 0;
131
132 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
133 if (!chunk_array)
134 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135
Christian König3cb485f2015-05-11 15:34:59 +0200136 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
137 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300138 ret = -EINVAL;
139 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +0200140 }
Dan Carpenter1d263472015-09-23 13:59:28 +0300141
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 /* get chunks */
Arnd Bergmann028423b2015-10-07 09:41:27 +0200143 chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 if (copy_from_user(chunk_array, chunk_array_user,
145 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300146 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +0100147 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148 }
149
150 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800151 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300153 if (!p->chunks) {
154 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100155 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 }
157
158 for (i = 0; i < p->nchunks; i++) {
159 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
160 struct drm_amdgpu_cs_chunk user_chunk;
161 uint32_t __user *cdata;
162
Arnd Bergmann028423b2015-10-07 09:41:27 +0200163 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 if (copy_from_user(&user_chunk, chunk_ptr,
165 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300166 ret = -EFAULT;
167 i--;
168 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 }
170 p->chunks[i].chunk_id = user_chunk.chunk_id;
171 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172
173 size = p->chunks[i].length_dw;
Arnd Bergmann028423b2015-10-07 09:41:27 +0200174 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175
176 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
177 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300178 ret = -ENOMEM;
179 i--;
180 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181 }
182 size *= sizeof(uint32_t);
183 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300184 ret = -EFAULT;
185 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186 }
187
Christian König9a5e8fb2015-06-23 17:07:03 +0200188 switch (p->chunks[i].chunk_id) {
189 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100190 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200191 break;
192
193 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100195 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300196 ret = -EINVAL;
197 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 }
Christian König91acbeb2015-12-14 16:42:31 +0100199
Christian König758ac172016-05-06 22:14:00 +0200200 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
201 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100202 if (ret)
203 goto free_partial_kdata;
204
Christian König9a5e8fb2015-06-23 17:07:03 +0200205 break;
206
Christian König2b48d322015-06-19 17:31:29 +0200207 case AMDGPU_CHUNK_ID_DEPENDENCIES:
208 break;
209
Christian König9a5e8fb2015-06-23 17:07:03 +0200210 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300211 ret = -EINVAL;
212 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400213 }
214 }
215
Monk Liuc5637832016-04-19 20:11:32 +0800216 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100217 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100218 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219
Christian König758ac172016-05-06 22:14:00 +0200220 if (p->uf_entry.robj) {
221 p->job->uf_bo = amdgpu_bo_ref(p->uf_entry.robj);
222 p->job->uf_offset = uf_offset;
223 }
Christian König4c0b2422016-02-01 11:20:37 +0100224
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300226 return 0;
227
228free_all_kdata:
229 i = p->nchunks - 1;
230free_partial_kdata:
231 for (; i >= 0; i--)
232 drm_free_large(p->chunks[i].kdata);
233 kfree(p->chunks);
Christian König2a7d9bd2015-12-18 20:33:52 +0100234put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300235 amdgpu_ctx_put(p->ctx);
236free_chunk:
237 kfree(chunk_array);
238
239 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240}
241
242/* Returns how many bytes TTM can move per IB.
243 */
244static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
245{
246 u64 real_vram_size = adev->mc.real_vram_size;
247 u64 vram_usage = atomic64_read(&adev->vram_usage);
248
249 /* This function is based on the current VRAM usage.
250 *
251 * - If all of VRAM is free, allow relocating the number of bytes that
252 * is equal to 1/4 of the size of VRAM for this IB.
253
254 * - If more than one half of VRAM is occupied, only allow relocating
255 * 1 MB of data for this IB.
256 *
257 * - From 0 to one half of used VRAM, the threshold decreases
258 * linearly.
259 * __________________
260 * 1/4 of -|\ |
261 * VRAM | \ |
262 * | \ |
263 * | \ |
264 * | \ |
265 * | \ |
266 * | \ |
267 * | \________|1 MB
268 * |----------------|
269 * VRAM 0 % 100 %
270 * used used
271 *
272 * Note: It's a threshold, not a limit. The threshold must be crossed
273 * for buffer relocations to stop, so any buffer of an arbitrary size
274 * can be moved as long as the threshold isn't crossed before
275 * the relocation takes place. We don't want to disable buffer
276 * relocations completely.
277 *
278 * The idea is that buffers should be placed in VRAM at creation time
279 * and TTM should only do a minimum number of relocations during
280 * command submission. In practice, you need to submit at least
281 * a dozen IBs to move all buffers to VRAM if they are in GTT.
282 *
283 * Also, things can get pretty crazy under memory pressure and actual
284 * VRAM usage can change a lot, so playing safe even at 50% does
285 * consistently increase performance.
286 */
287
288 u64 half_vram = real_vram_size >> 1;
289 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
290 u64 bytes_moved_threshold = half_free_vram >> 1;
291 return max(bytes_moved_threshold, 1024*1024ull);
292}
293
Christian Königf69f90a12015-12-21 19:47:42 +0100294int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200295 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297 struct amdgpu_bo_list_entry *lobj;
Christian Königf69f90a12015-12-21 19:47:42 +0100298 u64 initial_bytes_moved;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299 int r;
300
Christian Königa5b75052015-09-03 16:40:39 +0200301 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100302 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100303 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100304 struct mm_struct *usermm;
Christian König36409d122015-12-21 20:31:35 +0100305 uint32_t domain;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306
Christian Königcc325d12016-02-08 11:08:35 +0100307 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
308 if (usermm && usermm != current->mm)
309 return -EPERM;
310
Christian König2f568db2016-02-23 12:36:59 +0100311 /* Check if we have user pages and nobody bound the BO already */
312 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
313 size_t size = sizeof(struct page *);
314
315 size *= bo->tbo.ttm->num_pages;
316 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
317 binding_userptr = true;
318 }
319
Christian König36409d122015-12-21 20:31:35 +0100320 if (bo->pin_count)
321 continue;
322
323 /* Avoid moving this one if we have moved too many buffers
324 * for this IB already.
325 *
326 * Note that this allows moving at least one buffer of
327 * any size, because it doesn't take the current "bo"
328 * into account. We don't want to disallow buffer moves
329 * completely.
330 */
331 if (p->bytes_moved <= p->bytes_moved_threshold)
Christian König1ea863f2015-12-18 22:13:12 +0100332 domain = bo->prefered_domains;
Christian König36409d122015-12-21 20:31:35 +0100333 else
Christian König1ea863f2015-12-18 22:13:12 +0100334 domain = bo->allowed_domains;
Christian König36409d122015-12-21 20:31:35 +0100335
336 retry:
337 amdgpu_ttm_placement_from_domain(bo, domain);
338 initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
339 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
340 p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
341 initial_bytes_moved;
342
343 if (unlikely(r)) {
Christian König1ea863f2015-12-18 22:13:12 +0100344 if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
345 domain = bo->allowed_domains;
Christian König36409d122015-12-21 20:31:35 +0100346 goto retry;
347 }
348 return r;
349 }
Christian König2f568db2016-02-23 12:36:59 +0100350
351 if (binding_userptr) {
352 drm_free_large(lobj->user_pages);
353 lobj->user_pages = NULL;
354 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355 }
356 return 0;
357}
358
Christian König2a7d9bd2015-12-18 20:33:52 +0100359static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
360 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400361{
362 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100363 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200364 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800365 bool need_mmap_lock = false;
Christian König2f568db2016-02-23 12:36:59 +0100366 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100367 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400368
Christian König2a7d9bd2015-12-18 20:33:52 +0100369 INIT_LIST_HEAD(&p->validated);
370
371 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800372 if (p->bo_list) {
Christian König211dff52016-02-22 15:40:59 +0100373 need_mmap_lock = p->bo_list->first_userptr !=
374 p->bo_list->num_entries;
Christian König636ce252015-12-18 21:26:47 +0100375 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
monk.liu840d5142015-04-27 15:19:20 +0800376 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377
Christian König3c0eea62015-12-11 14:39:05 +0100378 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100379 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380
Christian König758ac172016-05-06 22:14:00 +0200381 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100382 list_add(&p->uf_entry.tv.head, &p->validated);
383
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400384 if (need_mmap_lock)
385 down_read(&current->mm->mmap_sem);
386
Christian König2f568db2016-02-23 12:36:59 +0100387 while (1) {
388 struct list_head need_pages;
389 unsigned i;
390
391 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
392 &duplicates);
393 if (unlikely(r != 0))
394 goto error_free_pages;
395
396 /* Without a BO list we don't have userptr BOs */
397 if (!p->bo_list)
398 break;
399
400 INIT_LIST_HEAD(&need_pages);
401 for (i = p->bo_list->first_userptr;
402 i < p->bo_list->num_entries; ++i) {
403
404 e = &p->bo_list->array[i];
405
406 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
407 &e->user_invalidated) && e->user_pages) {
408
409 /* We acquired a page array, but somebody
410 * invalidated it. Free it an try again
411 */
412 release_pages(e->user_pages,
413 e->robj->tbo.ttm->num_pages,
414 false);
415 drm_free_large(e->user_pages);
416 e->user_pages = NULL;
417 }
418
419 if (e->robj->tbo.ttm->state != tt_bound &&
420 !e->user_pages) {
421 list_del(&e->tv.head);
422 list_add(&e->tv.head, &need_pages);
423
424 amdgpu_bo_unreserve(e->robj);
425 }
426 }
427
428 if (list_empty(&need_pages))
429 break;
430
431 /* Unreserve everything again. */
432 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
433
434 /* We tried to often, just abort */
435 if (!--tries) {
436 r = -EDEADLK;
437 goto error_free_pages;
438 }
439
440 /* Fill the page arrays for all useptrs. */
441 list_for_each_entry(e, &need_pages, tv.head) {
442 struct ttm_tt *ttm = e->robj->tbo.ttm;
443
444 e->user_pages = drm_calloc_large(ttm->num_pages,
445 sizeof(struct page*));
446 if (!e->user_pages) {
447 r = -ENOMEM;
448 goto error_free_pages;
449 }
450
451 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
452 if (r) {
453 drm_free_large(e->user_pages);
454 e->user_pages = NULL;
455 goto error_free_pages;
456 }
457 }
458
459 /* And try again. */
460 list_splice(&need_pages, &p->validated);
461 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462
Christian Königee1782c2015-12-11 21:01:23 +0100463 amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100464
Christian Königf69f90a12015-12-21 19:47:42 +0100465 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
466 p->bytes_moved = 0;
467
468 r = amdgpu_cs_list_validate(p, &duplicates);
Christian Königa5b75052015-09-03 16:40:39 +0200469 if (r)
470 goto error_validate;
471
Christian Königf69f90a12015-12-21 19:47:42 +0100472 r = amdgpu_cs_list_validate(p, &p->validated);
Christian Königa8480302016-01-05 16:03:39 +0100473 if (r)
474 goto error_validate;
475
476 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200477 struct amdgpu_bo *gds = p->bo_list->gds_obj;
478 struct amdgpu_bo *gws = p->bo_list->gws_obj;
479 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100480 struct amdgpu_vm *vm = &fpriv->vm;
481 unsigned i;
482
483 for (i = 0; i < p->bo_list->num_entries; i++) {
484 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
485
486 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
487 }
Christian Königd88bf582016-05-06 17:50:03 +0200488
489 if (gds) {
490 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
491 p->job->gds_size = amdgpu_bo_size(gds);
492 }
493 if (gws) {
494 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
495 p->job->gws_size = amdgpu_bo_size(gws);
496 }
497 if (oa) {
498 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
499 p->job->oa_size = amdgpu_bo_size(oa);
500 }
Christian Königa8480302016-01-05 16:03:39 +0100501 }
Christian Königa5b75052015-09-03 16:40:39 +0200502
503error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100504 if (r) {
505 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200506 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100507 }
Christian Königa5b75052015-09-03 16:40:39 +0200508
Christian König2f568db2016-02-23 12:36:59 +0100509error_free_pages:
510
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 if (need_mmap_lock)
512 up_read(&current->mm->mmap_sem);
513
Christian König2f568db2016-02-23 12:36:59 +0100514 if (p->bo_list) {
515 for (i = p->bo_list->first_userptr;
516 i < p->bo_list->num_entries; ++i) {
517 e = &p->bo_list->array[i];
518
519 if (!e->user_pages)
520 continue;
521
522 release_pages(e->user_pages,
523 e->robj->tbo.ttm->num_pages,
524 false);
525 drm_free_large(e->user_pages);
526 }
527 }
528
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 return r;
530}
531
532static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
533{
534 struct amdgpu_bo_list_entry *e;
535 int r;
536
537 list_for_each_entry(e, &p->validated, tv.head) {
538 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100539 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540
541 if (r)
542 return r;
543 }
544 return 0;
545}
546
Christian König984810f2015-11-14 21:05:35 +0100547/**
548 * cs_parser_fini() - clean parser states
549 * @parser: parser structure holding parsing context.
550 * @error: error number
551 *
552 * If error is set than unvalidate buffer, otherwise just free memory
553 * used by parsing context.
554 **/
555static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800556{
Christian Königeceb8a12016-01-11 15:35:21 +0100557 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100558 unsigned i;
559
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500561 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
562
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100564 &parser->validated,
565 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566 } else if (backoff) {
567 ttm_eu_backoff_reservation(&parser->ticket,
568 &parser->validated);
569 }
Christian König984810f2015-11-14 21:05:35 +0100570 fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100571
Christian König3cb485f2015-05-11 15:34:59 +0200572 if (parser->ctx)
573 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800574 if (parser->bo_list)
575 amdgpu_bo_list_put(parser->bo_list);
576
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 for (i = 0; i < parser->nchunks; i++)
578 drm_free_large(parser->chunks[i].kdata);
579 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100580 if (parser->job)
581 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100582 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583}
584
585static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
586 struct amdgpu_vm *vm)
587{
588 struct amdgpu_device *adev = p->adev;
589 struct amdgpu_bo_va *bo_va;
590 struct amdgpu_bo *bo;
591 int i, r;
592
593 r = amdgpu_vm_update_page_directory(adev, vm);
594 if (r)
595 return r;
596
Christian Könige86f9ce2016-02-08 12:13:05 +0100597 r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200598 if (r)
599 return r;
600
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 r = amdgpu_vm_clear_freed(adev, vm);
602 if (r)
603 return r;
604
605 if (p->bo_list) {
606 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200607 struct fence *f;
608
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400609 /* ignore duplicates */
610 bo = p->bo_list->array[i].robj;
611 if (!bo)
612 continue;
613
614 bo_va = p->bo_list->array[i].bo_va;
615 if (bo_va == NULL)
616 continue;
617
618 r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
619 if (r)
620 return r;
621
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800622 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100623 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200624 if (r)
625 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 }
Christian Königb495bd32015-09-10 14:00:35 +0200627
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628 }
629
Christian Könige86f9ce2016-02-08 12:13:05 +0100630 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200631
632 if (amdgpu_vm_debug && p->bo_list) {
633 /* Invalidate all BOs to test for userspace bugs */
634 for (i = 0; i < p->bo_list->num_entries; i++) {
635 /* ignore duplicates */
636 bo = p->bo_list->array[i].robj;
637 if (!bo)
638 continue;
639
640 amdgpu_vm_bo_invalidate(adev, bo);
641 }
642 }
643
644 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645}
646
647static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100648 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649{
Christian Königb07c60c2016-01-31 12:29:04 +0100650 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100652 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653 int i, r;
654
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100656 if (ring->funcs->parse_cs) {
657 for (i = 0; i < p->job->num_ibs; i++) {
658 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 if (r)
660 return r;
661 }
662 }
663
Christian Königb07c60c2016-01-31 12:29:04 +0100664 r = amdgpu_bo_vm_update_pte(p, vm);
Christian König984810f2015-11-14 21:05:35 +0100665 if (!r)
Christian Königb07c60c2016-01-31 12:29:04 +0100666 amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 return r;
669}
670
671static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
672{
673 if (r == -EDEADLK) {
674 r = amdgpu_gpu_reset(adev);
675 if (!r)
676 r = -EAGAIN;
677 }
678 return r;
679}
680
681static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
682 struct amdgpu_cs_parser *parser)
683{
684 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
685 struct amdgpu_vm *vm = &fpriv->vm;
686 int i, j;
687 int r;
688
Christian König50838c82016-02-03 13:44:52 +0100689 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 struct amdgpu_cs_chunk *chunk;
691 struct amdgpu_ib *ib;
692 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694
695 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100696 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
698
699 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
700 continue;
701
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
703 chunk_ib->ip_instance, chunk_ib->ring,
704 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200705 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707
Christian Königb07c60c2016-01-31 12:29:04 +0100708 if (parser->job->ring && parser->job->ring != ring)
709 return -EINVAL;
710
711 parser->job->ring = ring;
712
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200714 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200715 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200716 uint64_t offset;
717 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200718
Christian König4802ce12015-06-10 17:20:11 +0200719 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
720 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200721 if (!aobj) {
722 DRM_ERROR("IB va_start is invalid\n");
723 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724 }
725
Christian König4802ce12015-06-10 17:20:11 +0200726 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
727 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
728 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
729 return -EINVAL;
730 }
731
Marek Olšák3ccec532015-06-02 17:44:49 +0200732 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200733 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735 return r;
736 }
737
Christian König4802ce12015-06-10 17:20:11 +0200738 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
739 kptr += chunk_ib->va_start - offset;
740
Christian Königb07c60c2016-01-31 12:29:04 +0100741 r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742 if (r) {
743 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744 return r;
745 }
746
747 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
748 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100750 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751 if (r) {
752 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 return r;
754 }
755
756 ib->gpu_addr = chunk_ib->va_start;
757 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758
Marek Olšák3ccec532015-06-02 17:44:49 +0200759 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800760 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 j++;
762 }
763
Christian König758ac172016-05-06 22:14:00 +0200764 /* UVD & VCE fw doesn't support user fences */
765 if (parser->job->uf_bo && (
766 parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
767 parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
768 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769
770 return 0;
771}
772
Christian König2b48d322015-06-19 17:31:29 +0200773static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
774 struct amdgpu_cs_parser *p)
775{
Christian König76a1ea62015-07-06 19:42:10 +0200776 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200777 int i, j, r;
778
Christian König2b48d322015-06-19 17:31:29 +0200779 for (i = 0; i < p->nchunks; ++i) {
780 struct drm_amdgpu_cs_chunk_dep *deps;
781 struct amdgpu_cs_chunk *chunk;
782 unsigned num_deps;
783
784 chunk = &p->chunks[i];
785
786 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
787 continue;
788
789 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
790 num_deps = chunk->length_dw * 4 /
791 sizeof(struct drm_amdgpu_cs_chunk_dep);
792
793 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200794 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200795 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200796 struct fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200797
798 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
799 deps[j].ip_instance,
800 deps[j].ring, &ring);
801 if (r)
802 return r;
803
Christian König76a1ea62015-07-06 19:42:10 +0200804 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
805 if (ctx == NULL)
806 return -EINVAL;
807
Christian König21c16bf2015-07-07 17:24:49 +0200808 fence = amdgpu_ctx_get_fence(ctx, ring,
809 deps[j].handle);
810 if (IS_ERR(fence)) {
811 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +0200812 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +0200813 return r;
Christian König21c16bf2015-07-07 17:24:49 +0200814
815 } else if (fence) {
Christian Könige86f9ce2016-02-08 12:13:05 +0100816 r = amdgpu_sync_fence(adev, &p->job->sync,
817 fence);
Christian König21c16bf2015-07-07 17:24:49 +0200818 fence_put(fence);
819 amdgpu_ctx_put(ctx);
820 if (r)
821 return r;
Christian König76a1ea62015-07-06 19:42:10 +0200822 }
Christian König2b48d322015-06-19 17:31:29 +0200823 }
824 }
825
826 return 0;
827}
828
Christian Königcd75dc62016-01-31 11:30:55 +0100829static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
830 union drm_amdgpu_cs *cs)
831{
Christian Königb07c60c2016-01-31 12:29:04 +0100832 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +0200833 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Monk Liue6869412016-03-07 12:49:55 +0800834 struct fence *fence;
Christian Königcd75dc62016-01-31 11:30:55 +0100835 struct amdgpu_job *job;
Monk Liue6869412016-03-07 12:49:55 +0800836 int r;
Christian Königcd75dc62016-01-31 11:30:55 +0100837
Christian König50838c82016-02-03 13:44:52 +0100838 job = p->job;
839 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +0100840
Monk Liue6869412016-03-07 12:49:55 +0800841 r = amd_sched_job_init(&job->base, &ring->sched,
Christian König92f25092016-05-06 15:57:42 +0200842 entity, amdgpu_job_timeout_func,
843 amdgpu_job_free_func,
844 p->filp, &fence);
Monk Liue6869412016-03-07 12:49:55 +0800845 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +0100846 amdgpu_job_free(job);
Monk Liue6869412016-03-07 12:49:55 +0800847 return r;
Christian Königcd75dc62016-01-31 11:30:55 +0100848 }
849
Monk Liue6869412016-03-07 12:49:55 +0800850 job->owner = p->filp;
Christian König92f25092016-05-06 15:57:42 +0200851 job->ctx = entity->fence_context;
Monk Liue6869412016-03-07 12:49:55 +0800852 p->fence = fence_get(fence);
853 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, fence);
Christian König758ac172016-05-06 22:14:00 +0200854 job->uf_sequence = cs->out.handle;
Christian Königcd75dc62016-01-31 11:30:55 +0100855
856 trace_amdgpu_cs_ioctl(job);
857 amd_sched_entity_push_job(&job->base);
858
859 return 0;
860}
861
Chunming Zhou049fc522015-07-21 14:36:51 +0800862int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
863{
864 struct amdgpu_device *adev = dev->dev_private;
865 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +0100866 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +0200867 bool reserved_buffers = false;
868 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +0800869
Christian König0c418f12015-09-01 15:13:53 +0200870 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +0800871 return -EBUSY;
Chunming Zhou049fc522015-07-21 14:36:51 +0800872
Christian König7e52a812015-11-04 15:44:39 +0100873 parser.adev = adev;
874 parser.filp = filp;
875
876 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +0800878 DRM_ERROR("Failed to initialize parser !\n");
Christian König7e52a812015-11-04 15:44:39 +0100879 amdgpu_cs_parser_fini(&parser, r, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880 r = amdgpu_cs_handle_lockup(adev, r);
881 return r;
882 }
Christian König2a7d9bd2015-12-18 20:33:52 +0100883 r = amdgpu_cs_parser_bos(&parser, data);
Christian König26a69802015-08-18 21:09:33 +0200884 if (r == -ENOMEM)
885 DRM_ERROR("Not enough memory for command submission!\n");
886 else if (r && r != -ERESTARTSYS)
887 DRM_ERROR("Failed to process the buffer list %d!\n", r);
888 else if (!r) {
889 reserved_buffers = true;
Christian König7e52a812015-11-04 15:44:39 +0100890 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +0200891 }
892
893 if (!r) {
Christian König7e52a812015-11-04 15:44:39 +0100894 r = amdgpu_cs_dependencies(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +0200895 if (r)
896 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
897 }
898
899 if (r)
900 goto out;
901
Christian König50838c82016-02-03 13:44:52 +0100902 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +0100903 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +0200904
Christian König7e52a812015-11-04 15:44:39 +0100905 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +0800906 if (r)
907 goto out;
908
Christian König4acabfe2016-01-31 11:32:04 +0100909 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400910
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{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +0100981 unsigned i;
982
983 if (!parser->bo_list)
984 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985
986 addr /= AMDGPU_GPU_PAGE_SIZE;
987
Christian König15486fd22015-12-22 16:06:12 +0100988 for (i = 0; i < parser->bo_list->num_entries; i++) {
989 struct amdgpu_bo_list_entry *lobj;
990
991 lobj = &parser->bo_list->array[i];
992 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993 continue;
994
Christian König15486fd22015-12-22 16:06:12 +0100995 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian König7fc11952015-07-30 11:53:42 +0200996 if (mapping->it.start > addr ||
997 addr > mapping->it.last)
998 continue;
999
Christian König15486fd22015-12-22 16:06:12 +01001000 *bo = lobj->bo_va->bo;
Christian König7fc11952015-07-30 11:53:42 +02001001 return mapping;
1002 }
1003
Christian König15486fd22015-12-22 16:06:12 +01001004 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005 if (mapping->it.start > addr ||
1006 addr > mapping->it.last)
1007 continue;
1008
Christian König15486fd22015-12-22 16:06:12 +01001009 *bo = lobj->bo_va->bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 return mapping;
1011 }
1012 }
1013
1014 return NULL;
1015}