blob: 38027a00f8ab9ca94044a3ef4eb8ac400233ca0d [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>
Marek Olšák7ca24cf2017-09-12 22:42:14 +020028#include <linux/sync_file.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <drm/drmP.h>
30#include <drm/amdgpu_drm.h>
Dave Airlie660e8552017-03-13 22:18:15 +000031#include <drm/drm_syncobj.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040032#include "amdgpu.h"
33#include "amdgpu_trace.h"
34
Christian König91acbeb2015-12-14 16:42:31 +010035static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
Christian König758ac172016-05-06 22:14:00 +020036 struct drm_amdgpu_cs_chunk_fence *data,
37 uint32_t *offset)
Christian König91acbeb2015-12-14 16:42:31 +010038{
39 struct drm_gem_object *gobj;
Christian Königaa290402016-09-09 11:21:43 +020040 unsigned long size;
Christian König91acbeb2015-12-14 16:42:31 +010041
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010042 gobj = drm_gem_object_lookup(p->filp, data->handle);
Christian König91acbeb2015-12-14 16:42:31 +010043 if (gobj == NULL)
44 return -EINVAL;
45
Christian König758ac172016-05-06 22:14:00 +020046 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
Christian König91acbeb2015-12-14 16:42:31 +010047 p->uf_entry.priority = 0;
48 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
49 p->uf_entry.tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +010050 p->uf_entry.user_pages = NULL;
Christian Königaa290402016-09-09 11:21:43 +020051
52 size = amdgpu_bo_size(p->uf_entry.robj);
53 if (size != PAGE_SIZE || (data->offset + 8) > size)
54 return -EINVAL;
55
Christian König758ac172016-05-06 22:14:00 +020056 *offset = data->offset;
Christian König91acbeb2015-12-14 16:42:31 +010057
Cihangir Akturkf62facc2017-08-03 14:58:16 +030058 drm_gem_object_put_unlocked(gobj);
Christian König758ac172016-05-06 22:14:00 +020059
60 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
61 amdgpu_bo_unref(&p->uf_entry.robj);
62 return -EINVAL;
63 }
64
Christian König91acbeb2015-12-14 16:42:31 +010065 return 0;
66}
67
Alex Xie9211c782017-06-20 16:35:04 -040068static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069{
Christian König4c0b2422016-02-01 11:20:37 +010070 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Monk Liuc5637832016-04-19 20:11:32 +080071 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 union drm_amdgpu_cs *cs = data;
73 uint64_t *chunk_array_user;
Dan Carpenter1d263472015-09-23 13:59:28 +030074 uint64_t *chunk_array;
Christian König50838c82016-02-03 13:44:52 +010075 unsigned size, num_ibs = 0;
Christian König758ac172016-05-06 22:14:00 +020076 uint32_t uf_offset = 0;
Dan Carpenter54313502015-09-25 14:36:55 +030077 int i;
Dan Carpenter1d263472015-09-23 13:59:28 +030078 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079
Dan Carpenter1d263472015-09-23 13:59:28 +030080 if (cs->in.num_chunks == 0)
81 return 0;
82
83 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
84 if (!chunk_array)
85 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086
Christian König3cb485f2015-05-11 15:34:59 +020087 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
88 if (!p->ctx) {
Dan Carpenter1d263472015-09-23 13:59:28 +030089 ret = -EINVAL;
90 goto free_chunk;
Christian König3cb485f2015-05-11 15:34:59 +020091 }
Dan Carpenter1d263472015-09-23 13:59:28 +030092
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 /* get chunks */
Christian König7ecc2452017-07-26 17:02:52 +020094 chunk_array_user = u64_to_user_ptr(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095 if (copy_from_user(chunk_array, chunk_array_user,
96 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +030097 ret = -EFAULT;
Christian König2a7d9bd2015-12-18 20:33:52 +010098 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099 }
100
101 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800102 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300104 if (!p->chunks) {
105 ret = -ENOMEM;
Christian König2a7d9bd2015-12-18 20:33:52 +0100106 goto put_ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107 }
108
109 for (i = 0; i < p->nchunks; i++) {
110 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
111 struct drm_amdgpu_cs_chunk user_chunk;
112 uint32_t __user *cdata;
113
Christian König7ecc2452017-07-26 17:02:52 +0200114 chunk_ptr = u64_to_user_ptr(chunk_array[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 if (copy_from_user(&user_chunk, chunk_ptr,
116 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300117 ret = -EFAULT;
118 i--;
119 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 }
121 p->chunks[i].chunk_id = user_chunk.chunk_id;
122 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123
124 size = p->chunks[i].length_dw;
Christian König7ecc2452017-07-26 17:02:52 +0200125 cdata = u64_to_user_ptr(user_chunk.chunk_data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126
Michal Hocko20981052017-05-17 14:23:12 +0200127 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300129 ret = -ENOMEM;
130 i--;
131 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132 }
133 size *= sizeof(uint32_t);
134 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300135 ret = -EFAULT;
136 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 }
138
Christian König9a5e8fb2015-06-23 17:07:03 +0200139 switch (p->chunks[i].chunk_id) {
140 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100141 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200142 break;
143
144 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100146 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300147 ret = -EINVAL;
148 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 }
Christian König91acbeb2015-12-14 16:42:31 +0100150
Christian König758ac172016-05-06 22:14:00 +0200151 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
152 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100153 if (ret)
154 goto free_partial_kdata;
155
Christian König9a5e8fb2015-06-23 17:07:03 +0200156 break;
157
Christian König2b48d322015-06-19 17:31:29 +0200158 case AMDGPU_CHUNK_ID_DEPENDENCIES:
Dave Airlie660e8552017-03-13 22:18:15 +0000159 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
160 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
Christian König2b48d322015-06-19 17:31:29 +0200161 break;
162
Christian König9a5e8fb2015-06-23 17:07:03 +0200163 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300164 ret = -EINVAL;
165 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166 }
167 }
168
Monk Liuc5637832016-04-19 20:11:32 +0800169 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100170 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100171 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172
Christian Königb5f5acb2016-06-29 13:26:41 +0200173 if (p->uf_entry.robj)
174 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300176 return 0;
177
178free_all_kdata:
179 i = p->nchunks - 1;
180free_partial_kdata:
181 for (; i >= 0; i--)
Michal Hocko20981052017-05-17 14:23:12 +0200182 kvfree(p->chunks[i].kdata);
Dan Carpenter1d263472015-09-23 13:59:28 +0300183 kfree(p->chunks);
Dave Airlie607523d2017-03-10 12:13:04 +1000184 p->chunks = NULL;
185 p->nchunks = 0;
Christian König2a7d9bd2015-12-18 20:33:52 +0100186put_ctx:
Dan Carpenter1d263472015-09-23 13:59:28 +0300187 amdgpu_ctx_put(p->ctx);
188free_chunk:
189 kfree(chunk_array);
190
191 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192}
193
Marek Olšák95844d22016-08-17 23:49:27 +0200194/* Convert microseconds to bytes. */
195static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
196{
197 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
198 return 0;
199
200 /* Since accum_us is incremented by a million per second, just
201 * multiply it by the number of MB/s to get the number of bytes.
202 */
203 return us << adev->mm_stats.log2_max_MBps;
204}
205
206static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
207{
208 if (!adev->mm_stats.log2_max_MBps)
209 return 0;
210
211 return bytes >> adev->mm_stats.log2_max_MBps;
212}
213
214/* Returns how many bytes TTM can move right now. If no bytes can be moved,
215 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
216 * which means it can go over the threshold once. If that happens, the driver
217 * will be in debt and no other buffer migrations can be done until that debt
218 * is repaid.
219 *
220 * This approach allows moving a buffer of any size (it's important to allow
221 * that).
222 *
223 * The currency is simply time in microseconds and it increases as the clock
224 * ticks. The accumulated microseconds (us) are converted to bytes and
225 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226 */
John Brooks00f06b22017-06-27 22:33:18 -0400227static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
228 u64 *max_bytes,
229 u64 *max_vis_bytes)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400230{
Marek Olšák95844d22016-08-17 23:49:27 +0200231 s64 time_us, increment_us;
Marek Olšák95844d22016-08-17 23:49:27 +0200232 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233
Marek Olšák95844d22016-08-17 23:49:27 +0200234 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
235 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 *
Marek Olšák95844d22016-08-17 23:49:27 +0200237 * It means that in order to get full max MBps, at least 5 IBs per
238 * second must be submitted and not more than 200ms apart from each
239 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 */
Marek Olšák95844d22016-08-17 23:49:27 +0200241 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242
John Brooks00f06b22017-06-27 22:33:18 -0400243 if (!adev->mm_stats.log2_max_MBps) {
244 *max_bytes = 0;
245 *max_vis_bytes = 0;
246 return;
247 }
Marek Olšák95844d22016-08-17 23:49:27 +0200248
249 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
Christian König3c848bb2017-08-07 17:46:49 +0200250 used_vram = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
Marek Olšák95844d22016-08-17 23:49:27 +0200251 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
252
253 spin_lock(&adev->mm_stats.lock);
254
255 /* Increase the amount of accumulated us. */
256 time_us = ktime_to_us(ktime_get());
257 increment_us = time_us - adev->mm_stats.last_update_us;
258 adev->mm_stats.last_update_us = time_us;
259 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
260 us_upper_bound);
261
262 /* This prevents the short period of low performance when the VRAM
263 * usage is low and the driver is in debt or doesn't have enough
264 * accumulated us to fill VRAM quickly.
265 *
266 * The situation can occur in these cases:
267 * - a lot of VRAM is freed by userspace
268 * - the presence of a big buffer causes a lot of evictions
269 * (solution: split buffers into smaller ones)
270 *
271 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
272 * accum_us to a positive number.
273 */
274 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
275 s64 min_us;
276
277 /* Be more aggresive on dGPUs. Try to fill a portion of free
278 * VRAM now.
279 */
280 if (!(adev->flags & AMD_IS_APU))
281 min_us = bytes_to_us(adev, free_vram / 4);
282 else
283 min_us = 0; /* Reset accum_us on APUs. */
284
285 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
286 }
287
John Brooks00f06b22017-06-27 22:33:18 -0400288 /* This is set to 0 if the driver is in debt to disallow (optional)
Marek Olšák95844d22016-08-17 23:49:27 +0200289 * buffer moves.
290 */
John Brooks00f06b22017-06-27 22:33:18 -0400291 *max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
292
293 /* Do the same for visible VRAM if half of it is free */
294 if (adev->mc.visible_vram_size < adev->mc.real_vram_size) {
295 u64 total_vis_vram = adev->mc.visible_vram_size;
Christian König3c848bb2017-08-07 17:46:49 +0200296 u64 used_vis_vram =
297 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
John Brooks00f06b22017-06-27 22:33:18 -0400298
299 if (used_vis_vram < total_vis_vram) {
300 u64 free_vis_vram = total_vis_vram - used_vis_vram;
301 adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
302 increment_us, us_upper_bound);
303
304 if (free_vis_vram >= total_vis_vram / 2)
305 adev->mm_stats.accum_us_vis =
306 max(bytes_to_us(adev, free_vis_vram / 2),
307 adev->mm_stats.accum_us_vis);
308 }
309
310 *max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
311 } else {
312 *max_vis_bytes = 0;
313 }
Marek Olšák95844d22016-08-17 23:49:27 +0200314
315 spin_unlock(&adev->mm_stats.lock);
Marek Olšák95844d22016-08-17 23:49:27 +0200316}
317
318/* Report how many bytes have really been moved for the last command
319 * submission. This can result in a debt that can stop buffer migrations
320 * temporarily.
321 */
John Brooks00f06b22017-06-27 22:33:18 -0400322void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
323 u64 num_vis_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200324{
325 spin_lock(&adev->mm_stats.lock);
326 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
John Brooks00f06b22017-06-27 22:33:18 -0400327 adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
Marek Olšák95844d22016-08-17 23:49:27 +0200328 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329}
330
Chunming Zhou14fd8332016-08-04 13:05:46 +0800331static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
332 struct amdgpu_bo *bo)
333{
Christian Königa7d64de2016-09-15 14:58:48 +0200334 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
John Brooks00f06b22017-06-27 22:33:18 -0400335 u64 initial_bytes_moved, bytes_moved;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800336 uint32_t domain;
337 int r;
338
339 if (bo->pin_count)
340 return 0;
341
Marek Olšák95844d22016-08-17 23:49:27 +0200342 /* Don't move this buffer if we have depleted our allowance
343 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800344 */
John Brooks00f06b22017-06-27 22:33:18 -0400345 if (p->bytes_moved < p->bytes_moved_threshold) {
346 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
347 (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
348 /* And don't move a CPU_ACCESS_REQUIRED BO to limited
349 * visible VRAM if we've depleted our allowance to do
350 * that.
351 */
352 if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
Kent Russell6d7d9c52017-08-08 07:58:01 -0400353 domain = bo->preferred_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400354 else
355 domain = bo->allowed_domains;
356 } else {
Kent Russell6d7d9c52017-08-08 07:58:01 -0400357 domain = bo->preferred_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400358 }
359 } else {
Chunming Zhou14fd8332016-08-04 13:05:46 +0800360 domain = bo->allowed_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400361 }
Chunming Zhou14fd8332016-08-04 13:05:46 +0800362
363retry:
364 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200365 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800366 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
John Brooks00f06b22017-06-27 22:33:18 -0400367 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
368 initial_bytes_moved;
369 p->bytes_moved += bytes_moved;
370 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
371 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
372 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT)
373 p->bytes_moved_vis += bytes_moved;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800374
Christian König1abdc3d2016-08-31 17:28:11 +0200375 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
376 domain = bo->allowed_domains;
377 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800378 }
379
380 return r;
381}
382
Christian König662bfa62016-09-01 12:13:18 +0200383/* Last resort, try to evict something from the current working set */
384static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200385 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200386{
Christian Königf7da30d2016-09-28 12:03:04 +0200387 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200388 int r;
389
390 if (!p->evictable)
391 return false;
392
393 for (;&p->evictable->tv.head != &p->validated;
394 p->evictable = list_prev_entry(p->evictable, tv.head)) {
395
396 struct amdgpu_bo_list_entry *candidate = p->evictable;
397 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200398 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
John Brooks00f06b22017-06-27 22:33:18 -0400399 u64 initial_bytes_moved, bytes_moved;
400 bool update_bytes_moved_vis;
Christian König662bfa62016-09-01 12:13:18 +0200401 uint32_t other;
402
403 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200404 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200405 break;
406
407 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
408
409 /* Check if this BO is in one of the domains we need space for */
410 if (!(other & domain))
411 continue;
412
413 /* Check if we can move this BO somewhere else */
414 other = bo->allowed_domains & ~domain;
415 if (!other)
416 continue;
417
418 /* Good we can try to move this BO somewhere else */
419 amdgpu_ttm_placement_from_domain(bo, other);
John Brooks00f06b22017-06-27 22:33:18 -0400420 update_bytes_moved_vis =
421 adev->mc.visible_vram_size < adev->mc.real_vram_size &&
422 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
423 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT;
Christian Königa7d64de2016-09-15 14:58:48 +0200424 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200425 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
John Brooks00f06b22017-06-27 22:33:18 -0400426 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200427 initial_bytes_moved;
John Brooks00f06b22017-06-27 22:33:18 -0400428 p->bytes_moved += bytes_moved;
429 if (update_bytes_moved_vis)
430 p->bytes_moved_vis += bytes_moved;
Christian König662bfa62016-09-01 12:13:18 +0200431
432 if (unlikely(r))
433 break;
434
435 p->evictable = list_prev_entry(p->evictable, tv.head);
436 list_move(&candidate->tv.head, &p->validated);
437
438 return true;
439 }
440
441 return false;
442}
443
Christian Königf7da30d2016-09-28 12:03:04 +0200444static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
445{
446 struct amdgpu_cs_parser *p = param;
447 int r;
448
449 do {
450 r = amdgpu_cs_bo_validate(p, bo);
451 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
452 if (r)
453 return r;
454
455 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500456 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200457
458 return r;
459}
460
Baoyou Xie761c2e82016-09-03 13:57:14 +0800461static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200462 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400465 int r;
466
Christian Königa5b75052015-09-03 16:40:39 +0200467 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100468 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100469 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100470 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471
Christian Königcc325d12016-02-08 11:08:35 +0100472 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
473 if (usermm && usermm != current->mm)
474 return -EPERM;
475
Christian König2f568db2016-02-23 12:36:59 +0100476 /* Check if we have user pages and nobody bound the BO already */
Christian Königca666a32017-09-05 14:30:05 +0200477 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
478 lobj->user_pages) {
Christian König1b0c0f92017-09-05 14:36:44 +0200479 amdgpu_ttm_placement_from_domain(bo,
480 AMDGPU_GEM_DOMAIN_CPU);
481 r = ttm_bo_validate(&bo->tbo, &bo->placement, true,
482 false);
483 if (r)
484 return r;
Christian Königa216ab02017-09-02 13:21:31 +0200485 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
486 lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100487 binding_userptr = true;
488 }
489
Christian König662bfa62016-09-01 12:13:18 +0200490 if (p->evictable == lobj)
491 p->evictable = NULL;
492
Christian Königf7da30d2016-09-28 12:03:04 +0200493 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800494 if (r)
Christian König36409d122015-12-21 20:31:35 +0100495 return r;
Christian König662bfa62016-09-01 12:13:18 +0200496
Christian König2f568db2016-02-23 12:36:59 +0100497 if (binding_userptr) {
Michal Hocko20981052017-05-17 14:23:12 +0200498 kvfree(lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100499 lobj->user_pages = NULL;
500 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 }
502 return 0;
503}
504
Christian König2a7d9bd2015-12-18 20:33:52 +0100505static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
506 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507{
508 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100509 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200510 struct list_head duplicates;
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);
Christian König3fe89772017-09-12 14:25:14 -0400517 if (p->bo_list) {
Christian König636ce252015-12-18 21:26:47 +0100518 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
Christian König3fe89772017-09-12 14:25:14 -0400519 if (p->bo_list->first_userptr != p->bo_list->num_entries)
520 p->mn = amdgpu_mn_get(p->adev);
521 }
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
Christian König2f568db2016-02-23 12:36:59 +0100529 while (1) {
530 struct list_head need_pages;
531 unsigned i;
532
533 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
534 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200535 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800536 if (r != -ERESTARTSYS)
537 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100538 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200539 }
Christian König2f568db2016-02-23 12:36:59 +0100540
541 /* Without a BO list we don't have userptr BOs */
542 if (!p->bo_list)
543 break;
544
545 INIT_LIST_HEAD(&need_pages);
546 for (i = p->bo_list->first_userptr;
547 i < p->bo_list->num_entries; ++i) {
Christian Königca666a32017-09-05 14:30:05 +0200548 struct amdgpu_bo *bo;
Christian König2f568db2016-02-23 12:36:59 +0100549
550 e = &p->bo_list->array[i];
Christian Königca666a32017-09-05 14:30:05 +0200551 bo = e->robj;
Christian König2f568db2016-02-23 12:36:59 +0100552
Christian Königca666a32017-09-05 14:30:05 +0200553 if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
Christian König2f568db2016-02-23 12:36:59 +0100554 &e->user_invalidated) && e->user_pages) {
555
556 /* We acquired a page array, but somebody
Alex Xie9f69c0f2017-06-20 16:33:02 -0400557 * invalidated it. Free it and try again
Christian König2f568db2016-02-23 12:36:59 +0100558 */
559 release_pages(e->user_pages,
Christian Königca666a32017-09-05 14:30:05 +0200560 bo->tbo.ttm->num_pages,
Christian König2f568db2016-02-23 12:36:59 +0100561 false);
Michal Hocko20981052017-05-17 14:23:12 +0200562 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100563 e->user_pages = NULL;
564 }
565
Christian Königca666a32017-09-05 14:30:05 +0200566 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
Christian König2f568db2016-02-23 12:36:59 +0100567 !e->user_pages) {
568 list_del(&e->tv.head);
569 list_add(&e->tv.head, &need_pages);
570
571 amdgpu_bo_unreserve(e->robj);
572 }
573 }
574
575 if (list_empty(&need_pages))
576 break;
577
578 /* Unreserve everything again. */
579 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
580
Marek Olšákf1037952016-07-30 00:48:39 +0200581 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100582 if (!--tries) {
583 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200584 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100585 goto error_free_pages;
586 }
587
Alex Xieeb0f0372017-06-08 14:53:26 -0400588 /* Fill the page arrays for all userptrs. */
Christian König2f568db2016-02-23 12:36:59 +0100589 list_for_each_entry(e, &need_pages, tv.head) {
590 struct ttm_tt *ttm = e->robj->tbo.ttm;
591
Michal Hocko20981052017-05-17 14:23:12 +0200592 e->user_pages = kvmalloc_array(ttm->num_pages,
593 sizeof(struct page*),
594 GFP_KERNEL | __GFP_ZERO);
Christian König2f568db2016-02-23 12:36:59 +0100595 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");
Michal Hocko20981052017-05-17 14:23:12 +0200604 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100605 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
John Brooks00f06b22017-06-27 22:33:18 -0400614 amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
615 &p->bytes_moved_vis_threshold);
Christian Königf69f90a12015-12-21 19:47:42 +0100616 p->bytes_moved = 0;
John Brooks00f06b22017-06-27 22:33:18 -0400617 p->bytes_moved_vis = 0;
Christian König662bfa62016-09-01 12:13:18 +0200618 p->evictable = list_last_entry(&p->validated,
619 struct amdgpu_bo_list_entry,
620 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100621
Christian Königf7da30d2016-09-28 12:03:04 +0200622 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
623 amdgpu_cs_validate, p);
624 if (r) {
625 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
626 goto error_validate;
627 }
628
Christian Königf69f90a12015-12-21 19:47:42 +0100629 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200630 if (r) {
631 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200632 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200633 }
Christian Königa5b75052015-09-03 16:40:39 +0200634
Christian Königf69f90a12015-12-21 19:47:42 +0100635 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200636 if (r) {
637 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100638 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200639 }
Christian Königa8480302016-01-05 16:03:39 +0100640
John Brooks00f06b22017-06-27 22:33:18 -0400641 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
642 p->bytes_moved_vis);
Christian Königa8480302016-01-05 16:03:39 +0100643 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200644 struct amdgpu_bo *gds = p->bo_list->gds_obj;
645 struct amdgpu_bo *gws = p->bo_list->gws_obj;
646 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100647 struct amdgpu_vm *vm = &fpriv->vm;
648 unsigned i;
649
650 for (i = 0; i < p->bo_list->num_entries; i++) {
651 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
652
653 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
654 }
Christian Königd88bf582016-05-06 17:50:03 +0200655
656 if (gds) {
657 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
658 p->job->gds_size = amdgpu_bo_size(gds);
659 }
660 if (gws) {
661 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
662 p->job->gws_size = amdgpu_bo_size(gws);
663 }
664 if (oa) {
665 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
666 p->job->oa_size = amdgpu_bo_size(oa);
667 }
Christian Königa8480302016-01-05 16:03:39 +0100668 }
Christian Königa5b75052015-09-03 16:40:39 +0200669
Christian Königc855e252016-09-05 17:00:57 +0200670 if (!r && p->uf_entry.robj) {
671 struct amdgpu_bo *uf = p->uf_entry.robj;
672
Christian Königbb990bb2016-09-09 16:32:33 +0200673 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200674 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
675 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200676
Christian Königa5b75052015-09-03 16:40:39 +0200677error_validate:
Christian Königb6369222017-08-03 11:44:01 -0400678 if (r)
Christian Königa5b75052015-09-03 16:40:39 +0200679 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
680
Christian König2f568db2016-02-23 12:36:59 +0100681error_free_pages:
682
Christian König2f568db2016-02-23 12:36:59 +0100683 if (p->bo_list) {
684 for (i = p->bo_list->first_userptr;
685 i < p->bo_list->num_entries; ++i) {
686 e = &p->bo_list->array[i];
687
688 if (!e->user_pages)
689 continue;
690
691 release_pages(e->user_pages,
692 e->robj->tbo.ttm->num_pages,
693 false);
Michal Hocko20981052017-05-17 14:23:12 +0200694 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100695 }
696 }
697
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 return r;
699}
700
701static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
702{
703 struct amdgpu_bo_list_entry *e;
704 int r;
705
706 list_for_each_entry(e, &p->validated, tv.head) {
707 struct reservation_object *resv = e->robj->tbo.resv;
Andres Rodriguez177ae092017-09-15 20:44:06 -0400708 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp,
709 amdgpu_bo_explicit_sync(e->robj));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710
711 if (r)
712 return r;
713 }
714 return 0;
715}
716
Christian König984810f2015-11-14 21:05:35 +0100717/**
718 * cs_parser_fini() - clean parser states
719 * @parser: parser structure holding parsing context.
720 * @error: error number
721 *
722 * If error is set than unvalidate buffer, otherwise just free memory
723 * used by parsing context.
724 **/
Christian Königb6369222017-08-03 11:44:01 -0400725static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
726 bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800727{
Christian König984810f2015-11-14 21:05:35 +0100728 unsigned i;
729
Christian König3fe89772017-09-12 14:25:14 -0400730 if (error && backoff)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 ttm_eu_backoff_reservation(&parser->ticket,
732 &parser->validated);
Dave Airlie660e8552017-03-13 22:18:15 +0000733
734 for (i = 0; i < parser->num_post_dep_syncobjs; i++)
735 drm_syncobj_put(parser->post_dep_syncobjs[i]);
736 kfree(parser->post_dep_syncobjs);
737
Chris Wilsonf54d1862016-10-25 13:00:45 +0100738 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100739
Christian König3cb485f2015-05-11 15:34:59 +0200740 if (parser->ctx)
741 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800742 if (parser->bo_list)
743 amdgpu_bo_list_put(parser->bo_list);
744
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400745 for (i = 0; i < parser->nchunks; i++)
Michal Hocko20981052017-05-17 14:23:12 +0200746 kvfree(parser->chunks[i].kdata);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100748 if (parser->job)
749 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100750 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751}
752
Junwei Zhangb85891b2017-01-16 13:59:01 +0800753static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754{
755 struct amdgpu_device *adev = p->adev;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800756 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
757 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 struct amdgpu_bo_va *bo_va;
759 struct amdgpu_bo *bo;
760 int i, r;
761
Christian König194d2162016-10-12 15:13:52 +0200762 r = amdgpu_vm_update_directories(adev, vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763 if (r)
764 return r;
765
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100766 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400767 if (r)
768 return r;
769
Junwei Zhangb85891b2017-01-16 13:59:01 +0800770 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
771 if (r)
772 return r;
773
774 r = amdgpu_sync_fence(adev, &p->job->sync,
775 fpriv->prt_va->last_pt_update);
776 if (r)
777 return r;
778
Monk Liu24936642017-01-09 15:54:32 +0800779 if (amdgpu_sriov_vf(adev)) {
780 struct dma_fence *f;
Christian König0f4b3c62017-07-31 15:32:40 +0200781
782 bo_va = fpriv->csa_va;
Monk Liu24936642017-01-09 15:54:32 +0800783 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önig4e55eb32017-09-11 16:54:59 +0200819 r = amdgpu_vm_handle_moved(adev, vm);
Christian Königd5884512017-09-08 14:09:41 +0200820 if (r)
821 return r;
822
823 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_update);
824 if (r)
825 return r;
Christian Königb495bd32015-09-10 14:00:35 +0200826
827 if (amdgpu_vm_debug && p->bo_list) {
828 /* Invalidate all BOs to test for userspace bugs */
829 for (i = 0; i < p->bo_list->num_entries; i++) {
830 /* ignore duplicates */
831 bo = p->bo_list->array[i].robj;
832 if (!bo)
833 continue;
834
Christian König3f3333f2017-08-03 14:02:13 +0200835 amdgpu_vm_bo_invalidate(adev, bo, false);
Christian Königb495bd32015-09-10 14:00:35 +0200836 }
837 }
838
839 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840}
841
842static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100843 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844{
Christian Königb07c60c2016-01-31 12:29:04 +0100845 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100847 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 int i, r;
849
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100851 if (ring->funcs->parse_cs) {
852 for (i = 0; i < p->job->num_ibs; i++) {
853 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854 if (r)
855 return r;
856 }
Christian König45088ef2016-10-05 16:49:19 +0200857 }
858
859 if (p->job->vm) {
Christian König3f3333f2017-08-03 14:02:13 +0200860 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.base.bo);
Christian König9a795882016-06-22 14:25:55 +0200861
Junwei Zhangb85891b2017-01-16 13:59:01 +0800862 r = amdgpu_bo_vm_update_pte(p);
Christian König9a795882016-06-22 14:25:55 +0200863 if (r)
864 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 }
866
Christian König9a795882016-06-22 14:25:55 +0200867 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868}
869
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
871 struct amdgpu_cs_parser *parser)
872{
873 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
874 struct amdgpu_vm *vm = &fpriv->vm;
875 int i, j;
Monk Liu9a1b3af2017-03-08 15:51:13 +0800876 int r, ce_preempt = 0, de_preempt = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400877
Christian König50838c82016-02-03 13:44:52 +0100878 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400879 struct amdgpu_cs_chunk *chunk;
880 struct amdgpu_ib *ib;
881 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400882 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400883
884 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100885 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
887
888 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
889 continue;
890
Monk Liu65333e42017-03-27 15:14:53 +0800891 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
Harry Wentlande51a3222017-03-28 11:29:53 -0400892 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
Monk Liu65333e42017-03-27 15:14:53 +0800893 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
894 ce_preempt++;
895 else
896 de_preempt++;
Harry Wentlande51a3222017-03-28 11:29:53 -0400897 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800898
Monk Liu65333e42017-03-27 15:14:53 +0800899 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
900 if (ce_preempt > 1 || de_preempt > 1)
Monk Liue9d672b2017-03-15 12:18:57 +0800901 return -EINVAL;
Monk Liu65333e42017-03-27 15:14:53 +0800902 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800903
Andres Rodriguezeffd9242017-02-16 00:47:32 -0500904 r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
905 chunk_ib->ip_instance, chunk_ib->ring, &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200906 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400907 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908
Monk Liu2a9ceb82017-03-28 11:00:03 +0800909 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
Monk Liu753ad492016-08-26 13:28:28 +0800910 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
911 if (!parser->ctx->preamble_presented) {
912 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
913 parser->ctx->preamble_presented = true;
914 }
915 }
916
Christian Königb07c60c2016-01-31 12:29:04 +0100917 if (parser->job->ring && parser->job->ring != ring)
918 return -EINVAL;
919
920 parser->job->ring = ring;
921
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200923 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200924 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200925 uint64_t offset;
926 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200927
Christian König9cca0b82017-09-06 16:15:28 +0200928 r = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
929 &aobj, &m);
930 if (r) {
Marek Olšák3ccec532015-06-02 17:44:49 +0200931 DRM_ERROR("IB va_start is invalid\n");
Christian König9cca0b82017-09-06 16:15:28 +0200932 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 }
934
Christian König4802ce12015-06-10 17:20:11 +0200935 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
Christian Königa9f87f62017-03-30 14:03:59 +0200936 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian König4802ce12015-06-10 17:20:11 +0200937 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
938 return -EINVAL;
939 }
940
Marek Olšák3ccec532015-06-02 17:44:49 +0200941 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200942 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400944 return r;
945 }
946
Christian Königa9f87f62017-03-30 14:03:59 +0200947 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
Christian König4802ce12015-06-10 17:20:11 +0200948 kptr += chunk_ib->va_start - offset;
949
Christian König45088ef2016-10-05 16:49:19 +0200950 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951 if (r) {
952 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953 return r;
954 }
955
956 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
957 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100959 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960 if (r) {
961 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962 return r;
963 }
964
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966
Christian König45088ef2016-10-05 16:49:19 +0200967 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200968 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800969 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400970 j++;
971 }
972
Christian König758ac172016-05-06 22:14:00 +0200973 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200974 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +0200975 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
976 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +0200977 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978
979 return 0;
980}
981
Dave Airlie6f0308e2017-03-09 03:45:52 +0000982static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
983 struct amdgpu_cs_chunk *chunk)
984{
985 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
986 unsigned num_deps;
987 int i, r;
988 struct drm_amdgpu_cs_chunk_dep *deps;
989
990 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
991 num_deps = chunk->length_dw * 4 /
992 sizeof(struct drm_amdgpu_cs_chunk_dep);
993
994 for (i = 0; i < num_deps; ++i) {
995 struct amdgpu_ring *ring;
996 struct amdgpu_ctx *ctx;
997 struct dma_fence *fence;
998
999 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
1000 if (ctx == NULL)
1001 return -EINVAL;
1002
1003 r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
1004 deps[i].ip_type,
1005 deps[i].ip_instance,
1006 deps[i].ring, &ring);
1007 if (r) {
1008 amdgpu_ctx_put(ctx);
1009 return r;
1010 }
1011
1012 fence = amdgpu_ctx_get_fence(ctx, ring,
1013 deps[i].handle);
1014 if (IS_ERR(fence)) {
1015 r = PTR_ERR(fence);
1016 amdgpu_ctx_put(ctx);
1017 return r;
1018 } else if (fence) {
1019 r = amdgpu_sync_fence(p->adev, &p->job->sync,
1020 fence);
1021 dma_fence_put(fence);
1022 amdgpu_ctx_put(ctx);
1023 if (r)
1024 return r;
1025 }
1026 }
1027 return 0;
1028}
1029
Dave Airlie660e8552017-03-13 22:18:15 +00001030static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
1031 uint32_t handle)
1032{
1033 int r;
1034 struct dma_fence *fence;
Jason Ekstrandafaf5922017-08-25 10:52:19 -07001035 r = drm_syncobj_find_fence(p->filp, handle, &fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001036 if (r)
1037 return r;
1038
1039 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence);
1040 dma_fence_put(fence);
1041
1042 return r;
1043}
1044
1045static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
1046 struct amdgpu_cs_chunk *chunk)
1047{
1048 unsigned num_deps;
1049 int i, r;
1050 struct drm_amdgpu_cs_chunk_sem *deps;
1051
1052 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1053 num_deps = chunk->length_dw * 4 /
1054 sizeof(struct drm_amdgpu_cs_chunk_sem);
1055
1056 for (i = 0; i < num_deps; ++i) {
1057 r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
1058 if (r)
1059 return r;
1060 }
1061 return 0;
1062}
1063
1064static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1065 struct amdgpu_cs_chunk *chunk)
1066{
1067 unsigned num_deps;
1068 int i;
1069 struct drm_amdgpu_cs_chunk_sem *deps;
1070 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1071 num_deps = chunk->length_dw * 4 /
1072 sizeof(struct drm_amdgpu_cs_chunk_sem);
1073
1074 p->post_dep_syncobjs = kmalloc_array(num_deps,
1075 sizeof(struct drm_syncobj *),
1076 GFP_KERNEL);
1077 p->num_post_dep_syncobjs = 0;
1078
Christophe JAILLET06f10a52017-08-23 07:52:36 +02001079 if (!p->post_dep_syncobjs)
1080 return -ENOMEM;
1081
Dave Airlie660e8552017-03-13 22:18:15 +00001082 for (i = 0; i < num_deps; ++i) {
1083 p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
1084 if (!p->post_dep_syncobjs[i])
1085 return -EINVAL;
1086 p->num_post_dep_syncobjs++;
1087 }
1088 return 0;
1089}
1090
Christian König2b48d322015-06-19 17:31:29 +02001091static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1092 struct amdgpu_cs_parser *p)
1093{
Dave Airlie6f0308e2017-03-09 03:45:52 +00001094 int i, r;
Christian König2b48d322015-06-19 17:31:29 +02001095
Christian König2b48d322015-06-19 17:31:29 +02001096 for (i = 0; i < p->nchunks; ++i) {
Christian König2b48d322015-06-19 17:31:29 +02001097 struct amdgpu_cs_chunk *chunk;
Christian König2b48d322015-06-19 17:31:29 +02001098
1099 chunk = &p->chunks[i];
1100
Dave Airlie6f0308e2017-03-09 03:45:52 +00001101 if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
1102 r = amdgpu_cs_process_fence_dep(p, chunk);
1103 if (r)
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001104 return r;
Dave Airlie660e8552017-03-13 22:18:15 +00001105 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
1106 r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1107 if (r)
1108 return r;
1109 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
1110 r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1111 if (r)
1112 return r;
Christian König2b48d322015-06-19 17:31:29 +02001113 }
1114 }
1115
1116 return 0;
1117}
1118
Dave Airlie660e8552017-03-13 22:18:15 +00001119static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1120{
1121 int i;
1122
Chris Wilson00fc2c22017-07-05 21:12:44 +01001123 for (i = 0; i < p->num_post_dep_syncobjs; ++i)
1124 drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001125}
1126
Christian Königcd75dc62016-01-31 11:30:55 +01001127static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1128 union drm_amdgpu_cs *cs)
1129{
Christian Königb07c60c2016-01-31 12:29:04 +01001130 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001131 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001132 struct amdgpu_job *job;
Christian König3fe89772017-09-12 14:25:14 -04001133 unsigned i;
Monk Liueb01abc2017-09-15 13:40:31 +08001134 uint64_t seq;
1135
Monk Liue6869412016-03-07 12:49:55 +08001136 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001137
Christian König3fe89772017-09-12 14:25:14 -04001138 amdgpu_mn_lock(p->mn);
1139 if (p->bo_list) {
1140 for (i = p->bo_list->first_userptr;
1141 i < p->bo_list->num_entries; ++i) {
1142 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
1143
1144 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
1145 amdgpu_mn_unlock(p->mn);
1146 return -ERESTARTSYS;
1147 }
1148 }
1149 }
1150
Christian König50838c82016-02-03 13:44:52 +01001151 job = p->job;
1152 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001153
Christian König595a9cd2016-06-30 10:52:03 +02001154 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001155 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001156 amdgpu_job_free(job);
Christian König3fe89772017-09-12 14:25:14 -04001157 amdgpu_mn_unlock(p->mn);
Monk Liue6869412016-03-07 12:49:55 +08001158 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001159 }
1160
Monk Liue6869412016-03-07 12:49:55 +08001161 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001162 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001163 p->fence = dma_fence_get(&job->base.s_fence->finished);
Dave Airlie660e8552017-03-13 22:18:15 +00001164
Monk Liueb01abc2017-09-15 13:40:31 +08001165 r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
1166 if (r) {
1167 dma_fence_put(p->fence);
1168 dma_fence_put(&job->base.s_fence->finished);
1169 amdgpu_job_free(job);
1170 amdgpu_mn_unlock(p->mn);
1171 return r;
1172 }
1173
Dave Airlie660e8552017-03-13 22:18:15 +00001174 amdgpu_cs_post_dependencies(p);
1175
Monk Liueb01abc2017-09-15 13:40:31 +08001176 cs->out.handle = seq;
1177 job->uf_sequence = seq;
1178
Christian Königa5fb4ec2016-06-29 15:10:31 +02001179 amdgpu_job_free_resources(job);
Christian Königcd75dc62016-01-31 11:30:55 +01001180
1181 trace_amdgpu_cs_ioctl(job);
1182 amd_sched_entity_push_job(&job->base);
Christian König3fe89772017-09-12 14:25:14 -04001183
1184 ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1185 amdgpu_mn_unlock(p->mn);
1186
Christian Königcd75dc62016-01-31 11:30:55 +01001187 return 0;
1188}
1189
Chunming Zhou049fc522015-07-21 14:36:51 +08001190int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1191{
1192 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001193 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +08001194 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001195 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001196 bool reserved_buffers = false;
1197 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001198
Christian König0c418f12015-09-01 15:13:53 +02001199 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001200 return -EBUSY;
Chunming Zhouf1892132017-05-15 16:48:27 +08001201 if (amdgpu_kms_vram_lost(adev, fpriv))
1202 return -ENODEV;
Chunming Zhou049fc522015-07-21 14:36:51 +08001203
Christian König7e52a812015-11-04 15:44:39 +01001204 parser.adev = adev;
1205 parser.filp = filp;
1206
1207 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001209 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001210 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211 }
Huang Ruia414cd72016-10-30 23:05:47 +08001212
Christian König2a7d9bd2015-12-18 20:33:52 +01001213 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001214 if (r) {
1215 if (r == -ENOMEM)
1216 DRM_ERROR("Not enough memory for command submission!\n");
1217 else if (r != -ERESTARTSYS)
1218 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1219 goto out;
Christian König26a69802015-08-18 21:09:33 +02001220 }
1221
Huang Ruia414cd72016-10-30 23:05:47 +08001222 reserved_buffers = true;
1223 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001224 if (r)
1225 goto out;
1226
Huang Ruia414cd72016-10-30 23:05:47 +08001227 r = amdgpu_cs_dependencies(adev, &parser);
1228 if (r) {
1229 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1230 goto out;
1231 }
1232
Christian König50838c82016-02-03 13:44:52 +01001233 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001234 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001235
Christian König7e52a812015-11-04 15:44:39 +01001236 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001237 if (r)
1238 goto out;
1239
Christian König4acabfe2016-01-31 11:32:04 +01001240 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001242out:
Christian König7e52a812015-11-04 15:44:39 +01001243 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 return r;
1245}
1246
1247/**
1248 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1249 *
1250 * @dev: drm device
1251 * @data: data from userspace
1252 * @filp: file private
1253 *
1254 * Wait for the command submission identified by handle to finish.
1255 */
1256int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1257 struct drm_file *filp)
1258{
1259 union drm_amdgpu_wait_cs *wait = data;
1260 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001261 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001263 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001264 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001265 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 long r;
1267
Chunming Zhouf1892132017-05-15 16:48:27 +08001268 if (amdgpu_kms_vram_lost(adev, fpriv))
1269 return -ENODEV;
Christian König21c16bf2015-07-07 17:24:49 +02001270
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001271 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1272 if (ctx == NULL)
1273 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001274
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001275 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
1276 wait->in.ip_type, wait->in.ip_instance,
1277 wait->in.ring, &ring);
1278 if (r) {
1279 amdgpu_ctx_put(ctx);
1280 return r;
1281 }
1282
Chunming Zhou4b559c92015-07-21 15:53:04 +08001283 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1284 if (IS_ERR(fence))
1285 r = PTR_ERR(fence);
1286 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001287 r = dma_fence_wait_timeout(fence, true, timeout);
1288 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001289 } else
Christian König21c16bf2015-07-07 17:24:49 +02001290 r = 1;
1291
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001292 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001293 if (r < 0)
1294 return r;
1295
1296 memset(wait, 0, sizeof(*wait));
1297 wait->out.status = (r == 0);
1298
1299 return 0;
1300}
1301
1302/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001303 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1304 *
1305 * @adev: amdgpu device
1306 * @filp: file private
1307 * @user: drm_amdgpu_fence copied from user space
1308 */
1309static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1310 struct drm_file *filp,
1311 struct drm_amdgpu_fence *user)
1312{
1313 struct amdgpu_ring *ring;
1314 struct amdgpu_ctx *ctx;
1315 struct dma_fence *fence;
1316 int r;
1317
Junwei Zhangeef18a82016-11-04 16:16:10 -04001318 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1319 if (ctx == NULL)
1320 return ERR_PTR(-EINVAL);
1321
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001322 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
1323 user->ip_instance, user->ring, &ring);
1324 if (r) {
1325 amdgpu_ctx_put(ctx);
1326 return ERR_PTR(r);
1327 }
1328
Junwei Zhangeef18a82016-11-04 16:16:10 -04001329 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1330 amdgpu_ctx_put(ctx);
1331
1332 return fence;
1333}
1334
Marek Olšák7ca24cf2017-09-12 22:42:14 +02001335int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1336 struct drm_file *filp)
1337{
1338 struct amdgpu_device *adev = dev->dev_private;
1339 struct amdgpu_fpriv *fpriv = filp->driver_priv;
1340 union drm_amdgpu_fence_to_handle *info = data;
1341 struct dma_fence *fence;
1342 struct drm_syncobj *syncobj;
1343 struct sync_file *sync_file;
1344 int fd, r;
1345
1346 if (amdgpu_kms_vram_lost(adev, fpriv))
1347 return -ENODEV;
1348
1349 fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1350 if (IS_ERR(fence))
1351 return PTR_ERR(fence);
1352
1353 switch (info->in.what) {
1354 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1355 r = drm_syncobj_create(&syncobj, 0, fence);
1356 dma_fence_put(fence);
1357 if (r)
1358 return r;
1359 r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1360 drm_syncobj_put(syncobj);
1361 return r;
1362
1363 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1364 r = drm_syncobj_create(&syncobj, 0, fence);
1365 dma_fence_put(fence);
1366 if (r)
1367 return r;
1368 r = drm_syncobj_get_fd(syncobj, (int*)&info->out.handle);
1369 drm_syncobj_put(syncobj);
1370 return r;
1371
1372 case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1373 fd = get_unused_fd_flags(O_CLOEXEC);
1374 if (fd < 0) {
1375 dma_fence_put(fence);
1376 return fd;
1377 }
1378
1379 sync_file = sync_file_create(fence);
1380 dma_fence_put(fence);
1381 if (!sync_file) {
1382 put_unused_fd(fd);
1383 return -ENOMEM;
1384 }
1385
1386 fd_install(fd, sync_file->file);
1387 info->out.handle = fd;
1388 return 0;
1389
1390 default:
1391 return -EINVAL;
1392 }
1393}
1394
Junwei Zhangeef18a82016-11-04 16:16:10 -04001395/**
1396 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1397 *
1398 * @adev: amdgpu device
1399 * @filp: file private
1400 * @wait: wait parameters
1401 * @fences: array of drm_amdgpu_fence
1402 */
1403static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1404 struct drm_file *filp,
1405 union drm_amdgpu_wait_fences *wait,
1406 struct drm_amdgpu_fence *fences)
1407{
1408 uint32_t fence_count = wait->in.fence_count;
1409 unsigned int i;
1410 long r = 1;
1411
1412 for (i = 0; i < fence_count; i++) {
1413 struct dma_fence *fence;
1414 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1415
1416 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1417 if (IS_ERR(fence))
1418 return PTR_ERR(fence);
1419 else if (!fence)
1420 continue;
1421
1422 r = dma_fence_wait_timeout(fence, true, timeout);
Chunming Zhou32df87d2017-04-07 17:05:45 +08001423 dma_fence_put(fence);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001424 if (r < 0)
1425 return r;
1426
1427 if (r == 0)
1428 break;
1429 }
1430
1431 memset(wait, 0, sizeof(*wait));
1432 wait->out.status = (r > 0);
1433
1434 return 0;
1435}
1436
1437/**
1438 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1439 *
1440 * @adev: amdgpu device
1441 * @filp: file private
1442 * @wait: wait parameters
1443 * @fences: array of drm_amdgpu_fence
1444 */
1445static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1446 struct drm_file *filp,
1447 union drm_amdgpu_wait_fences *wait,
1448 struct drm_amdgpu_fence *fences)
1449{
1450 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1451 uint32_t fence_count = wait->in.fence_count;
1452 uint32_t first = ~0;
1453 struct dma_fence **array;
1454 unsigned int i;
1455 long r;
1456
1457 /* Prepare the fence array */
1458 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1459
1460 if (array == NULL)
1461 return -ENOMEM;
1462
1463 for (i = 0; i < fence_count; i++) {
1464 struct dma_fence *fence;
1465
1466 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1467 if (IS_ERR(fence)) {
1468 r = PTR_ERR(fence);
1469 goto err_free_fence_array;
1470 } else if (fence) {
1471 array[i] = fence;
1472 } else { /* NULL, the fence has been already signaled */
1473 r = 1;
Monk Liua2138ea2017-08-11 17:49:48 +08001474 first = i;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001475 goto out;
1476 }
1477 }
1478
1479 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1480 &first);
1481 if (r < 0)
1482 goto err_free_fence_array;
1483
1484out:
1485 memset(wait, 0, sizeof(*wait));
1486 wait->out.status = (r > 0);
1487 wait->out.first_signaled = first;
1488 /* set return value 0 to indicate success */
1489 r = 0;
1490
1491err_free_fence_array:
1492 for (i = 0; i < fence_count; i++)
1493 dma_fence_put(array[i]);
1494 kfree(array);
1495
1496 return r;
1497}
1498
1499/**
1500 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1501 *
1502 * @dev: drm device
1503 * @data: data from userspace
1504 * @filp: file private
1505 */
1506int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1507 struct drm_file *filp)
1508{
1509 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001510 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001511 union drm_amdgpu_wait_fences *wait = data;
1512 uint32_t fence_count = wait->in.fence_count;
1513 struct drm_amdgpu_fence *fences_user;
1514 struct drm_amdgpu_fence *fences;
1515 int r;
1516
Chunming Zhouf1892132017-05-15 16:48:27 +08001517 if (amdgpu_kms_vram_lost(adev, fpriv))
1518 return -ENODEV;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001519 /* Get the fences from userspace */
1520 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1521 GFP_KERNEL);
1522 if (fences == NULL)
1523 return -ENOMEM;
1524
Christian König7ecc2452017-07-26 17:02:52 +02001525 fences_user = u64_to_user_ptr(wait->in.fences);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001526 if (copy_from_user(fences, fences_user,
1527 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1528 r = -EFAULT;
1529 goto err_free_fences;
1530 }
1531
1532 if (wait->in.wait_all)
1533 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1534 else
1535 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1536
1537err_free_fences:
1538 kfree(fences);
1539
1540 return r;
1541}
1542
1543/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544 * amdgpu_cs_find_bo_va - find bo_va for VM address
1545 *
1546 * @parser: command submission parser context
1547 * @addr: VM address
1548 * @bo: resulting BO of the mapping found
1549 *
1550 * Search the buffer objects in the command submission context for a certain
1551 * virtual memory address. Returns allocation structure when found, NULL
1552 * otherwise.
1553 */
Christian König9cca0b82017-09-06 16:15:28 +02001554int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1555 uint64_t addr, struct amdgpu_bo **bo,
1556 struct amdgpu_bo_va_mapping **map)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557{
Christian Königaebc5e62017-09-06 16:55:16 +02001558 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1559 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 struct amdgpu_bo_va_mapping *mapping;
Christian König9cca0b82017-09-06 16:15:28 +02001561 int r;
Christian König15486fd22015-12-22 16:06:12 +01001562
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001563 addr /= AMDGPU_GPU_PAGE_SIZE;
1564
Christian Königaebc5e62017-09-06 16:55:16 +02001565 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1566 if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1567 return -EINVAL;
Christian König15486fd22015-12-22 16:06:12 +01001568
Christian Königaebc5e62017-09-06 16:55:16 +02001569 *bo = mapping->bo_va->base.bo;
1570 *map = mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571
Christian Königaebc5e62017-09-06 16:55:16 +02001572 /* Double check that the BO is reserved by this CS */
1573 if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
1574 return -EINVAL;
Christian König7fc11952015-07-30 11:53:42 +02001575
Christian König9cca0b82017-09-06 16:15:28 +02001576 r = amdgpu_ttm_bind(&(*bo)->tbo, &(*bo)->tbo.mem);
1577 if (unlikely(r))
1578 return r;
Christian Königc855e252016-09-05 17:00:57 +02001579
Christian König9cca0b82017-09-06 16:15:28 +02001580 if ((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
Christian Königc855e252016-09-05 17:00:57 +02001581 return 0;
1582
Christian König9cca0b82017-09-06 16:15:28 +02001583 (*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1584 amdgpu_ttm_placement_from_domain(*bo, (*bo)->allowed_domains);
1585 return ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, false, false);
Christian Königc855e252016-09-05 17:00:57 +02001586}