blob: c2310d4eebc8bc2864f526633c5fd53ee9b9d73a [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
Cihangir Akturkf62facc2017-08-03 14:58:16 +030057 drm_gem_object_put_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 */
Christian König7ecc2452017-07-26 17:02:52 +020093 chunk_array_user = u64_to_user_ptr(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
Christian König7ecc2452017-07-26 17:02:52 +0200113 chunk_ptr = u64_to_user_ptr(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;
Christian König7ecc2452017-07-26 17:02:52 +0200124 cdata = u64_to_user_ptr(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 */
John Brooks00f06b22017-06-27 22:33:18 -0400226static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
227 u64 *max_bytes,
228 u64 *max_vis_bytes)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400229{
Marek Olšák95844d22016-08-17 23:49:27 +0200230 s64 time_us, increment_us;
Marek Olšák95844d22016-08-17 23:49:27 +0200231 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232
Marek Olšák95844d22016-08-17 23:49:27 +0200233 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
234 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235 *
Marek Olšák95844d22016-08-17 23:49:27 +0200236 * It means that in order to get full max MBps, at least 5 IBs per
237 * second must be submitted and not more than 200ms apart from each
238 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 */
Marek Olšák95844d22016-08-17 23:49:27 +0200240 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241
John Brooks00f06b22017-06-27 22:33:18 -0400242 if (!adev->mm_stats.log2_max_MBps) {
243 *max_bytes = 0;
244 *max_vis_bytes = 0;
245 return;
246 }
Marek Olšák95844d22016-08-17 23:49:27 +0200247
248 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
Christian König3c848bb2017-08-07 17:46:49 +0200249 used_vram = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
Marek Olšák95844d22016-08-17 23:49:27 +0200250 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
251
252 spin_lock(&adev->mm_stats.lock);
253
254 /* Increase the amount of accumulated us. */
255 time_us = ktime_to_us(ktime_get());
256 increment_us = time_us - adev->mm_stats.last_update_us;
257 adev->mm_stats.last_update_us = time_us;
258 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
259 us_upper_bound);
260
261 /* This prevents the short period of low performance when the VRAM
262 * usage is low and the driver is in debt or doesn't have enough
263 * accumulated us to fill VRAM quickly.
264 *
265 * The situation can occur in these cases:
266 * - a lot of VRAM is freed by userspace
267 * - the presence of a big buffer causes a lot of evictions
268 * (solution: split buffers into smaller ones)
269 *
270 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
271 * accum_us to a positive number.
272 */
273 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
274 s64 min_us;
275
276 /* Be more aggresive on dGPUs. Try to fill a portion of free
277 * VRAM now.
278 */
279 if (!(adev->flags & AMD_IS_APU))
280 min_us = bytes_to_us(adev, free_vram / 4);
281 else
282 min_us = 0; /* Reset accum_us on APUs. */
283
284 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
285 }
286
John Brooks00f06b22017-06-27 22:33:18 -0400287 /* This is set to 0 if the driver is in debt to disallow (optional)
Marek Olšák95844d22016-08-17 23:49:27 +0200288 * buffer moves.
289 */
John Brooks00f06b22017-06-27 22:33:18 -0400290 *max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
291
292 /* Do the same for visible VRAM if half of it is free */
293 if (adev->mc.visible_vram_size < adev->mc.real_vram_size) {
294 u64 total_vis_vram = adev->mc.visible_vram_size;
Christian König3c848bb2017-08-07 17:46:49 +0200295 u64 used_vis_vram =
296 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
John Brooks00f06b22017-06-27 22:33:18 -0400297
298 if (used_vis_vram < total_vis_vram) {
299 u64 free_vis_vram = total_vis_vram - used_vis_vram;
300 adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
301 increment_us, us_upper_bound);
302
303 if (free_vis_vram >= total_vis_vram / 2)
304 adev->mm_stats.accum_us_vis =
305 max(bytes_to_us(adev, free_vis_vram / 2),
306 adev->mm_stats.accum_us_vis);
307 }
308
309 *max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
310 } else {
311 *max_vis_bytes = 0;
312 }
Marek Olšák95844d22016-08-17 23:49:27 +0200313
314 spin_unlock(&adev->mm_stats.lock);
Marek Olšák95844d22016-08-17 23:49:27 +0200315}
316
317/* Report how many bytes have really been moved for the last command
318 * submission. This can result in a debt that can stop buffer migrations
319 * temporarily.
320 */
John Brooks00f06b22017-06-27 22:33:18 -0400321void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
322 u64 num_vis_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200323{
324 spin_lock(&adev->mm_stats.lock);
325 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
John Brooks00f06b22017-06-27 22:33:18 -0400326 adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
Marek Olšák95844d22016-08-17 23:49:27 +0200327 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328}
329
Chunming Zhou14fd8332016-08-04 13:05:46 +0800330static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
331 struct amdgpu_bo *bo)
332{
Christian Königa7d64de2016-09-15 14:58:48 +0200333 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
John Brooks00f06b22017-06-27 22:33:18 -0400334 u64 initial_bytes_moved, bytes_moved;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800335 uint32_t domain;
336 int r;
337
338 if (bo->pin_count)
339 return 0;
340
Marek Olšák95844d22016-08-17 23:49:27 +0200341 /* Don't move this buffer if we have depleted our allowance
342 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800343 */
John Brooks00f06b22017-06-27 22:33:18 -0400344 if (p->bytes_moved < p->bytes_moved_threshold) {
345 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
346 (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
347 /* And don't move a CPU_ACCESS_REQUIRED BO to limited
348 * visible VRAM if we've depleted our allowance to do
349 * that.
350 */
351 if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
Kent Russell6d7d9c52017-08-08 07:58:01 -0400352 domain = bo->preferred_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400353 else
354 domain = bo->allowed_domains;
355 } else {
Kent Russell6d7d9c52017-08-08 07:58:01 -0400356 domain = bo->preferred_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400357 }
358 } else {
Chunming Zhou14fd8332016-08-04 13:05:46 +0800359 domain = bo->allowed_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400360 }
Chunming Zhou14fd8332016-08-04 13:05:46 +0800361
362retry:
363 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200364 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800365 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
John Brooks00f06b22017-06-27 22:33:18 -0400366 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
367 initial_bytes_moved;
368 p->bytes_moved += bytes_moved;
369 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
370 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
371 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT)
372 p->bytes_moved_vis += bytes_moved;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800373
Christian König1abdc3d2016-08-31 17:28:11 +0200374 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
375 domain = bo->allowed_domains;
376 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800377 }
378
379 return r;
380}
381
Christian König662bfa62016-09-01 12:13:18 +0200382/* Last resort, try to evict something from the current working set */
383static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200384 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200385{
Christian Königf7da30d2016-09-28 12:03:04 +0200386 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200387 int r;
388
389 if (!p->evictable)
390 return false;
391
392 for (;&p->evictable->tv.head != &p->validated;
393 p->evictable = list_prev_entry(p->evictable, tv.head)) {
394
395 struct amdgpu_bo_list_entry *candidate = p->evictable;
396 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200397 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
John Brooks00f06b22017-06-27 22:33:18 -0400398 u64 initial_bytes_moved, bytes_moved;
399 bool update_bytes_moved_vis;
Christian König662bfa62016-09-01 12:13:18 +0200400 uint32_t other;
401
402 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200403 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200404 break;
405
406 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
407
408 /* Check if this BO is in one of the domains we need space for */
409 if (!(other & domain))
410 continue;
411
412 /* Check if we can move this BO somewhere else */
413 other = bo->allowed_domains & ~domain;
414 if (!other)
415 continue;
416
417 /* Good we can try to move this BO somewhere else */
418 amdgpu_ttm_placement_from_domain(bo, other);
John Brooks00f06b22017-06-27 22:33:18 -0400419 update_bytes_moved_vis =
420 adev->mc.visible_vram_size < adev->mc.real_vram_size &&
421 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
422 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT;
Christian Königa7d64de2016-09-15 14:58:48 +0200423 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200424 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
John Brooks00f06b22017-06-27 22:33:18 -0400425 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200426 initial_bytes_moved;
John Brooks00f06b22017-06-27 22:33:18 -0400427 p->bytes_moved += bytes_moved;
428 if (update_bytes_moved_vis)
429 p->bytes_moved_vis += bytes_moved;
Christian König662bfa62016-09-01 12:13:18 +0200430
431 if (unlikely(r))
432 break;
433
434 p->evictable = list_prev_entry(p->evictable, tv.head);
435 list_move(&candidate->tv.head, &p->validated);
436
437 return true;
438 }
439
440 return false;
441}
442
Christian Königf7da30d2016-09-28 12:03:04 +0200443static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
444{
445 struct amdgpu_cs_parser *p = param;
446 int r;
447
448 do {
449 r = amdgpu_cs_bo_validate(p, bo);
450 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
451 if (r)
452 return r;
453
454 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500455 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200456
457 return r;
458}
459
Baoyou Xie761c2e82016-09-03 13:57:14 +0800460static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200461 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 int r;
465
Christian Königa5b75052015-09-03 16:40:39 +0200466 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100467 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100468 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100469 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470
Christian Königcc325d12016-02-08 11:08:35 +0100471 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
472 if (usermm && usermm != current->mm)
473 return -EPERM;
474
Christian König2f568db2016-02-23 12:36:59 +0100475 /* Check if we have user pages and nobody bound the BO already */
Christian Königca666a32017-09-05 14:30:05 +0200476 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
477 lobj->user_pages) {
Christian König1b0c0f92017-09-05 14:36:44 +0200478 amdgpu_ttm_placement_from_domain(bo,
479 AMDGPU_GEM_DOMAIN_CPU);
480 r = ttm_bo_validate(&bo->tbo, &bo->placement, true,
481 false);
482 if (r)
483 return r;
Christian Königa216ab02017-09-02 13:21:31 +0200484 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
485 lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100486 binding_userptr = true;
487 }
488
Christian König662bfa62016-09-01 12:13:18 +0200489 if (p->evictable == lobj)
490 p->evictable = NULL;
491
Christian Königf7da30d2016-09-28 12:03:04 +0200492 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800493 if (r)
Christian König36409d122015-12-21 20:31:35 +0100494 return r;
Christian König662bfa62016-09-01 12:13:18 +0200495
Christian König2f568db2016-02-23 12:36:59 +0100496 if (binding_userptr) {
Michal Hocko20981052017-05-17 14:23:12 +0200497 kvfree(lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100498 lobj->user_pages = NULL;
499 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 }
501 return 0;
502}
503
Christian König2a7d9bd2015-12-18 20:33:52 +0100504static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
505 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506{
507 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100508 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200509 struct list_head duplicates;
Christian König2f568db2016-02-23 12:36:59 +0100510 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100511 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512
Christian König2a7d9bd2015-12-18 20:33:52 +0100513 INIT_LIST_HEAD(&p->validated);
514
515 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
Christian König3fe89772017-09-12 14:25:14 -0400516 if (p->bo_list) {
Christian König636ce252015-12-18 21:26:47 +0100517 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
Christian König3fe89772017-09-12 14:25:14 -0400518 if (p->bo_list->first_userptr != p->bo_list->num_entries)
519 p->mn = amdgpu_mn_get(p->adev);
520 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521
Christian König3c0eea62015-12-11 14:39:05 +0100522 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100523 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
Christian König758ac172016-05-06 22:14:00 +0200525 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100526 list_add(&p->uf_entry.tv.head, &p->validated);
527
Christian König2f568db2016-02-23 12:36:59 +0100528 while (1) {
529 struct list_head need_pages;
530 unsigned i;
531
532 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
533 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200534 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800535 if (r != -ERESTARTSYS)
536 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100537 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200538 }
Christian König2f568db2016-02-23 12:36:59 +0100539
540 /* Without a BO list we don't have userptr BOs */
541 if (!p->bo_list)
542 break;
543
544 INIT_LIST_HEAD(&need_pages);
545 for (i = p->bo_list->first_userptr;
546 i < p->bo_list->num_entries; ++i) {
Christian Königca666a32017-09-05 14:30:05 +0200547 struct amdgpu_bo *bo;
Christian König2f568db2016-02-23 12:36:59 +0100548
549 e = &p->bo_list->array[i];
Christian Königca666a32017-09-05 14:30:05 +0200550 bo = e->robj;
Christian König2f568db2016-02-23 12:36:59 +0100551
Christian Königca666a32017-09-05 14:30:05 +0200552 if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
Christian König2f568db2016-02-23 12:36:59 +0100553 &e->user_invalidated) && e->user_pages) {
554
555 /* We acquired a page array, but somebody
Alex Xie9f69c0f2017-06-20 16:33:02 -0400556 * invalidated it. Free it and try again
Christian König2f568db2016-02-23 12:36:59 +0100557 */
558 release_pages(e->user_pages,
Christian Königca666a32017-09-05 14:30:05 +0200559 bo->tbo.ttm->num_pages,
Christian König2f568db2016-02-23 12:36:59 +0100560 false);
Michal Hocko20981052017-05-17 14:23:12 +0200561 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100562 e->user_pages = NULL;
563 }
564
Christian Königca666a32017-09-05 14:30:05 +0200565 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
Christian König2f568db2016-02-23 12:36:59 +0100566 !e->user_pages) {
567 list_del(&e->tv.head);
568 list_add(&e->tv.head, &need_pages);
569
570 amdgpu_bo_unreserve(e->robj);
571 }
572 }
573
574 if (list_empty(&need_pages))
575 break;
576
577 /* Unreserve everything again. */
578 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
579
Marek Olšákf1037952016-07-30 00:48:39 +0200580 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100581 if (!--tries) {
582 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200583 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100584 goto error_free_pages;
585 }
586
Alex Xieeb0f0372017-06-08 14:53:26 -0400587 /* Fill the page arrays for all userptrs. */
Christian König2f568db2016-02-23 12:36:59 +0100588 list_for_each_entry(e, &need_pages, tv.head) {
589 struct ttm_tt *ttm = e->robj->tbo.ttm;
590
Michal Hocko20981052017-05-17 14:23:12 +0200591 e->user_pages = kvmalloc_array(ttm->num_pages,
592 sizeof(struct page*),
593 GFP_KERNEL | __GFP_ZERO);
Christian König2f568db2016-02-23 12:36:59 +0100594 if (!e->user_pages) {
595 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200596 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100597 goto error_free_pages;
598 }
599
600 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
601 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200602 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Michal Hocko20981052017-05-17 14:23:12 +0200603 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100604 e->user_pages = NULL;
605 goto error_free_pages;
606 }
607 }
608
609 /* And try again. */
610 list_splice(&need_pages, &p->validated);
611 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400612
John Brooks00f06b22017-06-27 22:33:18 -0400613 amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
614 &p->bytes_moved_vis_threshold);
Christian Königf69f90a12015-12-21 19:47:42 +0100615 p->bytes_moved = 0;
John Brooks00f06b22017-06-27 22:33:18 -0400616 p->bytes_moved_vis = 0;
Christian König662bfa62016-09-01 12:13:18 +0200617 p->evictable = list_last_entry(&p->validated,
618 struct amdgpu_bo_list_entry,
619 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100620
Christian Königf7da30d2016-09-28 12:03:04 +0200621 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
622 amdgpu_cs_validate, p);
623 if (r) {
624 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
625 goto error_validate;
626 }
627
Christian Königf69f90a12015-12-21 19:47:42 +0100628 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200629 if (r) {
630 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200631 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200632 }
Christian Königa5b75052015-09-03 16:40:39 +0200633
Christian Königf69f90a12015-12-21 19:47:42 +0100634 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200635 if (r) {
636 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100637 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200638 }
Christian Königa8480302016-01-05 16:03:39 +0100639
John Brooks00f06b22017-06-27 22:33:18 -0400640 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
641 p->bytes_moved_vis);
Christian Königa8480302016-01-05 16:03:39 +0100642 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200643 struct amdgpu_bo *gds = p->bo_list->gds_obj;
644 struct amdgpu_bo *gws = p->bo_list->gws_obj;
645 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100646 struct amdgpu_vm *vm = &fpriv->vm;
647 unsigned i;
648
649 for (i = 0; i < p->bo_list->num_entries; i++) {
650 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
651
652 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
653 }
Christian Königd88bf582016-05-06 17:50:03 +0200654
655 if (gds) {
656 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
657 p->job->gds_size = amdgpu_bo_size(gds);
658 }
659 if (gws) {
660 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
661 p->job->gws_size = amdgpu_bo_size(gws);
662 }
663 if (oa) {
664 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
665 p->job->oa_size = amdgpu_bo_size(oa);
666 }
Christian Königa8480302016-01-05 16:03:39 +0100667 }
Christian Königa5b75052015-09-03 16:40:39 +0200668
Christian Königc855e252016-09-05 17:00:57 +0200669 if (!r && p->uf_entry.robj) {
670 struct amdgpu_bo *uf = p->uf_entry.robj;
671
Christian Königbb990bb2016-09-09 16:32:33 +0200672 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200673 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
674 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200675
Christian Königa5b75052015-09-03 16:40:39 +0200676error_validate:
Christian Königb6369222017-08-03 11:44:01 -0400677 if (r)
Christian Königa5b75052015-09-03 16:40:39 +0200678 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
679
Christian König2f568db2016-02-23 12:36:59 +0100680error_free_pages:
681
Christian König2f568db2016-02-23 12:36:59 +0100682 if (p->bo_list) {
683 for (i = p->bo_list->first_userptr;
684 i < p->bo_list->num_entries; ++i) {
685 e = &p->bo_list->array[i];
686
687 if (!e->user_pages)
688 continue;
689
690 release_pages(e->user_pages,
691 e->robj->tbo.ttm->num_pages,
692 false);
Michal Hocko20981052017-05-17 14:23:12 +0200693 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100694 }
695 }
696
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 return r;
698}
699
700static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
701{
702 struct amdgpu_bo_list_entry *e;
703 int r;
704
705 list_for_each_entry(e, &p->validated, tv.head) {
706 struct reservation_object *resv = e->robj->tbo.resv;
Christian Könige86f9ce2016-02-08 12:13:05 +0100707 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708
709 if (r)
710 return r;
711 }
712 return 0;
713}
714
Christian König984810f2015-11-14 21:05:35 +0100715/**
716 * cs_parser_fini() - clean parser states
717 * @parser: parser structure holding parsing context.
718 * @error: error number
719 *
720 * If error is set than unvalidate buffer, otherwise just free memory
721 * used by parsing context.
722 **/
Christian Königb6369222017-08-03 11:44:01 -0400723static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
724 bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800725{
Christian König984810f2015-11-14 21:05:35 +0100726 unsigned i;
727
Christian König3fe89772017-09-12 14:25:14 -0400728 if (error && backoff)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 ttm_eu_backoff_reservation(&parser->ticket,
730 &parser->validated);
Dave Airlie660e8552017-03-13 22:18:15 +0000731
732 for (i = 0; i < parser->num_post_dep_syncobjs; i++)
733 drm_syncobj_put(parser->post_dep_syncobjs[i]);
734 kfree(parser->post_dep_syncobjs);
735
Chris Wilsonf54d1862016-10-25 13:00:45 +0100736 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100737
Christian König3cb485f2015-05-11 15:34:59 +0200738 if (parser->ctx)
739 amdgpu_ctx_put(parser->ctx);
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800740 if (parser->bo_list)
741 amdgpu_bo_list_put(parser->bo_list);
742
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400743 for (i = 0; i < parser->nchunks; i++)
Michal Hocko20981052017-05-17 14:23:12 +0200744 kvfree(parser->chunks[i].kdata);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400745 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100746 if (parser->job)
747 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100748 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749}
750
Junwei Zhangb85891b2017-01-16 13:59:01 +0800751static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752{
753 struct amdgpu_device *adev = p->adev;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800754 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
755 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756 struct amdgpu_bo_va *bo_va;
757 struct amdgpu_bo *bo;
758 int i, r;
759
Christian König194d2162016-10-12 15:13:52 +0200760 r = amdgpu_vm_update_directories(adev, vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 if (r)
762 return r;
763
Christian Königa24960f2016-10-12 13:20:52 +0200764 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200765 if (r)
766 return r;
767
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100768 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769 if (r)
770 return r;
771
Junwei Zhangb85891b2017-01-16 13:59:01 +0800772 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
773 if (r)
774 return r;
775
776 r = amdgpu_sync_fence(adev, &p->job->sync,
777 fpriv->prt_va->last_pt_update);
778 if (r)
779 return r;
780
Monk Liu24936642017-01-09 15:54:32 +0800781 if (amdgpu_sriov_vf(adev)) {
782 struct dma_fence *f;
Christian König0f4b3c62017-07-31 15:32:40 +0200783
784 bo_va = fpriv->csa_va;
Monk Liu24936642017-01-09 15:54:32 +0800785 BUG_ON(!bo_va);
786 r = amdgpu_vm_bo_update(adev, bo_va, false);
787 if (r)
788 return r;
789
790 f = bo_va->last_pt_update;
791 r = amdgpu_sync_fence(adev, &p->job->sync, f);
792 if (r)
793 return r;
794 }
795
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796 if (p->bo_list) {
797 for (i = 0; i < p->bo_list->num_entries; i++) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100798 struct dma_fence *f;
Christian König91e1a522015-07-06 22:06:40 +0200799
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400800 /* ignore duplicates */
801 bo = p->bo_list->array[i].robj;
802 if (!bo)
803 continue;
804
805 bo_va = p->bo_list->array[i].bo_va;
806 if (bo_va == NULL)
807 continue;
808
Christian König99e124f2016-08-16 14:43:17 +0200809 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400810 if (r)
811 return r;
812
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800813 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100814 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200815 if (r)
816 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817 }
Christian Königb495bd32015-09-10 14:00:35 +0200818
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819 }
820
Christian König73fb16e2017-08-16 11:13:48 +0200821 r = amdgpu_vm_handle_moved(adev, vm, &p->job->sync);
Christian Königb495bd32015-09-10 14:00:35 +0200822
823 if (amdgpu_vm_debug && p->bo_list) {
824 /* Invalidate all BOs to test for userspace bugs */
825 for (i = 0; i < p->bo_list->num_entries; i++) {
826 /* ignore duplicates */
827 bo = p->bo_list->array[i].robj;
828 if (!bo)
829 continue;
830
Christian König3f3333f2017-08-03 14:02:13 +0200831 amdgpu_vm_bo_invalidate(adev, bo, false);
Christian Königb495bd32015-09-10 14:00:35 +0200832 }
833 }
834
835 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400836}
837
838static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100839 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840{
Christian Königb07c60c2016-01-31 12:29:04 +0100841 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100843 struct amdgpu_ring *ring = p->job->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 int i, r;
845
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 /* Only for UVD/VCE VM emulation */
Christian Königb07c60c2016-01-31 12:29:04 +0100847 if (ring->funcs->parse_cs) {
848 for (i = 0; i < p->job->num_ibs; i++) {
849 r = amdgpu_ring_parse_cs(ring, p, i);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850 if (r)
851 return r;
852 }
Christian König45088ef2016-10-05 16:49:19 +0200853 }
854
855 if (p->job->vm) {
Christian König3f3333f2017-08-03 14:02:13 +0200856 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.base.bo);
Christian König9a795882016-06-22 14:25:55 +0200857
Junwei Zhangb85891b2017-01-16 13:59:01 +0800858 r = amdgpu_bo_vm_update_pte(p);
Christian König9a795882016-06-22 14:25:55 +0200859 if (r)
860 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861 }
862
Christian König9a795882016-06-22 14:25:55 +0200863 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400864}
865
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400866static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
867 struct amdgpu_cs_parser *parser)
868{
869 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
870 struct amdgpu_vm *vm = &fpriv->vm;
871 int i, j;
Monk Liu9a1b3af2017-03-08 15:51:13 +0800872 int r, ce_preempt = 0, de_preempt = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873
Christian König50838c82016-02-03 13:44:52 +0100874 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 struct amdgpu_cs_chunk *chunk;
876 struct amdgpu_ib *ib;
877 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400879
880 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100881 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400882 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
883
884 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
885 continue;
886
Monk Liu65333e42017-03-27 15:14:53 +0800887 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
Harry Wentlande51a3222017-03-28 11:29:53 -0400888 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
Monk Liu65333e42017-03-27 15:14:53 +0800889 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
890 ce_preempt++;
891 else
892 de_preempt++;
Harry Wentlande51a3222017-03-28 11:29:53 -0400893 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800894
Monk Liu65333e42017-03-27 15:14:53 +0800895 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
896 if (ce_preempt > 1 || de_preempt > 1)
Monk Liue9d672b2017-03-15 12:18:57 +0800897 return -EINVAL;
Monk Liu65333e42017-03-27 15:14:53 +0800898 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800899
Andres Rodriguezeffd9242017-02-16 00:47:32 -0500900 r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
901 chunk_ib->ip_instance, chunk_ib->ring, &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200902 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400903 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400904
Monk Liu2a9ceb82017-03-28 11:00:03 +0800905 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
Monk Liu753ad492016-08-26 13:28:28 +0800906 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
907 if (!parser->ctx->preamble_presented) {
908 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
909 parser->ctx->preamble_presented = true;
910 }
911 }
912
Christian Königb07c60c2016-01-31 12:29:04 +0100913 if (parser->job->ring && parser->job->ring != ring)
914 return -EINVAL;
915
916 parser->job->ring = ring;
917
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918 if (ring->funcs->parse_cs) {
Christian König4802ce12015-06-10 17:20:11 +0200919 struct amdgpu_bo_va_mapping *m;
Marek Olšák3ccec532015-06-02 17:44:49 +0200920 struct amdgpu_bo *aobj = NULL;
Christian König4802ce12015-06-10 17:20:11 +0200921 uint64_t offset;
922 uint8_t *kptr;
Marek Olšák3ccec532015-06-02 17:44:49 +0200923
Christian König4802ce12015-06-10 17:20:11 +0200924 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
925 &aobj);
Marek Olšák3ccec532015-06-02 17:44:49 +0200926 if (!aobj) {
927 DRM_ERROR("IB va_start is invalid\n");
928 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 }
930
Christian König4802ce12015-06-10 17:20:11 +0200931 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
Christian Königa9f87f62017-03-30 14:03:59 +0200932 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian König4802ce12015-06-10 17:20:11 +0200933 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
934 return -EINVAL;
935 }
936
Marek Olšák3ccec532015-06-02 17:44:49 +0200937 /* the IB should be reserved at this point */
Christian König4802ce12015-06-10 17:20:11 +0200938 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 return r;
941 }
942
Christian Königa9f87f62017-03-30 14:03:59 +0200943 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
Christian König4802ce12015-06-10 17:20:11 +0200944 kptr += chunk_ib->va_start - offset;
945
Christian König45088ef2016-10-05 16:49:19 +0200946 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947 if (r) {
948 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949 return r;
950 }
951
952 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
953 amdgpu_bo_kunmap(aobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954 } else {
Christian Königb07c60c2016-01-31 12:29:04 +0100955 r = amdgpu_ib_get(adev, vm, 0, ib);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 if (r) {
957 DRM_ERROR("Failed to get ib !\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958 return r;
959 }
960
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962
Christian König45088ef2016-10-05 16:49:19 +0200963 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200964 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800965 ib->flags = chunk_ib->flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966 j++;
967 }
968
Christian König758ac172016-05-06 22:14:00 +0200969 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200970 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +0200971 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
972 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +0200973 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974
975 return 0;
976}
977
Dave Airlie6f0308e2017-03-09 03:45:52 +0000978static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
979 struct amdgpu_cs_chunk *chunk)
980{
981 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
982 unsigned num_deps;
983 int i, r;
984 struct drm_amdgpu_cs_chunk_dep *deps;
985
986 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
987 num_deps = chunk->length_dw * 4 /
988 sizeof(struct drm_amdgpu_cs_chunk_dep);
989
990 for (i = 0; i < num_deps; ++i) {
991 struct amdgpu_ring *ring;
992 struct amdgpu_ctx *ctx;
993 struct dma_fence *fence;
994
995 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
996 if (ctx == NULL)
997 return -EINVAL;
998
999 r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
1000 deps[i].ip_type,
1001 deps[i].ip_instance,
1002 deps[i].ring, &ring);
1003 if (r) {
1004 amdgpu_ctx_put(ctx);
1005 return r;
1006 }
1007
1008 fence = amdgpu_ctx_get_fence(ctx, ring,
1009 deps[i].handle);
1010 if (IS_ERR(fence)) {
1011 r = PTR_ERR(fence);
1012 amdgpu_ctx_put(ctx);
1013 return r;
1014 } else if (fence) {
1015 r = amdgpu_sync_fence(p->adev, &p->job->sync,
1016 fence);
1017 dma_fence_put(fence);
1018 amdgpu_ctx_put(ctx);
1019 if (r)
1020 return r;
1021 }
1022 }
1023 return 0;
1024}
1025
Dave Airlie660e8552017-03-13 22:18:15 +00001026static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
1027 uint32_t handle)
1028{
1029 int r;
1030 struct dma_fence *fence;
Jason Ekstrandafaf5922017-08-25 10:52:19 -07001031 r = drm_syncobj_find_fence(p->filp, handle, &fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001032 if (r)
1033 return r;
1034
1035 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence);
1036 dma_fence_put(fence);
1037
1038 return r;
1039}
1040
1041static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
1042 struct amdgpu_cs_chunk *chunk)
1043{
1044 unsigned num_deps;
1045 int i, r;
1046 struct drm_amdgpu_cs_chunk_sem *deps;
1047
1048 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1049 num_deps = chunk->length_dw * 4 /
1050 sizeof(struct drm_amdgpu_cs_chunk_sem);
1051
1052 for (i = 0; i < num_deps; ++i) {
1053 r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
1054 if (r)
1055 return r;
1056 }
1057 return 0;
1058}
1059
1060static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1061 struct amdgpu_cs_chunk *chunk)
1062{
1063 unsigned num_deps;
1064 int i;
1065 struct drm_amdgpu_cs_chunk_sem *deps;
1066 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1067 num_deps = chunk->length_dw * 4 /
1068 sizeof(struct drm_amdgpu_cs_chunk_sem);
1069
1070 p->post_dep_syncobjs = kmalloc_array(num_deps,
1071 sizeof(struct drm_syncobj *),
1072 GFP_KERNEL);
1073 p->num_post_dep_syncobjs = 0;
1074
Christophe JAILLET06f10a52017-08-23 07:52:36 +02001075 if (!p->post_dep_syncobjs)
1076 return -ENOMEM;
1077
Dave Airlie660e8552017-03-13 22:18:15 +00001078 for (i = 0; i < num_deps; ++i) {
1079 p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
1080 if (!p->post_dep_syncobjs[i])
1081 return -EINVAL;
1082 p->num_post_dep_syncobjs++;
1083 }
1084 return 0;
1085}
1086
Christian König2b48d322015-06-19 17:31:29 +02001087static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1088 struct amdgpu_cs_parser *p)
1089{
Dave Airlie6f0308e2017-03-09 03:45:52 +00001090 int i, r;
Christian König2b48d322015-06-19 17:31:29 +02001091
Christian König2b48d322015-06-19 17:31:29 +02001092 for (i = 0; i < p->nchunks; ++i) {
Christian König2b48d322015-06-19 17:31:29 +02001093 struct amdgpu_cs_chunk *chunk;
Christian König2b48d322015-06-19 17:31:29 +02001094
1095 chunk = &p->chunks[i];
1096
Dave Airlie6f0308e2017-03-09 03:45:52 +00001097 if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
1098 r = amdgpu_cs_process_fence_dep(p, chunk);
1099 if (r)
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001100 return r;
Dave Airlie660e8552017-03-13 22:18:15 +00001101 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
1102 r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1103 if (r)
1104 return r;
1105 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
1106 r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1107 if (r)
1108 return r;
Christian König2b48d322015-06-19 17:31:29 +02001109 }
1110 }
1111
1112 return 0;
1113}
1114
Dave Airlie660e8552017-03-13 22:18:15 +00001115static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1116{
1117 int i;
1118
Chris Wilson00fc2c22017-07-05 21:12:44 +01001119 for (i = 0; i < p->num_post_dep_syncobjs; ++i)
1120 drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001121}
1122
Christian Königcd75dc62016-01-31 11:30:55 +01001123static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1124 union drm_amdgpu_cs *cs)
1125{
Christian Königb07c60c2016-01-31 12:29:04 +01001126 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001127 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001128 struct amdgpu_job *job;
Christian König3fe89772017-09-12 14:25:14 -04001129 unsigned i;
Monk Liue6869412016-03-07 12:49:55 +08001130 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001131
Christian König3fe89772017-09-12 14:25:14 -04001132 amdgpu_mn_lock(p->mn);
1133 if (p->bo_list) {
1134 for (i = p->bo_list->first_userptr;
1135 i < p->bo_list->num_entries; ++i) {
1136 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
1137
1138 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
1139 amdgpu_mn_unlock(p->mn);
1140 return -ERESTARTSYS;
1141 }
1142 }
1143 }
1144
Christian König50838c82016-02-03 13:44:52 +01001145 job = p->job;
1146 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001147
Christian König595a9cd2016-06-30 10:52:03 +02001148 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001149 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001150 amdgpu_job_free(job);
Christian König3fe89772017-09-12 14:25:14 -04001151 amdgpu_mn_unlock(p->mn);
Monk Liue6869412016-03-07 12:49:55 +08001152 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001153 }
1154
Monk Liue6869412016-03-07 12:49:55 +08001155 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001156 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001157 p->fence = dma_fence_get(&job->base.s_fence->finished);
Dave Airlie660e8552017-03-13 22:18:15 +00001158
1159 amdgpu_cs_post_dependencies(p);
1160
Christian König595a9cd2016-06-30 10:52:03 +02001161 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
Christian König758ac172016-05-06 22:14:00 +02001162 job->uf_sequence = cs->out.handle;
Christian Königa5fb4ec2016-06-29 15:10:31 +02001163 amdgpu_job_free_resources(job);
Christian Königcd75dc62016-01-31 11:30:55 +01001164
1165 trace_amdgpu_cs_ioctl(job);
1166 amd_sched_entity_push_job(&job->base);
Christian König3fe89772017-09-12 14:25:14 -04001167
1168 ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1169 amdgpu_mn_unlock(p->mn);
1170
Christian Königcd75dc62016-01-31 11:30:55 +01001171 return 0;
1172}
1173
Chunming Zhou049fc522015-07-21 14:36:51 +08001174int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1175{
1176 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001177 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Chunming Zhou049fc522015-07-21 14:36:51 +08001178 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001179 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001180 bool reserved_buffers = false;
1181 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001182
Christian König0c418f12015-09-01 15:13:53 +02001183 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001184 return -EBUSY;
Chunming Zhouf1892132017-05-15 16:48:27 +08001185 if (amdgpu_kms_vram_lost(adev, fpriv))
1186 return -ENODEV;
Chunming Zhou049fc522015-07-21 14:36:51 +08001187
Christian König7e52a812015-11-04 15:44:39 +01001188 parser.adev = adev;
1189 parser.filp = filp;
1190
1191 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001193 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001194 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 }
Huang Ruia414cd72016-10-30 23:05:47 +08001196
Christian König2a7d9bd2015-12-18 20:33:52 +01001197 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001198 if (r) {
1199 if (r == -ENOMEM)
1200 DRM_ERROR("Not enough memory for command submission!\n");
1201 else if (r != -ERESTARTSYS)
1202 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1203 goto out;
Christian König26a69802015-08-18 21:09:33 +02001204 }
1205
Huang Ruia414cd72016-10-30 23:05:47 +08001206 reserved_buffers = true;
1207 r = amdgpu_cs_ib_fill(adev, &parser);
Christian König26a69802015-08-18 21:09:33 +02001208 if (r)
1209 goto out;
1210
Huang Ruia414cd72016-10-30 23:05:47 +08001211 r = amdgpu_cs_dependencies(adev, &parser);
1212 if (r) {
1213 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1214 goto out;
1215 }
1216
Christian König50838c82016-02-03 13:44:52 +01001217 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001218 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001219
Christian König7e52a812015-11-04 15:44:39 +01001220 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001221 if (r)
1222 goto out;
1223
Christian König4acabfe2016-01-31 11:32:04 +01001224 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226out:
Christian König7e52a812015-11-04 15:44:39 +01001227 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001228 return r;
1229}
1230
1231/**
1232 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1233 *
1234 * @dev: drm device
1235 * @data: data from userspace
1236 * @filp: file private
1237 *
1238 * Wait for the command submission identified by handle to finish.
1239 */
1240int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1241 struct drm_file *filp)
1242{
1243 union drm_amdgpu_wait_cs *wait = data;
1244 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001245 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001247 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001248 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001249 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250 long r;
1251
Chunming Zhouf1892132017-05-15 16:48:27 +08001252 if (amdgpu_kms_vram_lost(adev, fpriv))
1253 return -ENODEV;
Christian König21c16bf2015-07-07 17:24:49 +02001254
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001255 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1256 if (ctx == NULL)
1257 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001258
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001259 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
1260 wait->in.ip_type, wait->in.ip_instance,
1261 wait->in.ring, &ring);
1262 if (r) {
1263 amdgpu_ctx_put(ctx);
1264 return r;
1265 }
1266
Chunming Zhou4b559c92015-07-21 15:53:04 +08001267 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1268 if (IS_ERR(fence))
1269 r = PTR_ERR(fence);
1270 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001271 r = dma_fence_wait_timeout(fence, true, timeout);
1272 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001273 } else
Christian König21c16bf2015-07-07 17:24:49 +02001274 r = 1;
1275
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001276 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001277 if (r < 0)
1278 return r;
1279
1280 memset(wait, 0, sizeof(*wait));
1281 wait->out.status = (r == 0);
1282
1283 return 0;
1284}
1285
1286/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001287 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1288 *
1289 * @adev: amdgpu device
1290 * @filp: file private
1291 * @user: drm_amdgpu_fence copied from user space
1292 */
1293static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1294 struct drm_file *filp,
1295 struct drm_amdgpu_fence *user)
1296{
1297 struct amdgpu_ring *ring;
1298 struct amdgpu_ctx *ctx;
1299 struct dma_fence *fence;
1300 int r;
1301
Junwei Zhangeef18a82016-11-04 16:16:10 -04001302 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1303 if (ctx == NULL)
1304 return ERR_PTR(-EINVAL);
1305
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001306 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
1307 user->ip_instance, user->ring, &ring);
1308 if (r) {
1309 amdgpu_ctx_put(ctx);
1310 return ERR_PTR(r);
1311 }
1312
Junwei Zhangeef18a82016-11-04 16:16:10 -04001313 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1314 amdgpu_ctx_put(ctx);
1315
1316 return fence;
1317}
1318
1319/**
1320 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1321 *
1322 * @adev: amdgpu device
1323 * @filp: file private
1324 * @wait: wait parameters
1325 * @fences: array of drm_amdgpu_fence
1326 */
1327static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1328 struct drm_file *filp,
1329 union drm_amdgpu_wait_fences *wait,
1330 struct drm_amdgpu_fence *fences)
1331{
1332 uint32_t fence_count = wait->in.fence_count;
1333 unsigned int i;
1334 long r = 1;
1335
1336 for (i = 0; i < fence_count; i++) {
1337 struct dma_fence *fence;
1338 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1339
1340 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1341 if (IS_ERR(fence))
1342 return PTR_ERR(fence);
1343 else if (!fence)
1344 continue;
1345
1346 r = dma_fence_wait_timeout(fence, true, timeout);
Chunming Zhou32df87d2017-04-07 17:05:45 +08001347 dma_fence_put(fence);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001348 if (r < 0)
1349 return r;
1350
1351 if (r == 0)
1352 break;
1353 }
1354
1355 memset(wait, 0, sizeof(*wait));
1356 wait->out.status = (r > 0);
1357
1358 return 0;
1359}
1360
1361/**
1362 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1363 *
1364 * @adev: amdgpu device
1365 * @filp: file private
1366 * @wait: wait parameters
1367 * @fences: array of drm_amdgpu_fence
1368 */
1369static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1370 struct drm_file *filp,
1371 union drm_amdgpu_wait_fences *wait,
1372 struct drm_amdgpu_fence *fences)
1373{
1374 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1375 uint32_t fence_count = wait->in.fence_count;
1376 uint32_t first = ~0;
1377 struct dma_fence **array;
1378 unsigned int i;
1379 long r;
1380
1381 /* Prepare the fence array */
1382 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1383
1384 if (array == NULL)
1385 return -ENOMEM;
1386
1387 for (i = 0; i < fence_count; i++) {
1388 struct dma_fence *fence;
1389
1390 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1391 if (IS_ERR(fence)) {
1392 r = PTR_ERR(fence);
1393 goto err_free_fence_array;
1394 } else if (fence) {
1395 array[i] = fence;
1396 } else { /* NULL, the fence has been already signaled */
1397 r = 1;
Monk Liua2138ea2017-08-11 17:49:48 +08001398 first = i;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001399 goto out;
1400 }
1401 }
1402
1403 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1404 &first);
1405 if (r < 0)
1406 goto err_free_fence_array;
1407
1408out:
1409 memset(wait, 0, sizeof(*wait));
1410 wait->out.status = (r > 0);
1411 wait->out.first_signaled = first;
1412 /* set return value 0 to indicate success */
1413 r = 0;
1414
1415err_free_fence_array:
1416 for (i = 0; i < fence_count; i++)
1417 dma_fence_put(array[i]);
1418 kfree(array);
1419
1420 return r;
1421}
1422
1423/**
1424 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1425 *
1426 * @dev: drm device
1427 * @data: data from userspace
1428 * @filp: file private
1429 */
1430int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1431 struct drm_file *filp)
1432{
1433 struct amdgpu_device *adev = dev->dev_private;
Chunming Zhouf1892132017-05-15 16:48:27 +08001434 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001435 union drm_amdgpu_wait_fences *wait = data;
1436 uint32_t fence_count = wait->in.fence_count;
1437 struct drm_amdgpu_fence *fences_user;
1438 struct drm_amdgpu_fence *fences;
1439 int r;
1440
Chunming Zhouf1892132017-05-15 16:48:27 +08001441 if (amdgpu_kms_vram_lost(adev, fpriv))
1442 return -ENODEV;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001443 /* Get the fences from userspace */
1444 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1445 GFP_KERNEL);
1446 if (fences == NULL)
1447 return -ENOMEM;
1448
Christian König7ecc2452017-07-26 17:02:52 +02001449 fences_user = u64_to_user_ptr(wait->in.fences);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001450 if (copy_from_user(fences, fences_user,
1451 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1452 r = -EFAULT;
1453 goto err_free_fences;
1454 }
1455
1456 if (wait->in.wait_all)
1457 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1458 else
1459 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1460
1461err_free_fences:
1462 kfree(fences);
1463
1464 return r;
1465}
1466
1467/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468 * amdgpu_cs_find_bo_va - find bo_va for VM address
1469 *
1470 * @parser: command submission parser context
1471 * @addr: VM address
1472 * @bo: resulting BO of the mapping found
1473 *
1474 * Search the buffer objects in the command submission context for a certain
1475 * virtual memory address. Returns allocation structure when found, NULL
1476 * otherwise.
1477 */
1478struct amdgpu_bo_va_mapping *
1479amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1480 uint64_t addr, struct amdgpu_bo **bo)
1481{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 struct amdgpu_bo_va_mapping *mapping;
Christian König15486fd22015-12-22 16:06:12 +01001483 unsigned i;
1484
1485 if (!parser->bo_list)
1486 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487
1488 addr /= AMDGPU_GPU_PAGE_SIZE;
1489
Christian König15486fd22015-12-22 16:06:12 +01001490 for (i = 0; i < parser->bo_list->num_entries; i++) {
1491 struct amdgpu_bo_list_entry *lobj;
1492
1493 lobj = &parser->bo_list->array[i];
1494 if (!lobj->bo_va)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001495 continue;
1496
Christian König15486fd22015-12-22 16:06:12 +01001497 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001498 if (mapping->start > addr ||
1499 addr > mapping->last)
Christian König7fc11952015-07-30 11:53:42 +02001500 continue;
1501
Christian Königec681542017-08-01 10:51:43 +02001502 *bo = lobj->bo_va->base.bo;
Christian König7fc11952015-07-30 11:53:42 +02001503 return mapping;
1504 }
1505
Christian König15486fd22015-12-22 16:06:12 +01001506 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
Christian Königa9f87f62017-03-30 14:03:59 +02001507 if (mapping->start > addr ||
1508 addr > mapping->last)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 continue;
1510
Christian Königec681542017-08-01 10:51:43 +02001511 *bo = lobj->bo_va->base.bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001512 return mapping;
1513 }
1514 }
1515
1516 return NULL;
1517}
Christian Königc855e252016-09-05 17:00:57 +02001518
1519/**
1520 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1521 *
1522 * @parser: command submission parser context
1523 *
1524 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1525 */
1526int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1527{
1528 unsigned i;
1529 int r;
1530
1531 if (!parser->bo_list)
1532 return 0;
1533
1534 for (i = 0; i < parser->bo_list->num_entries; i++) {
1535 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1536
Christian Königbb990bb2016-09-09 16:32:33 +02001537 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +02001538 if (unlikely(r))
1539 return r;
Christian König03f48dd2016-08-15 17:00:22 +02001540
1541 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
1542 continue;
1543
1544 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1545 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
1546 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
1547 if (unlikely(r))
1548 return r;
Christian Königc855e252016-09-05 17:00:57 +02001549 }
1550
1551 return 0;
1552}