1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Intel Corporation
4 */
5
6 #include <linux/pci.h>
7 #include <linux/vgaarb.h>
8
9 #include <video/vga.h>
10
11 #include "soc/intel_gmch.h"
12
13 #include "i915_drv.h"
14 #include "i915_reg.h"
15 #include "intel_de.h"
16 #include "intel_vga.h"
17
intel_vga_cntrl_reg(struct drm_i915_private * i915)18 static i915_reg_t intel_vga_cntrl_reg(struct drm_i915_private *i915)
19 {
20 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
21 return VLV_VGACNTRL;
22 else if (DISPLAY_VER(i915) >= 5)
23 return CPU_VGACNTRL;
24 else
25 return VGACNTRL;
26 }
27
28 /* Disable the VGA plane that we never use */
intel_vga_disable(struct drm_i915_private * dev_priv)29 void intel_vga_disable(struct drm_i915_private *dev_priv)
30 {
31 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
32 i915_reg_t vga_reg = intel_vga_cntrl_reg(dev_priv);
33 u8 sr1;
34
35 if (intel_de_read(dev_priv, vga_reg) & VGA_DISP_DISABLE)
36 return;
37
38 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
39 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
40 outb(0x01, VGA_SEQ_I);
41 sr1 = inb(VGA_SEQ_D);
42 outb(sr1 | VGA_SR01_SCREEN_OFF, VGA_SEQ_D);
43 vga_put(pdev, VGA_RSRC_LEGACY_IO);
44 udelay(300);
45
46 intel_de_write(dev_priv, vga_reg, VGA_DISP_DISABLE);
47 intel_de_posting_read(dev_priv, vga_reg);
48 }
49
intel_vga_redisable_power_on(struct drm_i915_private * dev_priv)50 void intel_vga_redisable_power_on(struct drm_i915_private *dev_priv)
51 {
52 i915_reg_t vga_reg = intel_vga_cntrl_reg(dev_priv);
53
54 if (!(intel_de_read(dev_priv, vga_reg) & VGA_DISP_DISABLE)) {
55 drm_dbg_kms(&dev_priv->drm,
56 "Something enabled VGA plane, disabling it\n");
57 intel_vga_disable(dev_priv);
58 }
59 }
60
intel_vga_redisable(struct drm_i915_private * i915)61 void intel_vga_redisable(struct drm_i915_private *i915)
62 {
63 intel_wakeref_t wakeref;
64
65 /*
66 * This function can be called both from intel_modeset_setup_hw_state or
67 * at a very early point in our resume sequence, where the power well
68 * structures are not yet restored. Since this function is at a very
69 * paranoid "someone might have enabled VGA while we were not looking"
70 * level, just check if the power well is enabled instead of trying to
71 * follow the "don't touch the power well if we don't need it" policy
72 * the rest of the driver uses.
73 */
74 wakeref = intel_display_power_get_if_enabled(i915, POWER_DOMAIN_VGA);
75 if (!wakeref)
76 return;
77
78 intel_vga_redisable_power_on(i915);
79
80 intel_display_power_put(i915, POWER_DOMAIN_VGA, wakeref);
81 }
82
intel_vga_reset_io_mem(struct drm_i915_private * i915)83 void intel_vga_reset_io_mem(struct drm_i915_private *i915)
84 {
85 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
86
87 /*
88 * After we re-enable the power well, if we touch VGA register 0x3d5
89 * we'll get unclaimed register interrupts. This stops after we write
90 * anything to the VGA MSR register. The vgacon module uses this
91 * register all the time, so if we unbind our driver and, as a
92 * consequence, bind vgacon, we'll get stuck in an infinite loop at
93 * console_unlock(). So make here we touch the VGA MSR register, making
94 * sure vgacon can keep working normally without triggering interrupts
95 * and error messages.
96 */
97 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
98 outb(inb(VGA_MIS_R), VGA_MIS_W);
99 vga_put(pdev, VGA_RSRC_LEGACY_IO);
100 }
101
102 static unsigned int
intel_vga_set_decode(struct pci_dev * pdev,bool enable_decode)103 intel_vga_set_decode(struct pci_dev *pdev, bool enable_decode)
104 {
105 struct drm_i915_private *i915 = pdev_to_i915(pdev);
106
107 intel_gmch_vga_set_state(i915, enable_decode);
108
109 if (enable_decode)
110 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
111 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
112 else
113 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
114 }
115
intel_vga_register(struct drm_i915_private * i915)116 int intel_vga_register(struct drm_i915_private *i915)
117 {
118
119 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
120 int ret;
121
122 /*
123 * If we have > 1 VGA cards, then we need to arbitrate access to the
124 * common VGA resources.
125 *
126 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
127 * then we do not take part in VGA arbitration and the
128 * vga_client_register() fails with -ENODEV.
129 */
130 ret = vga_client_register(pdev, intel_vga_set_decode);
131 if (ret && ret != -ENODEV)
132 return ret;
133
134 return 0;
135 }
136
intel_vga_unregister(struct drm_i915_private * i915)137 void intel_vga_unregister(struct drm_i915_private *i915)
138 {
139 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
140
141 vga_client_unregister(pdev);
142 }
143