Linux Audio

Check our new training course

Loading...
v6.13.7
  1/*
  2 * Copyright 2012 Red Hat Inc.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 20 * OTHER DEALINGS IN THE SOFTWARE.
 21 *
 22 */
 23
 24#include <nvif/client.h>
 25#include <nvif/driver.h>
 26#include <nvif/fifo.h>
 27#include <nvif/ioctl.h>
 28#include <nvif/class.h>
 29#include <nvif/cl0002.h>
 30#include <nvif/unpack.h>
 31
 32#include "nouveau_drv.h"
 33#include "nouveau_dma.h"
 34#include "nouveau_exec.h"
 35#include "nouveau_gem.h"
 36#include "nouveau_chan.h"
 37#include "nouveau_abi16.h"
 38#include "nouveau_vmm.h"
 39#include "nouveau_sched.h"
 40
 41static struct nouveau_abi16 *
 42nouveau_abi16(struct drm_file *file_priv)
 43{
 44	struct nouveau_cli *cli = nouveau_cli(file_priv);
 45	if (!cli->abi16) {
 46		struct nouveau_abi16 *abi16;
 47		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
 48		if (cli->abi16) {
 49			abi16->cli = cli;
 
 
 
 50			INIT_LIST_HEAD(&abi16->channels);
 51			INIT_LIST_HEAD(&abi16->objects);
 
 
 
 
 
 
 
 
 
 
 
 52		}
 53	}
 54	return cli->abi16;
 55}
 56
 57struct nouveau_abi16 *
 58nouveau_abi16_get(struct drm_file *file_priv)
 59{
 60	struct nouveau_cli *cli = nouveau_cli(file_priv);
 61	mutex_lock(&cli->mutex);
 62	if (nouveau_abi16(file_priv))
 63		return cli->abi16;
 64	mutex_unlock(&cli->mutex);
 65	return NULL;
 66}
 67
 68int
 69nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
 70{
 71	struct nouveau_cli *cli = abi16->cli;
 72	mutex_unlock(&cli->mutex);
 73	return ret;
 74}
 75
 76/* Tracks objects created via the DRM_NOUVEAU_NVIF ioctl.
 77 *
 78 * The only two types of object that userspace ever allocated via this
 79 * interface are 'device', in order to retrieve basic device info, and
 80 * 'engine objects', which instantiate HW classes on a channel.
 81 *
 82 * The remainder of what used to be available via DRM_NOUVEAU_NVIF has
 83 * been removed, but these object types need to be tracked to maintain
 84 * compatibility with userspace.
 85 */
 86struct nouveau_abi16_obj {
 87	enum nouveau_abi16_obj_type {
 88		DEVICE,
 89		ENGOBJ,
 90	} type;
 91	u64 object;
 92
 93	struct nvif_object engobj;
 94
 95	struct list_head head; /* protected by nouveau_abi16.cli.mutex */
 96};
 97
 98static struct nouveau_abi16_obj *
 99nouveau_abi16_obj_find(struct nouveau_abi16 *abi16, u64 object)
100{
101	struct nouveau_abi16_obj *obj;
102
103	list_for_each_entry(obj, &abi16->objects, head) {
104		if (obj->object == object)
105			return obj;
106	}
107
108	return NULL;
109}
110
111static void
112nouveau_abi16_obj_del(struct nouveau_abi16_obj *obj)
113{
114	list_del(&obj->head);
115	kfree(obj);
116}
117
118static struct nouveau_abi16_obj *
119nouveau_abi16_obj_new(struct nouveau_abi16 *abi16, enum nouveau_abi16_obj_type type, u64 object)
120{
121	struct nouveau_abi16_obj *obj;
122
123	obj = nouveau_abi16_obj_find(abi16, object);
124	if (obj)
125		return ERR_PTR(-EEXIST);
126
127	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
128	if (!obj)
129		return ERR_PTR(-ENOMEM);
130
131	obj->type = type;
132	obj->object = object;
133	list_add_tail(&obj->head, &abi16->objects);
134	return obj;
135}
136
137s32
138nouveau_abi16_swclass(struct nouveau_drm *drm)
139{
140	switch (drm->client.device.info.family) {
141	case NV_DEVICE_INFO_V0_TNT:
142		return NVIF_CLASS_SW_NV04;
143	case NV_DEVICE_INFO_V0_CELSIUS:
144	case NV_DEVICE_INFO_V0_KELVIN:
145	case NV_DEVICE_INFO_V0_RANKINE:
146	case NV_DEVICE_INFO_V0_CURIE:
147		return NVIF_CLASS_SW_NV10;
148	case NV_DEVICE_INFO_V0_TESLA:
149		return NVIF_CLASS_SW_NV50;
150	case NV_DEVICE_INFO_V0_FERMI:
151	case NV_DEVICE_INFO_V0_KEPLER:
152	case NV_DEVICE_INFO_V0_MAXWELL:
153	case NV_DEVICE_INFO_V0_PASCAL:
154	case NV_DEVICE_INFO_V0_VOLTA:
155		return NVIF_CLASS_SW_GF100;
156	}
157
158	return 0x0000;
159}
160
161static void
162nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
163			struct nouveau_abi16_ntfy *ntfy)
164{
165	nvif_object_dtor(&ntfy->object);
166	nvkm_mm_free(&chan->heap, &ntfy->node);
167	list_del(&ntfy->head);
168	kfree(ntfy);
169}
170
171static void
172nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
173			struct nouveau_abi16_chan *chan)
174{
175	struct nouveau_abi16_ntfy *ntfy, *temp;
176
177	/* Cancel all jobs from the entity's queue. */
178	if (chan->sched)
179		drm_sched_entity_fini(&chan->sched->entity);
180
181	if (chan->chan)
182		nouveau_channel_idle(chan->chan);
183
184	if (chan->sched)
185		nouveau_sched_destroy(&chan->sched);
186
187	/* cleanup notifier state */
188	list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
189		nouveau_abi16_ntfy_fini(chan, ntfy);
190	}
191
192	if (chan->ntfy) {
193		nouveau_vma_del(&chan->ntfy_vma);
194		nouveau_bo_unpin(chan->ntfy);
195		drm_gem_object_put(&chan->ntfy->bo.base);
196	}
197
198	if (chan->heap.block_size)
199		nvkm_mm_fini(&chan->heap);
200
201	/* destroy channel object, all children will be killed too */
202	if (chan->chan) {
203		nvif_object_dtor(&chan->ce);
204		nouveau_channel_del(&chan->chan);
205	}
206
207	list_del(&chan->head);
208	kfree(chan);
209}
210
211void
212nouveau_abi16_fini(struct nouveau_abi16 *abi16)
213{
214	struct nouveau_cli *cli = abi16->cli;
215	struct nouveau_abi16_chan *chan, *temp;
216	struct nouveau_abi16_obj *obj, *tmp;
217
218	/* cleanup objects */
219	list_for_each_entry_safe(obj, tmp, &abi16->objects, head) {
220		nouveau_abi16_obj_del(obj);
221	}
222
223	/* cleanup channels */
224	list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
225		nouveau_abi16_chan_fini(abi16, chan);
226	}
227
 
 
 
