blob: 9a0b2e5f3c116fc2feeb9e42ba155c0b696f2e3e [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:
Alex Deucher034041f2017-01-11 16:11:48 -050078 if (ring < adev->vce.num_rings){
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079 *out_ring = &adev->vce.ring[ring];
80 } else {
Alex Deucher034041f2017-01-11 16:11:48 -050081 DRM_ERROR("only %d VCE rings are supported\n", adev->vce.num_rings);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 return -EINVAL;
83 }
84 break;
Leo Liu166c8172017-01-10 11:57:24 -050085 case AMDGPU_HW_IP_UVD_ENC:
86 if (ring < adev->uvd.num_enc_rings){
87 *out_ring = &adev->uvd.ring_enc[ring];
88 } else {
89 DRM_ERROR("only %d UVD ENC rings are supported\n",
90 adev->uvd.num_enc_rings);
91 return -EINVAL;
92 }
93 break;
Leo Liufc739f82017-01-25 15:05:53 -050094 case AMDGPU_HW_IP_VCN_DEC:
95 *out_ring = &adev->vcn.ring_dec;
96 break;
Leo Liuf93aa002017-02-21 11:24:09 -050097 case AMDGPU_HW_IP_VCN_ENC:
98 if (ring < adev->vcn.num_enc_rings){
99 *out_ring = &adev->vcn.ring_enc[ring];
100 } else {
101 DRM_ERROR("only %d VCN ENC rings are supported\n",
102 adev->vcn.num_enc_rings);
103 return -EINVAL;
104 }
105 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 }
Ding Pixelc5f21c92017-01-18 17:26:38 +0800107
108 if (!(*out_ring && (*out_ring)->adev)) {
109 DRM_ERROR("Ring %d is not initialized on IP %d\n",
110 ring, ip_type);
111 return -EINVAL;
112 }
113
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114 return 0;
115}
116
Christian König91acbeb2015-12-14 16:42:31 +0100117static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +0200118 struct drm_amdgpu_cs_chunk_fence *data,
119 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +0100120{
121 struct drm_gem_object *gobj;
Christian Königaa290402016-09-09 11:21:43 +0200122 unsigned long size;
Christian König91acbeb2015-12-14 16:42:31 +0100123
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100124 gobj = drm_gem_object_lookup(p->filp, data->handle);
Christian König91acbeb2015-12-14 16:42:31 +0100125 if (gobj == NULL)
126 return -EINVAL;
127
Christian König758ac172016-05-06 22:14:00 +0200128 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +0100129 p->uf_entry.priority = 0;
130 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
131 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100132 p->uf_entry.user_pages = NULL;
Christian Königaa290402016-09-09 11:21:43 +0200133
134 size = amdgpu_bo_size(p->uf_entry.robj);
135 if (size != PAGE_SIZE || (data->offset + 8) > size)
136 return -EINVAL;
137
Christian König758ac172016-05-06 22:14:00 +0200138 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +0100139
140 drm_gem_object_unreference_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +0200141
142 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
143 amdgpu_bo_unref(&p->uf_entry.robj);
144 return -EINVAL;
145 }
146
Christian König91acbeb2015-12-14 16:42:31 +0100147 return 0;
148}
149
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
151{
Christian König4c0b2422016-02-01 11:20:37 +0100152 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +0800153 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 union drm_amdgpu_cs *cs = data;
155 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +0300156 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +0100157 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +0200158 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +0300159 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +0300160 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161
Dan Carpenter1d263472015-09-23 13:59:28 +0300162 if (cs->in.num_chunks == 0)
163 return 0;
164
165 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
166 if (!chunk_array)
167 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168
Christian König3cb485f2015-05-11 15:34:59 +0200169 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
170 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300171 ret = -EINVAL;
172 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +0200173 }
Dan Carpenter1d263472015-09-23 13:59:28 +0300174
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175 /* get chunks */
Alex Xief4e7c7c2017-04-05 16:54:34 -0400176 chunk_array_user = (uint64_t __user *)(uintptr_t)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177 if (copy_from_user(chunk_array, chunk_array_user,
178 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300179 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +0100180 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181 }
182
183 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800184 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300186 if (!p->chunks) {
187 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100188 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189 }
190
191 for (i = 0; i < p->nchunks; i++) {
192 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
193 struct drm_amdgpu_cs_chunk user_chunk;
194 uint32_t __user *cdata;
195
Alex Xief4e7c7c2017-04-05 16:54:34 -0400196 chunk_ptr = (void __user *)(uintptr_t)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197 if (copy_from_user(&user_chunk, chunk_ptr,
198 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300199 ret = -EFAULT;
200 i--;
201 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202 }
203 p->chunks[i].chunk_id = user_chunk.chunk_id;
204 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205
206 size = p->chunks[i].length_dw;
Alex Xief4e7c7c2017-04-05 16:54:34 -0400207 cdata = (void __user *)(uintptr_t)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208
209 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
210 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300211 ret = -ENOMEM;
212 i--;
213 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214 }
215 size *= sizeof(uint32_t);
216 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300217 ret = -EFAULT;
218 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219 }
220
Christian König9a5e8fb2015-06-23 17:07:03 +0200221 switch (p->chunks[i].chunk_id) {
222 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100223 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200224 break;
225
226 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400227 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100228 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300229 ret = -EINVAL;
230 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231 }
Christian König91acbeb2015-12-14 16:42:31 +0100232
Christian König758ac172016-05-06 22:14:00 +0200233 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
234 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100235 if (ret)
236 goto free_partial_kdata;
237
Christian König9a5e8fb2015-06-23 17:07:03 +0200238 break;
239
Christian König2b48d322015-06-19 17:31:29 +0200240 case AMDGPU_CHUNK_ID_DEPENDENCIES:
241 break;
242
Christian König9a5e8fb2015-06-23 17:07:03 +0200243 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300244 ret = -EINVAL;
245 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246 }
247 }
248
Monk Liuc5637832016-04-19 20:11:32 +0800249 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100250 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100251 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252
Christian Königb5f5acb2016-06-29 13:26:41 +0200253 if (p->uf_entry.robj)
254 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300256 return 0;
257
258free_all_kdata:
259 i = p->nchunks - 1;
260free_partial_kdata:
261 for (; i >= 0; i--)
262 drm_free_large(p->chunks[i].kdata);
263 kfree(p->chunks);
Dave Airlie607523d2017-03-10 12:13:04 +1000264 p->chunks = NULL;
265 p->nchunks = 0;
Christian König2a7d9bd2015-12-18 20:33:52 +0100266put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300267 amdgpu_ctx_put(p->ctx);
268free_chunk:
269 kfree(chunk_array);
270
271 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272}
273
Marek Olšák95844d22016-08-17 23:49:27 +0200274/* Convert microseconds to bytes. */
275static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
276{
277 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
278 return 0;
279
280 /* Since accum_us is incremented by a million per second, just
281 * multiply it by the number of MB/s to get the number of bytes.
282 */
283 return us << adev->mm_stats.log2_max_MBps;
284}
285
286static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
287{
288 if (!adev->mm_stats.log2_max_MBps)
289 return 0;
290
291 return bytes >> adev->mm_stats.log2_max_MBps;
292}
293
294/* Returns how many bytes TTM can move right now. If no bytes can be moved,
295 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
296 * which means it can go over the threshold once. If that happens, the driver
297 * will be in debt and no other buffer migrations can be done until that debt
298 * is repaid.
299 *
300 * This approach allows moving a buffer of any size (it's important to allow
301 * that).
302 *
303 * The currency is simply time in microseconds and it increases as the clock
304 * ticks. The accumulated microseconds (us) are converted to bytes and
305 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306 */
307static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
308{
Marek Olšák95844d22016-08-17 23:49:27 +0200309 s64 time_us, increment_us;
310 u64 max_bytes;
311 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312
Marek Olšák95844d22016-08-17 23:49:27 +0200313 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
314 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 *
Marek Olšák95844d22016-08-17 23:49:27 +0200316 * It means that in order to get full max MBps, at least 5 IBs per
317 * second must be submitted and not more than 200ms apart from each
318 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319 */
Marek Olšák95844d22016-08-17 23:49:27 +0200320 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400321
Marek Olšák95844d22016-08-17 23:49:27 +0200322 if (!adev->mm_stats.log2_max_MBps)
323 return 0;
324
325 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
326 used_vram = atomic64_read(&adev->vram_usage);
327 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
328
329 spin_lock(&adev->mm_stats.lock);
330
331 /* Increase the amount of accumulated us. */
332 time_us = ktime_to_us(ktime_get());
333 increment_us = time_us - adev->mm_stats.last_update_us;
334 adev->mm_stats.last_update_us = time_us;
335 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
336 us_upper_bound);
337
338 /* This prevents the short period of low performance when the VRAM
339 * usage is low and the driver is in debt or doesn't have enough
340 * accumulated us to fill VRAM quickly.
341 *
342 * The situation can occur in these cases:
343 * - a lot of VRAM is freed by userspace
344 * - the presence of a big buffer causes a lot of evictions
345 * (solution: split buffers into smaller ones)
346 *
347 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
348 * accum_us to a positive number.
349 */
350 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
351 s64 min_us;
352
353 /* Be more aggresive on dGPUs. Try to fill a portion of free
354 * VRAM now.
355 */
356 if (!(adev->flags & AMD_IS_APU))
357 min_us = bytes_to_us(adev, free_vram / 4);
358 else
359 min_us = 0; /* Reset accum_us on APUs. */
360
361 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
362 }
363
364 /* This returns 0 if the driver is in debt to disallow (optional)
365 * buffer moves.
366 */
367 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
368
369 spin_unlock(&adev->mm_stats.lock);
370 return max_bytes;
371}
372
373/* Report how many bytes have really been moved for the last command
374 * submission. This can result in a debt that can stop buffer migrations
375 * temporarily.
376 */
Samuel Pitoisetfad06122017-02-09 11:33:37 +0100377void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200378{
379 spin_lock(&adev->mm_stats.lock);
380 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
381 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382}
383
Chunming Zhou14fd8332016-08-04 13:05:46 +0800384static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
385 struct amdgpu_bo *bo)
386{
Christian Königa7d64de2016-09-15 14:58:48 +0200387 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800388 u64 initial_bytes_moved;
389 uint32_t domain;
390 int r;
391
392 if (bo->pin_count)
393 return 0;
394
Marek Olšák95844d22016-08-17 23:49:27 +0200395 /* Don't move this buffer if we have depleted our allowance
396 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800397 */
Marek Olšák95844d22016-08-17 23:49:27 +0200398 if (p->bytes_moved < p->bytes_moved_threshold)
Chunming Zhou14fd8332016-08-04 13:05:46 +0800399 domain = bo->prefered_domains;
400 else
401 domain = bo->allowed_domains;
402
403retry:
404 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200405 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800406 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200407 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Chunming Zhou14fd8332016-08-04 13:05:46 +0800408 initial_bytes_moved;
409
Christian König1abdc3d2016-08-31 17:28:11 +0200410 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
411 domain = bo->allowed_domains;
412 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800413 }
414
415 return r;
416}
417
Christian König662bfa62016-09-01 12:13:18 +0200418/* Last resort, try to evict something from the current working set */
419static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200420 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200421{
Christian Königf7da30d2016-09-28 12:03:04 +0200422 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200423 int r;
424
425 if (!p->evictable)
426 return false;
427
428 for (;&p->evictable->tv.head != &p->validated;
429 p->evictable = list_prev_entry(p->evictable, tv.head)) {
430
431 struct amdgpu_bo_list_entry *candidate = p->evictable;
432 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200433 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian König662bfa62016-09-01 12:13:18 +0200434 u64 initial_bytes_moved;
435 uint32_t other;
436
437 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200438 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200439 break;
440
441 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
442
443 /* Check if this BO is in one of the domains we need space for */
444 if (!(other & domain))
445 continue;
446
447 /* Check if we can move this BO somewhere else */
448 other = bo->allowed_domains & ~domain;
449 if (!other)
450 continue;
451
452 /* Good we can try to move this BO somewhere else */
453 amdgpu_ttm_placement_from_domain(bo, other);
Christian Königa7d64de2016-09-15 14:58:48 +0200454 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200455 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200456 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200457 initial_bytes_moved;
458
459 if (unlikely(r))
460 break;
461
462 p->evictable = list_prev_entry(p->evictable, tv.head);
463 list_move(&candidate->tv.head, &p->validated);
464
465 return true;
466 }
467
468 return false;
469}
470
Christian Königf7da30d2016-09-28 12:03:04 +0200471static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
472{
473 struct amdgpu_cs_parser *p = param;
474 int r;
475
476 do {
477 r = amdgpu_cs_bo_validate(p, bo);
478 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
479 if (r)
480 return r;
481
482 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500483 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200484
485 return r;
486}
487
Baoyou Xie761c2e82016-09-03 13:57:14 +0800488static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200489 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492 int r;
493
Christian Königa5b75052015-09-03 16:40:39 +0200494 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100495 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100496 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100497 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400498
Christian Königcc325d12016-02-08 11:08:35 +0100499 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
500 if (usermm && usermm != current->mm)
501 return -EPERM;
502
Christian König2f568db2016-02-23 12:36:59 +0100503 /* Check if we have user pages and nobody bound the BO already */
504 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
505 size_t size = sizeof(struct page *);
506
507 size *= bo->tbo.ttm->num_pages;
508 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
509 binding_userptr = true;
510 }
511
Christian König662bfa62016-09-01 12:13:18 +0200512 if (p->evictable == lobj)
513 p->evictable = NULL;
514
Christian Königf7da30d2016-09-28 12:03:04 +0200515 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800516 if (r)
Christian König36409d122015-12-21 20:31:35 +0100517 return r;
Christian König662bfa62016-09-01 12:13:18 +0200518
Christian König2f568db2016-02-23 12:36:59 +0100519 if (binding_userptr) {
520 drm_free_large(lobj->user_pages);
521 lobj->user_pages = NULL;
522 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 }
524 return 0;
525}
526
Christian König2a7d9bd2015-12-18 20:33:52 +0100527static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
528 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529{
530 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100531 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200532 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800533 bool need_mmap_lock = false;
Christian König2f568db2016-02-23 12:36:59 +0100534 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100535 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536
Christian König2a7d9bd2015-12-18 20:33:52 +0100537 INIT_LIST_HEAD(&p->validated);
538
539 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800540 if (p->bo_list) {
Christian König211dff52016-02-22 15:40:59 +0100541 need_mmap_lock = p->bo_list->first_userptr !=
542 p->bo_list->num_entries;
Christian König636ce252015-12-18 21:26:47 +0100543 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
monk.liu840d5142015-04-27 15:19:20 +0800544 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545
Christian König3c0eea62015-12-11 14:39:05 +0100546 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100547 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548
Christian König758ac172016-05-06 22:14:00 +0200549 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100550 list_add(&p->uf_entry.tv.head, &p->validated);
551
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400552 if (need_mmap_lock)
553 down_read(&current->mm->mmap_sem);
554
Christian König2f568db2016-02-23 12:36:59 +0100555 while (1) {
556 struct list_head need_pages;
557 unsigned i;
558
559 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
560 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200561 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800562 if (r != -ERESTARTSYS)
563 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100564 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200565 }
Christian König2f568db2016-02-23 12:36:59 +0100566
567 /* Without a BO list we don't have userptr BOs */
568 if (!p->bo_list)
569 break;
570
571 INIT_LIST_HEAD(&need_pages);
572 for (i = p->bo_list->first_userptr;
573 i < p->bo_list->num_entries; ++i) {
574
575 e = &p->bo_list->array[i];
576
577 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
578 &e->user_invalidated) && e->user_pages) {
579
580 /* We acquired a page array, but somebody
581 * invalidated it. Free it an try again
582 */
583 release_pages(e->user_pages,
584 e->robj->tbo.ttm->num_pages,
585 false);
586 drm_free_large(e->user_pages);
587 e->user_pages = NULL;
588 }
589
590 if (e->robj->tbo.ttm->state != tt_bound &&
591 !e->user_pages) {
592 list_del(&e->tv.head);
593 list_add(&e->tv.head, &need_pages);
594
595 amdgpu_bo_unreserve(e->robj);
596 }
597 }
598
599 if (list_empty(&need_pages))
600 break;
601
602 /* Unreserve everything again. */
603 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
604
Marek Olšákf1037952016-07-30 00:48:39 +0200605 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100606 if (!--tries) {
607 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200608 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100609 goto error_free_pages;
610 }
611
612 /* Fill the page arrays for all useptrs. */
613 list_for_each_entry(e, &need_pages, tv.head) {
614 struct ttm_tt *ttm = e->robj->tbo.ttm;
615
616 e->user_pages = drm_calloc_large(ttm->num_pages,
617 sizeof(struct page*));
618 if (!e->user_pages) {
619 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200620 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100621 goto error_free_pages;
622 }
623
624 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
625 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200626 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100627 drm_free_large(e->user_pages);
628 e->user_pages = NULL;
629 goto error_free_pages;
630 }
631 }
632
633 /* And try again. */
634 list_splice(&need_pages, &p->validated);
635 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636
Christian Königf69f90a12015-12-21 19:47:42 +0100637 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
638 p->bytes_moved = 0;
Christian König662bfa62016-09-01 12:13:18 +0200639 p->evictable = list_last_entry(&p->validated,
640 struct amdgpu_bo_list_entry,
641 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100642
Christian Königf7da30d2016-09-28 12:03:04 +0200643 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
644 amdgpu_cs_validate, p);
645 if (r) {
646 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
647 goto error_validate;
648 }
649
Christian Königf69f90a12015-12-21 19:47:42 +0100650 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200651 if (r) {
652 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200653 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200654 }
Christian Königa5b75052015-09-03 16:40:39 +0200655
Christian Königf69f90a12015-12-21 19:47:42 +0100656 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200657 if (r) {
658 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100659 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200660 }
Christian Königa8480302016-01-05 16:03:39 +0100661
Marek Olšák95844d22016-08-17 23:49:27 +0200662 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
663
Christian König5a712a82016-06-21 16:28:15 +0200664 fpriv->vm.last_eviction_counter =
665 atomic64_read(&p->adev->num_evictions);
666
Christian Königa8480302016-01-05 16:03:39 +0100667 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200668 struct amdgpu_bo *gds = p->bo_list->gds_obj;
669 struct amdgpu_bo *gws = p->bo_list->gws_obj;
670 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100671 struct amdgpu_vm *vm = &fpriv->vm;
672 unsigned i;
673
674 for (i = 0; i < p->bo_list->num_entries; i++) {
675 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
676
677 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
678 }
Christian Königd88bf582016-05-06 17:50:03 +0200679
680 if (gds) {
681 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
682 p->job->gds_size = amdgpu_bo_size(gds);
683 }
684 if (gws) {
685 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
686 p->job->gws_size = amdgpu_bo_size(gws);
687 }
688 if (oa) {
689 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
690 p->job->oa_size = amdgpu_bo_size(oa);
691 }
Christian Königa8480302016-01-05 16:03:39 +0100692 }
Christian Königa5b75052015-09-03 16:40:39 +0200693
Christian Königc855e252016-09-05 17:00:57 +0200694 if (!r && p->uf_entry.robj) {
695 struct amdgpu_bo *uf = p->uf_entry.robj;
696
Christian Königbb990bb2016-09-09 16:32:33 +0200697 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200698 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
699 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200700
Christian Königa5b75052015-09-03 16:40:39 +0200701error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100702 if (r) {
703 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200704 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100705 }
Christian Königa5b75052015-09-03 16:40:39 +0200706
Christian König2f568db2016-02-23 12:36:59 +0100707error_free_pages:
708
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 if (need_mmap_lock)
710 up_read(&current->mm->mmap_sem);
711
Christian König2f568db2016-02-23 12:36:59 +0100712 if (p->bo_list) {
713 for (i = p->bo_list->first_userptr;
714 i < p->bo_list->num_entries; ++i) {
715 e = &p->bo_list->array[i];
716
717 if (!e->user_pages)
718 continue;
719
720 release_pages(e->user_pages,
721 e->robj->tbo.ttm->num_pages,
722 false);
723 drm_free_large(e->user_pages);
724 }
725 }
726
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727 return r;
728}
729
730static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
731{
732 struct amdgpu_bo_list_entry *e;
733 int r;
734
735 list_for_each_entry(e, &p->validated, tv.head) {
736 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100737 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738
739 if (r)
740 return r;
741 }
742 return 0;
743}
744
Christian König984810f2015-11-14 21:05:35 +0100745/**
746 * cs_parser_fini() - clean parser states
747 * @parser: parser structure holding parsing context.
748 * @error: error number
749 *
750 * If error is set than unvalidate buffer, otherwise just free memory
751 * used by parsing context.
752 **/
753static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800754{
Christian Königeceb8a12016-01-11 15:35:21 +0100755 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100756 unsigned i;
757
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500759 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
760
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100762 &parser->validated,
763 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400764 } else if (backoff) {
765 ttm_eu_backoff_reservation(&parser->ticket,
766 &parser->validated);
767 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100768 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100769
Christian König3cb485f2015-05-11 15:34:59 +0200770 if (parser->ctx)
771 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800772 if (parser->bo_list)
773 amdgpu_bo_list_put(parser->bo_list);
774
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400775 for (i = 0; i < parser->nchunks; i++)
776 drm_free_large(parser->chunks[i].kdata);
777 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100778 if (parser->job)
779 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100780 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781}
782
Junwei Zhangb85891b2017-01-16 13:59:01 +0800783static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784{
785 struct amdgpu_device *adev = p->adev;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800786 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
787 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400788 struct amdgpu_bo_va *bo_va;
789 struct amdgpu_bo *bo;
790 int i, r;
791
Christian König194d2162016-10-12 15:13:52 +0200792 r = amdgpu_vm_update_directories(adev, vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400793 if (r)
794 return r;
795
Christian Königa24960f2016-10-12 13:20:52 +0200796 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200797 if (r)
798 return r;
799
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100800 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 if (r)
802 return r;
803
Junwei Zhangb85891b2017-01-16 13:59:01 +0800804 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
805 if (r)
806 return r;
807
808 r = amdgpu_sync_fence(adev, &p->job->sync,
809 fpriv->prt_va->last_pt_update);
810 if (r)
811 return r;
812
Monk Liu24936642017-01-09 15:54:32 +0800813 if (amdgpu_sriov_vf(adev)) {
814 struct dma_fence *f;
815 bo_va = vm->csa_bo_va;
816 BUG_ON(!bo_va);
817 r = amdgpu_vm_bo_update(adev, bo_va, false);
818 if (r)
819 return r;
820
821 f = bo_va->last_pt_update;
822 r = amdgpu_sync_fence(adev, &p->job->sync, f);
823 if (r)
824 return r;
825 }
826
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400827 if (p->bo_list) {
828 for (i = 0; i < p->bo_list->num_entries; i++) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100829 struct dma_fence *f;
Christian König91e1a522015-07-06 22:06:40 +0200830
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400831 /* ignore duplicates */
832 bo = p->bo_list->array[i].robj;
833 if (!bo)
834 continue;
835
836 bo_va = p->bo_list->array[i].bo_va;
837 if (bo_va == NULL)
838 continue;
839
Christian König99e124f2016-08-16 14:43:17 +0200840 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841 if (r)
842 return r;
843
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800844 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100845 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200846 if (r)
847 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 }
Christian Königb495bd32015-09-10 14:00:35 +0200849
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850 }
851
Christian Könige86f9ce2016-02-08 12:13:05 +0100852 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200853
854 if (amdgpu_vm_debug && p->bo_list) {
855 /* Invalidate all BOs to test for userspace bugs */
856 for (i = 0; i < p->bo_list->num_entries; i++) {
857 /* ignore duplicates */
858 bo = p->bo_list->array[i].robj;
859 if (!bo)
860 continue;
861
862 amdgpu_vm_bo_invalidate(adev, bo);
863 }
864 }
865
866 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867}
868
869static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100870 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400871{
Christian Königb07c60c2016-01-31 12:29:04 +0100872 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100874 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 int i, r;
876
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100878 if (ring->funcs->parse_cs) {
879 for (i = 0; i < p->job->num_ibs; i++) {
880 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881 if (r)
882 return r;
883 }
Christian König45088ef2016-10-05 16:49:19 +0200884 }
885
886 if (p->job->vm) {
Christian König67003a12016-10-12 14:46:26 +0200887 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
Christian König9a795882016-06-22 14:25:55 +0200888
Junwei Zhangb85891b2017-01-16 13:59:01 +0800889 r = amdgpu_bo_vm_update_pte(p);
Christian König9a795882016-06-22 14:25:55 +0200890 if (r)
891 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892 }
893
Christian König9a795882016-06-22 14:25:55 +0200894 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895}
896
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400897static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
898 struct amdgpu_cs_parser *parser)
899{
900 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
901 struct amdgpu_vm *vm = &fpriv->vm;
902 int i, j;
Monk Liu9a1b3af2017-03-08 15:51:13 +0800903 int r, ce_preempt = 0, de_preempt = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400904
Christian König50838c82016-02-03 13:44:52 +0100905 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906 struct amdgpu_cs_chunk *chunk;
907 struct amdgpu_ib *ib;
908 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400909 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400910
911 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100912 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
914
915 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
916 continue;
917
Monk Liu65333e42017-03-27 15:14:53 +0800918 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
Harry Wentlande51a3222017-03-28 11:29:53 -0400919 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
Monk Liu65333e42017-03-27 15:14:53 +0800920 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
921 ce_preempt++;
922 else
923 de_preempt++;
Harry Wentlande51a3222017-03-28 11:29:53 -0400924 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800925
Monk Liu65333e42017-03-27 15:14:53 +0800926 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
927 if (ce_preempt > 1 || de_preempt > 1)
Monk Liue9d672b2017-03-15 12:18:57 +0800928 return -EINVAL;
Monk Liu65333e42017-03-27 15:14:53 +0800929 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800930
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
932 chunk_ib->ip_instance, chunk_ib->ring,
933 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200934 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936
Monk Liu2a9ceb82017-03-28 11:00:03 +0800937 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
Monk Liu753ad492016-08-26 13:28:28 +0800938 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
939 if (!parser->ctx->preamble_presented) {
940 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
941 parser->ctx->preamble_presented = true;
942 }
943 }
944
Christian Königb07c60c2016-01-31 12:29:04 +0100945 if (parser->job->ring && parser->job->ring != ring)
946 return -EINVAL;
947
948 parser->job->ring = ring;
949
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200951 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200952 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200953 uint64_t offset;
954 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200955
Christian König4802ce12015-06-10 17:20:11 +0200956 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
957 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200958 if (!aobj) {
959 DRM_ERROR("IB va_start is invalid\n");
960 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961 }
962
Christian König4802ce12015-06-10 17:20:11 +0200963 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
Christian Königa9f87f62017-03-30 14:03:59 +0200964 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian König4802ce12015-06-10 17:20:11 +0200965 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
966 return -EINVAL;
967 }
968
Marek Olšák3ccec532015-06-02 17:44:49 +0200969 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200970 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400971 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972 return r;
973 }
974
Christian Königa9f87f62017-03-30 14:03:59 +0200975 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
Christian König4802ce12015-06-10 17:20:11 +0200976 kptr += chunk_ib->va_start - offset;
977
Christian König45088ef2016-10-05 16:49:19 +0200978 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 if (r) {
980 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 return r;
982 }
983
984 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
985 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100987 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400988 if (r) {
989 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 return r;
991 }
992
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994
Christian König45088ef2016-10-05 16:49:19 +0200995 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200996 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800997 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400998 j++;
999 }
1000
Christian König758ac172016-05-06 22:14:00 +02001001 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +02001002 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +02001003 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
1004 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +02001005 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006
1007 return 0;
1008}
1009
Christian König2b48d322015-06-19 17:31:29 +02001010static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1011 struct amdgpu_cs_parser *p)
1012{
Christian König76a1ea62015-07-06 19:42:10 +02001013 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +02001014 int i, j, r;
1015
Christian König2b48d322015-06-19 17:31:29 +02001016 for (i = 0; i < p->nchunks; ++i) {
1017 struct drm_amdgpu_cs_chunk_dep *deps;
1018 struct amdgpu_cs_chunk *chunk;
1019 unsigned num_deps;
1020
1021 chunk = &p->chunks[i];
1022
1023 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
1024 continue;
1025
1026 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1027 num_deps = chunk->length_dw * 4 /
1028 sizeof(struct drm_amdgpu_cs_chunk_dep);
1029
1030 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +02001031 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +02001032 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001033 struct dma_fence *fence;
Christian König2b48d322015-06-19 17:31:29 +02001034
1035 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
1036 deps[j].ip_instance,
1037 deps[j].ring, &ring);
1038 if (r)
1039 return r;
1040
Christian König76a1ea62015-07-06 19:42:10 +02001041 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
1042 if (ctx == NULL)
1043 return -EINVAL;
1044
Christian König21c16bf2015-07-07 17:24:49 +02001045 fence = amdgpu_ctx_get_fence(ctx, ring,
1046 deps[j].handle);
1047 if (IS_ERR(fence)) {
1048 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +02001049 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +02001050 return r;
Christian König21c16bf2015-07-07 17:24:49 +02001051
1052 } else if (fence) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001053 r = amdgpu_sync_fence(adev, &p->job->sync,
1054 fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001055 dma_fence_put(fence);
Christian König21c16bf2015-07-07 17:24:49 +02001056 amdgpu_ctx_put(ctx);
1057 if (r)
1058 return r;
Christian König76a1ea62015-07-06 19:42:10 +02001059 }
Christian König2b48d322015-06-19 17:31:29 +02001060 }
1061 }
1062
1063 return 0;
1064}
1065
Christian Königcd75dc62016-01-31 11:30:55 +01001066static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1067 union drm_amdgpu_cs *cs)
1068{
Christian Königb07c60c2016-01-31 12:29:04 +01001069 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001070 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001071 struct amdgpu_job *job;
Monk Liue6869412016-03-07 12:49:55 +08001072 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001073
Christian König50838c82016-02-03 13:44:52 +01001074 job = p->job;
1075 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001076
Christian König595a9cd2016-06-30 10:52:03 +02001077 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001078 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001079 amdgpu_job_free(job);
Monk Liue6869412016-03-07 12:49:55 +08001080 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001081 }
1082
Monk Liue6869412016-03-07 12:49:55 +08001083 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001084 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001085 p->fence = dma_fence_get(&job->base.s_fence->finished);
Christian König595a9cd2016-06-30 10:52:03 +02001086 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
Christian König758ac172016-05-06 22:14:00 +02001087 job->uf_sequence = cs->out.handle;
Christian Königa5fb4ec2016-06-29 15:10:31 +02001088 amdgpu_job_free_resources(job);
Chunming Zhou10e709c2017-04-27 15:13:52 +08001089 amdgpu_cs_parser_fini(p, 0, true);
Christian Königcd75dc62016-01-31 11:30:55 +01001090
1091 trace_amdgpu_cs_ioctl(job);
1092 amd_sched_entity_push_job(&job->base);
1093
1094 return 0;
1095}
1096
Chunming Zhou049fc522015-07-21 14:36:51 +08001097int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1098{
1099 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001100 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +08001101 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001102 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001103 bool reserved_buffers = false;
1104 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001105
Christian König0c418f12015-09-01 15:13:53 +02001106 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001107 return -EBUSY;
Chunming Zhouf1892132017-05-15 16:48:27 +08001108 if (amdgpu_kms_vram_lost(adev, fpriv))
1109 return -ENODEV;
Chunming Zhou049fc522015-07-21 14:36:51 +08001110
Christian König7e52a812015-11-04 15:44:39 +01001111 parser.adev = adev;
1112 parser.filp = filp;
1113
1114 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001115 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001116 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001117 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118 }
Huang Ruia414cd72016-10-30 23:05:47 +08001119
Christian König2a7d9bd2015-12-18 20:33:52 +01001120 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001121 if (r) {
1122 if (r == -ENOMEM)
1123 DRM_ERROR("Not enough memory for command submission!\n");
1124 else if (r != -ERESTARTSYS)
1125 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1126 goto out;
Christian König26a69802015-08-18 21:09:33 +02001127 }
1128
Huang Ruia414cd72016-10-30 23:05:47 +08001129 reserved_buffers = true;
1130 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001131 if (r)
1132 goto out;
1133
Huang Ruia414cd72016-10-30 23:05:47 +08001134 r = amdgpu_cs_dependencies(adev, &parser);
1135 if (r) {
1136 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1137 goto out;
1138 }
1139
Christian König50838c82016-02-03 13:44:52 +01001140 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001141 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001142
Christian König7e52a812015-11-04 15:44:39 +01001143 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001144 if (r)
1145 goto out;
1146
Christian König4acabfe2016-01-31 11:32:04 +01001147 r = amdgpu_cs_submit(&parser, cs);
Chunming Zhou10e709c2017-04-27 15:13:52 +08001148 if (r)
1149 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150
Chunming Zhou10e709c2017-04-27 15:13:52 +08001151 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152out:
Christian König7e52a812015-11-04 15:44:39 +01001153 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 return r;
1155}
1156
1157/**
1158 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1159 *
1160 * @dev: drm device
1161 * @data: data from userspace
1162 * @filp: file private
1163 *
1164 * Wait for the command submission identified by handle to finish.
1165 */
1166int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1167 struct drm_file *filp)
1168{
1169 union drm_amdgpu_wait_cs *wait = data;
1170 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001171 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001173 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001174 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001175 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001176 long r;
1177
Chunming Zhouf1892132017-05-15 16:48:27 +08001178 if (amdgpu_kms_vram_lost(adev, fpriv))
1179 return -ENODEV;
Christian König21c16bf2015-07-07 17:24:49 +02001180 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
1181 wait->in.ring, &ring);
1182 if (r)
1183 return r;
1184
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001185 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1186 if (ctx == NULL)
1187 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001188
1189 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1190 if (IS_ERR(fence))
1191 r = PTR_ERR(fence);
1192 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001193 r = dma_fence_wait_timeout(fence, true, timeout);
1194 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001195 } else
Christian König21c16bf2015-07-07 17:24:49 +02001196 r = 1;
1197
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001198 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 if (r < 0)
1200 return r;
1201
1202 memset(wait, 0, sizeof(*wait));
1203 wait->out.status = (r == 0);
1204
1205 return 0;
1206}
1207
1208/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001209 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1210 *
1211 * @adev: amdgpu device
1212 * @filp: file private
1213 * @user: drm_amdgpu_fence copied from user space
1214 */
1215static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1216 struct drm_file *filp,
1217 struct drm_amdgpu_fence *user)
1218{
1219 struct amdgpu_ring *ring;
1220 struct amdgpu_ctx *ctx;
1221 struct dma_fence *fence;
1222 int r;
1223
1224 r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance,
1225 user->ring, &ring);
1226 if (r)
1227 return ERR_PTR(r);
1228
1229 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1230 if (ctx == NULL)
1231 return ERR_PTR(-EINVAL);
1232
1233 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1234 amdgpu_ctx_put(ctx);
1235
1236 return fence;
1237}
1238
1239/**
1240 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1241 *
1242 * @adev: amdgpu device
1243 * @filp: file private
1244 * @wait: wait parameters
1245 * @fences: array of drm_amdgpu_fence
1246 */
1247static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1248 struct drm_file *filp,
1249 union drm_amdgpu_wait_fences *wait,
1250 struct drm_amdgpu_fence *fences)
1251{
1252 uint32_t fence_count = wait->in.fence_count;
1253 unsigned int i;
1254 long r = 1;
1255
1256 for (i = 0; i < fence_count; i++) {
1257 struct dma_fence *fence;
1258 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1259
1260 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1261 if (IS_ERR(fence))
1262 return PTR_ERR(fence);
1263 else if (!fence)
1264 continue;
1265
1266 r = dma_fence_wait_timeout(fence, true, timeout);
Chunming Zhou32df87d2017-04-07 17:05:45 +08001267 dma_fence_put(fence);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001268 if (r < 0)
1269 return r;
1270
1271 if (r == 0)
1272 break;
1273 }
1274
1275 memset(wait, 0, sizeof(*wait));
1276 wait->out.status = (r > 0);
1277
1278 return 0;
1279}
1280
1281/**
1282 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1283 *
1284 * @adev: amdgpu device
1285 * @filp: file private
1286 * @wait: wait parameters
1287 * @fences: array of drm_amdgpu_fence
1288 */
1289static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1290 struct drm_file *filp,
1291 union drm_amdgpu_wait_fences *wait,
1292 struct drm_amdgpu_fence *fences)
1293{
1294 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1295 uint32_t fence_count = wait->in.fence_count;
1296 uint32_t first = ~0;
1297 struct dma_fence **array;
1298 unsigned int i;
1299 long r;
1300
1301 /* Prepare the fence array */
1302 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1303
1304 if (array == NULL)
1305 return -ENOMEM;
1306
1307 for (i = 0; i < fence_count; i++) {
1308 struct dma_fence *fence;
1309
1310 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1311 if (IS_ERR(fence)) {
1312 r = PTR_ERR(fence);
1313 goto err_free_fence_array;
1314 } else if (fence) {
1315 array[i] = fence;
1316 } else { /* NULL, the fence has been already signaled */
1317 r = 1;
1318 goto out;
1319 }
1320 }
1321
1322 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1323 &first);
1324 if (r < 0)
1325 goto err_free_fence_array;
1326
1327out:
1328 memset(wait, 0, sizeof(*wait));
1329 wait->out.status = (r > 0);
1330 wait->out.first_signaled = first;
1331 /* set return value 0 to indicate success */
1332 r = 0;
1333
1334err_free_fence_array:
1335 for (i = 0; i < fence_count; i++)
1336 dma_fence_put(array[i]);
1337 kfree(array);
1338
1339 return r;
1340}
1341
1342/**
1343 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1344 *
1345 * @dev: drm device
1346 * @data: data from userspace
1347 * @filp: file private
1348 */
1349int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1350 struct drm_file *filp)
1351{
1352 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001353 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001354 union drm_amdgpu_wait_fences *wait = data;
1355 uint32_t fence_count = wait->in.fence_count;
1356 struct drm_amdgpu_fence *fences_user;
1357 struct drm_amdgpu_fence *fences;
1358 int r;
1359
Chunming Zhouf1892132017-05-15 16:48:27 +08001360 if (amdgpu_kms_vram_lost(adev, fpriv))
1361 return -ENODEV;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001362 /* Get the fences from userspace */
1363 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1364 GFP_KERNEL);
1365 if (fences == NULL)
1366 return -ENOMEM;
1367
Alex Xief4e7c7c2017-04-05 16:54:34 -04001368 fences_user = (void __user *)(uintptr_t)(wait->in.fences);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001369 if (copy_from_user(fences, fences_user,
1370 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1371 r = -EFAULT;
1372 goto err_free_fences;
1373 }
1374
1375 if (wait->in.wait_all)
1376 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1377 else
1378 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1379
1380err_free_fences:
1381 kfree(fences);
1382
1383 return r;
1384}
1385
1386/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001387 * amdgpu_cs_find_bo_va - find bo_va for VM address
1388 *
1389 * @parser: command submission parser context
1390 * @addr: VM address
1391 * @bo: resulting BO of the mapping found
1392 *
1393 * Search the buffer objects in the command submission context for a certain
1394 * virtual memory address. Returns allocation structure when found, NULL
1395 * otherwise.
1396 */
1397struct amdgpu_bo_va_mapping *
1398amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1399 uint64_t addr, struct amdgpu_bo **bo)
1400{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001401 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +01001402 unsigned i;
1403
1404 if (!parser->bo_list)
1405 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001406
1407 addr /= AMDGPU_GPU_PAGE_SIZE;
1408
Christian König15486fd22015-12-22 16:06:12 +01001409 for (i = 0; i < parser->bo_list->num_entries; i++) {
1410 struct amdgpu_bo_list_entry *lobj;
1411
1412 lobj = &parser->bo_list->array[i];
1413 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414 continue;
1415
Christian König15486fd22015-12-22 16:06:12 +01001416 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001417 if (mapping->start > addr ||
1418 addr > mapping->last)
Christian König7fc11952015-07-30 11:53:42 +02001419 continue;
1420
Christian König15486fd22015-12-22 16:06:12 +01001421 *bo = lobj->bo_va->bo;
Christian König7fc11952015-07-30 11:53:42 +02001422 return mapping;
1423 }
1424
Christian König15486fd22015-12-22 16:06:12 +01001425 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001426 if (mapping->start > addr ||
1427 addr > mapping->last)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428 continue;
1429
Christian König15486fd22015-12-22 16:06:12 +01001430 *bo = lobj->bo_va->bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001431 return mapping;
1432 }
1433 }
1434
1435 return NULL;
1436}
Christian Königc855e252016-09-05 17:00:57 +02001437
1438/**
1439 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1440 *
1441 * @parser: command submission parser context
1442 *
1443 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1444 */
1445int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1446{
1447 unsigned i;
1448 int r;
1449
1450 if (!parser->bo_list)
1451 return 0;
1452
1453 for (i = 0; i < parser->bo_list->num_entries; i++) {
1454 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1455
Christian Königbb990bb2016-09-09 16:32:33 +02001456 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +02001457 if (unlikely(r))
1458 return r;
Christian König03f48dd2016-08-15 17:00:22 +02001459
1460 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
1461 continue;
1462
1463 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1464 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
1465 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
1466 if (unlikely(r))
1467 return r;
Christian Königc855e252016-09-05 17:00:57 +02001468 }
1469
1470 return 0;
1471}