xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_cs.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2008 Jerome Glisse.
3b843c749SSergey Zigachev  * All Rights Reserved.
4b843c749SSergey Zigachev  *
5b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
6b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
7b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
8b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
10b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
11b843c749SSergey Zigachev  *
12b843c749SSergey Zigachev  * The above copyright notice and this permission notice (including the next
13b843c749SSergey Zigachev  * paragraph) shall be included in all copies or substantial portions of the
14b843c749SSergey Zigachev  * Software.
15b843c749SSergey Zigachev  *
16b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19b843c749SSergey Zigachev  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22b843c749SSergey Zigachev  * DEALINGS IN THE SOFTWARE.
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  * Authors:
25b843c749SSergey Zigachev  *    Jerome Glisse <glisse@freedesktop.org>
26b843c749SSergey Zigachev  */
27b843c749SSergey Zigachev #include <linux/pagemap.h>
28b843c749SSergey Zigachev #include <linux/sync_file.h>
29b843c749SSergey Zigachev #include <drm/drmP.h>
30b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
31b843c749SSergey Zigachev #include <drm/drm_syncobj.h>
32b843c749SSergey Zigachev #include "amdgpu.h"
33b843c749SSergey Zigachev #include "amdgpu_trace.h"
34b843c749SSergey Zigachev #include "amdgpu_gmc.h"
35b843c749SSergey Zigachev 
amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser * p,struct drm_amdgpu_cs_chunk_fence * data,uint32_t * offset)36b843c749SSergey Zigachev static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
37b843c749SSergey Zigachev 				      struct drm_amdgpu_cs_chunk_fence *data,
38b843c749SSergey Zigachev 				      uint32_t *offset)
39b843c749SSergey Zigachev {
40b843c749SSergey Zigachev 	struct drm_gem_object *gobj;
41b843c749SSergey Zigachev 	unsigned long size;
42b843c749SSergey Zigachev 	int r;
43b843c749SSergey Zigachev 
44b843c749SSergey Zigachev 	gobj = drm_gem_object_lookup(p->filp, data->handle);
45b843c749SSergey Zigachev 	if (gobj == NULL)
46b843c749SSergey Zigachev 		return -EINVAL;
47b843c749SSergey Zigachev 
48b843c749SSergey Zigachev 	p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
49b843c749SSergey Zigachev 	p->uf_entry.priority = 0;
50b843c749SSergey Zigachev 	p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
51b843c749SSergey Zigachev 	p->uf_entry.tv.shared = true;
52b843c749SSergey Zigachev 	p->uf_entry.user_pages = NULL;
53b843c749SSergey Zigachev 
54b843c749SSergey Zigachev 	drm_gem_object_put_unlocked(gobj);
55b843c749SSergey Zigachev 
56b843c749SSergey Zigachev 	size = amdgpu_bo_size(p->uf_entry.robj);
57b843c749SSergey Zigachev 	if (size != PAGE_SIZE || (data->offset + 8) > size) {
58b843c749SSergey Zigachev 		r = -EINVAL;
59b843c749SSergey Zigachev 		goto error_unref;
60b843c749SSergey Zigachev 	}
61b843c749SSergey Zigachev 
62b843c749SSergey Zigachev 	if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
63b843c749SSergey Zigachev 		r = -EINVAL;
64b843c749SSergey Zigachev 		goto error_unref;
65b843c749SSergey Zigachev 	}
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev 	*offset = data->offset;
68b843c749SSergey Zigachev 
69b843c749SSergey Zigachev 	return 0;
70b843c749SSergey Zigachev 
71b843c749SSergey Zigachev error_unref:
72b843c749SSergey Zigachev 	amdgpu_bo_unref(&p->uf_entry.robj);
73b843c749SSergey Zigachev 	return r;
74b843c749SSergey Zigachev }
75b843c749SSergey Zigachev 
amdgpu_cs_bo_handles_chunk(struct amdgpu_cs_parser * p,struct drm_amdgpu_bo_list_in * data)76b843c749SSergey Zigachev static int amdgpu_cs_bo_handles_chunk(struct amdgpu_cs_parser *p,
77b843c749SSergey Zigachev 				      struct drm_amdgpu_bo_list_in *data)
78b843c749SSergey Zigachev {
79b843c749SSergey Zigachev 	int r;
80b843c749SSergey Zigachev 	struct drm_amdgpu_bo_list_entry *info = NULL;
81b843c749SSergey Zigachev 
82b843c749SSergey Zigachev 	r = amdgpu_bo_create_list_entry_array(data, &info);
83b843c749SSergey Zigachev 	if (r)
84b843c749SSergey Zigachev 		return r;
85b843c749SSergey Zigachev 
86b843c749SSergey Zigachev 	r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
87b843c749SSergey Zigachev 				  &p->bo_list);
88b843c749SSergey Zigachev 	if (r)
89b843c749SSergey Zigachev 		goto error_free;
90b843c749SSergey Zigachev 
91b843c749SSergey Zigachev 	kvfree(info);
92b843c749SSergey Zigachev 	return 0;
93b843c749SSergey Zigachev 
94b843c749SSergey Zigachev error_free:
95b843c749SSergey Zigachev 	if (info)
96b843c749SSergey Zigachev 		kvfree(info);
97b843c749SSergey Zigachev 
98b843c749SSergey Zigachev 	return r;
99b843c749SSergey Zigachev }
100b843c749SSergey Zigachev 
amdgpu_cs_parser_init(struct amdgpu_cs_parser * p,union drm_amdgpu_cs * cs)101b843c749SSergey Zigachev static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs)
102b843c749SSergey Zigachev {
103b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
104b843c749SSergey Zigachev 	struct amdgpu_vm *vm = &fpriv->vm;
105b843c749SSergey Zigachev 	uint64_t *chunk_array_user;
106b843c749SSergey Zigachev 	uint64_t *chunk_array;
107b843c749SSergey Zigachev 	unsigned size, num_ibs = 0;
108b843c749SSergey Zigachev 	uint32_t uf_offset = 0;
109b843c749SSergey Zigachev 	int i;
110b843c749SSergey Zigachev 	int ret;
111b843c749SSergey Zigachev 
112b843c749SSergey Zigachev 	if (cs->in.num_chunks == 0)
113b843c749SSergey Zigachev 		return 0;
114b843c749SSergey Zigachev 
115b843c749SSergey Zigachev 	chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
116b843c749SSergey Zigachev 	if (!chunk_array)
117b843c749SSergey Zigachev 		return -ENOMEM;
118b843c749SSergey Zigachev 
119b843c749SSergey Zigachev 	p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
120b843c749SSergey Zigachev 	if (!p->ctx) {
121b843c749SSergey Zigachev 		ret = -EINVAL;
122b843c749SSergey Zigachev 		goto free_chunk;
123b843c749SSergey Zigachev 	}
124b843c749SSergey Zigachev 
125b843c749SSergey Zigachev 	mutex_lock(&p->ctx->lock);
126b843c749SSergey Zigachev 
127b843c749SSergey Zigachev 	/* skip guilty context job */
128b843c749SSergey Zigachev 	if (atomic_read(&p->ctx->guilty) == 1) {
129b843c749SSergey Zigachev 		ret = -ECANCELED;
130b843c749SSergey Zigachev 		goto free_chunk;
131b843c749SSergey Zigachev 	}
132b843c749SSergey Zigachev 
133b843c749SSergey Zigachev 	/* get chunks */
134b843c749SSergey Zigachev 	chunk_array_user = u64_to_user_ptr(cs->in.chunks);
135b843c749SSergey Zigachev 	if (copy_from_user(chunk_array, chunk_array_user,
136b843c749SSergey Zigachev 			   sizeof(uint64_t)*cs->in.num_chunks)) {
137b843c749SSergey Zigachev 		ret = -EFAULT;
138b843c749SSergey Zigachev 		goto free_chunk;
139b843c749SSergey Zigachev 	}
140b843c749SSergey Zigachev 
141b843c749SSergey Zigachev 	p->nchunks = cs->in.num_chunks;
142b843c749SSergey Zigachev 	p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
143b843c749SSergey Zigachev 			    GFP_KERNEL);
144b843c749SSergey Zigachev 	if (!p->chunks) {
145b843c749SSergey Zigachev 		ret = -ENOMEM;
146b843c749SSergey Zigachev 		goto free_chunk;
147b843c749SSergey Zigachev 	}
148b843c749SSergey Zigachev 
149b843c749SSergey Zigachev 	for (i = 0; i < p->nchunks; i++) {
150b843c749SSergey Zigachev 		struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
151b843c749SSergey Zigachev 		struct drm_amdgpu_cs_chunk user_chunk;
152b843c749SSergey Zigachev 		uint32_t __user *cdata;
153b843c749SSergey Zigachev 
154b843c749SSergey Zigachev 		chunk_ptr = u64_to_user_ptr(chunk_array[i]);
155b843c749SSergey Zigachev 		if (copy_from_user(&user_chunk, chunk_ptr,
156b843c749SSergey Zigachev 				       sizeof(struct drm_amdgpu_cs_chunk))) {
157b843c749SSergey Zigachev 			ret = -EFAULT;
158b843c749SSergey Zigachev 			i--;
159b843c749SSergey Zigachev 			goto free_partial_kdata;
160b843c749SSergey Zigachev 		}
161b843c749SSergey Zigachev 		p->chunks[i].chunk_id = user_chunk.chunk_id;
162b843c749SSergey Zigachev 		p->chunks[i].length_dw = user_chunk.length_dw;
163b843c749SSergey Zigachev 
164b843c749SSergey Zigachev 		size = p->chunks[i].length_dw;
165b843c749SSergey Zigachev 		cdata = u64_to_user_ptr(user_chunk.chunk_data);
166b843c749SSergey Zigachev 
167b843c749SSergey Zigachev 		p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
168b843c749SSergey Zigachev 		if (p->chunks[i].kdata == NULL) {
169b843c749SSergey Zigachev 			ret = -ENOMEM;
170b843c749SSergey Zigachev 			i--;
171b843c749SSergey Zigachev 			goto free_partial_kdata;
172b843c749SSergey Zigachev 		}
173b843c749SSergey Zigachev 		size *= sizeof(uint32_t);
174b843c749SSergey Zigachev 		if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
175b843c749SSergey Zigachev 			ret = -EFAULT;
176b843c749SSergey Zigachev 			goto free_partial_kdata;
177b843c749SSergey Zigachev 		}
178b843c749SSergey Zigachev 
179b843c749SSergey Zigachev 		switch (p->chunks[i].chunk_id) {
180b843c749SSergey Zigachev 		case AMDGPU_CHUNK_ID_IB:
181b843c749SSergey Zigachev 			++num_ibs;
182b843c749SSergey Zigachev 			break;
183b843c749SSergey Zigachev 
184b843c749SSergey Zigachev 		case AMDGPU_CHUNK_ID_FENCE:
185b843c749SSergey Zigachev 			size = sizeof(struct drm_amdgpu_cs_chunk_fence);
186b843c749SSergey Zigachev 			if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
187b843c749SSergey Zigachev 				ret = -EINVAL;
188b843c749SSergey Zigachev 				goto free_partial_kdata;
189b843c749SSergey Zigachev 			}
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev 			ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
192b843c749SSergey Zigachev 							 &uf_offset);
193b843c749SSergey Zigachev 			if (ret)
194b843c749SSergey Zigachev 				goto free_partial_kdata;
195b843c749SSergey Zigachev 
196b843c749SSergey Zigachev 			break;
197b843c749SSergey Zigachev 
198b843c749SSergey Zigachev 		case AMDGPU_CHUNK_ID_BO_HANDLES:
199b843c749SSergey Zigachev 			size = sizeof(struct drm_amdgpu_bo_list_in);
200b843c749SSergey Zigachev 			if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
201b843c749SSergey Zigachev 				ret = -EINVAL;
202b843c749SSergey Zigachev 				goto free_partial_kdata;
203b843c749SSergey Zigachev 			}
204b843c749SSergey Zigachev 
205b843c749SSergey Zigachev 			ret = amdgpu_cs_bo_handles_chunk(p, p->chunks[i].kdata);
206b843c749SSergey Zigachev 			if (ret)
207b843c749SSergey Zigachev 				goto free_partial_kdata;
208b843c749SSergey Zigachev 
209b843c749SSergey Zigachev 			break;
210b843c749SSergey Zigachev 
211b843c749SSergey Zigachev 		case AMDGPU_CHUNK_ID_DEPENDENCIES:
212b843c749SSergey Zigachev 		case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
213b843c749SSergey Zigachev 		case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
214b843c749SSergey Zigachev 			break;
215b843c749SSergey Zigachev 
216b843c749SSergey Zigachev 		default:
217b843c749SSergey Zigachev 			ret = -EINVAL;
218b843c749SSergey Zigachev 			goto free_partial_kdata;
219b843c749SSergey Zigachev 		}
220b843c749SSergey Zigachev 	}
221b843c749SSergey Zigachev 
222b843c749SSergey Zigachev 	ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
223b843c749SSergey Zigachev 	if (ret)
224b843c749SSergey Zigachev 		goto free_all_kdata;
225b843c749SSergey Zigachev 
226b843c749SSergey Zigachev 	if (p->ctx->vram_lost_counter != p->job->vram_lost_counter) {
227b843c749SSergey Zigachev 		ret = -ECANCELED;
228b843c749SSergey Zigachev 		goto free_all_kdata;
229b843c749SSergey Zigachev 	}
230b843c749SSergey Zigachev 
231b843c749SSergey Zigachev 	if (p->uf_entry.robj)
232b843c749SSergey Zigachev 		p->job->uf_addr = uf_offset;
233b843c749SSergey Zigachev 	kfree(chunk_array);
234b843c749SSergey Zigachev 
235b843c749SSergey Zigachev 	/* Use this opportunity to fill in task info for the vm */
236b843c749SSergey Zigachev 	amdgpu_vm_set_task_info(vm);
237b843c749SSergey Zigachev 
238b843c749SSergey Zigachev 	return 0;
239b843c749SSergey Zigachev 
240b843c749SSergey Zigachev free_all_kdata:
241b843c749SSergey Zigachev 	i = p->nchunks - 1;
242b843c749SSergey Zigachev free_partial_kdata:
243b843c749SSergey Zigachev 	for (; i >= 0; i--)
244b843c749SSergey Zigachev 		kvfree(p->chunks[i].kdata);
245b843c749SSergey Zigachev 	kfree(p->chunks);
246b843c749SSergey Zigachev 	p->chunks = NULL;
247b843c749SSergey Zigachev 	p->nchunks = 0;
248b843c749SSergey Zigachev free_chunk:
249b843c749SSergey Zigachev 	kfree(chunk_array);
250b843c749SSergey Zigachev 
251b843c749SSergey Zigachev 	return ret;
252b843c749SSergey Zigachev }
253b843c749SSergey Zigachev 
254b843c749SSergey Zigachev /* Convert microseconds to bytes. */
us_to_bytes(struct amdgpu_device * adev,s64 us)255b843c749SSergey Zigachev static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
256b843c749SSergey Zigachev {
257b843c749SSergey Zigachev 	if (us <= 0 || !adev->mm_stats.log2_max_MBps)
258b843c749SSergey Zigachev 		return 0;
259b843c749SSergey Zigachev 
260b843c749SSergey Zigachev 	/* Since accum_us is incremented by a million per second, just
261b843c749SSergey Zigachev 	 * multiply it by the number of MB/s to get the number of bytes.
262b843c749SSergey Zigachev 	 */
263b843c749SSergey Zigachev 	return us << adev->mm_stats.log2_max_MBps;
264b843c749SSergey Zigachev }
265b843c749SSergey Zigachev 
bytes_to_us(struct amdgpu_device * adev,u64 bytes)266b843c749SSergey Zigachev static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
267b843c749SSergey Zigachev {
268b843c749SSergey Zigachev 	if (!adev->mm_stats.log2_max_MBps)
269b843c749SSergey Zigachev 		return 0;
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev 	return bytes >> adev->mm_stats.log2_max_MBps;
272b843c749SSergey Zigachev }
273b843c749SSergey Zigachev 
274b843c749SSergey Zigachev /* Returns how many bytes TTM can move right now. If no bytes can be moved,
275b843c749SSergey Zigachev  * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
276b843c749SSergey Zigachev  * which means it can go over the threshold once. If that happens, the driver
277b843c749SSergey Zigachev  * will be in debt and no other buffer migrations can be done until that debt
278b843c749SSergey Zigachev  * is repaid.
279b843c749SSergey Zigachev  *
280b843c749SSergey Zigachev  * This approach allows moving a buffer of any size (it's important to allow
281b843c749SSergey Zigachev  * that).
282b843c749SSergey Zigachev  *
283b843c749SSergey Zigachev  * The currency is simply time in microseconds and it increases as the clock
284b843c749SSergey Zigachev  * ticks. The accumulated microseconds (us) are converted to bytes and
285b843c749SSergey Zigachev  * returned.
286b843c749SSergey Zigachev  */
amdgpu_cs_get_threshold_for_moves(struct amdgpu_device * adev,u64 * max_bytes,u64 * max_vis_bytes)287b843c749SSergey Zigachev static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
288b843c749SSergey Zigachev 					      u64 *max_bytes,
289b843c749SSergey Zigachev 					      u64 *max_vis_bytes)
290b843c749SSergey Zigachev {
291b843c749SSergey Zigachev 	s64 time_us, increment_us;
292b843c749SSergey Zigachev 	u64 free_vram, total_vram, used_vram;
293b843c749SSergey Zigachev 
294b843c749SSergey Zigachev 	/* Allow a maximum of 200 accumulated ms. This is basically per-IB
295b843c749SSergey Zigachev 	 * throttling.
296b843c749SSergey Zigachev 	 *
297b843c749SSergey Zigachev 	 * It means that in order to get full max MBps, at least 5 IBs per
298b843c749SSergey Zigachev 	 * second must be submitted and not more than 200ms apart from each
299b843c749SSergey Zigachev 	 * other.
300b843c749SSergey Zigachev 	 */
301b843c749SSergey Zigachev 	const s64 us_upper_bound = 200000;
302b843c749SSergey Zigachev 
303b843c749SSergey Zigachev 	if (!adev->mm_stats.log2_max_MBps) {
304b843c749SSergey Zigachev 		*max_bytes = 0;
305b843c749SSergey Zigachev 		*max_vis_bytes = 0;
306b843c749SSergey Zigachev 		return;
307b843c749SSergey Zigachev 	}
308b843c749SSergey Zigachev 
309b843c749SSergey Zigachev 	total_vram = adev->gmc.real_vram_size - atomic64_read(&adev->vram_pin_size);
310b843c749SSergey Zigachev 	used_vram = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
311b843c749SSergey Zigachev 	free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
312b843c749SSergey Zigachev 
313b843c749SSergey Zigachev 	spin_lock(&adev->mm_stats.lock);
314b843c749SSergey Zigachev 
315b843c749SSergey Zigachev 	/* Increase the amount of accumulated us. */
316b843c749SSergey Zigachev 	time_us = ktime_to_us(ktime_get());
317b843c749SSergey Zigachev 	increment_us = time_us - adev->mm_stats.last_update_us;
318b843c749SSergey Zigachev 	adev->mm_stats.last_update_us = time_us;
319b843c749SSergey Zigachev 	adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
320b843c749SSergey Zigachev                                       us_upper_bound);
321b843c749SSergey Zigachev 
322b843c749SSergey Zigachev 	/* This prevents the short period of low performance when the VRAM
323b843c749SSergey Zigachev 	 * usage is low and the driver is in debt or doesn't have enough
324b843c749SSergey Zigachev 	 * accumulated us to fill VRAM quickly.
325b843c749SSergey Zigachev 	 *
326b843c749SSergey Zigachev 	 * The situation can occur in these cases:
327b843c749SSergey Zigachev 	 * - a lot of VRAM is freed by userspace
328b843c749SSergey Zigachev 	 * - the presence of a big buffer causes a lot of evictions
329b843c749SSergey Zigachev 	 *   (solution: split buffers into smaller ones)
330b843c749SSergey Zigachev 	 *
331b843c749SSergey Zigachev 	 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
332b843c749SSergey Zigachev 	 * accum_us to a positive number.
333b843c749SSergey Zigachev 	 */
334b843c749SSergey Zigachev 	if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
335b843c749SSergey Zigachev 		s64 min_us;
336b843c749SSergey Zigachev 
337b843c749SSergey Zigachev 		/* Be more aggresive on dGPUs. Try to fill a portion of free
338b843c749SSergey Zigachev 		 * VRAM now.
339b843c749SSergey Zigachev 		 */
340b843c749SSergey Zigachev 		if (!(adev->flags & AMD_IS_APU))
341b843c749SSergey Zigachev 			min_us = bytes_to_us(adev, free_vram / 4);
342b843c749SSergey Zigachev 		else
343b843c749SSergey Zigachev 			min_us = 0; /* Reset accum_us on APUs. */
344b843c749SSergey Zigachev 
345b843c749SSergey Zigachev 		adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
346b843c749SSergey Zigachev 	}
347b843c749SSergey Zigachev 
348b843c749SSergey Zigachev 	/* This is set to 0 if the driver is in debt to disallow (optional)
349b843c749SSergey Zigachev 	 * buffer moves.
350b843c749SSergey Zigachev 	 */
351b843c749SSergey Zigachev 	*max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
352b843c749SSergey Zigachev 
353b843c749SSergey Zigachev 	/* Do the same for visible VRAM if half of it is free */
354b843c749SSergey Zigachev 	if (!amdgpu_gmc_vram_full_visible(&adev->gmc)) {
355b843c749SSergey Zigachev 		u64 total_vis_vram = adev->gmc.visible_vram_size;
356b843c749SSergey Zigachev 		u64 used_vis_vram =
357b843c749SSergey Zigachev 			amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
358b843c749SSergey Zigachev 
359b843c749SSergey Zigachev 		if (used_vis_vram < total_vis_vram) {
360b843c749SSergey Zigachev 			u64 free_vis_vram = total_vis_vram - used_vis_vram;
361b843c749SSergey Zigachev 			adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
362b843c749SSergey Zigachev 							  increment_us, us_upper_bound);
363b843c749SSergey Zigachev 
364b843c749SSergey Zigachev 			if (free_vis_vram >= total_vis_vram / 2)
365b843c749SSergey Zigachev 				adev->mm_stats.accum_us_vis =
366b843c749SSergey Zigachev 					max(bytes_to_us(adev, free_vis_vram / 2),
367b843c749SSergey Zigachev 					    adev->mm_stats.accum_us_vis);
368b843c749SSergey Zigachev 		}
369b843c749SSergey Zigachev 
370b843c749SSergey Zigachev 		*max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
371b843c749SSergey Zigachev 	} else {
372b843c749SSergey Zigachev 		*max_vis_bytes = 0;
373b843c749SSergey Zigachev 	}
374b843c749SSergey Zigachev 
375b843c749SSergey Zigachev 	spin_unlock(&adev->mm_stats.lock);
376b843c749SSergey Zigachev }
377b843c749SSergey Zigachev 
378b843c749SSergey Zigachev /* Report how many bytes have really been moved for the last command
379b843c749SSergey Zigachev  * submission. This can result in a debt that can stop buffer migrations
380b843c749SSergey Zigachev  * temporarily.
381b843c749SSergey Zigachev  */
amdgpu_cs_report_moved_bytes(struct amdgpu_device * adev,u64 num_bytes,u64 num_vis_bytes)382b843c749SSergey Zigachev void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
383b843c749SSergey Zigachev 				  u64 num_vis_bytes)
384b843c749SSergey Zigachev {
385b843c749SSergey Zigachev 	spin_lock(&adev->mm_stats.lock);
386b843c749SSergey Zigachev 	adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
387b843c749SSergey Zigachev 	adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
388b843c749SSergey Zigachev 	spin_unlock(&adev->mm_stats.lock);
389b843c749SSergey Zigachev }
390b843c749SSergey Zigachev 
amdgpu_cs_bo_validate(struct amdgpu_cs_parser * p,struct amdgpu_bo * bo)391b843c749SSergey Zigachev static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
392b843c749SSergey Zigachev 				 struct amdgpu_bo *bo)
393b843c749SSergey Zigachev {
394b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
395b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = {
396b843c749SSergey Zigachev 		.interruptible = true,
397b843c749SSergey Zigachev 		.no_wait_gpu = false,
398b843c749SSergey Zigachev 		.resv = bo->tbo.resv,
399b843c749SSergey Zigachev 		.flags = 0
400b843c749SSergey Zigachev 	};
401b843c749SSergey Zigachev 	uint32_t domain;
402b843c749SSergey Zigachev 	int r;
403b843c749SSergey Zigachev 
404b843c749SSergey Zigachev 	if (bo->pin_count)
405b843c749SSergey Zigachev 		return 0;
406b843c749SSergey Zigachev 
407b843c749SSergey Zigachev 	/* Don't move this buffer if we have depleted our allowance
408b843c749SSergey Zigachev 	 * to move it. Don't move anything if the threshold is zero.
409b843c749SSergey Zigachev 	 */
410b843c749SSergey Zigachev 	if (p->bytes_moved < p->bytes_moved_threshold) {
411b843c749SSergey Zigachev 		if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
412b843c749SSergey Zigachev 		    (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
413b843c749SSergey Zigachev 			/* And don't move a CPU_ACCESS_REQUIRED BO to limited
414b843c749SSergey Zigachev 			 * visible VRAM if we've depleted our allowance to do
415b843c749SSergey Zigachev 			 * that.
416b843c749SSergey Zigachev 			 */
417b843c749SSergey Zigachev 			if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
418b843c749SSergey Zigachev 				domain = bo->preferred_domains;
419b843c749SSergey Zigachev 			else
420b843c749SSergey Zigachev 				domain = bo->allowed_domains;
421b843c749SSergey Zigachev 		} else {
422b843c749SSergey Zigachev 			domain = bo->preferred_domains;
423b843c749SSergey Zigachev 		}
424b843c749SSergey Zigachev 	} else {
425b843c749SSergey Zigachev 		domain = bo->allowed_domains;
426b843c749SSergey Zigachev 	}
427b843c749SSergey Zigachev 
428b843c749SSergey Zigachev retry:
429b843c749SSergey Zigachev 	amdgpu_bo_placement_from_domain(bo, domain);
430b843c749SSergey Zigachev 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
431b843c749SSergey Zigachev 
432b843c749SSergey Zigachev 	p->bytes_moved += ctx.bytes_moved;
433b843c749SSergey Zigachev 	if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
434b843c749SSergey Zigachev 	    amdgpu_bo_in_cpu_visible_vram(bo))
435b843c749SSergey Zigachev 		p->bytes_moved_vis += ctx.bytes_moved;
436b843c749SSergey Zigachev 
437b843c749SSergey Zigachev 	if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
438b843c749SSergey Zigachev 		domain = bo->allowed_domains;
439b843c749SSergey Zigachev 		goto retry;
440b843c749SSergey Zigachev 	}
441b843c749SSergey Zigachev 
442b843c749SSergey Zigachev 	return r;
443b843c749SSergey Zigachev }
444b843c749SSergey Zigachev 
445b843c749SSergey Zigachev /* Last resort, try to evict something from the current working set */
amdgpu_cs_try_evict(struct amdgpu_cs_parser * p,struct amdgpu_bo * validated)446b843c749SSergey Zigachev static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
447b843c749SSergey Zigachev 				struct amdgpu_bo *validated)
448b843c749SSergey Zigachev {
449b843c749SSergey Zigachev 	uint32_t domain = validated->allowed_domains;
450b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = { true, false };
451b843c749SSergey Zigachev 	int r;
452b843c749SSergey Zigachev 
453b843c749SSergey Zigachev 	if (!p->evictable)
454b843c749SSergey Zigachev 		return false;
455b843c749SSergey Zigachev 
456b843c749SSergey Zigachev 	for (;&p->evictable->tv.head != &p->validated;
457b843c749SSergey Zigachev 	     p->evictable = list_prev_entry(p->evictable, tv.head)) {
458b843c749SSergey Zigachev 
459b843c749SSergey Zigachev 		struct amdgpu_bo_list_entry *candidate = p->evictable;
460b843c749SSergey Zigachev 		struct amdgpu_bo *bo = candidate->robj;
461b843c749SSergey Zigachev 		struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
462b843c749SSergey Zigachev 		bool update_bytes_moved_vis;
463b843c749SSergey Zigachev 		uint32_t other;
464b843c749SSergey Zigachev 
465b843c749SSergey Zigachev 		/* If we reached our current BO we can forget it */
466b843c749SSergey Zigachev 		if (candidate->robj == validated)
467b843c749SSergey Zigachev 			break;
468b843c749SSergey Zigachev 
469b843c749SSergey Zigachev 		/* We can't move pinned BOs here */
470b843c749SSergey Zigachev 		if (bo->pin_count)
471b843c749SSergey Zigachev 			continue;
472b843c749SSergey Zigachev 
473b843c749SSergey Zigachev 		other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
474b843c749SSergey Zigachev 
475b843c749SSergey Zigachev 		/* Check if this BO is in one of the domains we need space for */
476b843c749SSergey Zigachev 		if (!(other & domain))
477b843c749SSergey Zigachev 			continue;
478b843c749SSergey Zigachev 
479b843c749SSergey Zigachev 		/* Check if we can move this BO somewhere else */
480b843c749SSergey Zigachev 		other = bo->allowed_domains & ~domain;
481b843c749SSergey Zigachev 		if (!other)
482b843c749SSergey Zigachev 			continue;
483b843c749SSergey Zigachev 
484b843c749SSergey Zigachev 		/* Good we can try to move this BO somewhere else */
485b843c749SSergey Zigachev 		update_bytes_moved_vis =
486b843c749SSergey Zigachev 				!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
487b843c749SSergey Zigachev 				amdgpu_bo_in_cpu_visible_vram(bo);
488b843c749SSergey Zigachev 		amdgpu_bo_placement_from_domain(bo, other);
489b843c749SSergey Zigachev 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
490b843c749SSergey Zigachev 		p->bytes_moved += ctx.bytes_moved;
491b843c749SSergey Zigachev 		if (update_bytes_moved_vis)
492b843c749SSergey Zigachev 			p->bytes_moved_vis += ctx.bytes_moved;
493b843c749SSergey Zigachev 
494b843c749SSergey Zigachev 		if (unlikely(r))
495b843c749SSergey Zigachev 			break;
496b843c749SSergey Zigachev 
497b843c749SSergey Zigachev 		p->evictable = list_prev_entry(p->evictable, tv.head);
498b843c749SSergey Zigachev 		list_move(&candidate->tv.head, &p->validated);
499b843c749SSergey Zigachev 
500b843c749SSergey Zigachev 		return true;
501b843c749SSergey Zigachev 	}
502b843c749SSergey Zigachev 
503b843c749SSergey Zigachev 	return false;
504b843c749SSergey Zigachev }
505b843c749SSergey Zigachev 
amdgpu_cs_validate(void * param,struct amdgpu_bo * bo)506b843c749SSergey Zigachev static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
507b843c749SSergey Zigachev {
508b843c749SSergey Zigachev 	struct amdgpu_cs_parser *p = param;
509b843c749SSergey Zigachev 	int r;
510b843c749SSergey Zigachev 
511b843c749SSergey Zigachev 	do {
512b843c749SSergey Zigachev 		r = amdgpu_cs_bo_validate(p, bo);
513b843c749SSergey Zigachev 	} while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
514b843c749SSergey Zigachev 	if (r)
515b843c749SSergey Zigachev 		return r;
516b843c749SSergey Zigachev 
517b843c749SSergey Zigachev 	if (bo->shadow)
518b843c749SSergey Zigachev 		r = amdgpu_cs_bo_validate(p, bo->shadow);
519b843c749SSergey Zigachev 
520b843c749SSergey Zigachev 	return r;
521b843c749SSergey Zigachev }
522b843c749SSergey Zigachev 
amdgpu_cs_list_validate(struct amdgpu_cs_parser * p,struct list_head * validated)523b843c749SSergey Zigachev static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
524b843c749SSergey Zigachev 			    struct list_head *validated)
525b843c749SSergey Zigachev {
526b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = { true, false };
527b843c749SSergey Zigachev 	struct amdgpu_bo_list_entry *lobj;
528b843c749SSergey Zigachev 	int r;
529b843c749SSergey Zigachev 
530b843c749SSergey Zigachev 	list_for_each_entry(lobj, validated, tv.head) {
531b843c749SSergey Zigachev 		struct amdgpu_bo *bo = lobj->robj;
532b843c749SSergey Zigachev 		bool binding_userptr = false;
533b843c749SSergey Zigachev 		struct mm_struct *usermm;
534b843c749SSergey Zigachev 
535b843c749SSergey Zigachev 		usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
536*78973132SSergey Zigachev #if 0
537b843c749SSergey Zigachev 		if (usermm && usermm != current->mm)
538b843c749SSergey Zigachev 			return -EPERM;
539*78973132SSergey Zigachev #endif
540b843c749SSergey Zigachev 
541b843c749SSergey Zigachev 		/* Check if we have user pages and nobody bound the BO already */
542b843c749SSergey Zigachev 		if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
543b843c749SSergey Zigachev 		    lobj->user_pages) {
544b843c749SSergey Zigachev 			amdgpu_bo_placement_from_domain(bo,
545b843c749SSergey Zigachev 							AMDGPU_GEM_DOMAIN_CPU);
546b843c749SSergey Zigachev 			r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
547b843c749SSergey Zigachev 			if (r)
548b843c749SSergey Zigachev 				return r;
549b843c749SSergey Zigachev 			amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
550b843c749SSergey Zigachev 						     lobj->user_pages);
551b843c749SSergey Zigachev 			binding_userptr = true;
552b843c749SSergey Zigachev 		}
553b843c749SSergey Zigachev 
554b843c749SSergey Zigachev 		if (p->evictable == lobj)
555b843c749SSergey Zigachev 			p->evictable = NULL;
556b843c749SSergey Zigachev 
557b843c749SSergey Zigachev 		r = amdgpu_cs_validate(p, bo);
558b843c749SSergey Zigachev 		if (r)
559b843c749SSergey Zigachev 			return r;
560b843c749SSergey Zigachev 
561b843c749SSergey Zigachev 		if (binding_userptr) {
562b843c749SSergey Zigachev 			kvfree(lobj->user_pages);
563b843c749SSergey Zigachev 			lobj->user_pages = NULL;
564b843c749SSergey Zigachev 		}
565b843c749SSergey Zigachev 	}
566b843c749SSergey Zigachev 	return 0;
567b843c749SSergey Zigachev }
568b843c749SSergey Zigachev 
amdgpu_cs_parser_bos(struct amdgpu_cs_parser * p,union drm_amdgpu_cs * cs)569b843c749SSergey Zigachev static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
570b843c749SSergey Zigachev 				union drm_amdgpu_cs *cs)
571b843c749SSergey Zigachev {
572b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
573b843c749SSergey Zigachev 	struct amdgpu_vm *vm = &fpriv->vm;
574b843c749SSergey Zigachev 	struct amdgpu_bo_list_entry *e;
575b843c749SSergey Zigachev 	struct list_head duplicates;
576b843c749SSergey Zigachev 	struct amdgpu_bo *gds;
577b843c749SSergey Zigachev 	struct amdgpu_bo *gws;
578b843c749SSergey Zigachev 	struct amdgpu_bo *oa;
579b843c749SSergey Zigachev 	unsigned tries = 10;
580b843c749SSergey Zigachev 	int r;
581b843c749SSergey Zigachev 
582b843c749SSergey Zigachev 	INIT_LIST_HEAD(&p->validated);
583b843c749SSergey Zigachev 
584b843c749SSergey Zigachev 	/* p->bo_list could already be assigned if AMDGPU_CHUNK_ID_BO_HANDLES is present */
585b843c749SSergey Zigachev 	if (cs->in.bo_list_handle) {
586b843c749SSergey Zigachev 		if (p->bo_list)
587b843c749SSergey Zigachev 			return -EINVAL;
588b843c749SSergey Zigachev 
589b843c749SSergey Zigachev 		r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
590b843c749SSergey Zigachev 				       &p->bo_list);
591b843c749SSergey Zigachev 		if (r)
592b843c749SSergey Zigachev 			return r;
593b843c749SSergey Zigachev 	} else if (!p->bo_list) {
594b843c749SSergey Zigachev 		/* Create a empty bo_list when no handle is provided */
595b843c749SSergey Zigachev 		r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
596b843c749SSergey Zigachev 					  &p->bo_list);
597b843c749SSergey Zigachev 		if (r)
598b843c749SSergey Zigachev 			return r;
599b843c749SSergey Zigachev 	}
600b843c749SSergey Zigachev 
601b843c749SSergey Zigachev 	amdgpu_bo_list_get_list(p->bo_list, &p->validated);
602b843c749SSergey Zigachev 	if (p->bo_list->first_userptr != p->bo_list->num_entries)
603b843c749SSergey Zigachev 		p->mn = amdgpu_mn_get(p->adev, AMDGPU_MN_TYPE_GFX);
604b843c749SSergey Zigachev 
605b843c749SSergey Zigachev 	INIT_LIST_HEAD(&duplicates);
606b843c749SSergey Zigachev 	amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
607b843c749SSergey Zigachev 
608b843c749SSergey Zigachev 	if (p->uf_entry.robj && !p->uf_entry.robj->parent)
609b843c749SSergey Zigachev 		list_add(&p->uf_entry.tv.head, &p->validated);
610b843c749SSergey Zigachev 
611b843c749SSergey Zigachev 	while (1) {
612b843c749SSergey Zigachev 		struct list_head need_pages;
613b843c749SSergey Zigachev 
614b843c749SSergey Zigachev 		r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
615b843c749SSergey Zigachev 					   &duplicates);
616b843c749SSergey Zigachev 		if (unlikely(r != 0)) {
617b843c749SSergey Zigachev 			if (r != -ERESTARTSYS)
618b843c749SSergey Zigachev 				DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
619b843c749SSergey Zigachev 			goto error_free_pages;
620b843c749SSergey Zigachev 		}
621b843c749SSergey Zigachev 
622b843c749SSergey Zigachev 		INIT_LIST_HEAD(&need_pages);
623b843c749SSergey Zigachev 		amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
624b843c749SSergey Zigachev 			struct amdgpu_bo *bo = e->robj;
625b843c749SSergey Zigachev 
626b843c749SSergey Zigachev 			if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
627b843c749SSergey Zigachev 				 &e->user_invalidated) && e->user_pages) {
628b843c749SSergey Zigachev 
629b843c749SSergey Zigachev 				/* We acquired a page array, but somebody
630b843c749SSergey Zigachev 				 * invalidated it. Free it and try again
631b843c749SSergey Zigachev 				 */
632b843c749SSergey Zigachev 				release_pages(e->user_pages,
633b843c749SSergey Zigachev 					      bo->tbo.ttm->num_pages);
634b843c749SSergey Zigachev 				kvfree(e->user_pages);
635b843c749SSergey Zigachev 				e->user_pages = NULL;
636b843c749SSergey Zigachev 			}
637b843c749SSergey Zigachev 
638b843c749SSergey Zigachev 			if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
639b843c749SSergey Zigachev 			    !e->user_pages) {
640b843c749SSergey Zigachev 				list_del(&e->tv.head);
641b843c749SSergey Zigachev 				list_add(&e->tv.head, &need_pages);
642b843c749SSergey Zigachev 
643b843c749SSergey Zigachev 				amdgpu_bo_unreserve(e->robj);
644b843c749SSergey Zigachev 			}
645b843c749SSergey Zigachev 		}
646b843c749SSergey Zigachev 
647b843c749SSergey Zigachev 		if (list_empty(&need_pages))
648b843c749SSergey Zigachev 			break;
649b843c749SSergey Zigachev 
650b843c749SSergey Zigachev 		/* Unreserve everything again. */
651b843c749SSergey Zigachev 		ttm_eu_backoff_reservation(&p->ticket, &p->validated);
652b843c749SSergey Zigachev 
653b843c749SSergey Zigachev 		/* We tried too many times, just abort */
654b843c749SSergey Zigachev 		if (!--tries) {
655b843c749SSergey Zigachev 			r = -EDEADLK;
656b843c749SSergey Zigachev 			DRM_ERROR("deadlock in %s\n", __func__);
657b843c749SSergey Zigachev 			goto error_free_pages;
658b843c749SSergey Zigachev 		}
659b843c749SSergey Zigachev 
660b843c749SSergey Zigachev 		/* Fill the page arrays for all userptrs. */
661b843c749SSergey Zigachev 		list_for_each_entry(e, &need_pages, tv.head) {
662b843c749SSergey Zigachev 			struct ttm_tt *ttm = e->robj->tbo.ttm;
663b843c749SSergey Zigachev 
664b843c749SSergey Zigachev 			e->user_pages = kvmalloc_array(ttm->num_pages,
665b843c749SSergey Zigachev 							 sizeof(struct page*),
666b843c749SSergey Zigachev 							 GFP_KERNEL | __GFP_ZERO);
667b843c749SSergey Zigachev 			if (!e->user_pages) {
668b843c749SSergey Zigachev 				r = -ENOMEM;
669b843c749SSergey Zigachev 				DRM_ERROR("calloc failure in %s\n", __func__);
670b843c749SSergey Zigachev 				goto error_free_pages;
671b843c749SSergey Zigachev 			}
672b843c749SSergey Zigachev 
673b843c749SSergey Zigachev 			r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
674b843c749SSergey Zigachev 			if (r) {
675b843c749SSergey Zigachev 				DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
676b843c749SSergey Zigachev 				kvfree(e->user_pages);
677b843c749SSergey Zigachev 				e->user_pages = NULL;
678b843c749SSergey Zigachev 				goto error_free_pages;
679b843c749SSergey Zigachev 			}
680b843c749SSergey Zigachev 		}
681b843c749SSergey Zigachev 
682b843c749SSergey Zigachev 		/* And try again. */
683b843c749SSergey Zigachev 		list_splice(&need_pages, &p->validated);
684b843c749SSergey Zigachev 	}
685b843c749SSergey Zigachev 
686*78973132SSergey Zigachev 	amdgpu_cs_get_threshold_for_moves(p->adev, (u64 *)&p->bytes_moved_threshold,
687*78973132SSergey Zigachev 					  (u64 *)&p->bytes_moved_vis_threshold);
688b843c749SSergey Zigachev 	p->bytes_moved = 0;
689b843c749SSergey Zigachev 	p->bytes_moved_vis = 0;
690b843c749SSergey Zigachev 	p->evictable = list_last_entry(&p->validated,
691b843c749SSergey Zigachev 				       struct amdgpu_bo_list_entry,
692b843c749SSergey Zigachev 				       tv.head);
693b843c749SSergey Zigachev 
694b843c749SSergey Zigachev 	r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
695b843c749SSergey Zigachev 				      amdgpu_cs_validate, p);
696b843c749SSergey Zigachev 	if (r) {
697b843c749SSergey Zigachev 		DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
698b843c749SSergey Zigachev 		goto error_validate;
699b843c749SSergey Zigachev 	}
700b843c749SSergey Zigachev 
701b843c749SSergey Zigachev 	r = amdgpu_cs_list_validate(p, &duplicates);
702b843c749SSergey Zigachev 	if (r) {
703b843c749SSergey Zigachev 		DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
704b843c749SSergey Zigachev 		goto error_validate;
705b843c749SSergey Zigachev 	}
706b843c749SSergey Zigachev 
707b843c749SSergey Zigachev 	r = amdgpu_cs_list_validate(p, &p->validated);
708b843c749SSergey Zigachev 	if (r) {
709b843c749SSergey Zigachev 		DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
710b843c749SSergey Zigachev 		goto error_validate;
711b843c749SSergey Zigachev 	}
712b843c749SSergey Zigachev 
713b843c749SSergey Zigachev 	amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
714b843c749SSergey Zigachev 				     p->bytes_moved_vis);
715b843c749SSergey Zigachev 
716b843c749SSergey Zigachev 	gds = p->bo_list->gds_obj;
717b843c749SSergey Zigachev 	gws = p->bo_list->gws_obj;
718b843c749SSergey Zigachev 	oa = p->bo_list->oa_obj;
719b843c749SSergey Zigachev 
720b843c749SSergey Zigachev 	amdgpu_bo_list_for_each_entry(e, p->bo_list)
721b843c749SSergey Zigachev 		e->bo_va = amdgpu_vm_bo_find(vm, e->robj);
722b843c749SSergey Zigachev 
723b843c749SSergey Zigachev 	if (gds) {
724b843c749SSergey Zigachev 		p->job->gds_base = amdgpu_bo_gpu_offset(gds);
725b843c749SSergey Zigachev 		p->job->gds_size = amdgpu_bo_size(gds);
726b843c749SSergey Zigachev 	}
727b843c749SSergey Zigachev 	if (gws) {
728b843c749SSergey Zigachev 		p->job->gws_base = amdgpu_bo_gpu_offset(gws);
729b843c749SSergey Zigachev 		p->job->gws_size = amdgpu_bo_size(gws);
730b843c749SSergey Zigachev 	}
731b843c749SSergey Zigachev 	if (oa) {
732b843c749SSergey Zigachev 		p->job->oa_base = amdgpu_bo_gpu_offset(oa);
733b843c749SSergey Zigachev 		p->job->oa_size = amdgpu_bo_size(oa);
734b843c749SSergey Zigachev 	}
735b843c749SSergey Zigachev 
736b843c749SSergey Zigachev 	if (!r && p->uf_entry.robj) {
737b843c749SSergey Zigachev 		struct amdgpu_bo *uf = p->uf_entry.robj;
738b843c749SSergey Zigachev 
739b843c749SSergey Zigachev 		r = amdgpu_ttm_alloc_gart(&uf->tbo);
740b843c749SSergey Zigachev 		p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
741b843c749SSergey Zigachev 	}
742b843c749SSergey Zigachev 
743b843c749SSergey Zigachev error_validate:
744b843c749SSergey Zigachev 	if (r)
745b843c749SSergey Zigachev 		ttm_eu_backoff_reservation(&p->ticket, &p->validated);
746b843c749SSergey Zigachev 
747b843c749SSergey Zigachev error_free_pages:
748b843c749SSergey Zigachev 
749b843c749SSergey Zigachev 	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
750b843c749SSergey Zigachev 		if (!e->user_pages)
751b843c749SSergey Zigachev 			continue;
752b843c749SSergey Zigachev 
753b843c749SSergey Zigachev 		release_pages(e->user_pages,
754b843c749SSergey Zigachev 			      e->robj->tbo.ttm->num_pages);
755b843c749SSergey Zigachev 		kvfree(e->user_pages);
756b843c749SSergey Zigachev 	}
757b843c749SSergey Zigachev 
758b843c749SSergey Zigachev 	return r;
759b843c749SSergey Zigachev }
760b843c749SSergey Zigachev 
amdgpu_cs_sync_rings(struct amdgpu_cs_parser * p)761b843c749SSergey Zigachev static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
762b843c749SSergey Zigachev {
763b843c749SSergey Zigachev 	struct amdgpu_bo_list_entry *e;
764b843c749SSergey Zigachev 	int r;
765b843c749SSergey Zigachev 
766b843c749SSergey Zigachev 	list_for_each_entry(e, &p->validated, tv.head) {
767b843c749SSergey Zigachev 		struct reservation_object *resv = e->robj->tbo.resv;
768b843c749SSergey Zigachev 		r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp,
769b843c749SSergey Zigachev 				     amdgpu_bo_explicit_sync(e->robj));
770b843c749SSergey Zigachev 
771b843c749SSergey Zigachev 		if (r)
772b843c749SSergey Zigachev 			return r;
773b843c749SSergey Zigachev 	}
774b843c749SSergey Zigachev 	return 0;
775b843c749SSergey Zigachev }
776b843c749SSergey Zigachev 
777b843c749SSergey Zigachev /**
778b843c749SSergey Zigachev  * cs_parser_fini() - clean parser states
779b843c749SSergey Zigachev  * @parser:	parser structure holding parsing context.
780b843c749SSergey Zigachev  * @error:	error number
781b843c749SSergey Zigachev  *
782b843c749SSergey Zigachev  * If error is set than unvalidate buffer, otherwise just free memory
783b843c749SSergey Zigachev  * used by parsing context.
784b843c749SSergey Zigachev  **/
amdgpu_cs_parser_fini(struct amdgpu_cs_parser * parser,int error,bool backoff)785b843c749SSergey Zigachev static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
786b843c749SSergey Zigachev 				  bool backoff)
787b843c749SSergey Zigachev {
788b843c749SSergey Zigachev 	unsigned i;
789b843c749SSergey Zigachev 
790b843c749SSergey Zigachev 	if (error && backoff)
791b843c749SSergey Zigachev 		ttm_eu_backoff_reservation(&parser->ticket,
792b843c749SSergey Zigachev 					   &parser->validated);
793b843c749SSergey Zigachev 
794b843c749SSergey Zigachev 	for (i = 0; i < parser->num_post_dep_syncobjs; i++)
795b843c749SSergey Zigachev 		drm_syncobj_put(parser->post_dep_syncobjs[i]);
796b843c749SSergey Zigachev 	kfree(parser->post_dep_syncobjs);
797b843c749SSergey Zigachev 
798b843c749SSergey Zigachev 	dma_fence_put(parser->fence);
799b843c749SSergey Zigachev 
800b843c749SSergey Zigachev 	if (parser->ctx) {
801b843c749SSergey Zigachev 		mutex_unlock(&parser->ctx->lock);
802b843c749SSergey Zigachev 		amdgpu_ctx_put(parser->ctx);
803b843c749SSergey Zigachev 	}
804b843c749SSergey Zigachev 	if (parser->bo_list)
805b843c749SSergey Zigachev 		amdgpu_bo_list_put(parser->bo_list);
806b843c749SSergey Zigachev 
807b843c749SSergey Zigachev 	for (i = 0; i < parser->nchunks; i++)
808b843c749SSergey Zigachev 		kvfree(parser->chunks[i].kdata);
809b843c749SSergey Zigachev 	kfree(parser->chunks);
810b843c749SSergey Zigachev 	if (parser->job)
811b843c749SSergey Zigachev 		amdgpu_job_free(parser->job);
812b843c749SSergey Zigachev 	amdgpu_bo_unref(&parser->uf_entry.robj);
813b843c749SSergey Zigachev }
814b843c749SSergey Zigachev 
amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser * p)815b843c749SSergey Zigachev static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
816b843c749SSergey Zigachev {
817b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
818b843c749SSergey Zigachev 	struct amdgpu_device *adev = p->adev;
819b843c749SSergey Zigachev 	struct amdgpu_vm *vm = &fpriv->vm;
820b843c749SSergey Zigachev 	struct amdgpu_bo_list_entry *e;
821b843c749SSergey Zigachev 	struct amdgpu_bo_va *bo_va;
822b843c749SSergey Zigachev 	struct amdgpu_bo *bo;
823b843c749SSergey Zigachev 	int r;
824b843c749SSergey Zigachev 
825b843c749SSergey Zigachev 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
826b843c749SSergey Zigachev 	if (r)
827b843c749SSergey Zigachev 		return r;
828b843c749SSergey Zigachev 
829b843c749SSergey Zigachev 	r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
830b843c749SSergey Zigachev 	if (r)
831b843c749SSergey Zigachev 		return r;
832b843c749SSergey Zigachev 
833b843c749SSergey Zigachev 	r = amdgpu_sync_fence(adev, &p->job->sync,
834b843c749SSergey Zigachev 			      fpriv->prt_va->last_pt_update, false);
835b843c749SSergey Zigachev 	if (r)
836b843c749SSergey Zigachev 		return r;
837b843c749SSergey Zigachev 
838b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev)) {
839b843c749SSergey Zigachev 		struct dma_fence *f;
840b843c749SSergey Zigachev 
841b843c749SSergey Zigachev 		bo_va = fpriv->csa_va;
842b843c749SSergey Zigachev 		BUG_ON(!bo_va);
843b843c749SSergey Zigachev 		r = amdgpu_vm_bo_update(adev, bo_va, false);
844b843c749SSergey Zigachev 		if (r)
845b843c749SSergey Zigachev 			return r;
846b843c749SSergey Zigachev 
847b843c749SSergey Zigachev 		f = bo_va->last_pt_update;
848b843c749SSergey Zigachev 		r = amdgpu_sync_fence(adev, &p->job->sync, f, false);
849b843c749SSergey Zigachev 		if (r)
850b843c749SSergey Zigachev 			return r;
851b843c749SSergey Zigachev 	}
852b843c749SSergey Zigachev 
853b843c749SSergey Zigachev 	amdgpu_bo_list_for_each_entry(e, p->bo_list) {
854b843c749SSergey Zigachev 		struct dma_fence *f;
855b843c749SSergey Zigachev 
856b843c749SSergey Zigachev 		/* ignore duplicates */
857b843c749SSergey Zigachev 		bo = e->robj;
858b843c749SSergey Zigachev 		if (!bo)
859b843c749SSergey Zigachev 			continue;
860b843c749SSergey Zigachev 
861b843c749SSergey Zigachev 		bo_va = e->bo_va;
862b843c749SSergey Zigachev 		if (bo_va == NULL)
863b843c749SSergey Zigachev 			continue;
864b843c749SSergey Zigachev 
865b843c749SSergey Zigachev 		r = amdgpu_vm_bo_update(adev, bo_va, false);
866b843c749SSergey Zigachev 		if (r)
867b843c749SSergey Zigachev 			return r;
868b843c749SSergey Zigachev 
869b843c749SSergey Zigachev 		f = bo_va->last_pt_update;
870b843c749SSergey Zigachev 		r = amdgpu_sync_fence(adev, &p->job->sync, f, false);
871b843c749SSergey Zigachev 		if (r)
872b843c749SSergey Zigachev 			return r;
873b843c749SSergey Zigachev 	}
874b843c749SSergey Zigachev 
875b843c749SSergey Zigachev 	r = amdgpu_vm_handle_moved(adev, vm);
876b843c749SSergey Zigachev 	if (r)
877b843c749SSergey Zigachev 		return r;
878b843c749SSergey Zigachev 
879b843c749SSergey Zigachev 	r = amdgpu_vm_update_directories(adev, vm);
880b843c749SSergey Zigachev 	if (r)
881b843c749SSergey Zigachev 		return r;
882b843c749SSergey Zigachev 
883b843c749SSergey Zigachev 	r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_update, false);
884b843c749SSergey Zigachev 	if (r)
885b843c749SSergey Zigachev 		return r;
886b843c749SSergey Zigachev 
887b843c749SSergey Zigachev 	if (amdgpu_vm_debug) {
888b843c749SSergey Zigachev 		/* Invalidate all BOs to test for userspace bugs */
889b843c749SSergey Zigachev 		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
890b843c749SSergey Zigachev 			/* ignore duplicates */
891b843c749SSergey Zigachev 			if (!e->robj)
892b843c749SSergey Zigachev 				continue;
893b843c749SSergey Zigachev 
894b843c749SSergey Zigachev 			amdgpu_vm_bo_invalidate(adev, e->robj, false);
895b843c749SSergey Zigachev 		}
896b843c749SSergey Zigachev 	}
897b843c749SSergey Zigachev 
898b843c749SSergey Zigachev 	return r;
899b843c749SSergey Zigachev }
900b843c749SSergey Zigachev 
amdgpu_cs_ib_vm_chunk(struct amdgpu_device * adev,struct amdgpu_cs_parser * p)901b843c749SSergey Zigachev static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
902b843c749SSergey Zigachev 				 struct amdgpu_cs_parser *p)
903b843c749SSergey Zigachev {
904b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
905b843c749SSergey Zigachev 	struct amdgpu_vm *vm = &fpriv->vm;
906b843c749SSergey Zigachev 	struct amdgpu_ring *ring = p->ring;
907b843c749SSergey Zigachev 	int r;
908b843c749SSergey Zigachev 
909b843c749SSergey Zigachev 	/* Only for UVD/VCE VM emulation */
910b843c749SSergey Zigachev 	if (p->ring->funcs->parse_cs || p->ring->funcs->patch_cs_in_place) {
911b843c749SSergey Zigachev 		unsigned i, j;
912b843c749SSergey Zigachev 
913b843c749SSergey Zigachev 		for (i = 0, j = 0; i < p->nchunks && j < p->job->num_ibs; i++) {
914b843c749SSergey Zigachev 			struct drm_amdgpu_cs_chunk_ib *chunk_ib;
915b843c749SSergey Zigachev 			struct amdgpu_bo_va_mapping *m;
916b843c749SSergey Zigachev 			struct amdgpu_bo *aobj = NULL;
917b843c749SSergey Zigachev 			struct amdgpu_cs_chunk *chunk;
918b843c749SSergey Zigachev 			uint64_t offset, va_start;
919b843c749SSergey Zigachev 			struct amdgpu_ib *ib;
920b843c749SSergey Zigachev 			uint8_t *kptr;
921b843c749SSergey Zigachev 
922b843c749SSergey Zigachev 			chunk = &p->chunks[i];
923b843c749SSergey Zigachev 			ib = &p->job->ibs[j];
924b843c749SSergey Zigachev 			chunk_ib = chunk->kdata;
925b843c749SSergey Zigachev 
926b843c749SSergey Zigachev 			if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
927b843c749SSergey Zigachev 				continue;
928b843c749SSergey Zigachev 
929b843c749SSergey Zigachev 			va_start = chunk_ib->va_start & AMDGPU_VA_HOLE_MASK;
930b843c749SSergey Zigachev 			r = amdgpu_cs_find_mapping(p, va_start, &aobj, &m);
931b843c749SSergey Zigachev 			if (r) {
932b843c749SSergey Zigachev 				DRM_ERROR("IB va_start is invalid\n");
933b843c749SSergey Zigachev 				return r;
934b843c749SSergey Zigachev 			}
935b843c749SSergey Zigachev 
936b843c749SSergey Zigachev 			if ((va_start + chunk_ib->ib_bytes) >
937b843c749SSergey Zigachev 			    (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
938b843c749SSergey Zigachev 				DRM_ERROR("IB va_start+ib_bytes is invalid\n");
939b843c749SSergey Zigachev 				return -EINVAL;
940b843c749SSergey Zigachev 			}
941b843c749SSergey Zigachev 
942b843c749SSergey Zigachev 			/* the IB should be reserved at this point */
943b843c749SSergey Zigachev 			r = amdgpu_bo_kmap(aobj, (void **)&kptr);
944b843c749SSergey Zigachev 			if (r) {
945b843c749SSergey Zigachev 				return r;
946b843c749SSergey Zigachev 			}
947b843c749SSergey Zigachev 
948b843c749SSergey Zigachev 			offset = m->start * AMDGPU_GPU_PAGE_SIZE;
949b843c749SSergey Zigachev 			kptr += va_start - offset;
950b843c749SSergey Zigachev 
951b843c749SSergey Zigachev 			if (p->ring->funcs->parse_cs) {
952b843c749SSergey Zigachev 				memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
953b843c749SSergey Zigachev 				amdgpu_bo_kunmap(aobj);
954b843c749SSergey Zigachev 
955b843c749SSergey Zigachev 				r = amdgpu_ring_parse_cs(ring, p, j);
956b843c749SSergey Zigachev 				if (r)
957b843c749SSergey Zigachev 					return r;
958b843c749SSergey Zigachev 			} else {
959b843c749SSergey Zigachev 				ib->ptr = (uint32_t *)kptr;
960b843c749SSergey Zigachev 				r = amdgpu_ring_patch_cs_in_place(ring, p, j);
961b843c749SSergey Zigachev 				amdgpu_bo_kunmap(aobj);
962b843c749SSergey Zigachev 				if (r)
963b843c749SSergey Zigachev 					return r;
964b843c749SSergey Zigachev 			}
965b843c749SSergey Zigachev 
966b843c749SSergey Zigachev 			j++;
967b843c749SSergey Zigachev 		}
968b843c749SSergey Zigachev 	}
969b843c749SSergey Zigachev 
970b843c749SSergey Zigachev 	if (p->job->vm) {
971b843c749SSergey Zigachev 		p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.base.bo);
972b843c749SSergey Zigachev 
973b843c749SSergey Zigachev 		r = amdgpu_bo_vm_update_pte(p);
974b843c749SSergey Zigachev 		if (r)
975b843c749SSergey Zigachev 			return r;
976b843c749SSergey Zigachev 
977b843c749SSergey Zigachev 		r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
978b843c749SSergey Zigachev 		if (r)
979b843c749SSergey Zigachev 			return r;
980b843c749SSergey Zigachev 	}
981b843c749SSergey Zigachev 
982b843c749SSergey Zigachev 	return amdgpu_cs_sync_rings(p);
983b843c749SSergey Zigachev }
984b843c749SSergey Zigachev 
amdgpu_cs_ib_fill(struct amdgpu_device * adev,struct amdgpu_cs_parser * parser)985b843c749SSergey Zigachev static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
986b843c749SSergey Zigachev 			     struct amdgpu_cs_parser *parser)
987b843c749SSergey Zigachev {
988b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
989b843c749SSergey Zigachev 	struct amdgpu_vm *vm = &fpriv->vm;
990b843c749SSergey Zigachev 	int i, j;
991b843c749SSergey Zigachev 	int r, ce_preempt = 0, de_preempt = 0;
992b843c749SSergey Zigachev 
993b843c749SSergey Zigachev 	for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
994b843c749SSergey Zigachev 		struct amdgpu_cs_chunk *chunk;
995b843c749SSergey Zigachev 		struct amdgpu_ib *ib;
996b843c749SSergey Zigachev 		struct drm_amdgpu_cs_chunk_ib *chunk_ib;
997b843c749SSergey Zigachev 		struct amdgpu_ring *ring;
998b843c749SSergey Zigachev 
999b843c749SSergey Zigachev 		chunk = &parser->chunks[i];
1000b843c749SSergey Zigachev 		ib = &parser->job->ibs[j];
1001b843c749SSergey Zigachev 		chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
1002b843c749SSergey Zigachev 
1003b843c749SSergey Zigachev 		if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
1004b843c749SSergey Zigachev 			continue;
1005b843c749SSergey Zigachev 
1006b843c749SSergey Zigachev 		if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
1007b843c749SSergey Zigachev 			if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
1008b843c749SSergey Zigachev 				if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
1009b843c749SSergey Zigachev 					ce_preempt++;
1010b843c749SSergey Zigachev 				else
1011b843c749SSergey Zigachev 					de_preempt++;
1012b843c749SSergey Zigachev 			}
1013b843c749SSergey Zigachev 
1014b843c749SSergey Zigachev 			/* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
1015b843c749SSergey Zigachev 			if (ce_preempt > 1 || de_preempt > 1)
1016b843c749SSergey Zigachev 				return -EINVAL;
1017b843c749SSergey Zigachev 		}
1018b843c749SSergey Zigachev 
1019b843c749SSergey Zigachev 		r = amdgpu_queue_mgr_map(adev, &parser->ctx->queue_mgr, chunk_ib->ip_type,
1020b843c749SSergey Zigachev 					 chunk_ib->ip_instance, chunk_ib->ring, &ring);
1021b843c749SSergey Zigachev 		if (r)
1022b843c749SSergey Zigachev 			return r;
1023b843c749SSergey Zigachev 
1024b843c749SSergey Zigachev 		if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
1025b843c749SSergey Zigachev 			parser->job->preamble_status |=
1026b843c749SSergey Zigachev 				AMDGPU_PREAMBLE_IB_PRESENT;
1027b843c749SSergey Zigachev 
1028b843c749SSergey Zigachev 		if (parser->ring && parser->ring != ring)
1029b843c749SSergey Zigachev 			return -EINVAL;
1030b843c749SSergey Zigachev 
1031b843c749SSergey Zigachev 		parser->ring = ring;
1032b843c749SSergey Zigachev 
1033b843c749SSergey Zigachev 		r =  amdgpu_ib_get(adev, vm,
1034b843c749SSergey Zigachev 					ring->funcs->parse_cs ? chunk_ib->ib_bytes : 0,
1035b843c749SSergey Zigachev 					ib);
1036b843c749SSergey Zigachev 		if (r) {
1037b843c749SSergey Zigachev 			DRM_ERROR("Failed to get ib !\n");
1038b843c749SSergey Zigachev 			return r;
1039b843c749SSergey Zigachev 		}
1040b843c749SSergey Zigachev 
1041b843c749SSergey Zigachev 		ib->gpu_addr = chunk_ib->va_start;
1042b843c749SSergey Zigachev 		ib->length_dw = chunk_ib->ib_bytes / 4;
1043b843c749SSergey Zigachev 		ib->flags = chunk_ib->flags;
1044b843c749SSergey Zigachev 
1045b843c749SSergey Zigachev 		j++;
1046b843c749SSergey Zigachev 	}
1047b843c749SSergey Zigachev 
1048b843c749SSergey Zigachev 	/* UVD & VCE fw doesn't support user fences */
1049b843c749SSergey Zigachev 	if (parser->job->uf_addr && (
1050b843c749SSergey Zigachev 	    parser->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
1051b843c749SSergey Zigachev 	    parser->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
1052b843c749SSergey Zigachev 		return -EINVAL;
1053b843c749SSergey Zigachev 
1054b843c749SSergey Zigachev 	return amdgpu_ctx_wait_prev_fence(parser->ctx, parser->ring->idx);
1055b843c749SSergey Zigachev }
1056b843c749SSergey Zigachev 
amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser * p,struct amdgpu_cs_chunk * chunk)1057b843c749SSergey Zigachev static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
1058b843c749SSergey Zigachev 				       struct amdgpu_cs_chunk *chunk)
1059b843c749SSergey Zigachev {
1060b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1061b843c749SSergey Zigachev 	unsigned num_deps;
1062b843c749SSergey Zigachev 	int i, r;
1063b843c749SSergey Zigachev 	struct drm_amdgpu_cs_chunk_dep *deps;
1064b843c749SSergey Zigachev 
1065b843c749SSergey Zigachev 	deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1066b843c749SSergey Zigachev 	num_deps = chunk->length_dw * 4 /
1067b843c749SSergey Zigachev 		sizeof(struct drm_amdgpu_cs_chunk_dep);
1068b843c749SSergey Zigachev 
1069b843c749SSergey Zigachev 	for (i = 0; i < num_deps; ++i) {
1070b843c749SSergey Zigachev 		struct amdgpu_ring *ring;
1071b843c749SSergey Zigachev 		struct amdgpu_ctx *ctx;
1072b843c749SSergey Zigachev 		struct dma_fence *fence;
1073b843c749SSergey Zigachev 
1074b843c749SSergey Zigachev 		ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
1075b843c749SSergey Zigachev 		if (ctx == NULL)
1076b843c749SSergey Zigachev 			return -EINVAL;
1077b843c749SSergey Zigachev 
1078b843c749SSergey Zigachev 		r = amdgpu_queue_mgr_map(p->adev, &ctx->queue_mgr,
1079b843c749SSergey Zigachev 					 deps[i].ip_type,
1080b843c749SSergey Zigachev 					 deps[i].ip_instance,
1081b843c749SSergey Zigachev 					 deps[i].ring, &ring);
1082b843c749SSergey Zigachev 		if (r) {
1083b843c749SSergey Zigachev 			amdgpu_ctx_put(ctx);
1084b843c749SSergey Zigachev 			return r;
1085b843c749SSergey Zigachev 		}
1086b843c749SSergey Zigachev 
1087b843c749SSergey Zigachev 		fence = amdgpu_ctx_get_fence(ctx, ring,
1088b843c749SSergey Zigachev 					     deps[i].handle);
1089b843c749SSergey Zigachev 		if (IS_ERR(fence)) {
1090b843c749SSergey Zigachev 			r = PTR_ERR(fence);
1091b843c749SSergey Zigachev 			amdgpu_ctx_put(ctx);
1092b843c749SSergey Zigachev 			return r;
1093b843c749SSergey Zigachev 		} else if (fence) {
1094b843c749SSergey Zigachev 			r = amdgpu_sync_fence(p->adev, &p->job->sync, fence,
1095b843c749SSergey Zigachev 					true);
1096b843c749SSergey Zigachev 			dma_fence_put(fence);
1097b843c749SSergey Zigachev 			amdgpu_ctx_put(ctx);
1098b843c749SSergey Zigachev 			if (r)
1099b843c749SSergey Zigachev 				return r;
1100b843c749SSergey Zigachev 		}
1101b843c749SSergey Zigachev 	}
1102b843c749SSergey Zigachev 	return 0;
1103b843c749SSergey Zigachev }
1104b843c749SSergey Zigachev 
amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser * p,uint32_t handle)1105b843c749SSergey Zigachev static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
1106b843c749SSergey Zigachev 						 uint32_t handle)
1107b843c749SSergey Zigachev {
1108b843c749SSergey Zigachev 	int r;
1109b843c749SSergey Zigachev 	struct dma_fence *fence;
1110b843c749SSergey Zigachev 	r = drm_syncobj_find_fence(p->filp, handle, &fence);
1111b843c749SSergey Zigachev 	if (r)
1112b843c749SSergey Zigachev 		return r;
1113b843c749SSergey Zigachev 
1114b843c749SSergey Zigachev 	r = amdgpu_sync_fence(p->adev, &p->job->sync, fence, true);
1115b843c749SSergey Zigachev 	dma_fence_put(fence);
1116b843c749SSergey Zigachev 
1117b843c749SSergey Zigachev 	return r;
1118b843c749SSergey Zigachev }
1119b843c749SSergey Zigachev 
amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser * p,struct amdgpu_cs_chunk * chunk)1120b843c749SSergey Zigachev static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
1121b843c749SSergey Zigachev 					    struct amdgpu_cs_chunk *chunk)
1122b843c749SSergey Zigachev {
1123b843c749SSergey Zigachev 	unsigned num_deps;
1124b843c749SSergey Zigachev 	int i, r;
1125b843c749SSergey Zigachev 	struct drm_amdgpu_cs_chunk_sem *deps;
1126b843c749SSergey Zigachev 
1127b843c749SSergey Zigachev 	deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1128b843c749SSergey Zigachev 	num_deps = chunk->length_dw * 4 /
1129b843c749SSergey Zigachev 		sizeof(struct drm_amdgpu_cs_chunk_sem);
1130b843c749SSergey Zigachev 
1131b843c749SSergey Zigachev 	for (i = 0; i < num_deps; ++i) {
1132b843c749SSergey Zigachev 		r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
1133b843c749SSergey Zigachev 		if (r)
1134b843c749SSergey Zigachev 			return r;
1135b843c749SSergey Zigachev 	}
1136b843c749SSergey Zigachev 	return 0;
1137b843c749SSergey Zigachev }
1138b843c749SSergey Zigachev 
amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser * p,struct amdgpu_cs_chunk * chunk)1139b843c749SSergey Zigachev static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1140b843c749SSergey Zigachev 					     struct amdgpu_cs_chunk *chunk)
1141b843c749SSergey Zigachev {
1142b843c749SSergey Zigachev 	unsigned num_deps;
1143b843c749SSergey Zigachev 	int i;
1144b843c749SSergey Zigachev 	struct drm_amdgpu_cs_chunk_sem *deps;
1145b843c749SSergey Zigachev 	deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1146b843c749SSergey Zigachev 	num_deps = chunk->length_dw * 4 /
1147b843c749SSergey Zigachev 		sizeof(struct drm_amdgpu_cs_chunk_sem);
1148b843c749SSergey Zigachev 
1149b843c749SSergey Zigachev 	p->post_dep_syncobjs = kmalloc_array(num_deps,
1150b843c749SSergey Zigachev 					     sizeof(struct drm_syncobj *),
1151b843c749SSergey Zigachev 					     GFP_KERNEL);
1152b843c749SSergey Zigachev 	p->num_post_dep_syncobjs = 0;
1153b843c749SSergey Zigachev 
1154b843c749SSergey Zigachev 	if (!p->post_dep_syncobjs)
1155b843c749SSergey Zigachev 		return -ENOMEM;
1156b843c749SSergey Zigachev 
1157b843c749SSergey Zigachev 	for (i = 0; i < num_deps; ++i) {
1158b843c749SSergey Zigachev 		p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
1159b843c749SSergey Zigachev 		if (!p->post_dep_syncobjs[i])
1160b843c749SSergey Zigachev 			return -EINVAL;
1161b843c749SSergey Zigachev 		p->num_post_dep_syncobjs++;
1162b843c749SSergey Zigachev 	}
1163b843c749SSergey Zigachev 	return 0;
1164b843c749SSergey Zigachev }
1165b843c749SSergey Zigachev 
amdgpu_cs_dependencies(struct amdgpu_device * adev,struct amdgpu_cs_parser * p)1166b843c749SSergey Zigachev static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1167b843c749SSergey Zigachev 				  struct amdgpu_cs_parser *p)
1168b843c749SSergey Zigachev {
1169b843c749SSergey Zigachev 	int i, r;
1170b843c749SSergey Zigachev 
1171b843c749SSergey Zigachev 	for (i = 0; i < p->nchunks; ++i) {
1172b843c749SSergey Zigachev 		struct amdgpu_cs_chunk *chunk;
1173b843c749SSergey Zigachev 
1174b843c749SSergey Zigachev 		chunk = &p->chunks[i];
1175b843c749SSergey Zigachev 
1176b843c749SSergey Zigachev 		if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
1177b843c749SSergey Zigachev 			r = amdgpu_cs_process_fence_dep(p, chunk);
1178b843c749SSergey Zigachev 			if (r)
1179b843c749SSergey Zigachev 				return r;
1180b843c749SSergey Zigachev 		} else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
1181b843c749SSergey Zigachev 			r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1182b843c749SSergey Zigachev 			if (r)
1183b843c749SSergey Zigachev 				return r;
1184b843c749SSergey Zigachev 		} else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
1185b843c749SSergey Zigachev 			r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1186b843c749SSergey Zigachev 			if (r)
1187b843c749SSergey Zigachev 				return r;
1188b843c749SSergey Zigachev 		}
1189b843c749SSergey Zigachev 	}
1190b843c749SSergey Zigachev 
1191b843c749SSergey Zigachev 	return 0;
1192b843c749SSergey Zigachev }
1193b843c749SSergey Zigachev 
amdgpu_cs_post_dependencies(struct amdgpu_cs_parser * p)1194b843c749SSergey Zigachev static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1195b843c749SSergey Zigachev {
1196b843c749SSergey Zigachev 	int i;
1197b843c749SSergey Zigachev 
1198b843c749SSergey Zigachev 	for (i = 0; i < p->num_post_dep_syncobjs; ++i)
1199b843c749SSergey Zigachev 		drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
1200b843c749SSergey Zigachev }
1201b843c749SSergey Zigachev 
amdgpu_cs_submit(struct amdgpu_cs_parser * p,union drm_amdgpu_cs * cs)1202b843c749SSergey Zigachev static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1203b843c749SSergey Zigachev 			    union drm_amdgpu_cs *cs)
1204b843c749SSergey Zigachev {
1205b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1206b843c749SSergey Zigachev 	struct amdgpu_ring *ring = p->ring;
1207b843c749SSergey Zigachev 	struct drm_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
1208b843c749SSergey Zigachev 	enum drm_sched_priority priority;
1209b843c749SSergey Zigachev 	struct amdgpu_bo_list_entry *e;
1210b843c749SSergey Zigachev 	struct amdgpu_job *job;
1211b843c749SSergey Zigachev 	uint64_t seq;
1212b843c749SSergey Zigachev 
1213b843c749SSergey Zigachev 	int r;
1214b843c749SSergey Zigachev 
1215b843c749SSergey Zigachev 	job = p->job;
1216b843c749SSergey Zigachev 	p->job = NULL;
1217b843c749SSergey Zigachev 
1218b843c749SSergey Zigachev 	r = drm_sched_job_init(&job->base, entity, p->filp);
1219b843c749SSergey Zigachev 	if (r)
1220b843c749SSergey Zigachev 		goto error_unlock;
1221b843c749SSergey Zigachev 
1222b843c749SSergey Zigachev 	/* No memory allocation is allowed while holding the mn lock */
1223b843c749SSergey Zigachev 	amdgpu_mn_lock(p->mn);
1224b843c749SSergey Zigachev 	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
1225b843c749SSergey Zigachev 		struct amdgpu_bo *bo = e->robj;
1226b843c749SSergey Zigachev 
1227b843c749SSergey Zigachev 		if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
1228b843c749SSergey Zigachev 			r = -ERESTARTSYS;
1229b843c749SSergey Zigachev 			goto error_abort;
1230b843c749SSergey Zigachev 		}
1231b843c749SSergey Zigachev 	}
1232b843c749SSergey Zigachev 
1233b843c749SSergey Zigachev 	job->owner = p->filp;
1234b843c749SSergey Zigachev 	p->fence = dma_fence_get(&job->base.s_fence->finished);
1235b843c749SSergey Zigachev 
1236b843c749SSergey Zigachev 	r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
1237b843c749SSergey Zigachev 	if (r) {
1238b843c749SSergey Zigachev 		dma_fence_put(p->fence);
1239b843c749SSergey Zigachev 		dma_fence_put(&job->base.s_fence->finished);
1240b843c749SSergey Zigachev 		amdgpu_job_free(job);
1241b843c749SSergey Zigachev 		amdgpu_mn_unlock(p->mn);
1242b843c749SSergey Zigachev 		return r;
1243b843c749SSergey Zigachev 	}
1244b843c749SSergey Zigachev 
1245b843c749SSergey Zigachev 	amdgpu_cs_post_dependencies(p);
1246b843c749SSergey Zigachev 
1247b843c749SSergey Zigachev 	if ((job->preamble_status & AMDGPU_PREAMBLE_IB_PRESENT) &&
1248b843c749SSergey Zigachev 	    !p->ctx->preamble_presented) {
1249b843c749SSergey Zigachev 		job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
1250b843c749SSergey Zigachev 		p->ctx->preamble_presented = true;
1251b843c749SSergey Zigachev 	}
1252b843c749SSergey Zigachev 
1253b843c749SSergey Zigachev 	cs->out.handle = seq;
1254b843c749SSergey Zigachev 	job->uf_sequence = seq;
1255b843c749SSergey Zigachev 
1256b843c749SSergey Zigachev 	amdgpu_job_free_resources(job);
1257b843c749SSergey Zigachev 
1258*78973132SSergey Zigachev #if 0
1259b843c749SSergey Zigachev 	trace_amdgpu_cs_ioctl(job);
1260*78973132SSergey Zigachev #endif
1261b843c749SSergey Zigachev 	amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->ticket);
1262b843c749SSergey Zigachev 	priority = job->base.s_priority;
1263b843c749SSergey Zigachev 	drm_sched_entity_push_job(&job->base, entity);
1264b843c749SSergey Zigachev 
1265b843c749SSergey Zigachev 	ring = to_amdgpu_ring(entity->rq->sched);
1266b843c749SSergey Zigachev 	amdgpu_ring_priority_get(ring, priority);
1267b843c749SSergey Zigachev 
1268b843c749SSergey Zigachev 	ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1269b843c749SSergey Zigachev 	amdgpu_mn_unlock(p->mn);
1270b843c749SSergey Zigachev 
1271b843c749SSergey Zigachev 	return 0;
1272b843c749SSergey Zigachev 
1273b843c749SSergey Zigachev error_abort:
1274b843c749SSergey Zigachev 	dma_fence_put(&job->base.s_fence->finished);
1275b843c749SSergey Zigachev 	job->base.s_fence = NULL;
1276b843c749SSergey Zigachev 	amdgpu_mn_unlock(p->mn);
1277b843c749SSergey Zigachev 
1278b843c749SSergey Zigachev error_unlock:
1279b843c749SSergey Zigachev 	amdgpu_job_free(job);
1280b843c749SSergey Zigachev 	return r;
1281b843c749SSergey Zigachev }
1282b843c749SSergey Zigachev 
amdgpu_cs_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1283b843c749SSergey Zigachev int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1284b843c749SSergey Zigachev {
1285b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1286b843c749SSergey Zigachev 	union drm_amdgpu_cs *cs = data;
1287b843c749SSergey Zigachev 	struct amdgpu_cs_parser parser = {};
1288b843c749SSergey Zigachev 	bool reserved_buffers = false;
1289*78973132SSergey Zigachev 	int r;
1290b843c749SSergey Zigachev 
1291b843c749SSergey Zigachev 	if (!adev->accel_working)
1292b843c749SSergey Zigachev 		return -EBUSY;
1293b843c749SSergey Zigachev 
1294b843c749SSergey Zigachev 	parser.adev = adev;
1295b843c749SSergey Zigachev 	parser.filp = filp;
1296b843c749SSergey Zigachev 
1297b843c749SSergey Zigachev 	r = amdgpu_cs_parser_init(&parser, data);
1298b843c749SSergey Zigachev 	if (r) {
1299b843c749SSergey Zigachev 		DRM_ERROR("Failed to initialize parser !\n");
1300b843c749SSergey Zigachev 		goto out;
1301b843c749SSergey Zigachev 	}
1302b843c749SSergey Zigachev 
1303b843c749SSergey Zigachev 	r = amdgpu_cs_ib_fill(adev, &parser);
1304b843c749SSergey Zigachev 	if (r)
1305b843c749SSergey Zigachev 		goto out;
1306b843c749SSergey Zigachev 
1307b843c749SSergey Zigachev 	r = amdgpu_cs_parser_bos(&parser, data);
1308b843c749SSergey Zigachev 	if (r) {
1309b843c749SSergey Zigachev 		if (r == -ENOMEM)
1310b843c749SSergey Zigachev 			DRM_ERROR("Not enough memory for command submission!\n");
1311b843c749SSergey Zigachev 		else if (r != -ERESTARTSYS)
1312b843c749SSergey Zigachev 			DRM_ERROR("Failed to process the buffer list %d!\n", r);
1313b843c749SSergey Zigachev 		goto out;
1314b843c749SSergey Zigachev 	}
1315b843c749SSergey Zigachev 
1316b843c749SSergey Zigachev 	reserved_buffers = true;
1317b843c749SSergey Zigachev 
1318b843c749SSergey Zigachev 	r = amdgpu_cs_dependencies(adev, &parser);
1319b843c749SSergey Zigachev 	if (r) {
1320b843c749SSergey Zigachev 		DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1321b843c749SSergey Zigachev 		goto out;
1322b843c749SSergey Zigachev 	}
1323b843c749SSergey Zigachev 
1324*78973132SSergey Zigachev #if 0
1325b843c749SSergey Zigachev 	for (i = 0; i < parser.job->num_ibs; i++)
1326b843c749SSergey Zigachev 		trace_amdgpu_cs(&parser, i);
1327*78973132SSergey Zigachev #endif
1328b843c749SSergey Zigachev 
1329b843c749SSergey Zigachev 	r = amdgpu_cs_ib_vm_chunk(adev, &parser);
1330b843c749SSergey Zigachev 	if (r)
1331b843c749SSergey Zigachev 		goto out;
1332b843c749SSergey Zigachev 
1333b843c749SSergey Zigachev 	r = amdgpu_cs_submit(&parser, cs);
1334b843c749SSergey Zigachev 
1335b843c749SSergey Zigachev out:
1336b843c749SSergey Zigachev 	amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
1337b843c749SSergey Zigachev 	return r;
1338b843c749SSergey Zigachev }
1339b843c749SSergey Zigachev 
1340b843c749SSergey Zigachev /**
1341b843c749SSergey Zigachev  * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1342b843c749SSergey Zigachev  *
1343b843c749SSergey Zigachev  * @dev: drm device
1344b843c749SSergey Zigachev  * @data: data from userspace
1345b843c749SSergey Zigachev  * @filp: file private
1346b843c749SSergey Zigachev  *
1347b843c749SSergey Zigachev  * Wait for the command submission identified by handle to finish.
1348b843c749SSergey Zigachev  */
amdgpu_cs_wait_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1349b843c749SSergey Zigachev int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1350b843c749SSergey Zigachev 			 struct drm_file *filp)
1351b843c749SSergey Zigachev {
1352b843c749SSergey Zigachev 	union drm_amdgpu_wait_cs *wait = data;
1353b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1354b843c749SSergey Zigachev 	unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
1355b843c749SSergey Zigachev 	struct amdgpu_ring *ring = NULL;
1356b843c749SSergey Zigachev 	struct amdgpu_ctx *ctx;
1357b843c749SSergey Zigachev 	struct dma_fence *fence;
1358b843c749SSergey Zigachev 	long r;
1359b843c749SSergey Zigachev 
1360b843c749SSergey Zigachev 	ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1361b843c749SSergey Zigachev 	if (ctx == NULL)
1362b843c749SSergey Zigachev 		return -EINVAL;
1363b843c749SSergey Zigachev 
1364b843c749SSergey Zigachev 	r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr,
1365b843c749SSergey Zigachev 				 wait->in.ip_type, wait->in.ip_instance,
1366b843c749SSergey Zigachev 				 wait->in.ring, &ring);
1367b843c749SSergey Zigachev 	if (r) {
1368b843c749SSergey Zigachev 		amdgpu_ctx_put(ctx);
1369b843c749SSergey Zigachev 		return r;
1370b843c749SSergey Zigachev 	}
1371b843c749SSergey Zigachev 
1372b843c749SSergey Zigachev 	fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1373b843c749SSergey Zigachev 	if (IS_ERR(fence))
1374b843c749SSergey Zigachev 		r = PTR_ERR(fence);
1375b843c749SSergey Zigachev 	else if (fence) {
1376b843c749SSergey Zigachev 		r = dma_fence_wait_timeout(fence, true, timeout);
1377b843c749SSergey Zigachev 		if (r > 0 && fence->error)
1378b843c749SSergey Zigachev 			r = fence->error;
1379b843c749SSergey Zigachev 		dma_fence_put(fence);
1380b843c749SSergey Zigachev 	} else
1381b843c749SSergey Zigachev 		r = 1;
1382b843c749SSergey Zigachev 
1383b843c749SSergey Zigachev 	amdgpu_ctx_put(ctx);
1384b843c749SSergey Zigachev 	if (r < 0)
1385b843c749SSergey Zigachev 		return r;
1386b843c749SSergey Zigachev 
1387b843c749SSergey Zigachev 	memset(wait, 0, sizeof(*wait));
1388b843c749SSergey Zigachev 	wait->out.status = (r == 0);
1389b843c749SSergey Zigachev 
1390b843c749SSergey Zigachev 	return 0;
1391b843c749SSergey Zigachev }
1392b843c749SSergey Zigachev 
1393b843c749SSergey Zigachev /**
1394b843c749SSergey Zigachev  * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1395b843c749SSergey Zigachev  *
1396b843c749SSergey Zigachev  * @adev: amdgpu device
1397b843c749SSergey Zigachev  * @filp: file private
1398b843c749SSergey Zigachev  * @user: drm_amdgpu_fence copied from user space
1399b843c749SSergey Zigachev  */
amdgpu_cs_get_fence(struct amdgpu_device * adev,struct drm_file * filp,struct drm_amdgpu_fence * user)1400b843c749SSergey Zigachev static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1401b843c749SSergey Zigachev 					     struct drm_file *filp,
1402b843c749SSergey Zigachev 					     struct drm_amdgpu_fence *user)
1403b843c749SSergey Zigachev {
1404b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
1405b843c749SSergey Zigachev 	struct amdgpu_ctx *ctx;
1406b843c749SSergey Zigachev 	struct dma_fence *fence;
1407b843c749SSergey Zigachev 	int r;
1408b843c749SSergey Zigachev 
1409b843c749SSergey Zigachev 	ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1410b843c749SSergey Zigachev 	if (ctx == NULL)
1411b843c749SSergey Zigachev 		return ERR_PTR(-EINVAL);
1412b843c749SSergey Zigachev 
1413b843c749SSergey Zigachev 	r = amdgpu_queue_mgr_map(adev, &ctx->queue_mgr, user->ip_type,
1414b843c749SSergey Zigachev 				 user->ip_instance, user->ring, &ring);
1415b843c749SSergey Zigachev 	if (r) {
1416b843c749SSergey Zigachev 		amdgpu_ctx_put(ctx);
1417b843c749SSergey Zigachev 		return ERR_PTR(r);
1418b843c749SSergey Zigachev 	}
1419b843c749SSergey Zigachev 
1420b843c749SSergey Zigachev 	fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1421b843c749SSergey Zigachev 	amdgpu_ctx_put(ctx);
1422b843c749SSergey Zigachev 
1423b843c749SSergey Zigachev 	return fence;
1424b843c749SSergey Zigachev }
1425b843c749SSergey Zigachev 
amdgpu_cs_fence_to_handle_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1426b843c749SSergey Zigachev int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1427b843c749SSergey Zigachev 				    struct drm_file *filp)
1428b843c749SSergey Zigachev {
1429b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1430b843c749SSergey Zigachev 	union drm_amdgpu_fence_to_handle *info = data;
1431b843c749SSergey Zigachev 	struct dma_fence *fence;
1432b843c749SSergey Zigachev 	struct drm_syncobj *syncobj;
1433b843c749SSergey Zigachev 	struct sync_file *sync_file;
1434b843c749SSergey Zigachev 	int fd, r;
1435b843c749SSergey Zigachev 
1436b843c749SSergey Zigachev 	fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1437b843c749SSergey Zigachev 	if (IS_ERR(fence))
1438b843c749SSergey Zigachev 		return PTR_ERR(fence);
1439b843c749SSergey Zigachev 
1440b843c749SSergey Zigachev 	switch (info->in.what) {
1441b843c749SSergey Zigachev 	case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1442b843c749SSergey Zigachev 		r = drm_syncobj_create(&syncobj, 0, fence);
1443b843c749SSergey Zigachev 		dma_fence_put(fence);
1444b843c749SSergey Zigachev 		if (r)
1445b843c749SSergey Zigachev 			return r;
1446b843c749SSergey Zigachev 		r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1447b843c749SSergey Zigachev 		drm_syncobj_put(syncobj);
1448b843c749SSergey Zigachev 		return r;
1449b843c749SSergey Zigachev 
1450b843c749SSergey Zigachev 	case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1451b843c749SSergey Zigachev 		r = drm_syncobj_create(&syncobj, 0, fence);
1452b843c749SSergey Zigachev 		dma_fence_put(fence);
1453b843c749SSergey Zigachev 		if (r)
1454b843c749SSergey Zigachev 			return r;
1455b843c749SSergey Zigachev 		r = drm_syncobj_get_fd(syncobj, (int*)&info->out.handle);
1456b843c749SSergey Zigachev 		drm_syncobj_put(syncobj);
1457b843c749SSergey Zigachev 		return r;
1458b843c749SSergey Zigachev 
1459b843c749SSergey Zigachev 	case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1460b843c749SSergey Zigachev 		fd = get_unused_fd_flags(O_CLOEXEC);
1461b843c749SSergey Zigachev 		if (fd < 0) {
1462b843c749SSergey Zigachev 			dma_fence_put(fence);
1463b843c749SSergey Zigachev 			return fd;
1464b843c749SSergey Zigachev 		}
1465b843c749SSergey Zigachev 
1466b843c749SSergey Zigachev 		sync_file = sync_file_create(fence);
1467b843c749SSergey Zigachev 		dma_fence_put(fence);
1468b843c749SSergey Zigachev 		if (!sync_file) {
1469b843c749SSergey Zigachev 			put_unused_fd(fd);
1470b843c749SSergey Zigachev 			return -ENOMEM;
1471b843c749SSergey Zigachev 		}
1472b843c749SSergey Zigachev 
1473b843c749SSergey Zigachev 		fd_install(fd, sync_file->file);
1474b843c749SSergey Zigachev 		info->out.handle = fd;
1475b843c749SSergey Zigachev 		return 0;
1476b843c749SSergey Zigachev 
1477b843c749SSergey Zigachev 	default:
1478b843c749SSergey Zigachev 		return -EINVAL;
1479b843c749SSergey Zigachev 	}
1480b843c749SSergey Zigachev }
1481b843c749SSergey Zigachev 
1482b843c749SSergey Zigachev /**
1483b843c749SSergey Zigachev  * amdgpu_cs_wait_all_fence - wait on all fences to signal
1484b843c749SSergey Zigachev  *
1485b843c749SSergey Zigachev  * @adev: amdgpu device
1486b843c749SSergey Zigachev  * @filp: file private
1487b843c749SSergey Zigachev  * @wait: wait parameters
1488b843c749SSergey Zigachev  * @fences: array of drm_amdgpu_fence
1489b843c749SSergey Zigachev  */
amdgpu_cs_wait_all_fences(struct amdgpu_device * adev,struct drm_file * filp,union drm_amdgpu_wait_fences * wait,struct drm_amdgpu_fence * fences)1490b843c749SSergey Zigachev static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1491b843c749SSergey Zigachev 				     struct drm_file *filp,
1492b843c749SSergey Zigachev 				     union drm_amdgpu_wait_fences *wait,
1493b843c749SSergey Zigachev 				     struct drm_amdgpu_fence *fences)
1494b843c749SSergey Zigachev {
1495b843c749SSergey Zigachev 	uint32_t fence_count = wait->in.fence_count;
1496b843c749SSergey Zigachev 	unsigned int i;
1497b843c749SSergey Zigachev 	long r = 1;
1498b843c749SSergey Zigachev 
1499b843c749SSergey Zigachev 	for (i = 0; i < fence_count; i++) {
1500b843c749SSergey Zigachev 		struct dma_fence *fence;
1501b843c749SSergey Zigachev 		unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1502b843c749SSergey Zigachev 
1503b843c749SSergey Zigachev 		fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1504b843c749SSergey Zigachev 		if (IS_ERR(fence))
1505b843c749SSergey Zigachev 			return PTR_ERR(fence);
1506b843c749SSergey Zigachev 		else if (!fence)
1507b843c749SSergey Zigachev 			continue;
1508b843c749SSergey Zigachev 
1509b843c749SSergey Zigachev 		r = dma_fence_wait_timeout(fence, true, timeout);
1510b843c749SSergey Zigachev 		dma_fence_put(fence);
1511b843c749SSergey Zigachev 		if (r < 0)
1512b843c749SSergey Zigachev 			return r;
1513b843c749SSergey Zigachev 
1514b843c749SSergey Zigachev 		if (r == 0)
1515b843c749SSergey Zigachev 			break;
1516b843c749SSergey Zigachev 
1517b843c749SSergey Zigachev 		if (fence->error)
1518b843c749SSergey Zigachev 			return fence->error;
1519b843c749SSergey Zigachev 	}
1520b843c749SSergey Zigachev 
1521b843c749SSergey Zigachev 	memset(wait, 0, sizeof(*wait));
1522b843c749SSergey Zigachev 	wait->out.status = (r > 0);
1523b843c749SSergey Zigachev 
1524b843c749SSergey Zigachev 	return 0;
1525b843c749SSergey Zigachev }
1526b843c749SSergey Zigachev 
1527b843c749SSergey Zigachev /**
1528b843c749SSergey Zigachev  * amdgpu_cs_wait_any_fence - wait on any fence to signal
1529b843c749SSergey Zigachev  *
1530b843c749SSergey Zigachev  * @adev: amdgpu device
1531b843c749SSergey Zigachev  * @filp: file private
1532b843c749SSergey Zigachev  * @wait: wait parameters
1533b843c749SSergey Zigachev  * @fences: array of drm_amdgpu_fence
1534b843c749SSergey Zigachev  */
amdgpu_cs_wait_any_fence(struct amdgpu_device * adev,struct drm_file * filp,union drm_amdgpu_wait_fences * wait,struct drm_amdgpu_fence * fences)1535b843c749SSergey Zigachev static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1536b843c749SSergey Zigachev 				    struct drm_file *filp,
1537b843c749SSergey Zigachev 				    union drm_amdgpu_wait_fences *wait,
1538b843c749SSergey Zigachev 				    struct drm_amdgpu_fence *fences)
1539b843c749SSergey Zigachev {
1540b843c749SSergey Zigachev 	unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1541b843c749SSergey Zigachev 	uint32_t fence_count = wait->in.fence_count;
1542b843c749SSergey Zigachev 	uint32_t first = ~0;
1543b843c749SSergey Zigachev 	struct dma_fence **array;
1544b843c749SSergey Zigachev 	unsigned int i;
1545b843c749SSergey Zigachev 	long r;
1546b843c749SSergey Zigachev 
1547b843c749SSergey Zigachev 	/* Prepare the fence array */
1548b843c749SSergey Zigachev 	array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1549b843c749SSergey Zigachev 
1550b843c749SSergey Zigachev 	if (array == NULL)
1551b843c749SSergey Zigachev 		return -ENOMEM;
1552b843c749SSergey Zigachev 
1553b843c749SSergey Zigachev 	for (i = 0; i < fence_count; i++) {
1554b843c749SSergey Zigachev 		struct dma_fence *fence;
1555b843c749SSergey Zigachev 
1556b843c749SSergey Zigachev 		fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1557b843c749SSergey Zigachev 		if (IS_ERR(fence)) {
1558b843c749SSergey Zigachev 			r = PTR_ERR(fence);
1559b843c749SSergey Zigachev 			goto err_free_fence_array;
1560b843c749SSergey Zigachev 		} else if (fence) {
1561b843c749SSergey Zigachev 			array[i] = fence;
1562b843c749SSergey Zigachev 		} else { /* NULL, the fence has been already signaled */
1563b843c749SSergey Zigachev 			r = 1;
1564b843c749SSergey Zigachev 			first = i;
1565b843c749SSergey Zigachev 			goto out;
1566b843c749SSergey Zigachev 		}
1567b843c749SSergey Zigachev 	}
1568b843c749SSergey Zigachev 
1569b843c749SSergey Zigachev 	r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1570b843c749SSergey Zigachev 				       &first);
1571b843c749SSergey Zigachev 	if (r < 0)
1572b843c749SSergey Zigachev 		goto err_free_fence_array;
1573b843c749SSergey Zigachev 
1574b843c749SSergey Zigachev out:
1575b843c749SSergey Zigachev 	memset(wait, 0, sizeof(*wait));
1576b843c749SSergey Zigachev 	wait->out.status = (r > 0);
1577b843c749SSergey Zigachev 	wait->out.first_signaled = first;
1578b843c749SSergey Zigachev 
1579b843c749SSergey Zigachev 	if (first < fence_count && array[first])
1580b843c749SSergey Zigachev 		r = array[first]->error;
1581b843c749SSergey Zigachev 	else
1582b843c749SSergey Zigachev 		r = 0;
1583b843c749SSergey Zigachev 
1584b843c749SSergey Zigachev err_free_fence_array:
1585b843c749SSergey Zigachev 	for (i = 0; i < fence_count; i++)
1586b843c749SSergey Zigachev 		dma_fence_put(array[i]);
1587b843c749SSergey Zigachev 	kfree(array);
1588b843c749SSergey Zigachev 
1589b843c749SSergey Zigachev 	return r;
1590b843c749SSergey Zigachev }
1591b843c749SSergey Zigachev 
1592b843c749SSergey Zigachev /**
1593b843c749SSergey Zigachev  * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1594b843c749SSergey Zigachev  *
1595b843c749SSergey Zigachev  * @dev: drm device
1596b843c749SSergey Zigachev  * @data: data from userspace
1597b843c749SSergey Zigachev  * @filp: file private
1598b843c749SSergey Zigachev  */
amdgpu_cs_wait_fences_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1599b843c749SSergey Zigachev int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1600b843c749SSergey Zigachev 				struct drm_file *filp)
1601b843c749SSergey Zigachev {
1602b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
1603b843c749SSergey Zigachev 	union drm_amdgpu_wait_fences *wait = data;
1604b843c749SSergey Zigachev 	uint32_t fence_count = wait->in.fence_count;
1605b843c749SSergey Zigachev 	struct drm_amdgpu_fence *fences_user;
1606b843c749SSergey Zigachev 	struct drm_amdgpu_fence *fences;
1607b843c749SSergey Zigachev 	int r;
1608b843c749SSergey Zigachev 
1609b843c749SSergey Zigachev 	/* Get the fences from userspace */
1610b843c749SSergey Zigachev 	fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1611b843c749SSergey Zigachev 			GFP_KERNEL);
1612b843c749SSergey Zigachev 	if (fences == NULL)
1613b843c749SSergey Zigachev 		return -ENOMEM;
1614b843c749SSergey Zigachev 
1615b843c749SSergey Zigachev 	fences_user = u64_to_user_ptr(wait->in.fences);
1616b843c749SSergey Zigachev 	if (copy_from_user(fences, fences_user,
1617b843c749SSergey Zigachev 		sizeof(struct drm_amdgpu_fence) * fence_count)) {
1618b843c749SSergey Zigachev 		r = -EFAULT;
1619b843c749SSergey Zigachev 		goto err_free_fences;
1620b843c749SSergey Zigachev 	}
1621b843c749SSergey Zigachev 
1622b843c749SSergey Zigachev 	if (wait->in.wait_all)
1623b843c749SSergey Zigachev 		r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1624b843c749SSergey Zigachev 	else
1625b843c749SSergey Zigachev 		r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1626b843c749SSergey Zigachev 
1627b843c749SSergey Zigachev err_free_fences:
1628b843c749SSergey Zigachev 	kfree(fences);
1629b843c749SSergey Zigachev 
1630b843c749SSergey Zigachev 	return r;
1631b843c749SSergey Zigachev }
1632b843c749SSergey Zigachev 
1633b843c749SSergey Zigachev /**
1634b843c749SSergey Zigachev  * amdgpu_cs_find_bo_va - find bo_va for VM address
1635b843c749SSergey Zigachev  *
1636b843c749SSergey Zigachev  * @parser: command submission parser context
1637b843c749SSergey Zigachev  * @addr: VM address
1638b843c749SSergey Zigachev  * @bo: resulting BO of the mapping found
1639b843c749SSergey Zigachev  *
1640b843c749SSergey Zigachev  * Search the buffer objects in the command submission context for a certain
1641b843c749SSergey Zigachev  * virtual memory address. Returns allocation structure when found, NULL
1642b843c749SSergey Zigachev  * otherwise.
1643b843c749SSergey Zigachev  */
amdgpu_cs_find_mapping(struct amdgpu_cs_parser * parser,uint64_t addr,struct amdgpu_bo ** bo,struct amdgpu_bo_va_mapping ** map)1644b843c749SSergey Zigachev int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1645b843c749SSergey Zigachev 			   uint64_t addr, struct amdgpu_bo **bo,
1646b843c749SSergey Zigachev 			   struct amdgpu_bo_va_mapping **map)
1647b843c749SSergey Zigachev {
1648b843c749SSergey Zigachev 	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1649b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = { false, false };
1650b843c749SSergey Zigachev 	struct amdgpu_vm *vm = &fpriv->vm;
1651b843c749SSergey Zigachev 	struct amdgpu_bo_va_mapping *mapping;
1652b843c749SSergey Zigachev 	int r;
1653b843c749SSergey Zigachev 
1654b843c749SSergey Zigachev 	addr /= AMDGPU_GPU_PAGE_SIZE;
1655b843c749SSergey Zigachev 
1656b843c749SSergey Zigachev 	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1657b843c749SSergey Zigachev 	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1658b843c749SSergey Zigachev 		return -EINVAL;
1659b843c749SSergey Zigachev 
1660b843c749SSergey Zigachev 	*bo = mapping->bo_va->base.bo;
1661b843c749SSergey Zigachev 	*map = mapping;
1662b843c749SSergey Zigachev 
1663b843c749SSergey Zigachev 	/* Double check that the BO is reserved by this CS */
1664b843c749SSergey Zigachev 	if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
1665b843c749SSergey Zigachev 		return -EINVAL;
1666b843c749SSergey Zigachev 
1667b843c749SSergey Zigachev 	if (!((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
1668b843c749SSergey Zigachev 		(*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1669b843c749SSergey Zigachev 		amdgpu_bo_placement_from_domain(*bo, (*bo)->allowed_domains);
1670b843c749SSergey Zigachev 		r = ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, &ctx);
1671b843c749SSergey Zigachev 		if (r)
1672b843c749SSergey Zigachev 			return r;
1673b843c749SSergey Zigachev 	}
1674b843c749SSergey Zigachev 
1675b843c749SSergey Zigachev 	return amdgpu_ttm_alloc_gart(&(*bo)->tbo);
1676b843c749SSergey Zigachev }
1677