blob: 17ffff225d24c4bd4b41773c2533c57101e339f6 [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
Tristan Corrickbc896cd2018-12-17 22:09:50 +13003#include <commonlib/helpers.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05004#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <delay.h>
7#include <cpu/intel/haswell/haswell.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05008#include <device/device.h>
9#include <device/pci.h>
10#include <device/pci_ids.h>
Tristan Corrickbc896cd2018-12-17 22:09:50 +130011#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050012#include <boot/tables.h>
Angel Pons4b290b72020-09-24 23:38:53 +020013#include <security/intel/txt/txt_register.h>
Angel Ponse2ec60f2021-01-26 19:18:09 +010014#include <southbridge/intel/lynxpoint/pch.h>
Elyes HAOUAS030d3382021-02-12 08:17:35 +010015#include <types.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010016
Aaron Durbin76c37002012-10-30 09:03:43 -050017#include "chip.h"
18#include "haswell.h"
19
Tristan Corrickf3127d42018-10-31 02:25:54 +130020static const char *northbridge_acpi_name(const struct device *dev)
21{
22 if (dev->path.type == DEVICE_PATH_DOMAIN)
23 return "PCI0";
24
Fabio Aiuto61ed4ef2022-09-30 14:55:53 +020025 if (!is_pci_dev_on_bus(dev, 0))
Tristan Corrickf3127d42018-10-31 02:25:54 +130026 return NULL;
27
28 switch (dev->path.pci.devfn) {
29 case PCI_DEVFN(0, 0):
30 return "MCHC";
31 }
32
33 return NULL;
34}
35
Arthur Heymans600fa262022-11-07 08:04:59 +010036struct device_operations haswell_pci_domain_ops = {
Angel Pons1db5bc72020-01-15 00:49:03 +010037 .read_resources = pci_domain_read_resources,
38 .set_resources = pci_domain_set_resources,
Angel Pons1db5bc72020-01-15 00:49:03 +010039 .scan_bus = pci_domain_scan_bus,
40 .acpi_name = northbridge_acpi_name,
Matt DeVillier85d98d92018-03-04 01:41:23 -060041 .write_acpi_tables = northbridge_write_acpi_tables,
Aaron Durbin76c37002012-10-30 09:03:43 -050042};
43
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +020044static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050045{
Angel Pons1db5bc72020-01-15 00:49:03 +010046 u32 bar = pci_read_config32(dev, index);
Aaron Durbin76c37002012-10-30 09:03:43 -050047
Angel Pons1db5bc72020-01-15 00:49:03 +010048 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -060049 if (!(bar & 0x1))
50 return 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050051
Angel Pons1db5bc72020-01-15 00:49:03 +010052 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -060053 *base = bar & ~1;
54
55 return 1;
Aaron Durbin76c37002012-10-30 09:03:43 -050056}
57
Angel Pons1db5bc72020-01-15 00:49:03 +010058/*
59 * There are special BARs that actually are programmed in the MCHBAR. These Intel special
60 * features, but they do consume resources that need to be accounted for.
61 */
62static int get_bar_in_mchbar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050063{
Angel Pons2e397ae2021-03-26 12:35:57 +010064 u32 bar = mchbar_read32(index);
Aaron Durbin76c37002012-10-30 09:03:43 -050065
Angel Pons1db5bc72020-01-15 00:49:03 +010066 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -060067 if (!(bar & 0x1))
68 return 0;
69
Angel Pons1db5bc72020-01-15 00:49:03 +010070 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -060071 *base = bar & ~1;
72
73 return 1;
74}
75
76struct fixed_mmio_descriptor {
77 unsigned int index;
78 u32 size;
Angel Pons1db5bc72020-01-15 00:49:03 +010079 int (*get_resource)(struct device *dev, unsigned int index, u32 *base, u32 *size);
Aaron Durbinc12ef972012-12-18 14:22:49 -060080 const char *description;
81};
82
Aaron Durbinc12ef972012-12-18 14:22:49 -060083struct fixed_mmio_descriptor mc_fixed_resources[] = {
Angel Pons3eeefba2021-06-14 09:23:40 +020084 { MCHBAR, MCH_BASE_SIZE, get_bar, "MCHBAR" },
85 { DMIBAR, DMI_BASE_SIZE, get_bar, "DMIBAR" },
86 { EPBAR, EP_BASE_SIZE, get_bar, "EPBAR" },
87 { GDXCBAR, GDXC_BASE_SIZE, get_bar_in_mchbar, "GDXCBAR" },
88 { EDRAMBAR, EDRAM_BASE_SIZE, get_bar_in_mchbar, "EDRAMBAR" },
Aaron Durbinc12ef972012-12-18 14:22:49 -060089};
Aaron Durbinc12ef972012-12-18 14:22:49 -060090
Angel Pons1db5bc72020-01-15 00:49:03 +010091/* Add all known fixed MMIO ranges that hang off the host bridge/memory controller device. */
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +020092static void mc_add_fixed_mmio_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -060093{
94 int i;
95
96 for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
97 u32 base;
98 u32 size;
99 struct resource *resource;
100 unsigned int index;
101
102 size = mc_fixed_resources[i].size;
103 index = mc_fixed_resources[i].index;
Angel Pons1db5bc72020-01-15 00:49:03 +0100104 if (!mc_fixed_resources[i].get_resource(dev, index, &base, &size))
Aaron Durbinc12ef972012-12-18 14:22:49 -0600105 continue;
106
107 resource = new_resource(dev, mc_fixed_resources[i].index);
Kyösti Mälkki4e4edf72022-05-26 19:03:55 +0300108 resource->base = base;
109 resource->size = size;
Angel Pons1db5bc72020-01-15 00:49:03 +0100110 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
111 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
112
Aaron Durbinc12ef972012-12-18 14:22:49 -0600113 printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
114 __func__, mc_fixed_resources[i].description, index,
115 (unsigned long)base, (unsigned long)(base + size - 1));
116 }
Angel Pons32770f82021-01-20 15:03:30 +0100117
118 mmconf_resource(dev, PCIEXBAR);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600119}
120
Angel Pons4b290b72020-09-24 23:38:53 +0200121/*
122 * Host Memory Map:
Aaron Durbinc12ef972012-12-18 14:22:49 -0600123 *
124 * +--------------------------+ TOUUD
125 * | |
126 * +--------------------------+ 4GiB
127 * | PCI Address Space |
128 * +--------------------------+ TOLUD (also maps into MC address space)
129 * | iGD |
130 * +--------------------------+ BDSM
131 * | GTT |
132 * +--------------------------+ BGSM
133 * | TSEG |
134 * +--------------------------+ TSEGMB
Angel Pons4b290b72020-09-24 23:38:53 +0200135 * | DPR |
136 * +--------------------------+ (DPR top - DPR size)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600137 * | Usage DRAM |
138 * +--------------------------+ 0
139 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100140 * Some of the base registers above can be equal, making the size of the regions within 0.
141 * This is because the memory controller internally subtracts the base registers from each
142 * other to determine sizes of the regions. In other words, the memory map regions are always
143 * in a fixed order, no matter what sizes they have.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600144 */
145
146struct map_entry {
147 int reg;
148 int is_64_bit;
149 int is_limit;
150 const char *description;
151};
152
Angel Pons1db5bc72020-01-15 00:49:03 +0100153static void read_map_entry(struct device *dev, struct map_entry *entry, uint64_t *result)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600154{
155 uint64_t value;
156 uint64_t mask;
157
Angel Pons1db5bc72020-01-15 00:49:03 +0100158 /* All registers have a 1MiB granularity */
159 mask = ((1ULL << 20) - 1);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600160 mask = ~mask;
161
162 value = 0;
163
164 if (entry->is_64_bit) {
165 value = pci_read_config32(dev, entry->reg + 4);
166 value <<= 32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500167 }
168
Aaron Durbinc12ef972012-12-18 14:22:49 -0600169 value |= pci_read_config32(dev, entry->reg);
170 value &= mask;
171
172 if (entry->is_limit)
173 value |= ~mask;
174
175 *result = value;
176}
177
178#define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
179 { \
180 .reg = reg_, \
181 .is_64_bit = is_64_, \
182 .is_limit = is_limit_, \
183 .description = desc_, \
184 }
185
Angel Pons1db5bc72020-01-15 00:49:03 +0100186#define MAP_ENTRY_BASE_32(reg_, desc_) MAP_ENTRY(reg_, 0, 0, desc_)
187#define MAP_ENTRY_BASE_64(reg_, desc_) MAP_ENTRY(reg_, 1, 0, desc_)
188#define MAP_ENTRY_LIMIT_64(reg_, desc_) MAP_ENTRY(reg_, 1, 1, desc_)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600189
190enum {
191 TOM_REG,
192 TOUUD_REG,
193 MESEG_BASE_REG,
194 MESEG_LIMIT_REG,
195 REMAP_BASE_REG,
196 REMAP_LIMIT_REG,
197 TOLUD_REG,
198 BGSM_REG,
199 BDSM_REG,
200 TSEG_REG,
Angel Pons1db5bc72020-01-15 00:49:03 +0100201 /* Must be last */
202 NUM_MAP_ENTRIES,
Aaron Durbinc12ef972012-12-18 14:22:49 -0600203};
204
205static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
Angel Pons1db5bc72020-01-15 00:49:03 +0100206 [TOM_REG] = MAP_ENTRY_BASE_64(TOM, "TOM"),
207 [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
208 [MESEG_BASE_REG] = MAP_ENTRY_BASE_64(MESEG_BASE, "MESEG_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600209 [MESEG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT, "MESEG_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100210 [REMAP_BASE_REG] = MAP_ENTRY_BASE_64(REMAPBASE, "REMAP_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600211 [REMAP_LIMIT_REG] = MAP_ENTRY_LIMIT_64(REMAPLIMIT, "REMAP_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100212 [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
213 [BDSM_REG] = MAP_ENTRY_BASE_32(BDSM, "BDSM"),
214 [BGSM_REG] = MAP_ENTRY_BASE_32(BGSM, "BGSM"),
Angel Ponsd8abb262020-05-07 00:48:35 +0200215 [TSEG_REG] = MAP_ENTRY_BASE_32(TSEG, "TSEGMB"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600216};
217
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200218static void mc_read_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600219{
220 int i;
221 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
222 read_map_entry(dev, &memory_map[i], &values[i]);
223 }
224}
225
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200226static void mc_report_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600227{
228 int i;
229 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
230 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
231 memory_map[i].description, values[i]);
232 }
Angel Pons1db5bc72020-01-15 00:49:03 +0100233 /* One can validate the BDSM and BGSM against the GGC */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600234 printk(BIOS_DEBUG, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev, GGC));
235}
236
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200237static void mc_add_dram_resources(struct device *dev, int *resource_cnt)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600238{
Kyösti Mälkki0a18d642021-06-28 21:43:31 +0300239 unsigned long base_k, size_k, index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600240 struct resource *resource;
241 uint64_t mc_values[NUM_MAP_ENTRIES];
242
Angel Pons1db5bc72020-01-15 00:49:03 +0100243 /* Read in the MAP registers and report their values */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600244 mc_read_map_entries(dev, &mc_values[0]);
245 mc_report_map_entries(dev, &mc_values[0]);
246
Angel Pons5d7c3a42020-10-29 21:18:14 +0100247 /*
248 * DMA Protected Range can be reserved below TSEG for PCODE patch
Paul Menzel7f5a1ee2021-12-15 10:47:05 +0100249 * or TXT/Boot Guard related data. Rather than report a base address,
Angel Pons5d7c3a42020-10-29 21:18:14 +0100250 * the DPR register reports the TOP of the region, which is the same
251 * as TSEG base. The region size is reported in MiB in bits 11:4.
252 */
Angel Pons4b290b72020-09-24 23:38:53 +0200253 const union dpr_register dpr = {
254 .raw = pci_read_config32(dev, DPR),
255 };
256 printk(BIOS_DEBUG, "MC MAP: DPR: 0x%x\n", dpr.raw);
257
Aaron Durbinc12ef972012-12-18 14:22:49 -0600258 /*
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600259 * These are the host memory ranges that should be added:
Angel Pons1db5bc72020-01-15 00:49:03 +0100260 * - 0 -> 0xa0000: cacheable
261 * - 0xc0000 -> TSEG: cacheable
262 * - TSEG -> BGSM: cacheable with standard MTRRs and reserved
263 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
264 * - 4GiB -> TOUUD: cacheable
Aaron Durbinc12ef972012-12-18 14:22:49 -0600265 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100266 * The default SMRAM space is reserved so that the range doesn't have to be saved
267 * during S3 Resume. Once marked reserved the OS cannot use the memory. This is a
268 * bit of an odd place to reserve the region, but the CPU devices don't have
269 * dev_ops->read_resources() called on them.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600270 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100271 * The range 0xa0000 -> 0xc0000 does not have any resources associated with it to
272 * handle legacy VGA memory. If this range is not omitted the mtrr code will setup
273 * the area as cacheable, causing VGA access to not work.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600274 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100275 * The TSEG region is mapped as cacheable so that one can perform SMRAM relocation
276 * faster. Once the SMRR is enabled, the SMRR takes precedence over the existing
277 * MTRRs covering this region.
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600278 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100279 * It should be noted that cacheable entry types need to be added in order. The reason
280 * is that the current MTRR code assumes this and falls over itself if it isn't.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600281 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100282 * The resource index starts low and should not meet or exceed PCI_BASE_ADDRESS_0.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600283 */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600284 index = *resource_cnt;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600285
Aaron Durbin6a360042014-02-13 10:30:42 -0600286 /* 0 - > 0xa0000 */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600287 base_k = 0;
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600288 size_k = (0xa0000 >> 10) - base_k;
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300289 ram_resource_kb(dev, index++, base_k, size_k);
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600290
Angel Pons5d7c3a42020-10-29 21:18:14 +0100291 /* 0xc0000 -> TSEG - DPR */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600292 base_k = 0xc0000 >> 10;
Angel Pons5d7c3a42020-10-29 21:18:14 +0100293 size_k = (unsigned long)(mc_values[TSEG_REG] >> 10) - base_k;
Tim Wawrzynczaka8f76902021-02-26 09:32:15 -0700294 size_k -= dpr.size * MiB / KiB;
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300295 ram_resource_kb(dev, index++, base_k, size_k);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600296
Angel Pons5d7c3a42020-10-29 21:18:14 +0100297 /* TSEG - DPR -> BGSM */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600298 resource = new_resource(dev, index++);
Tim Wawrzynczaka8f76902021-02-26 09:32:15 -0700299 resource->base = mc_values[TSEG_REG] - dpr.size * MiB;
Kyösti Mälkki4e4edf72022-05-26 19:03:55 +0300300 resource->size = mc_values[BGSM_REG] - (mc_values[TSEG_REG] - dpr.size * MiB);
Angel Pons1db5bc72020-01-15 00:49:03 +0100301 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
302 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE;
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600303
Angel Pons1db5bc72020-01-15 00:49:03 +0100304 /* BGSM -> TOLUD. If the IGD is disabled, BGSM can equal TOLUD. */
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300305 if (mc_values[BGSM_REG] != mc_values[TOLUD_REG]) {
306 resource = new_resource(dev, index++);
307 resource->base = mc_values[BGSM_REG];
Kyösti Mälkki4e4edf72022-05-26 19:03:55 +0300308 resource->size = mc_values[TOLUD_REG] - mc_values[BGSM_REG];
Angel Pons1db5bc72020-01-15 00:49:03 +0100309 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
310 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300311 }
Aaron Durbinc12ef972012-12-18 14:22:49 -0600312
313 /* 4GiB -> TOUUD */
Kyösti Mälkki0a18d642021-06-28 21:43:31 +0300314 upper_ram_end(dev, index++, mc_values[TOUUD_REG]);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600315
Aaron Durbinc9650762013-03-22 22:03:09 -0500316 /* Reserve everything between A segment and 1MB:
317 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100318 * 0xa0000 - 0xbffff: Legacy VGA
Aaron Durbinc9650762013-03-22 22:03:09 -0500319 * 0xc0000 - 0xfffff: RAM
320 */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300321 mmio_resource_kb(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
322 reserved_ram_resource_kb(dev, index++, (0xc0000 >> 10), (0x100000 - 0xc0000) >> 10);
Angel Pons1db5bc72020-01-15 00:49:03 +0100323
Matt DeVilliera51e3792018-03-04 01:44:15 -0600324 *resource_cnt = index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600325}
326
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200327static void mc_read_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600328{
Matt DeVilliera51e3792018-03-04 01:44:15 -0600329 int index = 0;
Angel Pons1db5bc72020-01-15 00:49:03 +0100330 const bool vtd_capable = !(pci_read_config32(dev, CAPID0_A) & VTD_DISABLE);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600331
Angel Pons1db5bc72020-01-15 00:49:03 +0100332 /* Read standard PCI resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600333 pci_dev_read_resources(dev);
334
Angel Pons1db5bc72020-01-15 00:49:03 +0100335 /* Add all fixed MMIO resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600336 mc_add_fixed_mmio_resources(dev);
337
Angel Pons1db5bc72020-01-15 00:49:03 +0100338 /* Add VT-d MMIO resources, if capable */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600339 if (vtd_capable) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300340 mmio_resource_kb(dev, index++, GFXVT_BASE_ADDRESS / KiB, GFXVT_BASE_SIZE / KiB);
341 mmio_resource_kb(dev, index++, VTVC0_BASE_ADDRESS / KiB, VTVC0_BASE_SIZE / KiB);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600342 }
343
Angel Pons1db5bc72020-01-15 00:49:03 +0100344 /* Calculate and add DRAM resources */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600345 mc_add_dram_resources(dev, &index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500346}
347
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300348/*
Angel Pons1db5bc72020-01-15 00:49:03 +0100349 * The Mini-HD audio device is disabled whenever the IGD is. This is because it provides
350 * audio over the integrated graphics port(s), which requires the IGD to be functional.
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300351 */
352static void disable_devices(void)
353{
354 static const struct {
355 const unsigned int devfn;
356 const u32 mask;
357 const char *const name;
358 } nb_devs[] = {
359 { PCI_DEVFN(1, 2), DEVEN_D1F2EN, "PEG12" },
360 { PCI_DEVFN(1, 1), DEVEN_D1F1EN, "PEG11" },
361 { PCI_DEVFN(1, 0), DEVEN_D1F0EN, "PEG10" },
362 { PCI_DEVFN(2, 0), DEVEN_D2EN | DEVEN_D3EN, "IGD" },
363 { PCI_DEVFN(3, 0), DEVEN_D3EN, "Mini-HD audio" },
364 { PCI_DEVFN(4, 0), DEVEN_D4EN, "\"device 4\"" },
365 { PCI_DEVFN(7, 0), DEVEN_D7EN, "\"device 7\"" },
366 };
367
Angel Pons1db5bc72020-01-15 00:49:03 +0100368 struct device *host_dev = pcidev_on_root(0, 0);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300369 u32 deven;
370 size_t i;
371
372 if (!host_dev)
373 return;
374
375 deven = pci_read_config32(host_dev, DEVEN);
376
377 for (i = 0; i < ARRAY_SIZE(nb_devs); i++) {
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300378 struct device *dev = pcidev_path_on_root(nb_devs[i].devfn);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300379 if (!dev || !dev->enabled) {
380 printk(BIOS_DEBUG, "Disabling %s.\n", nb_devs[i].name);
381 deven &= ~nb_devs[i].mask;
382 }
383 }
384
385 pci_write_config32(host_dev, DEVEN, deven);
386}
387
Angel Pons028b8e42020-07-24 14:03:29 +0200388static void init_egress(void)
389{
390 /* VC0: Enable, ID0, TC0 */
Angel Pons2e397ae2021-03-26 12:35:57 +0100391 epbar_write32(EPVC0RCTL, 1 << 31 | 0 << 24 | 1 << 0);
Angel Pons028b8e42020-07-24 14:03:29 +0200392
393 /* No Low Priority Extended VCs, one Extended VC */
Angel Pons2e397ae2021-03-26 12:35:57 +0100394 epbar_write32(EPPVCCAP1, 0 << 4 | 1 << 0);
Angel Pons028b8e42020-07-24 14:03:29 +0200395
396 /* VC1: Enable, ID1, TC1 */
Angel Pons2e397ae2021-03-26 12:35:57 +0100397 epbar_write32(EPVC1RCTL, 1 << 31 | 1 << 24 | 1 << 1);
Angel Pons028b8e42020-07-24 14:03:29 +0200398
399 /* Poll the VC1 Negotiation Pending bit */
Angel Pons2e397ae2021-03-26 12:35:57 +0100400 while ((epbar_read16(EPVC1RSTS) & (1 << 1)) != 0)
Angel Pons028b8e42020-07-24 14:03:29 +0200401 ;
402}
403
Angel Pons598ec6a2020-07-23 02:37:12 +0200404static void northbridge_dmi_init(void)
405{
406 const bool is_haswell_h = !CONFIG(INTEL_LYNXPOINT_LP);
407
Angel Pons598ec6a2020-07-23 02:37:12 +0200408 /* Steps prior to DMI ASPM */
409 if (is_haswell_h) {
410 /* Configure DMI De-Emphasis */
Angel Pons2e397ae2021-03-26 12:35:57 +0100411 dmibar_setbits16(DMILCTL2, 1 << 6); /* 0b: -6.0 dB, 1b: -3.5 dB */
Angel Pons598ec6a2020-07-23 02:37:12 +0200412
Angel Pons2e397ae2021-03-26 12:35:57 +0100413 dmibar_setbits32(DMIL0SLAT, 1 << 31);
414 dmibar_setbits32(DMILLTC, 1 << 29);
Angel Pons598ec6a2020-07-23 02:37:12 +0200415
Angel Pons2e397ae2021-03-26 12:35:57 +0100416 dmibar_clrsetbits32(DMI_AFE_PM_TMR, 0x1f, 0x13);
Angel Pons598ec6a2020-07-23 02:37:12 +0200417 }
418
419 /* Clear error status bits */
Angel Pons2e397ae2021-03-26 12:35:57 +0100420 dmibar_write32(DMIUESTS, 0xffffffff);
421 dmibar_write32(DMICESTS, 0xffffffff);
Angel Pons598ec6a2020-07-23 02:37:12 +0200422
423 if (is_haswell_h) {
424 /* Enable ASPM L0s and L1 on SA link, should happen before PCH link */
Angel Pons2e397ae2021-03-26 12:35:57 +0100425 dmibar_setbits16(DMILCTL, 1 << 1 | 1 << 0);
Angel Pons598ec6a2020-07-23 02:37:12 +0200426 }
427}
428
Angel Pons76b8bc22020-07-23 02:32:27 +0200429static void northbridge_topology_init(void)
430{
431 const u32 eple_a[3] = { EPLE2A, EPLE3A, EPLE4A };
432 const u32 eple_d[3] = { EPLE2D, EPLE3D, EPLE4D };
433
Angel Pons76b8bc22020-07-23 02:32:27 +0200434 /* Set the CID1 Egress Port 0 Root Topology */
Angel Pons2e397ae2021-03-26 12:35:57 +0100435 epbar_clrsetbits32(EPESD, 0xff << 16, 1 << 16);
Angel Pons76b8bc22020-07-23 02:32:27 +0200436
Angel Pons0acfe222021-03-26 13:08:23 +0100437 epbar_clrsetbits32(EPLE1D, 0xff << 16, 1 | 1 << 16);
Angel Pons2e397ae2021-03-26 12:35:57 +0100438 epbar_write32(EPLE1A, CONFIG_FIXED_DMIBAR_MMIO_BASE);
439 epbar_write32(EPLE1A + 4, 0);
Angel Pons76b8bc22020-07-23 02:32:27 +0200440
441 for (unsigned int i = 0; i <= 2; i++) {
442 const struct device *const dev = pcidev_on_root(1, i);
443
444 if (!dev || !dev->enabled)
445 continue;
446
Angel Pons2e397ae2021-03-26 12:35:57 +0100447 epbar_write32(eple_a[i], (u32)PCI_DEV(0, 1, i));
448 epbar_write32(eple_a[i] + 4, 0);
Angel Pons76b8bc22020-07-23 02:32:27 +0200449
Angel Pons0acfe222021-03-26 13:08:23 +0100450 epbar_clrsetbits32(eple_d[i], 0xff << 16, 1 | 1 << 16);
Angel Pons76b8bc22020-07-23 02:32:27 +0200451
452 pci_update_config32(dev, PEG_ESD, ~(0xff << 16), (1 << 16));
Angel Ponsf95b9b42021-01-20 01:10:48 +0100453 pci_write_config32(dev, PEG_LE1A, CONFIG_FIXED_EPBAR_MMIO_BASE);
Angel Pons76b8bc22020-07-23 02:32:27 +0200454 pci_write_config32(dev, PEG_LE1A + 4, 0);
455 pci_update_config32(dev, PEG_LE1D, ~(0xff << 16), (1 << 16) | 1);
456
457 /* Read and write to lock register */
458 pci_or_config32(dev, PEG_DCAP2, 0);
459 }
460
461 /* Set the CID1 DMI Port Root Topology */
Angel Pons2e397ae2021-03-26 12:35:57 +0100462 dmibar_clrsetbits32(DMIESD, 0xff << 16, 1 << 16);
Angel Pons76b8bc22020-07-23 02:32:27 +0200463
Angel Pons0acfe222021-03-26 13:08:23 +0100464 dmibar_clrsetbits32(DMILE1D, 0xffff << 16, 1 | 2 << 16);
Angel Pons2e397ae2021-03-26 12:35:57 +0100465 dmibar_write32(DMILE1A, CONFIG_FIXED_RCBA_MMIO_BASE);
466 dmibar_write32(DMILE1A + 4, 0);
Angel Pons76b8bc22020-07-23 02:32:27 +0200467
Angel Pons2e397ae2021-03-26 12:35:57 +0100468 dmibar_write32(DMILE2A, CONFIG_FIXED_EPBAR_MMIO_BASE);
469 dmibar_write32(DMILE2A + 4, 0);
Angel Pons0acfe222021-03-26 13:08:23 +0100470 dmibar_clrsetbits32(DMILE2D, 0xff << 16, 1 | 1 << 16);
Angel Pons76b8bc22020-07-23 02:32:27 +0200471
472 /* Program RO and Write-Once Registers */
Angel Pons2e397ae2021-03-26 12:35:57 +0100473 dmibar_setbits32(DMIPVCCAP1, 0);
474 dmibar_setbits32(DMILCAP, 0);
Angel Pons76b8bc22020-07-23 02:32:27 +0200475}
476
Aaron Durbin76c37002012-10-30 09:03:43 -0500477static void northbridge_init(struct device *dev)
478{
Angel Pons028b8e42020-07-24 14:03:29 +0200479 init_egress();
Angel Pons598ec6a2020-07-23 02:37:12 +0200480 northbridge_dmi_init();
Angel Pons76b8bc22020-07-23 02:32:27 +0200481 northbridge_topology_init();
Angel Pons598ec6a2020-07-23 02:37:12 +0200482
Angel Pons1db5bc72020-01-15 00:49:03 +0100483 /* Enable Power Aware Interrupt Routing. */
Angel Pons2e397ae2021-03-26 12:35:57 +0100484 mchbar_clrsetbits8(INTRDIRCTL, 0x7, 0x4); /* Clear 2:0, set Fixed Priority */
Aaron Durbin76c37002012-10-30 09:03:43 -0500485
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300486 disable_devices();
487
Aaron Durbin76c37002012-10-30 09:03:43 -0500488 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100489 * Set bits 0 + 1 of BIOS_RESET_CPL to indicate to the CPU
490 * that BIOS has initialized memory and power management.
Aaron Durbin76c37002012-10-30 09:03:43 -0500491 */
Angel Pons2e397ae2021-03-26 12:35:57 +0100492 mchbar_setbits8(BIOS_RESET_CPL, 3);
Aaron Durbin76c37002012-10-30 09:03:43 -0500493 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
494
Angel Pons1db5bc72020-01-15 00:49:03 +0100495 /* Configure turbo power limits 1ms after reset complete bit. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500496 mdelay(1);
497 set_power_limits(28);
Aaron Durbin76c37002012-10-30 09:03:43 -0500498}
499
Angel Pons34be1be2021-03-01 22:57:21 +0100500static void northbridge_final(struct device *dev)
501{
502 pci_or_config16(dev, GGC, 1 << 0);
503 pci_or_config32(dev, DPR, 1 << 0);
504 pci_or_config32(dev, MESEG_LIMIT, 1 << 10);
505 pci_or_config32(dev, REMAPBASE, 1 << 0);
506 pci_or_config32(dev, REMAPLIMIT, 1 << 0);
507 pci_or_config32(dev, TOM, 1 << 0);
508 pci_or_config32(dev, TOUUD, 1 << 0);
509 pci_or_config32(dev, BDSM, 1 << 0);
510 pci_or_config32(dev, BGSM, 1 << 0);
511 pci_or_config32(dev, TSEG, 1 << 0);
512 pci_or_config32(dev, TOLUD, 1 << 0);
513
514 /* Memory Controller Lockdown */
Angel Pons2e397ae2021-03-26 12:35:57 +0100515 mchbar_setbits32(MC_LOCK, 0x8f);
Angel Pons34be1be2021-03-01 22:57:21 +0100516
Angel Pons2e397ae2021-03-26 12:35:57 +0100517 mchbar_setbits32(MMIO_PAVP_MSG, 1 << 0); /* PAVP */
518 mchbar_setbits32(PCU_DDR_PTM_CTL, 1 << 5); /* DDR PTM */
519 mchbar_setbits32(DMIVCLIM, 1 << 31);
520 mchbar_setbits32(CRDTLCK, 1 << 0);
521 mchbar_setbits32(MCARBLCK, 1 << 0);
522 mchbar_setbits32(REQLIM, 1 << 31);
523 mchbar_setbits32(UMAGFXCTL, 1 << 0); /* UMA GFX */
524 mchbar_setbits32(VTDTRKLCK, 1 << 0); /* VTDTRK */
Angel Pons34be1be2021-03-01 22:57:21 +0100525
526 /* Read+write the following */
Angel Pons2e397ae2021-03-26 12:35:57 +0100527 mchbar_setbits32(VDMBDFBARKVM, 0);
528 mchbar_setbits32(VDMBDFBARPAVP, 0);
529 mchbar_setbits32(HDAUDRID, 0);
Angel Pons34be1be2021-03-01 22:57:21 +0100530}
531
Aaron Durbin76c37002012-10-30 09:03:43 -0500532static struct device_operations mc_ops = {
Angel Pons30c5e602021-03-01 22:59:00 +0100533 .read_resources = mc_read_resources,
534 .set_resources = pci_dev_set_resources,
535 .enable_resources = pci_dev_enable_resources,
536 .init = northbridge_init,
Angel Pons34be1be2021-03-01 22:57:21 +0100537 .final = northbridge_final,
Angel Pons30c5e602021-03-01 22:59:00 +0100538 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500539};
540
Tristan Corrickd3856242018-11-01 03:03:29 +1300541static const unsigned short mc_pci_device_ids[] = {
542 0x0c00, /* Desktop */
543 0x0c04, /* Mobile */
544 0x0a04, /* ULT */
Iru Cai0766c982018-12-17 13:21:36 +0800545 0x0c08, /* Server */
Iru Cai12a13e12020-05-22 22:57:03 +0800546 0x0d00, /* Crystal Well Desktop */
547 0x0d04, /* Crystal Well Mobile */
548 0x0d08, /* Crystal Well Server (by extrapolation) */
Tristan Corrickd3856242018-11-01 03:03:29 +1300549 0
Tristan Corrick48170122018-10-31 02:21:41 +1300550};
551
Tristan Corrickd3856242018-11-01 03:03:29 +1300552static const struct pci_driver mc_driver_hsw __pci_driver = {
553 .ops = &mc_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100554 .vendor = PCI_VID_INTEL,
Tristan Corrickd3856242018-11-01 03:03:29 +1300555 .devices = mc_pci_device_ids,
Duncan Lauriedf7be712012-12-17 11:22:57 -0800556};
557
Arthur Heymans600fa262022-11-07 08:04:59 +0100558struct device_operations haswell_cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200559 .read_resources = noop_read_resources,
560 .set_resources = noop_set_resources,
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300561 .init = mp_cpu_bus_init,
Arthur Heymansdd96ab62021-11-15 20:11:12 +0100562 .acpi_fill_ssdt = generate_cpu_entries,
Aaron Durbin76c37002012-10-30 09:03:43 -0500563};
564
Aaron Durbin76c37002012-10-30 09:03:43 -0500565struct chip_operations northbridge_intel_haswell_ops = {
Angel Pons7bbf45e2020-10-22 23:55:24 +0200566 CHIP_NAME("Intel Haswell integrated Northbridge")
Aaron Durbin76c37002012-10-30 09:03:43 -0500567};