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