blob: d2d0f60ff36d1f2fd4a80ef8b43d2d3d9737e1f9 [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;
85 }
Ding Pixelc5f21c92017-01-18 17:26:38 +080086
87 if (!(*out_ring && (*out_ring)->adev)) {
88 DRM_ERROR("Ring %d is not initialized on IP %d\n",
89 ring, ip_type);
90 return -EINVAL;
91 }
92
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 return 0;
94}
95
Christian König91acbeb2015-12-14 16:42:31 +010096static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +020097 struct drm_amdgpu_cs_chunk_fence *data,
98 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +010099{
100 struct drm_gem_object *gobj;
Christian Königaa290402016-09-09 11:21:43 +0200101 unsigned long size;
Christian König91acbeb2015-12-14 16:42:31 +0100102
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100103 gobj = drm_gem_object_lookup(p->filp, data->handle);
Christian König91acbeb2015-12-14 16:42:31 +0100104 if (gobj == NULL)
105 return -EINVAL;
106
Christian König758ac172016-05-06 22:14:00 +0200107 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +0100108 p->uf_entry.priority = 0;
109 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
110 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100111 p->uf_entry.user_pages = NULL;
Christian Königaa290402016-09-09 11:21:43 +0200112
113 size = amdgpu_bo_size(p->uf_entry.robj);
114 if (size != PAGE_SIZE || (data->offset + 8) > size)
115 return -EINVAL;
116
Christian König758ac172016-05-06 22:14:00 +0200117 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +0100118
119 drm_gem_object_unreference_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +0200120
121 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
122 amdgpu_bo_unref(&p->uf_entry.robj);
123 return -EINVAL;
124 }
125
Christian König91acbeb2015-12-14 16:42:31 +0100126 return 0;
127}
128
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
130{
Christian König4c0b2422016-02-01 11:20:37 +0100131 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +0800132 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 union drm_amdgpu_cs *cs = data;
134 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +0300135 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +0100136 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +0200137 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +0300138 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +0300139 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140
Dan Carpenter1d263472015-09-23 13:59:28 +0300141 if (cs->in.num_chunks == 0)
142 return 0;
143
144 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
145 if (!chunk_array)
146 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147
Christian König3cb485f2015-05-11 15:34:59 +0200148 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
149 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300150 ret = -EINVAL;
151 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +0200152 }
Dan Carpenter1d263472015-09-23 13:59:28 +0300153
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 /* get chunks */
Arnd Bergmann028423b2015-10-07 09:41:27 +0200155 chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 if (copy_from_user(chunk_array, chunk_array_user,
157 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300158 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +0100159 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 }
161
162 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800163 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300165 if (!p->chunks) {
166 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100167 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 }
169
170 for (i = 0; i < p->nchunks; i++) {
171 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
172 struct drm_amdgpu_cs_chunk user_chunk;
173 uint32_t __user *cdata;
174
Arnd Bergmann028423b2015-10-07 09:41:27 +0200175 chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 if (copy_from_user(&user_chunk, chunk_ptr,
177 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300178 ret = -EFAULT;
179 i--;
180 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181 }
182 p->chunks[i].chunk_id = user_chunk.chunk_id;
183 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184
185 size = p->chunks[i].length_dw;
Arnd Bergmann028423b2015-10-07 09:41:27 +0200186 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400187
188 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
189 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300190 ret = -ENOMEM;
191 i--;
192 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193 }
194 size *= sizeof(uint32_t);
195 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300196 ret = -EFAULT;
197 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 }
199
Christian König9a5e8fb2015-06-23 17:07:03 +0200200 switch (p->chunks[i].chunk_id) {
201 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100202 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200203 break;
204
205 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100207 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300208 ret = -EINVAL;
209 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210 }
Christian König91acbeb2015-12-14 16:42:31 +0100211
Christian König758ac172016-05-06 22:14:00 +0200212 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
213 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100214 if (ret)
215 goto free_partial_kdata;
216
Christian König9a5e8fb2015-06-23 17:07:03 +0200217 break;
218
Christian König2b48d322015-06-19 17:31:29 +0200219 case AMDGPU_CHUNK_ID_DEPENDENCIES:
220 break;
221
Christian König9a5e8fb2015-06-23 17:07:03 +0200222 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300223 ret = -EINVAL;
224 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225 }
226 }
227
Monk Liuc5637832016-04-19 20:11:32 +0800228 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100229 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100230 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231
Christian Königb5f5acb2016-06-29 13:26:41 +0200232 if (p->uf_entry.robj)
233 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300235 return 0;
236
237free_all_kdata:
238 i = p->nchunks - 1;
239free_partial_kdata:
240 for (; i >= 0; i--)
241 drm_free_large(p->chunks[i].kdata);
242 kfree(p->chunks);
Christian König2a7d9bd2015-12-18 20:33:52 +0100243put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300244 amdgpu_ctx_put(p->ctx);
245free_chunk:
246 kfree(chunk_array);
247
248 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400249}
250
Marek Olšák95844d22016-08-17 23:49:27 +0200251/* Convert microseconds to bytes. */
252static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
253{
254 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
255 return 0;
256
257 /* Since accum_us is incremented by a million per second, just
258 * multiply it by the number of MB/s to get the number of bytes.
259 */
260 return us << adev->mm_stats.log2_max_MBps;
261}
262
263static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
264{
265 if (!adev->mm_stats.log2_max_MBps)
266 return 0;
267
268 return bytes >> adev->mm_stats.log2_max_MBps;
269}
270
271/* Returns how many bytes TTM can move right now. If no bytes can be moved,
272 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
273 * which means it can go over the threshold once. If that happens, the driver
274 * will be in debt and no other buffer migrations can be done until that debt
275 * is repaid.
276 *
277 * This approach allows moving a buffer of any size (it's important to allow
278 * that).
279 *
280 * The currency is simply time in microseconds and it increases as the clock
281 * ticks. The accumulated microseconds (us) are converted to bytes and
282 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 */
284static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
285{
Marek Olšák95844d22016-08-17 23:49:27 +0200286 s64 time_us, increment_us;
287 u64 max_bytes;
288 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400289
Marek Olšák95844d22016-08-17 23:49:27 +0200290 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
291 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400292 *
Marek Olšák95844d22016-08-17 23:49:27 +0200293 * It means that in order to get full max MBps, at least 5 IBs per
294 * second must be submitted and not more than 200ms apart from each
295 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 */
Marek Olšák95844d22016-08-17 23:49:27 +0200297 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298
Marek Olšák95844d22016-08-17 23:49:27 +0200299 if (!adev->mm_stats.log2_max_MBps)
300 return 0;
301
302 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
303 used_vram = atomic64_read(&adev->vram_usage);
304 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
305
306 spin_lock(&adev->mm_stats.lock);
307
308 /* Increase the amount of accumulated us. */
309 time_us = ktime_to_us(ktime_get());
310 increment_us = time_us - adev->mm_stats.last_update_us;
311 adev->mm_stats.last_update_us = time_us;
312 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
313 us_upper_bound);
314
315 /* This prevents the short period of low performance when the VRAM
316 * usage is low and the driver is in debt or doesn't have enough
317 * accumulated us to fill VRAM quickly.
318 *
319 * The situation can occur in these cases:
320 * - a lot of VRAM is freed by userspace
321 * - the presence of a big buffer causes a lot of evictions
322 * (solution: split buffers into smaller ones)
323 *
324 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
325 * accum_us to a positive number.
326 */
327 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
328 s64 min_us;
329
330 /* Be more aggresive on dGPUs. Try to fill a portion of free
331 * VRAM now.
332 */
333 if (!(adev->flags & AMD_IS_APU))
334 min_us = bytes_to_us(adev, free_vram / 4);
335 else
336 min_us = 0; /* Reset accum_us on APUs. */
337
338 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
339 }
340
341 /* This returns 0 if the driver is in debt to disallow (optional)
342 * buffer moves.
343 */
344 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
345
346 spin_unlock(&adev->mm_stats.lock);
347 return max_bytes;
348}
349
350/* Report how many bytes have really been moved for the last command
351 * submission. This can result in a debt that can stop buffer migrations
352 * temporarily.
353 */
Samuel Pitoisetfad06122017-02-09 11:33:37 +0100354void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200355{
356 spin_lock(&adev->mm_stats.lock);
357 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
358 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359}
360
Chunming Zhou14fd8332016-08-04 13:05:46 +0800361static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
362 struct amdgpu_bo *bo)
363{
Christian Königa7d64de2016-09-15 14:58:48 +0200364 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800365 u64 initial_bytes_moved;
366 uint32_t domain;
367 int r;
368
369 if (bo->pin_count)
370 return 0;
371
Marek Olšák95844d22016-08-17 23:49:27 +0200372 /* Don't move this buffer if we have depleted our allowance
373 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800374 */
Marek Olšák95844d22016-08-17 23:49:27 +0200375 if (p->bytes_moved < p->bytes_moved_threshold)
Chunming Zhou14fd8332016-08-04 13:05:46 +0800376 domain = bo->prefered_domains;
377 else
378 domain = bo->allowed_domains;
379
380retry:
381 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200382 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800383 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200384 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Chunming Zhou14fd8332016-08-04 13:05:46 +0800385 initial_bytes_moved;
386
Christian König1abdc3d2016-08-31 17:28:11 +0200387 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
388 domain = bo->allowed_domains;
389 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800390 }
391
392 return r;
393}
394
Christian König662bfa62016-09-01 12:13:18 +0200395/* Last resort, try to evict something from the current working set */
396static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200397 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200398{
Christian Königf7da30d2016-09-28 12:03:04 +0200399 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200400 int r;
401
402 if (!p->evictable)
403 return false;
404
405 for (;&p->evictable->tv.head != &p->validated;
406 p->evictable = list_prev_entry(p->evictable, tv.head)) {
407
408 struct amdgpu_bo_list_entry *candidate = p->evictable;
409 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200410 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian König662bfa62016-09-01 12:13:18 +0200411 u64 initial_bytes_moved;
412 uint32_t other;
413
414 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200415 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200416 break;
417
418 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
419
420 /* Check if this BO is in one of the domains we need space for */
421 if (!(other & domain))
422 continue;
423
424 /* Check if we can move this BO somewhere else */
425 other = bo->allowed_domains & ~domain;
426 if (!other)
427 continue;
428
429 /* Good we can try to move this BO somewhere else */
430 amdgpu_ttm_placement_from_domain(bo, other);
Christian Königa7d64de2016-09-15 14:58:48 +0200431 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200432 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200433 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200434 initial_bytes_moved;
435
436 if (unlikely(r))
437 break;
438
439 p->evictable = list_prev_entry(p->evictable, tv.head);
440 list_move(&candidate->tv.head, &p->validated);
441
442 return true;
443 }
444
445 return false;
446}
447
Christian Königf7da30d2016-09-28 12:03:04 +0200448static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
449{
450 struct amdgpu_cs_parser *p = param;
451 int r;
452
453 do {
454 r = amdgpu_cs_bo_validate(p, bo);
455 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
456 if (r)
457 return r;
458
459 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500460 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200461
462 return r;
463}
464
Baoyou Xie761c2e82016-09-03 13:57:14 +0800465static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200466 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400467{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400468 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469 int r;
470
Christian Königa5b75052015-09-03 16:40:39 +0200471 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100472 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100473 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100474 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475
Christian Königcc325d12016-02-08 11:08:35 +0100476 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
477 if (usermm && usermm != current->mm)
478 return -EPERM;
479
Christian König2f568db2016-02-23 12:36:59 +0100480 /* Check if we have user pages and nobody bound the BO already */
481 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
482 size_t size = sizeof(struct page *);
483
484 size *= bo->tbo.ttm->num_pages;
485 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
486 binding_userptr = true;
487 }
488
Christian König662bfa62016-09-01 12:13:18 +0200489 if (p->evictable == lobj)
490 p->evictable = NULL;
491
Christian Königf7da30d2016-09-28 12:03:04 +0200492 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800493 if (r)
Christian König36409d122015-12-21 20:31:35 +0100494 return r;
Christian König662bfa62016-09-01 12:13:18 +0200495
Christian König2f568db2016-02-23 12:36:59 +0100496 if (binding_userptr) {
497 drm_free_large(lobj->user_pages);
498 lobj->user_pages = NULL;
499 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 }
501 return 0;
502}
503
Christian König2a7d9bd2015-12-18 20:33:52 +0100504static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
505 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506{
507 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100508 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200509 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800510 bool need_mmap_lock = false;
Christian König2f568db2016-02-23 12:36:59 +0100511 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100512 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513
Christian König2a7d9bd2015-12-18 20:33:52 +0100514 INIT_LIST_HEAD(&p->validated);
515
516 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800517 if (p->bo_list) {
Christian König211dff52016-02-22 15:40:59 +0100518 need_mmap_lock = p->bo_list->first_userptr !=
519 p->bo_list->num_entries;
Christian König636ce252015-12-18 21:26:47 +0100520 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
monk.liu840d5142015-04-27 15:19:20 +0800521 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522
Christian König3c0eea62015-12-11 14:39:05 +0100523 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100524 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525
Christian König758ac172016-05-06 22:14:00 +0200526 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100527 list_add(&p->uf_entry.tv.head, &p->validated);
528
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 if (need_mmap_lock)
530 down_read(&current->mm->mmap_sem);
531
Christian König2f568db2016-02-23 12:36:59 +0100532 while (1) {
533 struct list_head need_pages;
534 unsigned i;
535
536 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
537 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200538 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800539 if (r != -ERESTARTSYS)
540 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100541 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200542 }
Christian König2f568db2016-02-23 12:36:59 +0100543
544 /* Without a BO list we don't have userptr BOs */
545 if (!p->bo_list)
546 break;
547
548 INIT_LIST_HEAD(&need_pages);
549 for (i = p->bo_list->first_userptr;
550 i < p->bo_list->num_entries; ++i) {
551
552 e = &p->bo_list->array[i];
553
554 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
555 &e->user_invalidated) && e->user_pages) {
556
557 /* We acquired a page array, but somebody
558 * invalidated it. Free it an try again
559 */
560 release_pages(e->user_pages,
561 e->robj->tbo.ttm->num_pages,
562 false);
563 drm_free_large(e->user_pages);
564 e->user_pages = NULL;
565 }
566
567 if (e->robj->tbo.ttm->state != tt_bound &&
568 !e->user_pages) {
569 list_del(&e->tv.head);
570 list_add(&e->tv.head, &need_pages);
571
572 amdgpu_bo_unreserve(e->robj);
573 }
574 }
575
576 if (list_empty(&need_pages))
577 break;
578
579 /* Unreserve everything again. */
580 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
581
Marek Olšákf1037952016-07-30 00:48:39 +0200582 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100583 if (!--tries) {
584 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200585 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100586 goto error_free_pages;
587 }
588
589 /* Fill the page arrays for all useptrs. */
590 list_for_each_entry(e, &need_pages, tv.head) {
591 struct ttm_tt *ttm = e->robj->tbo.ttm;
592
593 e->user_pages = drm_calloc_large(ttm->num_pages,
594 sizeof(struct page*));
595 if (!e->user_pages) {
596 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200597 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100598 goto error_free_pages;
599 }
600
601 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
602 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200603 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100604 drm_free_large(e->user_pages);
605 e->user_pages = NULL;
606 goto error_free_pages;
607 }
608 }
609
610 /* And try again. */
611 list_splice(&need_pages, &p->validated);
612 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400613
Christian Königf69f90a12015-12-21 19:47:42 +0100614 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
615 p->bytes_moved = 0;
Christian König662bfa62016-09-01 12:13:18 +0200616 p->evictable = list_last_entry(&p->validated,
617 struct amdgpu_bo_list_entry,
618 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100619
Christian Königf7da30d2016-09-28 12:03:04 +0200620 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
621 amdgpu_cs_validate, p);
622 if (r) {
623 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
624 goto error_validate;
625 }
626
Christian Königf69f90a12015-12-21 19:47:42 +0100627 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200628 if (r) {
629 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200630 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200631 }
Christian Königa5b75052015-09-03 16:40:39 +0200632
Christian Königf69f90a12015-12-21 19:47:42 +0100633 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200634 if (r) {
635 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100636 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200637 }
Christian Königa8480302016-01-05 16:03:39 +0100638
Marek Olšák95844d22016-08-17 23:49:27 +0200639 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
640
Christian König5a712a82016-06-21 16:28:15 +0200641 fpriv->vm.last_eviction_counter =
642 atomic64_read(&p->adev->num_evictions);
643
Christian Königa8480302016-01-05 16:03:39 +0100644 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200645 struct amdgpu_bo *gds = p->bo_list->gds_obj;
646 struct amdgpu_bo *gws = p->bo_list->gws_obj;
647 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100648 struct amdgpu_vm *vm = &fpriv->vm;
649 unsigned i;
650
651 for (i = 0; i < p->bo_list->num_entries; i++) {
652 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
653
654 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
655 }
Christian Königd88bf582016-05-06 17:50:03 +0200656
657 if (gds) {
658 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
659 p->job->gds_size = amdgpu_bo_size(gds);
660 }
661 if (gws) {
662 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
663 p->job->gws_size = amdgpu_bo_size(gws);
664 }
665 if (oa) {
666 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
667 p->job->oa_size = amdgpu_bo_size(oa);
668 }
Christian Königa8480302016-01-05 16:03:39 +0100669 }
Christian Königa5b75052015-09-03 16:40:39 +0200670
Christian Königc855e252016-09-05 17:00:57 +0200671 if (!r && p->uf_entry.robj) {
672 struct amdgpu_bo *uf = p->uf_entry.robj;
673
Christian Königbb990bb2016-09-09 16:32:33 +0200674 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200675 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
676 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200677
Christian Königa5b75052015-09-03 16:40:39 +0200678error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100679 if (r) {
680 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200681 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100682 }
Christian Königa5b75052015-09-03 16:40:39 +0200683
Christian König2f568db2016-02-23 12:36:59 +0100684error_free_pages:
685
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 if (need_mmap_lock)
687 up_read(&current->mm->mmap_sem);
688
Christian König2f568db2016-02-23 12:36:59 +0100689 if (p->bo_list) {
690 for (i = p->bo_list->first_userptr;
691 i < p->bo_list->num_entries; ++i) {
692 e = &p->bo_list->array[i];
693
694 if (!e->user_pages)
695 continue;
696
697 release_pages(e->user_pages,
698 e->robj->tbo.ttm->num_pages,
699 false);
700 drm_free_large(e->user_pages);
701 }
702 }
703
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 return r;
705}
706
707static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
708{
709 struct amdgpu_bo_list_entry *e;
710 int r;
711
712 list_for_each_entry(e, &p->validated, tv.head) {
713 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100714 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715
716 if (r)
717 return r;
718 }
719 return 0;
720}
721
Christian König984810f2015-11-14 21:05:35 +0100722/**
723 * cs_parser_fini() - clean parser states
724 * @parser: parser structure holding parsing context.
725 * @error: error number
726 *
727 * If error is set than unvalidate buffer, otherwise just free memory
728 * used by parsing context.
729 **/
730static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800731{
Christian Königeceb8a12016-01-11 15:35:21 +0100732 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100733 unsigned i;
734
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500736 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
737
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100739 &parser->validated,
740 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 } else if (backoff) {
742 ttm_eu_backoff_reservation(&parser->ticket,
743 &parser->validated);
744 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100745 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100746
Christian König3cb485f2015-05-11 15:34:59 +0200747 if (parser->ctx)
748 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800749 if (parser->bo_list)
750 amdgpu_bo_list_put(parser->bo_list);
751
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752 for (i = 0; i < parser->nchunks; i++)
753 drm_free_large(parser->chunks[i].kdata);
754 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100755 if (parser->job)
756 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100757 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758}
759
760static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
761 struct amdgpu_vm *vm)
762{
763 struct amdgpu_device *adev = p->adev;
764 struct amdgpu_bo_va *bo_va;
765 struct amdgpu_bo *bo;
766 int i, r;
767
768 r = amdgpu_vm_update_page_directory(adev, vm);
769 if (r)
770 return r;
771
Christian Könige86f9ce2016-02-08 12:13:05 +0100772 r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200773 if (r)
774 return r;
775
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 r = amdgpu_vm_clear_freed(adev, vm);
777 if (r)
778 return r;
779
Monk Liu24936642017-01-09 15:54:32 +0800780 if (amdgpu_sriov_vf(adev)) {
781 struct dma_fence *f;
782 bo_va = vm->csa_bo_va;
783 BUG_ON(!bo_va);
784 r = amdgpu_vm_bo_update(adev, bo_va, false);
785 if (r)
786 return r;
787
788 f = bo_va->last_pt_update;
789 r = amdgpu_sync_fence(adev, &p->job->sync, f);
790 if (r)
791 return r;
792 }
793
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794 if (p->bo_list) {
795 for (i = 0; i < p->bo_list->num_entries; i++) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100796 struct dma_fence *f;
Christian König91e1a522015-07-06 22:06:40 +0200797
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798 /* ignore duplicates */
799 bo = p->bo_list->array[i].robj;
800 if (!bo)
801 continue;
802
803 bo_va = p->bo_list->array[i].bo_va;
804 if (bo_va == NULL)
805 continue;
806
Christian König99e124f2016-08-16 14:43:17 +0200807 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808 if (r)
809 return r;
810
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800811 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100812 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200813 if (r)
814 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400815 }
Christian Königb495bd32015-09-10 14:00:35 +0200816
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817 }
818
Christian Könige86f9ce2016-02-08 12:13:05 +0100819 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200820
821 if (amdgpu_vm_debug && p->bo_list) {
822 /* Invalidate all BOs to test for userspace bugs */
823 for (i = 0; i < p->bo_list->num_entries; i++) {
824 /* ignore duplicates */
825 bo = p->bo_list->array[i].robj;
826 if (!bo)
827 continue;
828
829 amdgpu_vm_bo_invalidate(adev, bo);
830 }
831 }
832
833 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400834}
835
836static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100837 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838{
Christian Königb07c60c2016-01-31 12:29:04 +0100839 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100841 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 int i, r;
843
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100845 if (ring->funcs->parse_cs) {
846 for (i = 0; i < p->job->num_ibs; i++) {
847 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 if (r)
849 return r;
850 }
Christian König45088ef2016-10-05 16:49:19 +0200851 }
852
853 if (p->job->vm) {
Christian König9a795882016-06-22 14:25:55 +0200854 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
855
856 r = amdgpu_bo_vm_update_pte(p, vm);
857 if (r)
858 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400859 }
860
Christian König9a795882016-06-22 14:25:55 +0200861 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400862}
863
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400864static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
865 struct amdgpu_cs_parser *parser)
866{
867 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
868 struct amdgpu_vm *vm = &fpriv->vm;
869 int i, j;
870 int r;
871
Christian König50838c82016-02-03 13:44:52 +0100872 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873 struct amdgpu_cs_chunk *chunk;
874 struct amdgpu_ib *ib;
875 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400876 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877
878 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100879 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
881
882 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
883 continue;
884
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
886 chunk_ib->ip_instance, chunk_ib->ring,
887 &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200888 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890
Monk Liu753ad492016-08-26 13:28:28 +0800891 if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
892 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
893 if (!parser->ctx->preamble_presented) {
894 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
895 parser->ctx->preamble_presented = true;
896 }
897 }
898
Christian Königb07c60c2016-01-31 12:29:04 +0100899 if (parser->job->ring && parser->job->ring != ring)
900 return -EINVAL;
901
902 parser->job->ring = ring;
903
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400904 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200905 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200906 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200907 uint64_t offset;
908 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200909
Christian König4802ce12015-06-10 17:20:11 +0200910 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
911 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200912 if (!aobj) {
913 DRM_ERROR("IB va_start is invalid\n");
914 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400915 }
916
Christian König4802ce12015-06-10 17:20:11 +0200917 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
918 (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
919 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
920 return -EINVAL;
921 }
922
Marek Olšák3ccec532015-06-02 17:44:49 +0200923 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200924 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400926 return r;
927 }
928
Christian König4802ce12015-06-10 17:20:11 +0200929 offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
930 kptr += chunk_ib->va_start - offset;
931
Christian König45088ef2016-10-05 16:49:19 +0200932 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 if (r) {
934 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 return r;
936 }
937
938 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
939 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100941 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942 if (r) {
943 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400944 return r;
945 }
946
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948
Christian König45088ef2016-10-05 16:49:19 +0200949 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200950 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800951 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 j++;
953 }
954
Christian König758ac172016-05-06 22:14:00 +0200955 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200956 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +0200957 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
958 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +0200959 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960
961 return 0;
962}
963
Christian König2b48d322015-06-19 17:31:29 +0200964static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
965 struct amdgpu_cs_parser *p)
966{
Christian König76a1ea62015-07-06 19:42:10 +0200967 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2b48d322015-06-19 17:31:29 +0200968 int i, j, r;
969
Christian König2b48d322015-06-19 17:31:29 +0200970 for (i = 0; i < p->nchunks; ++i) {
971 struct drm_amdgpu_cs_chunk_dep *deps;
972 struct amdgpu_cs_chunk *chunk;
973 unsigned num_deps;
974
975 chunk = &p->chunks[i];
976
977 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
978 continue;
979
980 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
981 num_deps = chunk->length_dw * 4 /
982 sizeof(struct drm_amdgpu_cs_chunk_dep);
983
984 for (j = 0; j < num_deps; ++j) {
Christian König2b48d322015-06-19 17:31:29 +0200985 struct amdgpu_ring *ring;
Christian König76a1ea62015-07-06 19:42:10 +0200986 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100987 struct dma_fence *fence;
Christian König2b48d322015-06-19 17:31:29 +0200988
989 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
990 deps[j].ip_instance,
991 deps[j].ring, &ring);
992 if (r)
993 return r;
994
Christian König76a1ea62015-07-06 19:42:10 +0200995 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
996 if (ctx == NULL)
997 return -EINVAL;
998
Christian König21c16bf2015-07-07 17:24:49 +0200999 fence = amdgpu_ctx_get_fence(ctx, ring,
1000 deps[j].handle);
1001 if (IS_ERR(fence)) {
1002 r = PTR_ERR(fence);
Christian König76a1ea62015-07-06 19:42:10 +02001003 amdgpu_ctx_put(ctx);
Christian König2b48d322015-06-19 17:31:29 +02001004 return r;
Christian König21c16bf2015-07-07 17:24:49 +02001005
1006 } else if (fence) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001007 r = amdgpu_sync_fence(adev, &p->job->sync,
1008 fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001009 dma_fence_put(fence);
Christian König21c16bf2015-07-07 17:24:49 +02001010 amdgpu_ctx_put(ctx);
1011 if (r)
1012 return r;
Christian König76a1ea62015-07-06 19:42:10 +02001013 }
Christian König2b48d322015-06-19 17:31:29 +02001014 }
1015 }
1016
1017 return 0;
1018}
1019
Christian Königcd75dc62016-01-31 11:30:55 +01001020static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1021 union drm_amdgpu_cs *cs)
1022{
Christian Königb07c60c2016-01-31 12:29:04 +01001023 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001024 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001025 struct amdgpu_job *job;
Monk Liue6869412016-03-07 12:49:55 +08001026 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001027
Christian König50838c82016-02-03 13:44:52 +01001028 job = p->job;
1029 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001030
Christian König595a9cd2016-06-30 10:52:03 +02001031 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001032 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001033 amdgpu_job_free(job);
Monk Liue6869412016-03-07 12:49:55 +08001034 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001035 }
1036
Monk Liue6869412016-03-07 12:49:55 +08001037 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001038 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001039 p->fence = dma_fence_get(&job->base.s_fence->finished);
Christian König595a9cd2016-06-30 10:52:03 +02001040 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
Christian König758ac172016-05-06 22:14:00 +02001041 job->uf_sequence = cs->out.handle;
Christian Königa5fb4ec2016-06-29 15:10:31 +02001042 amdgpu_job_free_resources(job);
Christian Königcd75dc62016-01-31 11:30:55 +01001043
1044 trace_amdgpu_cs_ioctl(job);
1045 amd_sched_entity_push_job(&job->base);
1046
1047 return 0;
1048}
1049
Chunming Zhou049fc522015-07-21 14:36:51 +08001050int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1051{
1052 struct amdgpu_device *adev = dev->dev_private;
1053 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001054 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001055 bool reserved_buffers = false;
1056 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001057
Christian König0c418f12015-09-01 15:13:53 +02001058 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001059 return -EBUSY;
Chunming Zhou049fc522015-07-21 14:36:51 +08001060
Christian König7e52a812015-11-04 15:44:39 +01001061 parser.adev = adev;
1062 parser.filp = filp;
1063
1064 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001066 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001067 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068 }
Huang Ruia414cd72016-10-30 23:05:47 +08001069
Christian König2a7d9bd2015-12-18 20:33:52 +01001070 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001071 if (r) {
1072 if (r == -ENOMEM)
1073 DRM_ERROR("Not enough memory for command submission!\n");
1074 else if (r != -ERESTARTSYS)
1075 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1076 goto out;
Christian König26a69802015-08-18 21:09:33 +02001077 }
1078
Huang Ruia414cd72016-10-30 23:05:47 +08001079 reserved_buffers = true;
1080 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001081 if (r)
1082 goto out;
1083
Huang Ruia414cd72016-10-30 23:05:47 +08001084 r = amdgpu_cs_dependencies(adev, &parser);
1085 if (r) {
1086 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1087 goto out;
1088 }
1089
Christian König50838c82016-02-03 13:44:52 +01001090 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001091 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001092
Christian König7e52a812015-11-04 15:44:39 +01001093 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001094 if (r)
1095 goto out;
1096
Christian König4acabfe2016-01-31 11:32:04 +01001097 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099out:
Christian König7e52a812015-11-04 15:44:39 +01001100 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 return r;
1102}
1103
1104/**
1105 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1106 *
1107 * @dev: drm device
1108 * @data: data from userspace
1109 * @filp: file private
1110 *
1111 * Wait for the command submission identified by handle to finish.
1112 */
1113int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1114 struct drm_file *filp)
1115{
1116 union drm_amdgpu_wait_cs *wait = data;
1117 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001119 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001120 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001121 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122 long r;
1123
Christian König21c16bf2015-07-07 17:24:49 +02001124 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
1125 wait->in.ring, &ring);
1126 if (r)
1127 return r;
1128
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001129 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1130 if (ctx == NULL)
1131 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001132
1133 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1134 if (IS_ERR(fence))
1135 r = PTR_ERR(fence);
1136 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001137 r = dma_fence_wait_timeout(fence, true, timeout);
1138 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001139 } else
Christian König21c16bf2015-07-07 17:24:49 +02001140 r = 1;
1141
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001142 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001143 if (r < 0)
1144 return r;
1145
1146 memset(wait, 0, sizeof(*wait));
1147 wait->out.status = (r == 0);
1148
1149 return 0;
1150}
1151
1152/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001153 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1154 *
1155 * @adev: amdgpu device
1156 * @filp: file private
1157 * @user: drm_amdgpu_fence copied from user space
1158 */
1159static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1160 struct drm_file *filp,
1161 struct drm_amdgpu_fence *user)
1162{
1163 struct amdgpu_ring *ring;
1164 struct amdgpu_ctx *ctx;
1165 struct dma_fence *fence;
1166 int r;
1167
1168 r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance,
1169 user->ring, &ring);
1170 if (r)
1171 return ERR_PTR(r);
1172
1173 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1174 if (ctx == NULL)
1175 return ERR_PTR(-EINVAL);
1176
1177 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1178 amdgpu_ctx_put(ctx);
1179
1180 return fence;
1181}
1182
1183/**
1184 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1185 *
1186 * @adev: amdgpu device
1187 * @filp: file private
1188 * @wait: wait parameters
1189 * @fences: array of drm_amdgpu_fence
1190 */
1191static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1192 struct drm_file *filp,
1193 union drm_amdgpu_wait_fences *wait,
1194 struct drm_amdgpu_fence *fences)
1195{
1196 uint32_t fence_count = wait->in.fence_count;
1197 unsigned int i;
1198 long r = 1;
1199
1200 for (i = 0; i < fence_count; i++) {
1201 struct dma_fence *fence;
1202 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1203
1204 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1205 if (IS_ERR(fence))
1206 return PTR_ERR(fence);
1207 else if (!fence)
1208 continue;
1209
1210 r = dma_fence_wait_timeout(fence, true, timeout);
1211 if (r < 0)
1212 return r;
1213
1214 if (r == 0)
1215 break;
1216 }
1217
1218 memset(wait, 0, sizeof(*wait));
1219 wait->out.status = (r > 0);
1220
1221 return 0;
1222}
1223
1224/**
1225 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1226 *
1227 * @adev: amdgpu device
1228 * @filp: file private
1229 * @wait: wait parameters
1230 * @fences: array of drm_amdgpu_fence
1231 */
1232static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1233 struct drm_file *filp,
1234 union drm_amdgpu_wait_fences *wait,
1235 struct drm_amdgpu_fence *fences)
1236{
1237 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1238 uint32_t fence_count = wait->in.fence_count;
1239 uint32_t first = ~0;
1240 struct dma_fence **array;
1241 unsigned int i;
1242 long r;
1243
1244 /* Prepare the fence array */
1245 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1246
1247 if (array == NULL)
1248 return -ENOMEM;
1249
1250 for (i = 0; i < fence_count; i++) {
1251 struct dma_fence *fence;
1252
1253 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1254 if (IS_ERR(fence)) {
1255 r = PTR_ERR(fence);
1256 goto err_free_fence_array;
1257 } else if (fence) {
1258 array[i] = fence;
1259 } else { /* NULL, the fence has been already signaled */
1260 r = 1;
1261 goto out;
1262 }
1263 }
1264
1265 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1266 &first);
1267 if (r < 0)
1268 goto err_free_fence_array;
1269
1270out:
1271 memset(wait, 0, sizeof(*wait));
1272 wait->out.status = (r > 0);
1273 wait->out.first_signaled = first;
1274 /* set return value 0 to indicate success */
1275 r = 0;
1276
1277err_free_fence_array:
1278 for (i = 0; i < fence_count; i++)
1279 dma_fence_put(array[i]);
1280 kfree(array);
1281
1282 return r;
1283}
1284
1285/**
1286 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1287 *
1288 * @dev: drm device
1289 * @data: data from userspace
1290 * @filp: file private
1291 */
1292int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1293 struct drm_file *filp)
1294{
1295 struct amdgpu_device *adev = dev->dev_private;
1296 union drm_amdgpu_wait_fences *wait = data;
1297 uint32_t fence_count = wait->in.fence_count;
1298 struct drm_amdgpu_fence *fences_user;
1299 struct drm_amdgpu_fence *fences;
1300 int r;
1301
1302 /* Get the fences from userspace */
1303 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1304 GFP_KERNEL);
1305 if (fences == NULL)
1306 return -ENOMEM;
1307
1308 fences_user = (void __user *)(unsigned long)(wait->in.fences);
1309 if (copy_from_user(fences, fences_user,
1310 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1311 r = -EFAULT;
1312 goto err_free_fences;
1313 }
1314
1315 if (wait->in.wait_all)
1316 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1317 else
1318 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1319
1320err_free_fences:
1321 kfree(fences);
1322
1323 return r;
1324}
1325
1326/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001327 * amdgpu_cs_find_bo_va - find bo_va for VM address
1328 *
1329 * @parser: command submission parser context
1330 * @addr: VM address
1331 * @bo: resulting BO of the mapping found
1332 *
1333 * Search the buffer objects in the command submission context for a certain
1334 * virtual memory address. Returns allocation structure when found, NULL
1335 * otherwise.
1336 */
1337struct amdgpu_bo_va_mapping *
1338amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1339 uint64_t addr, struct amdgpu_bo **bo)
1340{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +01001342 unsigned i;
1343
1344 if (!parser->bo_list)
1345 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346
1347 addr /= AMDGPU_GPU_PAGE_SIZE;
1348
Christian König15486fd22015-12-22 16:06:12 +01001349 for (i = 0; i < parser->bo_list->num_entries; i++) {
1350 struct amdgpu_bo_list_entry *lobj;
1351
1352 lobj = &parser->bo_list->array[i];
1353 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 continue;
1355
Christian König15486fd22015-12-22 16:06:12 +01001356 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian König7fc11952015-07-30 11:53:42 +02001357 if (mapping->it.start > addr ||
1358 addr > mapping->it.last)
1359 continue;
1360
Christian König15486fd22015-12-22 16:06:12 +01001361 *bo = lobj->bo_va->bo;
Christian König7fc11952015-07-30 11:53:42 +02001362 return mapping;
1363 }
1364
Christian König15486fd22015-12-22 16:06:12 +01001365 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 if (mapping->it.start > addr ||
1367 addr > mapping->it.last)
1368 continue;
1369
Christian König15486fd22015-12-22 16:06:12 +01001370 *bo = lobj->bo_va->bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 return mapping;
1372 }
1373 }
1374
1375 return NULL;
1376}
Christian Königc855e252016-09-05 17:00:57 +02001377
1378/**
1379 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1380 *
1381 * @parser: command submission parser context
1382 *
1383 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1384 */
1385int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1386{
1387 unsigned i;
1388 int r;
1389
1390 if (!parser->bo_list)
1391 return 0;
1392
1393 for (i = 0; i < parser->bo_list->num_entries; i++) {
1394 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1395
Christian Königbb990bb2016-09-09 16:32:33 +02001396 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +02001397 if (unlikely(r))
1398 return r;
Christian König03f48dd2016-08-15 17:00:22 +02001399
1400 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
1401 continue;
1402
1403 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1404 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
1405 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
1406 if (unlikely(r))
1407 return r;
Christian Königc855e252016-09-05 17:00:57 +02001408 }
1409
1410 return 0;
1411}