228	kfree(cli->abi16);
229	cli->abi16 = NULL;
230}
231
232static inline int
233getparam_dma_ib_max(struct nvif_device *device)
234{
235	const struct nvif_mclass dmas[] = {
236		{ NV03_CHANNEL_DMA, 0 },
237		{ NV10_CHANNEL_DMA, 0 },
238		{ NV17_CHANNEL_DMA, 0 },
239		{ NV40_CHANNEL_DMA, 0 },
240		{}
241	};
242
243	return nvif_mclass(&device->object, dmas) < 0 ? NV50_DMA_IB_MAX : 0;
244}
245
246int
247nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
248{
249	struct nouveau_cli *cli = nouveau_cli(file_priv);
250	struct nouveau_drm *drm = nouveau_drm(dev);
251	struct nvif_device *device = &drm->client.device;
252	struct nvkm_device *nvkm_device = nvxx_device(drm);
253	struct nvkm_gr *gr = nvxx_gr(drm);
254	struct drm_nouveau_getparam *getparam = data;
255	struct pci_dev *pdev = to_pci_dev(dev->dev);
256
257	switch (getparam->param) {
258	case NOUVEAU_GETPARAM_CHIPSET_ID:
259		getparam->value = device->info.chipset;
260		break;
261	case NOUVEAU_GETPARAM_PCI_VENDOR:
262		if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
263			getparam->value = pdev->vendor;
264		else
265			getparam->value = 0;
266		break;
267	case NOUVEAU_GETPARAM_PCI_DEVICE:
268		if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
269			getparam->value = pdev->device;
270		else
271			getparam->value = 0;
272		break;
273	case NOUVEAU_GETPARAM_BUS_TYPE:
274		switch (device->info.platform) {
275		case NV_DEVICE_INFO_V0_AGP : getparam->value = 0; break;
276		case NV_DEVICE_INFO_V0_PCI : getparam->value = 1; break;
277		case NV_DEVICE_INFO_V0_PCIE: getparam->value = 2; break;
278		case NV_DEVICE_INFO_V0_SOC : getparam->value = 3; break;
279		case NV_DEVICE_INFO_V0_IGP :
280			if (!pci_is_pcie(pdev))
281				getparam->value = 1;
282			else
283				getparam->value = 2;
284			break;
285		default:
286			WARN_ON(1);
287			break;
288		}
289		break;
290	case NOUVEAU_GETPARAM_FB_SIZE:
291		getparam->value = drm->gem.vram_available;
292		break;
293	case NOUVEAU_GETPARAM_AGP_SIZE:
294		getparam->value = drm->gem.gart_available;
295		break;
296	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
297		getparam->value = 0; /* deprecated */
298		break;
299	case NOUVEAU_GETPARAM_PTIMER_TIME:
300		getparam->value = nvif_device_time(device);
301		break;
302	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
303		getparam->value = 1;
304		break;
305	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
306		getparam->value = 1;
307		break;
308	case NOUVEAU_GETPARAM_GRAPH_UNITS:
309		getparam->value = nvkm_gr_units(gr);
310		break;
311	case NOUVEAU_GETPARAM_EXEC_PUSH_MAX: {
312		int ib_max = getparam_dma_ib_max(device);
313
314		getparam->value = nouveau_exec_push_max_from_ib_max(ib_max);
315		break;
316	}
317	case NOUVEAU_GETPARAM_VRAM_BAR_SIZE:
318		getparam->value = nvkm_device->func->resource_size(nvkm_device, 1);
319		break;
320	case NOUVEAU_GETPARAM_VRAM_USED: {
321		struct ttm_resource_manager *vram_mgr = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
322		getparam->value = (u64)ttm_resource_manager_usage(vram_mgr);
323		break;
324	}
325	case NOUVEAU_GETPARAM_HAS_VMA_TILEMODE:
326		getparam->value = 1;
327		break;
328	default:
329		NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
330		return -EINVAL;
331	}
332
333	return 0;
334}
335
336int
337nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
338{
339	struct drm_nouveau_channel_alloc *init = data;
340	struct nouveau_cli *cli = nouveau_cli(file_priv);
341	struct nouveau_drm *drm = nouveau_drm(dev);
342	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
343	struct nouveau_abi16_chan *chan;
344	struct nvif_device *device = &cli->device;
345	u64 engine, runm;
346	int ret;
347
348	if (unlikely(!abi16))
349		return -ENOMEM;
350
351	if (!drm->channel)
352		return nouveau_abi16_put(abi16, -ENODEV);
353
354	/* If uvmm wasn't initialized until now disable it completely to prevent
355	 * userspace from mixing up UAPIs.
356	 *
357	 * The client lock is already acquired by nouveau_abi16_get().
358	 */
359	__nouveau_cli_disable_uvmm_noinit(cli);
360
 
361	engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR;
362
363	/* hack to allow channel engine type specification on kepler */
364	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
365		if (init->fb_ctxdma_handle == ~0) {
366			switch (init->tt_ctxdma_handle) {
367			case NOUVEAU_FIFO_ENGINE_GR:
368				engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR;
369				break;
370			case NOUVEAU_FIFO_ENGINE_VP:
371				engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPDEC;
372				break;
373			case NOUVEAU_FIFO_ENGINE_PPP:
374				engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPPP;
375				break;
376			case NOUVEAU_FIFO_ENGINE_BSP:
377				engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSVLD;
378				break;
379			case NOUVEAU_FIFO_ENGINE_CE:
380				engine = NV_DEVICE_HOST_RUNLIST_ENGINES_CE;
381				break;
382			default:
383				return nouveau_abi16_put(abi16, -ENOSYS);
384			}
385
386			init->fb_ctxdma_handle = 0;
387			init->tt_ctxdma_handle = 0;
388		}
389	}
390
391	if (engine != NV_DEVICE_HOST_RUNLIST_ENGINES_CE)
392		runm = nvif_fifo_runlist(device, engine);
393	else
394		runm = nvif_fifo_runlist_ce(device);
395
396	if (!runm || init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
397		return nouveau_abi16_put(abi16, -EINVAL);
398
399	/* allocate "abi16 channel" data and make up a handle for it */
400	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
401	if (!chan)
402		return nouveau_abi16_put(abi16, -ENOMEM);
403
404	INIT_LIST_HEAD(&chan->notifiers);
405	list_add(&chan->head, &abi16->channels);
406
407	/* create channel object and initialise dma and fence management */
408	ret = nouveau_channel_new(cli, false, runm, init->fb_ctxdma_handle,
409				  init->tt_ctxdma_handle, &chan->chan);
410	if (ret)
411		goto done;
412
413	/* If we're not using the VM_BIND uAPI, we don't need a scheduler.
414	 *
415	 * The client lock is already acquired by nouveau_abi16_get().
416	 */
417	if (nouveau_cli_uvmm(cli)) {
418		ret = nouveau_sched_create(&chan->sched, drm, drm->sched_wq,
419					   chan->chan->dma.ib_max);
420		if (ret)
421			goto done;
422	}
423
424	init->channel = chan->chan->chid;
425
426	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
427		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
428					NOUVEAU_GEM_DOMAIN_GART;
429	else
430	if (chan->chan->push.buffer->bo.resource->mem_type == TTM_PL_VRAM)
431		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
432	else
433		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
434
435	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
436		init->subchan[0].handle = 0x00000000;
437		init->subchan[0].grclass = 0x0000;
438		init->subchan[1].handle = chan->chan->nvsw.handle;
439		init->subchan[1].grclass = 0x506e;
440		init->nr_subchan = 2;
441	}
442
443	/* Workaround "nvc0" gallium driver using classes it doesn't allocate on
444	 * Kepler and above.  NVKM no longer always sets CE_CTX_VALID as part of
445	 * channel init, now we know what that stuff actually is.
446	 *
447	 * Doesn't matter for Kepler/Pascal, CE context stored in NV_RAMIN.
448	 *
449	 * Userspace was fixed prior to adding Ampere support.
450	 */
451	switch (device->info.family) {
452	case NV_DEVICE_INFO_V0_VOLTA:
453		ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, VOLTA_DMA_COPY_A,
454				       NULL, 0, &chan->ce);
455		if (ret)
456			goto done;
457		break;
458	case NV_DEVICE_INFO_V0_TURING:
459		ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, TURING_DMA_COPY_A,
460				       NULL, 0, &chan->ce);
461		if (ret)
462			goto done;
463		break;
464	default:
465		break;
466	}
467
468	/* Named memory object area */
469	ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
470			      0, 0, &chan->ntfy);
471	if (ret == 0)
472		ret = nouveau_bo_pin(chan->ntfy, NOUVEAU_GEM_DOMAIN_GART,
473				     false);
474	if (ret)
475		goto done;
476
477	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
478		ret = nouveau_vma_new(chan->ntfy, chan->chan->vmm,
479				      &chan->ntfy_vma);
480		if (ret)
481			goto done;
482	}
483
484	ret = drm_gem_handle_create(file_priv, &chan->ntfy->bo.base,
485				    &init->notifier_handle);
486	if (ret)
487		goto done;
488
489	ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1);
490done:
491	if (ret)
492		nouveau_abi16_chan_fini(abi16, chan);
493	return nouveau_abi16_put(abi16, ret);
494}
495
496static struct nouveau_abi16_chan *
497nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
498{
499	struct nouveau_abi16_chan *chan;
500
501	list_for_each_entry(chan, &abi16->channels, head) {
502		if (chan->chan->chid == channel)
503			return chan;
504	}
505
506	return NULL;
507}
508
509int
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
511{
512	struct drm_nouveau_channel_free *req = data;
513	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
514	struct nouveau_abi16_chan *chan;
515
516	if (unlikely(!abi16))
517		return -ENOMEM;
518
519	chan = nouveau_abi16_chan(abi16, req->channel);
520	if (!chan)
521		return nouveau_abi16_put(abi16, -ENOENT);
522	nouveau_abi16_chan_fini(abi16, chan);
523	return nouveau_abi16_put(abi16, 0);
524}
525
526int
527nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
528{
529	struct drm_nouveau_grobj_alloc *init = data;
530	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
531	struct nouveau_abi16_chan *chan;
532	struct nouveau_abi16_ntfy *ntfy;
 
533	struct nvif_sclass *sclass;
534	s32 oclass = 0;
535	int ret, i;
536
537	if (unlikely(!abi16))
538		return -ENOMEM;
539
540	if (init->handle == ~0)
541		return nouveau_abi16_put(abi16, -EINVAL);
 
542
543	chan = nouveau_abi16_chan(abi16, init->channel);
544	if (!chan)
545		return nouveau_abi16_put(abi16, -ENOENT);
546
547	ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
548	if (ret < 0)
549		return nouveau_abi16_put(abi16, ret);
550
551	if ((init->class & 0x00ff) == 0x006e) {
552		/* nvsw: compatibility with older 0x*6e class identifier */
553		for (i = 0; !oclass && i < ret; i++) {
554			switch (sclass[i].oclass) {
555			case NVIF_CLASS_SW_NV04:
556			case NVIF_CLASS_SW_NV10:
557			case NVIF_CLASS_SW_NV50:
558			case NVIF_CLASS_SW_GF100:
559				oclass = sclass[i].oclass;
560				break;
561			default:
562				break;
563			}
564		}
565	} else
566	if ((init->class & 0x00ff) == 0x00b1) {
567		/* msvld: compatibility with incorrect version exposure */
568		for (i = 0; i < ret; i++) {
569			if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
570				oclass = sclass[i].oclass;
571				break;
572			}
573		}
574	} else
575	if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
576		/* mspdec: compatibility with incorrect version exposure */
577		for (i = 0; i < ret; i++) {
578			if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
579				oclass = sclass[i].oclass;
580				break;
581			}
582		}
583	} else
584	if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
585		/* msppp: compatibility with incorrect version exposure */
586		for (i = 0; i < ret; i++) {
587			if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
588				oclass = sclass[i].oclass;
589				break;
590			}
591		}
592	} else {
593		oclass = init->class;
594	}
595
596	nvif_object_sclass_put(&sclass);
597	if (!oclass)
598		return nouveau_abi16_put(abi16, -EINVAL);
599
600	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
601	if (!ntfy)
602		return nouveau_abi16_put(abi16, -ENOMEM);
603
604	list_add(&ntfy->head, &chan->notifiers);
605
 
