blob: bfb4b91869e7e68a6142ef25b0cf2bf686e06b79 [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 }
Ding Pixel8895ef42017-01-18 17:26:38 +080086
87 if (!(*out_ring && (*out_ring)->adev)) {
88 DRM_ERROR("Ring %d is not initialized on IP %d\n",
89 ring, ip_type);
90 return -EINVAL;
91 }
92
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 return 0;
94}
95
Christian König91acbeb2015-12-14 16:42:31 +010096static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +020097 struct drm_amdgpu_cs_chunk_fence *data,
98 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +010099{
100 struct drm_gem_object *gobj;
Christian Königaa290402016-09-09 11:21:43 +0200101 unsigned long size;
Christian König91acbeb2015-12-14 16:42:31 +0100102
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100103 gobj = drm_gem_object_lookup(p->filp, data->handle);
Christian König91acbeb2015-12-14 16:42:31 +0100104 if (gobj == NULL)
105 return -EINVAL;
106
Christian König758ac172016-05-06 22:14:00 +0200107 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +0100108 p->uf_entry.priority = 0;
109 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
110 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100111 p->uf_entry.user_pages = NULL;
Christian Königaa290402016-09-09 11:21:43 +0200112
113 size = amdgpu_bo_size(p->uf_entry.robj);
114 if (size != PAGE_SIZE || (data->offset + 8) > size)
115 return -EINVAL;
116
Christian König758ac172016-05-06 22:14:00 +0200117 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +0100118
119 drm_gem_object_unreference_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +0200120
121 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
122 amdgpu_bo_unref(&p->uf_entry.robj);
123 return -EINVAL;
124 }
125
Christian König91acbeb2015-12-14 16:42:31 +0100126 return 0;
127}
128
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
130{
Christian König4c0b2422016-02-01 11:20:37 +0100131 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +0800132 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 union drm_amdgpu_cs *cs = data;
134 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +0300135 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +0100136 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +0200137 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +0300138 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +0300139 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140
Dan Carpenter1d263472015-09-23 13:59:28 +0300141 if (cs->in.num_chunks == 0)
142 return 0;
143
144 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
145 if (!chunk_array)
146 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147
Christian König3cb485f2015-05-11 15:34:59 +0200148 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
149 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300150 ret = -EINVAL;
151 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +0200152 }
Dan Carpenter1d263472015-09-23 13:59:28 +0300153
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 /* get chunks */
Arnd Bergmann028423b2015-10-07 09:41:27 +0200155 chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 if (copy_from_user(chunk_array, chunk_array_user,
157 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300158 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +0100159 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 }
161
162 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800163 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300165 if (!p->chunks) {
166 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100167 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 }
169
170 for (i = 0; i < p->nchunks; i++) {
171 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
172 struct drm_amdgpu_cs_chunk user_chunk;
173 uint32_t __user *cdata;
174
Arnd Bergmann028423b2015-10-07 09:41:27 +0200175 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 if (copy_from_user(&user_chunk, chunk_ptr,
177 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300178 ret = -EFAULT;
179 i--;
180 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181 }
182 p->chunks[i].chunk_id = user_chunk.chunk_id;
183 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184
185 size = p->chunks[i].length_dw;
Arnd Bergmann028423b2015-10-07 09:41:27 +0200186 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400187
188 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
189 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300190 ret = -ENOMEM;
191 i--;
192 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193 }
194 size *= sizeof(uint32_t);
195 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300196 ret = -EFAULT;
197 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 }
199
Christian König9a5e8fb2015-06-23 17:07:03 +0200200 switch (p->chunks[i].chunk_id) {
201 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100202 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200203 break;
204
205 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100207 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300208 ret = -EINVAL;
209 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210 }
Christian König91acbeb2015-12-14 16:42:31 +0100211
Christian König758ac172016-05-06 22:14:00 +0200212 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
213 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100214 if (ret)
215 goto free_partial_kdata;
216
Christian König9a5e8fb2015-06-23 17:07:03 +0200217 break;
218
Christian König2b48d322015-06-19 17:31:29 +0200219 case AMDGPU_CHUNK_ID_DEPENDENCIES:
220 break;
221
Christian König9a5e8fb2015-06-23 17:07:03 +0200222 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300223 ret = -EINVAL;
224 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225 }
226 }
227
Monk Liuc5637832016-04-19 20:11:32 +0800228 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100229 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100230 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231
Christian Königb5f5acb2016-06-29 13:26:41 +0200232 if (p->uf_entry.robj)
233 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300235 return 0;
236
237free_all_kdata:
238 i = p->nchunks - 1;
239free_partial_kdata:
240 for (; i >= 0; i--)
241 drm_free_large(p->chunks[i].kdata);
242 kfree(p->chunks);
Christian König2a7d9bd2015-12-18 20:33:52 +0100243put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300244 amdgpu_ctx_put(p->ctx);
245free_chunk:
246 kfree(chunk_array);
247
248 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400249}
250
Marek Olšák95844d22016-08-17 23:49:27 +0200251/* Convert microseconds to bytes. */
252static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
253{
254 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
255 return 0;
256
257 /* Since accum_us is incremented by a million per second, just
258 * multiply it by the number of MB/s to get the number of bytes.
259 */
260 return us << adev->mm_stats.log2_max_MBps;
261}
262
263static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
264{
265 if (!adev->mm_stats.log2_max_MBps)
266 return 0;
267
268 return bytes >> adev->mm_stats.log2_max_MBps;
269}
270
271/* Returns how many bytes TTM can move right now. If no bytes can be moved,
272 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
273 * which means it can go over the threshold once. If that happens, the driver
274 * will be in debt and no other buffer migrations can be done until that debt
275 * is repaid.
276 *
277 * This approach allows moving a buffer of any size (it's important to allow
278 * that).
279 *
280 * The currency is simply time in microseconds and it increases as the clock
281 * ticks. The accumulated microseconds (us) are converted to bytes and
282 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 */
284static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
285{
Marek Olšák95844d22016-08-17 23:49:27 +0200286 s64 time_us, increment_us;
287 u64 max_bytes;
288 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400289
Marek Olšák95844d22016-08-17 23:49:27 +0200290 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
291 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400292 *
Marek Olšák95844d22016-08-17 23:49:27 +0200293 * It means that in order to get full max MBps, at least 5 IBs per
294 * second must be submitted and not more than 200ms apart from each
295 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 */
Marek Olšák95844d22016-08-17 23:49:27 +0200297 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298
Marek Olšák95844d22016-08-17 23:49:27 +0200299 if (!adev->mm_stats.log2_max_MBps)
300 return 0;
301
302 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
303 used_vram = atomic64_read(&adev->vram_usage);
304 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
305
306 spin_lock(&adev->mm_stats.lock);
307
308 /* Increase the amount of accumulated us. */
309 time_us = ktime_to_us(ktime_get());
310 increment_us = time_us - adev->mm_stats.last_update_us;
311 adev->mm_stats.last_update_us = time_us;
312 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
313 us_upper_bound);
314
315 /* This prevents the short period of low performance when the VRAM
316 * usage is low and the driver is in debt or doesn't have enough
317 * accumulated us to fill VRAM quickly.
318 *
319 * The situation can occur in these cases:
320 * - a lot of VRAM is freed by userspace
321 * - the presence of a big buffer causes a lot of evictions
322 * (solution: split buffers into smaller ones)
323 *
324 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
325 * accum_us to a positive number.
326 */
327 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
328 s64 min_us;
329
330 /* Be more aggresive on dGPUs. Try to fill a portion of free
331 * VRAM now.
332 */
333 if (!(adev->flags & AMD_IS_APU))
334 min_us = bytes_to_us(adev, free_vram / 4);
335 else
336 min_us = 0; /* Reset accum_us on APUs. */
337
338 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
339 }
340
341 /* This returns 0 if the driver is in debt to disallow (optional)
342 * buffer moves.
343 */
344 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
345
346 spin_unlock(&adev->mm_stats.lock);
347 return max_bytes;
348}
349
350/* Report how many bytes have really been moved for the last command
351 * submission. This can result in a debt that can stop buffer migrations
352 * temporarily.
353 */
354static void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev,
355 u64 num_bytes)
356{
357 spin_lock(&adev->mm_stats.lock);
358 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
359 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400360}
361
Chunming Zhou14fd8332016-08-04 13:05:46 +0800362static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
363 struct amdgpu_bo *bo)
364{
365 u64 initial_bytes_moved;
366 uint32_t domain;
367 int r;
368
369 if (bo->pin_count)
370 return 0;
371
Marek Olšák95844d22016-08-17 23:49:27 +0200372 /* Don't move this buffer if we have depleted our allowance
373 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800374 */
Marek Olšák95844d22016-08-17 23:49:27 +0200375 if (p->bytes_moved < p->bytes_moved_threshold)
Chunming Zhou14fd8332016-08-04 13:05:46 +0800376 domain = bo->prefered_domains;
377 else
378 domain = bo->allowed_domains;
379
380retry:
381 amdgpu_ttm_placement_from_domain(bo, domain);
382 initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
383 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
384 p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
385 initial_bytes_moved;
386
Christian König1abdc3d2016-08-31 17:28:11 +0200387 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
388 domain = bo->allowed_domains;
389 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800390 }
391
392 return r;
393}
394
Christian König662bfa62016-09-01 12:13:18 +0200395/* Last resort, try to evict something from the current working set */
396static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
397 struct amdgpu_bo_list_entry *lobj)
398{
399 uint32_t domain = lobj->robj->allowed_domains;
400 int r;
401
402 if (!p->evictable)
403 return false;
404
405 for (;&p->evictable->tv.head != &p->validated;
406 p->evictable = list_prev_entry(p->evictable, tv.head)) {
407
408 struct amdgpu_bo_list_entry *candidate = p->evictable;
409 struct amdgpu_bo *bo = candidate->robj;
410 u64 initial_bytes_moved;
411 uint32_t other;
412
413 /* If we reached our current BO we can forget it */
414 if (candidate == lobj)
415 break;
416
417 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
418
419 /* Check if this BO is in one of the domains we need space for */
420 if (!(other & domain))
421 continue;
422
423 /* Check if we can move this BO somewhere else */
424 other = bo->allowed_domains & ~domain;
425 if (!other)
426 continue;
427
428 /* Good we can try to move this BO somewhere else */
429 amdgpu_ttm_placement_from_domain(bo, other);
430 initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
431 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
432 p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
433 initial_bytes_moved;
434
435 if (unlikely(r))
436 break;
437
438 p->evictable = list_prev_entry(p->evictable, tv.head);
439 list_move(&candidate->tv.head, &p->validated);
440
441 return true;
442 }
443
444 return false;
445}
446
Baoyou Xie761c2e82016-09-03 13:57:14 +0800447static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200448 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 int r;
452
Christian Königa5b75052015-09-03 16:40:39 +0200453 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100454 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100455 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100456 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457
Christian Königcc325d12016-02-08 11:08:35 +0100458 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
459 if (usermm && usermm != current->mm)
460 return -EPERM;
461
Christian König2f568db2016-02-23 12:36:59 +0100462 /* Check if we have user pages and nobody bound the BO already */
463 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
464 size_t size = sizeof(struct page *);
465
466 size *= bo->tbo.ttm->num_pages;
467 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
468 binding_userptr = true;
469 }
470
Christian König662bfa62016-09-01 12:13:18 +0200471 if (p->evictable == lobj)
472 p->evictable = NULL;
473
474 do {
475 r = amdgpu_cs_bo_validate(p, bo);
476 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, lobj));
Chunming Zhou14fd8332016-08-04 13:05:46 +0800477 if (r)
Christian König36409d122015-12-21 20:31:35 +0100478 return r;
Christian König662bfa62016-09-01 12:13:18 +0200479
Chunming Zhou14fd8332016-08-04 13:05:46 +0800480 if (bo->shadow) {
481 r = amdgpu_cs_bo_validate(p, bo);
482 if (r)
483 return r;
Christian König36409d122015-12-21 20:31:35 +0100484 }
Christian König2f568db2016-02-23 12:36:59 +0100485
486 if (binding_userptr) {
487 drm_free_large(lobj->user_pages);
488 lobj->user_pages = NULL;
489 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490 }
491 return 0;
492}
493
Christian König2a7d9bd2015-12-18 20:33:52 +0100494static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
495 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496{
497 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100498 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200499 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800500 bool need_mmap_lock = false;
Christian König2f568db2016-02-23 12:36:59 +0100501 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100502 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503
Christian König2a7d9bd2015-12-18 20:33:52 +0100504 INIT_LIST_HEAD(&p->validated);
505
506 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800507 if (p->bo_list) {
Christian König211dff52016-02-22 15:40:59 +0100508 need_mmap_lock = p->bo_list->first_userptr !=
509 p->bo_list->num_entries;
Christian König636ce252015-12-18 21:26:47 +0100510 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
monk.liu840d5142015-04-27 15:19:20 +0800511 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512
Christian König3c0eea62015-12-11 14:39:05 +0100513 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100514 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515
Christian König758ac172016-05-06 22:14:00 +0200516 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100517 list_add(&p->uf_entry.tv.head, &p->validated);
518
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400519 if (need_mmap_lock)
520 down_read(&current->mm->mmap_sem);
521
Christian König2f568db2016-02-23 12:36:59 +0100522 while (1) {
523 struct list_head need_pages;
524 unsigned i;
525
526 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
527 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200528 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800529 if (r != -ERESTARTSYS)
530 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100531 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200532 }
Christian König2f568db2016-02-23 12:36:59 +0100533
534 /* Without a BO list we don't have userptr BOs */
535 if (!p->bo_list)
536 break;
537
538 INIT_LIST_HEAD(&need_pages);
539 for (i = p->bo_list->first_userptr;
540 i < p->bo_list->num_entries; ++i) {
541
542 e = &p->bo_list->array[i];
543
544 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
545 &e->user_invalidated) && e->user_pages) {
546
547 /* We acquired a page array, but somebody
548 * invalidated it. Free it an try again
549 */
550 release_pages(e->user_pages,
551 e->robj->tbo.ttm->num_pages,
552 false);
553 drm_free_large(e->user_pages);
554 e->user_pages = NULL;
555 }
556
557 if (e->robj->tbo.ttm->state != tt_bound &&
558 !e->user_pages) {
559 list_del(&e->tv.head);
560 list_add(&e->tv.head, &need_pages);
561
562 amdgpu_bo_unreserve(e->robj);
563 }
564 }
565
566 if (list_empty(&need_pages))
567 break;
568
569 /* Unreserve everything again. */
570 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
571
Marek Olšákf1037952016-07-30 00:48:39 +0200572 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100573 if (!--tries) {
574 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200575 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100576 goto error_free_pages;
577 }
578
579 /* Fill the page arrays for all useptrs. */
580 list_for_each_entry(e, &need_pages, tv.head) {
581 struct ttm_tt *ttm = e->robj->tbo.ttm;
582
583 e->user_pages = drm_calloc_large(ttm->num_pages,
584 sizeof(struct page*));
585 if (!e->user_pages) {
586 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200587 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100588 goto error_free_pages;
589 }
590
591 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
592 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200593 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100594 drm_free_large(e->user_pages);
595 e->user_pages = NULL;
596 goto error_free_pages;
597 }
598 }
599
600 /* And try again. */
601 list_splice(&need_pages, &p->validated);
602 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603
Christian König5a712a82016-06-21 16:28:15 +0200604 amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100605
Christian Königf69f90a12015-12-21 19:47:42 +0100606 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
607 p->bytes_moved = 0;
Christian König662bfa62016-09-01 12:13:18 +0200608 p->evictable = list_last_entry(&p->validated,
609 struct amdgpu_bo_list_entry,
610 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100611
612 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200613 if (r) {
614 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200615 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200616 }
Christian Königa5b75052015-09-03 16:40:39 +0200617
Christian Königf69f90a12015-12-21 19:47:42 +0100618 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200619 if (r) {
620 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100621 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200622 }
Christian Königa8480302016-01-05 16:03:39 +0100623
Marek Olšák95844d22016-08-17 23:49:27 +0200624 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
625
Christian König5a712a82016-06-21 16:28:15 +0200626 fpriv->vm.last_eviction_counter =
627 atomic64_read(&p->adev->num_evictions);
628
Christian Königa8480302016-01-05 16:03:39 +0100629 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200630 struct amdgpu_bo *gds = p->bo_list->gds_obj;
631 struct amdgpu_bo *gws = p->bo_list->gws_obj;
632 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100633 struct amdgpu_vm *vm = &fpriv->vm;
634 unsigned i;
635
636 for (i = 0; i < p->bo_list->num_entries; i++) {
637 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
638
639 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
640 }
Christian Königd88bf582016-05-06 17:50:03 +0200641
642 if (gds) {
643 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
644 p->job->gds_size = amdgpu_bo_size(gds);
645 }
646 if (gws) {
647 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
648 p->job->gws_size = amdgpu_bo_size(gws);
649 }
650 if (oa) {
651 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
652 p->job->oa_size = amdgpu_bo_size(oa);
653 }
Christian Königa8480302016-01-05 16:03:39 +0100654 }
Christian Königa5b75052015-09-03 16:40:39 +0200655
Christian Königc855e252016-09-05 17:00:57 +0200656 if (!r && p->uf_entry.robj) {
657 struct amdgpu_bo *uf = p->uf_entry.robj;
658
Christian Königbb990bb2016-09-09 16:32:33 +0200659 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200660 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
661 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200662
Christian Königa5b75052015-09-03 16:40:39 +0200663error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100664 if (r) {
665 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200666 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100667 }
Christian Königa5b75052015-09-03 16:40:39 +0200668
Christian König2f568db2016-02-23 12:36:59 +0100669error_free_pages:
670
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 if (need_mmap_lock)
672 up_read(&current->mm->mmap_sem);
673
Christian König2f568db2016-02-23 12:36:59 +0100674 if (p->bo_list) {
675 for (i = p->bo_list->first_userptr;
676 i < p->bo_list->num_entries; ++i) {
677 e = &p->bo_list->array[i];
678
679 if (!e->user_pages)
680 continue;
681
682 release_pages(e->user_pages,
683 e->robj->tbo.ttm->num_pages,
684 false);
685 drm_free_large(e->user_pages);
686 }
687 }
688
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 return r;
690}
691
692static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
693{
694 struct amdgpu_bo_list_entry *e;
695 int r;
696
697 list_for_each_entry(e, &p->validated, tv.head) {
698 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100699 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700
701 if (r)
702 return r;
703 }
704 return 0;
705}
706
Christian König984810f2015-11-14 21:05:35 +0100707/**
708 * cs_parser_fini() - clean parser states
709 * @parser: parser structure holding parsing context.
710 * @error: error number
711 *
712 * If error is set than unvalidate buffer, otherwise just free memory
713 * used by parsing context.
714 **/
715static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800716{
Christian Königeceb8a12016-01-11 15:35:21 +0100717 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100718 unsigned i;
719
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500721 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
722
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100724 &parser->validated,
725 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726 } else if (backoff) {
727 ttm_eu_backoff_reservation(&parser->ticket,
728 &parser->validated);
729 }
Christian König984810f2015-11-14 21:05:35 +0100730 fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100731
Christian König3cb485f2015-05-11 15:34:59 +0200732 if (parser->ctx)
733 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800734 if (parser->bo_list)
735 amdgpu_bo_list_put(parser->bo_list);
736
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 for (i = 0; i < parser->nchunks; i++)
738 drm_free_large(parser->chunks[i].kdata);
739 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100740 if (parser->job)
741 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100742 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400743}
744
745static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
746 struct amdgpu_vm *vm)
747{
748 struct amdgpu_device *adev = p->adev;
749 struct amdgpu_bo_va *bo_va;
750 struct amdgpu_bo *bo;
751 int i, r;
752
753 r = amdgpu_vm_update_page_directory(adev, vm);
754 if (r)
755 return r;
756
Christian Könige86f9ce2016-02-08 12:13:05 +0100757 r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200758 if (r)
759 return r;
760
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 r = amdgpu_vm_clear_freed(adev, vm);
762 if (r)
763 return r;
764
765 if (p->bo_list) {
766 for (i = 0; i < p->bo_list->num_entries; i++) {
Christian König91e1a522015-07-06 22:06:40 +0200767 struct fence *f;
768
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769 /* ignore duplicates */
770 bo = p->bo_list->array[i].robj;
771 if (!bo)
772 continue;
773
774 bo_va = p->bo_list->array[i].bo_va;
775 if (bo_va == NULL)
776 continue;
777
Christian König99e124f2016-08-16 14:43:17 +0200778 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779 if (r)
780 return r;
781
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800782 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100783 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200784 if (r)
785 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786 }
Christian Königb495bd32015-09-10 14:00:35 +0200787
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400788 }
789
Christian Könige86f9ce2016-02-08 12:13:05 +0100790 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200791
792 if (amdgpu_vm_debug && p->bo_list) {
793 /* Invalidate all BOs to test for userspace bugs */
794 for (i = 0; i < p->bo_list->num_entries; i++) {
795 /* ignore duplicates */
796 bo = p->bo_list->array[i].robj;
797 if (!bo)
798 continue;
799
800 amdgpu_vm_bo_invalidate(adev, bo);
801 }
802 }
803
804 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805}
806
807static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100808 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400809{
Christian Königb07c60c2016-01-31 12:29:04 +0100810 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400811 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100812 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400813 int i, r;
814
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400815 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100816 if (ring->funcs->parse_cs) {
Christian König9a795882016-06-22 14:25:55 +0200817 p->job->vm = NULL;
Christian Königb07c60c2016-01-31 12:29:04 +0100818 for (i = 0; i < p->job->num_ibs; i++) {
819 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820 if (r)
821 return r;
822 }
Christian König9a795882016-06-22 14:25:55 +0200823 } else {
824 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
825
826 r = amdgpu_bo_vm_update_pte(p, vm);
827 if (r)
828 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829 }
830
Christian König9a795882016-06-22 14:25:55 +0200831 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400832}
833
834static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
835{
836 if (r == -EDEADLK) {
837 r = amdgpu_gpu_reset(adev);
838 if (!r)
839 r = -EAGAIN;
840 }
841 return r;
842}
843
844static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
845 struct amdgpu_cs_parser *parser)
846{
847 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
848 struct amdgpu_vm *vm = &fpriv->vm;
849 int i, j;
850 int r;
851
Christian König50838c82016-02-03 13:44:52 +0100852 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853 struct amdgpu_cs_chunk *chunk;
854 struct amdgpu_ib *ib;
855 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857
858 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100859 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
861
862 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
863 continue;
864
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
866 chunk_ib->ip_instance, chunk_ib->ring,
867 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200868 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400869 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870
Monk Liu753ad492016-08-26 13:28:28 +0800871 if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
872 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
873 if (!parser->ctx->preamble_presented) {
874 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
875 parser->ctx->preamble_presented = true;
876 }
877 }
878
Christian Königb07c60c2016-01-31 12:29:04 +0100879 if (parser->job->ring && parser->job->ring != ring)
880 return -EINVAL;
881
882 parser->job->ring = ring;
883
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200885 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200886 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200887 uint64_t offset;
888 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200889
Christian König4802ce12015-06-10 17:20:11 +0200890 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
891 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200892 if (!aobj) {
893 DRM_ERROR("IB va_start is invalid\n");
894 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895 }
896
Christian König4802ce12015-06-10 17:20:11 +0200897 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
898 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
899 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
900 return -EINVAL;
901 }
902
Marek Olšák3ccec532015-06-02 17:44:49 +0200903 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200904 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400905 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906 return r;
907 }
908
Christian König4802ce12015-06-10 17:20:11 +0200909 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
910 kptr += chunk_ib->va_start - offset;
911
Christian Königb07c60c2016-01-31 12:29:04 +0100912 r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 if (r) {
914 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400915 return r;
916 }
917
918 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
919 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400920 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100921 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922 if (r) {
923 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400924 return r;
925 }
926
927 ib->gpu_addr = chunk_ib->va_start;
928 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929
Marek Olšák3ccec532015-06-02 17:44:49 +0200930 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800931 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400932 j++;
933 }
934
Christian König758ac172016-05-06 22:14:00 +0200935 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200936 if (parser->job->uf_addr && (
Christian König758ac172016-05-06 22:14:00 +0200937 parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
938 parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
939 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940
941 return 0;
942}
943
Christian König2b48d322015-06-19 17:31:29 +0200944static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
945 struct amdgpu_cs_parser *p)
946{
Christian König76a1ea62015-07-06 19:42:10 +0200947 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200948 int i, j, r;
949
Christian König2b48d322015-06-19 17:31:29 +0200950 for (i = 0; i < p->nchunks; ++i) {
951 struct drm_amdgpu_cs_chunk_dep *deps;
952 struct amdgpu_cs_chunk *chunk;
953 unsigned num_deps;
954
955 chunk = &p->chunks[i];
956
957 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
958 continue;
959
960 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
961 num_deps = chunk->length_dw * 4 /
962 sizeof(struct drm_amdgpu_cs_chunk_dep);
963
964 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200965 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200966 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +0200967 struct fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200968
969 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
970 deps[j].ip_instance,
971 deps[j].ring, &ring);
972 if (r)
973 return r;
974
Christian König76a1ea62015-07-06 19:42:10 +0200975 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
976 if (ctx == NULL)
977 return -EINVAL;
978
Christian König21c16bf2015-07-07 17:24:49 +0200979 fence = amdgpu_ctx_get_fence(ctx, ring,
980 deps[j].handle);
981 if (IS_ERR(fence)) {
982 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +0200983 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +0200984 return r;
Christian König21c16bf2015-07-07 17:24:49 +0200985
986 } else if (fence) {
Christian Könige86f9ce2016-02-08 12:13:05 +0100987 r = amdgpu_sync_fence(adev, &p->job->sync,
988 fence);
Christian König21c16bf2015-07-07 17:24:49 +0200989 fence_put(fence);
990 amdgpu_ctx_put(ctx);
991 if (r)
992 return r;
Christian König76a1ea62015-07-06 19:42:10 +0200993 }
Christian König2b48d322015-06-19 17:31:29 +0200994 }
995 }
996
997 return 0;
998}
999
Christian Königcd75dc62016-01-31 11:30:55 +01001000static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1001 union drm_amdgpu_cs *cs)
1002{
Christian Königb07c60c2016-01-31 12:29:04 +01001003 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001004 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001005 struct amdgpu_job *job;
Monk Liue6869412016-03-07 12:49:55 +08001006 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001007
Christian König50838c82016-02-03 13:44:52 +01001008 job = p->job;
1009 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001010
Christian König595a9cd2016-06-30 10:52:03 +02001011 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001012 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001013 amdgpu_job_free(job);
Monk Liue6869412016-03-07 12:49:55 +08001014 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001015 }
1016
Monk Liue6869412016-03-07 12:49:55 +08001017 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001018 job->fence_ctx = entity->fence_context;
Christian König595a9cd2016-06-30 10:52:03 +02001019 p->fence = fence_get(&job->base.s_fence->finished);
1020 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
Christian König758ac172016-05-06 22:14:00 +02001021 job->uf_sequence = cs->out.handle;
Christian Königa5fb4ec2016-06-29 15:10:31 +02001022 amdgpu_job_free_resources(job);
Christian Königcd75dc62016-01-31 11:30:55 +01001023
1024 trace_amdgpu_cs_ioctl(job);
1025 amd_sched_entity_push_job(&job->base);
1026
1027 return 0;
1028}
1029
Chunming Zhou049fc522015-07-21 14:36:51 +08001030int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1031{
1032 struct amdgpu_device *adev = dev->dev_private;
1033 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001034 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001035 bool reserved_buffers = false;
1036 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001037
Christian König0c418f12015-09-01 15:13:53 +02001038 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001039 return -EBUSY;
Chunming Zhou049fc522015-07-21 14:36:51 +08001040
Christian König7e52a812015-11-04 15:44:39 +01001041 parser.adev = adev;
1042 parser.filp = filp;
1043
1044 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001046 DRM_ERROR("Failed to initialize parser !\n");
Christian König7e52a812015-11-04 15:44:39 +01001047 amdgpu_cs_parser_fini(&parser, r, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001048 r = amdgpu_cs_handle_lockup(adev, r);
1049 return r;
1050 }
Christian König2a7d9bd2015-12-18 20:33:52 +01001051 r = amdgpu_cs_parser_bos(&parser, data);
Christian König26a69802015-08-18 21:09:33 +02001052 if (r == -ENOMEM)
1053 DRM_ERROR("Not enough memory for command submission!\n");
1054 else if (r && r != -ERESTARTSYS)
1055 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1056 else if (!r) {
1057 reserved_buffers = true;
Christian König7e52a812015-11-04 15:44:39 +01001058 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001059 }
1060
1061 if (!r) {
Christian König7e52a812015-11-04 15:44:39 +01001062 r = amdgpu_cs_dependencies(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001063 if (r)
1064 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1065 }
1066
1067 if (r)
1068 goto out;
1069
Christian König50838c82016-02-03 13:44:52 +01001070 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001071 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001072
Christian König7e52a812015-11-04 15:44:39 +01001073 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001074 if (r)
1075 goto out;
1076
Christian König4acabfe2016-01-31 11:32:04 +01001077 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001079out:
Christian König7e52a812015-11-04 15:44:39 +01001080 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081 r = amdgpu_cs_handle_lockup(adev, r);
1082 return r;
1083}
1084
1085/**
1086 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1087 *
1088 * @dev: drm device
1089 * @data: data from userspace
1090 * @filp: file private
1091 *
1092 * Wait for the command submission identified by handle to finish.
1093 */
1094int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1095 struct drm_file *filp)
1096{
1097 union drm_amdgpu_wait_cs *wait = data;
1098 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001100 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001101 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +02001102 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103 long r;
1104
Christian König21c16bf2015-07-07 17:24:49 +02001105 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
1106 wait->in.ring, &ring);
1107 if (r)
1108 return r;
1109
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001110 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1111 if (ctx == NULL)
1112 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001113
1114 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1115 if (IS_ERR(fence))
1116 r = PTR_ERR(fence);
1117 else if (fence) {
1118 r = fence_wait_timeout(fence, true, timeout);
1119 fence_put(fence);
1120 } else
Christian König21c16bf2015-07-07 17:24:49 +02001121 r = 1;
1122
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001123 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124 if (r < 0)
1125 return r;
1126
1127 memset(wait, 0, sizeof(*wait));
1128 wait->out.status = (r == 0);
1129
1130 return 0;
1131}
1132
1133/**
1134 * amdgpu_cs_find_bo_va - find bo_va for VM address
1135 *
1136 * @parser: command submission parser context
1137 * @addr: VM address
1138 * @bo: resulting BO of the mapping found
1139 *
1140 * Search the buffer objects in the command submission context for a certain
1141 * virtual memory address. Returns allocation structure when found, NULL
1142 * otherwise.
1143 */
1144struct amdgpu_bo_va_mapping *
1145amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1146 uint64_t addr, struct amdgpu_bo **bo)
1147{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001148 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +01001149 unsigned i;
1150
1151 if (!parser->bo_list)
1152 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153
1154 addr /= AMDGPU_GPU_PAGE_SIZE;
1155
Christian König15486fd22015-12-22 16:06:12 +01001156 for (i = 0; i < parser->bo_list->num_entries; i++) {
1157 struct amdgpu_bo_list_entry *lobj;
1158
1159 lobj = &parser->bo_list->array[i];
1160 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001161 continue;
1162
Christian König15486fd22015-12-22 16:06:12 +01001163 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian König7fc11952015-07-30 11:53:42 +02001164 if (mapping->it.start > addr ||
1165 addr > mapping->it.last)
1166 continue;
1167
Christian König15486fd22015-12-22 16:06:12 +01001168 *bo = lobj->bo_va->bo;
Christian König7fc11952015-07-30 11:53:42 +02001169 return mapping;
1170 }
1171
Christian König15486fd22015-12-22 16:06:12 +01001172 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 if (mapping->it.start > addr ||
1174 addr > mapping->it.last)
1175 continue;
1176
Christian König15486fd22015-12-22 16:06:12 +01001177 *bo = lobj->bo_va->bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178 return mapping;
1179 }
1180 }
1181
1182 return NULL;
1183}
Christian Königc855e252016-09-05 17:00:57 +02001184
1185/**
1186 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1187 *
1188 * @parser: command submission parser context
1189 *
1190 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1191 */
1192int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1193{
1194 unsigned i;
1195 int r;
1196
1197 if (!parser->bo_list)
1198 return 0;
1199
1200 for (i = 0; i < parser->bo_list->num_entries; i++) {
1201 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1202
Christian Königbb990bb2016-09-09 16:32:33 +02001203 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +02001204 if (unlikely(r))
1205 return r;
1206 }
1207
1208 return 0;
1209}