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.
25 #include <linux/console.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/vga_switcheroo.h>
32 #include "drm_crtc_helper.h"
34 #include <core/device.h>
35 #include <core/gpuobj.h>
36 #include <core/option.h>
38 #include "nouveau_drm.h"
39 #include "nouveau_dma.h"
40 #include "nouveau_ttm.h"
41 #include "nouveau_gem.h"
42 #include "nouveau_agp.h"
43 #include "nouveau_vga.h"
44 #include "nouveau_sysfs.h"
45 #include "nouveau_hwmon.h"
46 #include "nouveau_acpi.h"
47 #include "nouveau_bios.h"
48 #include "nouveau_ioctl.h"
49 #include "nouveau_abi16.h"
50 #include "nouveau_fbcon.h"
51 #include "nouveau_fence.h"
52 #include "nouveau_debugfs.h"
53 #include "nouveau_usif.h"
55 MODULE_PARM_DESC(config
, "option string to pass to driver core");
56 static char *nouveau_config
;
57 module_param_named(config
, nouveau_config
, charp
, 0400);
59 MODULE_PARM_DESC(debug
, "debug string to pass to driver core");
60 static char *nouveau_debug
;
61 module_param_named(debug
, nouveau_debug
, charp
, 0400);
63 MODULE_PARM_DESC(noaccel
, "disable kernel/abi16 acceleration");
64 static int nouveau_noaccel
= 0;
65 module_param_named(noaccel
, nouveau_noaccel
, int, 0400);
67 MODULE_PARM_DESC(modeset
, "enable driver (default: auto, "
68 "0 = disabled, 1 = enabled, 2 = headless)");
69 int nouveau_modeset
= -1;
70 module_param_named(modeset
, nouveau_modeset
, int, 0400);
72 MODULE_PARM_DESC(runpm
, "disable (0), force enable (1), optimus only default (-1)");
73 int nouveau_runtime_pm
= -1;
74 module_param_named(runpm
, nouveau_runtime_pm
, int, 0400);
76 static struct drm_driver driver_stub
;
77 static struct drm_driver driver_pci
;
78 static struct drm_driver driver_platform
;
81 nouveau_pci_name(struct pci_dev
*pdev
)
83 u64 name
= (u64
)pci_domain_nr(pdev
->bus
) << 32;
84 name
|= pdev
->bus
->number
<< 16;
85 name
|= PCI_SLOT(pdev
->devfn
) << 8;
86 return name
| PCI_FUNC(pdev
->devfn
);
90 nouveau_platform_name(struct platform_device
*platformdev
)
92 return platformdev
->id
;
96 nouveau_name(struct drm_device
*dev
)
99 return nouveau_pci_name(dev
->pdev
);
101 return nouveau_platform_name(dev
->platformdev
);
105 nouveau_cli_create(u64 name
, const char *sname
,
106 int size
, void **pcli
)
108 struct nouveau_cli
*cli
= *pcli
= kzalloc(size
, GFP_KERNEL
);
110 int ret
= nvif_client_init(NULL
, NULL
, sname
, name
,
111 nouveau_config
, nouveau_debug
,
114 mutex_init(&cli
->mutex
);
115 usif_client_init(cli
);
123 nouveau_cli_destroy(struct nouveau_cli
*cli
)
125 nouveau_vm_ref(NULL
, &nvkm_client(&cli
->base
)->vm
, NULL
);
126 nvif_client_fini(&cli
->base
);
127 usif_client_fini(cli
);
131 nouveau_accel_fini(struct nouveau_drm
*drm
)
133 nouveau_channel_del(&drm
->channel
);
134 nvif_object_fini(&drm
->ntfy
);
135 nouveau_gpuobj_ref(NULL
, &drm
->notify
);
136 nvif_object_fini(&drm
->nvsw
);
137 nouveau_channel_del(&drm
->cechan
);
138 nvif_object_fini(&drm
->ttm
.copy
);
140 nouveau_fence(drm
)->dtor(drm
);
144 nouveau_accel_init(struct nouveau_drm
*drm
)
146 struct nvif_device
*device
= &drm
->device
;
154 /* initialise synchronisation routines */
155 /*XXX: this is crap, but the fence/channel stuff is a little
156 * backwards in some places. this will be fixed.
158 ret
= nvif_object_sclass(&device
->base
, sclass
, ARRAY_SIZE(sclass
));
162 for (ret
= -ENOSYS
, i
= 0; ret
&& i
< ARRAY_SIZE(sclass
); i
++) {
164 case NV03_CHANNEL_DMA
:
165 ret
= nv04_fence_create(drm
);
167 case NV10_CHANNEL_DMA
:
168 ret
= nv10_fence_create(drm
);
170 case NV17_CHANNEL_DMA
:
171 case NV40_CHANNEL_DMA
:
172 ret
= nv17_fence_create(drm
);
174 case NV50_CHANNEL_GPFIFO
:
175 ret
= nv50_fence_create(drm
);
177 case G82_CHANNEL_GPFIFO
:
178 ret
= nv84_fence_create(drm
);
180 case FERMI_CHANNEL_GPFIFO
:
181 case KEPLER_CHANNEL_GPFIFO_A
:
182 ret
= nvc0_fence_create(drm
);
190 NV_ERROR(drm
, "failed to initialise sync subsystem, %d\n", ret
);
191 nouveau_accel_fini(drm
);
195 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
196 ret
= nouveau_channel_new(drm
, &drm
->device
, NVDRM_CHAN
+ 1,
197 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0
|
198 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1
,
201 NV_ERROR(drm
, "failed to create ce channel, %d\n", ret
);
203 arg0
= KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR
;
206 if (device
->info
.chipset
>= 0xa3 &&
207 device
->info
.chipset
!= 0xaa &&
208 device
->info
.chipset
!= 0xac) {
209 ret
= nouveau_channel_new(drm
, &drm
->device
, NVDRM_CHAN
+ 1,
210 NvDmaFB
, NvDmaTT
, &drm
->cechan
);
212 NV_ERROR(drm
, "failed to create ce channel, %d\n", ret
);
221 ret
= nouveau_channel_new(drm
, &drm
->device
, NVDRM_CHAN
, arg0
, arg1
,
224 NV_ERROR(drm
, "failed to create kernel channel, %d\n", ret
);
225 nouveau_accel_fini(drm
);
229 ret
= nvif_object_init(drm
->channel
->object
, NULL
, NVDRM_NVSW
,
230 nouveau_abi16_swclass(drm
), NULL
, 0, &drm
->nvsw
);
232 struct nouveau_software_chan
*swch
;
233 ret
= RING_SPACE(drm
->channel
, 2);
235 if (device
->info
.family
< NV_DEVICE_INFO_V0_FERMI
) {
236 BEGIN_NV04(drm
->channel
, NvSubSw
, 0, 1);
237 OUT_RING (drm
->channel
, NVDRM_NVSW
);
239 if (device
->info
.family
< NV_DEVICE_INFO_V0_KEPLER
) {
240 BEGIN_NVC0(drm
->channel
, FermiSw
, 0, 1);
241 OUT_RING (drm
->channel
, 0x001f0000);
244 swch
= (void *)nvkm_object(&drm
->nvsw
)->parent
;
245 swch
->flip
= nouveau_flip_complete
;
246 swch
->flip_data
= drm
->channel
;
250 NV_ERROR(drm
, "failed to allocate software object, %d\n", ret
);
251 nouveau_accel_fini(drm
);
255 if (device
->info
.family
< NV_DEVICE_INFO_V0_FERMI
) {
256 ret
= nouveau_gpuobj_new(nvkm_object(&drm
->device
), NULL
, 32,
259 NV_ERROR(drm
, "failed to allocate notifier, %d\n", ret
);
260 nouveau_accel_fini(drm
);
264 ret
= nvif_object_init(drm
->channel
->object
, NULL
, NvNotify0
,
266 &(struct nv_dma_v0
) {
267 .target
= NV_DMA_V0_TARGET_VRAM
,
268 .access
= NV_DMA_V0_ACCESS_RDWR
,
269 .start
= drm
->notify
->addr
,
270 .limit
= drm
->notify
->addr
+ 31
271 }, sizeof(struct nv_dma_v0
),
274 nouveau_accel_fini(drm
);
280 nouveau_bo_move_init(drm
);
283 static int nouveau_drm_probe(struct pci_dev
*pdev
,
284 const struct pci_device_id
*pent
)
286 struct nouveau_device
*device
;
287 struct apertures_struct
*aper
;
291 /* remove conflicting drivers (vesafb, efifb etc) */
292 aper
= alloc_apertures(3);
296 aper
->ranges
[0].base
= pci_resource_start(pdev
, 1);
297 aper
->ranges
[0].size
= pci_resource_len(pdev
, 1);
300 if (pci_resource_len(pdev
, 2)) {
301 aper
->ranges
[aper
->count
].base
= pci_resource_start(pdev
, 2);
302 aper
->ranges
[aper
->count
].size
= pci_resource_len(pdev
, 2);
306 if (pci_resource_len(pdev
, 3)) {
307 aper
->ranges
[aper
->count
].base
= pci_resource_start(pdev
, 3);
308 aper
->ranges
[aper
->count
].size
= pci_resource_len(pdev
, 3);
313 boot
= pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
;
315 if (nouveau_modeset
!= 2)
316 remove_conflicting_framebuffers(aper
, "nouveaufb", boot
);
319 ret
= nouveau_device_create(pdev
, NOUVEAU_BUS_PCI
,
320 nouveau_pci_name(pdev
), pci_name(pdev
),
321 nouveau_config
, nouveau_debug
, &device
);
325 pci_set_master(pdev
);
327 ret
= drm_get_pci_dev(pdev
, pent
, &driver_pci
);
329 nouveau_object_ref(NULL
, (struct nouveau_object
**)&device
);
336 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
339 nouveau_get_hdmi_dev(struct nouveau_drm
*drm
)
341 struct pci_dev
*pdev
= drm
->dev
->pdev
;
344 DRM_INFO("not a PCI device; no HDMI\n");
345 drm
->hdmi_device
= NULL
;
349 /* subfunction one is a hdmi audio device? */
350 drm
->hdmi_device
= pci_get_bus_and_slot((unsigned int)pdev
->bus
->number
,
351 PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 1));
353 if (!drm
->hdmi_device
) {
354 NV_DEBUG(drm
, "hdmi device not found %d %d %d\n", pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), 1);
358 if ((drm
->hdmi_device
->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO
) {
359 NV_DEBUG(drm
, "possible hdmi device not audio %d\n", drm
->hdmi_device
->class);
360 pci_dev_put(drm
->hdmi_device
);
361 drm
->hdmi_device
= NULL
;
367 nouveau_drm_load(struct drm_device
*dev
, unsigned long flags
)
369 struct pci_dev
*pdev
= dev
->pdev
;
370 struct nouveau_drm
*drm
;
373 ret
= nouveau_cli_create(nouveau_name(dev
), "DRM", sizeof(*drm
),
378 dev
->dev_private
= drm
;
380 nvkm_client(&drm
->client
.base
)->debug
=
381 nouveau_dbgopt(nouveau_debug
, "DRM");
383 INIT_LIST_HEAD(&drm
->clients
);
384 spin_lock_init(&drm
->tile
.lock
);
386 nouveau_get_hdmi_dev(drm
);
388 /* make sure AGP controller is in a consistent state before we
389 * (possibly) execute vbios init tables (see nouveau_agp.h)
391 if (pdev
&& drm_pci_device_is_agp(dev
) && dev
->agp
) {
392 const u64 enables
= NV_DEVICE_V0_DISABLE_IDENTIFY
|
393 NV_DEVICE_V0_DISABLE_MMIO
;
394 /* dummy device object, doesn't init anything, but allows
395 * agp code access to registers
397 ret
= nvif_device_init(&drm
->client
.base
.base
, NULL
,
398 NVDRM_DEVICE
, NV_DEVICE
,
399 &(struct nv_device_v0
) {
403 }, sizeof(struct nv_device_v0
),
408 nouveau_agp_reset(drm
);
409 nvif_device_fini(&drm
->device
);
412 ret
= nvif_device_init(&drm
->client
.base
.base
, NULL
, NVDRM_DEVICE
,
414 &(struct nv_device_v0
) {
418 }, sizeof(struct nv_device_v0
),
423 dev
->irq_enabled
= true;
425 /* workaround an odd issue on nvc1 by disabling the device's
426 * nosnoop capability. hopefully won't cause issues until a
427 * better fix is found - assuming there is one...
429 if (drm
->device
.info
.chipset
== 0xc1)
430 nvif_mask(&drm
->device
, 0x00088080, 0x00000800, 0x00000000);
432 nouveau_vga_init(drm
);
433 nouveau_agp_init(drm
);
435 if (drm
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
436 ret
= nouveau_vm_new(nvkm_device(&drm
->device
), 0, (1ULL << 40),
437 0x1000, &drm
->client
.vm
);
441 nvkm_client(&drm
->client
.base
)->vm
= drm
->client
.vm
;
444 ret
= nouveau_ttm_init(drm
);
448 ret
= nouveau_bios_init(dev
);
452 ret
= nouveau_display_create(dev
);
456 if (dev
->mode_config
.num_crtc
) {
457 ret
= nouveau_display_init(dev
);
462 nouveau_sysfs_init(dev
);
463 nouveau_hwmon_init(dev
);
464 nouveau_accel_init(drm
);
465 nouveau_fbcon_init(dev
);
467 if (nouveau_runtime_pm
!= 0) {
468 pm_runtime_use_autosuspend(dev
->dev
);
469 pm_runtime_set_autosuspend_delay(dev
->dev
, 5000);
470 pm_runtime_set_active(dev
->dev
);
471 pm_runtime_allow(dev
->dev
);
472 pm_runtime_mark_last_busy(dev
->dev
);
473 pm_runtime_put(dev
->dev
);
478 nouveau_display_destroy(dev
);
480 nouveau_bios_takedown(dev
);
482 nouveau_ttm_fini(drm
);
484 nouveau_agp_fini(drm
);
485 nouveau_vga_fini(drm
);
487 nvif_device_fini(&drm
->device
);
488 nouveau_cli_destroy(&drm
->client
);
493 nouveau_drm_unload(struct drm_device
*dev
)
495 struct nouveau_drm
*drm
= nouveau_drm(dev
);
497 pm_runtime_get_sync(dev
->dev
);
498 nouveau_fbcon_fini(dev
);
499 nouveau_accel_fini(drm
);
500 nouveau_hwmon_fini(dev
);
501 nouveau_sysfs_fini(dev
);
503 if (dev
->mode_config
.num_crtc
)
504 nouveau_display_fini(dev
);
505 nouveau_display_destroy(dev
);
507 nouveau_bios_takedown(dev
);
509 nouveau_ttm_fini(drm
);
510 nouveau_agp_fini(drm
);
511 nouveau_vga_fini(drm
);
513 nvif_device_fini(&drm
->device
);
514 if (drm
->hdmi_device
)
515 pci_dev_put(drm
->hdmi_device
);
516 nouveau_cli_destroy(&drm
->client
);
521 nouveau_drm_device_remove(struct drm_device
*dev
)
523 struct nouveau_drm
*drm
= nouveau_drm(dev
);
524 struct nouveau_client
*client
;
525 struct nouveau_object
*device
;
527 dev
->irq_enabled
= false;
528 client
= nvkm_client(&drm
->client
.base
);
529 device
= client
->device
;
532 nouveau_object_ref(NULL
, &device
);
533 nouveau_object_debug();
535 EXPORT_SYMBOL(nouveau_drm_device_remove
);
538 nouveau_drm_remove(struct pci_dev
*pdev
)
540 struct drm_device
*dev
= pci_get_drvdata(pdev
);
542 nouveau_drm_device_remove(dev
);
546 nouveau_do_suspend(struct drm_device
*dev
, bool runtime
)
548 struct nouveau_drm
*drm
= nouveau_drm(dev
);
549 struct nouveau_cli
*cli
;
552 if (dev
->mode_config
.num_crtc
&& !runtime
) {
553 NV_INFO(drm
, "suspending display...\n");
554 ret
= nouveau_display_suspend(dev
);
559 NV_INFO(drm
, "evicting buffers...\n");
560 ttm_bo_evict_mm(&drm
->ttm
.bdev
, TTM_PL_VRAM
);
562 NV_INFO(drm
, "waiting for kernel channels to go idle...\n");
564 ret
= nouveau_channel_idle(drm
->cechan
);
570 ret
= nouveau_channel_idle(drm
->channel
);
575 NV_INFO(drm
, "suspending client object trees...\n");
576 if (drm
->fence
&& nouveau_fence(drm
)->suspend
) {
577 if (!nouveau_fence(drm
)->suspend(drm
)) {
583 list_for_each_entry(cli
, &drm
->clients
, head
) {
584 ret
= nvif_client_suspend(&cli
->base
);
589 NV_INFO(drm
, "suspending kernel object tree...\n");
590 ret
= nvif_client_suspend(&drm
->client
.base
);
594 nouveau_agp_fini(drm
);
598 list_for_each_entry_continue_reverse(cli
, &drm
->clients
, head
) {
599 nvif_client_resume(&cli
->base
);
602 if (drm
->fence
&& nouveau_fence(drm
)->resume
)
603 nouveau_fence(drm
)->resume(drm
);
606 if (dev
->mode_config
.num_crtc
) {
607 NV_INFO(drm
, "resuming display...\n");
608 nouveau_display_resume(dev
);
613 int nouveau_pmops_suspend(struct device
*dev
)
615 struct pci_dev
*pdev
= to_pci_dev(dev
);
616 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
619 if (drm_dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
||
620 drm_dev
->switch_power_state
== DRM_SWITCH_POWER_DYNAMIC_OFF
)
623 if (drm_dev
->mode_config
.num_crtc
)
624 nouveau_fbcon_set_suspend(drm_dev
, 1);
626 ret
= nouveau_do_suspend(drm_dev
, false);
630 pci_save_state(pdev
);
631 pci_disable_device(pdev
);
632 pci_set_power_state(pdev
, PCI_D3hot
);
637 nouveau_do_resume(struct drm_device
*dev
)
639 struct nouveau_drm
*drm
= nouveau_drm(dev
);
640 struct nouveau_cli
*cli
;
642 NV_INFO(drm
, "re-enabling device...\n");
644 nouveau_agp_reset(drm
);
646 NV_INFO(drm
, "resuming kernel object tree...\n");
647 nvif_client_resume(&drm
->client
.base
);
648 nouveau_agp_init(drm
);
650 NV_INFO(drm
, "resuming client object trees...\n");
651 if (drm
->fence
&& nouveau_fence(drm
)->resume
)
652 nouveau_fence(drm
)->resume(drm
);
654 list_for_each_entry(cli
, &drm
->clients
, head
) {
655 nvif_client_resume(&cli
->base
);
658 nouveau_run_vbios_init(dev
);
660 if (dev
->mode_config
.num_crtc
) {
661 NV_INFO(drm
, "resuming display...\n");
662 nouveau_display_repin(dev
);
668 int nouveau_pmops_resume(struct device
*dev
)
670 struct pci_dev
*pdev
= to_pci_dev(dev
);
671 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
674 if (drm_dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
||
675 drm_dev
->switch_power_state
== DRM_SWITCH_POWER_DYNAMIC_OFF
)
678 pci_set_power_state(pdev
, PCI_D0
);
679 pci_restore_state(pdev
);
680 ret
= pci_enable_device(pdev
);
683 pci_set_master(pdev
);
685 ret
= nouveau_do_resume(drm_dev
);
689 if (drm_dev
->mode_config
.num_crtc
) {
690 nouveau_display_resume(drm_dev
);
691 nouveau_fbcon_set_suspend(drm_dev
, 0);
697 static int nouveau_pmops_freeze(struct device
*dev
)
699 struct pci_dev
*pdev
= to_pci_dev(dev
);
700 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
703 if (drm_dev
->mode_config
.num_crtc
)
704 nouveau_fbcon_set_suspend(drm_dev
, 1);
706 ret
= nouveau_do_suspend(drm_dev
, false);
710 static int nouveau_pmops_thaw(struct device
*dev
)
712 struct pci_dev
*pdev
= to_pci_dev(dev
);
713 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
716 ret
= nouveau_do_resume(drm_dev
);
720 if (drm_dev
->mode_config
.num_crtc
) {
721 nouveau_display_resume(drm_dev
);
722 nouveau_fbcon_set_suspend(drm_dev
, 0);
730 nouveau_drm_open(struct drm_device
*dev
, struct drm_file
*fpriv
)
732 struct nouveau_drm
*drm
= nouveau_drm(dev
);
733 struct nouveau_cli
*cli
;
734 char name
[32], tmpname
[TASK_COMM_LEN
];
737 /* need to bring up power immediately if opening device */
738 ret
= pm_runtime_get_sync(dev
->dev
);
739 if (ret
< 0 && ret
!= -EACCES
)
742 get_task_comm(tmpname
, current
);
743 snprintf(name
, sizeof(name
), "%s[%d]", tmpname
, pid_nr(fpriv
->pid
));
745 ret
= nouveau_cli_create(nouveau_name(dev
), name
, sizeof(*cli
),
751 cli
->base
.super
= false;
753 if (drm
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
754 ret
= nouveau_vm_new(nvkm_device(&drm
->device
), 0, (1ULL << 40),
757 nouveau_cli_destroy(cli
);
761 nvkm_client(&cli
->base
)->vm
= cli
->vm
;
764 fpriv
->driver_priv
= cli
;
766 mutex_lock(&drm
->client
.mutex
);
767 list_add(&cli
->head
, &drm
->clients
);
768 mutex_unlock(&drm
->client
.mutex
);
771 pm_runtime_mark_last_busy(dev
->dev
);
772 pm_runtime_put_autosuspend(dev
->dev
);
778 nouveau_drm_preclose(struct drm_device
*dev
, struct drm_file
*fpriv
)
780 struct nouveau_cli
*cli
= nouveau_cli(fpriv
);
781 struct nouveau_drm
*drm
= nouveau_drm(dev
);
783 pm_runtime_get_sync(dev
->dev
);
786 nouveau_abi16_fini(cli
->abi16
);
788 mutex_lock(&drm
->client
.mutex
);
789 list_del(&cli
->head
);
790 mutex_unlock(&drm
->client
.mutex
);
795 nouveau_drm_postclose(struct drm_device
*dev
, struct drm_file
*fpriv
)
797 struct nouveau_cli
*cli
= nouveau_cli(fpriv
);
798 nouveau_cli_destroy(cli
);
799 pm_runtime_mark_last_busy(dev
->dev
);
800 pm_runtime_put_autosuspend(dev
->dev
);
803 static const struct drm_ioctl_desc
805 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM
, nouveau_abi16_ioctl_getparam
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
806 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM
, nouveau_abi16_ioctl_setparam
, DRM_UNLOCKED
|DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
807 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC
, nouveau_abi16_ioctl_channel_alloc
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
808 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE
, nouveau_abi16_ioctl_channel_free
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
809 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC
, nouveau_abi16_ioctl_grobj_alloc
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
810 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC
, nouveau_abi16_ioctl_notifierobj_alloc
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
811 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE
, nouveau_abi16_ioctl_gpuobj_free
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
812 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW
, nouveau_gem_ioctl_new
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
813 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF
, nouveau_gem_ioctl_pushbuf
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
814 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP
, nouveau_gem_ioctl_cpu_prep
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
815 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI
, nouveau_gem_ioctl_cpu_fini
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
816 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO
, nouveau_gem_ioctl_info
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
820 nouveau_drm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
822 struct drm_file
*filp
= file
->private_data
;
823 struct drm_device
*dev
= filp
->minor
->dev
;
826 ret
= pm_runtime_get_sync(dev
->dev
);
827 if (ret
< 0 && ret
!= -EACCES
)
830 switch (_IOC_NR(cmd
) - DRM_COMMAND_BASE
) {
831 case DRM_NOUVEAU_NVIF
:
832 ret
= usif_ioctl(filp
, (void __user
*)arg
, _IOC_SIZE(cmd
));
835 ret
= drm_ioctl(file
, cmd
, arg
);
839 pm_runtime_mark_last_busy(dev
->dev
);
840 pm_runtime_put_autosuspend(dev
->dev
);
844 static const struct file_operations
845 nouveau_driver_fops
= {
846 .owner
= THIS_MODULE
,
848 .release
= drm_release
,
849 .unlocked_ioctl
= nouveau_drm_ioctl
,
850 .mmap
= nouveau_ttm_mmap
,
853 #if defined(CONFIG_COMPAT)
854 .compat_ioctl
= nouveau_compat_ioctl
,
856 .llseek
= noop_llseek
,
859 static struct drm_driver
863 DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
| DRIVER_RENDER
,
865 .load
= nouveau_drm_load
,
866 .unload
= nouveau_drm_unload
,
867 .open
= nouveau_drm_open
,
868 .preclose
= nouveau_drm_preclose
,
869 .postclose
= nouveau_drm_postclose
,
870 .lastclose
= nouveau_vga_lastclose
,
872 #if defined(CONFIG_DEBUG_FS)
873 .debugfs_init
= nouveau_debugfs_init
,
874 .debugfs_cleanup
= nouveau_debugfs_takedown
,
877 .get_vblank_counter
= drm_vblank_count
,
878 .enable_vblank
= nouveau_display_vblank_enable
,
879 .disable_vblank
= nouveau_display_vblank_disable
,
880 .get_scanout_position
= nouveau_display_scanoutpos
,
881 .get_vblank_timestamp
= nouveau_display_vblstamp
,
883 .ioctls
= nouveau_ioctls
,
884 .num_ioctls
= ARRAY_SIZE(nouveau_ioctls
),
885 .fops
= &nouveau_driver_fops
,
887 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
888 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
889 .gem_prime_export
= drm_gem_prime_export
,
890 .gem_prime_import
= drm_gem_prime_import
,
891 .gem_prime_pin
= nouveau_gem_prime_pin
,
892 .gem_prime_res_obj
= nouveau_gem_prime_res_obj
,
893 .gem_prime_unpin
= nouveau_gem_prime_unpin
,
894 .gem_prime_get_sg_table
= nouveau_gem_prime_get_sg_table
,
895 .gem_prime_import_sg_table
= nouveau_gem_prime_import_sg_table
,
896 .gem_prime_vmap
= nouveau_gem_prime_vmap
,
897 .gem_prime_vunmap
= nouveau_gem_prime_vunmap
,
899 .gem_free_object
= nouveau_gem_object_del
,
900 .gem_open_object
= nouveau_gem_object_open
,
901 .gem_close_object
= nouveau_gem_object_close
,
903 .dumb_create
= nouveau_display_dumb_create
,
904 .dumb_map_offset
= nouveau_display_dumb_map_offset
,
905 .dumb_destroy
= drm_gem_dumb_destroy
,
910 .date
= GIT_REVISION
,
914 .major
= DRIVER_MAJOR
,
915 .minor
= DRIVER_MINOR
,
916 .patchlevel
= DRIVER_PATCHLEVEL
,
919 static struct pci_device_id
920 nouveau_drm_pci_table
[] = {
922 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA
, PCI_ANY_ID
),
923 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
924 .class_mask
= 0xff << 16,
927 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS
, PCI_ANY_ID
),
928 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
929 .class_mask
= 0xff << 16,
934 static int nouveau_pmops_runtime_suspend(struct device
*dev
)
936 struct pci_dev
*pdev
= to_pci_dev(dev
);
937 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
940 if (nouveau_runtime_pm
== 0) {
941 pm_runtime_forbid(dev
);
945 /* are we optimus enabled? */
946 if (nouveau_runtime_pm
== -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
947 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
948 pm_runtime_forbid(dev
);
952 nv_debug_level(SILENT
);
953 drm_kms_helper_poll_disable(drm_dev
);
954 vga_switcheroo_set_dynamic_switch(pdev
, VGA_SWITCHEROO_OFF
);
955 nouveau_switcheroo_optimus_dsm();
956 ret
= nouveau_do_suspend(drm_dev
, true);
957 pci_save_state(pdev
);
958 pci_disable_device(pdev
);
959 pci_set_power_state(pdev
, PCI_D3cold
);
960 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_DYNAMIC_OFF
;
964 static int nouveau_pmops_runtime_resume(struct device
*dev
)
966 struct pci_dev
*pdev
= to_pci_dev(dev
);
967 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
968 struct nvif_device
*device
= &nouveau_drm(drm_dev
)->device
;
971 if (nouveau_runtime_pm
== 0)
974 pci_set_power_state(pdev
, PCI_D0
);
975 pci_restore_state(pdev
);
976 ret
= pci_enable_device(pdev
);
979 pci_set_master(pdev
);
981 ret
= nouveau_do_resume(drm_dev
);
982 drm_kms_helper_poll_enable(drm_dev
);
984 nvif_mask(device
, 0x88488, (1 << 25), (1 << 25));
985 vga_switcheroo_set_dynamic_switch(pdev
, VGA_SWITCHEROO_ON
);
986 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
987 nv_debug_level(NORMAL
);
991 static int nouveau_pmops_runtime_idle(struct device
*dev
)
993 struct pci_dev
*pdev
= to_pci_dev(dev
);
994 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
995 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
996 struct drm_crtc
*crtc
;
998 if (nouveau_runtime_pm
== 0) {
999 pm_runtime_forbid(dev
);
1003 /* are we optimus enabled? */
1004 if (nouveau_runtime_pm
== -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
1005 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
1006 pm_runtime_forbid(dev
);
1010 /* if we have a hdmi audio device - make sure it has a driver loaded */
1011 if (drm
->hdmi_device
) {
1012 if (!drm
->hdmi_device
->driver
) {
1013 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
1014 pm_runtime_mark_last_busy(dev
);
1019 list_for_each_entry(crtc
, &drm
->dev
->mode_config
.crtc_list
, head
) {
1020 if (crtc
->enabled
) {
1021 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
1025 pm_runtime_mark_last_busy(dev
);
1026 pm_runtime_autosuspend(dev
);
1027 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1031 static const struct dev_pm_ops nouveau_pm_ops
= {
1032 .suspend
= nouveau_pmops_suspend
,
1033 .resume
= nouveau_pmops_resume
,
1034 .freeze
= nouveau_pmops_freeze
,
1035 .thaw
= nouveau_pmops_thaw
,
1036 .poweroff
= nouveau_pmops_freeze
,
1037 .restore
= nouveau_pmops_resume
,
1038 .runtime_suspend
= nouveau_pmops_runtime_suspend
,
1039 .runtime_resume
= nouveau_pmops_runtime_resume
,
1040 .runtime_idle
= nouveau_pmops_runtime_idle
,
1043 static struct pci_driver
1044 nouveau_drm_pci_driver
= {
1046 .id_table
= nouveau_drm_pci_table
,
1047 .probe
= nouveau_drm_probe
,
1048 .remove
= nouveau_drm_remove
,
1049 .driver
.pm
= &nouveau_pm_ops
,
1053 nouveau_platform_device_create_(struct platform_device
*pdev
, int size
,
1056 struct drm_device
*drm
;
1059 err
= nouveau_device_create_(pdev
, NOUVEAU_BUS_PLATFORM
,
1060 nouveau_platform_name(pdev
),
1061 dev_name(&pdev
->dev
), nouveau_config
,
1062 nouveau_debug
, size
, pobject
);
1064 return ERR_PTR(err
);
1066 drm
= drm_dev_alloc(&driver_platform
, &pdev
->dev
);
1072 err
= drm_dev_set_unique(drm
, "%s", dev_name(&pdev
->dev
));
1076 drm
->platformdev
= pdev
;
1077 platform_set_drvdata(pdev
, drm
);
1082 nouveau_object_ref(NULL
, (struct nouveau_object
**)pobject
);
1084 return ERR_PTR(err
);
1086 EXPORT_SYMBOL(nouveau_platform_device_create_
);
1089 nouveau_drm_init(void)
1091 driver_pci
= driver_stub
;
1092 driver_pci
.set_busid
= drm_pci_set_busid
;
1093 driver_platform
= driver_stub
;
1094 driver_platform
.set_busid
= drm_platform_set_busid
;
1096 if (nouveau_modeset
== -1) {
1097 #ifdef CONFIG_VGA_CONSOLE
1098 if (vgacon_text_force())
1099 nouveau_modeset
= 0;
1103 if (!nouveau_modeset
)
1106 nouveau_register_dsm_handler();
1107 return drm_pci_init(&driver_pci
, &nouveau_drm_pci_driver
);
1111 nouveau_drm_exit(void)
1113 if (!nouveau_modeset
)
1116 drm_pci_exit(&driver_pci
, &nouveau_drm_pci_driver
);
1117 nouveau_unregister_dsm_handler();
1120 module_init(nouveau_drm_init
);
1121 module_exit(nouveau_drm_exit
);
1123 MODULE_DEVICE_TABLE(pci
, nouveau_drm_pci_table
);
1124 MODULE_AUTHOR(DRIVER_AUTHOR
);
1125 MODULE_DESCRIPTION(DRIVER_DESC
);
1126 MODULE_LICENSE("GPL and additional rights");