blob: d25338033dc89a68c998b4ce7652848c893ef2af [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
Angel Pons1db5bc72020-01-15 00:49:03 +010021static int get_pcie_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050022{
Angel Pons1db5bc72020-01-15 00:49:03 +010023 u32 pciexbar_reg, mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050024
25 *base = 0;
26 *len = 0;
27
Aaron Durbinc12ef972012-12-18 14:22:49 -060028 pciexbar_reg = pci_read_config32(dev, index);
Aaron Durbin76c37002012-10-30 09:03:43 -050029
30 if (!(pciexbar_reg & (1 << 0)))
31 return 0;
32
33 switch ((pciexbar_reg >> 1) & 3) {
Angel Pons1db5bc72020-01-15 00:49:03 +010034 case 0: /* 256MB */
Angel Ponsf5dd7b62020-10-24 12:24:19 +020035 mask = (1 << 31) | (1 << 30) | (1 << 29) | (1 << 28);
Ryan Salsamendifa0725d2017-06-30 17:29:37 -070036 *base = pciexbar_reg & mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050037 *len = 256 * 1024 * 1024;
38 return 1;
Angel Pons1db5bc72020-01-15 00:49:03 +010039 case 1: /* 128M */
Angel Ponsf5dd7b62020-10-24 12:24:19 +020040 mask = (1 << 31) | (1 << 30) | (1 << 29) | (1 << 28);
Ryan Salsamendifa0725d2017-06-30 17:29:37 -070041 mask |= (1 << 27);
42 *base = pciexbar_reg & mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050043 *len = 128 * 1024 * 1024;
44 return 1;
Angel Pons1db5bc72020-01-15 00:49:03 +010045 case 2: /* 64M */
Angel Ponsf5dd7b62020-10-24 12:24:19 +020046 mask = (1 << 31) | (1 << 30) | (1 << 29) | (1 << 28);
Ryan Salsamendifa0725d2017-06-30 17:29:37 -070047 mask |= (1 << 27) | (1 << 26);
48 *base = pciexbar_reg & mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050049 *len = 64 * 1024 * 1024;
50 return 1;
51 }
52
53 return 0;
54}
55
Angel Ponsf4fa1e12020-08-03 14:12:13 +020056int decode_pcie_bar(u32 *const base, u32 *const len)
57{
58 return get_pcie_bar(pcidev_on_root(0, 0), PCIEXBAR, base, len);
59}
60
Tristan Corrickf3127d42018-10-31 02:25:54 +130061static const char *northbridge_acpi_name(const struct device *dev)
62{
63 if (dev->path.type == DEVICE_PATH_DOMAIN)
64 return "PCI0";
65
66 if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
67 return NULL;
68
69 switch (dev->path.pci.devfn) {
70 case PCI_DEVFN(0, 0):
71 return "MCHC";
72 }
73
74 return NULL;
75}
76
Angel Pons1db5bc72020-01-15 00:49:03 +010077/*
78 * TODO: We could determine how many PCIe busses we need in the bar.
79 * For now, that number is hardcoded to a max of 64.
80 */
Aaron Durbin76c37002012-10-30 09:03:43 -050081static struct device_operations pci_domain_ops = {
Angel Pons1db5bc72020-01-15 00:49:03 +010082 .read_resources = pci_domain_read_resources,
83 .set_resources = pci_domain_set_resources,
Angel Pons1db5bc72020-01-15 00:49:03 +010084 .scan_bus = pci_domain_scan_bus,
85 .acpi_name = northbridge_acpi_name,
Matt DeVillier85d98d92018-03-04 01:41:23 -060086 .write_acpi_tables = northbridge_write_acpi_tables,
Aaron Durbin76c37002012-10-30 09:03:43 -050087};
88
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +020089static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050090{
Angel Pons1db5bc72020-01-15 00:49:03 +010091 u32 bar = pci_read_config32(dev, index);
Aaron Durbin76c37002012-10-30 09:03:43 -050092
Angel Pons1db5bc72020-01-15 00:49:03 +010093 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -060094 if (!(bar & 0x1))
95 return 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050096
Angel Pons1db5bc72020-01-15 00:49:03 +010097 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -060098 *base = bar & ~1;
99
100 return 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500101}
102
Angel Pons1db5bc72020-01-15 00:49:03 +0100103/*
104 * There are special BARs that actually are programmed in the MCHBAR. These Intel special
105 * features, but they do consume resources that need to be accounted for.
106 */
107static int get_bar_in_mchbar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -0500108{
Angel Pons1db5bc72020-01-15 00:49:03 +0100109 u32 bar = MCHBAR32(index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500110
Angel Pons1db5bc72020-01-15 00:49:03 +0100111 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600112 if (!(bar & 0x1))
113 return 0;
114
Angel Pons1db5bc72020-01-15 00:49:03 +0100115 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600116 *base = bar & ~1;
117
118 return 1;
119}
120
121struct fixed_mmio_descriptor {
122 unsigned int index;
123 u32 size;
Angel Pons1db5bc72020-01-15 00:49:03 +0100124 int (*get_resource)(struct device *dev, unsigned int index, u32 *base, u32 *size);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600125 const char *description;
126};
127
Angel Pons1db5bc72020-01-15 00:49:03 +0100128#define SIZE_KB(x) ((x) * 1024)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600129struct fixed_mmio_descriptor mc_fixed_resources[] = {
130 { PCIEXBAR, SIZE_KB(0), get_pcie_bar, "PCIEXBAR" },
131 { MCHBAR, SIZE_KB(32), get_bar, "MCHBAR" },
132 { DMIBAR, SIZE_KB(4), get_bar, "DMIBAR" },
133 { EPBAR, SIZE_KB(4), get_bar, "EPBAR" },
Angel Pons1db5bc72020-01-15 00:49:03 +0100134 { GDXCBAR, SIZE_KB(4), get_bar_in_mchbar, "GDXCBAR" },
135 { EDRAMBAR, SIZE_KB(16), get_bar_in_mchbar, "EDRAMBAR" },
Aaron Durbinc12ef972012-12-18 14:22:49 -0600136};
137#undef SIZE_KB
138
Angel Pons1db5bc72020-01-15 00:49:03 +0100139/* Add all known fixed MMIO ranges that hang off the host bridge/memory controller device. */
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200140static void mc_add_fixed_mmio_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600141{
142 int i;
143
144 for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
145 u32 base;
146 u32 size;
147 struct resource *resource;
148 unsigned int index;
149
150 size = mc_fixed_resources[i].size;
151 index = mc_fixed_resources[i].index;
Angel Pons1db5bc72020-01-15 00:49:03 +0100152 if (!mc_fixed_resources[i].get_resource(dev, index, &base, &size))
Aaron Durbinc12ef972012-12-18 14:22:49 -0600153 continue;
154
155 resource = new_resource(dev, mc_fixed_resources[i].index);
Angel Pons1db5bc72020-01-15 00:49:03 +0100156 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
157 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
158
Aaron Durbinc12ef972012-12-18 14:22:49 -0600159 resource->base = base;
160 resource->size = size;
161 printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
162 __func__, mc_fixed_resources[i].description, index,
163 (unsigned long)base, (unsigned long)(base + size - 1));
164 }
165}
166
Angel Pons4b290b72020-09-24 23:38:53 +0200167/*
168 * Host Memory Map:
Aaron Durbinc12ef972012-12-18 14:22:49 -0600169 *
170 * +--------------------------+ TOUUD
171 * | |
172 * +--------------------------+ 4GiB
173 * | PCI Address Space |
174 * +--------------------------+ TOLUD (also maps into MC address space)
175 * | iGD |
176 * +--------------------------+ BDSM
177 * | GTT |
178 * +--------------------------+ BGSM
179 * | TSEG |
180 * +--------------------------+ TSEGMB
Angel Pons4b290b72020-09-24 23:38:53 +0200181 * | DPR |
182 * +--------------------------+ (DPR top - DPR size)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600183 * | Usage DRAM |
184 * +--------------------------+ 0
185 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100186 * Some of the base registers above can be equal, making the size of the regions within 0.
187 * This is because the memory controller internally subtracts the base registers from each
188 * other to determine sizes of the regions. In other words, the memory map regions are always
189 * in a fixed order, no matter what sizes they have.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600190 */
191
192struct map_entry {
193 int reg;
194 int is_64_bit;
195 int is_limit;
196 const char *description;
197};
198
Angel Pons1db5bc72020-01-15 00:49:03 +0100199static void read_map_entry(struct device *dev, struct map_entry *entry, uint64_t *result)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600200{
201 uint64_t value;
202 uint64_t mask;
203
Angel Pons1db5bc72020-01-15 00:49:03 +0100204 /* All registers have a 1MiB granularity */
205 mask = ((1ULL << 20) - 1);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600206 mask = ~mask;
207
208 value = 0;
209
210 if (entry->is_64_bit) {
211 value = pci_read_config32(dev, entry->reg + 4);
212 value <<= 32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500213 }
214
Aaron Durbinc12ef972012-12-18 14:22:49 -0600215 value |= pci_read_config32(dev, entry->reg);
216 value &= mask;
217
218 if (entry->is_limit)
219 value |= ~mask;
220
221 *result = value;
222}
223
224#define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
225 { \
226 .reg = reg_, \
227 .is_64_bit = is_64_, \
228 .is_limit = is_limit_, \
229 .description = desc_, \
230 }
231
Angel Pons1db5bc72020-01-15 00:49:03 +0100232#define MAP_ENTRY_BASE_32(reg_, desc_) MAP_ENTRY(reg_, 0, 0, desc_)
233#define MAP_ENTRY_BASE_64(reg_, desc_) MAP_ENTRY(reg_, 1, 0, desc_)
234#define MAP_ENTRY_LIMIT_64(reg_, desc_) MAP_ENTRY(reg_, 1, 1, desc_)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600235
236enum {
237 TOM_REG,
238 TOUUD_REG,
239 MESEG_BASE_REG,
240 MESEG_LIMIT_REG,
241 REMAP_BASE_REG,
242 REMAP_LIMIT_REG,
243 TOLUD_REG,
244 BGSM_REG,
245 BDSM_REG,
246 TSEG_REG,
Angel Pons1db5bc72020-01-15 00:49:03 +0100247 /* Must be last */
248 NUM_MAP_ENTRIES,
Aaron Durbinc12ef972012-12-18 14:22:49 -0600249};
250
251static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
Angel Pons1db5bc72020-01-15 00:49:03 +0100252 [TOM_REG] = MAP_ENTRY_BASE_64(TOM, "TOM"),
253 [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
254 [MESEG_BASE_REG] = MAP_ENTRY_BASE_64(MESEG_BASE, "MESEG_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600255 [MESEG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT, "MESEG_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100256 [REMAP_BASE_REG] = MAP_ENTRY_BASE_64(REMAPBASE, "REMAP_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600257 [REMAP_LIMIT_REG] = MAP_ENTRY_LIMIT_64(REMAPLIMIT, "REMAP_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100258 [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
259 [BDSM_REG] = MAP_ENTRY_BASE_32(BDSM, "BDSM"),
260 [BGSM_REG] = MAP_ENTRY_BASE_32(BGSM, "BGSM"),
Angel Ponsd8abb262020-05-07 00:48:35 +0200261 [TSEG_REG] = MAP_ENTRY_BASE_32(TSEG, "TSEGMB"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600262};
263
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200264static void mc_read_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600265{
266 int i;
267 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
268 read_map_entry(dev, &memory_map[i], &values[i]);
269 }
270}
271
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200272static void mc_report_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600273{
274 int i;
275 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
276 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
277 memory_map[i].description, values[i]);
278 }
Angel Pons1db5bc72020-01-15 00:49:03 +0100279 /* One can validate the BDSM and BGSM against the GGC */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600280 printk(BIOS_DEBUG, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev, GGC));
281}
282
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200283static void mc_add_dram_resources(struct device *dev, int *resource_cnt)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600284{
Angel Pons1db5bc72020-01-15 00:49:03 +0100285 unsigned long base_k, size_k, touud_k, index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600286 struct resource *resource;
287 uint64_t mc_values[NUM_MAP_ENTRIES];
288
Angel Pons1db5bc72020-01-15 00:49:03 +0100289 /* Read in the MAP registers and report their values */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600290 mc_read_map_entries(dev, &mc_values[0]);
291 mc_report_map_entries(dev, &mc_values[0]);
292
Angel Pons4b290b72020-09-24 23:38:53 +0200293 /* The DPR register is special */
294 const union dpr_register dpr = {
295 .raw = pci_read_config32(dev, DPR),
296 };
297 printk(BIOS_DEBUG, "MC MAP: DPR: 0x%x\n", dpr.raw);
298
Aaron Durbinc12ef972012-12-18 14:22:49 -0600299 /*
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600300 * These are the host memory ranges that should be added:
Angel Pons1db5bc72020-01-15 00:49:03 +0100301 * - 0 -> 0xa0000: cacheable
302 * - 0xc0000 -> TSEG: cacheable
303 * - TSEG -> BGSM: cacheable with standard MTRRs and reserved
304 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
305 * - 4GiB -> TOUUD: cacheable
Aaron Durbinc12ef972012-12-18 14:22:49 -0600306 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100307 * The default SMRAM space is reserved so that the range doesn't have to be saved
308 * during S3 Resume. Once marked reserved the OS cannot use the memory. This is a
309 * bit of an odd place to reserve the region, but the CPU devices don't have
310 * dev_ops->read_resources() called on them.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600311 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100312 * The range 0xa0000 -> 0xc0000 does not have any resources associated with it to
313 * handle legacy VGA memory. If this range is not omitted the mtrr code will setup
314 * the area as cacheable, causing VGA access to not work.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600315 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100316 * The TSEG region is mapped as cacheable so that one can perform SMRAM relocation
317 * faster. Once the SMRR is enabled, the SMRR takes precedence over the existing
318 * MTRRs covering this region.
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600319 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100320 * It should be noted that cacheable entry types need to be added in order. The reason
321 * is that the current MTRR code assumes this and falls over itself if it isn't.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600322 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100323 * The resource index starts low and should not meet or exceed PCI_BASE_ADDRESS_0.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600324 */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600325 index = *resource_cnt;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600326
Aaron Durbin6a360042014-02-13 10:30:42 -0600327 /* 0 - > 0xa0000 */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600328 base_k = 0;
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600329 size_k = (0xa0000 >> 10) - base_k;
330 ram_resource(dev, index++, base_k, size_k);
331
Angel Pons4b290b72020-09-24 23:38:53 +0200332 /* 0xc0000 -> DPR base */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600333 base_k = 0xc0000 >> 10;
Angel Pons4b290b72020-09-24 23:38:53 +0200334 size_k = (unsigned long)(mc_values[TSEG_REG] >> 10) - (base_k + dpr.size);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600335 ram_resource(dev, index++, base_k, size_k);
336
Angel Pons4b290b72020-09-24 23:38:53 +0200337 /* DPR base -> TSEG */
338 if (dpr.size) {
339 resource = new_resource(dev, index++);
340 resource->base = (dpr.top - dpr.size) * MiB;
341 resource->size = dpr.size * MiB;
342 resource->flags = IORESOURCE_MEM | IORESOURCE_STORED | IORESOURCE_CACHEABLE |
343 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
344 }
345
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600346 /* TSEG -> BGSM */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600347 resource = new_resource(dev, index++);
348 resource->base = mc_values[TSEG_REG];
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600349 resource->size = mc_values[BGSM_REG] - resource->base;
Angel Pons1db5bc72020-01-15 00:49:03 +0100350 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
351 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE;
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600352
Angel Pons1db5bc72020-01-15 00:49:03 +0100353 /* BGSM -> TOLUD. If the IGD is disabled, BGSM can equal TOLUD. */
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300354 if (mc_values[BGSM_REG] != mc_values[TOLUD_REG]) {
355 resource = new_resource(dev, index++);
356 resource->base = mc_values[BGSM_REG];
357 resource->size = mc_values[TOLUD_REG] - resource->base;
Angel Pons1db5bc72020-01-15 00:49:03 +0100358 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
359 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300360 }
Aaron Durbinc12ef972012-12-18 14:22:49 -0600361
362 /* 4GiB -> TOUUD */
363 base_k = 4096 * 1024; /* 4GiB */
Aaron Durbin27435d32013-06-03 09:46:56 -0500364 touud_k = mc_values[TOUUD_REG] >> 10;
365 size_k = touud_k - base_k;
366 if (touud_k > base_k)
Aaron Durbin5c66f082013-01-08 10:10:33 -0600367 ram_resource(dev, index++, base_k, size_k);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600368
Aaron Durbinc9650762013-03-22 22:03:09 -0500369 /* Reserve everything between A segment and 1MB:
370 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100371 * 0xa0000 - 0xbffff: Legacy VGA
Aaron Durbinc9650762013-03-22 22:03:09 -0500372 * 0xc0000 - 0xfffff: RAM
373 */
374 mmio_resource(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
Angel Pons1db5bc72020-01-15 00:49:03 +0100375 reserved_ram_resource(dev, index++, (0xc0000 >> 10), (0x100000 - 0xc0000) >> 10);
376
Julius Wernercd49cce2019-03-05 16:53:33 -0800377#if CONFIG(CHROMEOS_RAMOOPS)
Aaron Durbinc9650762013-03-22 22:03:09 -0500378 reserved_ram_resource(dev, index++,
379 CONFIG_CHROMEOS_RAMOOPS_RAM_START >> 10,
Angel Pons1db5bc72020-01-15 00:49:03 +0100380 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE >> 10);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600381#endif
Matt DeVilliera51e3792018-03-04 01:44:15 -0600382 *resource_cnt = index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600383}
384
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200385static void mc_read_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600386{
Matt DeVilliera51e3792018-03-04 01:44:15 -0600387 int index = 0;
Angel Pons1db5bc72020-01-15 00:49:03 +0100388 const bool vtd_capable = !(pci_read_config32(dev, CAPID0_A) & VTD_DISABLE);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600389
Angel Pons1db5bc72020-01-15 00:49:03 +0100390 /* Read standard PCI resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600391 pci_dev_read_resources(dev);
392
Angel Pons1db5bc72020-01-15 00:49:03 +0100393 /* Add all fixed MMIO resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600394 mc_add_fixed_mmio_resources(dev);
395
Angel Pons1db5bc72020-01-15 00:49:03 +0100396 /* Add VT-d MMIO resources, if capable */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600397 if (vtd_capable) {
Angel Pons1db5bc72020-01-15 00:49:03 +0100398 mmio_resource(dev, index++, GFXVT_BASE_ADDRESS / KiB, GFXVT_BASE_SIZE / KiB);
399 mmio_resource(dev, index++, VTVC0_BASE_ADDRESS / KiB, VTVC0_BASE_SIZE / KiB);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600400 }
401
Angel Pons1db5bc72020-01-15 00:49:03 +0100402 /* Calculate and add DRAM resources */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600403 mc_add_dram_resources(dev, &index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500404}
405
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300406/*
Angel Pons1db5bc72020-01-15 00:49:03 +0100407 * The Mini-HD audio device is disabled whenever the IGD is. This is because it provides
408 * audio over the integrated graphics port(s), which requires the IGD to be functional.
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300409 */
410static void disable_devices(void)
411{
412 static const struct {
413 const unsigned int devfn;
414 const u32 mask;
415 const char *const name;
416 } nb_devs[] = {
417 { PCI_DEVFN(1, 2), DEVEN_D1F2EN, "PEG12" },
418 { PCI_DEVFN(1, 1), DEVEN_D1F1EN, "PEG11" },
419 { PCI_DEVFN(1, 0), DEVEN_D1F0EN, "PEG10" },
420 { PCI_DEVFN(2, 0), DEVEN_D2EN | DEVEN_D3EN, "IGD" },
421 { PCI_DEVFN(3, 0), DEVEN_D3EN, "Mini-HD audio" },
422 { PCI_DEVFN(4, 0), DEVEN_D4EN, "\"device 4\"" },
423 { PCI_DEVFN(7, 0), DEVEN_D7EN, "\"device 7\"" },
424 };
425
Angel Pons1db5bc72020-01-15 00:49:03 +0100426 struct device *host_dev = pcidev_on_root(0, 0);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300427 u32 deven;
428 size_t i;
429
430 if (!host_dev)
431 return;
432
433 deven = pci_read_config32(host_dev, DEVEN);
434
435 for (i = 0; i < ARRAY_SIZE(nb_devs); i++) {
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300436 struct device *dev = pcidev_path_on_root(nb_devs[i].devfn);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300437 if (!dev || !dev->enabled) {
438 printk(BIOS_DEBUG, "Disabling %s.\n", nb_devs[i].name);
439 deven &= ~nb_devs[i].mask;
440 }
441 }
442
443 pci_write_config32(host_dev, DEVEN, deven);
444}
445
Angel Pons028b8e42020-07-24 14:03:29 +0200446static void init_egress(void)
447{
448 /* VC0: Enable, ID0, TC0 */
449 EPBAR32(EPVC0RCTL) = (1 << 31) | (0 << 24) | (1 << 0);
450
451 /* No Low Priority Extended VCs, one Extended VC */
452 EPBAR32(EPPVCCAP1) = (0 << 4) | (1 << 0);
453
454 /* VC1: Enable, ID1, TC1 */
455 EPBAR32(EPVC1RCTL) = (1 << 31) | (1 << 24) | (1 << 1);
456
457 /* Poll the VC1 Negotiation Pending bit */
458 while ((EPBAR16(EPVC1RSTS) & (1 << 1)) != 0)
459 ;
460}
461
Angel Pons598ec6a2020-07-23 02:37:12 +0200462static void northbridge_dmi_init(void)
463{
464 const bool is_haswell_h = !CONFIG(INTEL_LYNXPOINT_LP);
465
466 u16 reg16;
467 u32 reg32;
468
469 /* Steps prior to DMI ASPM */
470 if (is_haswell_h) {
471 /* Configure DMI De-Emphasis */
472 reg16 = DMIBAR16(DMILCTL2);
473 reg16 |= (1 << 6); /* 0b: -6.0 dB, 1b: -3.5 dB */
474 DMIBAR16(DMILCTL2) = reg16;
475
476 reg32 = DMIBAR32(DMIL0SLAT);
477 reg32 |= (1 << 31);
478 DMIBAR32(DMIL0SLAT) = reg32;
479
480 reg32 = DMIBAR32(DMILLTC);
481 reg32 |= (1 << 29);
482 DMIBAR32(DMILLTC) = reg32;
483
484 reg32 = DMIBAR32(DMI_AFE_PM_TMR);
485 reg32 &= ~0x1f;
486 reg32 |= 0x13;
487 DMIBAR32(DMI_AFE_PM_TMR) = reg32;
488 }
489
490 /* Clear error status bits */
491 DMIBAR32(DMIUESTS) = 0xffffffff;
492 DMIBAR32(DMICESTS) = 0xffffffff;
493
494 if (is_haswell_h) {
495 /* Enable ASPM L0s and L1 on SA link, should happen before PCH link */
496 reg16 = DMIBAR16(DMILCTL);
497 reg16 |= (1 << 1) | (1 << 0);
498 DMIBAR16(DMILCTL) = reg16;
499 }
500}
501
Angel Pons76b8bc22020-07-23 02:32:27 +0200502static void northbridge_topology_init(void)
503{
504 const u32 eple_a[3] = { EPLE2A, EPLE3A, EPLE4A };
505 const u32 eple_d[3] = { EPLE2D, EPLE3D, EPLE4D };
506
507 u32 reg32;
508
509 /* Set the CID1 Egress Port 0 Root Topology */
510 reg32 = EPBAR32(EPESD);
511 reg32 &= ~(0xff << 16);
512 reg32 |= 1 << 16;
513 EPBAR32(EPESD) = reg32;
514
515 reg32 = EPBAR32(EPLE1D);
516 reg32 &= ~(0xff << 16);
517 reg32 |= 1 | (1 << 16);
518 EPBAR32(EPLE1D) = reg32;
519 EPBAR64(EPLE1A) = (uintptr_t)DEFAULT_DMIBAR;
520
521 for (unsigned int i = 0; i <= 2; i++) {
522 const struct device *const dev = pcidev_on_root(1, i);
523
524 if (!dev || !dev->enabled)
525 continue;
526
527 EPBAR64(eple_a[i]) = (u64)PCI_DEV(0, 1, i);
528
529 reg32 = EPBAR32(eple_d[i]);
530 reg32 &= ~(0xff << 16);
531 reg32 |= 1 | (1 << 16);
532 EPBAR32(eple_d[i]) = reg32;
533
534 pci_update_config32(dev, PEG_ESD, ~(0xff << 16), (1 << 16));
535 pci_write_config32(dev, PEG_LE1A, (uintptr_t)DEFAULT_EPBAR);
536 pci_write_config32(dev, PEG_LE1A + 4, 0);
537 pci_update_config32(dev, PEG_LE1D, ~(0xff << 16), (1 << 16) | 1);
538
539 /* Read and write to lock register */
540 pci_or_config32(dev, PEG_DCAP2, 0);
541 }
542
543 /* Set the CID1 DMI Port Root Topology */
544 reg32 = DMIBAR32(DMIESD);
545 reg32 &= ~(0xff << 16);
546 reg32 |= 1 << 16;
547 DMIBAR32(DMIESD) = reg32;
548
549 reg32 = DMIBAR32(DMILE1D);
550 reg32 &= ~(0xffff << 16);
551 reg32 |= 1 | (2 << 16);
552 DMIBAR32(DMILE1D) = reg32;
553 DMIBAR64(DMILE1A) = (uintptr_t)DEFAULT_RCBA;
554
555 DMIBAR64(DMILE2A) = (uintptr_t)DEFAULT_EPBAR;
556 reg32 = DMIBAR32(DMILE2D);
557 reg32 &= ~(0xff << 16);
558 reg32 |= 1 | (1 << 16);
559 DMIBAR32(DMILE2D) = reg32;
560
561 /* Program RO and Write-Once Registers */
562 DMIBAR32(DMIPVCCAP1) = DMIBAR32(DMIPVCCAP1);
563 DMIBAR32(DMILCAP) = DMIBAR32(DMILCAP);
564}
565
Aaron Durbin76c37002012-10-30 09:03:43 -0500566static void northbridge_init(struct device *dev)
567{
Duncan Lauriec70353f2013-06-28 14:40:38 -0700568 u8 bios_reset_cpl, pair;
Aaron Durbin76c37002012-10-30 09:03:43 -0500569
Angel Pons028b8e42020-07-24 14:03:29 +0200570 init_egress();
Angel Pons598ec6a2020-07-23 02:37:12 +0200571 northbridge_dmi_init();
Angel Pons76b8bc22020-07-23 02:32:27 +0200572 northbridge_topology_init();
Angel Pons598ec6a2020-07-23 02:37:12 +0200573
Angel Pons1db5bc72020-01-15 00:49:03 +0100574 /* Enable Power Aware Interrupt Routing. */
575 pair = MCHBAR8(INTRDIRCTL);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700576 pair &= ~0x7; /* Clear 2:0 */
577 pair |= 0x4; /* Fixed Priority */
Angel Pons1db5bc72020-01-15 00:49:03 +0100578 MCHBAR8(INTRDIRCTL) = pair;
Aaron Durbin76c37002012-10-30 09:03:43 -0500579
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300580 disable_devices();
581
Aaron Durbin76c37002012-10-30 09:03:43 -0500582 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100583 * Set bits 0 + 1 of BIOS_RESET_CPL to indicate to the CPU
584 * that BIOS has initialized memory and power management.
Aaron Durbin76c37002012-10-30 09:03:43 -0500585 */
586 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700587 bios_reset_cpl |= 3;
Aaron Durbin76c37002012-10-30 09:03:43 -0500588 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
589 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
590
Angel Pons1db5bc72020-01-15 00:49:03 +0100591 /* Configure turbo power limits 1ms after reset complete bit. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500592 mdelay(1);
593 set_power_limits(28);
594
Angel Pons1db5bc72020-01-15 00:49:03 +0100595 /* Set here before graphics PM init. */
596 MCHBAR32(MMIO_PAVP_MSG) = 0x00100001;
Aaron Durbin76c37002012-10-30 09:03:43 -0500597}
598
Aaron Durbin76c37002012-10-30 09:03:43 -0500599static struct device_operations mc_ops = {
Nico Huber68680dd2020-03-31 17:34:52 +0200600 .read_resources = mc_read_resources,
601 .set_resources = pci_dev_set_resources,
602 .enable_resources = pci_dev_enable_resources,
603 .init = northbridge_init,
604 .acpi_fill_ssdt = generate_cpu_entries,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200605 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500606};
607
Tristan Corrickd3856242018-11-01 03:03:29 +1300608static const unsigned short mc_pci_device_ids[] = {
609 0x0c00, /* Desktop */
610 0x0c04, /* Mobile */
611 0x0a04, /* ULT */
Iru Cai0766c982018-12-17 13:21:36 +0800612 0x0c08, /* Server */
Iru Cai12a13e12020-05-22 22:57:03 +0800613 0x0d00, /* Crystal Well Desktop */
614 0x0d04, /* Crystal Well Mobile */
615 0x0d08, /* Crystal Well Server (by extrapolation) */
Tristan Corrickd3856242018-11-01 03:03:29 +1300616 0
Tristan Corrick48170122018-10-31 02:21:41 +1300617};
618
Tristan Corrickd3856242018-11-01 03:03:29 +1300619static const struct pci_driver mc_driver_hsw __pci_driver = {
620 .ops = &mc_ops,
621 .vendor = PCI_VENDOR_ID_INTEL,
622 .devices = mc_pci_device_ids,
Duncan Lauriedf7be712012-12-17 11:22:57 -0800623};
624
Aaron Durbin76c37002012-10-30 09:03:43 -0500625static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200626 .read_resources = noop_read_resources,
627 .set_resources = noop_set_resources,
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300628 .init = mp_cpu_bus_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500629};
630
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200631static void enable_dev(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500632{
Angel Pons1db5bc72020-01-15 00:49:03 +0100633 /* Set the operations if it is a special bus type. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500634 if (dev->path.type == DEVICE_PATH_DOMAIN) {
635 dev->ops = &pci_domain_ops;
636 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
637 dev->ops = &cpu_bus_ops;
638 }
639}
640
641struct chip_operations northbridge_intel_haswell_ops = {
Angel Pons7bbf45e2020-10-22 23:55:24 +0200642 CHIP_NAME("Intel Haswell integrated Northbridge")
Aaron Durbin76c37002012-10-30 09:03:43 -0500643 .enable_dev = enable_dev,
644};