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/ioctl.h>
27 #include <nvif/class.h>
28 #include <nvif/cl0002.h>
29 #include <nvif/cla06f.h>
30 #include <nvif/unpack.h>
32 #include "nouveau_drv.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_gem.h"
35 #include "nouveau_chan.h"
36 #include "nouveau_abi16.h"
38 static struct nouveau_abi16
*
39 nouveau_abi16(struct drm_file
*file_priv
)
41 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
43 struct nouveau_abi16
*abi16
;
44 cli
->abi16
= abi16
= kzalloc(sizeof(*abi16
), GFP_KERNEL
);
46 struct nv_device_v0 args
= {
50 INIT_LIST_HEAD(&abi16
->channels
);
52 /* allocate device object targeting client's default
53 * device (ie. the one that belongs to the fd it
56 if (nvif_device_init(&cli
->base
.object
, 0, NV_DEVICE
,
68 struct nouveau_abi16
*
69 nouveau_abi16_get(struct drm_file
*file_priv
)
71 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
72 mutex_lock(&cli
->mutex
);
73 if (nouveau_abi16(file_priv
))
75 mutex_unlock(&cli
->mutex
);
80 nouveau_abi16_put(struct nouveau_abi16
*abi16
, int ret
)
82 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
83 mutex_unlock(&cli
->mutex
);
88 nouveau_abi16_swclass(struct nouveau_drm
*drm
)
90 switch (drm
->device
.info
.family
) {
91 case NV_DEVICE_INFO_V0_TNT
:
92 return NVIF_CLASS_SW_NV04
;
93 case NV_DEVICE_INFO_V0_CELSIUS
:
94 case NV_DEVICE_INFO_V0_KELVIN
:
95 case NV_DEVICE_INFO_V0_RANKINE
:
96 case NV_DEVICE_INFO_V0_CURIE
:
97 return NVIF_CLASS_SW_NV10
;
98 case NV_DEVICE_INFO_V0_TESLA
:
99 return NVIF_CLASS_SW_NV50
;
100 case NV_DEVICE_INFO_V0_FERMI
:
101 case NV_DEVICE_INFO_V0_KEPLER
:
102 case NV_DEVICE_INFO_V0_MAXWELL
:
103 return NVIF_CLASS_SW_GF100
;
110 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan
*chan
,
111 struct nouveau_abi16_ntfy
*ntfy
)
113 nvif_object_fini(&ntfy
->object
);
114 nvkm_mm_free(&chan
->heap
, &ntfy
->node
);
115 list_del(&ntfy
->head
);
120 nouveau_abi16_chan_fini(struct nouveau_abi16
*abi16
,
121 struct nouveau_abi16_chan
*chan
)
123 struct nouveau_abi16_ntfy
*ntfy
, *temp
;
125 /* wait for all activity to stop before releasing notify object, which
126 * may be still in use */
127 if (chan
->chan
&& chan
->ntfy
)
128 nouveau_channel_idle(chan
->chan
);
130 /* cleanup notifier state */
131 list_for_each_entry_safe(ntfy
, temp
, &chan
->notifiers
, head
) {
132 nouveau_abi16_ntfy_fini(chan
, ntfy
);
136 nouveau_bo_vma_del(chan
->ntfy
, &chan
->ntfy_vma
);
137 nouveau_bo_unpin(chan
->ntfy
);
138 drm_gem_object_unreference_unlocked(&chan
->ntfy
->gem
);
141 if (chan
->heap
.block_size
)
142 nvkm_mm_fini(&chan
->heap
);
144 /* destroy channel object, all children will be killed too */
146 nouveau_channel_idle(chan
->chan
);
147 nouveau_channel_del(&chan
->chan
);
150 list_del(&chan
->head
);
155 nouveau_abi16_fini(struct nouveau_abi16
*abi16
)
157 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
158 struct nouveau_abi16_chan
*chan
, *temp
;
160 /* cleanup channels */
161 list_for_each_entry_safe(chan
, temp
, &abi16
->channels
, head
) {
162 nouveau_abi16_chan_fini(abi16
, chan
);
165 /* destroy the device object */
166 nvif_device_fini(&abi16
->device
);
173 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS
)
175 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
176 struct nouveau_drm
*drm
= nouveau_drm(dev
);
177 struct nvif_device
*device
= &drm
->device
;
178 struct nvkm_gr
*gr
= nvxx_gr(device
);
179 struct drm_nouveau_getparam
*getparam
= data
;
181 switch (getparam
->param
) {
182 case NOUVEAU_GETPARAM_CHIPSET_ID
:
183 getparam
->value
= device
->info
.chipset
;
185 case NOUVEAU_GETPARAM_PCI_VENDOR
:
186 if (nvxx_device(device
)->func
->pci
)
187 getparam
->value
= dev
->pdev
->vendor
;
191 case NOUVEAU_GETPARAM_PCI_DEVICE
:
192 if (nvxx_device(device
)->func
->pci
)
193 getparam
->value
= dev
->pdev
->device
;
197 case NOUVEAU_GETPARAM_BUS_TYPE
:
198 if (!nvxx_device(device
)->func
->pci
)
201 if (drm_pci_device_is_agp(dev
))
204 if (!pci_is_pcie(dev
->pdev
))
209 case NOUVEAU_GETPARAM_FB_SIZE
:
210 getparam
->value
= drm
->gem
.vram_available
;
212 case NOUVEAU_GETPARAM_AGP_SIZE
:
213 getparam
->value
= drm
->gem
.gart_available
;
215 case NOUVEAU_GETPARAM_VM_VRAM_BASE
:
216 getparam
->value
= 0; /* deprecated */
218 case NOUVEAU_GETPARAM_PTIMER_TIME
:
219 getparam
->value
= nvif_device_time(device
);
221 case NOUVEAU_GETPARAM_HAS_BO_USAGE
:
224 case NOUVEAU_GETPARAM_HAS_PAGEFLIP
:
227 case NOUVEAU_GETPARAM_GRAPH_UNITS
:
228 getparam
->value
= nvkm_gr_units(gr
);
231 NV_PRINTK(dbg
, cli
, "unknown parameter %lld\n", getparam
->param
);
239 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS
)
245 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS
)
247 struct drm_nouveau_channel_alloc
*init
= data
;
248 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
249 struct nouveau_drm
*drm
= nouveau_drm(dev
);
250 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
251 struct nouveau_abi16_chan
*chan
;
252 struct nvif_device
*device
;
255 if (unlikely(!abi16
))
259 return nouveau_abi16_put(abi16
, -ENODEV
);
261 device
= &abi16
->device
;
263 /* hack to allow channel engine type specification on kepler */
264 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
265 if (init
->fb_ctxdma_handle
!= ~0)
266 init
->fb_ctxdma_handle
= NVA06F_V0_ENGINE_GR
;
268 init
->fb_ctxdma_handle
= 0;
269 #define _(A,B) if (init->tt_ctxdma_handle & (A)) init->fb_ctxdma_handle |= (B)
270 _(0x01, NVA06F_V0_ENGINE_GR
);
271 _(0x02, NVA06F_V0_ENGINE_MSPDEC
);
272 _(0x04, NVA06F_V0_ENGINE_MSPPP
);
273 _(0x08, NVA06F_V0_ENGINE_MSVLD
);
274 _(0x10, NVA06F_V0_ENGINE_CE0
);
275 _(0x20, NVA06F_V0_ENGINE_CE1
);
276 _(0x40, NVA06F_V0_ENGINE_MSENC
);
280 /* allow flips to be executed if this is a graphics channel */
281 init
->tt_ctxdma_handle
= 0;
282 if (init
->fb_ctxdma_handle
== NVA06F_V0_ENGINE_GR
)
283 init
->tt_ctxdma_handle
= 1;
286 if (init
->fb_ctxdma_handle
== ~0 || init
->tt_ctxdma_handle
== ~0)
287 return nouveau_abi16_put(abi16
, -EINVAL
);
289 /* allocate "abi16 channel" data and make up a handle for it */
290 chan
= kzalloc(sizeof(*chan
), GFP_KERNEL
);
292 return nouveau_abi16_put(abi16
, -ENOMEM
);
294 INIT_LIST_HEAD(&chan
->notifiers
);
295 list_add(&chan
->head
, &abi16
->channels
);
297 /* create channel object and initialise dma and fence management */
298 ret
= nouveau_channel_new(drm
, device
, init
->fb_ctxdma_handle
,
299 init
->tt_ctxdma_handle
, &chan
->chan
);
303 init
->channel
= chan
->chan
->chid
;
305 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
306 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
307 NOUVEAU_GEM_DOMAIN_GART
;
309 if (chan
->chan
->push
.buffer
->bo
.mem
.mem_type
== TTM_PL_VRAM
)
310 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
;
312 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_GART
;
314 if (device
->info
.family
< NV_DEVICE_INFO_V0_CELSIUS
) {
315 init
->subchan
[0].handle
= 0x00000000;
316 init
->subchan
[0].grclass
= 0x0000;
317 init
->subchan
[1].handle
= chan
->chan
->nvsw
.handle
;
318 init
->subchan
[1].grclass
= 0x506e;
319 init
->nr_subchan
= 2;
322 /* Named memory object area */
323 ret
= nouveau_gem_new(dev
, PAGE_SIZE
, 0, NOUVEAU_GEM_DOMAIN_GART
,
326 ret
= nouveau_bo_pin(chan
->ntfy
, TTM_PL_FLAG_TT
, false);
330 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
331 ret
= nouveau_bo_vma_add(chan
->ntfy
, cli
->vm
,
337 ret
= drm_gem_handle_create(file_priv
, &chan
->ntfy
->gem
,
338 &init
->notifier_handle
);
342 ret
= nvkm_mm_init(&chan
->heap
, 0, PAGE_SIZE
, 1);
345 nouveau_abi16_chan_fini(abi16
, chan
);
346 return nouveau_abi16_put(abi16
, ret
);
349 static struct nouveau_abi16_chan
*
350 nouveau_abi16_chan(struct nouveau_abi16
*abi16
, int channel
)
352 struct nouveau_abi16_chan
*chan
;
354 list_for_each_entry(chan
, &abi16
->channels
, head
) {
355 if (chan
->chan
->chid
== channel
)
363 nouveau_abi16_usif(struct drm_file
*file_priv
, void *data
, u32 size
)
366 struct nvif_ioctl_v0 v0
;
368 struct nouveau_abi16_chan
*chan
;
369 struct nouveau_abi16
*abi16
;
372 if (!(ret
= nvif_unpack(ret
, &data
, &size
, args
->v0
, 0, 0, true))) {
373 switch (args
->v0
.type
) {
374 case NVIF_IOCTL_V0_NEW
:
375 case NVIF_IOCTL_V0_MTHD
:
376 case NVIF_IOCTL_V0_SCLASS
:
384 if (!(abi16
= nouveau_abi16(file_priv
)))
387 if (args
->v0
.token
!= ~0ULL) {
388 if (!(chan
= nouveau_abi16_chan(abi16
, args
->v0
.token
)))
390 args
->v0
.object
= nvif_handle(&chan
->chan
->user
);
391 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
395 args
->v0
.object
= nvif_handle(&abi16
->device
.object
);
396 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
401 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS
)
403 struct drm_nouveau_channel_free
*req
= data
;
404 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
405 struct nouveau_abi16_chan
*chan
;
407 if (unlikely(!abi16
))
410 chan
= nouveau_abi16_chan(abi16
, req
->channel
);
412 return nouveau_abi16_put(abi16
, -ENOENT
);
413 nouveau_abi16_chan_fini(abi16
, chan
);
414 return nouveau_abi16_put(abi16
, 0);
418 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS
)
420 struct drm_nouveau_grobj_alloc
*init
= data
;
421 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
422 struct nouveau_abi16_chan
*chan
;
423 struct nouveau_abi16_ntfy
*ntfy
;
424 struct nvif_client
*client
;
425 struct nvif_sclass
*sclass
;
429 if (unlikely(!abi16
))
432 if (init
->handle
== ~0)
433 return nouveau_abi16_put(abi16
, -EINVAL
);
434 client
= abi16
->device
.object
.client
;
436 chan
= nouveau_abi16_chan(abi16
, init
->channel
);
438 return nouveau_abi16_put(abi16
, -ENOENT
);
440 ret
= nvif_object_sclass_get(&chan
->chan
->user
, &sclass
);
442 return nouveau_abi16_put(abi16
, ret
);
444 if ((init
->class & 0x00ff) == 0x006e) {
445 /* nvsw: compatibility with older 0x*6e class identifier */
446 for (i
= 0; !oclass
&& i
< ret
; i
++) {
447 switch (sclass
[i
].oclass
) {
448 case NVIF_CLASS_SW_NV04
:
449 case NVIF_CLASS_SW_NV10
:
450 case NVIF_CLASS_SW_NV50
:
451 case NVIF_CLASS_SW_GF100
:
452 oclass
= sclass
[i
].oclass
;
459 if ((init
->class & 0x00ff) == 0x00b1) {
460 /* msvld: compatibility with incorrect version exposure */
461 for (i
= 0; i
< ret
; i
++) {
462 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b1) {
463 oclass
= sclass
[i
].oclass
;
468 if ((init
->class & 0x00ff) == 0x00b2) { /* mspdec */
469 /* mspdec: compatibility with incorrect version exposure */
470 for (i
= 0; i
< ret
; i
++) {
471 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b2) {
472 oclass
= sclass
[i
].oclass
;
477 if ((init
->class & 0x00ff) == 0x00b3) { /* msppp */
478 /* msppp: compatibility with incorrect version exposure */
479 for (i
= 0; i
< ret
; i
++) {
480 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b3) {
481 oclass
= sclass
[i
].oclass
;
486 oclass
= init
->class;
489 nvif_object_sclass_put(&sclass
);
491 return nouveau_abi16_put(abi16
, -EINVAL
);
493 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
495 return nouveau_abi16_put(abi16
, -ENOMEM
);
497 list_add(&ntfy
->head
, &chan
->notifiers
);
499 client
->route
= NVDRM_OBJECT_ABI16
;
500 ret
= nvif_object_init(&chan
->chan
->user
, init
->handle
, oclass
,
501 NULL
, 0, &ntfy
->object
);
502 client
->route
= NVDRM_OBJECT_NVIF
;
505 nouveau_abi16_ntfy_fini(chan
, ntfy
);
506 return nouveau_abi16_put(abi16
, ret
);
510 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS
)
512 struct drm_nouveau_notifierobj_alloc
*info
= data
;
513 struct nouveau_drm
*drm
= nouveau_drm(dev
);
514 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
515 struct nouveau_abi16_chan
*chan
;
516 struct nouveau_abi16_ntfy
*ntfy
;
517 struct nvif_device
*device
= &abi16
->device
;
518 struct nvif_client
*client
;
519 struct nv_dma_v0 args
= {};
522 if (unlikely(!abi16
))
525 /* completely unnecessary for these chipsets... */
526 if (unlikely(device
->info
.family
>= NV_DEVICE_INFO_V0_FERMI
))
527 return nouveau_abi16_put(abi16
, -EINVAL
);
528 client
= abi16
->device
.object
.client
;
530 chan
= nouveau_abi16_chan(abi16
, info
->channel
);
532 return nouveau_abi16_put(abi16
, -ENOENT
);
534 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
536 return nouveau_abi16_put(abi16
, -ENOMEM
);
538 list_add(&ntfy
->head
, &chan
->notifiers
);
540 ret
= nvkm_mm_head(&chan
->heap
, 0, 1, info
->size
, info
->size
, 1,
545 args
.start
= ntfy
->node
->offset
;
546 args
.limit
= ntfy
->node
->offset
+ ntfy
->node
->length
- 1;
547 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
548 args
.target
= NV_DMA_V0_TARGET_VM
;
549 args
.access
= NV_DMA_V0_ACCESS_VM
;
550 args
.start
+= chan
->ntfy_vma
.offset
;
551 args
.limit
+= chan
->ntfy_vma
.offset
;
553 if (drm
->agp
.bridge
) {
554 args
.target
= NV_DMA_V0_TARGET_AGP
;
555 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
556 args
.start
+= drm
->agp
.base
+ chan
->ntfy
->bo
.offset
;
557 args
.limit
+= drm
->agp
.base
+ chan
->ntfy
->bo
.offset
;
559 args
.target
= NV_DMA_V0_TARGET_VM
;
560 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
561 args
.start
+= chan
->ntfy
->bo
.offset
;
562 args
.limit
+= chan
->ntfy
->bo
.offset
;
565 client
->route
= NVDRM_OBJECT_ABI16
;
566 client
->super
= true;
567 ret
= nvif_object_init(&chan
->chan
->user
, info
->handle
,
568 NV_DMA_IN_MEMORY
, &args
, sizeof(args
),
570 client
->super
= false;
571 client
->route
= NVDRM_OBJECT_NVIF
;
575 info
->offset
= ntfy
->node
->offset
;
578 nouveau_abi16_ntfy_fini(chan
, ntfy
);
579 return nouveau_abi16_put(abi16
, ret
);
583 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS
)
585 struct drm_nouveau_gpuobj_free
*fini
= data
;
586 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
587 struct nouveau_abi16_chan
*chan
;
588 struct nouveau_abi16_ntfy
*ntfy
;
591 if (unlikely(!abi16
))
594 chan
= nouveau_abi16_chan(abi16
, fini
->channel
);
596 return nouveau_abi16_put(abi16
, -EINVAL
);
598 /* synchronize with the user channel and destroy the gpu object */
599 nouveau_channel_idle(chan
->chan
);
601 list_for_each_entry(ntfy
, &chan
->notifiers
, head
) {
602 if (ntfy
->object
.handle
== fini
->handle
) {
603 nouveau_abi16_ntfy_fini(chan
, ntfy
);
609 return nouveau_abi16_put(abi16
, ret
);