606	ret = nvif_object_ctor(&chan->chan->user, "abi16EngObj", init->handle,
607			       oclass, NULL, 0, &ntfy->object);
 
608
609	if (ret)
610		nouveau_abi16_ntfy_fini(chan, ntfy);
611	return nouveau_abi16_put(abi16, ret);
612}
613
614int
615nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
616{
617	struct drm_nouveau_notifierobj_alloc *info = data;
618	struct nouveau_drm *drm = nouveau_drm(dev);
619	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
620	struct nouveau_abi16_chan *chan;
621	struct nouveau_abi16_ntfy *ntfy;
622	struct nvif_device *device;
 
623	struct nv_dma_v0 args = {};
624	int ret;
625
626	if (unlikely(!abi16))
627		return -ENOMEM;
628	device = &abi16->cli->device;
629
630	/* completely unnecessary for these chipsets... */
631	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
632		return nouveau_abi16_put(abi16, -EINVAL);
 
633
634	chan = nouveau_abi16_chan(abi16, info->channel);
635	if (!chan)
636		return nouveau_abi16_put(abi16, -ENOENT);
637
638	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
639	if (!ntfy)
640		return nouveau_abi16_put(abi16, -ENOMEM);
641
642	list_add(&ntfy->head, &chan->notifiers);
643
644	ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
645			   &ntfy->node);
646	if (ret)
647		goto done;
648
649	args.start = ntfy->node->offset;
650	args.limit = ntfy->node->offset + ntfy->node->length - 1;
651	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
652		args.target = NV_DMA_V0_TARGET_VM;
653		args.access = NV_DMA_V0_ACCESS_VM;
654		args.start += chan->ntfy_vma->addr;
655		args.limit += chan->ntfy_vma->addr;
656	} else
657	if (drm->agp.bridge) {
658		args.target = NV_DMA_V0_TARGET_AGP;
659		args.access = NV_DMA_V0_ACCESS_RDWR;
660		args.start += drm->agp.base + chan->ntfy->offset;
661		args.limit += drm->agp.base + chan->ntfy->offset;
662	} else {
663		args.target = NV_DMA_V0_TARGET_VM;
664		args.access = NV_DMA_V0_ACCESS_RDWR;
665		args.start += chan->ntfy->offset;
666		args.limit += chan->ntfy->offset;
667	}
668
 
