blob: cf44388b5bce6c460dd5fc02e43884c4b9a7bbb1 [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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 }
Ding Pixelc5f21c92017-01-18 17:26:38 +080095
96 if (!(*out_ring && (*out_ring)->adev)) {
97 DRM_ERROR("Ring %d is not initialized on IP %d\n",
98 ring, ip_type);
99 return -EINVAL;
100 }
101
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102 return 0;
103}
104
Christian König91acbeb2015-12-14 16:42:31 +0100105static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +0200106 struct drm_amdgpu_cs_chunk_fence *data,
107 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +0100108{
109 struct drm_gem_object *gobj;
Christian Königaa290402016-09-09 11:21:43 +0200110 unsigned long size;
Christian König91acbeb2015-12-14 16:42:31 +0100111
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100112 gobj = drm_gem_object_lookup(p->filp, data->handle);
Christian König91acbeb2015-12-14 16:42:31 +0100113 if (gobj == NULL)
114 return -EINVAL;
115
Christian König758ac172016-05-06 22:14:00 +0200116 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +0100117 p->uf_entry.priority = 0;
118 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
119 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100120 p->uf_entry.user_pages = NULL;
Christian Königaa290402016-09-09 11:21:43 +0200121
122 size = amdgpu_bo_size(p->uf_entry.robj);
123 if (size != PAGE_SIZE || (data->offset + 8) > size)
124 return -EINVAL;
125
Christian König758ac172016-05-06 22:14:00 +0200126 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +0100127
128 drm_gem_object_unreference_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +0200129
130 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
131 amdgpu_bo_unref(&p->uf_entry.robj);
132 return -EINVAL;
133 }
134
Christian König91acbeb2015-12-14 16:42:31 +0100135 return 0;
136}
137
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
139{
Christian König4c0b2422016-02-01 11:20:37 +0100140 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +0800141 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 union drm_amdgpu_cs *cs = data;
143 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +0300144 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +0100145 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +0200146 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +0300147 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +0300148 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149
Dan Carpenter1d263472015-09-23 13:59:28 +0300150 if (cs->in.num_chunks == 0)
151 return 0;
152
153 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
154 if (!chunk_array)
155 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156
Christian König3cb485f2015-05-11 15:34:59 +0200157 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
158 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300159 ret = -EINVAL;
160 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +0200161 }
Dan Carpenter1d263472015-09-23 13:59:28 +0300162
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163 /* get chunks */
Arnd Bergmann028423b2015-10-07 09:41:27 +0200164 chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 if (copy_from_user(chunk_array, chunk_array_user,
166 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300167 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +0100168 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 }
170
171 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800172 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300174 if (!p->chunks) {
175 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100176 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177 }
178
179 for (i = 0; i < p->nchunks; i++) {
180 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
181 struct drm_amdgpu_cs_chunk user_chunk;
182 uint32_t __user *cdata;
183
Arnd Bergmann028423b2015-10-07 09:41:27 +0200184 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 if (copy_from_user(&user_chunk, chunk_ptr,
186 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300187 ret = -EFAULT;
188 i--;
189 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190 }
191 p->chunks[i].chunk_id = user_chunk.chunk_id;
192 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193
194 size = p->chunks[i].length_dw;
Arnd Bergmann028423b2015-10-07 09:41:27 +0200195 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196
197 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
198 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300199 ret = -ENOMEM;
200 i--;
201 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202 }
203 size *= sizeof(uint32_t);
204 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300205 ret = -EFAULT;
206 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 }
208
Christian König9a5e8fb2015-06-23 17:07:03 +0200209 switch (p->chunks[i].chunk_id) {
210 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100211 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200212 break;
213
214 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100216 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300217 ret = -EINVAL;
218 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219 }
Christian König91acbeb2015-12-14 16:42:31 +0100220
Christian König758ac172016-05-06 22:14:00 +0200221 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
222 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100223 if (ret)
224 goto free_partial_kdata;
225
Christian König9a5e8fb2015-06-23 17:07:03 +0200226 break;
227
Christian König2b48d322015-06-19 17:31:29 +0200228 case AMDGPU_CHUNK_ID_DEPENDENCIES:
229 break;
230
Christian König9a5e8fb2015-06-23 17:07:03 +0200231 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300232 ret = -EINVAL;
233 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234 }
235 }
236
Monk Liuc5637832016-04-19 20:11:32 +0800237 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100238 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100239 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240
Christian Königb5f5acb2016-06-29 13:26:41 +0200241 if (p->uf_entry.robj)
242 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300244 return 0;
245
246free_all_kdata:
247 i = p->nchunks - 1;
248free_partial_kdata:
249 for (; i >= 0; i--)
250 drm_free_large(p->chunks[i].kdata);
251 kfree(p->chunks);
Dave Airlie607523d2017-03-10 12:13:04 +1000252 p->chunks = NULL;
253 p->nchunks = 0;
Christian König2a7d9bd2015-12-18 20:33:52 +0100254put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300255 amdgpu_ctx_put(p->ctx);
256free_chunk:
257 kfree(chunk_array);
258
259 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400260}
261
Marek Olšák95844d22016-08-17 23:49:27 +0200262/* Convert microseconds to bytes. */
263static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
264{
265 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
266 return 0;
267
268 /* Since accum_us is incremented by a million per second, just
269 * multiply it by the number of MB/s to get the number of bytes.
270 */
271 return us << adev->mm_stats.log2_max_MBps;
272}
273
274static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
275{
276 if (!adev->mm_stats.log2_max_MBps)
277 return 0;
278
279 return bytes >> adev->mm_stats.log2_max_MBps;
280}
281
282/* Returns how many bytes TTM can move right now. If no bytes can be moved,
283 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
284 * which means it can go over the threshold once. If that happens, the driver
285 * will be in debt and no other buffer migrations can be done until that debt
286 * is repaid.
287 *
288 * This approach allows moving a buffer of any size (it's important to allow
289 * that).
290 *
291 * The currency is simply time in microseconds and it increases as the clock
292 * ticks. The accumulated microseconds (us) are converted to bytes and
293 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294 */
295static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
296{
Marek Olšák95844d22016-08-17 23:49:27 +0200297 s64 time_us, increment_us;
298 u64 max_bytes;
299 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400300
Marek Olšák95844d22016-08-17 23:49:27 +0200301 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
302 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 *
Marek Olšák95844d22016-08-17 23:49:27 +0200304 * It means that in order to get full max MBps, at least 5 IBs per
305 * second must be submitted and not more than 200ms apart from each
306 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307 */
Marek Olšák95844d22016-08-17 23:49:27 +0200308 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400309
Marek Olšák95844d22016-08-17 23:49:27 +0200310 if (!adev->mm_stats.log2_max_MBps)
311 return 0;
312
313 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
314 used_vram = atomic64_read(&adev->vram_usage);
315 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
316
317 spin_lock(&adev->mm_stats.lock);
318
319 /* Increase the amount of accumulated us. */
320 time_us = ktime_to_us(ktime_get());
321 increment_us = time_us - adev->mm_stats.last_update_us;
322 adev->mm_stats.last_update_us = time_us;
323 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
324 us_upper_bound);
325
326 /* This prevents the short period of low performance when the VRAM
327 * usage is low and the driver is in debt or doesn't have enough
328 * accumulated us to fill VRAM quickly.
329 *
330 * The situation can occur in these cases:
331 * - a lot of VRAM is freed by userspace
332 * - the presence of a big buffer causes a lot of evictions
333 * (solution: split buffers into smaller ones)
334 *
335 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
336 * accum_us to a positive number.
337 */
338 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
339 s64 min_us;
340
341 /* Be more aggresive on dGPUs. Try to fill a portion of free
342 * VRAM now.
343 */
344 if (!(adev->flags & AMD_IS_APU))
345 min_us = bytes_to_us(adev, free_vram / 4);
346 else
347 min_us = 0; /* Reset accum_us on APUs. */
348
349 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
350 }
351
352 /* This returns 0 if the driver is in debt to disallow (optional)
353 * buffer moves.
354 */
355 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
356
357 spin_unlock(&adev->mm_stats.lock);
358 return max_bytes;
359}
360
361/* Report how many bytes have really been moved for the last command
362 * submission. This can result in a debt that can stop buffer migrations
363 * temporarily.
364 */
Samuel Pitoisetfad06122017-02-09 11:33:37 +0100365void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200366{
367 spin_lock(&adev->mm_stats.lock);
368 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
369 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370}
371
Chunming Zhou14fd8332016-08-04 13:05:46 +0800372static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
373 struct amdgpu_bo *bo)
374{
Christian Königa7d64de2016-09-15 14:58:48 +0200375 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800376 u64 initial_bytes_moved;
377 uint32_t domain;
378 int r;
379
380 if (bo->pin_count)
381 return 0;
382
Marek Olšák95844d22016-08-17 23:49:27 +0200383 /* Don't move this buffer if we have depleted our allowance
384 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800385 */
Marek Olšák95844d22016-08-17 23:49:27 +0200386 if (p->bytes_moved < p->bytes_moved_threshold)
Chunming Zhou14fd8332016-08-04 13:05:46 +0800387 domain = bo->prefered_domains;
388 else
389 domain = bo->allowed_domains;
390
391retry:
392 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200393 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800394 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200395 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Chunming Zhou14fd8332016-08-04 13:05:46 +0800396 initial_bytes_moved;
397
Christian König1abdc3d2016-08-31 17:28:11 +0200398 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
399 domain = bo->allowed_domains;
400 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800401 }
402
403 return r;
404}
405
Christian König662bfa62016-09-01 12:13:18 +0200406/* Last resort, try to evict something from the current working set */
407static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200408 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200409{
Christian Königf7da30d2016-09-28 12:03:04 +0200410 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200411 int r;
412
413 if (!p->evictable)
414 return false;
415
416 for (;&p->evictable->tv.head != &p->validated;
417 p->evictable = list_prev_entry(p->evictable, tv.head)) {
418
419 struct amdgpu_bo_list_entry *candidate = p->evictable;
420 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200421 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian König662bfa62016-09-01 12:13:18 +0200422 u64 initial_bytes_moved;
423 uint32_t other;
424
425 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200426 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200427 break;
428
429 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
430
431 /* Check if this BO is in one of the domains we need space for */
432 if (!(other & domain))
433 continue;
434
435 /* Check if we can move this BO somewhere else */
436 other = bo->allowed_domains & ~domain;
437 if (!other)
438 continue;
439
440 /* Good we can try to move this BO somewhere else */
441 amdgpu_ttm_placement_from_domain(bo, other);
Christian Königa7d64de2016-09-15 14:58:48 +0200442 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200443 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200444 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200445 initial_bytes_moved;
446
447 if (unlikely(r))
448 break;
449
450 p->evictable = list_prev_entry(p->evictable, tv.head);
451 list_move(&candidate->tv.head, &p->validated);
452
453 return true;
454 }
455
456 return false;
457}
458
Christian Königf7da30d2016-09-28 12:03:04 +0200459static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
460{
461 struct amdgpu_cs_parser *p = param;
462 int r;
463
464 do {
465 r = amdgpu_cs_bo_validate(p, bo);
466 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
467 if (r)
468 return r;
469
470 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500471 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200472
473 return r;
474}
475
Baoyou Xie761c2e82016-09-03 13:57:14 +0800476static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200477 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 int r;
481
Christian Königa5b75052015-09-03 16:40:39 +0200482 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100483 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100484 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100485 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486
Christian Königcc325d12016-02-08 11:08:35 +0100487 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
488 if (usermm && usermm != current->mm)
489 return -EPERM;
490
Christian König2f568db2016-02-23 12:36:59 +0100491 /* Check if we have user pages and nobody bound the BO already */
492 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
493 size_t size = sizeof(struct page *);
494
495 size *= bo->tbo.ttm->num_pages;
496 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
497 binding_userptr = true;
498 }
499
Christian König662bfa62016-09-01 12:13:18 +0200500 if (p->evictable == lobj)
501 p->evictable = NULL;
502
Christian Königf7da30d2016-09-28 12:03:04 +0200503 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800504 if (r)
Christian König36409d122015-12-21 20:31:35 +0100505 return r;
Christian König662bfa62016-09-01 12:13:18 +0200506
Christian König2f568db2016-02-23 12:36:59 +0100507 if (binding_userptr) {
508 drm_free_large(lobj->user_pages);
509 lobj->user_pages = NULL;
510 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 }
512 return 0;
513}
514
Christian König2a7d9bd2015-12-18 20:33:52 +0100515static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
516 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517{
518 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100519 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200520 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800521 bool need_mmap_lock = false;
Christian König2f568db2016-02-23 12:36:59 +0100522 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100523 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
Christian König2a7d9bd2015-12-18 20:33:52 +0100525 INIT_LIST_HEAD(&p->validated);
526
527 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800528 if (p->bo_list) {
Christian König211dff52016-02-22 15:40:59 +0100529 need_mmap_lock = p->bo_list->first_userptr !=
530 p->bo_list->num_entries;
Christian König636ce252015-12-18 21:26:47 +0100531 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
monk.liu840d5142015-04-27 15:19:20 +0800532 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533
Christian König3c0eea62015-12-11 14:39:05 +0100534 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100535 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536
Christian König758ac172016-05-06 22:14:00 +0200537 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100538 list_add(&p->uf_entry.tv.head, &p->validated);
539
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540 if (need_mmap_lock)
541 down_read(&current->mm->mmap_sem);
542
Christian König2f568db2016-02-23 12:36:59 +0100543 while (1) {
544 struct list_head need_pages;
545 unsigned i;
546
547 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
548 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200549 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800550 if (r != -ERESTARTSYS)
551 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100552 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200553 }
Christian König2f568db2016-02-23 12:36:59 +0100554
555 /* Without a BO list we don't have userptr BOs */
556 if (!p->bo_list)
557 break;
558
559 INIT_LIST_HEAD(&need_pages);
560 for (i = p->bo_list->first_userptr;
561 i < p->bo_list->num_entries; ++i) {
562
563 e = &p->bo_list->array[i];
564
565 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
566 &e->user_invalidated) && e->user_pages) {
567
568 /* We acquired a page array, but somebody
569 * invalidated it. Free it an try again
570 */
571 release_pages(e->user_pages,
572 e->robj->tbo.ttm->num_pages,
573 false);
574 drm_free_large(e->user_pages);
575 e->user_pages = NULL;
576 }
577
578 if (e->robj->tbo.ttm->state != tt_bound &&
579 !e->user_pages) {
580 list_del(&e->tv.head);
581 list_add(&e->tv.head, &need_pages);
582
583 amdgpu_bo_unreserve(e->robj);
584 }
585 }
586
587 if (list_empty(&need_pages))
588 break;
589
590 /* Unreserve everything again. */
591 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
592
Marek Olšákf1037952016-07-30 00:48:39 +0200593 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100594 if (!--tries) {
595 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200596 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100597 goto error_free_pages;
598 }
599
600 /* Fill the page arrays for all useptrs. */
601 list_for_each_entry(e, &need_pages, tv.head) {
602 struct ttm_tt *ttm = e->robj->tbo.ttm;
603
604 e->user_pages = drm_calloc_large(ttm->num_pages,
605 sizeof(struct page*));
606 if (!e->user_pages) {
607 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200608 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100609 goto error_free_pages;
610 }
611
612 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
613 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200614 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100615 drm_free_large(e->user_pages);
616 e->user_pages = NULL;
617 goto error_free_pages;
618 }
619 }
620
621 /* And try again. */
622 list_splice(&need_pages, &p->validated);
623 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
Christian Königf69f90a12015-12-21 19:47:42 +0100625 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
626 p->bytes_moved = 0;
Christian König662bfa62016-09-01 12:13:18 +0200627 p->evictable = list_last_entry(&p->validated,
628 struct amdgpu_bo_list_entry,
629 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100630
Christian Königf7da30d2016-09-28 12:03:04 +0200631 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
632 amdgpu_cs_validate, p);
633 if (r) {
634 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
635 goto error_validate;
636 }
637
Christian Königf69f90a12015-12-21 19:47:42 +0100638 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200639 if (r) {
640 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200641 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200642 }
Christian Königa5b75052015-09-03 16:40:39 +0200643
Christian Königf69f90a12015-12-21 19:47:42 +0100644 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200645 if (r) {
646 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100647 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200648 }
Christian Königa8480302016-01-05 16:03:39 +0100649
Marek Olšák95844d22016-08-17 23:49:27 +0200650 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
651
Christian König5a712a82016-06-21 16:28:15 +0200652 fpriv->vm.last_eviction_counter =
653 atomic64_read(&p->adev->num_evictions);
654
Christian Königa8480302016-01-05 16:03:39 +0100655 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200656 struct amdgpu_bo *gds = p->bo_list->gds_obj;
657 struct amdgpu_bo *gws = p->bo_list->gws_obj;
658 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100659 struct amdgpu_vm *vm = &fpriv->vm;
660 unsigned i;
661
662 for (i = 0; i < p->bo_list->num_entries; i++) {
663 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
664
665 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
666 }
Christian Königd88bf582016-05-06 17:50:03 +0200667
668 if (gds) {
669 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
670 p->job->gds_size = amdgpu_bo_size(gds);
671 }
672 if (gws) {
673 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
674 p->job->gws_size = amdgpu_bo_size(gws);
675 }
676 if (oa) {
677 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
678 p->job->oa_size = amdgpu_bo_size(oa);
679 }
Christian Königa8480302016-01-05 16:03:39 +0100680 }
Christian Königa5b75052015-09-03 16:40:39 +0200681
Christian Königc855e252016-09-05 17:00:57 +0200682 if (!r && p->uf_entry.robj) {
683 struct amdgpu_bo *uf = p->uf_entry.robj;
684
Christian Königbb990bb2016-09-09 16:32:33 +0200685 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200686 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
687 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200688
Christian Königa5b75052015-09-03 16:40:39 +0200689error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100690 if (r) {
691 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200692 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100693 }
Christian Königa5b75052015-09-03 16:40:39 +0200694
Christian König2f568db2016-02-23 12:36:59 +0100695error_free_pages:
696
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 if (need_mmap_lock)
698 up_read(&current->mm->mmap_sem);
699
Christian König2f568db2016-02-23 12:36:59 +0100700 if (p->bo_list) {
701 for (i = p->bo_list->first_userptr;
702 i < p->bo_list->num_entries; ++i) {
703 e = &p->bo_list->array[i];
704
705 if (!e->user_pages)
706 continue;
707
708 release_pages(e->user_pages,
709 e->robj->tbo.ttm->num_pages,
710 false);
711 drm_free_large(e->user_pages);
712 }
713 }
714
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 return r;
716}
717
718static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
719{
720 struct amdgpu_bo_list_entry *e;
721 int r;
722
723 list_for_each_entry(e, &p->validated, tv.head) {
724 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100725 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726
727 if (r)
728 return r;
729 }
730 return 0;
731}
732
Christian König984810f2015-11-14 21:05:35 +0100733/**
734 * cs_parser_fini() - clean parser states
735 * @parser: parser structure holding parsing context.
736 * @error: error number
737 *
738 * If error is set than unvalidate buffer, otherwise just free memory
739 * used by parsing context.
740 **/
741static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800742{
Christian Königeceb8a12016-01-11 15:35:21 +0100743 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100744 unsigned i;
745
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500747 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
748
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100750 &parser->validated,
751 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752 } else if (backoff) {
753 ttm_eu_backoff_reservation(&parser->ticket,
754 &parser->validated);
755 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100756 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100757
Christian König3cb485f2015-05-11 15:34:59 +0200758 if (parser->ctx)
759 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800760 if (parser->bo_list)
761 amdgpu_bo_list_put(parser->bo_list);
762
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763 for (i = 0; i < parser->nchunks; i++)
764 drm_free_large(parser->chunks[i].kdata);
765 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100766 if (parser->job)
767 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100768 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769}
770
Junwei Zhangb85891b2017-01-16 13:59:01 +0800771static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400772{
773 struct amdgpu_device *adev = p->adev;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800774 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
775 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 struct amdgpu_bo_va *bo_va;
777 struct amdgpu_bo *bo;
778 int i, r;
779
Christian König194d2162016-10-12 15:13:52 +0200780 r = amdgpu_vm_update_directories(adev, vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781 if (r)
782 return r;
783
Christian Königa24960f2016-10-12 13:20:52 +0200784 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200785 if (r)
786 return r;
787
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100788 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 if (r)
790 return r;
791
Junwei Zhangb85891b2017-01-16 13:59:01 +0800792 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
793 if (r)
794 return r;
795
796 r = amdgpu_sync_fence(adev, &p->job->sync,
797 fpriv->prt_va->last_pt_update);
798 if (r)
799 return r;
800
Monk Liu24936642017-01-09 15:54:32 +0800801 if (amdgpu_sriov_vf(adev)) {
802 struct dma_fence *f;
803 bo_va = vm->csa_bo_va;
804 BUG_ON(!bo_va);
805 r = amdgpu_vm_bo_update(adev, bo_va, false);
806 if (r)
807 return r;
808
809 f = bo_va->last_pt_update;
810 r = amdgpu_sync_fence(adev, &p->job->sync, f);
811 if (r)
812 return r;
813 }
814
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400815 if (p->bo_list) {
816 for (i = 0; i < p->bo_list->num_entries; i++) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100817 struct dma_fence *f;
Christian König91e1a522015-07-06 22:06:40 +0200818
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819 /* ignore duplicates */
820 bo = p->bo_list->array[i].robj;
821 if (!bo)
822 continue;
823
824 bo_va = p->bo_list->array[i].bo_va;
825 if (bo_va == NULL)
826 continue;
827
Christian König99e124f2016-08-16 14:43:17 +0200828 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829 if (r)
830 return r;
831
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800832 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100833 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200834 if (r)
835 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400836 }
Christian Königb495bd32015-09-10 14:00:35 +0200837
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838 }
839
Christian Könige86f9ce2016-02-08 12:13:05 +0100840 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200841
842 if (amdgpu_vm_debug && p->bo_list) {
843 /* Invalidate all BOs to test for userspace bugs */
844 for (i = 0; i < p->bo_list->num_entries; i++) {
845 /* ignore duplicates */
846 bo = p->bo_list->array[i].robj;
847 if (!bo)
848 continue;
849
850 amdgpu_vm_bo_invalidate(adev, bo);
851 }
852 }
853
854 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400855}
856
857static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100858 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400859{
Christian Königb07c60c2016-01-31 12:29:04 +0100860 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100862 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400863 int i, r;
864
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100866 if (ring->funcs->parse_cs) {
867 for (i = 0; i < p->job->num_ibs; i++) {
868 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400869 if (r)
870 return r;
871 }
Christian König45088ef2016-10-05 16:49:19 +0200872 }
873
874 if (p->job->vm) {
Christian König67003a12016-10-12 14:46:26 +0200875 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
Christian König9a795882016-06-22 14:25:55 +0200876
Junwei Zhangb85891b2017-01-16 13:59:01 +0800877 r = amdgpu_bo_vm_update_pte(p);
Christian König9a795882016-06-22 14:25:55 +0200878 if (r)
879 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880 }
881
Christian König9a795882016-06-22 14:25:55 +0200882 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400883}
884
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
886 struct amdgpu_cs_parser *parser)
887{
888 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
889 struct amdgpu_vm *vm = &fpriv->vm;
890 int i, j;
Monk Liu9a1b3af2017-03-08 15:51:13 +0800891 int r, ce_preempt = 0, de_preempt = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892
Christian König50838c82016-02-03 13:44:52 +0100893 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894 struct amdgpu_cs_chunk *chunk;
895 struct amdgpu_ib *ib;
896 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400897 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400898
899 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100900 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400901 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
902
903 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
904 continue;
905
Monk Liu9a1b3af2017-03-08 15:51:13 +0800906 if (ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
907 if (ib->flags & AMDGPU_IB_FLAG_CE)
908 ce_preempt++;
909 else
910 de_preempt++;
911 }
912
913 /* only one preemptible IB per submit for me/ce */
914 if (ce_preempt > 1 || de_preempt > 1)
915 return -EINVAL;
916
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400917 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
918 chunk_ib->ip_instance, chunk_ib->ring,
919 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200920 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922
Monk Liu2a9ceb82017-03-28 11:00:03 +0800923 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
Monk Liu753ad492016-08-26 13:28:28 +0800924 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
925 if (!parser->ctx->preamble_presented) {
926 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
927 parser->ctx->preamble_presented = true;
928 }
929 }
930
Christian Königb07c60c2016-01-31 12:29:04 +0100931 if (parser->job->ring && parser->job->ring != ring)
932 return -EINVAL;
933
934 parser->job->ring = ring;
935
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200937 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200938 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200939 uint64_t offset;
940 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200941
Christian König4802ce12015-06-10 17:20:11 +0200942 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
943 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200944 if (!aobj) {
945 DRM_ERROR("IB va_start is invalid\n");
946 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947 }
948
Christian König4802ce12015-06-10 17:20:11 +0200949 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
950 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
951 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
952 return -EINVAL;
953 }
954
Marek Olšák3ccec532015-06-02 17:44:49 +0200955 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200956 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400957 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958 return r;
959 }
960
Christian König4802ce12015-06-10 17:20:11 +0200961 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
962 kptr += chunk_ib->va_start - offset;
963
Christian König45088ef2016-10-05 16:49:19 +0200964 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 if (r) {
966 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400967 return r;
968 }
969
970 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
971 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100973 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 if (r) {
975 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976 return r;
977 }
978
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980
Christian König45088ef2016-10-05 16:49:19 +0200981 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200982 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800983 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984 j++;
985 }
986
Christian König758ac172016-05-06 22:14:00 +0200987 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200988 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +0200989 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
990 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +0200991 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992
993 return 0;
994}
995
Christian König2b48d322015-06-19 17:31:29 +0200996static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
997 struct amdgpu_cs_parser *p)
998{
Christian König76a1ea62015-07-06 19:42:10 +0200999 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +02001000 int i, j, r;
1001
Christian König2b48d322015-06-19 17:31:29 +02001002 for (i = 0; i < p->nchunks; ++i) {
1003 struct drm_amdgpu_cs_chunk_dep *deps;
1004 struct amdgpu_cs_chunk *chunk;
1005 unsigned num_deps;
1006
1007 chunk = &p->chunks[i];
1008
1009 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
1010 continue;
1011
1012 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1013 num_deps = chunk->length_dw * 4 /
1014 sizeof(struct drm_amdgpu_cs_chunk_dep);
1015
1016 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +02001017 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +02001018 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001019 struct dma_fence *fence;
Christian König2b48d322015-06-19 17:31:29 +02001020
1021 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
1022 deps[j].ip_instance,
1023 deps[j].ring, &ring);
1024 if (r)
1025 return r;
1026
Christian König76a1ea62015-07-06 19:42:10 +02001027 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
1028 if (ctx == NULL)
1029 return -EINVAL;
1030
Christian König21c16bf2015-07-07 17:24:49 +02001031 fence = amdgpu_ctx_get_fence(ctx, ring,
1032 deps[j].handle);
1033 if (IS_ERR(fence)) {
1034 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +02001035 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +02001036 return r;
Christian König21c16bf2015-07-07 17:24:49 +02001037
1038 } else if (fence) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001039 r = amdgpu_sync_fence(adev, &p->job->sync,
1040 fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001041 dma_fence_put(fence);
Christian König21c16bf2015-07-07 17:24:49 +02001042 amdgpu_ctx_put(ctx);
1043 if (r)
1044 return r;
Christian König76a1ea62015-07-06 19:42:10 +02001045 }
Christian König2b48d322015-06-19 17:31:29 +02001046 }
1047 }
1048
1049 return 0;
1050}
1051
Christian Königcd75dc62016-01-31 11:30:55 +01001052static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1053 union drm_amdgpu_cs *cs)
1054{
Christian Königb07c60c2016-01-31 12:29:04 +01001055 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001056 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001057 struct amdgpu_job *job;
Monk Liue6869412016-03-07 12:49:55 +08001058 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001059
Christian König50838c82016-02-03 13:44:52 +01001060 job = p->job;
1061 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001062
Christian König595a9cd2016-06-30 10:52:03 +02001063 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001064 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001065 amdgpu_job_free(job);
Monk Liue6869412016-03-07 12:49:55 +08001066 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001067 }
1068
Monk Liue6869412016-03-07 12:49:55 +08001069 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001070 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001071 p->fence = dma_fence_get(&job->base.s_fence->finished);
Christian König595a9cd2016-06-30 10:52:03 +02001072 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
Christian König758ac172016-05-06 22:14:00 +02001073 job->uf_sequence = cs->out.handle;
Christian Königa5fb4ec2016-06-29 15:10:31 +02001074 amdgpu_job_free_resources(job);
Christian Königcd75dc62016-01-31 11:30:55 +01001075
1076 trace_amdgpu_cs_ioctl(job);
1077 amd_sched_entity_push_job(&job->base);
1078
1079 return 0;
1080}
1081
Chunming Zhou049fc522015-07-21 14:36:51 +08001082int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1083{
1084 struct amdgpu_device *adev = dev->dev_private;
1085 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001086 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001087 bool reserved_buffers = false;
1088 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001089
Christian König0c418f12015-09-01 15:13:53 +02001090 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001091 return -EBUSY;
Chunming Zhou049fc522015-07-21 14:36:51 +08001092
Christian König7e52a812015-11-04 15:44:39 +01001093 parser.adev = adev;
1094 parser.filp = filp;
1095
1096 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001098 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001099 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 }
Huang Ruia414cd72016-10-30 23:05:47 +08001101
Christian König2a7d9bd2015-12-18 20:33:52 +01001102 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001103 if (r) {
1104 if (r == -ENOMEM)
1105 DRM_ERROR("Not enough memory for command submission!\n");
1106 else if (r != -ERESTARTSYS)
1107 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1108 goto out;
Christian König26a69802015-08-18 21:09:33 +02001109 }
1110
Huang Ruia414cd72016-10-30 23:05:47 +08001111 reserved_buffers = true;
1112 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001113 if (r)
1114 goto out;
1115
Huang Ruia414cd72016-10-30 23:05:47 +08001116 r = amdgpu_cs_dependencies(adev, &parser);
1117 if (r) {
1118 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1119 goto out;
1120 }
1121
Christian König50838c82016-02-03 13:44:52 +01001122 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001123 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001124
Christian König7e52a812015-11-04 15:44:39 +01001125 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001126 if (r)
1127 goto out;
1128
Christian König4acabfe2016-01-31 11:32:04 +01001129 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001131out:
Christian König7e52a812015-11-04 15:44:39 +01001132 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133 return r;
1134}
1135
1136/**
1137 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1138 *
1139 * @dev: drm device
1140 * @data: data from userspace
1141 * @filp: file private
1142 *
1143 * Wait for the command submission identified by handle to finish.
1144 */
1145int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1146 struct drm_file *filp)
1147{
1148 union drm_amdgpu_wait_cs *wait = data;
1149 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001151 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001152 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001153 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 long r;
1155
Christian König21c16bf2015-07-07 17:24:49 +02001156 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
1157 wait->in.ring, &ring);
1158 if (r)
1159 return r;
1160
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001161 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1162 if (ctx == NULL)
1163 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001164
1165 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1166 if (IS_ERR(fence))
1167 r = PTR_ERR(fence);
1168 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001169 r = dma_fence_wait_timeout(fence, true, timeout);
1170 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001171 } else
Christian König21c16bf2015-07-07 17:24:49 +02001172 r = 1;
1173
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001174 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001175 if (r < 0)
1176 return r;
1177
1178 memset(wait, 0, sizeof(*wait));
1179 wait->out.status = (r == 0);
1180
1181 return 0;
1182}
1183
1184/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001185 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1186 *
1187 * @adev: amdgpu device
1188 * @filp: file private
1189 * @user: drm_amdgpu_fence copied from user space
1190 */
1191static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1192 struct drm_file *filp,
1193 struct drm_amdgpu_fence *user)
1194{
1195 struct amdgpu_ring *ring;
1196 struct amdgpu_ctx *ctx;
1197 struct dma_fence *fence;
1198 int r;
1199
1200 r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance,
1201 user->ring, &ring);
1202 if (r)
1203 return ERR_PTR(r);
1204
1205 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1206 if (ctx == NULL)
1207 return ERR_PTR(-EINVAL);
1208
1209 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1210 amdgpu_ctx_put(ctx);
1211
1212 return fence;
1213}
1214
1215/**
1216 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1217 *
1218 * @adev: amdgpu device
1219 * @filp: file private
1220 * @wait: wait parameters
1221 * @fences: array of drm_amdgpu_fence
1222 */
1223static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1224 struct drm_file *filp,
1225 union drm_amdgpu_wait_fences *wait,
1226 struct drm_amdgpu_fence *fences)
1227{
1228 uint32_t fence_count = wait->in.fence_count;
1229 unsigned int i;
1230 long r = 1;
1231
1232 for (i = 0; i < fence_count; i++) {
1233 struct dma_fence *fence;
1234 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1235
1236 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1237 if (IS_ERR(fence))
1238 return PTR_ERR(fence);
1239 else if (!fence)
1240 continue;
1241
1242 r = dma_fence_wait_timeout(fence, true, timeout);
1243 if (r < 0)
1244 return r;
1245
1246 if (r == 0)
1247 break;
1248 }
1249
1250 memset(wait, 0, sizeof(*wait));
1251 wait->out.status = (r > 0);
1252
1253 return 0;
1254}
1255
1256/**
1257 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1258 *
1259 * @adev: amdgpu device
1260 * @filp: file private
1261 * @wait: wait parameters
1262 * @fences: array of drm_amdgpu_fence
1263 */
1264static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1265 struct drm_file *filp,
1266 union drm_amdgpu_wait_fences *wait,
1267 struct drm_amdgpu_fence *fences)
1268{
1269 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1270 uint32_t fence_count = wait->in.fence_count;
1271 uint32_t first = ~0;
1272 struct dma_fence **array;
1273 unsigned int i;
1274 long r;
1275
1276 /* Prepare the fence array */
1277 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1278
1279 if (array == NULL)
1280 return -ENOMEM;
1281
1282 for (i = 0; i < fence_count; i++) {
1283 struct dma_fence *fence;
1284
1285 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1286 if (IS_ERR(fence)) {
1287 r = PTR_ERR(fence);
1288 goto err_free_fence_array;
1289 } else if (fence) {
1290 array[i] = fence;
1291 } else { /* NULL, the fence has been already signaled */
1292 r = 1;
1293 goto out;
1294 }
1295 }
1296
1297 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1298 &first);
1299 if (r < 0)
1300 goto err_free_fence_array;
1301
1302out:
1303 memset(wait, 0, sizeof(*wait));
1304 wait->out.status = (r > 0);
1305 wait->out.first_signaled = first;
1306 /* set return value 0 to indicate success */
1307 r = 0;
1308
1309err_free_fence_array:
1310 for (i = 0; i < fence_count; i++)
1311 dma_fence_put(array[i]);
1312 kfree(array);
1313
1314 return r;
1315}
1316
1317/**
1318 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1319 *
1320 * @dev: drm device
1321 * @data: data from userspace
1322 * @filp: file private
1323 */
1324int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1325 struct drm_file *filp)
1326{
1327 struct amdgpu_device *adev = dev->dev_private;
1328 union drm_amdgpu_wait_fences *wait = data;
1329 uint32_t fence_count = wait->in.fence_count;
1330 struct drm_amdgpu_fence *fences_user;
1331 struct drm_amdgpu_fence *fences;
1332 int r;
1333
1334 /* Get the fences from userspace */
1335 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1336 GFP_KERNEL);
1337 if (fences == NULL)
1338 return -ENOMEM;
1339
1340 fences_user = (void __user *)(unsigned long)(wait->in.fences);
1341 if (copy_from_user(fences, fences_user,
1342 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1343 r = -EFAULT;
1344 goto err_free_fences;
1345 }
1346
1347 if (wait->in.wait_all)
1348 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1349 else
1350 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1351
1352err_free_fences:
1353 kfree(fences);
1354
1355 return r;
1356}
1357
1358/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359 * amdgpu_cs_find_bo_va - find bo_va for VM address
1360 *
1361 * @parser: command submission parser context
1362 * @addr: VM address
1363 * @bo: resulting BO of the mapping found
1364 *
1365 * Search the buffer objects in the command submission context for a certain
1366 * virtual memory address. Returns allocation structure when found, NULL
1367 * otherwise.
1368 */
1369struct amdgpu_bo_va_mapping *
1370amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1371 uint64_t addr, struct amdgpu_bo **bo)
1372{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001373 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +01001374 unsigned i;
1375
1376 if (!parser->bo_list)
1377 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378
1379 addr /= AMDGPU_GPU_PAGE_SIZE;
1380
Christian König15486fd22015-12-22 16:06:12 +01001381 for (i = 0; i < parser->bo_list->num_entries; i++) {
1382 struct amdgpu_bo_list_entry *lobj;
1383
1384 lobj = &parser->bo_list->array[i];
1385 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 continue;
1387
Christian König15486fd22015-12-22 16:06:12 +01001388 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian König7fc11952015-07-30 11:53:42 +02001389 if (mapping->it.start > addr ||
1390 addr > mapping->it.last)
1391 continue;
1392
Christian König15486fd22015-12-22 16:06:12 +01001393 *bo = lobj->bo_va->bo;
Christian König7fc11952015-07-30 11:53:42 +02001394 return mapping;
1395 }
1396
Christian König15486fd22015-12-22 16:06:12 +01001397 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 if (mapping->it.start > addr ||
1399 addr > mapping->it.last)
1400 continue;
1401
Christian König15486fd22015-12-22 16:06:12 +01001402 *bo = lobj->bo_va->bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 return mapping;
1404 }
1405 }
1406
1407 return NULL;
1408}
Christian Königc855e252016-09-05 17:00:57 +02001409
1410/**
1411 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1412 *
1413 * @parser: command submission parser context
1414 *
1415 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1416 */
1417int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1418{
1419 unsigned i;
1420 int r;
1421
1422 if (!parser->bo_list)
1423 return 0;
1424
1425 for (i = 0; i < parser->bo_list->num_entries; i++) {
1426 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1427
Christian Königbb990bb2016-09-09 16:32:33 +02001428 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +02001429 if (unlikely(r))
1430 return r;
Christian König03f48dd2016-08-15 17:00:22 +02001431
1432 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
1433 continue;
1434
1435 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1436 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
1437 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
1438 if (unlikely(r))
1439 return r;
Christian Königc855e252016-09-05 17:00:57 +02001440 }
1441
1442 return 0;
1443}