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/class.h>
37 #include <core/option.h>
39 #include "nouveau_drm.h"
40 #include "nouveau_dma.h"
41 #include "nouveau_ttm.h"
42 #include "nouveau_gem.h"
43 #include "nouveau_agp.h"
44 #include "nouveau_vga.h"
45 #include "nouveau_sysfs.h"
46 #include "nouveau_hwmon.h"
47 #include "nouveau_acpi.h"
48 #include "nouveau_bios.h"
49 #include "nouveau_ioctl.h"
50 #include "nouveau_abi16.h"
51 #include "nouveau_fbcon.h"
52 #include "nouveau_fence.h"
53 #include "nouveau_debugfs.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
;
79 nouveau_pci_name(struct pci_dev
*pdev
)
81 u64 name
= (u64
)pci_domain_nr(pdev
->bus
) << 32;
82 name
|= pdev
->bus
->number
<< 16;
83 name
|= PCI_SLOT(pdev
->devfn
) << 8;
84 return name
| PCI_FUNC(pdev
->devfn
);
88 nouveau_platform_name(struct platform_device
*platformdev
)
90 return platformdev
->id
;
94 nouveau_name(struct drm_device
*dev
)
97 return nouveau_pci_name(dev
->pdev
);
99 return nouveau_platform_name(dev
->platformdev
);
103 nouveau_cli_create(u64 name
, const char *sname
,
104 int size
, void **pcli
)
106 struct nouveau_cli
*cli
= *pcli
= kzalloc(size
, GFP_KERNEL
);
108 int ret
= nvif_client_init(NULL
, NULL
, sname
, name
,
109 nouveau_config
, nouveau_debug
,
112 mutex_init(&cli
->mutex
);
119 nouveau_cli_destroy(struct nouveau_cli
*cli
)
121 nouveau_vm_ref(NULL
, &nvkm_client(&cli
->base
)->vm
, NULL
);
122 nvif_client_fini(&cli
->base
);
126 nouveau_accel_fini(struct nouveau_drm
*drm
)
128 nouveau_channel_del(&drm
->channel
);
129 nvif_object_fini(&drm
->ntfy
);
130 nouveau_gpuobj_ref(NULL
, &drm
->notify
);
131 nvif_object_fini(&drm
->nvsw
);
132 nouveau_channel_del(&drm
->cechan
);
133 nvif_object_fini(&drm
->ttm
.copy
);
135 nouveau_fence(drm
)->dtor(drm
);
139 nouveau_accel_init(struct nouveau_drm
*drm
)
141 struct nvif_device
*device
= &drm
->device
;
149 /* initialise synchronisation routines */
150 /*XXX: this is crap, but the fence/channel stuff is a little
151 * backwards in some places. this will be fixed.
153 ret
= nvif_object_sclass(&device
->base
, sclass
, ARRAY_SIZE(sclass
));
157 for (ret
= -ENOSYS
, i
= 0; ret
&& i
< ARRAY_SIZE(sclass
); i
++) {
159 case NV03_CHANNEL_DMA
:
160 ret
= nv04_fence_create(drm
);
162 case NV10_CHANNEL_DMA
:
163 ret
= nv10_fence_create(drm
);
165 case NV17_CHANNEL_DMA
:
166 case NV40_CHANNEL_DMA
:
167 ret
= nv17_fence_create(drm
);
169 case NV50_CHANNEL_GPFIFO
:
170 ret
= nv50_fence_create(drm
);
172 case G82_CHANNEL_GPFIFO
:
173 ret
= nv84_fence_create(drm
);
175 case FERMI_CHANNEL_GPFIFO
:
176 case KEPLER_CHANNEL_GPFIFO_A
:
177 ret
= nvc0_fence_create(drm
);
185 NV_ERROR(drm
, "failed to initialise sync subsystem, %d\n", ret
);
186 nouveau_accel_fini(drm
);
190 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
191 ret
= nouveau_channel_new(drm
, &drm
->device
, NVDRM_CHAN
+ 1,
192 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0
|
193 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1
,
196 NV_ERROR(drm
, "failed to create ce channel, %d\n", ret
);
198 arg0
= KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR
;
201 if (device
->info
.chipset
>= 0xa3 &&
202 device
->info
.chipset
!= 0xaa &&
203 device
->info
.chipset
!= 0xac) {
204 ret
= nouveau_channel_new(drm
, &drm
->device
, NVDRM_CHAN
+ 1,
205 NvDmaFB
, NvDmaTT
, &drm
->cechan
);
207 NV_ERROR(drm
, "failed to create ce channel, %d\n", ret
);
216 ret
= nouveau_channel_new(drm
, &drm
->device
, NVDRM_CHAN
, arg0
, arg1
,
219 NV_ERROR(drm
, "failed to create kernel channel, %d\n", ret
);
220 nouveau_accel_fini(drm
);
224 ret
= nvif_object_init(drm
->channel
->object
, NULL
, NVDRM_NVSW
,
225 nouveau_abi16_swclass(drm
), NULL
, 0, &drm
->nvsw
);
227 struct nouveau_software_chan
*swch
;
228 ret
= RING_SPACE(drm
->channel
, 2);
230 if (device
->info
.family
< NV_DEVICE_INFO_V0_FERMI
) {
231 BEGIN_NV04(drm
->channel
, NvSubSw
, 0, 1);
232 OUT_RING (drm
->channel
, NVDRM_NVSW
);
234 if (device
->info
.family
< NV_DEVICE_INFO_V0_KEPLER
) {
235 BEGIN_NVC0(drm
->channel
, FermiSw
, 0, 1);
236 OUT_RING (drm
->channel
, 0x001f0000);
239 swch
= (void *)nvkm_object(&drm
->nvsw
)->parent
;
240 swch
->flip
= nouveau_flip_complete
;
241 swch
->flip_data
= drm
->channel
;
245 NV_ERROR(drm
, "failed to allocate software object, %d\n", ret
);
246 nouveau_accel_fini(drm
);
250 if (device
->info
.family
< NV_DEVICE_INFO_V0_FERMI
) {
251 ret
= nouveau_gpuobj_new(nvkm_object(&drm
->device
), NULL
, 32,
254 NV_ERROR(drm
, "failed to allocate notifier, %d\n", ret
);
255 nouveau_accel_fini(drm
);
259 ret
= nvif_object_init(drm
->channel
->object
, NULL
, NvNotify0
,
261 &(struct nv_dma_v0
) {
262 .target
= NV_DMA_V0_TARGET_VRAM
,
263 .access
= NV_DMA_V0_ACCESS_RDWR
,
264 .start
= drm
->notify
->addr
,
265 .limit
= drm
->notify
->addr
+ 31
266 }, sizeof(struct nv_dma_v0
),
269 nouveau_accel_fini(drm
);
275 nouveau_bo_move_init(drm
);
278 static int nouveau_drm_probe(struct pci_dev
*pdev
,
279 const struct pci_device_id
*pent
)
281 struct nouveau_device
*device
;
282 struct apertures_struct
*aper
;
286 /* remove conflicting drivers (vesafb, efifb etc) */
287 aper
= alloc_apertures(3);
291 aper
->ranges
[0].base
= pci_resource_start(pdev
, 1);
292 aper
->ranges
[0].size
= pci_resource_len(pdev
, 1);
295 if (pci_resource_len(pdev
, 2)) {
296 aper
->ranges
[aper
->count
].base
= pci_resource_start(pdev
, 2);
297 aper
->ranges
[aper
->count
].size
= pci_resource_len(pdev
, 2);
301 if (pci_resource_len(pdev
, 3)) {
302 aper
->ranges
[aper
->count
].base
= pci_resource_start(pdev
, 3);
303 aper
->ranges
[aper
->count
].size
= pci_resource_len(pdev
, 3);
308 boot
= pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
;
310 remove_conflicting_framebuffers(aper
, "nouveaufb", boot
);
313 ret
= nouveau_device_create(pdev
, NOUVEAU_BUS_PCI
,
314 nouveau_pci_name(pdev
), pci_name(pdev
),
315 nouveau_config
, nouveau_debug
, &device
);
319 pci_set_master(pdev
);
321 ret
= drm_get_pci_dev(pdev
, pent
, &driver
);
323 nouveau_object_ref(NULL
, (struct nouveau_object
**)&device
);
330 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
333 nouveau_get_hdmi_dev(struct nouveau_drm
*drm
)
335 struct pci_dev
*pdev
= drm
->dev
->pdev
;
338 DRM_INFO("not a PCI device; no HDMI\n");
339 drm
->hdmi_device
= NULL
;
343 /* subfunction one is a hdmi audio device? */
344 drm
->hdmi_device
= pci_get_bus_and_slot((unsigned int)pdev
->bus
->number
,
345 PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 1));
347 if (!drm
->hdmi_device
) {
348 NV_DEBUG(drm
, "hdmi device not found %d %d %d\n", pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), 1);
352 if ((drm
->hdmi_device
->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO
) {
353 NV_DEBUG(drm
, "possible hdmi device not audio %d\n", drm
->hdmi_device
->class);
354 pci_dev_put(drm
->hdmi_device
);
355 drm
->hdmi_device
= NULL
;
361 nouveau_drm_load(struct drm_device
*dev
, unsigned long flags
)
363 struct pci_dev
*pdev
= dev
->pdev
;
364 struct nouveau_drm
*drm
;
367 ret
= nouveau_cli_create(nouveau_name(dev
), "DRM", sizeof(*drm
),
372 dev
->dev_private
= drm
;
374 nvkm_client(&drm
->client
.base
)->debug
=
375 nouveau_dbgopt(nouveau_debug
, "DRM");
377 INIT_LIST_HEAD(&drm
->clients
);
378 spin_lock_init(&drm
->tile
.lock
);
380 nouveau_get_hdmi_dev(drm
);
382 /* make sure AGP controller is in a consistent state before we
383 * (possibly) execute vbios init tables (see nouveau_agp.h)
385 if (pdev
&& drm_pci_device_is_agp(dev
) && dev
->agp
) {
386 const u64 enables
= NV_DEVICE_V0_DISABLE_IDENTIFY
|
387 NV_DEVICE_V0_DISABLE_MMIO
;
388 /* dummy device object, doesn't init anything, but allows
389 * agp code access to registers
391 ret
= nvif_device_init(&drm
->client
.base
.base
, NULL
,
392 NVDRM_DEVICE
, NV_DEVICE
,
393 &(struct nv_device_v0
) {
397 }, sizeof(struct nv_device_v0
),
402 nouveau_agp_reset(drm
);
403 nvif_device_fini(&drm
->device
);
406 ret
= nvif_device_init(&drm
->client
.base
.base
, NULL
, NVDRM_DEVICE
,
408 &(struct nv_device_v0
) {
412 }, sizeof(struct nv_device_v0
),
417 dev
->irq_enabled
= true;
419 /* workaround an odd issue on nvc1 by disabling the device's
420 * nosnoop capability. hopefully won't cause issues until a
421 * better fix is found - assuming there is one...
423 if (drm
->device
.info
.chipset
== 0xc1)
424 nvif_mask(&drm
->device
, 0x00088080, 0x00000800, 0x00000000);
426 nouveau_vga_init(drm
);
427 nouveau_agp_init(drm
);
429 if (drm
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
430 ret
= nouveau_vm_new(nvkm_device(&drm
->device
), 0, (1ULL << 40),
431 0x1000, &drm
->client
.vm
);
435 nvkm_client(&drm
->client
.base
)->vm
= drm
->client
.vm
;
438 ret
= nouveau_ttm_init(drm
);
442 ret
= nouveau_bios_init(dev
);
446 ret
= nouveau_display_create(dev
);
450 if (dev
->mode_config
.num_crtc
) {
451 ret
= nouveau_display_init(dev
);
456 nouveau_sysfs_init(dev
);
457 nouveau_hwmon_init(dev
);
458 nouveau_accel_init(drm
);
459 nouveau_fbcon_init(dev
);
461 if (nouveau_runtime_pm
!= 0) {
462 pm_runtime_use_autosuspend(dev
->dev
);
463 pm_runtime_set_autosuspend_delay(dev
->dev
, 5000);
464 pm_runtime_set_active(dev
->dev
);
465 pm_runtime_allow(dev
->dev
);
466 pm_runtime_mark_last_busy(dev
->dev
);
467 pm_runtime_put(dev
->dev
);
472 nouveau_display_destroy(dev
);
474 nouveau_bios_takedown(dev
);
476 nouveau_ttm_fini(drm
);
478 nouveau_agp_fini(drm
);
479 nouveau_vga_fini(drm
);
481 nvif_device_fini(&drm
->device
);
482 nouveau_cli_destroy(&drm
->client
);
487 nouveau_drm_unload(struct drm_device
*dev
)
489 struct nouveau_drm
*drm
= nouveau_drm(dev
);
491 pm_runtime_get_sync(dev
->dev
);
492 nouveau_fbcon_fini(dev
);
493 nouveau_accel_fini(drm
);
494 nouveau_hwmon_fini(dev
);
495 nouveau_sysfs_fini(dev
);
497 if (dev
->mode_config
.num_crtc
)
498 nouveau_display_fini(dev
);
499 nouveau_display_destroy(dev
);
501 nouveau_bios_takedown(dev
);
503 nouveau_ttm_fini(drm
);
504 nouveau_agp_fini(drm
);
505 nouveau_vga_fini(drm
);
507 nvif_device_fini(&drm
->device
);
508 if (drm
->hdmi_device
)
509 pci_dev_put(drm
->hdmi_device
);
510 nouveau_cli_destroy(&drm
->client
);
515 nouveau_drm_device_remove(struct drm_device
*dev
)
517 struct nouveau_drm
*drm
= nouveau_drm(dev
);
518 struct nouveau_client
*client
;
519 struct nouveau_object
*device
;
521 dev
->irq_enabled
= false;
522 client
= nvkm_client(&drm
->client
.base
);
523 device
= client
->device
;
526 nouveau_object_ref(NULL
, &device
);
527 nouveau_object_debug();
529 EXPORT_SYMBOL(nouveau_drm_device_remove
);
532 nouveau_drm_remove(struct pci_dev
*pdev
)
534 struct drm_device
*dev
= pci_get_drvdata(pdev
);
536 nouveau_drm_device_remove(dev
);
540 nouveau_do_suspend(struct drm_device
*dev
, bool runtime
)
542 struct nouveau_drm
*drm
= nouveau_drm(dev
);
543 struct nouveau_cli
*cli
;
546 if (dev
->mode_config
.num_crtc
&& !runtime
) {
547 NV_INFO(drm
, "suspending display...\n");
548 ret
= nouveau_display_suspend(dev
);
553 NV_INFO(drm
, "evicting buffers...\n");
554 ttm_bo_evict_mm(&drm
->ttm
.bdev
, TTM_PL_VRAM
);
556 NV_INFO(drm
, "waiting for kernel channels to go idle...\n");
558 ret
= nouveau_channel_idle(drm
->cechan
);
564 ret
= nouveau_channel_idle(drm
->channel
);
569 NV_INFO(drm
, "suspending client object trees...\n");
570 if (drm
->fence
&& nouveau_fence(drm
)->suspend
) {
571 if (!nouveau_fence(drm
)->suspend(drm
)) {
577 list_for_each_entry(cli
, &drm
->clients
, head
) {
578 ret
= nvif_client_suspend(&cli
->base
);
583 NV_INFO(drm
, "suspending kernel object tree...\n");
584 ret
= nvif_client_suspend(&drm
->client
.base
);
588 nouveau_agp_fini(drm
);
592 list_for_each_entry_continue_reverse(cli
, &drm
->clients
, head
) {
593 nvif_client_resume(&cli
->base
);
596 if (drm
->fence
&& nouveau_fence(drm
)->resume
)
597 nouveau_fence(drm
)->resume(drm
);
600 if (dev
->mode_config
.num_crtc
) {
601 NV_INFO(drm
, "resuming display...\n");
602 nouveau_display_resume(dev
);
607 int nouveau_pmops_suspend(struct device
*dev
)
609 struct pci_dev
*pdev
= to_pci_dev(dev
);
610 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
613 if (drm_dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
||
614 drm_dev
->switch_power_state
== DRM_SWITCH_POWER_DYNAMIC_OFF
)
617 if (drm_dev
->mode_config
.num_crtc
)
618 nouveau_fbcon_set_suspend(drm_dev
, 1);
620 ret
= nouveau_do_suspend(drm_dev
, false);
624 pci_save_state(pdev
);
625 pci_disable_device(pdev
);
626 pci_set_power_state(pdev
, PCI_D3hot
);
631 nouveau_do_resume(struct drm_device
*dev
)
633 struct nouveau_drm
*drm
= nouveau_drm(dev
);
634 struct nouveau_cli
*cli
;
636 NV_INFO(drm
, "re-enabling device...\n");
638 nouveau_agp_reset(drm
);
640 NV_INFO(drm
, "resuming kernel object tree...\n");
641 nvif_client_resume(&drm
->client
.base
);
642 nouveau_agp_init(drm
);
644 NV_INFO(drm
, "resuming client object trees...\n");
645 if (drm
->fence
&& nouveau_fence(drm
)->resume
)
646 nouveau_fence(drm
)->resume(drm
);
648 list_for_each_entry(cli
, &drm
->clients
, head
) {
649 nvif_client_resume(&cli
->base
);
652 nouveau_run_vbios_init(dev
);
654 if (dev
->mode_config
.num_crtc
) {
655 NV_INFO(drm
, "resuming display...\n");
656 nouveau_display_repin(dev
);
662 int nouveau_pmops_resume(struct device
*dev
)
664 struct pci_dev
*pdev
= to_pci_dev(dev
);
665 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
668 if (drm_dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
||
669 drm_dev
->switch_power_state
== DRM_SWITCH_POWER_DYNAMIC_OFF
)
672 pci_set_power_state(pdev
, PCI_D0
);
673 pci_restore_state(pdev
);
674 ret
= pci_enable_device(pdev
);
677 pci_set_master(pdev
);
679 ret
= nouveau_do_resume(drm_dev
);
683 if (drm_dev
->mode_config
.num_crtc
) {
684 nouveau_display_resume(drm_dev
);
685 nouveau_fbcon_set_suspend(drm_dev
, 0);
691 static int nouveau_pmops_freeze(struct device
*dev
)
693 struct pci_dev
*pdev
= to_pci_dev(dev
);
694 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
697 if (drm_dev
->mode_config
.num_crtc
)
698 nouveau_fbcon_set_suspend(drm_dev
, 1);
700 ret
= nouveau_do_suspend(drm_dev
, false);
704 static int nouveau_pmops_thaw(struct device
*dev
)
706 struct pci_dev
*pdev
= to_pci_dev(dev
);
707 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
710 ret
= nouveau_do_resume(drm_dev
);
714 if (drm_dev
->mode_config
.num_crtc
) {
715 nouveau_display_resume(drm_dev
);
716 nouveau_fbcon_set_suspend(drm_dev
, 0);
724 nouveau_drm_open(struct drm_device
*dev
, struct drm_file
*fpriv
)
726 struct nouveau_drm
*drm
= nouveau_drm(dev
);
727 struct nouveau_cli
*cli
;
728 char name
[32], tmpname
[TASK_COMM_LEN
];
731 /* need to bring up power immediately if opening device */
732 ret
= pm_runtime_get_sync(dev
->dev
);
733 if (ret
< 0 && ret
!= -EACCES
)
736 get_task_comm(tmpname
, current
);
737 snprintf(name
, sizeof(name
), "%s[%d]", tmpname
, pid_nr(fpriv
->pid
));
739 ret
= nouveau_cli_create(nouveau_name(dev
), name
, sizeof(*cli
),
745 cli
->base
.super
= false;
747 if (drm
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
748 ret
= nouveau_vm_new(nvkm_device(&drm
->device
), 0, (1ULL << 40),
751 nouveau_cli_destroy(cli
);
755 nvkm_client(&cli
->base
)->vm
= cli
->vm
;
758 fpriv
->driver_priv
= cli
;
760 mutex_lock(&drm
->client
.mutex
);
761 list_add(&cli
->head
, &drm
->clients
);
762 mutex_unlock(&drm
->client
.mutex
);
765 pm_runtime_mark_last_busy(dev
->dev
);
766 pm_runtime_put_autosuspend(dev
->dev
);
772 nouveau_drm_preclose(struct drm_device
*dev
, struct drm_file
*fpriv
)
774 struct nouveau_cli
*cli
= nouveau_cli(fpriv
);
775 struct nouveau_drm
*drm
= nouveau_drm(dev
);
777 pm_runtime_get_sync(dev
->dev
);
780 nouveau_abi16_fini(cli
->abi16
);
782 mutex_lock(&drm
->client
.mutex
);
783 list_del(&cli
->head
);
784 mutex_unlock(&drm
->client
.mutex
);
789 nouveau_drm_postclose(struct drm_device
*dev
, struct drm_file
*fpriv
)
791 struct nouveau_cli
*cli
= nouveau_cli(fpriv
);
792 nouveau_cli_destroy(cli
);
793 pm_runtime_mark_last_busy(dev
->dev
);
794 pm_runtime_put_autosuspend(dev
->dev
);
797 static const struct drm_ioctl_desc
799 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM
, nouveau_abi16_ioctl_getparam
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
800 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM
, nouveau_abi16_ioctl_setparam
, DRM_UNLOCKED
|DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
801 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC
, nouveau_abi16_ioctl_channel_alloc
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
802 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE
, nouveau_abi16_ioctl_channel_free
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
803 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC
, nouveau_abi16_ioctl_grobj_alloc
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
804 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC
, nouveau_abi16_ioctl_notifierobj_alloc
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
805 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE
, nouveau_abi16_ioctl_gpuobj_free
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
806 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW
, nouveau_gem_ioctl_new
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
807 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF
, nouveau_gem_ioctl_pushbuf
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
808 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP
, nouveau_gem_ioctl_cpu_prep
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
809 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI
, nouveau_gem_ioctl_cpu_fini
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
810 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO
, nouveau_gem_ioctl_info
, DRM_UNLOCKED
|DRM_AUTH
|DRM_RENDER_ALLOW
),
813 long nouveau_drm_ioctl(struct file
*filp
,
814 unsigned int cmd
, unsigned long arg
)
816 struct drm_file
*file_priv
= filp
->private_data
;
817 struct drm_device
*dev
;
819 dev
= file_priv
->minor
->dev
;
821 ret
= pm_runtime_get_sync(dev
->dev
);
822 if (ret
< 0 && ret
!= -EACCES
)
825 ret
= drm_ioctl(filp
, cmd
, arg
);
827 pm_runtime_mark_last_busy(dev
->dev
);
828 pm_runtime_put_autosuspend(dev
->dev
);
831 static const struct file_operations
832 nouveau_driver_fops
= {
833 .owner
= THIS_MODULE
,
835 .release
= drm_release
,
836 .unlocked_ioctl
= nouveau_drm_ioctl
,
837 .mmap
= nouveau_ttm_mmap
,
840 #if defined(CONFIG_COMPAT)
841 .compat_ioctl
= nouveau_compat_ioctl
,
843 .llseek
= noop_llseek
,
846 static struct drm_driver
850 DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
| DRIVER_RENDER
,
852 .load
= nouveau_drm_load
,
853 .unload
= nouveau_drm_unload
,
854 .open
= nouveau_drm_open
,
855 .preclose
= nouveau_drm_preclose
,
856 .postclose
= nouveau_drm_postclose
,
857 .lastclose
= nouveau_vga_lastclose
,
859 #if defined(CONFIG_DEBUG_FS)
860 .debugfs_init
= nouveau_debugfs_init
,
861 .debugfs_cleanup
= nouveau_debugfs_takedown
,
864 .get_vblank_counter
= drm_vblank_count
,
865 .enable_vblank
= nouveau_display_vblank_enable
,
866 .disable_vblank
= nouveau_display_vblank_disable
,
867 .get_scanout_position
= nouveau_display_scanoutpos
,
868 .get_vblank_timestamp
= nouveau_display_vblstamp
,
870 .ioctls
= nouveau_ioctls
,
871 .num_ioctls
= ARRAY_SIZE(nouveau_ioctls
),
872 .fops
= &nouveau_driver_fops
,
874 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
875 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
876 .gem_prime_export
= drm_gem_prime_export
,
877 .gem_prime_import
= drm_gem_prime_import
,
878 .gem_prime_pin
= nouveau_gem_prime_pin
,
879 .gem_prime_unpin
= nouveau_gem_prime_unpin
,
880 .gem_prime_get_sg_table
= nouveau_gem_prime_get_sg_table
,
881 .gem_prime_import_sg_table
= nouveau_gem_prime_import_sg_table
,
882 .gem_prime_vmap
= nouveau_gem_prime_vmap
,
883 .gem_prime_vunmap
= nouveau_gem_prime_vunmap
,
885 .gem_free_object
= nouveau_gem_object_del
,
886 .gem_open_object
= nouveau_gem_object_open
,
887 .gem_close_object
= nouveau_gem_object_close
,
889 .dumb_create
= nouveau_display_dumb_create
,
890 .dumb_map_offset
= nouveau_display_dumb_map_offset
,
891 .dumb_destroy
= drm_gem_dumb_destroy
,
896 .date
= GIT_REVISION
,
900 .major
= DRIVER_MAJOR
,
901 .minor
= DRIVER_MINOR
,
902 .patchlevel
= DRIVER_PATCHLEVEL
,
905 static struct pci_device_id
906 nouveau_drm_pci_table
[] = {
908 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA
, PCI_ANY_ID
),
909 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
910 .class_mask
= 0xff << 16,
913 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS
, PCI_ANY_ID
),
914 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
915 .class_mask
= 0xff << 16,
920 static int nouveau_pmops_runtime_suspend(struct device
*dev
)
922 struct pci_dev
*pdev
= to_pci_dev(dev
);
923 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
926 if (nouveau_runtime_pm
== 0) {
927 pm_runtime_forbid(dev
);
931 /* are we optimus enabled? */
932 if (nouveau_runtime_pm
== -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
933 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
934 pm_runtime_forbid(dev
);
938 nv_debug_level(SILENT
);
939 drm_kms_helper_poll_disable(drm_dev
);
940 vga_switcheroo_set_dynamic_switch(pdev
, VGA_SWITCHEROO_OFF
);
941 nouveau_switcheroo_optimus_dsm();
942 ret
= nouveau_do_suspend(drm_dev
, true);
943 pci_save_state(pdev
);
944 pci_disable_device(pdev
);
945 pci_set_power_state(pdev
, PCI_D3cold
);
946 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_DYNAMIC_OFF
;
950 static int nouveau_pmops_runtime_resume(struct device
*dev
)
952 struct pci_dev
*pdev
= to_pci_dev(dev
);
953 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
954 struct nvif_device
*device
= &nouveau_drm(drm_dev
)->device
;
957 if (nouveau_runtime_pm
== 0)
960 pci_set_power_state(pdev
, PCI_D0
);
961 pci_restore_state(pdev
);
962 ret
= pci_enable_device(pdev
);
965 pci_set_master(pdev
);
967 ret
= nouveau_do_resume(drm_dev
);
968 drm_kms_helper_poll_enable(drm_dev
);
970 nvif_mask(device
, 0x88488, (1 << 25), (1 << 25));
971 vga_switcheroo_set_dynamic_switch(pdev
, VGA_SWITCHEROO_ON
);
972 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
973 nv_debug_level(NORMAL
);
977 static int nouveau_pmops_runtime_idle(struct device
*dev
)
979 struct pci_dev
*pdev
= to_pci_dev(dev
);
980 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
981 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
982 struct drm_crtc
*crtc
;
984 if (nouveau_runtime_pm
== 0) {
985 pm_runtime_forbid(dev
);
989 /* are we optimus enabled? */
990 if (nouveau_runtime_pm
== -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
991 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
992 pm_runtime_forbid(dev
);
996 /* if we have a hdmi audio device - make sure it has a driver loaded */
997 if (drm
->hdmi_device
) {
998 if (!drm
->hdmi_device
->driver
) {
999 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
1000 pm_runtime_mark_last_busy(dev
);
1005 list_for_each_entry(crtc
, &drm
->dev
->mode_config
.crtc_list
, head
) {
1006 if (crtc
->enabled
) {
1007 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
1011 pm_runtime_mark_last_busy(dev
);
1012 pm_runtime_autosuspend(dev
);
1013 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1017 static const struct dev_pm_ops nouveau_pm_ops
= {
1018 .suspend
= nouveau_pmops_suspend
,
1019 .resume
= nouveau_pmops_resume
,
1020 .freeze
= nouveau_pmops_freeze
,
1021 .thaw
= nouveau_pmops_thaw
,
1022 .poweroff
= nouveau_pmops_freeze
,
1023 .restore
= nouveau_pmops_resume
,
1024 .runtime_suspend
= nouveau_pmops_runtime_suspend
,
1025 .runtime_resume
= nouveau_pmops_runtime_resume
,
1026 .runtime_idle
= nouveau_pmops_runtime_idle
,
1029 static struct pci_driver
1030 nouveau_drm_pci_driver
= {
1032 .id_table
= nouveau_drm_pci_table
,
1033 .probe
= nouveau_drm_probe
,
1034 .remove
= nouveau_drm_remove
,
1035 .driver
.pm
= &nouveau_pm_ops
,
1039 nouveau_platform_device_create_(struct platform_device
*pdev
, int size
,
1042 struct drm_device
*drm
;
1045 err
= nouveau_device_create_(pdev
, NOUVEAU_BUS_PLATFORM
,
1046 nouveau_platform_name(pdev
),
1047 dev_name(&pdev
->dev
), nouveau_config
,
1048 nouveau_debug
, size
, pobject
);
1050 return ERR_PTR(err
);
1052 drm
= drm_dev_alloc(&driver
, &pdev
->dev
);
1058 err
= drm_dev_set_unique(drm
, "%s", dev_name(&pdev
->dev
));
1062 drm
->platformdev
= pdev
;
1063 platform_set_drvdata(pdev
, drm
);
1068 nouveau_object_ref(NULL
, (struct nouveau_object
**)pobject
);
1070 return ERR_PTR(err
);
1072 EXPORT_SYMBOL(nouveau_platform_device_create_
);
1075 nouveau_drm_init(void)
1077 if (nouveau_modeset
== -1) {
1078 #ifdef CONFIG_VGA_CONSOLE
1079 if (vgacon_text_force())
1080 nouveau_modeset
= 0;
1084 if (!nouveau_modeset
)
1087 nouveau_register_dsm_handler();
1088 return drm_pci_init(&driver
, &nouveau_drm_pci_driver
);
1092 nouveau_drm_exit(void)
1094 if (!nouveau_modeset
)
1097 drm_pci_exit(&driver
, &nouveau_drm_pci_driver
);
1098 nouveau_unregister_dsm_handler();
1101 module_init(nouveau_drm_init
);
1102 module_exit(nouveau_drm_exit
);
1104 MODULE_DEVICE_TABLE(pci
, nouveau_drm_pci_table
);
1105 MODULE_AUTHOR(DRIVER_AUTHOR
);
1106 MODULE_DESCRIPTION(DRIVER_DESC
);
1107 MODULE_LICENSE("GPL and additional rights");