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/cla06f.h>
31 #include <nvif/unpack.h>
33 #include "nouveau_drv.h"
34 #include "nouveau_dma.h"
35 #include "nouveau_gem.h"
36 #include "nouveau_chan.h"
37 #include "nouveau_abi16.h"
38 #include "nouveau_vmm.h"
40 static struct nouveau_abi16
*
41 nouveau_abi16(struct drm_file
*file_priv
)
43 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
45 struct nouveau_abi16
*abi16
;
46 cli
->abi16
= abi16
= kzalloc(sizeof(*abi16
), GFP_KERNEL
);
48 struct nv_device_v0 args
= {
52 INIT_LIST_HEAD(&abi16
->channels
);
54 /* allocate device object targeting client's default
55 * device (ie. the one that belongs to the fd it
58 if (nvif_device_init(&cli
->base
.object
, 0, NV_DEVICE
,
70 struct nouveau_abi16
*
71 nouveau_abi16_get(struct drm_file
*file_priv
)
73 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
74 mutex_lock(&cli
->mutex
);
75 if (nouveau_abi16(file_priv
))
77 mutex_unlock(&cli
->mutex
);
82 nouveau_abi16_put(struct nouveau_abi16
*abi16
, int ret
)
84 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
85 mutex_unlock(&cli
->mutex
);
90 nouveau_abi16_swclass(struct nouveau_drm
*drm
)
92 switch (drm
->client
.device
.info
.family
) {
93 case NV_DEVICE_INFO_V0_TNT
:
94 return NVIF_CLASS_SW_NV04
;
95 case NV_DEVICE_INFO_V0_CELSIUS
:
96 case NV_DEVICE_INFO_V0_KELVIN
:
97 case NV_DEVICE_INFO_V0_RANKINE
:
98 case NV_DEVICE_INFO_V0_CURIE
:
99 return NVIF_CLASS_SW_NV10
;
100 case NV_DEVICE_INFO_V0_TESLA
:
101 return NVIF_CLASS_SW_NV50
;
102 case NV_DEVICE_INFO_V0_FERMI
:
103 case NV_DEVICE_INFO_V0_KEPLER
:
104 case NV_DEVICE_INFO_V0_MAXWELL
:
105 case NV_DEVICE_INFO_V0_PASCAL
:
106 case NV_DEVICE_INFO_V0_VOLTA
:
107 return NVIF_CLASS_SW_GF100
;
114 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan
*chan
,
115 struct nouveau_abi16_ntfy
*ntfy
)
117 nvif_object_fini(&ntfy
->object
);
118 nvkm_mm_free(&chan
->heap
, &ntfy
->node
);
119 list_del(&ntfy
->head
);
124 nouveau_abi16_chan_fini(struct nouveau_abi16
*abi16
,
125 struct nouveau_abi16_chan
*chan
)
127 struct nouveau_abi16_ntfy
*ntfy
, *temp
;
129 /* wait for all activity to stop before releasing notify object, which
130 * may be still in use */
131 if (chan
->chan
&& chan
->ntfy
)
132 nouveau_channel_idle(chan
->chan
);
134 /* cleanup notifier state */
135 list_for_each_entry_safe(ntfy
, temp
, &chan
->notifiers
, head
) {
136 nouveau_abi16_ntfy_fini(chan
, ntfy
);
140 nouveau_vma_del(&chan
->ntfy_vma
);
141 nouveau_bo_unpin(chan
->ntfy
);
142 drm_gem_object_put_unlocked(&chan
->ntfy
->gem
);
145 if (chan
->heap
.block_size
)
146 nvkm_mm_fini(&chan
->heap
);
148 /* destroy channel object, all children will be killed too */
150 nouveau_channel_idle(chan
->chan
);
151 nouveau_channel_del(&chan
->chan
);
154 list_del(&chan
->head
);
159 nouveau_abi16_fini(struct nouveau_abi16
*abi16
)
161 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
162 struct nouveau_abi16_chan
*chan
, *temp
;
164 /* cleanup channels */
165 list_for_each_entry_safe(chan
, temp
, &abi16
->channels
, head
) {
166 nouveau_abi16_chan_fini(abi16
, chan
);
169 /* destroy the device object */
170 nvif_device_fini(&abi16
->device
);
177 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS
)
179 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
180 struct nouveau_drm
*drm
= nouveau_drm(dev
);
181 struct nvif_device
*device
= &drm
->client
.device
;
182 struct nvkm_gr
*gr
= nvxx_gr(device
);
183 struct drm_nouveau_getparam
*getparam
= data
;
185 switch (getparam
->param
) {
186 case NOUVEAU_GETPARAM_CHIPSET_ID
:
187 getparam
->value
= device
->info
.chipset
;
189 case NOUVEAU_GETPARAM_PCI_VENDOR
:
190 if (device
->info
.platform
!= NV_DEVICE_INFO_V0_SOC
)
191 getparam
->value
= dev
->pdev
->vendor
;
195 case NOUVEAU_GETPARAM_PCI_DEVICE
:
196 if (device
->info
.platform
!= NV_DEVICE_INFO_V0_SOC
)
197 getparam
->value
= dev
->pdev
->device
;
201 case NOUVEAU_GETPARAM_BUS_TYPE
:
202 switch (device
->info
.platform
) {
203 case NV_DEVICE_INFO_V0_AGP
: getparam
->value
= 0; break;
204 case NV_DEVICE_INFO_V0_PCI
: getparam
->value
= 1; break;
205 case NV_DEVICE_INFO_V0_PCIE
: getparam
->value
= 2; break;
206 case NV_DEVICE_INFO_V0_SOC
: getparam
->value
= 3; break;
207 case NV_DEVICE_INFO_V0_IGP
:
208 if (!pci_is_pcie(dev
->pdev
))
218 case NOUVEAU_GETPARAM_FB_SIZE
:
219 getparam
->value
= drm
->gem
.vram_available
;
221 case NOUVEAU_GETPARAM_AGP_SIZE
:
222 getparam
->value
= drm
->gem
.gart_available
;
224 case NOUVEAU_GETPARAM_VM_VRAM_BASE
:
225 getparam
->value
= 0; /* deprecated */
227 case NOUVEAU_GETPARAM_PTIMER_TIME
:
228 getparam
->value
= nvif_device_time(device
);
230 case NOUVEAU_GETPARAM_HAS_BO_USAGE
:
233 case NOUVEAU_GETPARAM_HAS_PAGEFLIP
:
236 case NOUVEAU_GETPARAM_GRAPH_UNITS
:
237 getparam
->value
= nvkm_gr_units(gr
);
240 NV_PRINTK(dbg
, cli
, "unknown parameter %lld\n", getparam
->param
);
248 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS
)
254 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS
)
256 struct drm_nouveau_channel_alloc
*init
= data
;
257 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
258 struct nouveau_drm
*drm
= nouveau_drm(dev
);
259 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
260 struct nouveau_abi16_chan
*chan
;
261 struct nvif_device
*device
;
265 if (unlikely(!abi16
))
269 return nouveau_abi16_put(abi16
, -ENODEV
);
271 device
= &abi16
->device
;
273 /* hack to allow channel engine type specification on kepler */
274 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
275 if (init
->fb_ctxdma_handle
== ~0) {
276 switch (init
->tt_ctxdma_handle
) {
277 case 0x01: engine
= NV_DEVICE_INFO_ENGINE_GR
; break;
278 case 0x02: engine
= NV_DEVICE_INFO_ENGINE_MSPDEC
; break;
279 case 0x04: engine
= NV_DEVICE_INFO_ENGINE_MSPPP
; break;
280 case 0x08: engine
= NV_DEVICE_INFO_ENGINE_MSVLD
; break;
281 case 0x30: engine
= NV_DEVICE_INFO_ENGINE_CE
; break;
283 return nouveau_abi16_put(abi16
, -ENOSYS
);
286 engine
= NV_DEVICE_INFO_ENGINE_GR
;
289 if (engine
!= NV_DEVICE_INFO_ENGINE_CE
)
290 engine
= nvif_fifo_runlist(device
, engine
);
292 engine
= nvif_fifo_runlist_ce(device
);
293 init
->fb_ctxdma_handle
= engine
;
294 init
->tt_ctxdma_handle
= 0;
297 if (init
->fb_ctxdma_handle
== ~0 || init
->tt_ctxdma_handle
== ~0)
298 return nouveau_abi16_put(abi16
, -EINVAL
);
300 /* allocate "abi16 channel" data and make up a handle for it */
301 chan
= kzalloc(sizeof(*chan
), GFP_KERNEL
);
303 return nouveau_abi16_put(abi16
, -ENOMEM
);
305 INIT_LIST_HEAD(&chan
->notifiers
);
306 list_add(&chan
->head
, &abi16
->channels
);
308 /* create channel object and initialise dma and fence management */
309 ret
= nouveau_channel_new(drm
, device
, init
->fb_ctxdma_handle
,
310 init
->tt_ctxdma_handle
, false, &chan
->chan
);
314 init
->channel
= chan
->chan
->chid
;
316 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
317 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
318 NOUVEAU_GEM_DOMAIN_GART
;
320 if (chan
->chan
->push
.buffer
->bo
.mem
.mem_type
== TTM_PL_VRAM
)
321 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
;
323 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_GART
;
325 if (device
->info
.family
< NV_DEVICE_INFO_V0_CELSIUS
) {
326 init
->subchan
[0].handle
= 0x00000000;
327 init
->subchan
[0].grclass
= 0x0000;
328 init
->subchan
[1].handle
= chan
->chan
->nvsw
.handle
;
329 init
->subchan
[1].grclass
= 0x506e;
330 init
->nr_subchan
= 2;
333 /* Named memory object area */
334 ret
= nouveau_gem_new(cli
, PAGE_SIZE
, 0, NOUVEAU_GEM_DOMAIN_GART
,
337 ret
= nouveau_bo_pin(chan
->ntfy
, TTM_PL_FLAG_TT
, false);
341 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
342 ret
= nouveau_vma_new(chan
->ntfy
, chan
->chan
->vmm
,
348 ret
= drm_gem_handle_create(file_priv
, &chan
->ntfy
->gem
,
349 &init
->notifier_handle
);
353 ret
= nvkm_mm_init(&chan
->heap
, 0, 0, PAGE_SIZE
, 1);
356 nouveau_abi16_chan_fini(abi16
, chan
);
357 return nouveau_abi16_put(abi16
, ret
);
360 static struct nouveau_abi16_chan
*
361 nouveau_abi16_chan(struct nouveau_abi16
*abi16
, int channel
)
363 struct nouveau_abi16_chan
*chan
;
365 list_for_each_entry(chan
, &abi16
->channels
, head
) {
366 if (chan
->chan
->chid
== channel
)
374 nouveau_abi16_usif(struct drm_file
*file_priv
, void *data
, u32 size
)
377 struct nvif_ioctl_v0 v0
;
379 struct nouveau_abi16_chan
*chan
;
380 struct nouveau_abi16
*abi16
;
383 if (!(ret
= nvif_unpack(ret
, &data
, &size
, args
->v0
, 0, 0, true))) {
384 switch (args
->v0
.type
) {
385 case NVIF_IOCTL_V0_NEW
:
386 case NVIF_IOCTL_V0_MTHD
:
387 case NVIF_IOCTL_V0_SCLASS
:
395 if (!(abi16
= nouveau_abi16(file_priv
)))
398 if (args
->v0
.token
!= ~0ULL) {
399 if (!(chan
= nouveau_abi16_chan(abi16
, args
->v0
.token
)))
401 args
->v0
.object
= nvif_handle(&chan
->chan
->user
);
402 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
406 args
->v0
.object
= nvif_handle(&abi16
->device
.object
);
407 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
412 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS
)
414 struct drm_nouveau_channel_free
*req
= data
;
415 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
416 struct nouveau_abi16_chan
*chan
;
418 if (unlikely(!abi16
))
421 chan
= nouveau_abi16_chan(abi16
, req
->channel
);
423 return nouveau_abi16_put(abi16
, -ENOENT
);
424 nouveau_abi16_chan_fini(abi16
, chan
);
425 return nouveau_abi16_put(abi16
, 0);
429 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS
)
431 struct drm_nouveau_grobj_alloc
*init
= data
;
432 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
433 struct nouveau_abi16_chan
*chan
;
434 struct nouveau_abi16_ntfy
*ntfy
;
435 struct nvif_client
*client
;
436 struct nvif_sclass
*sclass
;
440 if (unlikely(!abi16
))
443 if (init
->handle
== ~0)
444 return nouveau_abi16_put(abi16
, -EINVAL
);
445 client
= abi16
->device
.object
.client
;
447 chan
= nouveau_abi16_chan(abi16
, init
->channel
);
449 return nouveau_abi16_put(abi16
, -ENOENT
);
451 ret
= nvif_object_sclass_get(&chan
->chan
->user
, &sclass
);
453 return nouveau_abi16_put(abi16
, ret
);
455 if ((init
->class & 0x00ff) == 0x006e) {
456 /* nvsw: compatibility with older 0x*6e class identifier */
457 for (i
= 0; !oclass
&& i
< ret
; i
++) {
458 switch (sclass
[i
].oclass
) {
459 case NVIF_CLASS_SW_NV04
:
460 case NVIF_CLASS_SW_NV10
:
461 case NVIF_CLASS_SW_NV50
:
462 case NVIF_CLASS_SW_GF100
:
463 oclass
= sclass
[i
].oclass
;
470 if ((init
->class & 0x00ff) == 0x00b1) {
471 /* msvld: compatibility with incorrect version exposure */
472 for (i
= 0; i
< ret
; i
++) {
473 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b1) {
474 oclass
= sclass
[i
].oclass
;
479 if ((init
->class & 0x00ff) == 0x00b2) { /* mspdec */
480 /* mspdec: compatibility with incorrect version exposure */
481 for (i
= 0; i
< ret
; i
++) {
482 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b2) {
483 oclass
= sclass
[i
].oclass
;
488 if ((init
->class & 0x00ff) == 0x00b3) { /* msppp */
489 /* msppp: compatibility with incorrect version exposure */
490 for (i
= 0; i
< ret
; i
++) {
491 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b3) {
492 oclass
= sclass
[i
].oclass
;
497 oclass
= init
->class;
500 nvif_object_sclass_put(&sclass
);
502 return nouveau_abi16_put(abi16
, -EINVAL
);
504 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
506 return nouveau_abi16_put(abi16
, -ENOMEM
);
508 list_add(&ntfy
->head
, &chan
->notifiers
);
510 client
->route
= NVDRM_OBJECT_ABI16
;
511 ret
= nvif_object_init(&chan
->chan
->user
, init
->handle
, oclass
,
512 NULL
, 0, &ntfy
->object
);
513 client
->route
= NVDRM_OBJECT_NVIF
;
516 nouveau_abi16_ntfy_fini(chan
, ntfy
);
517 return nouveau_abi16_put(abi16
, ret
);
521 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS
)
523 struct drm_nouveau_notifierobj_alloc
*info
= data
;
524 struct nouveau_drm
*drm
= nouveau_drm(dev
);
525 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
526 struct nouveau_abi16_chan
*chan
;
527 struct nouveau_abi16_ntfy
*ntfy
;
528 struct nvif_device
*device
= &abi16
->device
;
529 struct nvif_client
*client
;
530 struct nv_dma_v0 args
= {};
533 if (unlikely(!abi16
))
536 /* completely unnecessary for these chipsets... */
537 if (unlikely(device
->info
.family
>= NV_DEVICE_INFO_V0_FERMI
))
538 return nouveau_abi16_put(abi16
, -EINVAL
);
539 client
= abi16
->device
.object
.client
;
541 chan
= nouveau_abi16_chan(abi16
, info
->channel
);
543 return nouveau_abi16_put(abi16
, -ENOENT
);
545 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
547 return nouveau_abi16_put(abi16
, -ENOMEM
);
549 list_add(&ntfy
->head
, &chan
->notifiers
);
551 ret
= nvkm_mm_head(&chan
->heap
, 0, 1, info
->size
, info
->size
, 1,
556 args
.start
= ntfy
->node
->offset
;
557 args
.limit
= ntfy
->node
->offset
+ ntfy
->node
->length
- 1;
558 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
559 args
.target
= NV_DMA_V0_TARGET_VM
;
560 args
.access
= NV_DMA_V0_ACCESS_VM
;
561 args
.start
+= chan
->ntfy_vma
->addr
;
562 args
.limit
+= chan
->ntfy_vma
->addr
;
564 if (drm
->agp
.bridge
) {
565 args
.target
= NV_DMA_V0_TARGET_AGP
;
566 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
567 args
.start
+= drm
->agp
.base
+ chan
->ntfy
->bo
.offset
;
568 args
.limit
+= drm
->agp
.base
+ chan
->ntfy
->bo
.offset
;
570 args
.target
= NV_DMA_V0_TARGET_VM
;
571 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
572 args
.start
+= chan
->ntfy
->bo
.offset
;
573 args
.limit
+= chan
->ntfy
->bo
.offset
;
576 client
->route
= NVDRM_OBJECT_ABI16
;
577 client
->super
= true;
578 ret
= nvif_object_init(&chan
->chan
->user
, info
->handle
,
579 NV_DMA_IN_MEMORY
, &args
, sizeof(args
),
581 client
->super
= false;
582 client
->route
= NVDRM_OBJECT_NVIF
;
586 info
->offset
= ntfy
->node
->offset
;
589 nouveau_abi16_ntfy_fini(chan
, ntfy
);
590 return nouveau_abi16_put(abi16
, ret
);
594 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS
)
596 struct drm_nouveau_gpuobj_free
*fini
= data
;
597 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
598 struct nouveau_abi16_chan
*chan
;
599 struct nouveau_abi16_ntfy
*ntfy
;
602 if (unlikely(!abi16
))
605 chan
= nouveau_abi16_chan(abi16
, fini
->channel
);
607 return nouveau_abi16_put(abi16
, -EINVAL
);
609 /* synchronize with the user channel and destroy the gpu object */
610 nouveau_channel_idle(chan
->chan
);
612 list_for_each_entry(ntfy
, &chan
->notifiers
, head
) {
613 if (ntfy
->object
.handle
== fini
->handle
) {
614 nouveau_abi16_ntfy_fini(chan
, ntfy
);
620 return nouveau_abi16_put(abi16
, ret
);