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/delay.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vga_switcheroo.h>
33 #include <drm/drm_crtc_helper.h>
35 #include <core/gpuobj.h>
36 #include <core/option.h>
38 #include <core/tegra.h>
40 #include <nvif/driver.h>
42 #include <nvif/class.h>
43 #include <nvif/cl0002.h>
44 #include <nvif/cla06f.h>
45 #include <nvif/if0004.h>
47 #include "nouveau_drv.h"
48 #include "nouveau_dma.h"
49 #include "nouveau_ttm.h"
50 #include "nouveau_gem.h"
51 #include "nouveau_vga.h"
52 #include "nouveau_led.h"
53 #include "nouveau_hwmon.h"
54 #include "nouveau_acpi.h"
55 #include "nouveau_bios.h"
56 #include "nouveau_ioctl.h"
57 #include "nouveau_abi16.h"
58 #include "nouveau_fbcon.h"
59 #include "nouveau_fence.h"
60 #include "nouveau_debugfs.h"
61 #include "nouveau_usif.h"
62 #include "nouveau_connector.h"
63 #include "nouveau_platform.h"
65 MODULE_PARM_DESC(config
, "option string to pass to driver core");
66 static char *nouveau_config
;
67 module_param_named(config
, nouveau_config
, charp
, 0400);
69 MODULE_PARM_DESC(debug
, "debug string to pass to driver core");
70 static char *nouveau_debug
;
71 module_param_named(debug
, nouveau_debug
, charp
, 0400);
73 MODULE_PARM_DESC(noaccel
, "disable kernel/abi16 acceleration");
74 static int nouveau_noaccel
= 0;
75 module_param_named(noaccel
, nouveau_noaccel
, int, 0400);
77 MODULE_PARM_DESC(modeset
, "enable driver (default: auto, "
78 "0 = disabled, 1 = enabled, 2 = headless)");
79 int nouveau_modeset
= -1;
80 module_param_named(modeset
, nouveau_modeset
, int, 0400);
82 MODULE_PARM_DESC(runpm
, "disable (0), force enable (1), optimus only default (-1)");
83 static int nouveau_runtime_pm
= -1;
84 module_param_named(runpm
, nouveau_runtime_pm
, int, 0400);
86 static struct drm_driver driver_stub
;
87 static struct drm_driver driver_pci
;
88 static struct drm_driver driver_platform
;
91 nouveau_pci_name(struct pci_dev
*pdev
)
93 u64 name
= (u64
)pci_domain_nr(pdev
->bus
) << 32;
94 name
|= pdev
->bus
->number
<< 16;
95 name
|= PCI_SLOT(pdev
->devfn
) << 8;
96 return name
| PCI_FUNC(pdev
->devfn
);
100 nouveau_platform_name(struct platform_device
*platformdev
)
102 return platformdev
->id
;
106 nouveau_name(struct drm_device
*dev
)
109 return nouveau_pci_name(dev
->pdev
);
111 return nouveau_platform_name(to_platform_device(dev
->dev
));
115 nouveau_cli_fini(struct nouveau_cli
*cli
)
117 usif_client_fini(cli
);
118 nouveau_vmm_fini(&cli
->vmm
);
119 nvif_device_fini(&cli
->device
);
120 mutex_lock(&cli
->drm
->master
.lock
);
121 nvif_client_fini(&cli
->base
);
122 mutex_unlock(&cli
->drm
->master
.lock
);
126 nouveau_cli_init(struct nouveau_drm
*drm
, const char *sname
,
127 struct nouveau_cli
*cli
)
129 u64 device
= nouveau_name(drm
->dev
);
132 snprintf(cli
->name
, sizeof(cli
->name
), "%s", sname
);
134 mutex_init(&cli
->mutex
);
135 usif_client_init(cli
);
137 mutex_init(&cli
->lock
);
139 if (cli
== &drm
->master
) {
140 ret
= nvif_driver_init(NULL
, nouveau_config
, nouveau_debug
,
141 cli
->name
, device
, &cli
->base
);
143 mutex_lock(&drm
->master
.lock
);
144 ret
= nvif_client_init(&drm
->master
.base
, cli
->name
, device
,
146 mutex_unlock(&drm
->master
.lock
);
149 NV_ERROR(drm
, "Client allocation failed: %d\n", ret
);
153 ret
= nvif_device_init(&cli
->base
.object
, 0, NV_DEVICE
,
154 &(struct nv_device_v0
) {
156 }, sizeof(struct nv_device_v0
),
159 NV_ERROR(drm
, "Device allocation failed: %d\n", ret
);
165 nouveau_cli_fini(cli
);
170 nouveau_accel_fini(struct nouveau_drm
*drm
)
172 nouveau_channel_idle(drm
->channel
);
173 nvif_object_fini(&drm
->ntfy
);
174 nvkm_gpuobj_del(&drm
->notify
);
175 nvif_notify_fini(&drm
->flip
);
176 nvif_object_fini(&drm
->nvsw
);
177 nouveau_channel_del(&drm
->channel
);
179 nouveau_channel_idle(drm
->cechan
);
180 nvif_object_fini(&drm
->ttm
.copy
);
181 nouveau_channel_del(&drm
->cechan
);
184 nouveau_fence(drm
)->dtor(drm
);
188 nouveau_accel_init(struct nouveau_drm
*drm
)
190 struct nvif_device
*device
= &drm
->client
.device
;
191 struct nvif_sclass
*sclass
;
198 /* initialise synchronisation routines */
199 /*XXX: this is crap, but the fence/channel stuff is a little
200 * backwards in some places. this will be fixed.
202 ret
= n
= nvif_object_sclass_get(&device
->object
, &sclass
);
206 for (ret
= -ENOSYS
, i
= 0; i
< n
; i
++) {
207 switch (sclass
[i
].oclass
) {
208 case NV03_CHANNEL_DMA
:
209 ret
= nv04_fence_create(drm
);
211 case NV10_CHANNEL_DMA
:
212 ret
= nv10_fence_create(drm
);
214 case NV17_CHANNEL_DMA
:
215 case NV40_CHANNEL_DMA
:
216 ret
= nv17_fence_create(drm
);
218 case NV50_CHANNEL_GPFIFO
:
219 ret
= nv50_fence_create(drm
);
221 case G82_CHANNEL_GPFIFO
:
222 ret
= nv84_fence_create(drm
);
224 case FERMI_CHANNEL_GPFIFO
:
225 case KEPLER_CHANNEL_GPFIFO_A
:
226 case KEPLER_CHANNEL_GPFIFO_B
:
227 case MAXWELL_CHANNEL_GPFIFO_A
:
228 case PASCAL_CHANNEL_GPFIFO_A
:
229 ret
= nvc0_fence_create(drm
);
236 nvif_object_sclass_put(&sclass
);
238 NV_ERROR(drm
, "failed to initialise sync subsystem, %d\n", ret
);
239 nouveau_accel_fini(drm
);
243 if (device
->info
.family
>= NV_DEVICE_INFO_V0_KEPLER
) {
244 ret
= nouveau_channel_new(drm
, &drm
->client
.device
,
245 NVA06F_V0_ENGINE_CE0
|
246 NVA06F_V0_ENGINE_CE1
,
249 NV_ERROR(drm
, "failed to create ce channel, %d\n", ret
);
251 arg0
= NVA06F_V0_ENGINE_GR
;
254 if (device
->info
.chipset
>= 0xa3 &&
255 device
->info
.chipset
!= 0xaa &&
256 device
->info
.chipset
!= 0xac) {
257 ret
= nouveau_channel_new(drm
, &drm
->client
.device
,
258 NvDmaFB
, NvDmaTT
, &drm
->cechan
);
260 NV_ERROR(drm
, "failed to create ce channel, %d\n", ret
);
269 ret
= nouveau_channel_new(drm
, &drm
->client
.device
,
270 arg0
, arg1
, &drm
->channel
);
272 NV_ERROR(drm
, "failed to create kernel channel, %d\n", ret
);
273 nouveau_accel_fini(drm
);
277 ret
= nvif_object_init(&drm
->channel
->user
, NVDRM_NVSW
,
278 nouveau_abi16_swclass(drm
), NULL
, 0, &drm
->nvsw
);
280 ret
= RING_SPACE(drm
->channel
, 2);
282 if (device
->info
.family
< NV_DEVICE_INFO_V0_FERMI
) {
283 BEGIN_NV04(drm
->channel
, NvSubSw
, 0, 1);
284 OUT_RING (drm
->channel
, NVDRM_NVSW
);
286 if (device
->info
.family
< NV_DEVICE_INFO_V0_KEPLER
) {
287 BEGIN_NVC0(drm
->channel
, FermiSw
, 0, 1);
288 OUT_RING (drm
->channel
, 0x001f0000);
292 ret
= nvif_notify_init(&drm
->nvsw
, nouveau_flip_complete
,
293 false, NV04_NVSW_NTFY_UEVENT
,
294 NULL
, 0, 0, &drm
->flip
);
296 ret
= nvif_notify_get(&drm
->flip
);
298 nouveau_accel_fini(drm
);
304 NV_ERROR(drm
, "failed to allocate software object, %d\n", ret
);
305 nouveau_accel_fini(drm
);
309 if (device
->info
.family
< NV_DEVICE_INFO_V0_FERMI
) {
310 ret
= nvkm_gpuobj_new(nvxx_device(&drm
->client
.device
), 32, 0,
311 false, NULL
, &drm
->notify
);
313 NV_ERROR(drm
, "failed to allocate notifier, %d\n", ret
);
314 nouveau_accel_fini(drm
);
318 ret
= nvif_object_init(&drm
->channel
->user
, NvNotify0
,
320 &(struct nv_dma_v0
) {
321 .target
= NV_DMA_V0_TARGET_VRAM
,
322 .access
= NV_DMA_V0_ACCESS_RDWR
,
323 .start
= drm
->notify
->addr
,
324 .limit
= drm
->notify
->addr
+ 31
325 }, sizeof(struct nv_dma_v0
),
328 nouveau_accel_fini(drm
);
334 nouveau_bo_move_init(drm
);
337 static int nouveau_drm_probe(struct pci_dev
*pdev
,
338 const struct pci_device_id
*pent
)
340 struct nvkm_device
*device
;
341 struct apertures_struct
*aper
;
345 if (vga_switcheroo_client_probe_defer(pdev
))
346 return -EPROBE_DEFER
;
348 /* We need to check that the chipset is supported before booting
349 * fbdev off the hardware, as there's no way to put it back.
351 ret
= nvkm_device_pci_new(pdev
, NULL
, "error", true, false, 0, &device
);
355 nvkm_device_del(&device
);
357 /* Remove conflicting drivers (vesafb, efifb etc). */
358 aper
= alloc_apertures(3);
362 aper
->ranges
[0].base
= pci_resource_start(pdev
, 1);
363 aper
->ranges
[0].size
= pci_resource_len(pdev
, 1);
366 if (pci_resource_len(pdev
, 2)) {
367 aper
->ranges
[aper
->count
].base
= pci_resource_start(pdev
, 2);
368 aper
->ranges
[aper
->count
].size
= pci_resource_len(pdev
, 2);
372 if (pci_resource_len(pdev
, 3)) {
373 aper
->ranges
[aper
->count
].base
= pci_resource_start(pdev
, 3);
374 aper
->ranges
[aper
->count
].size
= pci_resource_len(pdev
, 3);
379 boot
= pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
;
381 if (nouveau_modeset
!= 2)
382 drm_fb_helper_remove_conflicting_framebuffers(aper
, "nouveaufb", boot
);
385 ret
= nvkm_device_pci_new(pdev
, nouveau_config
, nouveau_debug
,
386 true, true, ~0ULL, &device
);
390 pci_set_master(pdev
);
392 ret
= drm_get_pci_dev(pdev
, pent
, &driver_pci
);
394 nvkm_device_del(&device
);
401 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
404 nouveau_get_hdmi_dev(struct nouveau_drm
*drm
)
406 struct pci_dev
*pdev
= drm
->dev
->pdev
;
409 NV_DEBUG(drm
, "not a PCI device; no HDMI\n");
410 drm
->hdmi_device
= NULL
;
414 /* subfunction one is a hdmi audio device? */
415 drm
->hdmi_device
= pci_get_bus_and_slot((unsigned int)pdev
->bus
->number
,
416 PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 1));
418 if (!drm
->hdmi_device
) {
419 NV_DEBUG(drm
, "hdmi device not found %d %d %d\n", pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), 1);
423 if ((drm
->hdmi_device
->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO
) {
424 NV_DEBUG(drm
, "possible hdmi device not audio %d\n", drm
->hdmi_device
->class);
425 pci_dev_put(drm
->hdmi_device
);
426 drm
->hdmi_device
= NULL
;
432 nouveau_drm_load(struct drm_device
*dev
, unsigned long flags
)
434 struct nouveau_drm
*drm
;
437 if (!(drm
= kzalloc(sizeof(*drm
), GFP_KERNEL
)))
439 dev
->dev_private
= drm
;
442 ret
= nouveau_cli_init(drm
, "DRM-master", &drm
->master
);
446 ret
= nouveau_cli_init(drm
, "DRM", &drm
->client
);
450 dev
->irq_enabled
= true;
452 nvxx_client(&drm
->client
.base
)->debug
=
453 nvkm_dbgopt(nouveau_debug
, "DRM");
455 INIT_LIST_HEAD(&drm
->clients
);
456 spin_lock_init(&drm
->tile
.lock
);
458 nouveau_get_hdmi_dev(drm
);
460 /* workaround an odd issue on nvc1 by disabling the device's
461 * nosnoop capability. hopefully won't cause issues until a
462 * better fix is found - assuming there is one...
464 if (drm
->client
.device
.info
.chipset
== 0xc1)
465 nvif_mask(&drm
->client
.device
.object
, 0x00088080, 0x00000800, 0x00000000);
467 nouveau_vga_init(drm
);
469 if (drm
->client
.device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
470 if (!nvxx_device(&drm
->client
.device
)->mmu
) {
475 ret
= nouveau_vmm_init(&drm
->client
, 0, &drm
->client
.vmm
);
479 drm
->client
.vm
= drm
->client
.vmm
.vm
;
480 nvxx_client(&drm
->client
.base
)->vm
= drm
->client
.vm
;
483 ret
= nouveau_ttm_init(drm
);
487 ret
= nouveau_bios_init(dev
);
491 ret
= nouveau_display_create(dev
);
495 if (dev
->mode_config
.num_crtc
) {
496 ret
= nouveau_display_init(dev
);
501 nouveau_debugfs_init(drm
);
502 nouveau_hwmon_init(dev
);
503 nouveau_accel_init(drm
);
504 nouveau_fbcon_init(dev
);
505 nouveau_led_init(dev
);
507 if (nouveau_pmops_runtime()) {
508 pm_runtime_use_autosuspend(dev
->dev
);
509 pm_runtime_set_autosuspend_delay(dev
->dev
, 5000);
510 pm_runtime_set_active(dev
->dev
);
511 pm_runtime_allow(dev
->dev
);
512 pm_runtime_mark_last_busy(dev
->dev
);
513 pm_runtime_put(dev
->dev
);
515 /* enable polling for external displays */
516 drm_kms_helper_poll_enable(dev
);
521 nouveau_display_destroy(dev
);
523 nouveau_bios_takedown(dev
);
525 nouveau_ttm_fini(drm
);
527 nouveau_vga_fini(drm
);
529 nouveau_cli_fini(&drm
->client
);
530 nouveau_cli_fini(&drm
->master
);
536 nouveau_drm_unload(struct drm_device
*dev
)
538 struct nouveau_drm
*drm
= nouveau_drm(dev
);
540 if (nouveau_pmops_runtime()) {
541 pm_runtime_get_sync(dev
->dev
);
542 pm_runtime_forbid(dev
->dev
);
545 nouveau_led_fini(dev
);
546 nouveau_fbcon_fini(dev
);
547 nouveau_accel_fini(drm
);
548 nouveau_hwmon_fini(dev
);
549 nouveau_debugfs_fini(drm
);
551 if (dev
->mode_config
.num_crtc
)
552 nouveau_display_fini(dev
, false);
553 nouveau_display_destroy(dev
);
555 nouveau_bios_takedown(dev
);
557 nouveau_ttm_fini(drm
);
558 nouveau_vga_fini(drm
);
560 if (drm
->hdmi_device
)
561 pci_dev_put(drm
->hdmi_device
);
562 nouveau_cli_fini(&drm
->client
);
563 nouveau_cli_fini(&drm
->master
);
568 nouveau_drm_device_remove(struct drm_device
*dev
)
570 struct nouveau_drm
*drm
= nouveau_drm(dev
);
571 struct nvkm_client
*client
;
572 struct nvkm_device
*device
;
574 dev
->irq_enabled
= false;
575 client
= nvxx_client(&drm
->client
.base
);
576 device
= nvkm_device_find(client
->device
);
579 nvkm_device_del(&device
);
583 nouveau_drm_remove(struct pci_dev
*pdev
)
585 struct drm_device
*dev
= pci_get_drvdata(pdev
);
587 nouveau_drm_device_remove(dev
);
591 nouveau_do_suspend(struct drm_device
*dev
, bool runtime
)
593 struct nouveau_drm
*drm
= nouveau_drm(dev
);
596 nouveau_led_suspend(dev
);
598 if (dev
->mode_config
.num_crtc
) {
599 NV_DEBUG(drm
, "suspending console...\n");
600 nouveau_fbcon_set_suspend(dev
, 1);
601 NV_DEBUG(drm
, "suspending display...\n");
602 ret
= nouveau_display_suspend(dev
, runtime
);
607 NV_DEBUG(drm
, "evicting buffers...\n");
608 ttm_bo_evict_mm(&drm
->ttm
.bdev
, TTM_PL_VRAM
);
610 NV_DEBUG(drm
, "waiting for kernel channels to go idle...\n");
612 ret
= nouveau_channel_idle(drm
->cechan
);
618 ret
= nouveau_channel_idle(drm
->channel
);
623 NV_DEBUG(drm
, "suspending fence...\n");
624 if (drm
->fence
&& nouveau_fence(drm
)->suspend
) {
625 if (!nouveau_fence(drm
)->suspend(drm
)) {
631 NV_DEBUG(drm
, "suspending object tree...\n");
632 ret
= nvif_client_suspend(&drm
->master
.base
);
639 if (drm
->fence
&& nouveau_fence(drm
)->resume
)
640 nouveau_fence(drm
)->resume(drm
);
643 if (dev
->mode_config
.num_crtc
) {
644 NV_DEBUG(drm
, "resuming display...\n");
645 nouveau_display_resume(dev
, runtime
);
651 nouveau_do_resume(struct drm_device
*dev
, bool runtime
)
653 struct nouveau_drm
*drm
= nouveau_drm(dev
);
655 NV_DEBUG(drm
, "resuming object tree...\n");
656 nvif_client_resume(&drm
->master
.base
);
658 NV_DEBUG(drm
, "resuming fence...\n");
659 if (drm
->fence
&& nouveau_fence(drm
)->resume
)
660 nouveau_fence(drm
)->resume(drm
);
662 nouveau_run_vbios_init(dev
);
664 if (dev
->mode_config
.num_crtc
) {
665 NV_DEBUG(drm
, "resuming display...\n");
666 nouveau_display_resume(dev
, runtime
);
667 NV_DEBUG(drm
, "resuming console...\n");
668 nouveau_fbcon_set_suspend(dev
, 0);
671 nouveau_led_resume(dev
);
677 nouveau_pmops_suspend(struct device
*dev
)
679 struct pci_dev
*pdev
= to_pci_dev(dev
);
680 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
683 if (drm_dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
||
684 drm_dev
->switch_power_state
== DRM_SWITCH_POWER_DYNAMIC_OFF
)
687 ret
= nouveau_do_suspend(drm_dev
, false);
691 pci_save_state(pdev
);
692 pci_disable_device(pdev
);
693 pci_set_power_state(pdev
, PCI_D3hot
);
699 nouveau_pmops_resume(struct device
*dev
)
701 struct pci_dev
*pdev
= to_pci_dev(dev
);
702 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
705 if (drm_dev
->switch_power_state
== DRM_SWITCH_POWER_OFF
||
706 drm_dev
->switch_power_state
== DRM_SWITCH_POWER_DYNAMIC_OFF
)
709 pci_set_power_state(pdev
, PCI_D0
);
710 pci_restore_state(pdev
);
711 ret
= pci_enable_device(pdev
);
714 pci_set_master(pdev
);
716 ret
= nouveau_do_resume(drm_dev
, false);
718 /* Monitors may have been connected / disconnected during suspend */
719 schedule_work(&nouveau_drm(drm_dev
)->hpd_work
);
725 nouveau_pmops_freeze(struct device
*dev
)
727 struct pci_dev
*pdev
= to_pci_dev(dev
);
728 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
729 return nouveau_do_suspend(drm_dev
, false);
733 nouveau_pmops_thaw(struct device
*dev
)
735 struct pci_dev
*pdev
= to_pci_dev(dev
);
736 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
737 return nouveau_do_resume(drm_dev
, false);
741 nouveau_pmops_runtime(void)
743 if (nouveau_runtime_pm
== -1)
744 return nouveau_is_optimus() || nouveau_is_v1_dsm();
745 return nouveau_runtime_pm
== 1;
749 nouveau_pmops_runtime_suspend(struct device
*dev
)
751 struct pci_dev
*pdev
= to_pci_dev(dev
);
752 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
755 if (!nouveau_pmops_runtime()) {
756 pm_runtime_forbid(dev
);
760 drm_kms_helper_poll_disable(drm_dev
);
761 vga_switcheroo_set_dynamic_switch(pdev
, VGA_SWITCHEROO_OFF
);
762 nouveau_switcheroo_optimus_dsm();
763 ret
= nouveau_do_suspend(drm_dev
, true);
764 pci_save_state(pdev
);
765 pci_disable_device(pdev
);
766 pci_ignore_hotplug(pdev
);
767 pci_set_power_state(pdev
, PCI_D3cold
);
768 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_DYNAMIC_OFF
;
773 nouveau_pmops_runtime_resume(struct device
*dev
)
775 struct pci_dev
*pdev
= to_pci_dev(dev
);
776 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
777 struct nvif_device
*device
= &nouveau_drm(drm_dev
)->client
.device
;
780 if (!nouveau_pmops_runtime()) {
781 pm_runtime_forbid(dev
);
785 pci_set_power_state(pdev
, PCI_D0
);
786 pci_restore_state(pdev
);
787 ret
= pci_enable_device(pdev
);
790 pci_set_master(pdev
);
792 ret
= nouveau_do_resume(drm_dev
, true);
795 nvif_mask(&device
->object
, 0x088488, (1 << 25), (1 << 25));
796 vga_switcheroo_set_dynamic_switch(pdev
, VGA_SWITCHEROO_ON
);
797 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
799 /* Monitors may have been connected / disconnected during suspend */
800 schedule_work(&nouveau_drm(drm_dev
)->hpd_work
);
806 nouveau_pmops_runtime_idle(struct device
*dev
)
808 struct pci_dev
*pdev
= to_pci_dev(dev
);
809 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
810 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
811 struct drm_crtc
*crtc
;
813 if (!nouveau_pmops_runtime()) {
814 pm_runtime_forbid(dev
);
818 /* if we have a hdmi audio device - make sure it has a driver loaded */
819 if (drm
->hdmi_device
) {
820 if (!drm
->hdmi_device
->driver
) {
821 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
822 pm_runtime_mark_last_busy(dev
);
827 list_for_each_entry(crtc
, &drm
->dev
->mode_config
.crtc_list
, head
) {
829 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
833 pm_runtime_mark_last_busy(dev
);
834 pm_runtime_autosuspend(dev
);
835 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
840 nouveau_drm_open(struct drm_device
*dev
, struct drm_file
*fpriv
)
842 struct nouveau_drm
*drm
= nouveau_drm(dev
);
843 struct nouveau_cli
*cli
;
844 char name
[32], tmpname
[TASK_COMM_LEN
];
847 /* need to bring up power immediately if opening device */
848 ret
= pm_runtime_get_sync(dev
->dev
);
849 if (ret
< 0 && ret
!= -EACCES
)
852 get_task_comm(tmpname
, current
);
853 snprintf(name
, sizeof(name
), "%s[%d]", tmpname
, pid_nr(fpriv
->pid
));
855 if (!(cli
= kzalloc(sizeof(*cli
), GFP_KERNEL
)))
858 ret
= nouveau_cli_init(drm
, name
, cli
);
862 cli
->base
.super
= false;
864 if (drm
->client
.device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
) {
865 ret
= nouveau_vmm_init(cli
, 0, &cli
->vmm
);
869 cli
->vm
= cli
->vmm
.vm
;
870 nvxx_client(&cli
->base
)->vm
= cli
->vm
;
873 fpriv
->driver_priv
= cli
;
875 mutex_lock(&drm
->client
.mutex
);
876 list_add(&cli
->head
, &drm
->clients
);
877 mutex_unlock(&drm
->client
.mutex
);
881 nouveau_cli_fini(cli
);
885 pm_runtime_mark_last_busy(dev
->dev
);
886 pm_runtime_put_autosuspend(dev
->dev
);
891 nouveau_drm_postclose(struct drm_device
*dev
, struct drm_file
*fpriv
)
893 struct nouveau_cli
*cli
= nouveau_cli(fpriv
);
894 struct nouveau_drm
*drm
= nouveau_drm(dev
);
896 pm_runtime_get_sync(dev
->dev
);
898 mutex_lock(&cli
->mutex
);
900 nouveau_abi16_fini(cli
->abi16
);
901 mutex_unlock(&cli
->mutex
);
903 mutex_lock(&drm
->client
.mutex
);
904 list_del(&cli
->head
);
905 mutex_unlock(&drm
->client
.mutex
);
907 nouveau_cli_fini(cli
);
909 pm_runtime_mark_last_busy(dev
->dev
);
910 pm_runtime_put_autosuspend(dev
->dev
);
913 static const struct drm_ioctl_desc
915 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM
, nouveau_abi16_ioctl_getparam
, DRM_AUTH
|DRM_RENDER_ALLOW
),
916 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM
, nouveau_abi16_ioctl_setparam
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
917 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC
, nouveau_abi16_ioctl_channel_alloc
, DRM_AUTH
|DRM_RENDER_ALLOW
),
918 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE
, nouveau_abi16_ioctl_channel_free
, DRM_AUTH
|DRM_RENDER_ALLOW
),
919 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC
, nouveau_abi16_ioctl_grobj_alloc
, DRM_AUTH
|DRM_RENDER_ALLOW
),
920 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC
, nouveau_abi16_ioctl_notifierobj_alloc
, DRM_AUTH
|DRM_RENDER_ALLOW
),
921 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE
, nouveau_abi16_ioctl_gpuobj_free
, DRM_AUTH
|DRM_RENDER_ALLOW
),
922 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW
, nouveau_gem_ioctl_new
, DRM_AUTH
|DRM_RENDER_ALLOW
),
923 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF
, nouveau_gem_ioctl_pushbuf
, DRM_AUTH
|DRM_RENDER_ALLOW
),
924 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP
, nouveau_gem_ioctl_cpu_prep
, DRM_AUTH
|DRM_RENDER_ALLOW
),
925 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI
, nouveau_gem_ioctl_cpu_fini
, DRM_AUTH
|DRM_RENDER_ALLOW
),
926 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO
, nouveau_gem_ioctl_info
, DRM_AUTH
|DRM_RENDER_ALLOW
),
930 nouveau_drm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
932 struct drm_file
*filp
= file
->private_data
;
933 struct drm_device
*dev
= filp
->minor
->dev
;
936 ret
= pm_runtime_get_sync(dev
->dev
);
937 if (ret
< 0 && ret
!= -EACCES
)
940 switch (_IOC_NR(cmd
) - DRM_COMMAND_BASE
) {
941 case DRM_NOUVEAU_NVIF
:
942 ret
= usif_ioctl(filp
, (void __user
*)arg
, _IOC_SIZE(cmd
));
945 ret
= drm_ioctl(file
, cmd
, arg
);
949 pm_runtime_mark_last_busy(dev
->dev
);
950 pm_runtime_put_autosuspend(dev
->dev
);
954 static const struct file_operations
955 nouveau_driver_fops
= {
956 .owner
= THIS_MODULE
,
958 .release
= drm_release
,
959 .unlocked_ioctl
= nouveau_drm_ioctl
,
960 .mmap
= nouveau_ttm_mmap
,
963 #if defined(CONFIG_COMPAT)
964 .compat_ioctl
= nouveau_compat_ioctl
,
966 .llseek
= noop_llseek
,
969 static struct drm_driver
972 DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
| DRIVER_RENDER
|
973 DRIVER_KMS_LEGACY_CONTEXT
,
975 .load
= nouveau_drm_load
,
976 .unload
= nouveau_drm_unload
,
977 .open
= nouveau_drm_open
,
978 .postclose
= nouveau_drm_postclose
,
979 .lastclose
= nouveau_vga_lastclose
,
981 #if defined(CONFIG_DEBUG_FS)
982 .debugfs_init
= nouveau_drm_debugfs_init
,
985 .enable_vblank
= nouveau_display_vblank_enable
,
986 .disable_vblank
= nouveau_display_vblank_disable
,
987 .get_scanout_position
= nouveau_display_scanoutpos
,
988 .get_vblank_timestamp
= drm_calc_vbltimestamp_from_scanoutpos
,
990 .ioctls
= nouveau_ioctls
,
991 .num_ioctls
= ARRAY_SIZE(nouveau_ioctls
),
992 .fops
= &nouveau_driver_fops
,
994 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
995 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
996 .gem_prime_export
= drm_gem_prime_export
,
997 .gem_prime_import
= drm_gem_prime_import
,
998 .gem_prime_pin
= nouveau_gem_prime_pin
,
999 .gem_prime_res_obj
= nouveau_gem_prime_res_obj
,
1000 .gem_prime_unpin
= nouveau_gem_prime_unpin
,
1001 .gem_prime_get_sg_table
= nouveau_gem_prime_get_sg_table
,
1002 .gem_prime_import_sg_table
= nouveau_gem_prime_import_sg_table
,
1003 .gem_prime_vmap
= nouveau_gem_prime_vmap
,
1004 .gem_prime_vunmap
= nouveau_gem_prime_vunmap
,
1006 .gem_free_object_unlocked
= nouveau_gem_object_del
,
1007 .gem_open_object
= nouveau_gem_object_open
,
1008 .gem_close_object
= nouveau_gem_object_close
,
1010 .dumb_create
= nouveau_display_dumb_create
,
1011 .dumb_map_offset
= nouveau_display_dumb_map_offset
,
1013 .name
= DRIVER_NAME
,
1014 .desc
= DRIVER_DESC
,
1016 .date
= GIT_REVISION
,
1018 .date
= DRIVER_DATE
,
1020 .major
= DRIVER_MAJOR
,
1021 .minor
= DRIVER_MINOR
,
1022 .patchlevel
= DRIVER_PATCHLEVEL
,
1025 static struct pci_device_id
1026 nouveau_drm_pci_table
[] = {
1028 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA
, PCI_ANY_ID
),
1029 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
1030 .class_mask
= 0xff << 16,
1033 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS
, PCI_ANY_ID
),
1034 .class = PCI_BASE_CLASS_DISPLAY
<< 16,
1035 .class_mask
= 0xff << 16,
1040 static void nouveau_display_options(void)
1042 DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
1044 DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable
);
1045 DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid
);
1046 DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink
);
1047 DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel
);
1048 DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config
);
1049 DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug
);
1050 DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel
);
1051 DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset
);
1052 DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm
);
1053 DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf
);
1054 DRM_DEBUG_DRIVER("... hdmimhz : %d\n", nouveau_hdmimhz
);
1057 static const struct dev_pm_ops nouveau_pm_ops
= {
1058 .suspend
= nouveau_pmops_suspend
,
1059 .resume
= nouveau_pmops_resume
,
1060 .freeze
= nouveau_pmops_freeze
,
1061 .thaw
= nouveau_pmops_thaw
,
1062 .poweroff
= nouveau_pmops_freeze
,
1063 .restore
= nouveau_pmops_resume
,
1064 .runtime_suspend
= nouveau_pmops_runtime_suspend
,
1065 .runtime_resume
= nouveau_pmops_runtime_resume
,
1066 .runtime_idle
= nouveau_pmops_runtime_idle
,
1069 static struct pci_driver
1070 nouveau_drm_pci_driver
= {
1072 .id_table
= nouveau_drm_pci_table
,
1073 .probe
= nouveau_drm_probe
,
1074 .remove
= nouveau_drm_remove
,
1075 .driver
.pm
= &nouveau_pm_ops
,
1079 nouveau_platform_device_create(const struct nvkm_device_tegra_func
*func
,
1080 struct platform_device
*pdev
,
1081 struct nvkm_device
**pdevice
)
1083 struct drm_device
*drm
;
1086 err
= nvkm_device_tegra_new(func
, pdev
, nouveau_config
, nouveau_debug
,
1087 true, true, ~0ULL, pdevice
);
1091 drm
= drm_dev_alloc(&driver_platform
, &pdev
->dev
);
1097 platform_set_drvdata(pdev
, drm
);
1102 nvkm_device_del(pdevice
);
1104 return ERR_PTR(err
);
1108 nouveau_drm_init(void)
1110 driver_pci
= driver_stub
;
1111 driver_platform
= driver_stub
;
1113 nouveau_display_options();
1115 if (nouveau_modeset
== -1) {
1116 if (vgacon_text_force())
1117 nouveau_modeset
= 0;
1120 if (!nouveau_modeset
)
1123 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1124 platform_driver_register(&nouveau_platform_driver
);
1127 nouveau_register_dsm_handler();
1128 nouveau_backlight_ctor();
1131 return pci_register_driver(&nouveau_drm_pci_driver
);
1138 nouveau_drm_exit(void)
1140 if (!nouveau_modeset
)
1144 pci_unregister_driver(&nouveau_drm_pci_driver
);
1146 nouveau_backlight_dtor();
1147 nouveau_unregister_dsm_handler();
1149 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1150 platform_driver_unregister(&nouveau_platform_driver
);
1154 module_init(nouveau_drm_init
);
1155 module_exit(nouveau_drm_exit
);
1157 MODULE_DEVICE_TABLE(pci
, nouveau_drm_pci_table
);
1158 MODULE_AUTHOR(DRIVER_AUTHOR
);
1159 MODULE_DESCRIPTION(DRIVER_DESC
);
1160 MODULE_LICENSE("GPL and additional rights");