1 #include <linux/vgaarb.h>
2 #include <linux/vga_switcheroo.h>
5 #include <drm/drm_crtc_helper.h>
7 #include "nouveau_drm.h"
8 #include "nouveau_acpi.h"
9 #include "nouveau_fbcon.h"
10 #include "nouveau_vga.h"
13 nouveau_vga_set_decode(void *priv
, bool state
)
15 struct nouveau_device
*device
= nouveau_dev(priv
);
17 if (device
->card_type
== NV_40
&& device
->chipset
>= 0x4c)
18 nv_wr32(device
, 0x088060, state
);
19 else if (device
->chipset
>= 0x40)
20 nv_wr32(device
, 0x088054, state
);
22 nv_wr32(device
, 0x001854, state
);
25 return VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
26 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
28 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
32 nouveau_switcheroo_set_state(struct pci_dev
*pdev
,
33 enum vga_switcheroo_state state
)
35 struct drm_device
*dev
= pci_get_drvdata(pdev
);
37 if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state
== VGA_SWITCHEROO_OFF
)
40 if (state
== VGA_SWITCHEROO_ON
) {
41 printk(KERN_ERR
"VGA switcheroo: switched nouveau on\n");
42 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
43 nouveau_pmops_resume(&pdev
->dev
);
44 drm_kms_helper_poll_enable(dev
);
45 dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
47 printk(KERN_ERR
"VGA switcheroo: switched nouveau off\n");
48 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
49 drm_kms_helper_poll_disable(dev
);
50 nouveau_switcheroo_optimus_dsm();
51 nouveau_pmops_suspend(&pdev
->dev
);
52 dev
->switch_power_state
= DRM_SWITCH_POWER_OFF
;
57 nouveau_switcheroo_reprobe(struct pci_dev
*pdev
)
59 struct drm_device
*dev
= pci_get_drvdata(pdev
);
60 nouveau_fbcon_output_poll_changed(dev
);
64 nouveau_switcheroo_can_switch(struct pci_dev
*pdev
)
66 struct drm_device
*dev
= pci_get_drvdata(pdev
);
69 * FIXME: open_count is protected by drm_global_mutex but that would lead to
70 * locking inversion with the driver load path. And the access here is
71 * completely racy anyway. So don't bother with locking for now.
73 return dev
->open_count
== 0;
76 static const struct vga_switcheroo_client_ops
77 nouveau_switcheroo_ops
= {
78 .set_gpu_state
= nouveau_switcheroo_set_state
,
79 .reprobe
= nouveau_switcheroo_reprobe
,
80 .can_switch
= nouveau_switcheroo_can_switch
,
84 nouveau_vga_init(struct nouveau_drm
*drm
)
86 struct drm_device
*dev
= drm
->dev
;
89 /* only relevant for PCI devices */
93 vga_client_register(dev
->pdev
, dev
, NULL
, nouveau_vga_set_decode
);
95 if (nouveau_runtime_pm
== 1)
97 if ((nouveau_runtime_pm
== -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
99 vga_switcheroo_register_client(dev
->pdev
, &nouveau_switcheroo_ops
, runtime
);
101 if (runtime
&& nouveau_is_v1_dsm() && !nouveau_is_optimus())
102 vga_switcheroo_init_domain_pm_ops(drm
->dev
->dev
, &drm
->vga_pm_domain
);
106 nouveau_vga_fini(struct nouveau_drm
*drm
)
108 struct drm_device
*dev
= drm
->dev
;
109 vga_switcheroo_unregister_client(dev
->pdev
);
110 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
115 nouveau_vga_lastclose(struct drm_device
*dev
)
117 vga_switcheroo_process_delayed_switch();