blob: 5599c01b265d40c105b59b5ca0f3f14c2efdf40e [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>
Dave Airlie660e8552017-03-13 22:18:15 +000030#include <drm/drm_syncobj.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include "amdgpu.h"
32#include "amdgpu_trace.h"
33
Christian König91acbeb2015-12-14 16:42:31 +010034static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +020035 struct drm_amdgpu_cs_chunk_fence *data,
36 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +010037{
38 struct drm_gem_object *gobj;
Christian Königaa290402016-09-09 11:21:43 +020039 unsigned long size;
Christian König91acbeb2015-12-14 16:42:31 +010040
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010041 gobj = drm_gem_object_lookup(p->filp, data->handle);
Christian König91acbeb2015-12-14 16:42:31 +010042 if (gobj == NULL)
43 return -EINVAL;
44
Christian König758ac172016-05-06 22:14:00 +020045 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +010046 p->uf_entry.priority = 0;
47 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
48 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +010049 p->uf_entry.user_pages = NULL;
Christian Königaa290402016-09-09 11:21:43 +020050
51 size = amdgpu_bo_size(p->uf_entry.robj);
52 if (size != PAGE_SIZE || (data->offset + 8) > size)
53 return -EINVAL;
54
Christian König758ac172016-05-06 22:14:00 +020055 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +010056
57 drm_gem_object_unreference_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +020058
59 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
60 amdgpu_bo_unref(&p->uf_entry.robj);
61 return -EINVAL;
62 }
63
Christian König91acbeb2015-12-14 16:42:31 +010064 return 0;
65}
66
Alex Xie9211c782017-06-20 16:35:04 -040067static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068{
Christian König4c0b2422016-02-01 11:20:37 +010069 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +080070 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040071 union drm_amdgpu_cs *cs = data;
72 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +030073 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +010074 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +020075 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +030076 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +030077 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078
Dan Carpenter1d263472015-09-23 13:59:28 +030079 if (cs->in.num_chunks == 0)
80 return 0;
81
82 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
83 if (!chunk_array)
84 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085
Christian König3cb485f2015-05-11 15:34:59 +020086 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
87 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +030088 ret = -EINVAL;
89 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +020090 }
Dan Carpenter1d263472015-09-23 13:59:28 +030091
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092 /* get chunks */
Alex Xief4e7c7c2017-04-05 16:54:34 -040093 chunk_array_user = (uint64_t __user *)(uintptr_t)(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 if (copy_from_user(chunk_array, chunk_array_user,
95 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +030096 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +010097 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 }
99
100 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800101 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300103 if (!p->chunks) {
104 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100105 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 }
107
108 for (i = 0; i < p->nchunks; i++) {
109 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
110 struct drm_amdgpu_cs_chunk user_chunk;
111 uint32_t __user *cdata;
112
Alex Xief4e7c7c2017-04-05 16:54:34 -0400113 chunk_ptr = (void __user *)(uintptr_t)chunk_array[i];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114 if (copy_from_user(&user_chunk, chunk_ptr,
115 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300116 ret = -EFAULT;
117 i--;
118 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 }
120 p->chunks[i].chunk_id = user_chunk.chunk_id;
121 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122
123 size = p->chunks[i].length_dw;
Alex Xief4e7c7c2017-04-05 16:54:34 -0400124 cdata = (void __user *)(uintptr_t)user_chunk.chunk_data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125
Michal Hocko20981052017-05-17 14:23:12 +0200126 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300128 ret = -ENOMEM;
129 i--;
130 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 }
132 size *= sizeof(uint32_t);
133 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300134 ret = -EFAULT;
135 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136 }
137
Christian König9a5e8fb2015-06-23 17:07:03 +0200138 switch (p->chunks[i].chunk_id) {
139 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100140 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200141 break;
142
143 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100145 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300146 ret = -EINVAL;
147 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148 }
Christian König91acbeb2015-12-14 16:42:31 +0100149
Christian König758ac172016-05-06 22:14:00 +0200150 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
151 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100152 if (ret)
153 goto free_partial_kdata;
154
Christian König9a5e8fb2015-06-23 17:07:03 +0200155 break;
156
Christian König2b48d322015-06-19 17:31:29 +0200157 case AMDGPU_CHUNK_ID_DEPENDENCIES:
Dave Airlie660e8552017-03-13 22:18:15 +0000158 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
159 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
Christian König2b48d322015-06-19 17:31:29 +0200160 break;
161
Christian König9a5e8fb2015-06-23 17:07:03 +0200162 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300163 ret = -EINVAL;
164 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 }
166 }
167
Monk Liuc5637832016-04-19 20:11:32 +0800168 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100169 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100170 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171
Christian Königb5f5acb2016-06-29 13:26:41 +0200172 if (p->uf_entry.robj)
173 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300175 return 0;
176
177free_all_kdata:
178 i = p->nchunks - 1;
179free_partial_kdata:
180 for (; i >= 0; i--)
Michal Hocko20981052017-05-17 14:23:12 +0200181 kvfree(p->chunks[i].kdata);
Dan Carpenter1d263472015-09-23 13:59:28 +0300182 kfree(p->chunks);
Dave Airlie607523d2017-03-10 12:13:04 +1000183 p->chunks = NULL;
184 p->nchunks = 0;
Christian König2a7d9bd2015-12-18 20:33:52 +0100185put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300186 amdgpu_ctx_put(p->ctx);
187free_chunk:
188 kfree(chunk_array);
189
190 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191}
192
Marek Olšák95844d22016-08-17 23:49:27 +0200193/* Convert microseconds to bytes. */
194static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
195{
196 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
197 return 0;
198
199 /* Since accum_us is incremented by a million per second, just
200 * multiply it by the number of MB/s to get the number of bytes.
201 */
202 return us << adev->mm_stats.log2_max_MBps;
203}
204
205static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
206{
207 if (!adev->mm_stats.log2_max_MBps)
208 return 0;
209
210 return bytes >> adev->mm_stats.log2_max_MBps;
211}
212
213/* Returns how many bytes TTM can move right now. If no bytes can be moved,
214 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
215 * which means it can go over the threshold once. If that happens, the driver
216 * will be in debt and no other buffer migrations can be done until that debt
217 * is repaid.
218 *
219 * This approach allows moving a buffer of any size (it's important to allow
220 * that).
221 *
222 * The currency is simply time in microseconds and it increases as the clock
223 * ticks. The accumulated microseconds (us) are converted to bytes and
224 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225 */
226static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
227{
Marek Olšák95844d22016-08-17 23:49:27 +0200228 s64 time_us, increment_us;
229 u64 max_bytes;
230 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231
Marek Olšák95844d22016-08-17 23:49:27 +0200232 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
233 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234 *
Marek Olšák95844d22016-08-17 23:49:27 +0200235 * It means that in order to get full max MBps, at least 5 IBs per
236 * second must be submitted and not more than 200ms apart from each
237 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238 */
Marek Olšák95844d22016-08-17 23:49:27 +0200239 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240
Marek Olšák95844d22016-08-17 23:49:27 +0200241 if (!adev->mm_stats.log2_max_MBps)
242 return 0;
243
244 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
245 used_vram = atomic64_read(&adev->vram_usage);
246 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
247
248 spin_lock(&adev->mm_stats.lock);
249
250 /* Increase the amount of accumulated us. */
251 time_us = ktime_to_us(ktime_get());
252 increment_us = time_us - adev->mm_stats.last_update_us;
253 adev->mm_stats.last_update_us = time_us;
254 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
255 us_upper_bound);
256
257 /* This prevents the short period of low performance when the VRAM
258 * usage is low and the driver is in debt or doesn't have enough
259 * accumulated us to fill VRAM quickly.
260 *
261 * The situation can occur in these cases:
262 * - a lot of VRAM is freed by userspace
263 * - the presence of a big buffer causes a lot of evictions
264 * (solution: split buffers into smaller ones)
265 *
266 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
267 * accum_us to a positive number.
268 */
269 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
270 s64 min_us;
271
272 /* Be more aggresive on dGPUs. Try to fill a portion of free
273 * VRAM now.
274 */
275 if (!(adev->flags & AMD_IS_APU))
276 min_us = bytes_to_us(adev, free_vram / 4);
277 else
278 min_us = 0; /* Reset accum_us on APUs. */
279
280 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
281 }
282
283 /* This returns 0 if the driver is in debt to disallow (optional)
284 * buffer moves.
285 */
286 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
287
288 spin_unlock(&adev->mm_stats.lock);
289 return max_bytes;
290}
291
292/* Report how many bytes have really been moved for the last command
293 * submission. This can result in a debt that can stop buffer migrations
294 * temporarily.
295 */
Samuel Pitoisetfad06122017-02-09 11:33:37 +0100296void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200297{
298 spin_lock(&adev->mm_stats.lock);
299 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
300 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301}
302
Chunming Zhou14fd8332016-08-04 13:05:46 +0800303static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
304 struct amdgpu_bo *bo)
305{
Christian Königa7d64de2016-09-15 14:58:48 +0200306 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800307 u64 initial_bytes_moved;
308 uint32_t domain;
309 int r;
310
311 if (bo->pin_count)
312 return 0;
313
Marek Olšák95844d22016-08-17 23:49:27 +0200314 /* Don't move this buffer if we have depleted our allowance
315 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800316 */
Marek Olšák95844d22016-08-17 23:49:27 +0200317 if (p->bytes_moved < p->bytes_moved_threshold)
Chunming Zhou14fd8332016-08-04 13:05:46 +0800318 domain = bo->prefered_domains;
319 else
320 domain = bo->allowed_domains;
321
322retry:
323 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200324 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800325 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200326 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Chunming Zhou14fd8332016-08-04 13:05:46 +0800327 initial_bytes_moved;
328
Christian König1abdc3d2016-08-31 17:28:11 +0200329 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
330 domain = bo->allowed_domains;
331 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800332 }
333
334 return r;
335}
336
Christian König662bfa62016-09-01 12:13:18 +0200337/* Last resort, try to evict something from the current working set */
338static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200339 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200340{
Christian Königf7da30d2016-09-28 12:03:04 +0200341 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200342 int r;
343
344 if (!p->evictable)
345 return false;
346
347 for (;&p->evictable->tv.head != &p->validated;
348 p->evictable = list_prev_entry(p->evictable, tv.head)) {
349
350 struct amdgpu_bo_list_entry *candidate = p->evictable;
351 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200352 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian König662bfa62016-09-01 12:13:18 +0200353 u64 initial_bytes_moved;
354 uint32_t other;
355
356 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200357 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200358 break;
359
360 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
361
362 /* Check if this BO is in one of the domains we need space for */
363 if (!(other & domain))
364 continue;
365
366 /* Check if we can move this BO somewhere else */
367 other = bo->allowed_domains & ~domain;
368 if (!other)
369 continue;
370
371 /* Good we can try to move this BO somewhere else */
372 amdgpu_ttm_placement_from_domain(bo, other);
Christian Königa7d64de2016-09-15 14:58:48 +0200373 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200374 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
Christian Königa7d64de2016-09-15 14:58:48 +0200375 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200376 initial_bytes_moved;
377
378 if (unlikely(r))
379 break;
380
381 p->evictable = list_prev_entry(p->evictable, tv.head);
382 list_move(&candidate->tv.head, &p->validated);
383
384 return true;
385 }
386
387 return false;
388}
389
Christian Königf7da30d2016-09-28 12:03:04 +0200390static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
391{
392 struct amdgpu_cs_parser *p = param;
393 int r;
394
395 do {
396 r = amdgpu_cs_bo_validate(p, bo);
397 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
398 if (r)
399 return r;
400
401 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500402 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200403
404 return r;
405}
406
Baoyou Xie761c2e82016-09-03 13:57:14 +0800407static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200408 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411 int r;
412
Christian Königa5b75052015-09-03 16:40:39 +0200413 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100414 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100415 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100416 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417
Christian Königcc325d12016-02-08 11:08:35 +0100418 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
419 if (usermm && usermm != current->mm)
420 return -EPERM;
421
Christian König2f568db2016-02-23 12:36:59 +0100422 /* Check if we have user pages and nobody bound the BO already */
423 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
424 size_t size = sizeof(struct page *);
425
426 size *= bo->tbo.ttm->num_pages;
427 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
428 binding_userptr = true;
429 }
430
Christian König662bfa62016-09-01 12:13:18 +0200431 if (p->evictable == lobj)
432 p->evictable = NULL;
433
Christian Königf7da30d2016-09-28 12:03:04 +0200434 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800435 if (r)
Christian König36409d122015-12-21 20:31:35 +0100436 return r;
Christian König662bfa62016-09-01 12:13:18 +0200437
Christian König2f568db2016-02-23 12:36:59 +0100438 if (binding_userptr) {
Michal Hocko20981052017-05-17 14:23:12 +0200439 kvfree(lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100440 lobj->user_pages = NULL;
441 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442 }
443 return 0;
444}
445
Christian König2a7d9bd2015-12-18 20:33:52 +0100446static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
447 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448{
449 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100450 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200451 struct list_head duplicates;
monk.liu840d5142015-04-27 15:19:20 +0800452 bool need_mmap_lock = false;
Christian König2f568db2016-02-23 12:36:59 +0100453 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100454 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400455
Christian König2a7d9bd2015-12-18 20:33:52 +0100456 INIT_LIST_HEAD(&p->validated);
457
458 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
monk.liu840d5142015-04-27 15:19:20 +0800459 if (p->bo_list) {
Christian König211dff52016-02-22 15:40:59 +0100460 need_mmap_lock = p->bo_list->first_userptr !=
461 p->bo_list->num_entries;
Christian König636ce252015-12-18 21:26:47 +0100462 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
monk.liu840d5142015-04-27 15:19:20 +0800463 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464
Christian König3c0eea62015-12-11 14:39:05 +0100465 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100466 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400467
Christian König758ac172016-05-06 22:14:00 +0200468 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100469 list_add(&p->uf_entry.tv.head, &p->validated);
470
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 if (need_mmap_lock)
472 down_read(&current->mm->mmap_sem);
473
Christian König2f568db2016-02-23 12:36:59 +0100474 while (1) {
475 struct list_head need_pages;
476 unsigned i;
477
478 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
479 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200480 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800481 if (r != -ERESTARTSYS)
482 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100483 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200484 }
Christian König2f568db2016-02-23 12:36:59 +0100485
486 /* Without a BO list we don't have userptr BOs */
487 if (!p->bo_list)
488 break;
489
490 INIT_LIST_HEAD(&need_pages);
491 for (i = p->bo_list->first_userptr;
492 i < p->bo_list->num_entries; ++i) {
493
494 e = &p->bo_list->array[i];
495
496 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
497 &e->user_invalidated) && e->user_pages) {
498
499 /* We acquired a page array, but somebody
Alex Xie9f69c0f2017-06-20 16:33:02 -0400500 * invalidated it. Free it and try again
Christian König2f568db2016-02-23 12:36:59 +0100501 */
502 release_pages(e->user_pages,
503 e->robj->tbo.ttm->num_pages,
504 false);
Michal Hocko20981052017-05-17 14:23:12 +0200505 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100506 e->user_pages = NULL;
507 }
508
509 if (e->robj->tbo.ttm->state != tt_bound &&
510 !e->user_pages) {
511 list_del(&e->tv.head);
512 list_add(&e->tv.head, &need_pages);
513
514 amdgpu_bo_unreserve(e->robj);
515 }
516 }
517
518 if (list_empty(&need_pages))
519 break;
520
521 /* Unreserve everything again. */
522 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
523
Marek Olšákf1037952016-07-30 00:48:39 +0200524 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100525 if (!--tries) {
526 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200527 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100528 goto error_free_pages;
529 }
530
Alex Xieeb0f0372017-06-08 14:53:26 -0400531 /* Fill the page arrays for all userptrs. */
Christian König2f568db2016-02-23 12:36:59 +0100532 list_for_each_entry(e, &need_pages, tv.head) {
533 struct ttm_tt *ttm = e->robj->tbo.ttm;
534
Michal Hocko20981052017-05-17 14:23:12 +0200535 e->user_pages = kvmalloc_array(ttm->num_pages,
536 sizeof(struct page*),
537 GFP_KERNEL | __GFP_ZERO);
Christian König2f568db2016-02-23 12:36:59 +0100538 if (!e->user_pages) {
539 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200540 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100541 goto error_free_pages;
542 }
543
544 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
545 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200546 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Michal Hocko20981052017-05-17 14:23:12 +0200547 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100548 e->user_pages = NULL;
549 goto error_free_pages;
550 }
551 }
552
553 /* And try again. */
554 list_splice(&need_pages, &p->validated);
555 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556
Christian Königf69f90a12015-12-21 19:47:42 +0100557 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
558 p->bytes_moved = 0;
Christian König662bfa62016-09-01 12:13:18 +0200559 p->evictable = list_last_entry(&p->validated,
560 struct amdgpu_bo_list_entry,
561 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100562
Christian Königf7da30d2016-09-28 12:03:04 +0200563 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
564 amdgpu_cs_validate, p);
565 if (r) {
566 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
567 goto error_validate;
568 }
569
Christian Königf69f90a12015-12-21 19:47:42 +0100570 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200571 if (r) {
572 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200573 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200574 }
Christian Königa5b75052015-09-03 16:40:39 +0200575
Christian Königf69f90a12015-12-21 19:47:42 +0100576 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200577 if (r) {
578 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100579 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200580 }
Christian Königa8480302016-01-05 16:03:39 +0100581
Marek Olšák95844d22016-08-17 23:49:27 +0200582 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
583
Christian König5a712a82016-06-21 16:28:15 +0200584 fpriv->vm.last_eviction_counter =
585 atomic64_read(&p->adev->num_evictions);
586
Christian Königa8480302016-01-05 16:03:39 +0100587 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200588 struct amdgpu_bo *gds = p->bo_list->gds_obj;
589 struct amdgpu_bo *gws = p->bo_list->gws_obj;
590 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100591 struct amdgpu_vm *vm = &fpriv->vm;
592 unsigned i;
593
594 for (i = 0; i < p->bo_list->num_entries; i++) {
595 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
596
597 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
598 }
Christian Königd88bf582016-05-06 17:50:03 +0200599
600 if (gds) {
601 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
602 p->job->gds_size = amdgpu_bo_size(gds);
603 }
604 if (gws) {
605 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
606 p->job->gws_size = amdgpu_bo_size(gws);
607 }
608 if (oa) {
609 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
610 p->job->oa_size = amdgpu_bo_size(oa);
611 }
Christian Königa8480302016-01-05 16:03:39 +0100612 }
Christian Königa5b75052015-09-03 16:40:39 +0200613
Christian Königc855e252016-09-05 17:00:57 +0200614 if (!r && p->uf_entry.robj) {
615 struct amdgpu_bo *uf = p->uf_entry.robj;
616
Christian Königbb990bb2016-09-09 16:32:33 +0200617 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200618 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
619 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200620
Christian Königa5b75052015-09-03 16:40:39 +0200621error_validate:
Christian Königeceb8a12016-01-11 15:35:21 +0100622 if (r) {
623 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
Christian Königa5b75052015-09-03 16:40:39 +0200624 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
Christian Königeceb8a12016-01-11 15:35:21 +0100625 }
Christian Königa5b75052015-09-03 16:40:39 +0200626
Christian König2f568db2016-02-23 12:36:59 +0100627error_free_pages:
628
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629 if (need_mmap_lock)
630 up_read(&current->mm->mmap_sem);
631
Christian König2f568db2016-02-23 12:36:59 +0100632 if (p->bo_list) {
633 for (i = p->bo_list->first_userptr;
634 i < p->bo_list->num_entries; ++i) {
635 e = &p->bo_list->array[i];
636
637 if (!e->user_pages)
638 continue;
639
640 release_pages(e->user_pages,
641 e->robj->tbo.ttm->num_pages,
642 false);
Michal Hocko20981052017-05-17 14:23:12 +0200643 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100644 }
645 }
646
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 return r;
648}
649
650static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
651{
652 struct amdgpu_bo_list_entry *e;
653 int r;
654
655 list_for_each_entry(e, &p->validated, tv.head) {
656 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100657 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400658
659 if (r)
660 return r;
661 }
662 return 0;
663}
664
Christian König984810f2015-11-14 21:05:35 +0100665/**
666 * cs_parser_fini() - clean parser states
667 * @parser: parser structure holding parsing context.
668 * @error: error number
669 *
670 * If error is set than unvalidate buffer, otherwise just free memory
671 * used by parsing context.
672 **/
673static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800674{
Christian Königeceb8a12016-01-11 15:35:21 +0100675 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
Christian König984810f2015-11-14 21:05:35 +0100676 unsigned i;
677
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678 if (!error) {
Nicolai Hähnle28b8d662016-01-27 11:04:19 -0500679 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
680
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 ttm_eu_fence_buffer_objects(&parser->ticket,
Christian König984810f2015-11-14 21:05:35 +0100682 &parser->validated,
683 parser->fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 } else if (backoff) {
685 ttm_eu_backoff_reservation(&parser->ticket,
686 &parser->validated);
687 }
Dave Airlie660e8552017-03-13 22:18:15 +0000688
689 for (i = 0; i < parser->num_post_dep_syncobjs; i++)
690 drm_syncobj_put(parser->post_dep_syncobjs[i]);
691 kfree(parser->post_dep_syncobjs);
692
Chris Wilsonf54d1862016-10-25 13:00:45 +0100693 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100694
Christian König3cb485f2015-05-11 15:34:59 +0200695 if (parser->ctx)
696 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800697 if (parser->bo_list)
698 amdgpu_bo_list_put(parser->bo_list);
699
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 for (i = 0; i < parser->nchunks; i++)
Michal Hocko20981052017-05-17 14:23:12 +0200701 kvfree(parser->chunks[i].kdata);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100703 if (parser->job)
704 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100705 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706}
707
Junwei Zhangb85891b2017-01-16 13:59:01 +0800708static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709{
710 struct amdgpu_device *adev = p->adev;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800711 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
712 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 struct amdgpu_bo_va *bo_va;
714 struct amdgpu_bo *bo;
715 int i, r;
716
Christian König194d2162016-10-12 15:13:52 +0200717 r = amdgpu_vm_update_directories(adev, vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718 if (r)
719 return r;
720
Christian Königa24960f2016-10-12 13:20:52 +0200721 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200722 if (r)
723 return r;
724
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100725 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726 if (r)
727 return r;
728
Junwei Zhangb85891b2017-01-16 13:59:01 +0800729 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
730 if (r)
731 return r;
732
733 r = amdgpu_sync_fence(adev, &p->job->sync,
734 fpriv->prt_va->last_pt_update);
735 if (r)
736 return r;
737
Monk Liu24936642017-01-09 15:54:32 +0800738 if (amdgpu_sriov_vf(adev)) {
739 struct dma_fence *f;
740 bo_va = vm->csa_bo_va;
741 BUG_ON(!bo_va);
742 r = amdgpu_vm_bo_update(adev, bo_va, false);
743 if (r)
744 return r;
745
746 f = bo_va->last_pt_update;
747 r = amdgpu_sync_fence(adev, &p->job->sync, f);
748 if (r)
749 return r;
750 }
751
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752 if (p->bo_list) {
753 for (i = 0; i < p->bo_list->num_entries; i++) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100754 struct dma_fence *f;
Christian König91e1a522015-07-06 22:06:40 +0200755
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756 /* ignore duplicates */
757 bo = p->bo_list->array[i].robj;
758 if (!bo)
759 continue;
760
761 bo_va = p->bo_list->array[i].bo_va;
762 if (bo_va == NULL)
763 continue;
764
Christian König99e124f2016-08-16 14:43:17 +0200765 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766 if (r)
767 return r;
768
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800769 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100770 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200771 if (r)
772 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400773 }
Christian Königb495bd32015-09-10 14:00:35 +0200774
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400775 }
776
Christian Könige86f9ce2016-02-08 12:13:05 +0100777 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200778
779 if (amdgpu_vm_debug && p->bo_list) {
780 /* Invalidate all BOs to test for userspace bugs */
781 for (i = 0; i < p->bo_list->num_entries; i++) {
782 /* ignore duplicates */
783 bo = p->bo_list->array[i].robj;
784 if (!bo)
785 continue;
786
787 amdgpu_vm_bo_invalidate(adev, bo);
788 }
789 }
790
791 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792}
793
794static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100795 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796{
Christian Königb07c60c2016-01-31 12:29:04 +0100797 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100799 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400800 int i, r;
801
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400802 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100803 if (ring->funcs->parse_cs) {
804 for (i = 0; i < p->job->num_ibs; i++) {
805 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400806 if (r)
807 return r;
808 }
Christian König45088ef2016-10-05 16:49:19 +0200809 }
810
811 if (p->job->vm) {
Christian König67003a12016-10-12 14:46:26 +0200812 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
Christian König9a795882016-06-22 14:25:55 +0200813
Junwei Zhangb85891b2017-01-16 13:59:01 +0800814 r = amdgpu_bo_vm_update_pte(p);
Christian König9a795882016-06-22 14:25:55 +0200815 if (r)
816 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817 }
818
Christian König9a795882016-06-22 14:25:55 +0200819 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820}
821
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400822static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
823 struct amdgpu_cs_parser *parser)
824{
825 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
826 struct amdgpu_vm *vm = &fpriv->vm;
827 int i, j;
Monk Liu9a1b3af2017-03-08 15:51:13 +0800828 int r, ce_preempt = 0, de_preempt = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829
Christian König50838c82016-02-03 13:44:52 +0100830 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400831 struct amdgpu_cs_chunk *chunk;
832 struct amdgpu_ib *ib;
833 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400834 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835
836 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100837 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
839
840 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
841 continue;
842
Monk Liu65333e42017-03-27 15:14:53 +0800843 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
Harry Wentlande51a3222017-03-28 11:29:53 -0400844 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
Monk Liu65333e42017-03-27 15:14:53 +0800845 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
846 ce_preempt++;
847 else
848 de_preempt++;
Harry Wentlande51a3222017-03-28 11:29:53 -0400849 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800850
Monk Liu65333e42017-03-27 15:14:53 +0800851 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
852 if (ce_preempt > 1 || de_preempt > 1)
Monk Liue9d672b2017-03-15 12:18:57 +0800853 return -EINVAL;
Monk Liu65333e42017-03-27 15:14:53 +0800854 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800855
Andres Rodriguezeffd9242017-02-16 00:47:32 -0500856 r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
857 chunk_ib->ip_instance, chunk_ib->ring, &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200858 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400859 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860
Monk Liu2a9ceb82017-03-28 11:00:03 +0800861 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
Monk Liu753ad492016-08-26 13:28:28 +0800862 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
863 if (!parser->ctx->preamble_presented) {
864 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
865 parser->ctx->preamble_presented = true;
866 }
867 }
868
Christian Königb07c60c2016-01-31 12:29:04 +0100869 if (parser->job->ring && parser->job->ring != ring)
870 return -EINVAL;
871
872 parser->job->ring = ring;
873
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200875 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200876 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200877 uint64_t offset;
878 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200879
Christian König4802ce12015-06-10 17:20:11 +0200880 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
881 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200882 if (!aobj) {
883 DRM_ERROR("IB va_start is invalid\n");
884 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885 }
886
Christian König4802ce12015-06-10 17:20:11 +0200887 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
Christian Königa9f87f62017-03-30 14:03:59 +0200888 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian König4802ce12015-06-10 17:20:11 +0200889 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
890 return -EINVAL;
891 }
892
Marek Olšák3ccec532015-06-02 17:44:49 +0200893 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200894 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400896 return r;
897 }
898
Christian Königa9f87f62017-03-30 14:03:59 +0200899 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
Christian König4802ce12015-06-10 17:20:11 +0200900 kptr += chunk_ib->va_start - offset;
901
Christian König45088ef2016-10-05 16:49:19 +0200902 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400903 if (r) {
904 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400905 return r;
906 }
907
908 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
909 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400910 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100911 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400912 if (r) {
913 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914 return r;
915 }
916
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400917 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918
Christian König45088ef2016-10-05 16:49:19 +0200919 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200920 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800921 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922 j++;
923 }
924
Christian König758ac172016-05-06 22:14:00 +0200925 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200926 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +0200927 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
928 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +0200929 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930
931 return 0;
932}
933
Dave Airlie6f0308e2017-03-09 03:45:52 +0000934static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
935 struct amdgpu_cs_chunk *chunk)
936{
937 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
938 unsigned num_deps;
939 int i, r;
940 struct drm_amdgpu_cs_chunk_dep *deps;
941
942 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
943 num_deps = chunk->length_dw * 4 /
944 sizeof(struct drm_amdgpu_cs_chunk_dep);
945
946 for (i = 0; i < num_deps; ++i) {
947 struct amdgpu_ring *ring;
948 struct amdgpu_ctx *ctx;
949 struct dma_fence *fence;
950
951 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
952 if (ctx == NULL)
953 return -EINVAL;
954
955 r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
956 deps[i].ip_type,
957 deps[i].ip_instance,
958 deps[i].ring, &ring);
959 if (r) {
960 amdgpu_ctx_put(ctx);
961 return r;
962 }
963
964 fence = amdgpu_ctx_get_fence(ctx, ring,
965 deps[i].handle);
966 if (IS_ERR(fence)) {
967 r = PTR_ERR(fence);
968 amdgpu_ctx_put(ctx);
969 return r;
970 } else if (fence) {
971 r = amdgpu_sync_fence(p->adev, &p->job->sync,
972 fence);
973 dma_fence_put(fence);
974 amdgpu_ctx_put(ctx);
975 if (r)
976 return r;
977 }
978 }
979 return 0;
980}
981
Dave Airlie660e8552017-03-13 22:18:15 +0000982static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
983 uint32_t handle)
984{
985 int r;
986 struct dma_fence *fence;
987 r = drm_syncobj_fence_get(p->filp, handle, &fence);
988 if (r)
989 return r;
990
991 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence);
992 dma_fence_put(fence);
993
994 return r;
995}
996
997static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
998 struct amdgpu_cs_chunk *chunk)
999{
1000 unsigned num_deps;
1001 int i, r;
1002 struct drm_amdgpu_cs_chunk_sem *deps;
1003
1004 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1005 num_deps = chunk->length_dw * 4 /
1006 sizeof(struct drm_amdgpu_cs_chunk_sem);
1007
1008 for (i = 0; i < num_deps; ++i) {
1009 r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
1010 if (r)
1011 return r;
1012 }
1013 return 0;
1014}
1015
1016static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1017 struct amdgpu_cs_chunk *chunk)
1018{
1019 unsigned num_deps;
1020 int i;
1021 struct drm_amdgpu_cs_chunk_sem *deps;
1022 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1023 num_deps = chunk->length_dw * 4 /
1024 sizeof(struct drm_amdgpu_cs_chunk_sem);
1025
1026 p->post_dep_syncobjs = kmalloc_array(num_deps,
1027 sizeof(struct drm_syncobj *),
1028 GFP_KERNEL);
1029 p->num_post_dep_syncobjs = 0;
1030
1031 for (i = 0; i < num_deps; ++i) {
1032 p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
1033 if (!p->post_dep_syncobjs[i])
1034 return -EINVAL;
1035 p->num_post_dep_syncobjs++;
1036 }
1037 return 0;
1038}
1039
Christian König2b48d322015-06-19 17:31:29 +02001040static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1041 struct amdgpu_cs_parser *p)
1042{
Dave Airlie6f0308e2017-03-09 03:45:52 +00001043 int i, r;
Christian König2b48d322015-06-19 17:31:29 +02001044
Christian König2b48d322015-06-19 17:31:29 +02001045 for (i = 0; i < p->nchunks; ++i) {
Christian König2b48d322015-06-19 17:31:29 +02001046 struct amdgpu_cs_chunk *chunk;
Christian König2b48d322015-06-19 17:31:29 +02001047
1048 chunk = &p->chunks[i];
1049
Dave Airlie6f0308e2017-03-09 03:45:52 +00001050 if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
1051 r = amdgpu_cs_process_fence_dep(p, chunk);
1052 if (r)
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001053 return r;
Dave Airlie660e8552017-03-13 22:18:15 +00001054 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
1055 r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1056 if (r)
1057 return r;
1058 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
1059 r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1060 if (r)
1061 return r;
Christian König2b48d322015-06-19 17:31:29 +02001062 }
1063 }
1064
1065 return 0;
1066}
1067
Dave Airlie660e8552017-03-13 22:18:15 +00001068static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1069{
1070 int i;
1071
Chris Wilson00fc2c22017-07-05 21:12:44 +01001072 for (i = 0; i < p->num_post_dep_syncobjs; ++i)
1073 drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001074}
1075
Christian Königcd75dc62016-01-31 11:30:55 +01001076static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1077 union drm_amdgpu_cs *cs)
1078{
Christian Königb07c60c2016-01-31 12:29:04 +01001079 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001080 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001081 struct amdgpu_job *job;
Monk Liue6869412016-03-07 12:49:55 +08001082 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001083
Christian König50838c82016-02-03 13:44:52 +01001084 job = p->job;
1085 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001086
Christian König595a9cd2016-06-30 10:52:03 +02001087 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001088 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001089 amdgpu_job_free(job);
Monk Liue6869412016-03-07 12:49:55 +08001090 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001091 }
1092
Monk Liue6869412016-03-07 12:49:55 +08001093 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001094 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001095 p->fence = dma_fence_get(&job->base.s_fence->finished);
Dave Airlie660e8552017-03-13 22:18:15 +00001096
1097 amdgpu_cs_post_dependencies(p);
1098
Christian König595a9cd2016-06-30 10:52:03 +02001099 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
Christian König758ac172016-05-06 22:14:00 +02001100 job->uf_sequence = cs->out.handle;
Christian Königa5fb4ec2016-06-29 15:10:31 +02001101 amdgpu_job_free_resources(job);
Chunming Zhou10e709c2017-04-27 15:13:52 +08001102 amdgpu_cs_parser_fini(p, 0, true);
Christian Königcd75dc62016-01-31 11:30:55 +01001103
1104 trace_amdgpu_cs_ioctl(job);
1105 amd_sched_entity_push_job(&job->base);
Christian Königcd75dc62016-01-31 11:30:55 +01001106 return 0;
1107}
1108
Chunming Zhou049fc522015-07-21 14:36:51 +08001109int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1110{
1111 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001112 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +08001113 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001114 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001115 bool reserved_buffers = false;
1116 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001117
Christian König0c418f12015-09-01 15:13:53 +02001118 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001119 return -EBUSY;
Chunming Zhouf1892132017-05-15 16:48:27 +08001120 if (amdgpu_kms_vram_lost(adev, fpriv))
1121 return -ENODEV;
Chunming Zhou049fc522015-07-21 14:36:51 +08001122
Christian König7e52a812015-11-04 15:44:39 +01001123 parser.adev = adev;
1124 parser.filp = filp;
1125
1126 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001128 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001129 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 }
Huang Ruia414cd72016-10-30 23:05:47 +08001131
Christian König2a7d9bd2015-12-18 20:33:52 +01001132 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001133 if (r) {
1134 if (r == -ENOMEM)
1135 DRM_ERROR("Not enough memory for command submission!\n");
1136 else if (r != -ERESTARTSYS)
1137 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1138 goto out;
Christian König26a69802015-08-18 21:09:33 +02001139 }
1140
Huang Ruia414cd72016-10-30 23:05:47 +08001141 reserved_buffers = true;
1142 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001143 if (r)
1144 goto out;
1145
Huang Ruia414cd72016-10-30 23:05:47 +08001146 r = amdgpu_cs_dependencies(adev, &parser);
1147 if (r) {
1148 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1149 goto out;
1150 }
1151
Christian König50838c82016-02-03 13:44:52 +01001152 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001153 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001154
Christian König7e52a812015-11-04 15:44:39 +01001155 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001156 if (r)
1157 goto out;
1158
Christian König4acabfe2016-01-31 11:32:04 +01001159 r = amdgpu_cs_submit(&parser, cs);
Chunming Zhou10e709c2017-04-27 15:13:52 +08001160 if (r)
1161 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162
Chunming Zhou10e709c2017-04-27 15:13:52 +08001163 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001164out:
Christian König7e52a812015-11-04 15:44:39 +01001165 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001166 return r;
1167}
1168
1169/**
1170 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1171 *
1172 * @dev: drm device
1173 * @data: data from userspace
1174 * @filp: file private
1175 *
1176 * Wait for the command submission identified by handle to finish.
1177 */
1178int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1179 struct drm_file *filp)
1180{
1181 union drm_amdgpu_wait_cs *wait = data;
1182 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001183 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001185 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001186 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001187 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 long r;
1189
Chunming Zhouf1892132017-05-15 16:48:27 +08001190 if (amdgpu_kms_vram_lost(adev, fpriv))
1191 return -ENODEV;
Christian König21c16bf2015-07-07 17:24:49 +02001192
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001193 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1194 if (ctx == NULL)
1195 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001196
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001197 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
1198 wait->in.ip_type, wait->in.ip_instance,
1199 wait->in.ring, &ring);
1200 if (r) {
1201 amdgpu_ctx_put(ctx);
1202 return r;
1203 }
1204
Chunming Zhou4b559c92015-07-21 15:53:04 +08001205 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1206 if (IS_ERR(fence))
1207 r = PTR_ERR(fence);
1208 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001209 r = dma_fence_wait_timeout(fence, true, timeout);
1210 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001211 } else
Christian König21c16bf2015-07-07 17:24:49 +02001212 r = 1;
1213
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001214 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215 if (r < 0)
1216 return r;
1217
1218 memset(wait, 0, sizeof(*wait));
1219 wait->out.status = (r == 0);
1220
1221 return 0;
1222}
1223
1224/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001225 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1226 *
1227 * @adev: amdgpu device
1228 * @filp: file private
1229 * @user: drm_amdgpu_fence copied from user space
1230 */
1231static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1232 struct drm_file *filp,
1233 struct drm_amdgpu_fence *user)
1234{
1235 struct amdgpu_ring *ring;
1236 struct amdgpu_ctx *ctx;
1237 struct dma_fence *fence;
1238 int r;
1239
Junwei Zhangeef18a82016-11-04 16:16:10 -04001240 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1241 if (ctx == NULL)
1242 return ERR_PTR(-EINVAL);
1243
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001244 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
1245 user->ip_instance, user->ring, &ring);
1246 if (r) {
1247 amdgpu_ctx_put(ctx);
1248 return ERR_PTR(r);
1249 }
1250
Junwei Zhangeef18a82016-11-04 16:16:10 -04001251 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1252 amdgpu_ctx_put(ctx);
1253
1254 return fence;
1255}
1256
1257/**
1258 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1259 *
1260 * @adev: amdgpu device
1261 * @filp: file private
1262 * @wait: wait parameters
1263 * @fences: array of drm_amdgpu_fence
1264 */
1265static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1266 struct drm_file *filp,
1267 union drm_amdgpu_wait_fences *wait,
1268 struct drm_amdgpu_fence *fences)
1269{
1270 uint32_t fence_count = wait->in.fence_count;
1271 unsigned int i;
1272 long r = 1;
1273
1274 for (i = 0; i < fence_count; i++) {
1275 struct dma_fence *fence;
1276 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1277
1278 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1279 if (IS_ERR(fence))
1280 return PTR_ERR(fence);
1281 else if (!fence)
1282 continue;
1283
1284 r = dma_fence_wait_timeout(fence, true, timeout);
Chunming Zhou32df87d2017-04-07 17:05:45 +08001285 dma_fence_put(fence);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001286 if (r < 0)
1287 return r;
1288
1289 if (r == 0)
1290 break;
1291 }
1292
1293 memset(wait, 0, sizeof(*wait));
1294 wait->out.status = (r > 0);
1295
1296 return 0;
1297}
1298
1299/**
1300 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1301 *
1302 * @adev: amdgpu device
1303 * @filp: file private
1304 * @wait: wait parameters
1305 * @fences: array of drm_amdgpu_fence
1306 */
1307static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1308 struct drm_file *filp,
1309 union drm_amdgpu_wait_fences *wait,
1310 struct drm_amdgpu_fence *fences)
1311{
1312 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1313 uint32_t fence_count = wait->in.fence_count;
1314 uint32_t first = ~0;
1315 struct dma_fence **array;
1316 unsigned int i;
1317 long r;
1318
1319 /* Prepare the fence array */
1320 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1321
1322 if (array == NULL)
1323 return -ENOMEM;
1324
1325 for (i = 0; i < fence_count; i++) {
1326 struct dma_fence *fence;
1327
1328 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1329 if (IS_ERR(fence)) {
1330 r = PTR_ERR(fence);
1331 goto err_free_fence_array;
1332 } else if (fence) {
1333 array[i] = fence;
1334 } else { /* NULL, the fence has been already signaled */
1335 r = 1;
1336 goto out;
1337 }
1338 }
1339
1340 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1341 &first);
1342 if (r < 0)
1343 goto err_free_fence_array;
1344
1345out:
1346 memset(wait, 0, sizeof(*wait));
1347 wait->out.status = (r > 0);
1348 wait->out.first_signaled = first;
1349 /* set return value 0 to indicate success */
1350 r = 0;
1351
1352err_free_fence_array:
1353 for (i = 0; i < fence_count; i++)
1354 dma_fence_put(array[i]);
1355 kfree(array);
1356
1357 return r;
1358}
1359
1360/**
1361 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1362 *
1363 * @dev: drm device
1364 * @data: data from userspace
1365 * @filp: file private
1366 */
1367int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1368 struct drm_file *filp)
1369{
1370 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001371 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001372 union drm_amdgpu_wait_fences *wait = data;
1373 uint32_t fence_count = wait->in.fence_count;
1374 struct drm_amdgpu_fence *fences_user;
1375 struct drm_amdgpu_fence *fences;
1376 int r;
1377
Chunming Zhouf1892132017-05-15 16:48:27 +08001378 if (amdgpu_kms_vram_lost(adev, fpriv))
1379 return -ENODEV;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001380 /* Get the fences from userspace */
1381 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1382 GFP_KERNEL);
1383 if (fences == NULL)
1384 return -ENOMEM;
1385
Alex Xief4e7c7c2017-04-05 16:54:34 -04001386 fences_user = (void __user *)(uintptr_t)(wait->in.fences);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001387 if (copy_from_user(fences, fences_user,
1388 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1389 r = -EFAULT;
1390 goto err_free_fences;
1391 }
1392
1393 if (wait->in.wait_all)
1394 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1395 else
1396 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1397
1398err_free_fences:
1399 kfree(fences);
1400
1401 return r;
1402}
1403
1404/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405 * amdgpu_cs_find_bo_va - find bo_va for VM address
1406 *
1407 * @parser: command submission parser context
1408 * @addr: VM address
1409 * @bo: resulting BO of the mapping found
1410 *
1411 * Search the buffer objects in the command submission context for a certain
1412 * virtual memory address. Returns allocation structure when found, NULL
1413 * otherwise.
1414 */
1415struct amdgpu_bo_va_mapping *
1416amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1417 uint64_t addr, struct amdgpu_bo **bo)
1418{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +01001420 unsigned i;
1421
1422 if (!parser->bo_list)
1423 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001424
1425 addr /= AMDGPU_GPU_PAGE_SIZE;
1426
Christian König15486fd22015-12-22 16:06:12 +01001427 for (i = 0; i < parser->bo_list->num_entries; i++) {
1428 struct amdgpu_bo_list_entry *lobj;
1429
1430 lobj = &parser->bo_list->array[i];
1431 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 continue;
1433
Christian König15486fd22015-12-22 16:06:12 +01001434 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001435 if (mapping->start > addr ||
1436 addr > mapping->last)
Christian König7fc11952015-07-30 11:53:42 +02001437 continue;
1438
Christian König15486fd22015-12-22 16:06:12 +01001439 *bo = lobj->bo_va->bo;
Christian König7fc11952015-07-30 11:53:42 +02001440 return mapping;
1441 }
1442
Christian König15486fd22015-12-22 16:06:12 +01001443 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001444 if (mapping->start > addr ||
1445 addr > mapping->last)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001446 continue;
1447
Christian König15486fd22015-12-22 16:06:12 +01001448 *bo = lobj->bo_va->bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001449 return mapping;
1450 }
1451 }
1452
1453 return NULL;
1454}
Christian Königc855e252016-09-05 17:00:57 +02001455
1456/**
1457 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1458 *
1459 * @parser: command submission parser context
1460 *
1461 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1462 */
1463int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1464{
1465 unsigned i;
1466 int r;
1467
1468 if (!parser->bo_list)
1469 return 0;
1470
1471 for (i = 0; i < parser->bo_list->num_entries; i++) {
1472 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1473
Christian Königbb990bb2016-09-09 16:32:33 +02001474 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +02001475 if (unlikely(r))
1476 return r;
Christian König03f48dd2016-08-15 17:00:22 +02001477
1478 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
1479 continue;
1480
1481 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1482 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
1483 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
1484 if (unlikely(r))
1485 return r;
Christian Königc855e252016-09-05 17:00:57 +02001486 }
1487
1488 return 0;
1489}