blob: dfd37785563f28c30355746b374e392a703b38a4 [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
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -040093 mutex_lock(&p->ctx->lock);
94
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095 /* get chunks */
Christian König7ecc2452017-07-26 17:02:52 +020096 chunk_array_user = u64_to_user_ptr(cs->in.chunks);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097 if (copy_from_user(chunk_array, chunk_array_user,
98 sizeof(uint64_t)*cs->in.num_chunks)) {
Dan Carpenter1d263472015-09-23 13:59:28 +030099 ret = -EFAULT;
Andrey Grodzovsky26eedf62017-10-11 17:02:02 -0400100 goto free_chunk;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 }
102
103 p->nchunks = cs->in.num_chunks;
monk.liue60b3442015-07-17 18:39:25 +0800104 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 GFP_KERNEL);
Dan Carpenter1d263472015-09-23 13:59:28 +0300106 if (!p->chunks) {
107 ret = -ENOMEM;
Andrey Grodzovsky26eedf62017-10-11 17:02:02 -0400108 goto free_chunk;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 }
110
111 for (i = 0; i < p->nchunks; i++) {
112 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
113 struct drm_amdgpu_cs_chunk user_chunk;
114 uint32_t __user *cdata;
115
Christian König7ecc2452017-07-26 17:02:52 +0200116 chunk_ptr = u64_to_user_ptr(chunk_array[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 if (copy_from_user(&user_chunk, chunk_ptr,
118 sizeof(struct drm_amdgpu_cs_chunk))) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300119 ret = -EFAULT;
120 i--;
121 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 }
123 p->chunks[i].chunk_id = user_chunk.chunk_id;
124 p->chunks[i].length_dw = user_chunk.length_dw;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125
126 size = p->chunks[i].length_dw;
Christian König7ecc2452017-07-26 17:02:52 +0200127 cdata = u64_to_user_ptr(user_chunk.chunk_data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128
Michal Hocko20981052017-05-17 14:23:12 +0200129 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130 if (p->chunks[i].kdata == NULL) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300131 ret = -ENOMEM;
132 i--;
133 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 }
135 size *= sizeof(uint32_t);
136 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300137 ret = -EFAULT;
138 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400139 }
140
Christian König9a5e8fb2015-06-23 17:07:03 +0200141 switch (p->chunks[i].chunk_id) {
142 case AMDGPU_CHUNK_ID_IB:
Christian König50838c82016-02-03 13:44:52 +0100143 ++num_ibs;
Christian König9a5e8fb2015-06-23 17:07:03 +0200144 break;
145
146 case AMDGPU_CHUNK_ID_FENCE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
Christian König91acbeb2015-12-14 16:42:31 +0100148 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
Dan Carpenter1d263472015-09-23 13:59:28 +0300149 ret = -EINVAL;
150 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151 }
Christian König91acbeb2015-12-14 16:42:31 +0100152
Christian König758ac172016-05-06 22:14:00 +0200153 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
154 &uf_offset);
Christian König91acbeb2015-12-14 16:42:31 +0100155 if (ret)
156 goto free_partial_kdata;
157
Christian König9a5e8fb2015-06-23 17:07:03 +0200158 break;
159
Christian König2b48d322015-06-19 17:31:29 +0200160 case AMDGPU_CHUNK_ID_DEPENDENCIES:
Dave Airlie660e8552017-03-13 22:18:15 +0000161 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
162 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
Christian König2b48d322015-06-19 17:31:29 +0200163 break;
164
Christian König9a5e8fb2015-06-23 17:07:03 +0200165 default:
Dan Carpenter1d263472015-09-23 13:59:28 +0300166 ret = -EINVAL;
167 goto free_partial_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 }
169 }
170
Monk Liuc5637832016-04-19 20:11:32 +0800171 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
Christian König50838c82016-02-03 13:44:52 +0100172 if (ret)
Christian König4acabfe2016-01-31 11:32:04 +0100173 goto free_all_kdata;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174
Christian Könige55f2b62017-10-09 15:18:43 +0200175 p->job->vram_lost_counter = atomic_read(&p->adev->vram_lost_counter);
176 if (p->ctx->vram_lost_counter != p->job->vram_lost_counter) {
177 ret = -ECANCELED;
178 goto free_all_kdata;
179 }
Christian König14e47f92017-10-09 15:04:41 +0200180
Christian Königb5f5acb2016-06-29 13:26:41 +0200181 if (p->uf_entry.robj)
182 p->job->uf_addr = uf_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183 kfree(chunk_array);
Dan Carpenter1d263472015-09-23 13:59:28 +0300184 return 0;
185
186free_all_kdata:
187 i = p->nchunks - 1;
188free_partial_kdata:
189 for (; i >= 0; i--)
Michal Hocko20981052017-05-17 14:23:12 +0200190 kvfree(p->chunks[i].kdata);
Dan Carpenter1d263472015-09-23 13:59:28 +0300191 kfree(p->chunks);
Dave Airlie607523d2017-03-10 12:13:04 +1000192 p->chunks = NULL;
193 p->nchunks = 0;
Dan Carpenter1d263472015-09-23 13:59:28 +0300194free_chunk:
195 kfree(chunk_array);
196
197 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198}
199
Marek Olšák95844d22016-08-17 23:49:27 +0200200/* Convert microseconds to bytes. */
201static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
202{
203 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
204 return 0;
205
206 /* Since accum_us is incremented by a million per second, just
207 * multiply it by the number of MB/s to get the number of bytes.
208 */
209 return us << adev->mm_stats.log2_max_MBps;
210}
211
212static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
213{
214 if (!adev->mm_stats.log2_max_MBps)
215 return 0;
216
217 return bytes >> adev->mm_stats.log2_max_MBps;
218}
219
220/* Returns how many bytes TTM can move right now. If no bytes can be moved,
221 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
222 * which means it can go over the threshold once. If that happens, the driver
223 * will be in debt and no other buffer migrations can be done until that debt
224 * is repaid.
225 *
226 * This approach allows moving a buffer of any size (it's important to allow
227 * that).
228 *
229 * The currency is simply time in microseconds and it increases as the clock
230 * ticks. The accumulated microseconds (us) are converted to bytes and
231 * returned.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232 */
John Brooks00f06b22017-06-27 22:33:18 -0400233static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
234 u64 *max_bytes,
235 u64 *max_vis_bytes)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236{
Marek Olšák95844d22016-08-17 23:49:27 +0200237 s64 time_us, increment_us;
Marek Olšák95844d22016-08-17 23:49:27 +0200238 u64 free_vram, total_vram, used_vram;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239
Marek Olšák95844d22016-08-17 23:49:27 +0200240 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
241 * throttling.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242 *
Marek Olšák95844d22016-08-17 23:49:27 +0200243 * It means that in order to get full max MBps, at least 5 IBs per
244 * second must be submitted and not more than 200ms apart from each
245 * other.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246 */
Marek Olšák95844d22016-08-17 23:49:27 +0200247 const s64 us_upper_bound = 200000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400248
John Brooks00f06b22017-06-27 22:33:18 -0400249 if (!adev->mm_stats.log2_max_MBps) {
250 *max_bytes = 0;
251 *max_vis_bytes = 0;
252 return;
253 }
Marek Olšák95844d22016-08-17 23:49:27 +0200254
255 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
Christian König3c848bb2017-08-07 17:46:49 +0200256 used_vram = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
Marek Olšák95844d22016-08-17 23:49:27 +0200257 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
258
259 spin_lock(&adev->mm_stats.lock);
260
261 /* Increase the amount of accumulated us. */
262 time_us = ktime_to_us(ktime_get());
263 increment_us = time_us - adev->mm_stats.last_update_us;
264 adev->mm_stats.last_update_us = time_us;
265 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
266 us_upper_bound);
267
268 /* This prevents the short period of low performance when the VRAM
269 * usage is low and the driver is in debt or doesn't have enough
270 * accumulated us to fill VRAM quickly.
271 *
272 * The situation can occur in these cases:
273 * - a lot of VRAM is freed by userspace
274 * - the presence of a big buffer causes a lot of evictions
275 * (solution: split buffers into smaller ones)
276 *
277 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
278 * accum_us to a positive number.
279 */
280 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
281 s64 min_us;
282
283 /* Be more aggresive on dGPUs. Try to fill a portion of free
284 * VRAM now.
285 */
286 if (!(adev->flags & AMD_IS_APU))
287 min_us = bytes_to_us(adev, free_vram / 4);
288 else
289 min_us = 0; /* Reset accum_us on APUs. */
290
291 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
292 }
293
John Brooks00f06b22017-06-27 22:33:18 -0400294 /* This is set to 0 if the driver is in debt to disallow (optional)
Marek Olšák95844d22016-08-17 23:49:27 +0200295 * buffer moves.
296 */
John Brooks00f06b22017-06-27 22:33:18 -0400297 *max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
298
299 /* Do the same for visible VRAM if half of it is free */
300 if (adev->mc.visible_vram_size < adev->mc.real_vram_size) {
301 u64 total_vis_vram = adev->mc.visible_vram_size;
Christian König3c848bb2017-08-07 17:46:49 +0200302 u64 used_vis_vram =
303 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
John Brooks00f06b22017-06-27 22:33:18 -0400304
305 if (used_vis_vram < total_vis_vram) {
306 u64 free_vis_vram = total_vis_vram - used_vis_vram;
307 adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
308 increment_us, us_upper_bound);
309
310 if (free_vis_vram >= total_vis_vram / 2)
311 adev->mm_stats.accum_us_vis =
312 max(bytes_to_us(adev, free_vis_vram / 2),
313 adev->mm_stats.accum_us_vis);
314 }
315
316 *max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
317 } else {
318 *max_vis_bytes = 0;
319 }
Marek Olšák95844d22016-08-17 23:49:27 +0200320
321 spin_unlock(&adev->mm_stats.lock);
Marek Olšák95844d22016-08-17 23:49:27 +0200322}
323
324/* Report how many bytes have really been moved for the last command
325 * submission. This can result in a debt that can stop buffer migrations
326 * temporarily.
327 */
John Brooks00f06b22017-06-27 22:33:18 -0400328void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
329 u64 num_vis_bytes)
Marek Olšák95844d22016-08-17 23:49:27 +0200330{
331 spin_lock(&adev->mm_stats.lock);
332 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
John Brooks00f06b22017-06-27 22:33:18 -0400333 adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
Marek Olšák95844d22016-08-17 23:49:27 +0200334 spin_unlock(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400335}
336
Chunming Zhou14fd8332016-08-04 13:05:46 +0800337static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
338 struct amdgpu_bo *bo)
339{
Christian Königa7d64de2016-09-15 14:58:48 +0200340 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
John Brooks00f06b22017-06-27 22:33:18 -0400341 u64 initial_bytes_moved, bytes_moved;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800342 uint32_t domain;
343 int r;
344
345 if (bo->pin_count)
346 return 0;
347
Marek Olšák95844d22016-08-17 23:49:27 +0200348 /* Don't move this buffer if we have depleted our allowance
349 * to move it. Don't move anything if the threshold is zero.
Chunming Zhou14fd8332016-08-04 13:05:46 +0800350 */
John Brooks00f06b22017-06-27 22:33:18 -0400351 if (p->bytes_moved < p->bytes_moved_threshold) {
352 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
353 (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
354 /* And don't move a CPU_ACCESS_REQUIRED BO to limited
355 * visible VRAM if we've depleted our allowance to do
356 * that.
357 */
358 if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
Kent Russell6d7d9c52017-08-08 07:58:01 -0400359 domain = bo->preferred_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400360 else
361 domain = bo->allowed_domains;
362 } else {
Kent Russell6d7d9c52017-08-08 07:58:01 -0400363 domain = bo->preferred_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400364 }
365 } else {
Chunming Zhou14fd8332016-08-04 13:05:46 +0800366 domain = bo->allowed_domains;
John Brooks00f06b22017-06-27 22:33:18 -0400367 }
Chunming Zhou14fd8332016-08-04 13:05:46 +0800368
369retry:
370 amdgpu_ttm_placement_from_domain(bo, domain);
Christian Königa7d64de2016-09-15 14:58:48 +0200371 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800372 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
John Brooks00f06b22017-06-27 22:33:18 -0400373 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
374 initial_bytes_moved;
375 p->bytes_moved += bytes_moved;
376 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
377 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
378 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT)
379 p->bytes_moved_vis += bytes_moved;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800380
Christian König1abdc3d2016-08-31 17:28:11 +0200381 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
382 domain = bo->allowed_domains;
383 goto retry;
Chunming Zhou14fd8332016-08-04 13:05:46 +0800384 }
385
386 return r;
387}
388
Christian König662bfa62016-09-01 12:13:18 +0200389/* Last resort, try to evict something from the current working set */
390static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
Christian Königf7da30d2016-09-28 12:03:04 +0200391 struct amdgpu_bo *validated)
Christian König662bfa62016-09-01 12:13:18 +0200392{
Christian Königf7da30d2016-09-28 12:03:04 +0200393 uint32_t domain = validated->allowed_domains;
Christian König662bfa62016-09-01 12:13:18 +0200394 int r;
395
396 if (!p->evictable)
397 return false;
398
399 for (;&p->evictable->tv.head != &p->validated;
400 p->evictable = list_prev_entry(p->evictable, tv.head)) {
401
402 struct amdgpu_bo_list_entry *candidate = p->evictable;
403 struct amdgpu_bo *bo = candidate->robj;
Christian Königa7d64de2016-09-15 14:58:48 +0200404 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
John Brooks00f06b22017-06-27 22:33:18 -0400405 u64 initial_bytes_moved, bytes_moved;
406 bool update_bytes_moved_vis;
Christian König662bfa62016-09-01 12:13:18 +0200407 uint32_t other;
408
409 /* If we reached our current BO we can forget it */
Christian Königf7da30d2016-09-28 12:03:04 +0200410 if (candidate->robj == validated)
Christian König662bfa62016-09-01 12:13:18 +0200411 break;
412
413 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
414
415 /* Check if this BO is in one of the domains we need space for */
416 if (!(other & domain))
417 continue;
418
419 /* Check if we can move this BO somewhere else */
420 other = bo->allowed_domains & ~domain;
421 if (!other)
422 continue;
423
424 /* Good we can try to move this BO somewhere else */
425 amdgpu_ttm_placement_from_domain(bo, other);
John Brooks00f06b22017-06-27 22:33:18 -0400426 update_bytes_moved_vis =
427 adev->mc.visible_vram_size < adev->mc.real_vram_size &&
428 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
429 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT;
Christian Königa7d64de2016-09-15 14:58:48 +0200430 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
Christian König662bfa62016-09-01 12:13:18 +0200431 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
John Brooks00f06b22017-06-27 22:33:18 -0400432 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
Christian König662bfa62016-09-01 12:13:18 +0200433 initial_bytes_moved;
John Brooks00f06b22017-06-27 22:33:18 -0400434 p->bytes_moved += bytes_moved;
435 if (update_bytes_moved_vis)
436 p->bytes_moved_vis += bytes_moved;
Christian König662bfa62016-09-01 12:13:18 +0200437
438 if (unlikely(r))
439 break;
440
441 p->evictable = list_prev_entry(p->evictable, tv.head);
442 list_move(&candidate->tv.head, &p->validated);
443
444 return true;
445 }
446
447 return false;
448}
449
Christian Königf7da30d2016-09-28 12:03:04 +0200450static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
451{
452 struct amdgpu_cs_parser *p = param;
453 int r;
454
455 do {
456 r = amdgpu_cs_bo_validate(p, bo);
457 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
458 if (r)
459 return r;
460
461 if (bo->shadow)
Alex Xie1cd99a82016-11-30 17:19:40 -0500462 r = amdgpu_cs_bo_validate(p, bo->shadow);
Christian Königf7da30d2016-09-28 12:03:04 +0200463
464 return r;
465}
466
Baoyou Xie761c2e82016-09-03 13:57:14 +0800467static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
Christian Königa5b75052015-09-03 16:40:39 +0200468 struct list_head *validated)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470 struct amdgpu_bo_list_entry *lobj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 int r;
472
Christian Königa5b75052015-09-03 16:40:39 +0200473 list_for_each_entry(lobj, validated, tv.head) {
Christian König36409d122015-12-21 20:31:35 +0100474 struct amdgpu_bo *bo = lobj->robj;
Christian König2f568db2016-02-23 12:36:59 +0100475 bool binding_userptr = false;
Christian Königcc325d12016-02-08 11:08:35 +0100476 struct mm_struct *usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400477
Christian Königcc325d12016-02-08 11:08:35 +0100478 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
479 if (usermm && usermm != current->mm)
480 return -EPERM;
481
Christian König2f568db2016-02-23 12:36:59 +0100482 /* Check if we have user pages and nobody bound the BO already */
Christian Königca666a32017-09-05 14:30:05 +0200483 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
484 lobj->user_pages) {
Christian König1b0c0f92017-09-05 14:36:44 +0200485 amdgpu_ttm_placement_from_domain(bo,
486 AMDGPU_GEM_DOMAIN_CPU);
487 r = ttm_bo_validate(&bo->tbo, &bo->placement, true,
488 false);
489 if (r)
490 return r;
Christian Königa216ab02017-09-02 13:21:31 +0200491 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
492 lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100493 binding_userptr = true;
494 }
495
Christian König662bfa62016-09-01 12:13:18 +0200496 if (p->evictable == lobj)
497 p->evictable = NULL;
498
Christian Königf7da30d2016-09-28 12:03:04 +0200499 r = amdgpu_cs_validate(p, bo);
Chunming Zhou14fd8332016-08-04 13:05:46 +0800500 if (r)
Christian König36409d122015-12-21 20:31:35 +0100501 return r;
Christian König662bfa62016-09-01 12:13:18 +0200502
Christian König2f568db2016-02-23 12:36:59 +0100503 if (binding_userptr) {
Michal Hocko20981052017-05-17 14:23:12 +0200504 kvfree(lobj->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100505 lobj->user_pages = NULL;
506 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 }
508 return 0;
509}
510
Christian König2a7d9bd2015-12-18 20:33:52 +0100511static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
512 union drm_amdgpu_cs *cs)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513{
514 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Christian König2f568db2016-02-23 12:36:59 +0100515 struct amdgpu_bo_list_entry *e;
Christian Königa5b75052015-09-03 16:40:39 +0200516 struct list_head duplicates;
Christian König2f568db2016-02-23 12:36:59 +0100517 unsigned i, tries = 10;
Christian König636ce252015-12-18 21:26:47 +0100518 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400519
Christian König2a7d9bd2015-12-18 20:33:52 +0100520 INIT_LIST_HEAD(&p->validated);
521
522 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
Christian König3fe89772017-09-12 14:25:14 -0400523 if (p->bo_list) {
Christian König636ce252015-12-18 21:26:47 +0100524 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
Christian König3fe89772017-09-12 14:25:14 -0400525 if (p->bo_list->first_userptr != p->bo_list->num_entries)
526 p->mn = amdgpu_mn_get(p->adev);
527 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528
Christian König3c0eea62015-12-11 14:39:05 +0100529 INIT_LIST_HEAD(&duplicates);
Christian König56467eb2015-12-11 15:16:32 +0100530 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400531
Christian König758ac172016-05-06 22:14:00 +0200532 if (p->uf_entry.robj)
Christian König91acbeb2015-12-14 16:42:31 +0100533 list_add(&p->uf_entry.tv.head, &p->validated);
534
Christian König2f568db2016-02-23 12:36:59 +0100535 while (1) {
536 struct list_head need_pages;
537 unsigned i;
538
539 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
540 &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200541 if (unlikely(r != 0)) {
jimqu57d7f9b2016-10-20 14:58:04 +0800542 if (r != -ERESTARTSYS)
543 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
Christian König2f568db2016-02-23 12:36:59 +0100544 goto error_free_pages;
Marek Olšákf1037952016-07-30 00:48:39 +0200545 }
Christian König2f568db2016-02-23 12:36:59 +0100546
547 /* Without a BO list we don't have userptr BOs */
548 if (!p->bo_list)
549 break;
550
551 INIT_LIST_HEAD(&need_pages);
552 for (i = p->bo_list->first_userptr;
553 i < p->bo_list->num_entries; ++i) {
Christian Königca666a32017-09-05 14:30:05 +0200554 struct amdgpu_bo *bo;
Christian König2f568db2016-02-23 12:36:59 +0100555
556 e = &p->bo_list->array[i];
Christian Königca666a32017-09-05 14:30:05 +0200557 bo = e->robj;
Christian König2f568db2016-02-23 12:36:59 +0100558
Christian Königca666a32017-09-05 14:30:05 +0200559 if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
Christian König2f568db2016-02-23 12:36:59 +0100560 &e->user_invalidated) && e->user_pages) {
561
562 /* We acquired a page array, but somebody
Alex Xie9f69c0f2017-06-20 16:33:02 -0400563 * invalidated it. Free it and try again
Christian König2f568db2016-02-23 12:36:59 +0100564 */
565 release_pages(e->user_pages,
Christian Königca666a32017-09-05 14:30:05 +0200566 bo->tbo.ttm->num_pages,
Christian König2f568db2016-02-23 12:36:59 +0100567 false);
Michal Hocko20981052017-05-17 14:23:12 +0200568 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100569 e->user_pages = NULL;
570 }
571
Christian Königca666a32017-09-05 14:30:05 +0200572 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
Christian König2f568db2016-02-23 12:36:59 +0100573 !e->user_pages) {
574 list_del(&e->tv.head);
575 list_add(&e->tv.head, &need_pages);
576
577 amdgpu_bo_unreserve(e->robj);
578 }
579 }
580
581 if (list_empty(&need_pages))
582 break;
583
584 /* Unreserve everything again. */
585 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
586
Marek Olšákf1037952016-07-30 00:48:39 +0200587 /* We tried too many times, just abort */
Christian König2f568db2016-02-23 12:36:59 +0100588 if (!--tries) {
589 r = -EDEADLK;
Marek Olšákf1037952016-07-30 00:48:39 +0200590 DRM_ERROR("deadlock in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100591 goto error_free_pages;
592 }
593
Alex Xieeb0f0372017-06-08 14:53:26 -0400594 /* Fill the page arrays for all userptrs. */
Christian König2f568db2016-02-23 12:36:59 +0100595 list_for_each_entry(e, &need_pages, tv.head) {
596 struct ttm_tt *ttm = e->robj->tbo.ttm;
597
Michal Hocko20981052017-05-17 14:23:12 +0200598 e->user_pages = kvmalloc_array(ttm->num_pages,
599 sizeof(struct page*),
600 GFP_KERNEL | __GFP_ZERO);
Christian König2f568db2016-02-23 12:36:59 +0100601 if (!e->user_pages) {
602 r = -ENOMEM;
Marek Olšákf1037952016-07-30 00:48:39 +0200603 DRM_ERROR("calloc failure in %s\n", __func__);
Christian König2f568db2016-02-23 12:36:59 +0100604 goto error_free_pages;
605 }
606
607 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
608 if (r) {
Marek Olšákf1037952016-07-30 00:48:39 +0200609 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
Michal Hocko20981052017-05-17 14:23:12 +0200610 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100611 e->user_pages = NULL;
612 goto error_free_pages;
613 }
614 }
615
616 /* And try again. */
617 list_splice(&need_pages, &p->validated);
618 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619
John Brooks00f06b22017-06-27 22:33:18 -0400620 amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
621 &p->bytes_moved_vis_threshold);
Christian Königf69f90a12015-12-21 19:47:42 +0100622 p->bytes_moved = 0;
John Brooks00f06b22017-06-27 22:33:18 -0400623 p->bytes_moved_vis = 0;
Christian König662bfa62016-09-01 12:13:18 +0200624 p->evictable = list_last_entry(&p->validated,
625 struct amdgpu_bo_list_entry,
626 tv.head);
Christian Königf69f90a12015-12-21 19:47:42 +0100627
Christian Königf7da30d2016-09-28 12:03:04 +0200628 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
629 amdgpu_cs_validate, p);
630 if (r) {
631 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
632 goto error_validate;
633 }
634
Christian Königf69f90a12015-12-21 19:47:42 +0100635 r = amdgpu_cs_list_validate(p, &duplicates);
Marek Olšákf1037952016-07-30 00:48:39 +0200636 if (r) {
637 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
Christian Königa5b75052015-09-03 16:40:39 +0200638 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200639 }
Christian Königa5b75052015-09-03 16:40:39 +0200640
Christian Königf69f90a12015-12-21 19:47:42 +0100641 r = amdgpu_cs_list_validate(p, &p->validated);
Marek Olšákf1037952016-07-30 00:48:39 +0200642 if (r) {
643 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
Christian Königa8480302016-01-05 16:03:39 +0100644 goto error_validate;
Marek Olšákf1037952016-07-30 00:48:39 +0200645 }
Christian Königa8480302016-01-05 16:03:39 +0100646
John Brooks00f06b22017-06-27 22:33:18 -0400647 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
648 p->bytes_moved_vis);
Christian Königa8480302016-01-05 16:03:39 +0100649 if (p->bo_list) {
Christian Königd88bf582016-05-06 17:50:03 +0200650 struct amdgpu_bo *gds = p->bo_list->gds_obj;
651 struct amdgpu_bo *gws = p->bo_list->gws_obj;
652 struct amdgpu_bo *oa = p->bo_list->oa_obj;
Christian Königa8480302016-01-05 16:03:39 +0100653 struct amdgpu_vm *vm = &fpriv->vm;
654 unsigned i;
655
656 for (i = 0; i < p->bo_list->num_entries; i++) {
657 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
658
659 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
660 }
Christian Königd88bf582016-05-06 17:50:03 +0200661
662 if (gds) {
663 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
664 p->job->gds_size = amdgpu_bo_size(gds);
665 }
666 if (gws) {
667 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
668 p->job->gws_size = amdgpu_bo_size(gws);
669 }
670 if (oa) {
671 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
672 p->job->oa_size = amdgpu_bo_size(oa);
673 }
Christian Königa8480302016-01-05 16:03:39 +0100674 }
Christian Königa5b75052015-09-03 16:40:39 +0200675
Christian Königc855e252016-09-05 17:00:57 +0200676 if (!r && p->uf_entry.robj) {
677 struct amdgpu_bo *uf = p->uf_entry.robj;
678
Christian Königbb990bb2016-09-09 16:32:33 +0200679 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200680 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
681 }
Christian Königb5f5acb2016-06-29 13:26:41 +0200682
Christian Königa5b75052015-09-03 16:40:39 +0200683error_validate:
Christian Königb6369222017-08-03 11:44:01 -0400684 if (r)
Christian Königa5b75052015-09-03 16:40:39 +0200685 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
686
Christian König2f568db2016-02-23 12:36:59 +0100687error_free_pages:
688
Christian König2f568db2016-02-23 12:36:59 +0100689 if (p->bo_list) {
690 for (i = p->bo_list->first_userptr;
691 i < p->bo_list->num_entries; ++i) {
692 e = &p->bo_list->array[i];
693
694 if (!e->user_pages)
695 continue;
696
697 release_pages(e->user_pages,
698 e->robj->tbo.ttm->num_pages,
699 false);
Michal Hocko20981052017-05-17 14:23:12 +0200700 kvfree(e->user_pages);
Christian König2f568db2016-02-23 12:36:59 +0100701 }
702 }
703
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 return r;
705}
706
707static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
708{
709 struct amdgpu_bo_list_entry *e;
710 int r;
711
712 list_for_each_entry(e, &p->validated, tv.head) {
713 struct reservation_object *resv = e->robj->tbo.resv;
Andres Rodriguez177ae092017-09-15 20:44:06 -0400714 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp,
715 amdgpu_bo_explicit_sync(e->robj));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716
717 if (r)
718 return r;
719 }
720 return 0;
721}
722
Christian König984810f2015-11-14 21:05:35 +0100723/**
724 * cs_parser_fini() - clean parser states
725 * @parser: parser structure holding parsing context.
726 * @error: error number
727 *
728 * If error is set than unvalidate buffer, otherwise just free memory
729 * used by parsing context.
730 **/
Christian Königb6369222017-08-03 11:44:01 -0400731static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
732 bool backoff)
Chunming Zhou049fc522015-07-21 14:36:51 +0800733{
Christian König984810f2015-11-14 21:05:35 +0100734 unsigned i;
735
Christian König3fe89772017-09-12 14:25:14 -0400736 if (error && backoff)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 ttm_eu_backoff_reservation(&parser->ticket,
738 &parser->validated);
Dave Airlie660e8552017-03-13 22:18:15 +0000739
740 for (i = 0; i < parser->num_post_dep_syncobjs; i++)
741 drm_syncobj_put(parser->post_dep_syncobjs[i]);
742 kfree(parser->post_dep_syncobjs);
743
Chris Wilsonf54d1862016-10-25 13:00:45 +0100744 dma_fence_put(parser->fence);
Christian König7e52a812015-11-04 15:44:39 +0100745
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400746 if (parser->ctx) {
747 mutex_unlock(&parser->ctx->lock);
Christian König3cb485f2015-05-11 15:34:59 +0200748 amdgpu_ctx_put(parser->ctx);
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400749 }
Chunming Zhoua3348bb2015-08-18 16:25:46 +0800750 if (parser->bo_list)
751 amdgpu_bo_list_put(parser->bo_list);
752
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 for (i = 0; i < parser->nchunks; i++)
Michal Hocko20981052017-05-17 14:23:12 +0200754 kvfree(parser->chunks[i].kdata);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400755 kfree(parser->chunks);
Christian König50838c82016-02-03 13:44:52 +0100756 if (parser->job)
757 amdgpu_job_free(parser->job);
Christian König91acbeb2015-12-14 16:42:31 +0100758 amdgpu_bo_unref(&parser->uf_entry.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759}
760
Junwei Zhangb85891b2017-01-16 13:59:01 +0800761static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762{
763 struct amdgpu_device *adev = p->adev;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800764 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
765 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766 struct amdgpu_bo_va *bo_va;
767 struct amdgpu_bo *bo;
768 int i, r;
769
Christian König194d2162016-10-12 15:13:52 +0200770 r = amdgpu_vm_update_directories(adev, vm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400771 if (r)
772 return r;
773
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100774 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400775 if (r)
776 return r;
777
Junwei Zhangb85891b2017-01-16 13:59:01 +0800778 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
779 if (r)
780 return r;
781
782 r = amdgpu_sync_fence(adev, &p->job->sync,
783 fpriv->prt_va->last_pt_update);
784 if (r)
785 return r;
786
Monk Liu24936642017-01-09 15:54:32 +0800787 if (amdgpu_sriov_vf(adev)) {
788 struct dma_fence *f;
Christian König0f4b3c62017-07-31 15:32:40 +0200789
790 bo_va = fpriv->csa_va;
Monk Liu24936642017-01-09 15:54:32 +0800791 BUG_ON(!bo_va);
792 r = amdgpu_vm_bo_update(adev, bo_va, false);
793 if (r)
794 return r;
795
796 f = bo_va->last_pt_update;
797 r = amdgpu_sync_fence(adev, &p->job->sync, f);
798 if (r)
799 return r;
800 }
801
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400802 if (p->bo_list) {
803 for (i = 0; i < p->bo_list->num_entries; i++) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100804 struct dma_fence *f;
Christian König91e1a522015-07-06 22:06:40 +0200805
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400806 /* ignore duplicates */
807 bo = p->bo_list->array[i].robj;
808 if (!bo)
809 continue;
810
811 bo_va = p->bo_list->array[i].bo_va;
812 if (bo_va == NULL)
813 continue;
814
Christian König99e124f2016-08-16 14:43:17 +0200815 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400816 if (r)
817 return r;
818
Chunming Zhoubb1e38a42015-08-03 18:19:38 +0800819 f = bo_va->last_pt_update;
Christian Könige86f9ce2016-02-08 12:13:05 +0100820 r = amdgpu_sync_fence(adev, &p->job->sync, f);
Christian König91e1a522015-07-06 22:06:40 +0200821 if (r)
822 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823 }
Christian Königb495bd32015-09-10 14:00:35 +0200824
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400825 }
826
Christian König4e55eb32017-09-11 16:54:59 +0200827 r = amdgpu_vm_handle_moved(adev, vm);
Christian Königd5884512017-09-08 14:09:41 +0200828 if (r)
829 return r;
830
831 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_update);
832 if (r)
833 return r;
Christian Königb495bd32015-09-10 14:00:35 +0200834
835 if (amdgpu_vm_debug && p->bo_list) {
836 /* Invalidate all BOs to test for userspace bugs */
837 for (i = 0; i < p->bo_list->num_entries; i++) {
838 /* ignore duplicates */
839 bo = p->bo_list->array[i].robj;
840 if (!bo)
841 continue;
842
Christian König3f3333f2017-08-03 14:02:13 +0200843 amdgpu_vm_bo_invalidate(adev, bo, false);
Christian Königb495bd32015-09-10 14:00:35 +0200844 }
845 }
846
847 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848}
849
850static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
Christian Königb07c60c2016-01-31 12:29:04 +0100851 struct amdgpu_cs_parser *p)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852{
Christian Königb07c60c2016-01-31 12:29:04 +0100853 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb07c60c2016-01-31 12:29:04 +0100855 struct amdgpu_ring *ring = p->job->ring;
Andrey Grodzovskyad864d22017-10-10 16:50:16 -0400856 int i, j, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857
Andrey Grodzovskyad864d22017-10-10 16:50:16 -0400858 for (i = 0, j = 0; i < p->nchunks && j < p->job->num_ibs; i++) {
859
860 struct amdgpu_cs_chunk *chunk;
861 struct amdgpu_ib *ib;
862 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
863
864 chunk = &p->chunks[i];
865 ib = &p->job->ibs[j];
866 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
867
868 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
869 continue;
870
871 if (p->job->ring->funcs->parse_cs) {
872 struct amdgpu_bo_va_mapping *m;
873 struct amdgpu_bo *aobj = NULL;
874 uint64_t offset;
875 uint8_t *kptr;
876
877 r = amdgpu_cs_find_mapping(p, chunk_ib->va_start,
878 &aobj, &m);
879 if (r) {
880 DRM_ERROR("IB va_start is invalid\n");
881 return r;
882 }
883
884 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
885 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
886 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
887 return -EINVAL;
888 }
889
890 /* the IB should be reserved at this point */
891 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
892 if (r) {
893 return r;
894 }
895
896 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
897 kptr += chunk_ib->va_start - offset;
898
899 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
900 amdgpu_bo_kunmap(aobj);
901
902 /* Only for UVD/VCE VM emulation */
903 r = amdgpu_ring_parse_cs(ring, p, j);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400904 if (r)
905 return r;
906 }
Andrey Grodzovskyad864d22017-10-10 16:50:16 -0400907 j++;
Christian König45088ef2016-10-05 16:49:19 +0200908 }
909
910 if (p->job->vm) {
Christian König3f3333f2017-08-03 14:02:13 +0200911 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.base.bo);
Christian König9a795882016-06-22 14:25:55 +0200912
Junwei Zhangb85891b2017-01-16 13:59:01 +0800913 r = amdgpu_bo_vm_update_pte(p);
Christian König9a795882016-06-22 14:25:55 +0200914 if (r)
915 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400916 }
917
Christian König9a795882016-06-22 14:25:55 +0200918 return amdgpu_cs_sync_rings(p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400919}
920
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
922 struct amdgpu_cs_parser *parser)
923{
924 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
925 struct amdgpu_vm *vm = &fpriv->vm;
926 int i, j;
Monk Liu9a1b3af2017-03-08 15:51:13 +0800927 int r, ce_preempt = 0, de_preempt = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400928
Christian König50838c82016-02-03 13:44:52 +0100929 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930 struct amdgpu_cs_chunk *chunk;
931 struct amdgpu_ib *ib;
932 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934
935 chunk = &parser->chunks[i];
Christian König50838c82016-02-03 13:44:52 +0100936 ib = &parser->job->ibs[j];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
938
939 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
940 continue;
941
Monk Liu65333e42017-03-27 15:14:53 +0800942 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
Harry Wentlande51a3222017-03-28 11:29:53 -0400943 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
Monk Liu65333e42017-03-27 15:14:53 +0800944 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
945 ce_preempt++;
946 else
947 de_preempt++;
Harry Wentlande51a3222017-03-28 11:29:53 -0400948 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800949
Monk Liu65333e42017-03-27 15:14:53 +0800950 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
951 if (ce_preempt > 1 || de_preempt > 1)
Monk Liue9d672b2017-03-15 12:18:57 +0800952 return -EINVAL;
Monk Liu65333e42017-03-27 15:14:53 +0800953 }
Monk Liu9a1b3af2017-03-08 15:51:13 +0800954
Andres Rodriguezeffd9242017-02-16 00:47:32 -0500955 r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
956 chunk_ib->ip_instance, chunk_ib->ring, &ring);
Marek Olšák3ccec532015-06-02 17:44:49 +0200957 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400958 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400959
Monk Liu2a9ceb82017-03-28 11:00:03 +0800960 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
Monk Liu753ad492016-08-26 13:28:28 +0800961 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
962 if (!parser->ctx->preamble_presented) {
963 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
964 parser->ctx->preamble_presented = true;
965 }
966 }
967
Christian Königb07c60c2016-01-31 12:29:04 +0100968 if (parser->job->ring && parser->job->ring != ring)
969 return -EINVAL;
970
971 parser->job->ring = ring;
972
Andrey Grodzovskyad864d22017-10-10 16:50:16 -0400973 r = amdgpu_ib_get(adev, vm,
974 ring->funcs->parse_cs ? chunk_ib->ib_bytes : 0,
975 ib);
976 if (r) {
977 DRM_ERROR("Failed to get ib !\n");
978 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980
Christian König45088ef2016-10-05 16:49:19 +0200981 ib->gpu_addr = chunk_ib->va_start;
Marek Olšák3ccec532015-06-02 17:44:49 +0200982 ib->length_dw = chunk_ib->ib_bytes / 4;
Jammy Zhoude807f82015-05-11 23:41:41 +0800983 ib->flags = chunk_ib->flags;
Andrey Grodzovskyad864d22017-10-10 16:50:16 -0400984
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985 j++;
986 }
987
Christian König758ac172016-05-06 22:14:00 +0200988 /* UVD & VCE fw doesn't support user fences */
Christian Königb5f5acb2016-06-29 13:26:41 +0200989 if (parser->job->uf_addr && (
Christian König21cd9422016-10-05 15:36:39 +0200990 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
991 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
Christian König758ac172016-05-06 22:14:00 +0200992 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400994 return amdgpu_ctx_wait_prev_fence(parser->ctx, parser->job->ring->idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995}
996
Dave Airlie6f0308e2017-03-09 03:45:52 +0000997static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
998 struct amdgpu_cs_chunk *chunk)
999{
1000 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1001 unsigned num_deps;
1002 int i, r;
1003 struct drm_amdgpu_cs_chunk_dep *deps;
1004
1005 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1006 num_deps = chunk->length_dw * 4 /
1007 sizeof(struct drm_amdgpu_cs_chunk_dep);
1008
1009 for (i = 0; i < num_deps; ++i) {
1010 struct amdgpu_ring *ring;
1011 struct amdgpu_ctx *ctx;
1012 struct dma_fence *fence;
1013
1014 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
1015 if (ctx == NULL)
1016 return -EINVAL;
1017
1018 r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
1019 deps[i].ip_type,
1020 deps[i].ip_instance,
1021 deps[i].ring, &ring);
1022 if (r) {
1023 amdgpu_ctx_put(ctx);
1024 return r;
1025 }
1026
1027 fence = amdgpu_ctx_get_fence(ctx, ring,
1028 deps[i].handle);
1029 if (IS_ERR(fence)) {
1030 r = PTR_ERR(fence);
1031 amdgpu_ctx_put(ctx);
1032 return r;
1033 } else if (fence) {
1034 r = amdgpu_sync_fence(p->adev, &p->job->sync,
1035 fence);
1036 dma_fence_put(fence);
1037 amdgpu_ctx_put(ctx);
1038 if (r)
1039 return r;
1040 }
1041 }
1042 return 0;
1043}
1044
Dave Airlie660e8552017-03-13 22:18:15 +00001045static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
1046 uint32_t handle)
1047{
1048 int r;
1049 struct dma_fence *fence;
Jason Ekstrandafaf5922017-08-25 10:52:19 -07001050 r = drm_syncobj_find_fence(p->filp, handle, &fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001051 if (r)
1052 return r;
1053
1054 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence);
1055 dma_fence_put(fence);
1056
1057 return r;
1058}
1059
1060static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
1061 struct amdgpu_cs_chunk *chunk)
1062{
1063 unsigned num_deps;
1064 int i, r;
1065 struct drm_amdgpu_cs_chunk_sem *deps;
1066
1067 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1068 num_deps = chunk->length_dw * 4 /
1069 sizeof(struct drm_amdgpu_cs_chunk_sem);
1070
1071 for (i = 0; i < num_deps; ++i) {
1072 r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
1073 if (r)
1074 return r;
1075 }
1076 return 0;
1077}
1078
1079static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1080 struct amdgpu_cs_chunk *chunk)
1081{
1082 unsigned num_deps;
1083 int i;
1084 struct drm_amdgpu_cs_chunk_sem *deps;
1085 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1086 num_deps = chunk->length_dw * 4 /
1087 sizeof(struct drm_amdgpu_cs_chunk_sem);
1088
1089 p->post_dep_syncobjs = kmalloc_array(num_deps,
1090 sizeof(struct drm_syncobj *),
1091 GFP_KERNEL);
1092 p->num_post_dep_syncobjs = 0;
1093
Christophe JAILLET06f10a52017-08-23 07:52:36 +02001094 if (!p->post_dep_syncobjs)
1095 return -ENOMEM;
1096
Dave Airlie660e8552017-03-13 22:18:15 +00001097 for (i = 0; i < num_deps; ++i) {
1098 p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
1099 if (!p->post_dep_syncobjs[i])
1100 return -EINVAL;
1101 p->num_post_dep_syncobjs++;
1102 }
1103 return 0;
1104}
1105
Christian König2b48d322015-06-19 17:31:29 +02001106static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1107 struct amdgpu_cs_parser *p)
1108{
Dave Airlie6f0308e2017-03-09 03:45:52 +00001109 int i, r;
Christian König2b48d322015-06-19 17:31:29 +02001110
Christian König2b48d322015-06-19 17:31:29 +02001111 for (i = 0; i < p->nchunks; ++i) {
Christian König2b48d322015-06-19 17:31:29 +02001112 struct amdgpu_cs_chunk *chunk;
Christian König2b48d322015-06-19 17:31:29 +02001113
1114 chunk = &p->chunks[i];
1115
Dave Airlie6f0308e2017-03-09 03:45:52 +00001116 if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
1117 r = amdgpu_cs_process_fence_dep(p, chunk);
1118 if (r)
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001119 return r;
Dave Airlie660e8552017-03-13 22:18:15 +00001120 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
1121 r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1122 if (r)
1123 return r;
1124 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
1125 r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1126 if (r)
1127 return r;
Christian König2b48d322015-06-19 17:31:29 +02001128 }
1129 }
1130
1131 return 0;
1132}
1133
Dave Airlie660e8552017-03-13 22:18:15 +00001134static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1135{
1136 int i;
1137
Chris Wilson00fc2c22017-07-05 21:12:44 +01001138 for (i = 0; i < p->num_post_dep_syncobjs; ++i)
1139 drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
Dave Airlie660e8552017-03-13 22:18:15 +00001140}
1141
Christian Königcd75dc62016-01-31 11:30:55 +01001142static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1143 union drm_amdgpu_cs *cs)
1144{
Christian Königb07c60c2016-01-31 12:29:04 +01001145 struct amdgpu_ring *ring = p->job->ring;
Christian König92f25092016-05-06 15:57:42 +02001146 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
Christian Königcd75dc62016-01-31 11:30:55 +01001147 struct amdgpu_job *job;
Christian König3fe89772017-09-12 14:25:14 -04001148 unsigned i;
Monk Liueb01abc2017-09-15 13:40:31 +08001149 uint64_t seq;
1150
Monk Liue6869412016-03-07 12:49:55 +08001151 int r;
Christian Königcd75dc62016-01-31 11:30:55 +01001152
Christian König3fe89772017-09-12 14:25:14 -04001153 amdgpu_mn_lock(p->mn);
1154 if (p->bo_list) {
1155 for (i = p->bo_list->first_userptr;
1156 i < p->bo_list->num_entries; ++i) {
1157 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
1158
1159 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
1160 amdgpu_mn_unlock(p->mn);
1161 return -ERESTARTSYS;
1162 }
1163 }
1164 }
1165
Christian König50838c82016-02-03 13:44:52 +01001166 job = p->job;
1167 p->job = NULL;
Christian Königcd75dc62016-01-31 11:30:55 +01001168
Christian König595a9cd2016-06-30 10:52:03 +02001169 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
Monk Liue6869412016-03-07 12:49:55 +08001170 if (r) {
Christian Königd71518b2016-02-01 12:20:25 +01001171 amdgpu_job_free(job);
Christian König3fe89772017-09-12 14:25:14 -04001172 amdgpu_mn_unlock(p->mn);
Monk Liue6869412016-03-07 12:49:55 +08001173 return r;
Christian Königcd75dc62016-01-31 11:30:55 +01001174 }
1175
Monk Liue6869412016-03-07 12:49:55 +08001176 job->owner = p->filp;
Monk Liu3aecd242016-08-25 15:40:48 +08001177 job->fence_ctx = entity->fence_context;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001178 p->fence = dma_fence_get(&job->base.s_fence->finished);
Dave Airlie660e8552017-03-13 22:18:15 +00001179
Monk Liueb01abc2017-09-15 13:40:31 +08001180 r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
1181 if (r) {
1182 dma_fence_put(p->fence);
1183 dma_fence_put(&job->base.s_fence->finished);
1184 amdgpu_job_free(job);
1185 amdgpu_mn_unlock(p->mn);
1186 return r;
1187 }
1188
Dave Airlie660e8552017-03-13 22:18:15 +00001189 amdgpu_cs_post_dependencies(p);
1190
Monk Liueb01abc2017-09-15 13:40:31 +08001191 cs->out.handle = seq;
1192 job->uf_sequence = seq;
1193
Christian Königa5fb4ec2016-06-29 15:10:31 +02001194 amdgpu_job_free_resources(job);
Andres Rodriguezb2ff0e82017-02-20 17:53:19 -05001195 amdgpu_ring_priority_get(job->ring,
1196 amd_sched_get_job_priority(&job->base));
Christian Königcd75dc62016-01-31 11:30:55 +01001197
1198 trace_amdgpu_cs_ioctl(job);
1199 amd_sched_entity_push_job(&job->base);
Christian König3fe89772017-09-12 14:25:14 -04001200
1201 ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1202 amdgpu_mn_unlock(p->mn);
1203
Christian Königcd75dc62016-01-31 11:30:55 +01001204 return 0;
1205}
1206
Chunming Zhou049fc522015-07-21 14:36:51 +08001207int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1208{
1209 struct amdgpu_device *adev = dev->dev_private;
1210 union drm_amdgpu_cs *cs = data;
Christian König7e52a812015-11-04 15:44:39 +01001211 struct amdgpu_cs_parser parser = {};
Christian König26a69802015-08-18 21:09:33 +02001212 bool reserved_buffers = false;
1213 int i, r;
Chunming Zhou049fc522015-07-21 14:36:51 +08001214
Christian König0c418f12015-09-01 15:13:53 +02001215 if (!adev->accel_working)
Chunming Zhou049fc522015-07-21 14:36:51 +08001216 return -EBUSY;
Chunming Zhou049fc522015-07-21 14:36:51 +08001217
Christian König7e52a812015-11-04 15:44:39 +01001218 parser.adev = adev;
1219 parser.filp = filp;
1220
1221 r = amdgpu_cs_parser_init(&parser, data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 if (r) {
Chunming Zhou049fc522015-07-21 14:36:51 +08001223 DRM_ERROR("Failed to initialize parser !\n");
Huang Ruia414cd72016-10-30 23:05:47 +08001224 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 }
Huang Ruia414cd72016-10-30 23:05:47 +08001226
Andrey Grodzovskyad864d22017-10-10 16:50:16 -04001227 r = amdgpu_cs_ib_fill(adev, &parser);
1228 if (r)
1229 goto out;
1230
Christian König2a7d9bd2015-12-18 20:33:52 +01001231 r = amdgpu_cs_parser_bos(&parser, data);
Huang Ruia414cd72016-10-30 23:05:47 +08001232 if (r) {
1233 if (r == -ENOMEM)
1234 DRM_ERROR("Not enough memory for command submission!\n");
1235 else if (r != -ERESTARTSYS)
1236 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1237 goto out;
Christian König26a69802015-08-18 21:09:33 +02001238 }
1239
Huang Ruia414cd72016-10-30 23:05:47 +08001240 reserved_buffers = true;
Christian König26a69802015-08-18 21:09:33 +02001241
Huang Ruia414cd72016-10-30 23:05:47 +08001242 r = amdgpu_cs_dependencies(adev, &parser);
1243 if (r) {
1244 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1245 goto out;
1246 }
1247
Christian König50838c82016-02-03 13:44:52 +01001248 for (i = 0; i < parser.job->num_ibs; i++)
Christian König7e52a812015-11-04 15:44:39 +01001249 trace_amdgpu_cs(&parser, i);
Christian König26a69802015-08-18 21:09:33 +02001250
Christian König7e52a812015-11-04 15:44:39 +01001251 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
Chunming Zhou4fe63112015-08-18 16:12:15 +08001252 if (r)
1253 goto out;
1254
Christian König4acabfe2016-01-31 11:32:04 +01001255 r = amdgpu_cs_submit(&parser, cs);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257out:
Christian König7e52a812015-11-04 15:44:39 +01001258 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 return r;
1260}
1261
1262/**
1263 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1264 *
1265 * @dev: drm device
1266 * @data: data from userspace
1267 * @filp: file private
1268 *
1269 * Wait for the command submission identified by handle to finish.
1270 */
1271int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1272 struct drm_file *filp)
1273{
1274 union drm_amdgpu_wait_cs *wait = data;
1275 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
Christian König03507c42015-06-19 17:00:19 +02001277 struct amdgpu_ring *ring = NULL;
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001278 struct amdgpu_ctx *ctx;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001279 struct dma_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 long r;
1281
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001282 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1283 if (ctx == NULL)
1284 return -EINVAL;
Chunming Zhou4b559c92015-07-21 15:53:04 +08001285
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001286 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
1287 wait->in.ip_type, wait->in.ip_instance,
1288 wait->in.ring, &ring);
1289 if (r) {
1290 amdgpu_ctx_put(ctx);
1291 return r;
1292 }
1293
Chunming Zhou4b559c92015-07-21 15:53:04 +08001294 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1295 if (IS_ERR(fence))
1296 r = PTR_ERR(fence);
1297 else if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001298 r = dma_fence_wait_timeout(fence, true, timeout);
Christian König7a0a48d2017-10-09 15:51:10 +02001299 if (r > 0 && fence->error)
1300 r = fence->error;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001301 dma_fence_put(fence);
Chunming Zhou4b559c92015-07-21 15:53:04 +08001302 } else
Christian König21c16bf2015-07-07 17:24:49 +02001303 r = 1;
1304
Jammy Zhou66b3cf22015-05-08 17:29:40 +08001305 amdgpu_ctx_put(ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 if (r < 0)
1307 return r;
1308
1309 memset(wait, 0, sizeof(*wait));
1310 wait->out.status = (r == 0);
1311
1312 return 0;
1313}
1314
1315/**
Junwei Zhangeef18a82016-11-04 16:16:10 -04001316 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1317 *
1318 * @adev: amdgpu device
1319 * @filp: file private
1320 * @user: drm_amdgpu_fence copied from user space
1321 */
1322static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1323 struct drm_file *filp,
1324 struct drm_amdgpu_fence *user)
1325{
1326 struct amdgpu_ring *ring;
1327 struct amdgpu_ctx *ctx;
1328 struct dma_fence *fence;
1329 int r;
1330
Junwei Zhangeef18a82016-11-04 16:16:10 -04001331 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1332 if (ctx == NULL)
1333 return ERR_PTR(-EINVAL);
1334
Andres Rodriguezeffd9242017-02-16 00:47:32 -05001335 r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
1336 user->ip_instance, user->ring, &ring);
1337 if (r) {
1338 amdgpu_ctx_put(ctx);
1339 return ERR_PTR(r);
1340 }
1341
Junwei Zhangeef18a82016-11-04 16:16:10 -04001342 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1343 amdgpu_ctx_put(ctx);
1344
1345 return fence;
1346}
1347
Marek Olšák7ca24cf2017-09-12 22:42:14 +02001348int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1349 struct drm_file *filp)
1350{
1351 struct amdgpu_device *adev = dev->dev_private;
Marek Olšák7ca24cf2017-09-12 22:42:14 +02001352 union drm_amdgpu_fence_to_handle *info = data;
1353 struct dma_fence *fence;
1354 struct drm_syncobj *syncobj;
1355 struct sync_file *sync_file;
1356 int fd, r;
1357
Marek Olšák7ca24cf2017-09-12 22:42:14 +02001358 fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1359 if (IS_ERR(fence))
1360 return PTR_ERR(fence);
1361
1362 switch (info->in.what) {
1363 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1364 r = drm_syncobj_create(&syncobj, 0, fence);
1365 dma_fence_put(fence);
1366 if (r)
1367 return r;
1368 r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1369 drm_syncobj_put(syncobj);
1370 return r;
1371
1372 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1373 r = drm_syncobj_create(&syncobj, 0, fence);
1374 dma_fence_put(fence);
1375 if (r)
1376 return r;
1377 r = drm_syncobj_get_fd(syncobj, (int*)&info->out.handle);
1378 drm_syncobj_put(syncobj);
1379 return r;
1380
1381 case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1382 fd = get_unused_fd_flags(O_CLOEXEC);
1383 if (fd < 0) {
1384 dma_fence_put(fence);
1385 return fd;
1386 }
1387
1388 sync_file = sync_file_create(fence);
1389 dma_fence_put(fence);
1390 if (!sync_file) {
1391 put_unused_fd(fd);
1392 return -ENOMEM;
1393 }
1394
1395 fd_install(fd, sync_file->file);
1396 info->out.handle = fd;
1397 return 0;
1398
1399 default:
1400 return -EINVAL;
1401 }
1402}
1403
Junwei Zhangeef18a82016-11-04 16:16:10 -04001404/**
1405 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1406 *
1407 * @adev: amdgpu device
1408 * @filp: file private
1409 * @wait: wait parameters
1410 * @fences: array of drm_amdgpu_fence
1411 */
1412static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1413 struct drm_file *filp,
1414 union drm_amdgpu_wait_fences *wait,
1415 struct drm_amdgpu_fence *fences)
1416{
1417 uint32_t fence_count = wait->in.fence_count;
1418 unsigned int i;
1419 long r = 1;
1420
1421 for (i = 0; i < fence_count; i++) {
1422 struct dma_fence *fence;
1423 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1424
1425 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1426 if (IS_ERR(fence))
1427 return PTR_ERR(fence);
1428 else if (!fence)
1429 continue;
1430
1431 r = dma_fence_wait_timeout(fence, true, timeout);
Chunming Zhou32df87d2017-04-07 17:05:45 +08001432 dma_fence_put(fence);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001433 if (r < 0)
1434 return r;
1435
1436 if (r == 0)
1437 break;
Christian König7a0a48d2017-10-09 15:51:10 +02001438
1439 if (fence->error)
1440 return fence->error;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001441 }
1442
1443 memset(wait, 0, sizeof(*wait));
1444 wait->out.status = (r > 0);
1445
1446 return 0;
1447}
1448
1449/**
1450 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1451 *
1452 * @adev: amdgpu device
1453 * @filp: file private
1454 * @wait: wait parameters
1455 * @fences: array of drm_amdgpu_fence
1456 */
1457static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1458 struct drm_file *filp,
1459 union drm_amdgpu_wait_fences *wait,
1460 struct drm_amdgpu_fence *fences)
1461{
1462 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1463 uint32_t fence_count = wait->in.fence_count;
1464 uint32_t first = ~0;
1465 struct dma_fence **array;
1466 unsigned int i;
1467 long r;
1468
1469 /* Prepare the fence array */
1470 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1471
1472 if (array == NULL)
1473 return -ENOMEM;
1474
1475 for (i = 0; i < fence_count; i++) {
1476 struct dma_fence *fence;
1477
1478 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1479 if (IS_ERR(fence)) {
1480 r = PTR_ERR(fence);
1481 goto err_free_fence_array;
1482 } else if (fence) {
1483 array[i] = fence;
1484 } else { /* NULL, the fence has been already signaled */
1485 r = 1;
Monk Liua2138ea2017-08-11 17:49:48 +08001486 first = i;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001487 goto out;
1488 }
1489 }
1490
1491 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1492 &first);
1493 if (r < 0)
1494 goto err_free_fence_array;
1495
1496out:
1497 memset(wait, 0, sizeof(*wait));
1498 wait->out.status = (r > 0);
1499 wait->out.first_signaled = first;
1500 /* set return value 0 to indicate success */
Christian König7a0a48d2017-10-09 15:51:10 +02001501 r = array[first]->error;
Junwei Zhangeef18a82016-11-04 16:16:10 -04001502
1503err_free_fence_array:
1504 for (i = 0; i < fence_count; i++)
1505 dma_fence_put(array[i]);
1506 kfree(array);
1507
1508 return r;
1509}
1510
1511/**
1512 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1513 *
1514 * @dev: drm device
1515 * @data: data from userspace
1516 * @filp: file private
1517 */
1518int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1519 struct drm_file *filp)
1520{
1521 struct amdgpu_device *adev = dev->dev_private;
1522 union drm_amdgpu_wait_fences *wait = data;
1523 uint32_t fence_count = wait->in.fence_count;
1524 struct drm_amdgpu_fence *fences_user;
1525 struct drm_amdgpu_fence *fences;
1526 int r;
1527
1528 /* Get the fences from userspace */
1529 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1530 GFP_KERNEL);
1531 if (fences == NULL)
1532 return -ENOMEM;
1533
Christian König7ecc2452017-07-26 17:02:52 +02001534 fences_user = u64_to_user_ptr(wait->in.fences);
Junwei Zhangeef18a82016-11-04 16:16:10 -04001535 if (copy_from_user(fences, fences_user,
1536 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1537 r = -EFAULT;
1538 goto err_free_fences;
1539 }
1540
1541 if (wait->in.wait_all)
1542 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1543 else
1544 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1545
1546err_free_fences:
1547 kfree(fences);
1548
1549 return r;
1550}
1551
1552/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 * amdgpu_cs_find_bo_va - find bo_va for VM address
1554 *
1555 * @parser: command submission parser context
1556 * @addr: VM address
1557 * @bo: resulting BO of the mapping found
1558 *
1559 * Search the buffer objects in the command submission context for a certain
1560 * virtual memory address. Returns allocation structure when found, NULL
1561 * otherwise.
1562 */
Christian König9cca0b82017-09-06 16:15:28 +02001563int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1564 uint64_t addr, struct amdgpu_bo **bo,
1565 struct amdgpu_bo_va_mapping **map)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566{
Christian Königaebc5e62017-09-06 16:55:16 +02001567 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1568 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 struct amdgpu_bo_va_mapping *mapping;
Christian König9cca0b82017-09-06 16:15:28 +02001570 int r;
Christian König15486fd22015-12-22 16:06:12 +01001571
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572 addr /= AMDGPU_GPU_PAGE_SIZE;
1573
Christian Königaebc5e62017-09-06 16:55:16 +02001574 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1575 if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1576 return -EINVAL;
Christian König15486fd22015-12-22 16:06:12 +01001577
Christian Königaebc5e62017-09-06 16:55:16 +02001578 *bo = mapping->bo_va->base.bo;
1579 *map = mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580
Christian Königaebc5e62017-09-06 16:55:16 +02001581 /* Double check that the BO is reserved by this CS */
1582 if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
1583 return -EINVAL;
Christian König7fc11952015-07-30 11:53:42 +02001584
Christian König9cca0b82017-09-06 16:15:28 +02001585 r = amdgpu_ttm_bind(&(*bo)->tbo, &(*bo)->tbo.mem);
1586 if (unlikely(r))
1587 return r;
Christian Königc855e252016-09-05 17:00:57 +02001588
Christian König9cca0b82017-09-06 16:15:28 +02001589 if ((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
Christian Königc855e252016-09-05 17:00:57 +02001590 return 0;
1591
Christian König9cca0b82017-09-06 16:15:28 +02001592 (*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1593 amdgpu_ttm_placement_from_domain(*bo, (*bo)->allowed_domains);
1594 return ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, false, false);
Christian Königc855e252016-09-05 17:00:57 +02001595}