nouveau_vga.c revision 1.1.1.2.30.1 1 /* $NetBSD: nouveau_vga.c,v 1.1.1.2.30.1 2018/09/06 06:56:18 pgoyette Exp $ */
2
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: nouveau_vga.c,v 1.1.1.2.30.1 2018/09/06 06:56:18 pgoyette Exp $");
5
6 #include <linux/vgaarb.h>
7 #include <linux/vga_switcheroo.h>
8
9 #include <drm/drmP.h>
10 #include <drm/drm_crtc_helper.h>
11
12 #include "nouveau_drm.h"
13 #include "nouveau_acpi.h"
14 #include "nouveau_fbcon.h"
15 #include "nouveau_vga.h"
16
17 static unsigned int
18 nouveau_vga_set_decode(void *priv, bool state)
19 {
20 struct nouveau_drm *drm = nouveau_drm(priv);
21 struct nvif_object *device = &drm->device.object;
22
23 if (drm->device.info.family == NV_DEVICE_INFO_V0_CURIE &&
24 drm->device.info.chipset >= 0x4c)
25 nvif_wr32(device, 0x088060, state);
26 else
27 if (drm->device.info.chipset >= 0x40)
28 nvif_wr32(device, 0x088054, state);
29 else
30 nvif_wr32(device, 0x001854, state);
31
32 if (state)
33 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
34 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
35 else
36 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
37 }
38
39 static void
40 nouveau_switcheroo_set_state(struct pci_dev *pdev,
41 enum vga_switcheroo_state state)
42 {
43 struct drm_device *dev = pci_get_drvdata(pdev);
44
45 if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
46 return;
47
48 if (state == VGA_SWITCHEROO_ON) {
49 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
50 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
51 nouveau_pmops_resume(&pdev->dev);
52 drm_kms_helper_poll_enable(dev);
53 dev->switch_power_state = DRM_SWITCH_POWER_ON;
54 } else {
55 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
56 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
57 drm_kms_helper_poll_disable(dev);
58 nouveau_switcheroo_optimus_dsm();
59 nouveau_pmops_suspend(&pdev->dev);
60 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
61 }
62 }
63
64 static void
65 nouveau_switcheroo_reprobe(struct pci_dev *pdev)
66 {
67 struct drm_device *dev = pci_get_drvdata(pdev);
68 nouveau_fbcon_output_poll_changed(dev);
69 }
70
71 static bool
72 nouveau_switcheroo_can_switch(struct pci_dev *pdev)
73 {
74 struct drm_device *dev = pci_get_drvdata(pdev);
75
76 /*
77 * FIXME: open_count is protected by drm_global_mutex but that would lead to
78 * locking inversion with the driver load path. And the access here is
79 * completely racy anyway. So don't bother with locking for now.
80 */
81 return dev->open_count == 0;
82 }
83
84 static const struct vga_switcheroo_client_ops
85 nouveau_switcheroo_ops = {
86 .set_gpu_state = nouveau_switcheroo_set_state,
87 .reprobe = nouveau_switcheroo_reprobe,
88 .can_switch = nouveau_switcheroo_can_switch,
89 };
90
91 void
92 nouveau_vga_init(struct nouveau_drm *drm)
93 {
94 struct drm_device *dev = drm->dev;
95 bool runtime = false;
96
97 /* only relevant for PCI devices */
98 if (!dev->pdev)
99 return;
100
101 vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
102
103 if (nouveau_runtime_pm == 1)
104 runtime = true;
105 if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
106 runtime = true;
107 vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
108
109 if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
110 vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
111 }
112
113 void
114 nouveau_vga_fini(struct nouveau_drm *drm)
115 {
116 struct drm_device *dev = drm->dev;
117 bool runtime = false;
118
119 if (nouveau_runtime_pm == 1)
120 runtime = true;
121 if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
122 runtime = true;
123
124 vga_switcheroo_unregister_client(dev->pdev);
125 if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
126 vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
127 vga_client_register(dev->pdev, NULL, NULL, NULL);
128 }
129
130
131 void
132 nouveau_vga_lastclose(struct drm_device *dev)
133 {
134 vga_switcheroo_process_delayed_switch();
135 }
136