2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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>
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"
41 static struct nouveau_abi16
*
42 nouveau_abi16(struct drm_file
*file_priv
)
44 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
46 struct nouveau_abi16
*abi16
;
47 cli
->abi16
= abi16
= kzalloc(sizeof(*abi16
), GFP_KERNEL
);
49 struct nv_device_v0 args
= {
53 INIT_LIST_HEAD(&abi16
->channels
);
55 /* allocate device object targeting client's default
56 * device (ie. the one that belongs to the fd it
59 if (nvif_device_ctor(&cli
->base
.object
, "abi16Device",
60 0, NV_DEVICE
, &args
, sizeof(args
),
71 struct nouveau_abi16
*
72 nouveau_abi16_get(struct drm_file
*file_priv
)
74 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
75 mutex_lock(&cli
->mutex
);
76 if (nouveau_abi16(file_priv
))
78 mutex_unlock(&cli
->mutex
);
83 nouveau_abi16_put(struct nouveau_abi16
*abi16
, int ret
)
85 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
86 mutex_unlock(&cli
->mutex
);
91 nouveau_abi16_swclass(struct nouveau_drm
*drm
)
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
;
115 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan
*chan
,
116 struct nouveau_abi16_ntfy
*ntfy
)
118 nvif_object_dtor(&ntfy
->object
);
119 nvkm_mm_free(&chan
->heap
, &ntfy
->node
);
120 list_del(&ntfy
->head
);
125 nouveau_abi16_chan_fini(struct nouveau_abi16
*abi16
,
126 struct nouveau_abi16_chan
*chan
)
128 struct nouveau_abi16_ntfy
*ntfy
, *temp
;
130 /* When a client exits without waiting for it's queued up jobs to
131 * finish it might happen that we fault the channel. This is due to
132 * drm_file_free() calling drm_gem_release() before the postclose()
133 * callback. Hence, we can't tear down this scheduler entity before
134 * uvmm mappings are unmapped. Currently, we can't detect this case.
136 * However, this should be rare and harmless, since the channel isn't
139 nouveau_sched_entity_fini(&chan
->sched_entity
);
141 /* wait for all activity to stop before cleaning up */
143 nouveau_channel_idle(chan
->chan
);
145 /* cleanup notifier state */
146 list_for_each_entry_safe(ntfy
, temp
, &chan
->notifiers
, head
) {
147 nouveau_abi16_ntfy_fini(chan
, ntfy
);
151 nouveau_vma_del(&chan
->ntfy_vma
);
152 nouveau_bo_unpin(chan
->ntfy
);
153 drm_gem_object_put(&chan
->ntfy
->bo
.base
);
156 if (chan
->heap
.block_size
)
157 nvkm_mm_fini(&chan
->heap
);
159 /* destroy channel object, all children will be killed too */
161 nvif_object_dtor(&chan
->ce
);
162 nouveau_channel_del(&chan
->chan
);
165 list_del(&chan
->head
);
170 nouveau_abi16_fini(struct nouveau_abi16
*abi16
)
172 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
173 struct nouveau_abi16_chan
*chan
, *temp
;
175 /* cleanup channels */
176 list_for_each_entry_safe(chan
, temp
, &abi16
->channels
, head
) {
177 nouveau_abi16_chan_fini(abi16
, chan
);
180 /* destroy the device object */
181 nvif_device_dtor(&abi16
->device
);
188 getparam_dma_ib_max(struct nvif_device
*device
)
190 const struct nvif_mclass dmas
[] = {
191 { NV03_CHANNEL_DMA
, 0 },
192 { NV10_CHANNEL_DMA
, 0 },
193 { NV17_CHANNEL_DMA
, 0 },
194 { NV40_CHANNEL_DMA
, 0 },
198 return nvif_mclass(&device
->object
, dmas
) < 0 ? NV50_DMA_IB_MAX
: 0;
202 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS
)
204 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
205 struct nouveau_drm
*drm
= nouveau_drm(dev
);
206 struct nvif_device
*device
= &drm
->client
.device
;
207 struct nvkm_gr
*gr
= nvxx_gr(device
);
208 struct drm_nouveau_getparam
*getparam
= data
;
209 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
211 switch (getparam
->param
) {
212 case NOUVEAU_GETPARAM_CHIPSET_ID
:
213 getparam
->value
= device
->info
.chipset
;
215 case NOUVEAU_GETPARAM_PCI_VENDOR
:
216 if (device
->info
.platform
!= NV_DEVICE_INFO_V0_SOC
)
217 getparam
->value
= pdev
->vendor
;
221 case NOUVEAU_GETPARAM_PCI_DEVICE
:
222 if (device
->info
.platform
!= NV_DEVICE_INFO_V0_SOC
)
223 getparam
->value
= pdev
->device
;
227 case NOUVEAU_GETPARAM_BUS_TYPE
:
228 switch (device
->info
.platform
) {
229 case NV_DEVICE_INFO_V0_AGP
: getparam
->value
= 0; break;
230 case NV_DEVICE_INFO_V0_PCI
: getparam
->value
= 1; break;
231 case NV_DEVICE_INFO_V0_PCIE
: getparam
->value
= 2; break;
232 case NV_DEVICE_INFO_V0_SOC
: getparam
->value
= 3; break;
233 case NV_DEVICE_INFO_V0_IGP
:
234 if (!pci_is_pcie(pdev
))
244 case NOUVEAU_GETPARAM_FB_SIZE
:
245 getparam
->value
= drm
->gem
.vram_available
;
247 case NOUVEAU_GETPARAM_AGP_SIZE
:
248 getparam
->value
= drm
->gem
.gart_available
;
250 case NOUVEAU_GETPARAM_VM_VRAM_BASE
:
251 getparam
->value
= 0; /* deprecated */
253 case NOUVEAU_GETPARAM_PTIMER_TIME
:
254 getparam
->value
= nvif_device_time(device
);
256 case NOUVEAU_GETPARAM_HAS_BO_USAGE
:
259 case NOUVEAU_GETPARAM_HAS_PAGEFLIP
:
262 case NOUVEAU_GETPARAM_GRAPH_UNITS
:
263 getparam
->value
= nvkm_gr_units(gr
);
265 case NOUVEAU_GETPARAM_EXEC_PUSH_MAX
: {
266 int ib_max
= getparam_dma_ib_max(device
);
268 getparam
->value
= nouveau_exec_push_max_from_ib_max(ib_max
);
272 NV_PRINTK(dbg
, cli
, "unknown parameter %lld\n", getparam
->param
);
280 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS
)
282 struct drm_nouveau_channel_alloc
*init
= data
;
283 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
284 struct nouveau_drm
*drm
= nouveau_drm(dev
);
285 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
286 struct nouveau_abi16_chan
*chan
;
287 struct nvif_device
*device
;
291 if (unlikely(!abi16
))
295 return nouveau_abi16_put(abi16
, -ENODEV
);
297 /* If uvmm wasn't initialized until now disable it completely to prevent
298 * userspace from mixing up UAPIs.
300 * The client lock is already acquired by nouveau_abi16_get().
302 __nouveau_cli_disable_uvmm_noinit(cli
);
304 device
= &abi16
->device
;
305 engine
= NV_DEVICE_HOST_RUNLIST_ENGINES_GR
;
307 /* hack to allow channel engine type specification on kepler */
308 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
309 if (init
->fb_ctxdma_handle
== ~0) {
310 switch (init
->tt_ctxdma_handle
) {
311 case 0x01: engine
= NV_DEVICE_HOST_RUNLIST_ENGINES_GR
; break;
312 case 0x02: engine
= NV_DEVICE_HOST_RUNLIST_ENGINES_MSPDEC
; break;
313 case 0x04: engine
= NV_DEVICE_HOST_RUNLIST_ENGINES_MSPPP
; break;
314 case 0x08: engine
= NV_DEVICE_HOST_RUNLIST_ENGINES_MSVLD
; break;
315 case 0x30: engine
= NV_DEVICE_HOST_RUNLIST_ENGINES_CE
; break;
317 return nouveau_abi16_put(abi16
, -ENOSYS
);
320 init
->fb_ctxdma_handle
= 0;
321 init
->tt_ctxdma_handle
= 0;
325 if (engine
!= NV_DEVICE_HOST_RUNLIST_ENGINES_CE
)
326 runm
= nvif_fifo_runlist(device
, engine
);
328 runm
= nvif_fifo_runlist_ce(device
);
330 if (!runm
|| init
->fb_ctxdma_handle
== ~0 || init
->tt_ctxdma_handle
== ~0)
331 return nouveau_abi16_put(abi16
, -EINVAL
);
333 /* allocate "abi16 channel" data and make up a handle for it */
334 chan
= kzalloc(sizeof(*chan
), GFP_KERNEL
);
336 return nouveau_abi16_put(abi16
, -ENOMEM
);
338 INIT_LIST_HEAD(&chan
->notifiers
);
339 list_add(&chan
->head
, &abi16
->channels
);
341 /* create channel object and initialise dma and fence management */
342 ret
= nouveau_channel_new(drm
, device
, false, runm
, init
->fb_ctxdma_handle
,
343 init
->tt_ctxdma_handle
, &chan
->chan
);
347 ret
= nouveau_sched_entity_init(&chan
->sched_entity
, &drm
->sched
,
352 init
->channel
= chan
->chan
->chid
;
354 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
355 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
356 NOUVEAU_GEM_DOMAIN_GART
;
358 if (chan
->chan
->push
.buffer
->bo
.resource
->mem_type
== TTM_PL_VRAM
)
359 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
;
361 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_GART
;
363 if (device
->info
.family
< NV_DEVICE_INFO_V0_CELSIUS
) {
364 init
->subchan
[0].handle
= 0x00000000;
365 init
->subchan
[0].grclass
= 0x0000;
366 init
->subchan
[1].handle
= chan
->chan
->nvsw
.handle
;
367 init
->subchan
[1].grclass
= 0x506e;
368 init
->nr_subchan
= 2;
371 /* Workaround "nvc0" gallium driver using classes it doesn't allocate on
372 * Kepler and above. NVKM no longer always sets CE_CTX_VALID as part of
373 * channel init, now we know what that stuff actually is.
375 * Doesn't matter for Kepler/Pascal, CE context stored in NV_RAMIN.
377 * Userspace was fixed prior to adding Ampere support.
379 switch (device
->info
.family
) {
380 case NV_DEVICE_INFO_V0_VOLTA
:
381 ret
= nvif_object_ctor(&chan
->chan
->user
, "abi16CeWar", 0, VOLTA_DMA_COPY_A
,
386 case NV_DEVICE_INFO_V0_TURING
:
387 ret
= nvif_object_ctor(&chan
->chan
->user
, "abi16CeWar", 0, TURING_DMA_COPY_A
,
396 /* Named memory object area */
397 ret
= nouveau_gem_new(cli
, PAGE_SIZE
, 0, NOUVEAU_GEM_DOMAIN_GART
,
400 ret
= nouveau_bo_pin(chan
->ntfy
, NOUVEAU_GEM_DOMAIN_GART
,
405 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
406 ret
= nouveau_vma_new(chan
->ntfy
, chan
->chan
->vmm
,
412 ret
= drm_gem_handle_create(file_priv
, &chan
->ntfy
->bo
.base
,
413 &init
->notifier_handle
);
417 ret
= nvkm_mm_init(&chan
->heap
, 0, 0, PAGE_SIZE
, 1);
420 nouveau_abi16_chan_fini(abi16
, chan
);
421 return nouveau_abi16_put(abi16
, ret
);
424 static struct nouveau_abi16_chan
*
425 nouveau_abi16_chan(struct nouveau_abi16
*abi16
, int channel
)
427 struct nouveau_abi16_chan
*chan
;
429 list_for_each_entry(chan
, &abi16
->channels
, head
) {
430 if (chan
->chan
->chid
== channel
)
438 nouveau_abi16_usif(struct drm_file
*file_priv
, void *data
, u32 size
)
441 struct nvif_ioctl_v0 v0
;
443 struct nouveau_abi16_chan
*chan
;
444 struct nouveau_abi16
*abi16
;
447 if (!(ret
= nvif_unpack(ret
, &data
, &size
, args
->v0
, 0, 0, true))) {
448 switch (args
->v0
.type
) {
449 case NVIF_IOCTL_V0_NEW
:
450 case NVIF_IOCTL_V0_MTHD
:
451 case NVIF_IOCTL_V0_SCLASS
:
459 if (!(abi16
= nouveau_abi16(file_priv
)))
462 if (args
->v0
.token
!= ~0ULL) {
463 if (!(chan
= nouveau_abi16_chan(abi16
, args
->v0
.token
)))
465 args
->v0
.object
= nvif_handle(&chan
->chan
->user
);
466 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
470 args
->v0
.object
= nvif_handle(&abi16
->device
.object
);
471 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
476 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS
)
478 struct drm_nouveau_channel_free
*req
= data
;
479 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
480 struct nouveau_abi16_chan
*chan
;
482 if (unlikely(!abi16
))
485 chan
= nouveau_abi16_chan(abi16
, req
->channel
);
487 return nouveau_abi16_put(abi16
, -ENOENT
);
488 nouveau_abi16_chan_fini(abi16
, chan
);
489 return nouveau_abi16_put(abi16
, 0);
493 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS
)
495 struct drm_nouveau_grobj_alloc
*init
= data
;
496 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
497 struct nouveau_abi16_chan
*chan
;
498 struct nouveau_abi16_ntfy
*ntfy
;
499 struct nvif_client
*client
;
500 struct nvif_sclass
*sclass
;
504 if (unlikely(!abi16
))
507 if (init
->handle
== ~0)
508 return nouveau_abi16_put(abi16
, -EINVAL
);
509 client
= abi16
->device
.object
.client
;
511 chan
= nouveau_abi16_chan(abi16
, init
->channel
);
513 return nouveau_abi16_put(abi16
, -ENOENT
);
515 ret
= nvif_object_sclass_get(&chan
->chan
->user
, &sclass
);
517 return nouveau_abi16_put(abi16
, ret
);
519 if ((init
->class & 0x00ff) == 0x006e) {
520 /* nvsw: compatibility with older 0x*6e class identifier */
521 for (i
= 0; !oclass
&& i
< ret
; i
++) {
522 switch (sclass
[i
].oclass
) {
523 case NVIF_CLASS_SW_NV04
:
524 case NVIF_CLASS_SW_NV10
:
525 case NVIF_CLASS_SW_NV50
:
526 case NVIF_CLASS_SW_GF100
:
527 oclass
= sclass
[i
].oclass
;
534 if ((init
->class & 0x00ff) == 0x00b1) {
535 /* msvld: compatibility with incorrect version exposure */
536 for (i
= 0; i
< ret
; i
++) {
537 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b1) {
538 oclass
= sclass
[i
].oclass
;
543 if ((init
->class & 0x00ff) == 0x00b2) { /* mspdec */
544 /* mspdec: compatibility with incorrect version exposure */
545 for (i
= 0; i
< ret
; i
++) {
546 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b2) {
547 oclass
= sclass
[i
].oclass
;
552 if ((init
->class & 0x00ff) == 0x00b3) { /* msppp */
553 /* msppp: compatibility with incorrect version exposure */
554 for (i
= 0; i
< ret
; i
++) {
555 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b3) {
556 oclass
= sclass
[i
].oclass
;
561 oclass
= init
->class;
564 nvif_object_sclass_put(&sclass
);
566 return nouveau_abi16_put(abi16
, -EINVAL
);
568 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
570 return nouveau_abi16_put(abi16
, -ENOMEM
);
572 list_add(&ntfy
->head
, &chan
->notifiers
);
574 client
->route
= NVDRM_OBJECT_ABI16
;
575 ret
= nvif_object_ctor(&chan
->chan
->user
, "abi16EngObj", init
->handle
,
576 oclass
, NULL
, 0, &ntfy
->object
);
577 client
->route
= NVDRM_OBJECT_NVIF
;
580 nouveau_abi16_ntfy_fini(chan
, ntfy
);
581 return nouveau_abi16_put(abi16
, ret
);
585 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS
)
587 struct drm_nouveau_notifierobj_alloc
*info
= data
;
588 struct nouveau_drm
*drm
= nouveau_drm(dev
);
589 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
590 struct nouveau_abi16_chan
*chan
;
591 struct nouveau_abi16_ntfy
*ntfy
;
592 struct nvif_device
*device
= &abi16
->device
;
593 struct nvif_client
*client
;
594 struct nv_dma_v0 args
= {};
597 if (unlikely(!abi16
))
600 /* completely unnecessary for these chipsets... */
601 if (unlikely(device
->info
.family
>= NV_DEVICE_INFO_V0_FERMI
))
602 return nouveau_abi16_put(abi16
, -EINVAL
);
603 client
= abi16
->device
.object
.client
;
605 chan
= nouveau_abi16_chan(abi16
, info
->channel
);
607 return nouveau_abi16_put(abi16
, -ENOENT
);
609 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
611 return nouveau_abi16_put(abi16
, -ENOMEM
);
613 list_add(&ntfy
->head
, &chan
->notifiers
);
615 ret
= nvkm_mm_head(&chan
->heap
, 0, 1, info
->size
, info
->size
, 1,
620 args
.start
= ntfy
->node
->offset
;
621 args
.limit
= ntfy
->node
->offset
+ ntfy
->node
->length
- 1;
622 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
623 args
.target
= NV_DMA_V0_TARGET_VM
;
624 args
.access
= NV_DMA_V0_ACCESS_VM
;
625 args
.start
+= chan
->ntfy_vma
->addr
;
626 args
.limit
+= chan
->ntfy_vma
->addr
;
628 if (drm
->agp
.bridge
) {
629 args
.target
= NV_DMA_V0_TARGET_AGP
;
630 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
631 args
.start
+= drm
->agp
.base
+ chan
->ntfy
->offset
;
632 args
.limit
+= drm
->agp
.base
+ chan
->ntfy
->offset
;
634 args
.target
= NV_DMA_V0_TARGET_VM
;
635 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
636 args
.start
+= chan
->ntfy
->offset
;
637 args
.limit
+= chan
->ntfy
->offset
;
640 client
->route
= NVDRM_OBJECT_ABI16
;
641 ret
= nvif_object_ctor(&chan
->chan
->user
, "abi16Ntfy", info
->handle
,
642 NV_DMA_IN_MEMORY
, &args
, sizeof(args
),
644 client
->route
= NVDRM_OBJECT_NVIF
;
648 info
->offset
= ntfy
->node
->offset
;
651 nouveau_abi16_ntfy_fini(chan
, ntfy
);
652 return nouveau_abi16_put(abi16
, ret
);
656 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS
)
658 struct drm_nouveau_gpuobj_free
*fini
= data
;
659 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
660 struct nouveau_abi16_chan
*chan
;
661 struct nouveau_abi16_ntfy
*ntfy
;
664 if (unlikely(!abi16
))
667 chan
= nouveau_abi16_chan(abi16
, fini
->channel
);
669 return nouveau_abi16_put(abi16
, -EINVAL
);
671 /* synchronize with the user channel and destroy the gpu object */
672 nouveau_channel_idle(chan
->chan
);
674 list_for_each_entry(ntfy
, &chan
->notifiers
, head
) {
675 if (ntfy
->object
.handle
== fini
->handle
) {
676 nouveau_abi16_ntfy_fini(chan
, ntfy
);
682 return nouveau_abi16_put(abi16
, ret
);