669	ret = nvif_object_ctor(&chan->chan->user, "abi16Ntfy", info->handle,
670			       NV_DMA_IN_MEMORY, &args, sizeof(args),
671			       &ntfy->object);
 
672	if (ret)
673		goto done;
674
675	info->offset = ntfy->node->offset;
676done:
677	if (ret)
678		nouveau_abi16_ntfy_fini(chan, ntfy);
679	return nouveau_abi16_put(abi16, ret);
680}
681
682int
683nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
684{
685	struct drm_nouveau_gpuobj_free *fini = data;
686	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
687	struct nouveau_abi16_chan *chan;
688	struct nouveau_abi16_ntfy *ntfy;
689	int ret = -ENOENT;
690
691	if (unlikely(!abi16))
692		return -ENOMEM;
693
694	chan = nouveau_abi16_chan(abi16, fini->channel);
695	if (!chan)
696		return nouveau_abi16_put(abi16, -EINVAL);
697
698	/* synchronize with the user channel and destroy the gpu object */
699	nouveau_channel_idle(chan->chan);
700
701	list_for_each_entry(ntfy, &chan->notifiers, head) {
702		if (ntfy->object.handle == fini->handle) {
703			nouveau_abi16_ntfy_fini(chan, ntfy);
704			ret = 0;
705			break;
706		}
707	}
708
709	return nouveau_abi16_put(abi16, ret);
710}
711
712static int
713nouveau_abi16_ioctl_mthd(struct nouveau_abi16 *abi16, struct nvif_ioctl_v0 *ioctl, u32 argc)
714{
715	struct nouveau_cli *cli = abi16->cli;
716	struct nvif_ioctl_mthd_v0 *args;
717	struct nouveau_abi16_obj *obj;
718	struct nv_device_info_v0 *info;
719
720	if (ioctl->route || argc < sizeof(*args))
721		return -EINVAL;
722	args = (void *)ioctl->data;
723	argc -= sizeof(*args);
724
725	obj = nouveau_abi16_obj_find(abi16, ioctl->object);
726	if (!obj || obj->type != DEVICE)
727		return -EINVAL;
728
729	if (args->method != NV_DEVICE_V0_INFO ||
730	    argc != sizeof(*info))
731		return -EINVAL;
732
733	info = (void *)args->data;
734	if (info->version != 0x00)
735		return -EINVAL;
736
737	info = &cli->device.info;
738	memcpy(args->data, info, sizeof(*info));
739	return 0;
740}
741
742static int
743nouveau_abi16_ioctl_del(struct nouveau_abi16 *abi16, struct nvif_ioctl_v0 *ioctl, u32 argc)
744{
745	struct nouveau_abi16_obj *obj;
746
747	if (ioctl->route || argc)
748		return -EINVAL;
749
750	obj = nouveau_abi16_obj_find(abi16, ioctl->object);
751	if (obj) {
752		if (obj->type == ENGOBJ)
753			nvif_object_dtor(&obj->engobj);
754		nouveau_abi16_obj_del(obj);
755	}
756
757	return 0;
758}
759
760static int
761nouveau_abi16_ioctl_new(struct nouveau_abi16 *abi16, struct nvif_ioctl_v0 *ioctl, u32 argc)
762{
763	struct nvif_ioctl_new_v0 *args;
764	struct nouveau_abi16_chan *chan;
765	struct nouveau_abi16_obj *obj;
766	int ret;
767
768	if (argc < sizeof(*args))
769		return -EINVAL;
770	args = (void *)ioctl->data;
771	argc -= sizeof(*args);
772
773	if (args->version != 0)
774		return -EINVAL;
775
776	if (!ioctl->route) {
777		if (ioctl->object || args->oclass != NV_DEVICE)
778			return -EINVAL;
779
780		obj = nouveau_abi16_obj_new(abi16, DEVICE, args->object);
781		if (IS_ERR(obj))
782			return PTR_ERR(obj);
783
784		return 0;
785	}
786
787	chan = nouveau_abi16_chan(abi16, ioctl->token);
788	if (!chan)
789		return -EINVAL;
790
791	obj = nouveau_abi16_obj_new(abi16, ENGOBJ, args->object);
792	if (IS_ERR(obj))
793		return PTR_ERR(obj);
794
795	ret = nvif_object_ctor(&chan->chan->user, "abi16EngObj", args->handle, args->oclass,
796			       NULL, 0, &obj->engobj);
797	if (ret)
798		nouveau_abi16_obj_del(obj);
799
800	return ret;
801}
802
803static int
804nouveau_abi16_ioctl_sclass(struct nouveau_abi16 *abi16, struct nvif_ioctl_v0 *ioctl, u32 argc)
805{
806	struct nvif_ioctl_sclass_v0 *args;
807	struct nouveau_abi16_chan *chan;
808	struct nvif_sclass *sclass;
809	int ret;
810
811	if (!ioctl->route || argc < sizeof(*args))
812		return -EINVAL;
813	args = (void *)ioctl->data;
814	argc -= sizeof(*args);
815
816	if (argc != args->count * sizeof(args->oclass[0]))
817		return -EINVAL;
818
819	chan = nouveau_abi16_chan(abi16, ioctl->token);
820	if (!chan)
821		return -EINVAL;
822
823	ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
824	if (ret < 0)
825		return ret;
826
827	for (int i = 0; i < min_t(u8, args->count, ret); i++) {
828		args->oclass[i].oclass = sclass[i].oclass;
829		args->oclass[i].minver = sclass[i].minver;
830		args->oclass[i].maxver = sclass[i].maxver;
831	}
832	args->count = ret;
833
834	nvif_object_sclass_put(&sclass);
835	return 0;
836}
837
838int
839nouveau_abi16_ioctl(struct drm_file *filp, void __user *user, u32 size)
840{
841	struct nvif_ioctl_v0 *ioctl;
842	struct nouveau_abi16 *abi16;
843	u32 argc = size;
844	int ret;
845
846	if (argc < sizeof(*ioctl))
847		return -EINVAL;
848	argc -= sizeof(*ioctl);
849
850	ioctl = kmalloc(size, GFP_KERNEL);
851	if (!ioctl)
852		return -ENOMEM;
853
854	ret = -EFAULT;
855	if (copy_from_user(ioctl, user, size))
856		goto done_free;
857
858	if (ioctl->version != 0x00 ||
859	    (ioctl->route && ioctl->route != 0xff)) {
860		ret = -EINVAL;
861		goto done_free;
862	}
863
864	abi16 = nouveau_abi16_get(filp);
865	if (unlikely(!abi16)) {
866		ret = -ENOMEM;
867		goto done_free;
868	}
869
870	switch (ioctl->type) {
871	case NVIF_IOCTL_V0_SCLASS: ret = nouveau_abi16_ioctl_sclass(abi16, ioctl, argc); break;
872	case NVIF_IOCTL_V0_NEW   : ret = nouveau_abi16_ioctl_new   (abi16, ioctl, argc); break;
873	case NVIF_IOCTL_V0_DEL   : ret = nouveau_abi16_ioctl_del   (abi16, ioctl, argc); break;
874	case NVIF_IOCTL_V0_MTHD  : ret = nouveau_abi16_ioctl_mthd  (abi16, ioctl, argc); break;
875	default:
876		ret = -EINVAL;
877		break;
878	}
879
880	nouveau_abi16_put(abi16, 0);
881
882	if (ret == 0) {
883		if (copy_to_user(user, ioctl, size))
884			ret = -EFAULT;
885	}
886
887done_free:
888	kfree(ioctl);
889	return ret;
890}
v6.8
  1/*
  2 * Copyright 2012 Red Hat Inc.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 20 * OTHER DEALINGS IN THE SOFTWARE.
 21 *
 22 */
 23
 24#include <nvif/client.h>
 25#include <nvif/driver.h>
 26#include <nvif/fifo.h>
 27#include <nvif/ioctl.h>
 28#include <nvif/class.h>
 29#include <nvif/cl0002.h>
 30#include <nvif/unpack.h>
 31
 32#include "nouveau_drv.h"
 33#include "nouveau_dma.h"
 34#include "nouveau_exec.h"
 35#include "nouveau_gem.h"
 36#include "nouveau_chan.h"
 37#include "nouveau_abi16.h"
 38#include "nouveau_vmm.h"
 39#include "nouveau_sched.h"
 40
 41static struct nouveau_abi16 *
 42nouveau_abi16(struct drm_file *file_priv)
 43{
 44	struct nouveau_cli *cli = nouveau_cli(file_priv);
 45	if (!cli->abi16) {
 46		struct nouveau_abi16 *abi16;
 47		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
 48		if (cli->abi16) {
 49			struct nv_device_v0 args = {
 50				.device = ~0ULL,
 51			};
 52
 53			INIT_LIST_HEAD(&abi16->channels);
 54
 55			/* allocate device object targeting client's default
 56			 * device (ie. the one that belongs to the fd it
 57			 * opened)
 58			 */
 59			if (nvif_device_ctor(&cli->base.object, "abi16Device",
 60					     0, NV_DEVICE, &args, sizeof(args),
 61					     &abi16->device) == 0)
 62				return cli->abi16;
 63
 64			kfree(cli->abi16);
 65			cli->abi16 = NULL;
 66		}
 67	}
 68	return cli->abi16;
 69}
 70
 71struct nouveau_abi16 *
 72nouveau_abi16_get(struct drm_file *file_priv)
 73{
 74	struct nouveau_cli *cli = nouveau_cli(file_priv);
 75	mutex_lock(&cli->mutex);
 76	if (nouveau_abi16(file_priv))
 77		return cli->abi16;
 78	mutex_unlock(&cli->mutex);
 79	return NULL;
 80}
 81
 82int
 83nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
 84{
 85	struct nouveau_cli *cli = (void *)abi16->device.object.client;
 86	mutex_unlock(&cli->mutex);
 87	return ret;
 88}
 89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 90s32
 91nouveau_abi16_swclass(struct nouveau_drm *drm)
 92{
 93	switch (drm->client.device.info.family) {
 94	case NV_DEVICE_INFO_V0_TNT:
 95		return NVIF_CLASS_SW_NV04;
 96	case NV_DEVICE_INFO_V0_CELSIUS:
 97	case NV_DEVICE_INFO_V0_KELVIN:
 98	case NV_DEVICE_INFO_V0_RANKINE:
 99	case NV_DEVICE_INFO_V0_CURIE:
100		return NVIF_CLASS_SW_NV10;
101	case NV_DEVICE_INFO_V0_TESLA:
102		return NVIF_CLASS_SW_NV50;
103	case NV_DEVICE_INFO_V0_FERMI:
104	case NV_DEVICE_INFO_V0_KEPLER:
105	case NV_DEVICE_INFO_V0_MAXWELL:
106	case NV_DEVICE_INFO_V0_PASCAL:
107	case NV_DEVICE_INFO_V0_VOLTA:
108		return NVIF_CLASS_SW_GF100;
109	}
110
111	return 0x0000;
112}
113
114static void
115nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
116			struct nouveau_abi16_ntfy *ntfy)
117{
118	nvif_object_dtor(&ntfy->object);
119	nvkm_mm_free(&chan->heap, &ntfy->node);
120	list_del(&ntfy->head);
121	kfree(ntfy);
122}
123
124static void
125nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
126			struct nouveau_abi16_chan *chan)
127{
128	struct nouveau_abi16_ntfy *ntfy, *temp;
129
130	/* Cancel all jobs from the entity's queue. */
131	if (chan->sched)
132		drm_sched_entity_fini(&chan->sched->entity);
133
134	if (chan->chan)
135		nouveau_channel_idle(chan->chan);
136
137	if (chan->sched)
138		nouveau_sched_destroy(&chan->sched);
139
140	/* cleanup notifier state */
141	list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
142		nouveau_abi16_ntfy_fini(chan, ntfy);
143	}
144
145	if (chan->ntfy) {
146		nouveau_vma_del(&chan->ntfy_vma);
147		nouveau_bo_unpin(chan->ntfy);
148		drm_gem_object_put(&chan->ntfy->bo.base);
149	}
150
151	if (chan->heap.block_size)
152		nvkm_mm_fini(&chan->heap);
153
154	/* destroy channel object, all children will be killed too */
155	if (chan->chan) {
156		nvif_object_dtor(&chan->ce);
157		nouveau_channel_del(&chan->chan);
158	}
159
160	list_del(&chan->head);
161	kfree(chan);
162}
163
164void
165nouveau_abi16_fini(struct nouveau_abi16 *abi16)
166{
167	struct nouveau_cli *cli = (void *)abi16->device.object.client;
168	struct nouveau_abi16_chan *chan, *temp;
 
 
 
 
 
 
169
170	/* cleanup channels */
171	list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
172		nouveau_abi16_chan_fini(abi16, chan);
173	}
174
175	/* destroy the device object */
176	nvif_device_dtor(&abi16->device);
177
178	kfree(cli->abi16);
179	cli->abi16 = NULL;
180}
181
182static inline int
183getparam_dma_ib_max(struct nvif_device *device)
184{
185	const struct nvif_mclass dmas[] = {
186		{ NV03_CHANNEL_DMA, 0 },
187		{ NV10_CHANNEL_DMA, 0 },
188		{ NV17_CHANNEL_DMA, 0 },
189		{ NV40_CHANNEL_DMA, 0 },
190		{}
191	};
192
193	return nvif_mclass(&device->object, dmas) < 0 ? NV50_DMA_IB_MAX : 0;
194}
195
196int
197nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
198{
199	struct nouveau_cli *cli = nouveau_cli(file_priv);
200	struct nouveau_drm *drm = nouveau_drm(dev);
201	struct nvif_device *device = &drm->client.device;
202	struct nvkm_device *nvkm_device = nvxx_device(&drm->client.device);
203	struct nvkm_gr *gr = nvxx_gr(device);
204	struct drm_nouveau_getparam *getparam = data;
205	struct pci_dev *pdev = to_pci_dev(dev->dev);
206
207	switch (getparam->param) {
208	case NOUVEAU_GETPARAM_CHIPSET_ID:
209		getparam->value = device->info.chipset;
210		break;
211	case NOUVEAU_GETPARAM_PCI_VENDOR:
212		if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
213			getparam->value = pdev->vendor;
214		else
215			getparam->value = 0;
216		break;
217	case NOUVEAU_GETPARAM_PCI_DEVICE:
218		if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
219			getparam->value = pdev->device;
220		else
221			getparam->value = 0;
222		break;
223	case NOUVEAU_GETPARAM_BUS_TYPE:
224		switch (device->info.platform) {
225		case NV_DEVICE_INFO_V0_AGP : getparam->value = 0; break;
226		case NV_DEVICE_INFO_V0_PCI : getparam->value = 1; break;
227		case NV_DEVICE_INFO_V0_PCIE: getparam->value = 2; break;
228		case NV_DEVICE_INFO_V0_SOC : getparam->value = 3; break;
229		case NV_DEVICE_INFO_V0_IGP :
230			if (!pci_is_pcie(pdev))
231				getparam->value = 1;
232			else
233				getparam->value = 2;
234			break;
235		default:
236			WARN_ON(1);
237			break;
238		}
239		break;
240	case NOUVEAU_GETPARAM_FB_SIZE:
241		getparam->value = drm->gem.vram_available;
242		break;
243	case NOUVEAU_GETPARAM_AGP_SIZE:
244		getparam->value = drm->gem.gart_available;
245		break;
246	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
247		getparam->value = 0; /* deprecated */
248		break;
249	case NOUVEAU_GETPARAM_PTIMER_TIME:
250		getparam->value = nvif_device_time(device);
251		break;
252	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
253		getparam->value = 1;
254		break;
255	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
256		getparam->value = 1;
257		break;
258	case NOUVEAU_GETPARAM_GRAPH_UNITS:
259		getparam->value = nvkm_gr_units(gr);
260		break;
261	case NOUVEAU_GETPARAM_EXEC_PUSH_MAX: {
262		int ib_max = getparam_dma_ib_max(device);
263
264		getparam->value = nouveau_exec_push_max_from_ib_max(ib_max);
265		break;
266	}
267	case NOUVEAU_GETPARAM_VRAM_BAR_SIZE:
268		getparam->value = nvkm_device->func->resource_size(nvkm_device, 1);
269		break;
270	case NOUVEAU_GETPARAM_VRAM_USED: {
271		struct ttm_resource_manager *vram_mgr = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
272		getparam->value = (u64)ttm_resource_manager_usage(vram_mgr);
273		break;
274	}
 
 
 
275	default:
276		NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
277		return -EINVAL;
278	}
279
280	return 0;
281}
282
283int
284nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
285{
286	struct drm_nouveau_channel_alloc *init = data;
287	struct nouveau_cli *cli = nouveau_cli(file_priv);
288	struct nouveau_drm *drm = nouveau_drm(dev);
289	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
290	struct nouveau_abi16_chan *chan;
291	struct nvif_device *device;
292	u64 engine, runm;
293	int ret;
294
295	if (unlikely(!abi16))
296		return -ENOMEM;
297
298	if (!drm->channel)
299		return nouveau_abi16_put(abi16, -ENODEV);
300
301	/* If uvmm wasn't initialized until now disable it completely to prevent
302	 * userspace from mixing up UAPIs.
303	 *
304	 * The client lock is already acquired by nouveau_abi16_get().
305	 */
306	__nouveau_cli_disable_uvmm_noinit(cli);
307
308	device = &abi16->device;
309	engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR;
310
311	/* hack to allow channel engine type specification on kepler */
312	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
313		if (init->fb_ctxdma_handle == ~0) {
314			switch (init->tt_ctxdma_handle) {
315			case 0x01: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR    ; break;
316			case 0x02: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPDEC; break;
317			case 0x04: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPPP ; break;
318			case 0x08: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSVLD ; break;
319			case 0x30: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_CE    ; break;
 
 
 
 
 
 
 
 
 
 
320			default:
321				return nouveau_abi16_put(abi16, -ENOSYS);
322			}
323
324			init->fb_ctxdma_handle = 0;
325			init->tt_ctxdma_handle = 0;
326		}
327	}
328
329	if (engine != NV_DEVICE_HOST_RUNLIST_ENGINES_CE)
330		runm = nvif_fifo_runlist(device, engine);
331	else
332		runm = nvif_fifo_runlist_ce(device);
333
334	if (!runm || init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
335		return nouveau_abi16_put(abi16, -EINVAL);
336
337	/* allocate "abi16 channel" data and make up a handle for it */
338	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
339	if (!chan)
340		return nouveau_abi16_put(abi16, -ENOMEM);
341
342	INIT_LIST_HEAD(&chan->notifiers);
343	list_add(&chan->head, &abi16->channels);
344
345	/* create channel object and initialise dma and fence management */
346	ret = nouveau_channel_new(drm, device, false, runm, init->fb_ctxdma_handle,
347				  init->tt_ctxdma_handle, &chan->chan);
348	if (ret)
349		goto done;
350
351	/* If we're not using the VM_BIND uAPI, we don't need a scheduler.
352	 *
353	 * The client lock is already acquired by nouveau_abi16_get().
354	 */
355	if (nouveau_cli_uvmm(cli)) {
356		ret = nouveau_sched_create(&chan->sched, drm, drm->sched_wq,
357					   chan->chan->dma.ib_max);
358		if (ret)
359			goto done;
360	}
361
362	init->channel = chan->chan->chid;
363
364	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
365		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
366					NOUVEAU_GEM_DOMAIN_GART;
367	else
368	if (chan->chan->push.buffer->bo.resource->mem_type == TTM_PL_VRAM)
369		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
370	else
371		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
372
373	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
374		init->subchan[0].handle = 0x00000000;
375		init->subchan[0].grclass = 0x0000;
376		init->subchan[1].handle = chan->chan->nvsw.handle;
377		init->subchan[1].grclass = 0x506e;
378		init->nr_subchan = 2;
379	}
380
381	/* Workaround "nvc0" gallium driver using classes it doesn't allocate on
382	 * Kepler and above.  NVKM no longer always sets CE_CTX_VALID as part of
383	 * channel init, now we know what that stuff actually is.
384	 *
385	 * Doesn't matter for Kepler/Pascal, CE context stored in NV_RAMIN.
386	 *
387	 * Userspace was fixed prior to adding Ampere support.
388	 */
389	switch (device->info.family) {
390	case NV_DEVICE_INFO_V0_VOLTA:
391		ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, VOLTA_DMA_COPY_A,
392				       NULL, 0, &chan->ce);
393		if (ret)
394			goto done;
395		break;
396	case NV_DEVICE_INFO_V0_TURING:
397		ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, TURING_DMA_COPY_A,
398				       NULL, 0, &chan->ce);
399		if (ret)
400			goto done;
401		break;
402	default:
403		break;
404	}
405
406	/* Named memory object area */
407	ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
408			      0, 0, &chan->ntfy);
409	if (ret == 0)
410		ret = nouveau_bo_pin(chan->ntfy, NOUVEAU_GEM_DOMAIN_GART,
411				     false);
412	if (ret)
413		goto done;
414
415	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
416		ret = nouveau_vma_new(chan->ntfy, chan->chan->vmm,
417				      &chan->ntfy_vma);
418		if (ret)
419			goto done;
420	}
421
422	ret = drm_gem_handle_create(file_priv, &chan->ntfy->bo.base,
423				    &init->notifier_handle);
424	if (ret)
425		goto done;
426
427	ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1);
428done:
429	if (ret)
430		nouveau_abi16_chan_fini(abi16, chan);
431	return nouveau_abi16_put(abi16, ret);
432}
433
434static struct nouveau_abi16_chan *
435nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
436{
437	struct nouveau_abi16_chan *chan;
438
439	list_for_each_entry(chan, &abi16->channels, head) {
440		if (chan->chan->chid == channel)
441			return chan;
442	}
443
444	return NULL;
445}
446
447int
448nouveau_abi16_usif(struct drm_file *file_priv, void *data, u32 size)
449{
450	union {
451		struct nvif_ioctl_v0 v0;
452	} *args = data;
453	struct nouveau_abi16_chan *chan;
454	struct nouveau_abi16 *abi16;
455	int ret = -ENOSYS;
456
457	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
458		switch (args->v0.type) {
459		case NVIF_IOCTL_V0_NEW:
460		case NVIF_IOCTL_V0_MTHD:
461		case NVIF_IOCTL_V0_SCLASS:
462			break;
463		default:
464			return -EACCES;
465		}
466	} else
467		return ret;
468
469	if (!(abi16 = nouveau_abi16(file_priv)))
470		return -ENOMEM;
471
472	if (args->v0.token != ~0ULL) {
473		if (!(chan = nouveau_abi16_chan(abi16, args->v0.token)))
474			return -EINVAL;
475		args->v0.object = nvif_handle(&chan->chan->user);
476		args->v0.owner  = NVIF_IOCTL_V0_OWNER_ANY;
477		return 0;
478	}
479
480	args->v0.object = nvif_handle(&abi16->device.object);
481	args->v0.owner  = NVIF_IOCTL_V0_OWNER_ANY;
482	return 0;
483}
484
485int
486nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
487{
488	struct drm_nouveau_channel_free *req = data;
489	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
490	struct nouveau_abi16_chan *chan;
491
492	if (unlikely(!abi16))
493		return -ENOMEM;
494
495	chan = nouveau_abi16_chan(abi16, req->channel);
496	if (!chan)
497		return nouveau_abi16_put(abi16, -ENOENT);
498	nouveau_abi16_chan_fini(abi16, chan);
499	return nouveau_abi16_put(abi16, 0);
500}
501
502int
503nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
504{
505	struct drm_nouveau_grobj_alloc *init = data;
506	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
507	struct nouveau_abi16_chan *chan;
508	struct nouveau_abi16_ntfy *ntfy;
509	struct nvif_client *client;
510	struct nvif_sclass *sclass;
511	s32 oclass = 0;
512	int ret, i;
513
514	if (unlikely(!abi16))
515		return -ENOMEM;
516
517	if (init->handle == ~0)
518		return nouveau_abi16_put(abi16, -EINVAL);
519	client = abi16->device.object.client;
520
521	chan = nouveau_abi16_chan(abi16, init->channel);
522	if (!chan)
523		return nouveau_abi16_put(abi16, -ENOENT);
524
525	ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
526	if (ret < 0)
527		return nouveau_abi16_put(abi16, ret);
528
529	if ((init->class & 0x00ff) == 0x006e) {
530		/* nvsw: compatibility with older 0x*6e class identifier */
531		for (i = 0; !oclass && i < ret; i++) {
532			switch (sclass[i].oclass) {
533			case NVIF_CLASS_SW_NV04:
534			case NVIF_CLASS_SW_NV10:
535			case NVIF_CLASS_SW_NV50:
536			case NVIF_CLASS_SW_GF100:
537				oclass = sclass[i].oclass;
538				break;
539			default:
540				break;
541			}
542		}
543	} else
544	if ((init->class & 0x00ff) == 0x00b1) {
545		/* msvld: compatibility with incorrect version exposure */
546		for (i = 0; i < ret; i++) {
547			if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
548				oclass = sclass[i].oclass;
549				break;
550			}
551		}
552	} else
553	if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
554		/* mspdec: compatibility with incorrect version exposure */
555		for (i = 0; i < ret; i++) {
556			if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
557				oclass = sclass[i].oclass;
558				break;
559			}
560		}
561	} else
562	if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
563		/* msppp: compatibility with incorrect version exposure */
564		for (i = 0; i < ret; i++) {
565			if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
566				oclass = sclass[i].oclass;
567				break;
568			}
569		}
570	} else {
571		oclass = init->class;
572	}
573
574	nvif_object_sclass_put(&sclass);
575	if (!oclass)
576		return nouveau_abi16_put(abi16, -EINVAL);
577
578	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
579	if (!ntfy)
580		return nouveau_abi16_put(abi16, -ENOMEM);
581
582	list_add(&ntfy->head, &chan->notifiers);
583
584	client->route = NVDRM_OBJECT_ABI16;
585	ret = nvif_object_ctor(&chan->chan->user, "abi16EngObj", init->handle,
586			       oclass, NULL, 0, &ntfy->object);
587	client->route = NVDRM_OBJECT_NVIF;
588
589	if (ret)
590		nouveau_abi16_ntfy_fini(chan, ntfy);
591	return nouveau_abi16_put(abi16, ret);
592}
593
594int
595nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
596{
597	struct drm_nouveau_notifierobj_alloc *info = data;
598	struct nouveau_drm *drm = nouveau_drm(dev);
599	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
600	struct nouveau_abi16_chan *chan;
601	struct nouveau_abi16_ntfy *ntfy;
602	struct nvif_device *device = &abi16->device;
603	struct nvif_client *client;
604	struct nv_dma_v0 args = {};
605	int ret;
606
607	if (unlikely(!abi16))
608		return -ENOMEM;
 
609
610	/* completely unnecessary for these chipsets... */
611	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
612		return nouveau_abi16_put(abi16, -EINVAL);
613	client = abi16->device.object.client;
614
615	chan = nouveau_abi16_chan(abi16, info->channel);
616	if (!chan)
617		return nouveau_abi16_put(abi16, -ENOENT);
618
619	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
620	if (!ntfy)
621		return nouveau_abi16_put(abi16, -ENOMEM);
622
623	list_add(&ntfy->head, &chan->notifiers);
624
625	ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
626			   &ntfy->node);
627	if (ret)
628		goto done;
629
630	args.start = ntfy->node->offset;
631	args.limit = ntfy->node->offset + ntfy->node->length - 1;
632	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
633		args.target = NV_DMA_V0_TARGET_VM;
634		args.access = NV_DMA_V0_ACCESS_VM;
635		args.start += chan->ntfy_vma->addr;
636		args.limit += chan->ntfy_vma->addr;
637	} else
638	if (drm->agp.bridge) {
639		args.target = NV_DMA_V0_TARGET_AGP;
640		args.access = NV_DMA_V0_ACCESS_RDWR;
641		args.start += drm->agp.base + chan->ntfy->offset;
642		args.limit += drm->agp.base + chan->ntfy->offset;
643	} else {
644		args.target = NV_DMA_V0_TARGET_VM;
645		args.access = NV_DMA_V0_ACCESS_RDWR;
646		args.start += chan->ntfy->offset;
647		args.limit += chan->ntfy->offset;
648	}
649
650	client->route = NVDRM_OBJECT_ABI16;
651	ret = nvif_object_ctor(&chan->chan->user, "abi16Ntfy", info->handle,
652			       NV_DMA_IN_MEMORY, &args, sizeof(args),
653			       &ntfy->object);
654	client->route = NVDRM_OBJECT_NVIF;
655	if (ret)
656		goto done;
657
658	info->offset = ntfy->node->offset;
659done:
660	if (ret)
661		nouveau_abi16_ntfy_fini(chan, ntfy);
662	return nouveau_abi16_put(abi16, ret);
663}
664
665int
666nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
667{
668	struct drm_nouveau_gpuobj_free *fini = data;
669	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
670	struct nouveau_abi16_chan *chan;
671	struct nouveau_abi16_ntfy *ntfy;
672	int ret = -ENOENT;
673
674	if (unlikely(!abi16))
675		return -ENOMEM;
676
677	chan = nouveau_abi16_chan(abi16, fini->channel);
678	if (!chan)
679		return nouveau_abi16_put(abi16, -EINVAL);
680
681	/* synchronize with the user channel and destroy the gpu object */
682	nouveau_channel_idle(chan->chan);
683
684	list_for_each_entry(ntfy, &chan->notifiers, head) {
685		if (ntfy->object.handle == fini->handle) {
686			nouveau_abi16_ntfy_fini(chan, ntfy);
687			ret = 0;
688			break;
689		}
690	}
691
692	return nouveau_abi16_put(abi16, ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693}