blob: 81638d7f2efff69cdce86ac515aac2c19191aefe [file] [log] [blame]
Ben Skeggs77145f12012-07-31 16:16:21 +10001#include <linux/vgaarb.h>
2#include <linux/vga_switcheroo.h>
3
Linus Torvalds612a9aa2012-10-03 23:29:23 -07004#include <drm/drmP.h>
5#include <drm/drm_crtc_helper.h>
Ben Skeggs77145f12012-07-31 16:16:21 +10006
7#include "nouveau_drm.h"
8#include "nouveau_acpi.h"
9#include "nouveau_fbcon.h"
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +020010#include "nouveau_vga.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100011
12static unsigned int
13nouveau_vga_set_decode(void *priv, bool state)
14{
15 struct nouveau_device *device = nouveau_dev(priv);
16
17 if (device->chipset >= 0x40)
18 nv_wr32(device, 0x088054, state);
19 else
20 nv_wr32(device, 0x001854, state);
21
22 if (state)
23 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
24 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
25 else
26 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
27}
28
29static void
30nouveau_switcheroo_set_state(struct pci_dev *pdev,
31 enum vga_switcheroo_state state)
32{
33 struct drm_device *dev = pci_get_drvdata(pdev);
Ben Skeggs77145f12012-07-31 16:16:21 +100034
Dave Airlie5addcf02012-09-10 14:20:51 +100035 if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
36 return;
37
Ben Skeggs77145f12012-07-31 16:16:21 +100038 if (state == VGA_SWITCHEROO_ON) {
39 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
40 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +100041 nouveau_pmops_resume(&pdev->dev);
Ben Skeggs77145f12012-07-31 16:16:21 +100042 drm_kms_helper_poll_enable(dev);
43 dev->switch_power_state = DRM_SWITCH_POWER_ON;
44 } else {
45 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
46 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
47 drm_kms_helper_poll_disable(dev);
48 nouveau_switcheroo_optimus_dsm();
Dave Airlie2d8b9cc2012-11-02 11:04:28 +100049 nouveau_pmops_suspend(&pdev->dev);
Ben Skeggs77145f12012-07-31 16:16:21 +100050 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
51 }
52}
53
54static void
55nouveau_switcheroo_reprobe(struct pci_dev *pdev)
56{
57 struct drm_device *dev = pci_get_drvdata(pdev);
58 nouveau_fbcon_output_poll_changed(dev);
59}
60
61static bool
62nouveau_switcheroo_can_switch(struct pci_dev *pdev)
63{
64 struct drm_device *dev = pci_get_drvdata(pdev);
65 bool can_switch;
66
67 spin_lock(&dev->count_lock);
68 can_switch = (dev->open_count == 0);
69 spin_unlock(&dev->count_lock);
70 return can_switch;
71}
72
73static const struct vga_switcheroo_client_ops
74nouveau_switcheroo_ops = {
75 .set_gpu_state = nouveau_switcheroo_set_state,
76 .reprobe = nouveau_switcheroo_reprobe,
77 .can_switch = nouveau_switcheroo_can_switch,
78};
79
80void
81nouveau_vga_init(struct nouveau_drm *drm)
82{
83 struct drm_device *dev = drm->dev;
Dave Airlie5addcf02012-09-10 14:20:51 +100084 bool runtime = false;
Ben Skeggs77145f12012-07-31 16:16:21 +100085 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
Dave Airlie5addcf02012-09-10 14:20:51 +100086
87 if (nouveau_runtime_pm == 1)
88 runtime = true;
89 if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
90 runtime = true;
91 vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
92
93 if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
94 vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
Ben Skeggs77145f12012-07-31 16:16:21 +100095}
96
97void
98nouveau_vga_fini(struct nouveau_drm *drm)
99{
100 struct drm_device *dev = drm->dev;
101 vga_switcheroo_unregister_client(dev->pdev);
102 vga_client_register(dev->pdev, NULL, NULL, NULL);
103}
104
105
106void
107nouveau_vga_lastclose(struct drm_device *dev)
108{
109 vga_switcheroo_process_delayed_switch();
110}