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 return NVIF_CLASS_SW_GF100
;
113 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan
*chan
,
114 struct nouveau_abi16_ntfy
*ntfy
)
116 nvif_object_fini(&ntfy
->object
);
117 nvkm_mm_free(&chan
->heap
, &ntfy
->node
);
118 list_del(&ntfy
->head
);
123 nouveau_abi16_chan_fini(struct nouveau_abi16
*abi16
,
124 struct nouveau_abi16_chan
*chan
)
126 struct nouveau_abi16_ntfy
*ntfy
, *temp
;
128 /* wait for all activity to stop before releasing notify object, which
129 * may be still in use */
130 if (chan
->chan
&& chan
->ntfy
)
131 nouveau_channel_idle(chan
->chan
);
133 /* cleanup notifier state */
134 list_for_each_entry_safe(ntfy
, temp
, &chan
->notifiers
, head
) {
135 nouveau_abi16_ntfy_fini(chan
, ntfy
);
139 nouveau_vma_del(&chan
->ntfy_vma
);
140 nouveau_bo_unpin(chan
->ntfy
);
141 drm_gem_object_unreference_unlocked(&chan
->ntfy
->gem
);
144 if (chan
->heap
.block_size
)
145 nvkm_mm_fini(&chan
->heap
);
147 /* destroy channel object, all children will be killed too */
149 nouveau_channel_idle(chan
->chan
);
150 nouveau_channel_del(&chan
->chan
);
153 list_del(&chan
->head
);
158 nouveau_abi16_fini(struct nouveau_abi16
*abi16
)
160 struct nouveau_cli
*cli
= (void *)abi16
->device
.object
.client
;
161 struct nouveau_abi16_chan
*chan
, *temp
;
163 /* cleanup channels */
164 list_for_each_entry_safe(chan
, temp
, &abi16
->channels
, head
) {
165 nouveau_abi16_chan_fini(abi16
, chan
);
168 /* destroy the device object */
169 nvif_device_fini(&abi16
->device
);
176 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS
)
178 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
179 struct nouveau_drm
*drm
= nouveau_drm(dev
);
180 struct nvif_device
*device
= &drm
->client
.device
;
181 struct nvkm_gr
*gr
= nvxx_gr(device
);
182 struct drm_nouveau_getparam
*getparam
= data
;
184 switch (getparam
->param
) {
185 case NOUVEAU_GETPARAM_CHIPSET_ID
:
186 getparam
->value
= device
->info
.chipset
;
188 case NOUVEAU_GETPARAM_PCI_VENDOR
:
189 if (device
->info
.platform
!= NV_DEVICE_INFO_V0_SOC
)
190 getparam
->value
= dev
->pdev
->vendor
;
194 case NOUVEAU_GETPARAM_PCI_DEVICE
:
195 if (device
->info
.platform
!= NV_DEVICE_INFO_V0_SOC
)
196 getparam
->value
= dev
->pdev
->device
;
200 case NOUVEAU_GETPARAM_BUS_TYPE
:
201 switch (device
->info
.platform
) {
202 case NV_DEVICE_INFO_V0_AGP
: getparam
->value
= 0; break;
203 case NV_DEVICE_INFO_V0_PCI
: getparam
->value
= 1; break;
204 case NV_DEVICE_INFO_V0_PCIE
: getparam
->value
= 2; break;
205 case NV_DEVICE_INFO_V0_SOC
: getparam
->value
= 3; break;
206 case NV_DEVICE_INFO_V0_IGP
:
207 if (!pci_is_pcie(dev
->pdev
))
216 case NOUVEAU_GETPARAM_FB_SIZE
:
217 getparam
->value
= drm
->gem
.vram_available
;
219 case NOUVEAU_GETPARAM_AGP_SIZE
:
220 getparam
->value
= drm
->gem
.gart_available
;
222 case NOUVEAU_GETPARAM_VM_VRAM_BASE
:
223 getparam
->value
= 0; /* deprecated */
225 case NOUVEAU_GETPARAM_PTIMER_TIME
:
226 getparam
->value
= nvif_device_time(device
);
228 case NOUVEAU_GETPARAM_HAS_BO_USAGE
:
231 case NOUVEAU_GETPARAM_HAS_PAGEFLIP
:
234 case NOUVEAU_GETPARAM_GRAPH_UNITS
:
235 getparam
->value
= nvkm_gr_units(gr
);
238 NV_PRINTK(dbg
, cli
, "unknown parameter %lld\n", getparam
->param
);
246 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS
)
252 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS
)
254 struct drm_nouveau_channel_alloc
*init
= data
;
255 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
256 struct nouveau_drm
*drm
= nouveau_drm(dev
);
257 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
258 struct nouveau_abi16_chan
*chan
;
259 struct nvif_device
*device
;
263 if (unlikely(!abi16
))
267 return nouveau_abi16_put(abi16
, -ENODEV
);
269 device
= &abi16
->device
;
271 /* hack to allow channel engine type specification on kepler */
272 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
273 if (init
->fb_ctxdma_handle
== ~0) {
274 switch (init
->tt_ctxdma_handle
) {
275 case 0x01: engine
= NV_DEVICE_INFO_ENGINE_GR
; break;
276 case 0x02: engine
= NV_DEVICE_INFO_ENGINE_MSPDEC
; break;
277 case 0x04: engine
= NV_DEVICE_INFO_ENGINE_MSPPP
; break;
278 case 0x08: engine
= NV_DEVICE_INFO_ENGINE_MSVLD
; break;
279 case 0x30: engine
= NV_DEVICE_INFO_ENGINE_CE
; break;
281 return nouveau_abi16_put(abi16
, -ENOSYS
);
284 engine
= NV_DEVICE_INFO_ENGINE_GR
;
287 if (engine
!= NV_DEVICE_INFO_ENGINE_CE
)
288 engine
= nvif_fifo_runlist(device
, engine
);
290 engine
= nvif_fifo_runlist_ce(device
);
291 init
->fb_ctxdma_handle
= engine
;
292 init
->tt_ctxdma_handle
= 0;
295 if (init
->fb_ctxdma_handle
== ~0 || init
->tt_ctxdma_handle
== ~0)
296 return nouveau_abi16_put(abi16
, -EINVAL
);
298 /* allocate "abi16 channel" data and make up a handle for it */
299 chan
= kzalloc(sizeof(*chan
), GFP_KERNEL
);
301 return nouveau_abi16_put(abi16
, -ENOMEM
);
303 INIT_LIST_HEAD(&chan
->notifiers
);
304 list_add(&chan
->head
, &abi16
->channels
);
306 /* create channel object and initialise dma and fence management */
307 ret
= nouveau_channel_new(drm
, device
, init
->fb_ctxdma_handle
,
308 init
->tt_ctxdma_handle
, &chan
->chan
);
312 init
->channel
= chan
->chan
->chid
;
314 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
315 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
316 NOUVEAU_GEM_DOMAIN_GART
;
318 if (chan
->chan
->push
.buffer
->bo
.mem
.mem_type
== TTM_PL_VRAM
)
319 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_VRAM
;
321 init
->pushbuf_domains
= NOUVEAU_GEM_DOMAIN_GART
;
323 if (device
->info
.family
< NV_DEVICE_INFO_V0_CELSIUS
) {
324 init
->subchan
[0].handle
= 0x00000000;
325 init
->subchan
[0].grclass
= 0x0000;
326 init
->subchan
[1].handle
= chan
->chan
->nvsw
.handle
;
327 init
->subchan
[1].grclass
= 0x506e;
328 init
->nr_subchan
= 2;
331 /* Named memory object area */
332 ret
= nouveau_gem_new(cli
, PAGE_SIZE
, 0, NOUVEAU_GEM_DOMAIN_GART
,
335 ret
= nouveau_bo_pin(chan
->ntfy
, TTM_PL_FLAG_TT
, false);
339 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
340 ret
= nouveau_vma_new(chan
->ntfy
, &cli
->vmm
, &chan
->ntfy_vma
);
345 ret
= drm_gem_handle_create(file_priv
, &chan
->ntfy
->gem
,
346 &init
->notifier_handle
);
350 ret
= nvkm_mm_init(&chan
->heap
, 0, 0, PAGE_SIZE
, 1);
353 nouveau_abi16_chan_fini(abi16
, chan
);
354 return nouveau_abi16_put(abi16
, ret
);
357 static struct nouveau_abi16_chan
*
358 nouveau_abi16_chan(struct nouveau_abi16
*abi16
, int channel
)
360 struct nouveau_abi16_chan
*chan
;
362 list_for_each_entry(chan
, &abi16
->channels
, head
) {
363 if (chan
->chan
->chid
== channel
)
371 nouveau_abi16_usif(struct drm_file
*file_priv
, void *data
, u32 size
)
374 struct nvif_ioctl_v0 v0
;
376 struct nouveau_abi16_chan
*chan
;
377 struct nouveau_abi16
*abi16
;
380 if (!(ret
= nvif_unpack(ret
, &data
, &size
, args
->v0
, 0, 0, true))) {
381 switch (args
->v0
.type
) {
382 case NVIF_IOCTL_V0_NEW
:
383 case NVIF_IOCTL_V0_MTHD
:
384 case NVIF_IOCTL_V0_SCLASS
:
392 if (!(abi16
= nouveau_abi16(file_priv
)))
395 if (args
->v0
.token
!= ~0ULL) {
396 if (!(chan
= nouveau_abi16_chan(abi16
, args
->v0
.token
)))
398 args
->v0
.object
= nvif_handle(&chan
->chan
->user
);
399 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
403 args
->v0
.object
= nvif_handle(&abi16
->device
.object
);
404 args
->v0
.owner
= NVIF_IOCTL_V0_OWNER_ANY
;
409 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS
)
411 struct drm_nouveau_channel_free
*req
= data
;
412 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
413 struct nouveau_abi16_chan
*chan
;
415 if (unlikely(!abi16
))
418 chan
= nouveau_abi16_chan(abi16
, req
->channel
);
420 return nouveau_abi16_put(abi16
, -ENOENT
);
421 nouveau_abi16_chan_fini(abi16
, chan
);
422 return nouveau_abi16_put(abi16
, 0);
426 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS
)
428 struct drm_nouveau_grobj_alloc
*init
= data
;
429 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
430 struct nouveau_abi16_chan
*chan
;
431 struct nouveau_abi16_ntfy
*ntfy
;
432 struct nvif_client
*client
;
433 struct nvif_sclass
*sclass
;
437 if (unlikely(!abi16
))
440 if (init
->handle
== ~0)
441 return nouveau_abi16_put(abi16
, -EINVAL
);
442 client
= abi16
->device
.object
.client
;
444 chan
= nouveau_abi16_chan(abi16
, init
->channel
);
446 return nouveau_abi16_put(abi16
, -ENOENT
);
448 ret
= nvif_object_sclass_get(&chan
->chan
->user
, &sclass
);
450 return nouveau_abi16_put(abi16
, ret
);
452 if ((init
->class & 0x00ff) == 0x006e) {
453 /* nvsw: compatibility with older 0x*6e class identifier */
454 for (i
= 0; !oclass
&& i
< ret
; i
++) {
455 switch (sclass
[i
].oclass
) {
456 case NVIF_CLASS_SW_NV04
:
457 case NVIF_CLASS_SW_NV10
:
458 case NVIF_CLASS_SW_NV50
:
459 case NVIF_CLASS_SW_GF100
:
460 oclass
= sclass
[i
].oclass
;
467 if ((init
->class & 0x00ff) == 0x00b1) {
468 /* msvld: compatibility with incorrect version exposure */
469 for (i
= 0; i
< ret
; i
++) {
470 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b1) {
471 oclass
= sclass
[i
].oclass
;
476 if ((init
->class & 0x00ff) == 0x00b2) { /* mspdec */
477 /* mspdec: compatibility with incorrect version exposure */
478 for (i
= 0; i
< ret
; i
++) {
479 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b2) {
480 oclass
= sclass
[i
].oclass
;
485 if ((init
->class & 0x00ff) == 0x00b3) { /* msppp */
486 /* msppp: compatibility with incorrect version exposure */
487 for (i
= 0; i
< ret
; i
++) {
488 if ((sclass
[i
].oclass
& 0x00ff) == 0x00b3) {
489 oclass
= sclass
[i
].oclass
;
494 oclass
= init
->class;
497 nvif_object_sclass_put(&sclass
);
499 return nouveau_abi16_put(abi16
, -EINVAL
);
501 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
503 return nouveau_abi16_put(abi16
, -ENOMEM
);
505 list_add(&ntfy
->head
, &chan
->notifiers
);
507 client
->route
= NVDRM_OBJECT_ABI16
;
508 ret
= nvif_object_init(&chan
->chan
->user
, init
->handle
, oclass
,
509 NULL
, 0, &ntfy
->object
);
510 client
->route
= NVDRM_OBJECT_NVIF
;
513 nouveau_abi16_ntfy_fini(chan
, ntfy
);
514 return nouveau_abi16_put(abi16
, ret
);
518 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS
)
520 struct drm_nouveau_notifierobj_alloc
*info
= data
;
521 struct nouveau_drm
*drm
= nouveau_drm(dev
);
522 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
523 struct nouveau_abi16_chan
*chan
;
524 struct nouveau_abi16_ntfy
*ntfy
;
525 struct nvif_device
*device
= &abi16
->device
;
526 struct nvif_client
*client
;
527 struct nv_dma_v0 args
= {};
530 if (unlikely(!abi16
))
533 /* completely unnecessary for these chipsets... */
534 if (unlikely(device
->info
.family
>= NV_DEVICE_INFO_V0_FERMI
))
535 return nouveau_abi16_put(abi16
, -EINVAL
);
536 client
= abi16
->device
.object
.client
;
538 chan
= nouveau_abi16_chan(abi16
, info
->channel
);
540 return nouveau_abi16_put(abi16
, -ENOENT
);
542 ntfy
= kzalloc(sizeof(*ntfy
), GFP_KERNEL
);
544 return nouveau_abi16_put(abi16
, -ENOMEM
);
546 list_add(&ntfy
->head
, &chan
->notifiers
);
548 ret
= nvkm_mm_head(&chan
->heap
, 0, 1, info
->size
, info
->size
, 1,
553 args
.start
= ntfy
->node
->offset
;
554 args
.limit
= ntfy
->node
->offset
+ ntfy
->node
->length
- 1;
555 if (device
->info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
556 args
.target
= NV_DMA_V0_TARGET_VM
;
557 args
.access
= NV_DMA_V0_ACCESS_VM
;
558 args
.start
+= chan
->ntfy_vma
->addr
;
559 args
.limit
+= chan
->ntfy_vma
->addr
;
561 if (drm
->agp
.bridge
) {
562 args
.target
= NV_DMA_V0_TARGET_AGP
;
563 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
564 args
.start
+= drm
->agp
.base
+ chan
->ntfy
->bo
.offset
;
565 args
.limit
+= drm
->agp
.base
+ chan
->ntfy
->bo
.offset
;
567 args
.target
= NV_DMA_V0_TARGET_VM
;
568 args
.access
= NV_DMA_V0_ACCESS_RDWR
;
569 args
.start
+= chan
->ntfy
->bo
.offset
;
570 args
.limit
+= chan
->ntfy
->bo
.offset
;
573 client
->route
= NVDRM_OBJECT_ABI16
;
574 client
->super
= true;
575 ret
= nvif_object_init(&chan
->chan
->user
, info
->handle
,
576 NV_DMA_IN_MEMORY
, &args
, sizeof(args
),
578 client
->super
= false;
579 client
->route
= NVDRM_OBJECT_NVIF
;
583 info
->offset
= ntfy
->node
->offset
;
586 nouveau_abi16_ntfy_fini(chan
, ntfy
);
587 return nouveau_abi16_put(abi16
, ret
);
591 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS
)
593 struct drm_nouveau_gpuobj_free
*fini
= data
;
594 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
595 struct nouveau_abi16_chan
*chan
;
596 struct nouveau_abi16_ntfy
*ntfy
;
599 if (unlikely(!abi16
))
602 chan
= nouveau_abi16_chan(abi16
, fini
->channel
);
604 return nouveau_abi16_put(abi16
, -EINVAL
);
606 /* synchronize with the user channel and destroy the gpu object */
607 nouveau_channel_idle(chan
->chan
);
609 list_for_each_entry(ntfy
, &chan
->notifiers
, head
) {
610 if (ntfy
->object
.handle
== fini
->handle
) {
611 nouveau_abi16_ntfy_fini(chan
, ntfy
);
617 return nouveau_abi16_put(abi16
, ret
);