xref: /linux/drivers/gpu/drm/nouveau/nouveau_exec.c (revision 9a0c32d698c1d0c4a6f5642ac017da31febad1eb)
1b88baab8SDanilo Krummrich // SPDX-License-Identifier: MIT
2b88baab8SDanilo Krummrich 
3b88baab8SDanilo Krummrich #include "nouveau_drv.h"
4b88baab8SDanilo Krummrich #include "nouveau_gem.h"
5b88baab8SDanilo Krummrich #include "nouveau_mem.h"
6b88baab8SDanilo Krummrich #include "nouveau_dma.h"
7b88baab8SDanilo Krummrich #include "nouveau_exec.h"
8b88baab8SDanilo Krummrich #include "nouveau_abi16.h"
9b88baab8SDanilo Krummrich #include "nouveau_chan.h"
10b88baab8SDanilo Krummrich #include "nouveau_sched.h"
11b88baab8SDanilo Krummrich #include "nouveau_uvmm.h"
12b88baab8SDanilo Krummrich 
13b88baab8SDanilo Krummrich /**
14b88baab8SDanilo Krummrich  * DOC: Overview
15b88baab8SDanilo Krummrich  *
16b88baab8SDanilo Krummrich  * Nouveau's VM_BIND / EXEC UAPI consists of three ioctls: DRM_NOUVEAU_VM_INIT,
17b88baab8SDanilo Krummrich  * DRM_NOUVEAU_VM_BIND and DRM_NOUVEAU_EXEC.
18b88baab8SDanilo Krummrich  *
19b88baab8SDanilo Krummrich  * In order to use the UAPI firstly a user client must initialize the VA space
20b88baab8SDanilo Krummrich  * using the DRM_NOUVEAU_VM_INIT ioctl specifying which region of the VA space
21b88baab8SDanilo Krummrich  * should be managed by the kernel and which by the UMD.
22b88baab8SDanilo Krummrich  *
23b88baab8SDanilo Krummrich  * The DRM_NOUVEAU_VM_BIND ioctl provides clients an interface to manage the
24b88baab8SDanilo Krummrich  * userspace-managable portion of the VA space. It provides operations to map
25b88baab8SDanilo Krummrich  * and unmap memory. Mappings may be flagged as sparse. Sparse mappings are not
26b88baab8SDanilo Krummrich  * backed by a GEM object and the kernel will ignore GEM handles provided
27b88baab8SDanilo Krummrich  * alongside a sparse mapping.
28b88baab8SDanilo Krummrich  *
29b88baab8SDanilo Krummrich  * Userspace may request memory backed mappings either within or outside of the
30b88baab8SDanilo Krummrich  * bounds (but not crossing those bounds) of a previously mapped sparse
31b88baab8SDanilo Krummrich  * mapping. Subsequently requested memory backed mappings within a sparse
32b88baab8SDanilo Krummrich  * mapping will take precedence over the corresponding range of the sparse
33b88baab8SDanilo Krummrich  * mapping. If such memory backed mappings are unmapped the kernel will make
34b88baab8SDanilo Krummrich  * sure that the corresponding sparse mapping will take their place again.
35b88baab8SDanilo Krummrich  * Requests to unmap a sparse mapping that still contains memory backed mappings
36b88baab8SDanilo Krummrich  * will result in those memory backed mappings being unmapped first.
37b88baab8SDanilo Krummrich  *
38b88baab8SDanilo Krummrich  * Unmap requests are not bound to the range of existing mappings and can even
39b88baab8SDanilo Krummrich  * overlap the bounds of sparse mappings. For such a request the kernel will
40b88baab8SDanilo Krummrich  * make sure to unmap all memory backed mappings within the given range,
41b88baab8SDanilo Krummrich  * splitting up memory backed mappings which are only partially contained
42b88baab8SDanilo Krummrich  * within the given range. Unmap requests with the sparse flag set must match
43b88baab8SDanilo Krummrich  * the range of a previously mapped sparse mapping exactly though.
44b88baab8SDanilo Krummrich  *
45b88baab8SDanilo Krummrich  * While the kernel generally permits arbitrary sequences and ranges of memory
46b88baab8SDanilo Krummrich  * backed mappings being mapped and unmapped, either within a single or multiple
47b88baab8SDanilo Krummrich  * VM_BIND ioctl calls, there are some restrictions for sparse mappings.
48b88baab8SDanilo Krummrich  *
49b88baab8SDanilo Krummrich  * The kernel does not permit to:
50b88baab8SDanilo Krummrich  *   - unmap non-existent sparse mappings
51b88baab8SDanilo Krummrich  *   - unmap a sparse mapping and map a new sparse mapping overlapping the range
52b88baab8SDanilo Krummrich  *     of the previously unmapped sparse mapping within the same VM_BIND ioctl
53b88baab8SDanilo Krummrich  *   - unmap a sparse mapping and map new memory backed mappings overlapping the
54b88baab8SDanilo Krummrich  *     range of the previously unmapped sparse mapping within the same VM_BIND
55b88baab8SDanilo Krummrich  *     ioctl
56b88baab8SDanilo Krummrich  *
57b88baab8SDanilo Krummrich  * When using the VM_BIND ioctl to request the kernel to map memory to a given
58b88baab8SDanilo Krummrich  * virtual address in the GPU's VA space there is no guarantee that the actual
59b88baab8SDanilo Krummrich  * mappings are created in the GPU's MMU. If the given memory is swapped out
60b88baab8SDanilo Krummrich  * at the time the bind operation is executed the kernel will stash the mapping
61b88baab8SDanilo Krummrich  * details into it's internal alloctor and create the actual MMU mappings once
62b88baab8SDanilo Krummrich  * the memory is swapped back in. While this is transparent for userspace, it is
63b88baab8SDanilo Krummrich  * guaranteed that all the backing memory is swapped back in and all the memory
64b88baab8SDanilo Krummrich  * mappings, as requested by userspace previously, are actually mapped once the
65b88baab8SDanilo Krummrich  * DRM_NOUVEAU_EXEC ioctl is called to submit an exec job.
66b88baab8SDanilo Krummrich  *
67b88baab8SDanilo Krummrich  * A VM_BIND job can be executed either synchronously or asynchronously. If
68b88baab8SDanilo Krummrich  * exectued asynchronously, userspace may provide a list of syncobjs this job
69b88baab8SDanilo Krummrich  * will wait for and/or a list of syncobj the kernel will signal once the
70b88baab8SDanilo Krummrich  * VM_BIND job finished execution. If executed synchronously the ioctl will
71b88baab8SDanilo Krummrich  * block until the bind job is finished. For synchronous jobs the kernel will
72b88baab8SDanilo Krummrich  * not permit any syncobjs submitted to the kernel.
73b88baab8SDanilo Krummrich  *
74b88baab8SDanilo Krummrich  * To execute a push buffer the UAPI provides the DRM_NOUVEAU_EXEC ioctl. EXEC
75b88baab8SDanilo Krummrich  * jobs are always executed asynchronously, and, equal to VM_BIND jobs, provide
76b88baab8SDanilo Krummrich  * the option to synchronize them with syncobjs.
77b88baab8SDanilo Krummrich  *
78b88baab8SDanilo Krummrich  * Besides that, EXEC jobs can be scheduled for a specified channel to execute on.
79b88baab8SDanilo Krummrich  *
80b88baab8SDanilo Krummrich  * Since VM_BIND jobs update the GPU's VA space on job submit, EXEC jobs do have
81b88baab8SDanilo Krummrich  * an up to date view of the VA space. However, the actual mappings might still
82b88baab8SDanilo Krummrich  * be pending. Hence, EXEC jobs require to have the particular fences - of
83b88baab8SDanilo Krummrich  * the corresponding VM_BIND jobs they depent on - attached to them.
84b88baab8SDanilo Krummrich  */
85b88baab8SDanilo Krummrich 
86b88baab8SDanilo Krummrich static int
87014f831aSDanilo Krummrich nouveau_exec_job_submit(struct nouveau_job *job,
88014f831aSDanilo Krummrich 			struct drm_gpuvm_exec *vme)
89b88baab8SDanilo Krummrich {
90b88baab8SDanilo Krummrich 	struct nouveau_exec_job *exec_job = to_nouveau_exec_job(job);
91b88baab8SDanilo Krummrich 	struct nouveau_cli *cli = job->cli;
92b88baab8SDanilo Krummrich 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(cli);
93b88baab8SDanilo Krummrich 	int ret;
94b88baab8SDanilo Krummrich 
95978474dcSDanilo Krummrich 	/* Create a new fence, but do not emit yet. */
96978474dcSDanilo Krummrich 	ret = nouveau_fence_create(&exec_job->fence, exec_job->chan);
97b88baab8SDanilo Krummrich 	if (ret)
98b88baab8SDanilo Krummrich 		return ret;
99b88baab8SDanilo Krummrich 
100b88baab8SDanilo Krummrich 	nouveau_uvmm_lock(uvmm);
101014f831aSDanilo Krummrich 	ret = drm_gpuvm_exec_lock(vme);
102014f831aSDanilo Krummrich 	if (ret) {
103014f831aSDanilo Krummrich 		nouveau_uvmm_unlock(uvmm);
104014f831aSDanilo Krummrich 		return ret;
105b88baab8SDanilo Krummrich 	}
106b88baab8SDanilo Krummrich 	nouveau_uvmm_unlock(uvmm);
107b88baab8SDanilo Krummrich 
108014f831aSDanilo Krummrich 	ret = drm_gpuvm_exec_validate(vme);
109014f831aSDanilo Krummrich 	if (ret) {
110014f831aSDanilo Krummrich 		drm_gpuvm_exec_unlock(vme);
111014f831aSDanilo Krummrich 		return ret;
112b88baab8SDanilo Krummrich 	}
113b88baab8SDanilo Krummrich 
114b88baab8SDanilo Krummrich 	return 0;
115b88baab8SDanilo Krummrich }
116b88baab8SDanilo Krummrich 
117b88baab8SDanilo Krummrich static void
118014f831aSDanilo Krummrich nouveau_exec_job_armed_submit(struct nouveau_job *job,
119014f831aSDanilo Krummrich 			      struct drm_gpuvm_exec *vme)
120b88baab8SDanilo Krummrich {
121014f831aSDanilo Krummrich 	drm_gpuvm_exec_resv_add_fence(vme, job->done_fence,
122014f831aSDanilo Krummrich 				      job->resv_usage, job->resv_usage);
123014f831aSDanilo Krummrich 	drm_gpuvm_exec_unlock(vme);
124b88baab8SDanilo Krummrich }
125b88baab8SDanilo Krummrich 
126b88baab8SDanilo Krummrich static struct dma_fence *
127b88baab8SDanilo Krummrich nouveau_exec_job_run(struct nouveau_job *job)
128b88baab8SDanilo Krummrich {
129b88baab8SDanilo Krummrich 	struct nouveau_exec_job *exec_job = to_nouveau_exec_job(job);
130b88baab8SDanilo Krummrich 	struct nouveau_channel *chan = exec_job->chan;
131b88baab8SDanilo Krummrich 	struct nouveau_fence *fence = exec_job->fence;
132b88baab8SDanilo Krummrich 	int i, ret;
133b88baab8SDanilo Krummrich 
134b88baab8SDanilo Krummrich 	ret = nouveau_dma_wait(chan, exec_job->push.count + 1, 16);
135b88baab8SDanilo Krummrich 	if (ret) {
136b88baab8SDanilo Krummrich 		NV_PRINTK(err, job->cli, "nv50cal_space: %d\n", ret);
137b88baab8SDanilo Krummrich 		return ERR_PTR(ret);
138b88baab8SDanilo Krummrich 	}
139b88baab8SDanilo Krummrich 
140b88baab8SDanilo Krummrich 	for (i = 0; i < exec_job->push.count; i++) {
141443f9e0bSDanilo Krummrich 		struct drm_nouveau_exec_push *p = &exec_job->push.s[i];
142443f9e0bSDanilo Krummrich 		bool no_prefetch = p->flags & DRM_NOUVEAU_EXEC_PUSH_NO_PREFETCH;
143443f9e0bSDanilo Krummrich 
144443f9e0bSDanilo Krummrich 		nv50_dma_push(chan, p->va, p->va_len, no_prefetch);
145b88baab8SDanilo Krummrich 	}
146b88baab8SDanilo Krummrich 
147978474dcSDanilo Krummrich 	ret = nouveau_fence_emit(fence);
148b88baab8SDanilo Krummrich 	if (ret) {
149978474dcSDanilo Krummrich 		nouveau_fence_unref(&exec_job->fence);
150b88baab8SDanilo Krummrich 		NV_PRINTK(err, job->cli, "error fencing pushbuf: %d\n", ret);
151b88baab8SDanilo Krummrich 		WIND_RING(chan);
152b88baab8SDanilo Krummrich 		return ERR_PTR(ret);
153b88baab8SDanilo Krummrich 	}
154b88baab8SDanilo Krummrich 
155978474dcSDanilo Krummrich 	/* The fence was emitted successfully, set the job's fence pointer to
156978474dcSDanilo Krummrich 	 * NULL in order to avoid freeing it up when the job is cleaned up.
157978474dcSDanilo Krummrich 	 */
158b88baab8SDanilo Krummrich 	exec_job->fence = NULL;
159b88baab8SDanilo Krummrich 
160b88baab8SDanilo Krummrich 	return &fence->base;
161b88baab8SDanilo Krummrich }
162b88baab8SDanilo Krummrich 
163b88baab8SDanilo Krummrich static void
164b88baab8SDanilo Krummrich nouveau_exec_job_free(struct nouveau_job *job)
165b88baab8SDanilo Krummrich {
166b88baab8SDanilo Krummrich 	struct nouveau_exec_job *exec_job = to_nouveau_exec_job(job);
167b88baab8SDanilo Krummrich 
1685f03a507SDanilo Krummrich 	nouveau_job_done(job);
169b88baab8SDanilo Krummrich 	nouveau_job_free(job);
170b88baab8SDanilo Krummrich 
171978474dcSDanilo Krummrich 	kfree(exec_job->fence);
172b88baab8SDanilo Krummrich 	kfree(exec_job->push.s);
173b88baab8SDanilo Krummrich 	kfree(exec_job);
174b88baab8SDanilo Krummrich }
175b88baab8SDanilo Krummrich 
176b88baab8SDanilo Krummrich static enum drm_gpu_sched_stat
177b88baab8SDanilo Krummrich nouveau_exec_job_timeout(struct nouveau_job *job)
178b88baab8SDanilo Krummrich {
179b88baab8SDanilo Krummrich 	struct nouveau_exec_job *exec_job = to_nouveau_exec_job(job);
180b88baab8SDanilo Krummrich 	struct nouveau_channel *chan = exec_job->chan;
181b88baab8SDanilo Krummrich 
182b88baab8SDanilo Krummrich 	if (unlikely(!atomic_read(&chan->killed)))
183b88baab8SDanilo Krummrich 		nouveau_channel_kill(chan);
184b88baab8SDanilo Krummrich 
185b88baab8SDanilo Krummrich 	NV_PRINTK(warn, job->cli, "job timeout, channel %d killed!\n",
186b88baab8SDanilo Krummrich 		  chan->chid);
187b88baab8SDanilo Krummrich 
18831499b01SDanilo Krummrich 	return DRM_GPU_SCHED_STAT_NOMINAL;
189b88baab8SDanilo Krummrich }
190b88baab8SDanilo Krummrich 
191b88baab8SDanilo Krummrich static struct nouveau_job_ops nouveau_exec_job_ops = {
192b88baab8SDanilo Krummrich 	.submit = nouveau_exec_job_submit,
193b88baab8SDanilo Krummrich 	.armed_submit = nouveau_exec_job_armed_submit,
194b88baab8SDanilo Krummrich 	.run = nouveau_exec_job_run,
195b88baab8SDanilo Krummrich 	.free = nouveau_exec_job_free,
196b88baab8SDanilo Krummrich 	.timeout = nouveau_exec_job_timeout,
197b88baab8SDanilo Krummrich };
198b88baab8SDanilo Krummrich 
199b88baab8SDanilo Krummrich int
200b88baab8SDanilo Krummrich nouveau_exec_job_init(struct nouveau_exec_job **pjob,
201b88baab8SDanilo Krummrich 		      struct nouveau_exec_job_args *__args)
202b88baab8SDanilo Krummrich {
203b88baab8SDanilo Krummrich 	struct nouveau_exec_job *job;
204b88baab8SDanilo Krummrich 	struct nouveau_job_args args = {};
205443f9e0bSDanilo Krummrich 	int i, ret;
206443f9e0bSDanilo Krummrich 
207443f9e0bSDanilo Krummrich 	for (i = 0; i < __args->push.count; i++) {
208443f9e0bSDanilo Krummrich 		struct drm_nouveau_exec_push *p = &__args->push.s[i];
209443f9e0bSDanilo Krummrich 
210443f9e0bSDanilo Krummrich 		if (unlikely(p->va_len > NV50_DMA_PUSH_MAX_LENGTH)) {
211443f9e0bSDanilo Krummrich 			NV_PRINTK(err, nouveau_cli(__args->file_priv),
212443f9e0bSDanilo Krummrich 				  "pushbuf size exceeds limit: 0x%x max 0x%x\n",
213443f9e0bSDanilo Krummrich 				  p->va_len, NV50_DMA_PUSH_MAX_LENGTH);
214443f9e0bSDanilo Krummrich 			return -EINVAL;
215443f9e0bSDanilo Krummrich 		}
216443f9e0bSDanilo Krummrich 	}
217b88baab8SDanilo Krummrich 
218b88baab8SDanilo Krummrich 	job = *pjob = kzalloc(sizeof(*job), GFP_KERNEL);
219b88baab8SDanilo Krummrich 	if (!job)
220b88baab8SDanilo Krummrich 		return -ENOMEM;
221b88baab8SDanilo Krummrich 
222b88baab8SDanilo Krummrich 	job->push.count = __args->push.count;
223b88baab8SDanilo Krummrich 	if (__args->push.count) {
224b88baab8SDanilo Krummrich 		job->push.s = kmemdup(__args->push.s,
225b88baab8SDanilo Krummrich 				      sizeof(*__args->push.s) *
226b88baab8SDanilo Krummrich 				      __args->push.count,
227b88baab8SDanilo Krummrich 				      GFP_KERNEL);
228b88baab8SDanilo Krummrich 		if (!job->push.s) {
229b88baab8SDanilo Krummrich 			ret = -ENOMEM;
230b88baab8SDanilo Krummrich 			goto err_free_job;
231b88baab8SDanilo Krummrich 		}
232b88baab8SDanilo Krummrich 	}
233b88baab8SDanilo Krummrich 
23446990918SDanilo Krummrich 	args.file_priv = __args->file_priv;
235b88baab8SDanilo Krummrich 	job->chan = __args->chan;
236b88baab8SDanilo Krummrich 
2375f03a507SDanilo Krummrich 	args.sched = __args->sched;
23846990918SDanilo Krummrich 	/* Plus one to account for the HW fence. */
23946990918SDanilo Krummrich 	args.credits = job->push.count + 1;
240b88baab8SDanilo Krummrich 
241b88baab8SDanilo Krummrich 	args.in_sync.count = __args->in_sync.count;
242b88baab8SDanilo Krummrich 	args.in_sync.s = __args->in_sync.s;
243b88baab8SDanilo Krummrich 
244b88baab8SDanilo Krummrich 	args.out_sync.count = __args->out_sync.count;
245b88baab8SDanilo Krummrich 	args.out_sync.s = __args->out_sync.s;
246b88baab8SDanilo Krummrich 
247b88baab8SDanilo Krummrich 	args.ops = &nouveau_exec_job_ops;
248b88baab8SDanilo Krummrich 	args.resv_usage = DMA_RESV_USAGE_WRITE;
249b88baab8SDanilo Krummrich 
250b88baab8SDanilo Krummrich 	ret = nouveau_job_init(&job->base, &args);
251b88baab8SDanilo Krummrich 	if (ret)
252b88baab8SDanilo Krummrich 		goto err_free_pushs;
253b88baab8SDanilo Krummrich 
254b88baab8SDanilo Krummrich 	return 0;
255b88baab8SDanilo Krummrich 
256b88baab8SDanilo Krummrich err_free_pushs:
257b88baab8SDanilo Krummrich 	kfree(job->push.s);
258b88baab8SDanilo Krummrich err_free_job:
259b88baab8SDanilo Krummrich 	kfree(job);
260b88baab8SDanilo Krummrich 	*pjob = NULL;
261b88baab8SDanilo Krummrich 
262b88baab8SDanilo Krummrich 	return ret;
263b88baab8SDanilo Krummrich }
264b88baab8SDanilo Krummrich 
265b88baab8SDanilo Krummrich static int
266b88baab8SDanilo Krummrich nouveau_exec(struct nouveau_exec_job_args *args)
267b88baab8SDanilo Krummrich {
268b88baab8SDanilo Krummrich 	struct nouveau_exec_job *job;
269b88baab8SDanilo Krummrich 	int ret;
270b88baab8SDanilo Krummrich 
271b88baab8SDanilo Krummrich 	ret = nouveau_exec_job_init(&job, args);
272b88baab8SDanilo Krummrich 	if (ret)
273b88baab8SDanilo Krummrich 		return ret;
274b88baab8SDanilo Krummrich 
275b88baab8SDanilo Krummrich 	ret = nouveau_job_submit(&job->base);
276b88baab8SDanilo Krummrich 	if (ret)
277b88baab8SDanilo Krummrich 		goto err_job_fini;
278b88baab8SDanilo Krummrich 
279b88baab8SDanilo Krummrich 	return 0;
280b88baab8SDanilo Krummrich 
281b88baab8SDanilo Krummrich err_job_fini:
282b88baab8SDanilo Krummrich 	nouveau_job_fini(&job->base);
283b88baab8SDanilo Krummrich 	return ret;
284b88baab8SDanilo Krummrich }
285b88baab8SDanilo Krummrich 
286b88baab8SDanilo Krummrich static int
287b88baab8SDanilo Krummrich nouveau_exec_ucopy(struct nouveau_exec_job_args *args,
288e39701e3SDanilo Krummrich 		   struct drm_nouveau_exec *req)
289b88baab8SDanilo Krummrich {
290b88baab8SDanilo Krummrich 	struct drm_nouveau_sync **s;
291b88baab8SDanilo Krummrich 	u32 inc = req->wait_count;
292b88baab8SDanilo Krummrich 	u64 ins = req->wait_ptr;
293b88baab8SDanilo Krummrich 	u32 outc = req->sig_count;
294b88baab8SDanilo Krummrich 	u64 outs = req->sig_ptr;
295b88baab8SDanilo Krummrich 	u32 pushc = req->push_count;
296b88baab8SDanilo Krummrich 	u64 pushs = req->push_ptr;
297b88baab8SDanilo Krummrich 	int ret;
298b88baab8SDanilo Krummrich 
299b88baab8SDanilo Krummrich 	if (pushc) {
300b88baab8SDanilo Krummrich 		args->push.count = pushc;
301b88baab8SDanilo Krummrich 		args->push.s = u_memcpya(pushs, pushc, sizeof(*args->push.s));
302b88baab8SDanilo Krummrich 		if (IS_ERR(args->push.s))
303b88baab8SDanilo Krummrich 			return PTR_ERR(args->push.s);
304b88baab8SDanilo Krummrich 	}
305b88baab8SDanilo Krummrich 
306b88baab8SDanilo Krummrich 	if (inc) {
307b88baab8SDanilo Krummrich 		s = &args->in_sync.s;
308b88baab8SDanilo Krummrich 
309b88baab8SDanilo Krummrich 		args->in_sync.count = inc;
310b88baab8SDanilo Krummrich 		*s = u_memcpya(ins, inc, sizeof(**s));
311b88baab8SDanilo Krummrich 		if (IS_ERR(*s)) {
312b88baab8SDanilo Krummrich 			ret = PTR_ERR(*s);
313b88baab8SDanilo Krummrich 			goto err_free_pushs;
314b88baab8SDanilo Krummrich 		}
315b88baab8SDanilo Krummrich 	}
316b88baab8SDanilo Krummrich 
317b88baab8SDanilo Krummrich 	if (outc) {
318b88baab8SDanilo Krummrich 		s = &args->out_sync.s;
319b88baab8SDanilo Krummrich 
320b88baab8SDanilo Krummrich 		args->out_sync.count = outc;
321b88baab8SDanilo Krummrich 		*s = u_memcpya(outs, outc, sizeof(**s));
322b88baab8SDanilo Krummrich 		if (IS_ERR(*s)) {
323b88baab8SDanilo Krummrich 			ret = PTR_ERR(*s);
324b88baab8SDanilo Krummrich 			goto err_free_ins;
325b88baab8SDanilo Krummrich 		}
326b88baab8SDanilo Krummrich 	}
327b88baab8SDanilo Krummrich 
328b88baab8SDanilo Krummrich 	return 0;
329b88baab8SDanilo Krummrich 
330b88baab8SDanilo Krummrich err_free_pushs:
331b88baab8SDanilo Krummrich 	u_free(args->push.s);
332b88baab8SDanilo Krummrich err_free_ins:
333b88baab8SDanilo Krummrich 	u_free(args->in_sync.s);
334b88baab8SDanilo Krummrich 	return ret;
335b88baab8SDanilo Krummrich }
336b88baab8SDanilo Krummrich 
337b88baab8SDanilo Krummrich static void
338b88baab8SDanilo Krummrich nouveau_exec_ufree(struct nouveau_exec_job_args *args)
339b88baab8SDanilo Krummrich {
340b88baab8SDanilo Krummrich 	u_free(args->push.s);
341b88baab8SDanilo Krummrich 	u_free(args->in_sync.s);
342b88baab8SDanilo Krummrich 	u_free(args->out_sync.s);
343b88baab8SDanilo Krummrich }
344b88baab8SDanilo Krummrich 
345b88baab8SDanilo Krummrich int
346b88baab8SDanilo Krummrich nouveau_exec_ioctl_exec(struct drm_device *dev,
347e39701e3SDanilo Krummrich 			void *data,
348b88baab8SDanilo Krummrich 			struct drm_file *file_priv)
349b88baab8SDanilo Krummrich {
350b88baab8SDanilo Krummrich 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
351b88baab8SDanilo Krummrich 	struct nouveau_cli *cli = nouveau_cli(file_priv);
352b88baab8SDanilo Krummrich 	struct nouveau_abi16_chan *chan16;
353b88baab8SDanilo Krummrich 	struct nouveau_channel *chan = NULL;
354b88baab8SDanilo Krummrich 	struct nouveau_exec_job_args args = {};
355e39701e3SDanilo Krummrich 	struct drm_nouveau_exec *req = data;
356d59e75eeSDanilo Krummrich 	int push_max, ret = 0;
357b88baab8SDanilo Krummrich 
358b88baab8SDanilo Krummrich 	if (unlikely(!abi16))
359b88baab8SDanilo Krummrich 		return -ENOMEM;
360b88baab8SDanilo Krummrich 
361b88baab8SDanilo Krummrich 	/* abi16 locks already */
362b88baab8SDanilo Krummrich 	if (unlikely(!nouveau_cli_uvmm(cli)))
363b88baab8SDanilo Krummrich 		return nouveau_abi16_put(abi16, -ENOSYS);
364b88baab8SDanilo Krummrich 
365b88baab8SDanilo Krummrich 	list_for_each_entry(chan16, &abi16->channels, head) {
366b88baab8SDanilo Krummrich 		if (chan16->chan->chid == req->channel) {
367b88baab8SDanilo Krummrich 			chan = chan16->chan;
368b88baab8SDanilo Krummrich 			break;
369b88baab8SDanilo Krummrich 		}
370b88baab8SDanilo Krummrich 	}
371b88baab8SDanilo Krummrich 
372b88baab8SDanilo Krummrich 	if (!chan)
373b88baab8SDanilo Krummrich 		return nouveau_abi16_put(abi16, -ENOENT);
374b88baab8SDanilo Krummrich 
375b88baab8SDanilo Krummrich 	if (unlikely(atomic_read(&chan->killed)))
376b88baab8SDanilo Krummrich 		return nouveau_abi16_put(abi16, -ENODEV);
377b88baab8SDanilo Krummrich 
378b88baab8SDanilo Krummrich 	if (!chan->dma.ib_max)
379b88baab8SDanilo Krummrich 		return nouveau_abi16_put(abi16, -ENOSYS);
380b88baab8SDanilo Krummrich 
381d59e75eeSDanilo Krummrich 	push_max = nouveau_exec_push_max_from_ib_max(chan->dma.ib_max);
382d59e75eeSDanilo Krummrich 	if (unlikely(req->push_count > push_max)) {
383b88baab8SDanilo Krummrich 		NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
384d59e75eeSDanilo Krummrich 			  req->push_count, push_max);
385b88baab8SDanilo Krummrich 		return nouveau_abi16_put(abi16, -EINVAL);
386b88baab8SDanilo Krummrich 	}
387b88baab8SDanilo Krummrich 
388b88baab8SDanilo Krummrich 	ret = nouveau_exec_ucopy(&args, req);
389b88baab8SDanilo Krummrich 	if (ret)
390b88baab8SDanilo Krummrich 		goto out;
391b88baab8SDanilo Krummrich 
392*9a0c32d6SDanilo Krummrich 	args.sched = chan16->sched;
393b88baab8SDanilo Krummrich 	args.file_priv = file_priv;
394b88baab8SDanilo Krummrich 	args.chan = chan;
395b88baab8SDanilo Krummrich 
396b88baab8SDanilo Krummrich 	ret = nouveau_exec(&args);
397b88baab8SDanilo Krummrich 	if (ret)
398b88baab8SDanilo Krummrich 		goto out_free_args;
399b88baab8SDanilo Krummrich 
400b88baab8SDanilo Krummrich out_free_args:
401b88baab8SDanilo Krummrich 	nouveau_exec_ufree(&args);
402b88baab8SDanilo Krummrich out:
403b88baab8SDanilo Krummrich 	return nouveau_abi16_put(abi16, ret);
404b88baab8SDanilo Krummrich }
405