blob: fb148fab1a579a2b10f4fed457066549369ebaac [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <stdint.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05004#include <console/console.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02005#include <device/mmio.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <device/pci_def.h>
Tristan Corrick334be322018-12-17 22:10:21 +13007#include <device/pci_ops.h>
Elyes HAOUASc27014b2019-06-23 11:11:53 +02008
Aaron Durbin76c37002012-10-30 09:03:43 -05009#include "haswell.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050010
Tristan Corrick334be322018-12-17 22:10:21 +130011static bool peg_hidden[3];
12
Aaron Durbin76c37002012-10-30 09:03:43 -050013static void haswell_setup_bars(void)
14{
Aaron Durbin76c37002012-10-30 09:03:43 -050015 printk(BIOS_DEBUG, "Setting up static northbridge registers...");
16 /* Set up all hardcoded northbridge BARs */
Angel Pons1db5bc72020-01-15 00:49:03 +010017 pci_write_config32(HOST_BRIDGE, EPBAR, DEFAULT_EPBAR | 1);
18 pci_write_config32(HOST_BRIDGE, EPBAR + 4, (0LL + DEFAULT_EPBAR) >> 32);
19 pci_write_config32(HOST_BRIDGE, MCHBAR, DEFAULT_MCHBAR | 1);
20 pci_write_config32(HOST_BRIDGE, MCHBAR + 4, (0LL + DEFAULT_MCHBAR) >> 32);
21 pci_write_config32(HOST_BRIDGE, DMIBAR, DEFAULT_DMIBAR | 1);
22 pci_write_config32(HOST_BRIDGE, DMIBAR + 4, (0LL + DEFAULT_DMIBAR) >> 32);
Aaron Durbin76c37002012-10-30 09:03:43 -050023
24 /* Set C0000-FFFFF to access RAM on both reads and writes */
Angel Pons1db5bc72020-01-15 00:49:03 +010025 pci_write_config8(HOST_BRIDGE, PAM0, 0x30);
26 pci_write_config8(HOST_BRIDGE, PAM1, 0x33);
27 pci_write_config8(HOST_BRIDGE, PAM2, 0x33);
28 pci_write_config8(HOST_BRIDGE, PAM3, 0x33);
29 pci_write_config8(HOST_BRIDGE, PAM4, 0x33);
30 pci_write_config8(HOST_BRIDGE, PAM5, 0x33);
31 pci_write_config8(HOST_BRIDGE, PAM6, 0x33);
Aaron Durbin76c37002012-10-30 09:03:43 -050032
33 printk(BIOS_DEBUG, " done.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -050034}
35
Tristan Corrick334be322018-12-17 22:10:21 +130036static void haswell_setup_igd(void)
Aaron Durbin76c37002012-10-30 09:03:43 -050037{
Tristan Corrickc5d367b2018-12-17 22:10:07 +130038 bool igd_enabled;
39 u16 ggc;
Aaron Durbin76c37002012-10-30 09:03:43 -050040 u8 reg8;
41
Tristan Corrick334be322018-12-17 22:10:21 +130042 printk(BIOS_DEBUG, "Initializing IGD...\n");
Aaron Durbin76c37002012-10-30 09:03:43 -050043
Angel Pons1db5bc72020-01-15 00:49:03 +010044 igd_enabled = !!(pci_read_config32(HOST_BRIDGE, DEVEN) & DEVEN_D2EN);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130045
Angel Pons1db5bc72020-01-15 00:49:03 +010046 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130047 ggc &= ~0x3f8;
48 if (igd_enabled) {
49 ggc |= GGC_GTT_2MB | GGC_IGD_MEM_IN_32MB_UNITS(1);
50 ggc &= ~GGC_DISABLE_VGA_IO_DECODE;
51 } else {
Angel Pons1db5bc72020-01-15 00:49:03 +010052 ggc |= GGC_GTT_0MB | GGC_IGD_MEM_IN_32MB_UNITS(0) | GGC_DISABLE_VGA_IO_DECODE;
Tristan Corrickc5d367b2018-12-17 22:10:07 +130053 }
Angel Pons1db5bc72020-01-15 00:49:03 +010054 pci_write_config16(HOST_BRIDGE, GGC, ggc);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130055
56 if (!igd_enabled) {
57 printk(BIOS_DEBUG, "IGD is disabled.\n");
58 return;
59 }
Aaron Durbin76c37002012-10-30 09:03:43 -050060
61 /* Enable 256MB aperture */
62 reg8 = pci_read_config8(PCI_DEV(0, 2, 0), MSAC);
63 reg8 &= ~0x06;
64 reg8 |= 0x02;
65 pci_write_config8(PCI_DEV(0, 2, 0), MSAC, reg8);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130066}
67
Tristan Corrick334be322018-12-17 22:10:21 +130068static void start_peg2_link_training(const pci_devfn_t dev)
69{
70 u32 mask;
71
72 switch (dev) {
73 case PCI_DEV(0, 1, 2):
74 mask = DEVEN_D1F2EN;
75 break;
76 case PCI_DEV(0, 1, 1):
77 mask = DEVEN_D1F1EN;
78 break;
79 case PCI_DEV(0, 1, 0):
80 mask = DEVEN_D1F0EN;
81 break;
82 default:
83 printk(BIOS_ERR, "Link training tried on a non-PEG device!\n");
84 return;
85 }
86
87 pci_update_config32(dev, 0xc24, ~(1 << 16), 1 << 5);
Chris Morgan2806ec92020-02-05 10:51:46 -060088 printk(BIOS_DEBUG, "Started PEG1%d link training.\n", PCI_FUNC(PCI_DEV2DEVFN(dev)));
Tristan Corrick334be322018-12-17 22:10:21 +130089
90 /*
Angel Pons1db5bc72020-01-15 00:49:03 +010091 * Hide the PEG device while the MRC runs. This is because the MRC makes
92 * configurations that are not ideal if it sees a VGA device in a PEG slot,
93 * and it locks registers preventing changes to these configurations.
Tristan Corrick334be322018-12-17 22:10:21 +130094 */
Angel Pons1db5bc72020-01-15 00:49:03 +010095 pci_update_config32(HOST_BRIDGE, DEVEN, ~mask, 0);
Chris Morgan2806ec92020-02-05 10:51:46 -060096 peg_hidden[PCI_FUNC(PCI_DEV2DEVFN(dev))] = true;
97 printk(BIOS_DEBUG, "Temporarily hiding PEG1%d.\n", PCI_FUNC(PCI_DEV2DEVFN(dev)));
Tristan Corrick334be322018-12-17 22:10:21 +130098}
99
100void haswell_unhide_peg(void)
101{
Angel Pons1db5bc72020-01-15 00:49:03 +0100102 u32 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Tristan Corrick334be322018-12-17 22:10:21 +1300103
104 for (u8 fn = 0; fn <= 2; fn++) {
105 if (peg_hidden[fn]) {
106 deven |= DEVEN_D1F0EN >> fn;
107 peg_hidden[fn] = false;
108 printk(BIOS_DEBUG, "Unhiding PEG1%d.\n", fn);
109 }
110 }
111
Angel Pons1db5bc72020-01-15 00:49:03 +0100112 pci_write_config32(HOST_BRIDGE, DEVEN, deven);
Tristan Corrick334be322018-12-17 22:10:21 +1300113}
114
115static void haswell_setup_peg(void)
116{
Angel Pons1db5bc72020-01-15 00:49:03 +0100117 u32 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Tristan Corrick334be322018-12-17 22:10:21 +1300118
119 if (deven & DEVEN_D1F2EN)
120 start_peg2_link_training(PCI_DEV(0, 1, 2));
Angel Pons1db5bc72020-01-15 00:49:03 +0100121
Tristan Corrick334be322018-12-17 22:10:21 +1300122 if (deven & DEVEN_D1F1EN)
123 start_peg2_link_training(PCI_DEV(0, 1, 1));
Angel Pons1db5bc72020-01-15 00:49:03 +0100124
Tristan Corrick334be322018-12-17 22:10:21 +1300125 if (deven & DEVEN_D1F0EN)
126 start_peg2_link_training(PCI_DEV(0, 1, 0));
127}
128
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300129static void haswell_setup_misc(void)
130{
131 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500132
133 /* Erratum workarounds */
Angel Pons1db5bc72020-01-15 00:49:03 +0100134 reg32 = MCHBAR32(SAPMCTL);
135 reg32 |= (1 << 9) | (1 << 10);
136 MCHBAR32(SAPMCTL) = reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500137
138 /* Enable SA Clock Gating */
Angel Pons1db5bc72020-01-15 00:49:03 +0100139 reg32 = MCHBAR32(SAPMCTL);
140 MCHBAR32(SAPMCTL) = reg32 | 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500141
142 /* GPU RC6 workaround for sighting 366252 */
Angel Pons1db5bc72020-01-15 00:49:03 +0100143 reg32 = MCHBAR32(SSKPD + 4);
Ryan Salsamendib9bc2572017-07-04 13:35:06 -0700144 reg32 |= (1UL << 31);
Angel Pons1db5bc72020-01-15 00:49:03 +0100145 MCHBAR32(SSKPD + 4) = reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500146
Angel Pons1db5bc72020-01-15 00:49:03 +0100147 /* VLW (Virtual Legacy Wire?) */
Aaron Durbin76c37002012-10-30 09:03:43 -0500148 reg32 = MCHBAR32(0x6120);
149 reg32 &= ~(1 << 0);
150 MCHBAR32(0x6120) = reg32;
151
Angel Pons1db5bc72020-01-15 00:49:03 +0100152 reg32 = MCHBAR32(INTRDIRCTL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500153 reg32 |= (1 << 4) | (1 << 5);
Angel Pons1db5bc72020-01-15 00:49:03 +0100154 MCHBAR32(INTRDIRCTL) = reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500155}
156
Matt DeVilliera51e3792018-03-04 01:44:15 -0600157static void haswell_setup_iommu(void)
158{
Angel Pons1db5bc72020-01-15 00:49:03 +0100159 const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600160
161 if (capid0_a & VTD_DISABLE)
162 return;
163
Angel Pons1db5bc72020-01-15 00:49:03 +0100164 /* Setup BARs: zeroize top 32 bits; set enable bit */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600165 MCHBAR32(GFXVTBAR + 4) = GFXVT_BASE_ADDRESS >> 32;
Angel Pons1db5bc72020-01-15 00:49:03 +0100166 MCHBAR32(GFXVTBAR) = GFXVT_BASE_ADDRESS | 1;
Matt DeVilliera51e3792018-03-04 01:44:15 -0600167 MCHBAR32(VTVC0BAR + 4) = VTVC0_BASE_ADDRESS >> 32;
Angel Pons1db5bc72020-01-15 00:49:03 +0100168 MCHBAR32(VTVC0BAR) = VTVC0_BASE_ADDRESS | 1;
Matt DeVilliera51e3792018-03-04 01:44:15 -0600169
Angel Pons1db5bc72020-01-15 00:49:03 +0100170 /* Set L3HIT2PEND_DIS, lock GFXVTBAR policy config registers */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600171 u32 reg32;
172 reg32 = read32((void *)(GFXVT_BASE_ADDRESS + ARCHDIS));
Angel Pons1db5bc72020-01-15 00:49:03 +0100173 write32((void *)(GFXVT_BASE_ADDRESS + ARCHDIS), reg32 | DMAR_LCKDN | L3HIT2PEND_DIS);
174
175 /* Clear SPCAPCTRL */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600176 reg32 = read32((void *)(VTVC0_BASE_ADDRESS + ARCHDIS)) & ~SPCAPCTRL;
Angel Pons1db5bc72020-01-15 00:49:03 +0100177
178 /* Set GLBIOTLBINV, GLBCTXTINV; lock VTVC0BAR policy config registers */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600179 write32((void *)(VTVC0_BASE_ADDRESS + ARCHDIS),
180 reg32 | DMAR_LCKDN | GLBIOTLBINV | GLBCTXTINV);
181}
182
Aaron Durbin76c37002012-10-30 09:03:43 -0500183void haswell_early_initialization(int chipset_type)
184{
Aaron Durbin76c37002012-10-30 09:03:43 -0500185 /* Setup all BARs required for early PCIe and raminit */
186 haswell_setup_bars();
187
Matt DeVilliera51e3792018-03-04 01:44:15 -0600188 /* Setup IOMMU BARs */
189 haswell_setup_iommu();
190
Tristan Corrick334be322018-12-17 22:10:21 +1300191 haswell_setup_peg();
192 haswell_setup_igd();
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300193
194 haswell_setup_misc();
Aaron Durbin76c37002012-10-30 09:03:43 -0500195}