blob: 8af6eb27f02078dcd0b22177437898142eff6f95 [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 <stdint.h>
7#include <delay.h>
8#include <cpu/intel/haswell/haswell.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05009#include <device/device.h>
10#include <device/pci.h>
Tristan Corrickbc896cd2018-12-17 22:09:50 +130011#include <device/pci_def.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050012#include <device/pci_ids.h>
Tristan Corrickbc896cd2018-12-17 22:09:50 +130013#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050014#include <boot/tables.h>
Angel Pons4b290b72020-09-24 23:38:53 +020015#include <security/intel/txt/txt_register.h>
Angel Ponse2ec60f2021-01-26 19:18:09 +010016#include <southbridge/intel/lynxpoint/pch.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010017
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include "chip.h"
19#include "haswell.h"
20
Tristan Corrickf3127d42018-10-31 02:25:54 +130021static const char *northbridge_acpi_name(const struct device *dev)
22{
23 if (dev->path.type == DEVICE_PATH_DOMAIN)
24 return "PCI0";
25
26 if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
27 return NULL;
28
29 switch (dev->path.pci.devfn) {
30 case PCI_DEVFN(0, 0):
31 return "MCHC";
32 }
33
34 return NULL;
35}
36
Angel Pons1db5bc72020-01-15 00:49:03 +010037/*
38 * TODO: We could determine how many PCIe busses we need in the bar.
39 * For now, that number is hardcoded to a max of 64.
40 */
Aaron Durbin76c37002012-10-30 09:03:43 -050041static struct device_operations pci_domain_ops = {
Angel Pons1db5bc72020-01-15 00:49:03 +010042 .read_resources = pci_domain_read_resources,
43 .set_resources = pci_domain_set_resources,
Angel Pons1db5bc72020-01-15 00:49:03 +010044 .scan_bus = pci_domain_scan_bus,
45 .acpi_name = northbridge_acpi_name,
Matt DeVillier85d98d92018-03-04 01:41:23 -060046 .write_acpi_tables = northbridge_write_acpi_tables,
Aaron Durbin76c37002012-10-30 09:03:43 -050047};
48
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +020049static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050050{
Angel Pons1db5bc72020-01-15 00:49:03 +010051 u32 bar = pci_read_config32(dev, index);
Aaron Durbin76c37002012-10-30 09:03:43 -050052
Angel Pons1db5bc72020-01-15 00:49:03 +010053 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -060054 if (!(bar & 0x1))
55 return 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050056
Angel Pons1db5bc72020-01-15 00:49:03 +010057 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -060058 *base = bar & ~1;
59
60 return 1;
Aaron Durbin76c37002012-10-30 09:03:43 -050061}
62
Angel Pons1db5bc72020-01-15 00:49:03 +010063/*
64 * There are special BARs that actually are programmed in the MCHBAR. These Intel special
65 * features, but they do consume resources that need to be accounted for.
66 */
67static int get_bar_in_mchbar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050068{
Angel Pons1db5bc72020-01-15 00:49:03 +010069 u32 bar = MCHBAR32(index);
Aaron Durbin76c37002012-10-30 09:03:43 -050070
Angel Pons1db5bc72020-01-15 00:49:03 +010071 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -060072 if (!(bar & 0x1))
73 return 0;
74
Angel Pons1db5bc72020-01-15 00:49:03 +010075 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -060076 *base = bar & ~1;
77
78 return 1;
79}
80
81struct fixed_mmio_descriptor {
82 unsigned int index;
83 u32 size;
Angel Pons1db5bc72020-01-15 00:49:03 +010084 int (*get_resource)(struct device *dev, unsigned int index, u32 *base, u32 *size);
Aaron Durbinc12ef972012-12-18 14:22:49 -060085 const char *description;
86};
87
Angel Pons1db5bc72020-01-15 00:49:03 +010088#define SIZE_KB(x) ((x) * 1024)
Aaron Durbinc12ef972012-12-18 14:22:49 -060089struct fixed_mmio_descriptor mc_fixed_resources[] = {
Aaron Durbinc12ef972012-12-18 14:22:49 -060090 { MCHBAR, SIZE_KB(32), get_bar, "MCHBAR" },
91 { DMIBAR, SIZE_KB(4), get_bar, "DMIBAR" },
92 { EPBAR, SIZE_KB(4), get_bar, "EPBAR" },
Angel Pons1db5bc72020-01-15 00:49:03 +010093 { GDXCBAR, SIZE_KB(4), get_bar_in_mchbar, "GDXCBAR" },
94 { EDRAMBAR, SIZE_KB(16), get_bar_in_mchbar, "EDRAMBAR" },
Aaron Durbinc12ef972012-12-18 14:22:49 -060095};
96#undef SIZE_KB
97
Angel Pons1db5bc72020-01-15 00:49:03 +010098/* Add all known fixed MMIO ranges that hang off the host bridge/memory controller device. */
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +020099static void mc_add_fixed_mmio_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600100{
101 int i;
102
103 for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
104 u32 base;
105 u32 size;
106 struct resource *resource;
107 unsigned int index;
108
109 size = mc_fixed_resources[i].size;
110 index = mc_fixed_resources[i].index;
Angel Pons1db5bc72020-01-15 00:49:03 +0100111 if (!mc_fixed_resources[i].get_resource(dev, index, &base, &size))
Aaron Durbinc12ef972012-12-18 14:22:49 -0600112 continue;
113
114 resource = new_resource(dev, mc_fixed_resources[i].index);
Angel Pons1db5bc72020-01-15 00:49:03 +0100115 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
116 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
117
Aaron Durbinc12ef972012-12-18 14:22:49 -0600118 resource->base = base;
119 resource->size = size;
120 printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
121 __func__, mc_fixed_resources[i].description, index,
122 (unsigned long)base, (unsigned long)(base + size - 1));
123 }
Angel Pons32770f82021-01-20 15:03:30 +0100124
125 mmconf_resource(dev, PCIEXBAR);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600126}
127
Angel Pons4b290b72020-09-24 23:38:53 +0200128/*
129 * Host Memory Map:
Aaron Durbinc12ef972012-12-18 14:22:49 -0600130 *
131 * +--------------------------+ TOUUD
132 * | |
133 * +--------------------------+ 4GiB
134 * | PCI Address Space |
135 * +--------------------------+ TOLUD (also maps into MC address space)
136 * | iGD |
137 * +--------------------------+ BDSM
138 * | GTT |
139 * +--------------------------+ BGSM
140 * | TSEG |
141 * +--------------------------+ TSEGMB
Angel Pons4b290b72020-09-24 23:38:53 +0200142 * | DPR |
143 * +--------------------------+ (DPR top - DPR size)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600144 * | Usage DRAM |
145 * +--------------------------+ 0
146 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100147 * Some of the base registers above can be equal, making the size of the regions within 0.
148 * This is because the memory controller internally subtracts the base registers from each
149 * other to determine sizes of the regions. In other words, the memory map regions are always
150 * in a fixed order, no matter what sizes they have.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600151 */
152
153struct map_entry {
154 int reg;
155 int is_64_bit;
156 int is_limit;
157 const char *description;
158};
159
Angel Pons1db5bc72020-01-15 00:49:03 +0100160static void read_map_entry(struct device *dev, struct map_entry *entry, uint64_t *result)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600161{
162 uint64_t value;
163 uint64_t mask;
164
Angel Pons1db5bc72020-01-15 00:49:03 +0100165 /* All registers have a 1MiB granularity */
166 mask = ((1ULL << 20) - 1);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600167 mask = ~mask;
168
169 value = 0;
170
171 if (entry->is_64_bit) {
172 value = pci_read_config32(dev, entry->reg + 4);
173 value <<= 32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500174 }
175
Aaron Durbinc12ef972012-12-18 14:22:49 -0600176 value |= pci_read_config32(dev, entry->reg);
177 value &= mask;
178
179 if (entry->is_limit)
180 value |= ~mask;
181
182 *result = value;
183}
184
185#define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
186 { \
187 .reg = reg_, \
188 .is_64_bit = is_64_, \
189 .is_limit = is_limit_, \
190 .description = desc_, \
191 }
192
Angel Pons1db5bc72020-01-15 00:49:03 +0100193#define MAP_ENTRY_BASE_32(reg_, desc_) MAP_ENTRY(reg_, 0, 0, desc_)
194#define MAP_ENTRY_BASE_64(reg_, desc_) MAP_ENTRY(reg_, 1, 0, desc_)
195#define MAP_ENTRY_LIMIT_64(reg_, desc_) MAP_ENTRY(reg_, 1, 1, desc_)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600196
197enum {
198 TOM_REG,
199 TOUUD_REG,
200 MESEG_BASE_REG,
201 MESEG_LIMIT_REG,
202 REMAP_BASE_REG,
203 REMAP_LIMIT_REG,
204 TOLUD_REG,
205 BGSM_REG,
206 BDSM_REG,
207 TSEG_REG,
Angel Pons1db5bc72020-01-15 00:49:03 +0100208 /* Must be last */
209 NUM_MAP_ENTRIES,
Aaron Durbinc12ef972012-12-18 14:22:49 -0600210};
211
212static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
Angel Pons1db5bc72020-01-15 00:49:03 +0100213 [TOM_REG] = MAP_ENTRY_BASE_64(TOM, "TOM"),
214 [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
215 [MESEG_BASE_REG] = MAP_ENTRY_BASE_64(MESEG_BASE, "MESEG_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600216 [MESEG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT, "MESEG_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100217 [REMAP_BASE_REG] = MAP_ENTRY_BASE_64(REMAPBASE, "REMAP_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600218 [REMAP_LIMIT_REG] = MAP_ENTRY_LIMIT_64(REMAPLIMIT, "REMAP_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100219 [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
220 [BDSM_REG] = MAP_ENTRY_BASE_32(BDSM, "BDSM"),
221 [BGSM_REG] = MAP_ENTRY_BASE_32(BGSM, "BGSM"),
Angel Ponsd8abb262020-05-07 00:48:35 +0200222 [TSEG_REG] = MAP_ENTRY_BASE_32(TSEG, "TSEGMB"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600223};
224
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200225static void mc_read_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600226{
227 int i;
228 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
229 read_map_entry(dev, &memory_map[i], &values[i]);
230 }
231}
232
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200233static void mc_report_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600234{
235 int i;
236 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
237 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
238 memory_map[i].description, values[i]);
239 }
Angel Pons1db5bc72020-01-15 00:49:03 +0100240 /* One can validate the BDSM and BGSM against the GGC */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600241 printk(BIOS_DEBUG, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev, GGC));
242}
243
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200244static void mc_add_dram_resources(struct device *dev, int *resource_cnt)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600245{
Angel Pons1db5bc72020-01-15 00:49:03 +0100246 unsigned long base_k, size_k, touud_k, index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600247 struct resource *resource;
248 uint64_t mc_values[NUM_MAP_ENTRIES];
249
Angel Pons1db5bc72020-01-15 00:49:03 +0100250 /* Read in the MAP registers and report their values */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600251 mc_read_map_entries(dev, &mc_values[0]);
252 mc_report_map_entries(dev, &mc_values[0]);
253
Angel Pons4b290b72020-09-24 23:38:53 +0200254 /* The DPR register is special */
255 const union dpr_register dpr = {
256 .raw = pci_read_config32(dev, DPR),
257 };
258 printk(BIOS_DEBUG, "MC MAP: DPR: 0x%x\n", dpr.raw);
259
Aaron Durbinc12ef972012-12-18 14:22:49 -0600260 /*
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600261 * These are the host memory ranges that should be added:
Angel Pons1db5bc72020-01-15 00:49:03 +0100262 * - 0 -> 0xa0000: cacheable
263 * - 0xc0000 -> TSEG: cacheable
264 * - TSEG -> BGSM: cacheable with standard MTRRs and reserved
265 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
266 * - 4GiB -> TOUUD: cacheable
Aaron Durbinc12ef972012-12-18 14:22:49 -0600267 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100268 * The default SMRAM space is reserved so that the range doesn't have to be saved
269 * during S3 Resume. Once marked reserved the OS cannot use the memory. This is a
270 * bit of an odd place to reserve the region, but the CPU devices don't have
271 * dev_ops->read_resources() called on them.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600272 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100273 * The range 0xa0000 -> 0xc0000 does not have any resources associated with it to
274 * handle legacy VGA memory. If this range is not omitted the mtrr code will setup
275 * the area as cacheable, causing VGA access to not work.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600276 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100277 * The TSEG region is mapped as cacheable so that one can perform SMRAM relocation
278 * faster. Once the SMRR is enabled, the SMRR takes precedence over the existing
279 * MTRRs covering this region.
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600280 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100281 * It should be noted that cacheable entry types need to be added in order. The reason
282 * is that the current MTRR code assumes this and falls over itself if it isn't.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600283 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100284 * The resource index starts low and should not meet or exceed PCI_BASE_ADDRESS_0.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600285 */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600286 index = *resource_cnt;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600287
Aaron Durbin6a360042014-02-13 10:30:42 -0600288 /* 0 - > 0xa0000 */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600289 base_k = 0;
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600290 size_k = (0xa0000 >> 10) - base_k;
291 ram_resource(dev, index++, base_k, size_k);
292
Angel Pons4b290b72020-09-24 23:38:53 +0200293 /* 0xc0000 -> DPR base */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600294 base_k = 0xc0000 >> 10;
Angel Pons4b290b72020-09-24 23:38:53 +0200295 size_k = (unsigned long)(mc_values[TSEG_REG] >> 10) - (base_k + dpr.size);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600296 ram_resource(dev, index++, base_k, size_k);
297
Angel Pons4b290b72020-09-24 23:38:53 +0200298 /* DPR base -> TSEG */
299 if (dpr.size) {
300 resource = new_resource(dev, index++);
301 resource->base = (dpr.top - dpr.size) * MiB;
302 resource->size = dpr.size * MiB;
303 resource->flags = IORESOURCE_MEM | IORESOURCE_STORED | IORESOURCE_CACHEABLE |
304 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
305 }
306
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600307 /* TSEG -> BGSM */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600308 resource = new_resource(dev, index++);
309 resource->base = mc_values[TSEG_REG];
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600310 resource->size = mc_values[BGSM_REG] - resource->base;
Angel Pons1db5bc72020-01-15 00:49:03 +0100311 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
312 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE;
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600313
Angel Pons1db5bc72020-01-15 00:49:03 +0100314 /* BGSM -> TOLUD. If the IGD is disabled, BGSM can equal TOLUD. */
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300315 if (mc_values[BGSM_REG] != mc_values[TOLUD_REG]) {
316 resource = new_resource(dev, index++);
317 resource->base = mc_values[BGSM_REG];
318 resource->size = mc_values[TOLUD_REG] - resource->base;
Angel Pons1db5bc72020-01-15 00:49:03 +0100319 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
320 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300321 }
Aaron Durbinc12ef972012-12-18 14:22:49 -0600322
323 /* 4GiB -> TOUUD */
324 base_k = 4096 * 1024; /* 4GiB */
Aaron Durbin27435d32013-06-03 09:46:56 -0500325 touud_k = mc_values[TOUUD_REG] >> 10;
326 size_k = touud_k - base_k;
327 if (touud_k > base_k)
Aaron Durbin5c66f082013-01-08 10:10:33 -0600328 ram_resource(dev, index++, base_k, size_k);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600329
Aaron Durbinc9650762013-03-22 22:03:09 -0500330 /* Reserve everything between A segment and 1MB:
331 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100332 * 0xa0000 - 0xbffff: Legacy VGA
Aaron Durbinc9650762013-03-22 22:03:09 -0500333 * 0xc0000 - 0xfffff: RAM
334 */
335 mmio_resource(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
Angel Pons1db5bc72020-01-15 00:49:03 +0100336 reserved_ram_resource(dev, index++, (0xc0000 >> 10), (0x100000 - 0xc0000) >> 10);
337
Julius Wernercd49cce2019-03-05 16:53:33 -0800338#if CONFIG(CHROMEOS_RAMOOPS)
Aaron Durbinc9650762013-03-22 22:03:09 -0500339 reserved_ram_resource(dev, index++,
340 CONFIG_CHROMEOS_RAMOOPS_RAM_START >> 10,
Angel Pons1db5bc72020-01-15 00:49:03 +0100341 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE >> 10);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600342#endif
Matt DeVilliera51e3792018-03-04 01:44:15 -0600343 *resource_cnt = index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600344}
345
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200346static void mc_read_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600347{
Matt DeVilliera51e3792018-03-04 01:44:15 -0600348 int index = 0;
Angel Pons1db5bc72020-01-15 00:49:03 +0100349 const bool vtd_capable = !(pci_read_config32(dev, CAPID0_A) & VTD_DISABLE);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600350
Angel Pons1db5bc72020-01-15 00:49:03 +0100351 /* Read standard PCI resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600352 pci_dev_read_resources(dev);
353
Angel Pons1db5bc72020-01-15 00:49:03 +0100354 /* Add all fixed MMIO resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600355 mc_add_fixed_mmio_resources(dev);
356
Angel Pons1db5bc72020-01-15 00:49:03 +0100357 /* Add VT-d MMIO resources, if capable */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600358 if (vtd_capable) {
Angel Pons1db5bc72020-01-15 00:49:03 +0100359 mmio_resource(dev, index++, GFXVT_BASE_ADDRESS / KiB, GFXVT_BASE_SIZE / KiB);
360 mmio_resource(dev, index++, VTVC0_BASE_ADDRESS / KiB, VTVC0_BASE_SIZE / KiB);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600361 }
362
Angel Pons1db5bc72020-01-15 00:49:03 +0100363 /* Calculate and add DRAM resources */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600364 mc_add_dram_resources(dev, &index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500365}
366
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300367/*
Angel Pons1db5bc72020-01-15 00:49:03 +0100368 * The Mini-HD audio device is disabled whenever the IGD is. This is because it provides
369 * audio over the integrated graphics port(s), which requires the IGD to be functional.
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300370 */
371static void disable_devices(void)
372{
373 static const struct {
374 const unsigned int devfn;
375 const u32 mask;
376 const char *const name;
377 } nb_devs[] = {
378 { PCI_DEVFN(1, 2), DEVEN_D1F2EN, "PEG12" },
379 { PCI_DEVFN(1, 1), DEVEN_D1F1EN, "PEG11" },
380 { PCI_DEVFN(1, 0), DEVEN_D1F0EN, "PEG10" },
381 { PCI_DEVFN(2, 0), DEVEN_D2EN | DEVEN_D3EN, "IGD" },
382 { PCI_DEVFN(3, 0), DEVEN_D3EN, "Mini-HD audio" },
383 { PCI_DEVFN(4, 0), DEVEN_D4EN, "\"device 4\"" },
384 { PCI_DEVFN(7, 0), DEVEN_D7EN, "\"device 7\"" },
385 };
386
Angel Pons1db5bc72020-01-15 00:49:03 +0100387 struct device *host_dev = pcidev_on_root(0, 0);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300388 u32 deven;
389 size_t i;
390
391 if (!host_dev)
392 return;
393
394 deven = pci_read_config32(host_dev, DEVEN);
395
396 for (i = 0; i < ARRAY_SIZE(nb_devs); i++) {
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300397 struct device *dev = pcidev_path_on_root(nb_devs[i].devfn);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300398 if (!dev || !dev->enabled) {
399 printk(BIOS_DEBUG, "Disabling %s.\n", nb_devs[i].name);
400 deven &= ~nb_devs[i].mask;
401 }
402 }
403
404 pci_write_config32(host_dev, DEVEN, deven);
405}
406
Angel Pons028b8e42020-07-24 14:03:29 +0200407static void init_egress(void)
408{
409 /* VC0: Enable, ID0, TC0 */
410 EPBAR32(EPVC0RCTL) = (1 << 31) | (0 << 24) | (1 << 0);
411
412 /* No Low Priority Extended VCs, one Extended VC */
413 EPBAR32(EPPVCCAP1) = (0 << 4) | (1 << 0);
414
415 /* VC1: Enable, ID1, TC1 */
416 EPBAR32(EPVC1RCTL) = (1 << 31) | (1 << 24) | (1 << 1);
417
418 /* Poll the VC1 Negotiation Pending bit */
419 while ((EPBAR16(EPVC1RSTS) & (1 << 1)) != 0)
420 ;
421}
422
Angel Pons598ec6a2020-07-23 02:37:12 +0200423static void northbridge_dmi_init(void)
424{
425 const bool is_haswell_h = !CONFIG(INTEL_LYNXPOINT_LP);
426
427 u16 reg16;
428 u32 reg32;
429
430 /* Steps prior to DMI ASPM */
431 if (is_haswell_h) {
432 /* Configure DMI De-Emphasis */
433 reg16 = DMIBAR16(DMILCTL2);
434 reg16 |= (1 << 6); /* 0b: -6.0 dB, 1b: -3.5 dB */
435 DMIBAR16(DMILCTL2) = reg16;
436
437 reg32 = DMIBAR32(DMIL0SLAT);
438 reg32 |= (1 << 31);
439 DMIBAR32(DMIL0SLAT) = reg32;
440
441 reg32 = DMIBAR32(DMILLTC);
442 reg32 |= (1 << 29);
443 DMIBAR32(DMILLTC) = reg32;
444
445 reg32 = DMIBAR32(DMI_AFE_PM_TMR);
446 reg32 &= ~0x1f;
447 reg32 |= 0x13;
448 DMIBAR32(DMI_AFE_PM_TMR) = reg32;
449 }
450
451 /* Clear error status bits */
452 DMIBAR32(DMIUESTS) = 0xffffffff;
453 DMIBAR32(DMICESTS) = 0xffffffff;
454
455 if (is_haswell_h) {
456 /* Enable ASPM L0s and L1 on SA link, should happen before PCH link */
457 reg16 = DMIBAR16(DMILCTL);
458 reg16 |= (1 << 1) | (1 << 0);
459 DMIBAR16(DMILCTL) = reg16;
460 }
461}
462
Angel Pons76b8bc22020-07-23 02:32:27 +0200463static void northbridge_topology_init(void)
464{
465 const u32 eple_a[3] = { EPLE2A, EPLE3A, EPLE4A };
466 const u32 eple_d[3] = { EPLE2D, EPLE3D, EPLE4D };
467
468 u32 reg32;
469
470 /* Set the CID1 Egress Port 0 Root Topology */
471 reg32 = EPBAR32(EPESD);
472 reg32 &= ~(0xff << 16);
473 reg32 |= 1 << 16;
474 EPBAR32(EPESD) = reg32;
475
476 reg32 = EPBAR32(EPLE1D);
477 reg32 &= ~(0xff << 16);
478 reg32 |= 1 | (1 << 16);
479 EPBAR32(EPLE1D) = reg32;
480 EPBAR64(EPLE1A) = (uintptr_t)DEFAULT_DMIBAR;
481
482 for (unsigned int i = 0; i <= 2; i++) {
483 const struct device *const dev = pcidev_on_root(1, i);
484
485 if (!dev || !dev->enabled)
486 continue;
487
488 EPBAR64(eple_a[i]) = (u64)PCI_DEV(0, 1, i);
489
490 reg32 = EPBAR32(eple_d[i]);
491 reg32 &= ~(0xff << 16);
492 reg32 |= 1 | (1 << 16);
493 EPBAR32(eple_d[i]) = reg32;
494
495 pci_update_config32(dev, PEG_ESD, ~(0xff << 16), (1 << 16));
496 pci_write_config32(dev, PEG_LE1A, (uintptr_t)DEFAULT_EPBAR);
497 pci_write_config32(dev, PEG_LE1A + 4, 0);
498 pci_update_config32(dev, PEG_LE1D, ~(0xff << 16), (1 << 16) | 1);
499
500 /* Read and write to lock register */
501 pci_or_config32(dev, PEG_DCAP2, 0);
502 }
503
504 /* Set the CID1 DMI Port Root Topology */
505 reg32 = DMIBAR32(DMIESD);
506 reg32 &= ~(0xff << 16);
507 reg32 |= 1 << 16;
508 DMIBAR32(DMIESD) = reg32;
509
510 reg32 = DMIBAR32(DMILE1D);
511 reg32 &= ~(0xffff << 16);
512 reg32 |= 1 | (2 << 16);
513 DMIBAR32(DMILE1D) = reg32;
514 DMIBAR64(DMILE1A) = (uintptr_t)DEFAULT_RCBA;
515
516 DMIBAR64(DMILE2A) = (uintptr_t)DEFAULT_EPBAR;
517 reg32 = DMIBAR32(DMILE2D);
518 reg32 &= ~(0xff << 16);
519 reg32 |= 1 | (1 << 16);
520 DMIBAR32(DMILE2D) = reg32;
521
522 /* Program RO and Write-Once Registers */
523 DMIBAR32(DMIPVCCAP1) = DMIBAR32(DMIPVCCAP1);
524 DMIBAR32(DMILCAP) = DMIBAR32(DMILCAP);
525}
526
Aaron Durbin76c37002012-10-30 09:03:43 -0500527static void northbridge_init(struct device *dev)
528{
Duncan Lauriec70353f2013-06-28 14:40:38 -0700529 u8 bios_reset_cpl, pair;
Aaron Durbin76c37002012-10-30 09:03:43 -0500530
Angel Pons028b8e42020-07-24 14:03:29 +0200531 init_egress();
Angel Pons598ec6a2020-07-23 02:37:12 +0200532 northbridge_dmi_init();
Angel Pons76b8bc22020-07-23 02:32:27 +0200533 northbridge_topology_init();
Angel Pons598ec6a2020-07-23 02:37:12 +0200534
Angel Pons1db5bc72020-01-15 00:49:03 +0100535 /* Enable Power Aware Interrupt Routing. */
536 pair = MCHBAR8(INTRDIRCTL);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700537 pair &= ~0x7; /* Clear 2:0 */
538 pair |= 0x4; /* Fixed Priority */
Angel Pons1db5bc72020-01-15 00:49:03 +0100539 MCHBAR8(INTRDIRCTL) = pair;
Aaron Durbin76c37002012-10-30 09:03:43 -0500540
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300541 disable_devices();
542
Aaron Durbin76c37002012-10-30 09:03:43 -0500543 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100544 * Set bits 0 + 1 of BIOS_RESET_CPL to indicate to the CPU
545 * that BIOS has initialized memory and power management.
Aaron Durbin76c37002012-10-30 09:03:43 -0500546 */
547 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700548 bios_reset_cpl |= 3;
Aaron Durbin76c37002012-10-30 09:03:43 -0500549 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
550 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
551
Angel Pons1db5bc72020-01-15 00:49:03 +0100552 /* Configure turbo power limits 1ms after reset complete bit. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500553 mdelay(1);
554 set_power_limits(28);
555
Angel Pons1db5bc72020-01-15 00:49:03 +0100556 /* Set here before graphics PM init. */
557 MCHBAR32(MMIO_PAVP_MSG) = 0x00100001;
Aaron Durbin76c37002012-10-30 09:03:43 -0500558}
559
Aaron Durbin76c37002012-10-30 09:03:43 -0500560static struct device_operations mc_ops = {
Nico Huber68680dd2020-03-31 17:34:52 +0200561 .read_resources = mc_read_resources,
562 .set_resources = pci_dev_set_resources,
563 .enable_resources = pci_dev_enable_resources,
564 .init = northbridge_init,
565 .acpi_fill_ssdt = generate_cpu_entries,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200566 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500567};
568
Tristan Corrickd3856242018-11-01 03:03:29 +1300569static const unsigned short mc_pci_device_ids[] = {
570 0x0c00, /* Desktop */
571 0x0c04, /* Mobile */
572 0x0a04, /* ULT */
Iru Cai0766c982018-12-17 13:21:36 +0800573 0x0c08, /* Server */
Iru Cai12a13e12020-05-22 22:57:03 +0800574 0x0d00, /* Crystal Well Desktop */
575 0x0d04, /* Crystal Well Mobile */
576 0x0d08, /* Crystal Well Server (by extrapolation) */
Tristan Corrickd3856242018-11-01 03:03:29 +1300577 0
Tristan Corrick48170122018-10-31 02:21:41 +1300578};
579
Tristan Corrickd3856242018-11-01 03:03:29 +1300580static const struct pci_driver mc_driver_hsw __pci_driver = {
581 .ops = &mc_ops,
582 .vendor = PCI_VENDOR_ID_INTEL,
583 .devices = mc_pci_device_ids,
Duncan Lauriedf7be712012-12-17 11:22:57 -0800584};
585
Aaron Durbin76c37002012-10-30 09:03:43 -0500586static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200587 .read_resources = noop_read_resources,
588 .set_resources = noop_set_resources,
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300589 .init = mp_cpu_bus_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500590};
591
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200592static void enable_dev(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500593{
Angel Pons1db5bc72020-01-15 00:49:03 +0100594 /* Set the operations if it is a special bus type. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500595 if (dev->path.type == DEVICE_PATH_DOMAIN) {
596 dev->ops = &pci_domain_ops;
597 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
598 dev->ops = &cpu_bus_ops;
599 }
600}
601
602struct chip_operations northbridge_intel_haswell_ops = {
Angel Pons7bbf45e2020-10-22 23:55:24 +0200603 CHIP_NAME("Intel Haswell integrated Northbridge")
Aaron Durbin76c37002012-10-30 09:03:43 -0500604 .enable_dev = enable_dev,
605};