blob: a0e4211f028814c7ebfe54defd685083666f9ccd [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
Tristan Corrick334be322018-12-17 22:10:21 +130041 printk(BIOS_DEBUG, "Initializing IGD...\n");
Aaron Durbin76c37002012-10-30 09:03:43 -050042
Angel Pons1db5bc72020-01-15 00:49:03 +010043 igd_enabled = !!(pci_read_config32(HOST_BRIDGE, DEVEN) & DEVEN_D2EN);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130044
Angel Pons1db5bc72020-01-15 00:49:03 +010045 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130046 ggc &= ~0x3f8;
47 if (igd_enabled) {
48 ggc |= GGC_GTT_2MB | GGC_IGD_MEM_IN_32MB_UNITS(1);
49 ggc &= ~GGC_DISABLE_VGA_IO_DECODE;
50 } else {
Angel Pons1db5bc72020-01-15 00:49:03 +010051 ggc |= GGC_GTT_0MB | GGC_IGD_MEM_IN_32MB_UNITS(0) | GGC_DISABLE_VGA_IO_DECODE;
Tristan Corrickc5d367b2018-12-17 22:10:07 +130052 }
Angel Pons1db5bc72020-01-15 00:49:03 +010053 pci_write_config16(HOST_BRIDGE, GGC, ggc);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130054
55 if (!igd_enabled) {
56 printk(BIOS_DEBUG, "IGD is disabled.\n");
57 return;
58 }
Aaron Durbin76c37002012-10-30 09:03:43 -050059
60 /* Enable 256MB aperture */
Angel Pons26886072020-06-07 22:19:21 +020061 pci_update_config8(PCI_DEV(0, 2, 0), MSAC, ~0x06, 0x02);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130062}
63
Tristan Corrick334be322018-12-17 22:10:21 +130064static void start_peg2_link_training(const pci_devfn_t dev)
65{
66 u32 mask;
67
68 switch (dev) {
69 case PCI_DEV(0, 1, 2):
70 mask = DEVEN_D1F2EN;
71 break;
72 case PCI_DEV(0, 1, 1):
73 mask = DEVEN_D1F1EN;
74 break;
75 case PCI_DEV(0, 1, 0):
76 mask = DEVEN_D1F0EN;
77 break;
78 default:
79 printk(BIOS_ERR, "Link training tried on a non-PEG device!\n");
80 return;
81 }
82
83 pci_update_config32(dev, 0xc24, ~(1 << 16), 1 << 5);
Chris Morgan2806ec92020-02-05 10:51:46 -060084 printk(BIOS_DEBUG, "Started PEG1%d link training.\n", PCI_FUNC(PCI_DEV2DEVFN(dev)));
Tristan Corrick334be322018-12-17 22:10:21 +130085
86 /*
Angel Pons84641c82020-08-29 02:52:09 +020087 * The MRC will perform PCI enumeration, and if it detects a VGA
88 * device in a PEG slot, it will disable the IGD and not reserve
89 * any memory for it. Since the memory map is locked by the time
90 * MRC finishes, the IGD can't be enabled afterwards. Wonderful.
91 *
92 * If one really wants to enable the Intel iGPU as primary, hide
93 * all PEG devices during MRC execution. This will trick the MRC
94 * into thinking there aren't any, and will enable the IGD. Note
95 * that PEG AFE settings will not be programmed, which may cause
96 * stability problems at higher PCIe link speeds. The most ideal
97 * way to fix this problem for good is to implement native init.
Tristan Corrick334be322018-12-17 22:10:21 +130098 */
Angel Pons84641c82020-08-29 02:52:09 +020099 if (CONFIG(HASWELL_HIDE_PEG_FROM_MRC)) {
100 pci_update_config32(HOST_BRIDGE, DEVEN, ~mask, 0);
101 peg_hidden[PCI_FUNC(PCI_DEV2DEVFN(dev))] = true;
102 printk(BIOS_DEBUG, "Temporarily hiding PEG1%d.\n",
103 PCI_FUNC(PCI_DEV2DEVFN(dev)));
104 }
Tristan Corrick334be322018-12-17 22:10:21 +1300105}
106
107void haswell_unhide_peg(void)
108{
Angel Pons1db5bc72020-01-15 00:49:03 +0100109 u32 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Tristan Corrick334be322018-12-17 22:10:21 +1300110
111 for (u8 fn = 0; fn <= 2; fn++) {
112 if (peg_hidden[fn]) {
113 deven |= DEVEN_D1F0EN >> fn;
114 peg_hidden[fn] = false;
115 printk(BIOS_DEBUG, "Unhiding PEG1%d.\n", fn);
116 }
117 }
118
Angel Pons1db5bc72020-01-15 00:49:03 +0100119 pci_write_config32(HOST_BRIDGE, DEVEN, deven);
Tristan Corrick334be322018-12-17 22:10:21 +1300120}
121
122static void haswell_setup_peg(void)
123{
Angel Pons1db5bc72020-01-15 00:49:03 +0100124 u32 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Tristan Corrick334be322018-12-17 22:10:21 +1300125
126 if (deven & DEVEN_D1F2EN)
127 start_peg2_link_training(PCI_DEV(0, 1, 2));
Angel Pons1db5bc72020-01-15 00:49:03 +0100128
Tristan Corrick334be322018-12-17 22:10:21 +1300129 if (deven & DEVEN_D1F1EN)
130 start_peg2_link_training(PCI_DEV(0, 1, 1));
Angel Pons1db5bc72020-01-15 00:49:03 +0100131
Tristan Corrick334be322018-12-17 22:10:21 +1300132 if (deven & DEVEN_D1F0EN)
133 start_peg2_link_training(PCI_DEV(0, 1, 0));
134}
135
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300136static void haswell_setup_misc(void)
137{
138 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500139
140 /* Erratum workarounds */
Angel Pons1db5bc72020-01-15 00:49:03 +0100141 reg32 = MCHBAR32(SAPMCTL);
142 reg32 |= (1 << 9) | (1 << 10);
143 MCHBAR32(SAPMCTL) = reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500144
145 /* Enable SA Clock Gating */
Angel Pons1db5bc72020-01-15 00:49:03 +0100146 reg32 = MCHBAR32(SAPMCTL);
147 MCHBAR32(SAPMCTL) = reg32 | 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500148
Angel Pons1db5bc72020-01-15 00:49:03 +0100149 reg32 = MCHBAR32(INTRDIRCTL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500150 reg32 |= (1 << 4) | (1 << 5);
Angel Pons1db5bc72020-01-15 00:49:03 +0100151 MCHBAR32(INTRDIRCTL) = reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500152}
153
Matt DeVilliera51e3792018-03-04 01:44:15 -0600154static void haswell_setup_iommu(void)
155{
Angel Pons1db5bc72020-01-15 00:49:03 +0100156 const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600157
158 if (capid0_a & VTD_DISABLE)
159 return;
160
Angel Pons1db5bc72020-01-15 00:49:03 +0100161 /* Setup BARs: zeroize top 32 bits; set enable bit */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600162 MCHBAR32(GFXVTBAR + 4) = GFXVT_BASE_ADDRESS >> 32;
Angel Pons1db5bc72020-01-15 00:49:03 +0100163 MCHBAR32(GFXVTBAR) = GFXVT_BASE_ADDRESS | 1;
Matt DeVilliera51e3792018-03-04 01:44:15 -0600164 MCHBAR32(VTVC0BAR + 4) = VTVC0_BASE_ADDRESS >> 32;
Angel Pons1db5bc72020-01-15 00:49:03 +0100165 MCHBAR32(VTVC0BAR) = VTVC0_BASE_ADDRESS | 1;
Matt DeVilliera51e3792018-03-04 01:44:15 -0600166
Angel Pons1db5bc72020-01-15 00:49:03 +0100167 /* Set L3HIT2PEND_DIS, lock GFXVTBAR policy config registers */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600168 u32 reg32;
169 reg32 = read32((void *)(GFXVT_BASE_ADDRESS + ARCHDIS));
Angel Pons1db5bc72020-01-15 00:49:03 +0100170 write32((void *)(GFXVT_BASE_ADDRESS + ARCHDIS), reg32 | DMAR_LCKDN | L3HIT2PEND_DIS);
171
172 /* Clear SPCAPCTRL */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600173 reg32 = read32((void *)(VTVC0_BASE_ADDRESS + ARCHDIS)) & ~SPCAPCTRL;
Angel Pons1db5bc72020-01-15 00:49:03 +0100174
175 /* Set GLBIOTLBINV, GLBCTXTINV; lock VTVC0BAR policy config registers */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600176 write32((void *)(VTVC0_BASE_ADDRESS + ARCHDIS),
177 reg32 | DMAR_LCKDN | GLBIOTLBINV | GLBCTXTINV);
178}
179
Angel Ponse8168292020-07-03 11:42:22 +0200180void haswell_early_initialization(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500181{
Aaron Durbin76c37002012-10-30 09:03:43 -0500182 /* Setup all BARs required for early PCIe and raminit */
183 haswell_setup_bars();
184
Matt DeVilliera51e3792018-03-04 01:44:15 -0600185 /* Setup IOMMU BARs */
186 haswell_setup_iommu();
187
Tristan Corrick334be322018-12-17 22:10:21 +1300188 haswell_setup_peg();
189 haswell_setup_igd();
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300190
191 haswell_setup_misc();
Aaron Durbin76c37002012-10-30 09:03:43 -0500192}