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