blob: e47deb5da6a9832120399b88bec12f94c7f3b9f5 [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 Ponsf95b9b42021-01-20 01:10:48 +010017 pci_write_config32(HOST_BRIDGE, EPBAR, CONFIG_FIXED_EPBAR_MMIO_BASE | 1);
18 pci_write_config32(HOST_BRIDGE, EPBAR + 4, 0);
19 pci_write_config32(HOST_BRIDGE, MCHBAR, CONFIG_FIXED_MCHBAR_MMIO_BASE | 1);
20 pci_write_config32(HOST_BRIDGE, MCHBAR + 4, 0);
21 pci_write_config32(HOST_BRIDGE, DMIBAR, CONFIG_FIXED_DMIBAR_MMIO_BASE | 1);
22 pci_write_config32(HOST_BRIDGE, DMIBAR + 4, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -050023
Angel Pons6a2a5142021-06-14 09:39:01 +020024 mchbar_write32(EDRAMBAR, EDRAM_BASE_ADDRESS | 1);
25 mchbar_write32(GDXCBAR, GDXC_BASE_ADDRESS | 1);
26
Aaron Durbin76c37002012-10-30 09:03:43 -050027 /* Set C0000-FFFFF to access RAM on both reads and writes */
Angel Pons1db5bc72020-01-15 00:49:03 +010028 pci_write_config8(HOST_BRIDGE, PAM0, 0x30);
29 pci_write_config8(HOST_BRIDGE, PAM1, 0x33);
30 pci_write_config8(HOST_BRIDGE, PAM2, 0x33);
31 pci_write_config8(HOST_BRIDGE, PAM3, 0x33);
32 pci_write_config8(HOST_BRIDGE, PAM4, 0x33);
33 pci_write_config8(HOST_BRIDGE, PAM5, 0x33);
34 pci_write_config8(HOST_BRIDGE, PAM6, 0x33);
Aaron Durbin76c37002012-10-30 09:03:43 -050035
36 printk(BIOS_DEBUG, " done.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -050037}
38
Tristan Corrick334be322018-12-17 22:10:21 +130039static void haswell_setup_igd(void)
Aaron Durbin76c37002012-10-30 09:03:43 -050040{
Tristan Corrickc5d367b2018-12-17 22:10:07 +130041 bool igd_enabled;
42 u16 ggc;
Aaron Durbin76c37002012-10-30 09:03:43 -050043
Tristan Corrick334be322018-12-17 22:10:21 +130044 printk(BIOS_DEBUG, "Initializing IGD...\n");
Aaron Durbin76c37002012-10-30 09:03:43 -050045
Angel Pons1db5bc72020-01-15 00:49:03 +010046 igd_enabled = !!(pci_read_config32(HOST_BRIDGE, DEVEN) & DEVEN_D2EN);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130047
Angel Pons1db5bc72020-01-15 00:49:03 +010048 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130049 ggc &= ~0x3f8;
50 if (igd_enabled) {
51 ggc |= GGC_GTT_2MB | GGC_IGD_MEM_IN_32MB_UNITS(1);
52 ggc &= ~GGC_DISABLE_VGA_IO_DECODE;
53 } else {
Angel Pons1db5bc72020-01-15 00:49:03 +010054 ggc |= GGC_GTT_0MB | GGC_IGD_MEM_IN_32MB_UNITS(0) | GGC_DISABLE_VGA_IO_DECODE;
Tristan Corrickc5d367b2018-12-17 22:10:07 +130055 }
Angel Pons1db5bc72020-01-15 00:49:03 +010056 pci_write_config16(HOST_BRIDGE, GGC, ggc);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130057
58 if (!igd_enabled) {
59 printk(BIOS_DEBUG, "IGD is disabled.\n");
60 return;
61 }
Aaron Durbin76c37002012-10-30 09:03:43 -050062
63 /* Enable 256MB aperture */
Angel Pons26886072020-06-07 22:19:21 +020064 pci_update_config8(PCI_DEV(0, 2, 0), MSAC, ~0x06, 0x02);
Tristan Corrickc5d367b2018-12-17 22:10:07 +130065}
66
Tristan Corrick334be322018-12-17 22:10:21 +130067static void start_peg2_link_training(const pci_devfn_t dev)
68{
69 u32 mask;
70
71 switch (dev) {
72 case PCI_DEV(0, 1, 2):
73 mask = DEVEN_D1F2EN;
74 break;
75 case PCI_DEV(0, 1, 1):
76 mask = DEVEN_D1F1EN;
77 break;
78 case PCI_DEV(0, 1, 0):
79 mask = DEVEN_D1F0EN;
80 break;
81 default:
82 printk(BIOS_ERR, "Link training tried on a non-PEG device!\n");
83 return;
84 }
85
86 pci_update_config32(dev, 0xc24, ~(1 << 16), 1 << 5);
Chris Morgan2806ec92020-02-05 10:51:46 -060087 printk(BIOS_DEBUG, "Started PEG1%d link training.\n", PCI_FUNC(PCI_DEV2DEVFN(dev)));
Tristan Corrick334be322018-12-17 22:10:21 +130088
89 /*
Angel Pons84641c82020-08-29 02:52:09 +020090 * The MRC will perform PCI enumeration, and if it detects a VGA
91 * device in a PEG slot, it will disable the IGD and not reserve
92 * any memory for it. Since the memory map is locked by the time
93 * MRC finishes, the IGD can't be enabled afterwards. Wonderful.
94 *
95 * If one really wants to enable the Intel iGPU as primary, hide
96 * all PEG devices during MRC execution. This will trick the MRC
97 * into thinking there aren't any, and will enable the IGD. Note
98 * that PEG AFE settings will not be programmed, which may cause
99 * stability problems at higher PCIe link speeds. The most ideal
100 * way to fix this problem for good is to implement native init.
Tristan Corrick334be322018-12-17 22:10:21 +1300101 */
Angel Pons84641c82020-08-29 02:52:09 +0200102 if (CONFIG(HASWELL_HIDE_PEG_FROM_MRC)) {
103 pci_update_config32(HOST_BRIDGE, DEVEN, ~mask, 0);
104 peg_hidden[PCI_FUNC(PCI_DEV2DEVFN(dev))] = true;
105 printk(BIOS_DEBUG, "Temporarily hiding PEG1%d.\n",
106 PCI_FUNC(PCI_DEV2DEVFN(dev)));
107 }
Tristan Corrick334be322018-12-17 22:10:21 +1300108}
109
110void haswell_unhide_peg(void)
111{
Angel Pons1db5bc72020-01-15 00:49:03 +0100112 u32 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Tristan Corrick334be322018-12-17 22:10:21 +1300113
114 for (u8 fn = 0; fn <= 2; fn++) {
115 if (peg_hidden[fn]) {
116 deven |= DEVEN_D1F0EN >> fn;
117 peg_hidden[fn] = false;
118 printk(BIOS_DEBUG, "Unhiding PEG1%d.\n", fn);
119 }
120 }
121
Angel Pons1db5bc72020-01-15 00:49:03 +0100122 pci_write_config32(HOST_BRIDGE, DEVEN, deven);
Tristan Corrick334be322018-12-17 22:10:21 +1300123}
124
125static void haswell_setup_peg(void)
126{
Angel Pons1db5bc72020-01-15 00:49:03 +0100127 u32 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Tristan Corrick334be322018-12-17 22:10:21 +1300128
129 if (deven & DEVEN_D1F2EN)
130 start_peg2_link_training(PCI_DEV(0, 1, 2));
Angel Pons1db5bc72020-01-15 00:49:03 +0100131
Tristan Corrick334be322018-12-17 22:10:21 +1300132 if (deven & DEVEN_D1F1EN)
133 start_peg2_link_training(PCI_DEV(0, 1, 1));
Angel Pons1db5bc72020-01-15 00:49:03 +0100134
Tristan Corrick334be322018-12-17 22:10:21 +1300135 if (deven & DEVEN_D1F0EN)
136 start_peg2_link_training(PCI_DEV(0, 1, 0));
137}
138
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300139static void haswell_setup_misc(void)
140{
141 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500142
143 /* Erratum workarounds */
Angel Pons2e397ae2021-03-26 12:35:57 +0100144 reg32 = mchbar_read32(SAPMCTL);
Angel Pons1db5bc72020-01-15 00:49:03 +0100145 reg32 |= (1 << 9) | (1 << 10);
Angel Pons2e397ae2021-03-26 12:35:57 +0100146 mchbar_write32(SAPMCTL, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500147
148 /* Enable SA Clock Gating */
Angel Pons2e397ae2021-03-26 12:35:57 +0100149 reg32 = mchbar_read32(SAPMCTL);
150 mchbar_write32(SAPMCTL, reg32 | 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500151
Angel Pons2e397ae2021-03-26 12:35:57 +0100152 reg32 = mchbar_read32(INTRDIRCTL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500153 reg32 |= (1 << 4) | (1 << 5);
Angel Pons2e397ae2021-03-26 12:35:57 +0100154 mchbar_write32(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 */
Angel Pons2e397ae2021-03-26 12:35:57 +0100165 mchbar_write32(GFXVTBAR + 4, GFXVT_BASE_ADDRESS >> 32);
166 mchbar_write32(GFXVTBAR + 0, GFXVT_BASE_ADDRESS | 1);
167 mchbar_write32(VTVC0BAR + 4, VTVC0_BASE_ADDRESS >> 32);
168 mchbar_write32(VTVC0BAR + 0, 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;
Elyes Haouas9a83eae2022-12-03 13:31:38 +0100172 reg32 = read32p(GFXVT_BASE_ADDRESS + ARCHDIS);
173 write32p(GFXVT_BASE_ADDRESS + ARCHDIS, reg32 | DMAR_LCKDN | L3HIT2PEND_DIS);
Angel Pons1db5bc72020-01-15 00:49:03 +0100174
175 /* Clear SPCAPCTRL */
Elyes Haouas9a83eae2022-12-03 13:31:38 +0100176 reg32 = read32p(VTVC0_BASE_ADDRESS + ARCHDIS) & ~SPCAPCTRL;
Angel Pons1db5bc72020-01-15 00:49:03 +0100177
178 /* Set GLBIOTLBINV, GLBCTXTINV; lock VTVC0BAR policy config registers */
Elyes Haouas9a83eae2022-12-03 13:31:38 +0100179 write32p(VTVC0_BASE_ADDRESS + ARCHDIS,
Matt DeVilliera51e3792018-03-04 01:44:15 -0600180 reg32 | DMAR_LCKDN | GLBIOTLBINV | GLBCTXTINV);
181}
182
Angel Ponse8168292020-07-03 11:42:22 +0200183void haswell_early_initialization(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500184{
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}