blob: b764aadb30d2d698173d44e722a09a8173bdea3b [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin76c37002012-10-30 09:03:43 -05004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050013 */
14
Tristan Corrickbc896cd2018-12-17 22:09:50 +130015#include <commonlib/helpers.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050016#include <console/console.h>
17#include <arch/acpi.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include <stdint.h>
19#include <delay.h>
20#include <cpu/intel/haswell/haswell.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050021#include <device/device.h>
22#include <device/pci.h>
Tristan Corrickbc896cd2018-12-17 22:09:50 +130023#include <device/pci_def.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050024#include <device/pci_ids.h>
Tristan Corrickbc896cd2018-12-17 22:09:50 +130025#include <device/pci_ops.h>
Aaron Durbin1fef1f52012-12-19 17:15:43 -060026#include <cpu/x86/smm.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050027#include <boot/tables.h>
Elyes HAOUASa1e22b82019-03-18 22:49:36 +010028
Aaron Durbin76c37002012-10-30 09:03:43 -050029#include "chip.h"
30#include "haswell.h"
31
Angel Pons1db5bc72020-01-15 00:49:03 +010032static int get_pcie_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -050033{
Angel Pons1db5bc72020-01-15 00:49:03 +010034 u32 pciexbar_reg, mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050035
36 *base = 0;
37 *len = 0;
38
Aaron Durbinc12ef972012-12-18 14:22:49 -060039 pciexbar_reg = pci_read_config32(dev, index);
Aaron Durbin76c37002012-10-30 09:03:43 -050040
41 if (!(pciexbar_reg & (1 << 0)))
42 return 0;
43
44 switch ((pciexbar_reg >> 1) & 3) {
Angel Pons1db5bc72020-01-15 00:49:03 +010045 case 0: /* 256MB */
Ryan Salsamendifa0725d2017-06-30 17:29:37 -070046 mask = (1UL << 31) | (1 << 30) | (1 << 29) | (1 << 28);
47 *base = pciexbar_reg & mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050048 *len = 256 * 1024 * 1024;
49 return 1;
Angel Pons1db5bc72020-01-15 00:49:03 +010050 case 1: /* 128M */
Ryan Salsamendifa0725d2017-06-30 17:29:37 -070051 mask = (1UL << 31) | (1 << 30) | (1 << 29) | (1 << 28);
52 mask |= (1 << 27);
53 *base = pciexbar_reg & mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050054 *len = 128 * 1024 * 1024;
55 return 1;
Angel Pons1db5bc72020-01-15 00:49:03 +010056 case 2: /* 64M */
Ryan Salsamendifa0725d2017-06-30 17:29:37 -070057 mask = (1UL << 31) | (1 << 30) | (1 << 29) | (1 << 28);
58 mask |= (1 << 27) | (1 << 26);
59 *base = pciexbar_reg & mask;
Aaron Durbin76c37002012-10-30 09:03:43 -050060 *len = 64 * 1024 * 1024;
61 return 1;
62 }
63
64 return 0;
65}
66
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +020067static void pci_domain_set_resources(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -050068{
Aaron Durbin76c37002012-10-30 09:03:43 -050069 assign_resources(dev->link_list);
Aaron Durbin76c37002012-10-30 09:03:43 -050070}
71
Tristan Corrickf3127d42018-10-31 02:25:54 +130072static const char *northbridge_acpi_name(const struct device *dev)
73{
74 if (dev->path.type == DEVICE_PATH_DOMAIN)
75 return "PCI0";
76
77 if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
78 return NULL;
79
80 switch (dev->path.pci.devfn) {
81 case PCI_DEVFN(0, 0):
82 return "MCHC";
83 }
84
85 return NULL;
86}
87
Angel Pons1db5bc72020-01-15 00:49:03 +010088/*
89 * TODO: We could determine how many PCIe busses we need in the bar.
90 * For now, that number is hardcoded to a max of 64.
91 */
Aaron Durbin76c37002012-10-30 09:03:43 -050092static struct device_operations pci_domain_ops = {
Angel Pons1db5bc72020-01-15 00:49:03 +010093 .read_resources = pci_domain_read_resources,
94 .set_resources = pci_domain_set_resources,
Angel Pons1db5bc72020-01-15 00:49:03 +010095 .scan_bus = pci_domain_scan_bus,
96 .acpi_name = northbridge_acpi_name,
Matt DeVillier85d98d92018-03-04 01:41:23 -060097 .write_acpi_tables = northbridge_write_acpi_tables,
Aaron Durbin76c37002012-10-30 09:03:43 -050098};
99
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200100static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -0500101{
Angel Pons1db5bc72020-01-15 00:49:03 +0100102 u32 bar = pci_read_config32(dev, index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500103
Angel Pons1db5bc72020-01-15 00:49:03 +0100104 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600105 if (!(bar & 0x1))
106 return 0;
Aaron Durbin76c37002012-10-30 09:03:43 -0500107
Angel Pons1db5bc72020-01-15 00:49:03 +0100108 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600109 *base = bar & ~1;
110
111 return 1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500112}
113
Angel Pons1db5bc72020-01-15 00:49:03 +0100114/*
115 * There are special BARs that actually are programmed in the MCHBAR. These Intel special
116 * features, but they do consume resources that need to be accounted for.
117 */
118static int get_bar_in_mchbar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Aaron Durbin76c37002012-10-30 09:03:43 -0500119{
Angel Pons1db5bc72020-01-15 00:49:03 +0100120 u32 bar = MCHBAR32(index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500121
Angel Pons1db5bc72020-01-15 00:49:03 +0100122 /* If not enabled don't report it */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600123 if (!(bar & 0x1))
124 return 0;
125
Angel Pons1db5bc72020-01-15 00:49:03 +0100126 /* Knock down the enable bit */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600127 *base = bar & ~1;
128
129 return 1;
130}
131
132struct fixed_mmio_descriptor {
133 unsigned int index;
134 u32 size;
Angel Pons1db5bc72020-01-15 00:49:03 +0100135 int (*get_resource)(struct device *dev, unsigned int index, u32 *base, u32 *size);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600136 const char *description;
137};
138
Angel Pons1db5bc72020-01-15 00:49:03 +0100139#define SIZE_KB(x) ((x) * 1024)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600140struct fixed_mmio_descriptor mc_fixed_resources[] = {
141 { PCIEXBAR, SIZE_KB(0), get_pcie_bar, "PCIEXBAR" },
142 { MCHBAR, SIZE_KB(32), get_bar, "MCHBAR" },
143 { DMIBAR, SIZE_KB(4), get_bar, "DMIBAR" },
144 { EPBAR, SIZE_KB(4), get_bar, "EPBAR" },
Angel Pons1db5bc72020-01-15 00:49:03 +0100145 { GDXCBAR, SIZE_KB(4), get_bar_in_mchbar, "GDXCBAR" },
146 { EDRAMBAR, SIZE_KB(16), get_bar_in_mchbar, "EDRAMBAR" },
Aaron Durbinc12ef972012-12-18 14:22:49 -0600147};
148#undef SIZE_KB
149
Angel Pons1db5bc72020-01-15 00:49:03 +0100150/* Add all known fixed MMIO ranges that hang off the host bridge/memory controller device. */
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200151static void mc_add_fixed_mmio_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600152{
153 int i;
154
155 for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
156 u32 base;
157 u32 size;
158 struct resource *resource;
159 unsigned int index;
160
161 size = mc_fixed_resources[i].size;
162 index = mc_fixed_resources[i].index;
Angel Pons1db5bc72020-01-15 00:49:03 +0100163 if (!mc_fixed_resources[i].get_resource(dev, index, &base, &size))
Aaron Durbinc12ef972012-12-18 14:22:49 -0600164 continue;
165
166 resource = new_resource(dev, mc_fixed_resources[i].index);
Angel Pons1db5bc72020-01-15 00:49:03 +0100167 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
168 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
169
Aaron Durbinc12ef972012-12-18 14:22:49 -0600170 resource->base = base;
171 resource->size = size;
172 printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
173 __func__, mc_fixed_resources[i].description, index,
174 (unsigned long)base, (unsigned long)(base + size - 1));
175 }
176}
177
178/* Host Memory Map:
179 *
180 * +--------------------------+ TOUUD
181 * | |
182 * +--------------------------+ 4GiB
183 * | PCI Address Space |
184 * +--------------------------+ TOLUD (also maps into MC address space)
185 * | iGD |
186 * +--------------------------+ BDSM
187 * | GTT |
188 * +--------------------------+ BGSM
189 * | TSEG |
190 * +--------------------------+ TSEGMB
191 * | Usage DRAM |
192 * +--------------------------+ 0
193 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100194 * Some of the base registers above can be equal, making the size of the regions within 0.
195 * This is because the memory controller internally subtracts the base registers from each
196 * other to determine sizes of the regions. In other words, the memory map regions are always
197 * in a fixed order, no matter what sizes they have.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600198 */
199
200struct map_entry {
201 int reg;
202 int is_64_bit;
203 int is_limit;
204 const char *description;
205};
206
Angel Pons1db5bc72020-01-15 00:49:03 +0100207static void read_map_entry(struct device *dev, struct map_entry *entry, uint64_t *result)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600208{
209 uint64_t value;
210 uint64_t mask;
211
Angel Pons1db5bc72020-01-15 00:49:03 +0100212 /* All registers have a 1MiB granularity */
213 mask = ((1ULL << 20) - 1);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600214 mask = ~mask;
215
216 value = 0;
217
218 if (entry->is_64_bit) {
219 value = pci_read_config32(dev, entry->reg + 4);
220 value <<= 32;
Aaron Durbin76c37002012-10-30 09:03:43 -0500221 }
222
Aaron Durbinc12ef972012-12-18 14:22:49 -0600223 value |= pci_read_config32(dev, entry->reg);
224 value &= mask;
225
226 if (entry->is_limit)
227 value |= ~mask;
228
229 *result = value;
230}
231
232#define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
233 { \
234 .reg = reg_, \
235 .is_64_bit = is_64_, \
236 .is_limit = is_limit_, \
237 .description = desc_, \
238 }
239
Angel Pons1db5bc72020-01-15 00:49:03 +0100240#define MAP_ENTRY_BASE_32(reg_, desc_) MAP_ENTRY(reg_, 0, 0, desc_)
241#define MAP_ENTRY_BASE_64(reg_, desc_) MAP_ENTRY(reg_, 1, 0, desc_)
242#define MAP_ENTRY_LIMIT_64(reg_, desc_) MAP_ENTRY(reg_, 1, 1, desc_)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600243
244enum {
245 TOM_REG,
246 TOUUD_REG,
247 MESEG_BASE_REG,
248 MESEG_LIMIT_REG,
249 REMAP_BASE_REG,
250 REMAP_LIMIT_REG,
251 TOLUD_REG,
252 BGSM_REG,
253 BDSM_REG,
254 TSEG_REG,
Angel Pons1db5bc72020-01-15 00:49:03 +0100255 /* Must be last */
256 NUM_MAP_ENTRIES,
Aaron Durbinc12ef972012-12-18 14:22:49 -0600257};
258
259static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
Angel Pons1db5bc72020-01-15 00:49:03 +0100260 [TOM_REG] = MAP_ENTRY_BASE_64(TOM, "TOM"),
261 [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
262 [MESEG_BASE_REG] = MAP_ENTRY_BASE_64(MESEG_BASE, "MESEG_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600263 [MESEG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT, "MESEG_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100264 [REMAP_BASE_REG] = MAP_ENTRY_BASE_64(REMAPBASE, "REMAP_BASE"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600265 [REMAP_LIMIT_REG] = MAP_ENTRY_LIMIT_64(REMAPLIMIT, "REMAP_LIMIT"),
Angel Pons1db5bc72020-01-15 00:49:03 +0100266 [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
267 [BDSM_REG] = MAP_ENTRY_BASE_32(BDSM, "BDSM"),
268 [BGSM_REG] = MAP_ENTRY_BASE_32(BGSM, "BGSM"),
269 [TSEG_REG] = MAP_ENTRY_BASE_32(TSEG, "TESGMB"),
Aaron Durbinc12ef972012-12-18 14:22:49 -0600270};
271
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200272static void mc_read_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 read_map_entry(dev, &memory_map[i], &values[i]);
277 }
278}
279
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200280static void mc_report_map_entries(struct device *dev, uint64_t *values)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600281{
282 int i;
283 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
284 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
285 memory_map[i].description, values[i]);
286 }
Angel Pons1db5bc72020-01-15 00:49:03 +0100287 /* One can validate the BDSM and BGSM against the GGC */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600288 printk(BIOS_DEBUG, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev, GGC));
289}
290
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200291static void mc_add_dram_resources(struct device *dev, int *resource_cnt)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600292{
Angel Pons1db5bc72020-01-15 00:49:03 +0100293 unsigned long base_k, size_k, touud_k, index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600294 struct resource *resource;
295 uint64_t mc_values[NUM_MAP_ENTRIES];
296
Angel Pons1db5bc72020-01-15 00:49:03 +0100297 /* Read in the MAP registers and report their values */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600298 mc_read_map_entries(dev, &mc_values[0]);
299 mc_report_map_entries(dev, &mc_values[0]);
300
301 /*
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600302 * These are the host memory ranges that should be added:
Angel Pons1db5bc72020-01-15 00:49:03 +0100303 * - 0 -> 0xa0000: cacheable
304 * - 0xc0000 -> TSEG: cacheable
305 * - TSEG -> BGSM: cacheable with standard MTRRs and reserved
306 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
307 * - 4GiB -> TOUUD: cacheable
Aaron Durbinc12ef972012-12-18 14:22:49 -0600308 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100309 * The default SMRAM space is reserved so that the range doesn't have to be saved
310 * during S3 Resume. Once marked reserved the OS cannot use the memory. This is a
311 * bit of an odd place to reserve the region, but the CPU devices don't have
312 * dev_ops->read_resources() called on them.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600313 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100314 * The range 0xa0000 -> 0xc0000 does not have any resources associated with it to
315 * handle legacy VGA memory. If this range is not omitted the mtrr code will setup
316 * the area as cacheable, causing VGA access to not work.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600317 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100318 * The TSEG region is mapped as cacheable so that one can perform SMRAM relocation
319 * faster. Once the SMRR is enabled, the SMRR takes precedence over the existing
320 * MTRRs covering this region.
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600321 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100322 * It should be noted that cacheable entry types need to be added in order. The reason
323 * is that the current MTRR code assumes this and falls over itself if it isn't.
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600324 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100325 * The resource index starts low and should not meet or exceed PCI_BASE_ADDRESS_0.
Aaron Durbinc12ef972012-12-18 14:22:49 -0600326 */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600327 index = *resource_cnt;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600328
Aaron Durbin6a360042014-02-13 10:30:42 -0600329 /* 0 - > 0xa0000 */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600330 base_k = 0;
Aaron Durbin1fef1f52012-12-19 17:15:43 -0600331 size_k = (0xa0000 >> 10) - base_k;
332 ram_resource(dev, index++, base_k, size_k);
333
334 /* 0xc0000 -> TSEG */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600335 base_k = 0xc0000 >> 10;
336 size_k = (unsigned long)(mc_values[TSEG_REG] >> 10) - base_k;
337 ram_resource(dev, index++, base_k, size_k);
338
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600339 /* TSEG -> BGSM */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600340 resource = new_resource(dev, index++);
341 resource->base = mc_values[TSEG_REG];
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600342 resource->size = mc_values[BGSM_REG] - resource->base;
Angel Pons1db5bc72020-01-15 00:49:03 +0100343 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
344 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE;
Aaron Durbine6c3b1d2012-12-21 21:22:07 -0600345
Angel Pons1db5bc72020-01-15 00:49:03 +0100346 /* BGSM -> TOLUD. If the IGD is disabled, BGSM can equal TOLUD. */
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300347 if (mc_values[BGSM_REG] != mc_values[TOLUD_REG]) {
348 resource = new_resource(dev, index++);
349 resource->base = mc_values[BGSM_REG];
350 resource->size = mc_values[TOLUD_REG] - resource->base;
Angel Pons1db5bc72020-01-15 00:49:03 +0100351 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
352 IORESOURCE_RESERVE | IORESOURCE_ASSIGNED;
Tristan Corrickc5d367b2018-12-17 22:10:07 +1300353 }
Aaron Durbinc12ef972012-12-18 14:22:49 -0600354
355 /* 4GiB -> TOUUD */
356 base_k = 4096 * 1024; /* 4GiB */
Aaron Durbin27435d32013-06-03 09:46:56 -0500357 touud_k = mc_values[TOUUD_REG] >> 10;
358 size_k = touud_k - base_k;
359 if (touud_k > base_k)
Aaron Durbin5c66f082013-01-08 10:10:33 -0600360 ram_resource(dev, index++, base_k, size_k);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600361
Aaron Durbinc9650762013-03-22 22:03:09 -0500362 /* Reserve everything between A segment and 1MB:
363 *
Angel Pons1db5bc72020-01-15 00:49:03 +0100364 * 0xa0000 - 0xbffff: Legacy VGA
Aaron Durbinc9650762013-03-22 22:03:09 -0500365 * 0xc0000 - 0xfffff: RAM
366 */
367 mmio_resource(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
Angel Pons1db5bc72020-01-15 00:49:03 +0100368 reserved_ram_resource(dev, index++, (0xc0000 >> 10), (0x100000 - 0xc0000) >> 10);
369
Julius Wernercd49cce2019-03-05 16:53:33 -0800370#if CONFIG(CHROMEOS_RAMOOPS)
Aaron Durbinc9650762013-03-22 22:03:09 -0500371 reserved_ram_resource(dev, index++,
372 CONFIG_CHROMEOS_RAMOOPS_RAM_START >> 10,
Angel Pons1db5bc72020-01-15 00:49:03 +0100373 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE >> 10);
Aaron Durbinc12ef972012-12-18 14:22:49 -0600374#endif
Matt DeVilliera51e3792018-03-04 01:44:15 -0600375 *resource_cnt = index;
Aaron Durbinc12ef972012-12-18 14:22:49 -0600376}
377
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200378static void mc_read_resources(struct device *dev)
Aaron Durbinc12ef972012-12-18 14:22:49 -0600379{
Matt DeVilliera51e3792018-03-04 01:44:15 -0600380 int index = 0;
Angel Pons1db5bc72020-01-15 00:49:03 +0100381 const bool vtd_capable = !(pci_read_config32(dev, CAPID0_A) & VTD_DISABLE);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600382
Angel Pons1db5bc72020-01-15 00:49:03 +0100383 /* Read standard PCI resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600384 pci_dev_read_resources(dev);
385
Angel Pons1db5bc72020-01-15 00:49:03 +0100386 /* Add all fixed MMIO resources */
Aaron Durbinc12ef972012-12-18 14:22:49 -0600387 mc_add_fixed_mmio_resources(dev);
388
Angel Pons1db5bc72020-01-15 00:49:03 +0100389 /* Add VT-d MMIO resources, if capable */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600390 if (vtd_capable) {
Angel Pons1db5bc72020-01-15 00:49:03 +0100391 mmio_resource(dev, index++, GFXVT_BASE_ADDRESS / KiB, GFXVT_BASE_SIZE / KiB);
392 mmio_resource(dev, index++, VTVC0_BASE_ADDRESS / KiB, VTVC0_BASE_SIZE / KiB);
Matt DeVilliera51e3792018-03-04 01:44:15 -0600393 }
394
Angel Pons1db5bc72020-01-15 00:49:03 +0100395 /* Calculate and add DRAM resources */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600396 mc_add_dram_resources(dev, &index);
Aaron Durbin76c37002012-10-30 09:03:43 -0500397}
398
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300399/*
Angel Pons1db5bc72020-01-15 00:49:03 +0100400 * The Mini-HD audio device is disabled whenever the IGD is. This is because it provides
401 * audio over the integrated graphics port(s), which requires the IGD to be functional.
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300402 */
403static void disable_devices(void)
404{
405 static const struct {
406 const unsigned int devfn;
407 const u32 mask;
408 const char *const name;
409 } nb_devs[] = {
410 { PCI_DEVFN(1, 2), DEVEN_D1F2EN, "PEG12" },
411 { PCI_DEVFN(1, 1), DEVEN_D1F1EN, "PEG11" },
412 { PCI_DEVFN(1, 0), DEVEN_D1F0EN, "PEG10" },
413 { PCI_DEVFN(2, 0), DEVEN_D2EN | DEVEN_D3EN, "IGD" },
414 { PCI_DEVFN(3, 0), DEVEN_D3EN, "Mini-HD audio" },
415 { PCI_DEVFN(4, 0), DEVEN_D4EN, "\"device 4\"" },
416 { PCI_DEVFN(7, 0), DEVEN_D7EN, "\"device 7\"" },
417 };
418
Angel Pons1db5bc72020-01-15 00:49:03 +0100419 struct device *host_dev = pcidev_on_root(0, 0);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300420 u32 deven;
421 size_t i;
422
423 if (!host_dev)
424 return;
425
426 deven = pci_read_config32(host_dev, DEVEN);
427
428 for (i = 0; i < ARRAY_SIZE(nb_devs); i++) {
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300429 struct device *dev = pcidev_path_on_root(nb_devs[i].devfn);
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300430 if (!dev || !dev->enabled) {
431 printk(BIOS_DEBUG, "Disabling %s.\n", nb_devs[i].name);
432 deven &= ~nb_devs[i].mask;
433 }
434 }
435
436 pci_write_config32(host_dev, DEVEN, deven);
437}
438
Aaron Durbin76c37002012-10-30 09:03:43 -0500439static void northbridge_init(struct device *dev)
440{
Duncan Lauriec70353f2013-06-28 14:40:38 -0700441 u8 bios_reset_cpl, pair;
Aaron Durbin76c37002012-10-30 09:03:43 -0500442
Angel Pons1db5bc72020-01-15 00:49:03 +0100443 /* Enable Power Aware Interrupt Routing. */
444 pair = MCHBAR8(INTRDIRCTL);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700445 pair &= ~0x7; /* Clear 2:0 */
446 pair |= 0x4; /* Fixed Priority */
Angel Pons1db5bc72020-01-15 00:49:03 +0100447 MCHBAR8(INTRDIRCTL) = pair;
Aaron Durbin76c37002012-10-30 09:03:43 -0500448
Tristan Corrickbc896cd2018-12-17 22:09:50 +1300449 disable_devices();
450
Aaron Durbin76c37002012-10-30 09:03:43 -0500451 /*
Angel Pons1db5bc72020-01-15 00:49:03 +0100452 * Set bits 0 + 1 of BIOS_RESET_CPL to indicate to the CPU
453 * that BIOS has initialized memory and power management.
Aaron Durbin76c37002012-10-30 09:03:43 -0500454 */
455 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
Duncan Lauriec70353f2013-06-28 14:40:38 -0700456 bios_reset_cpl |= 3;
Aaron Durbin76c37002012-10-30 09:03:43 -0500457 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
458 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
459
Angel Pons1db5bc72020-01-15 00:49:03 +0100460 /* Configure turbo power limits 1ms after reset complete bit. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500461 mdelay(1);
462 set_power_limits(28);
463
Angel Pons1db5bc72020-01-15 00:49:03 +0100464 /* Set here before graphics PM init. */
465 MCHBAR32(MMIO_PAVP_MSG) = 0x00100001;
Aaron Durbin76c37002012-10-30 09:03:43 -0500466}
467
Aaron Durbin76c37002012-10-30 09:03:43 -0500468static struct pci_operations intel_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530469 .set_subsystem = pci_dev_set_subsystem,
Aaron Durbin76c37002012-10-30 09:03:43 -0500470};
471
472static struct device_operations mc_ops = {
Nico Huber68680dd2020-03-31 17:34:52 +0200473 .read_resources = mc_read_resources,
474 .set_resources = pci_dev_set_resources,
475 .enable_resources = pci_dev_enable_resources,
476 .init = northbridge_init,
477 .acpi_fill_ssdt = generate_cpu_entries,
Nico Huber68680dd2020-03-31 17:34:52 +0200478 .ops_pci = &intel_pci_ops,
Aaron Durbin76c37002012-10-30 09:03:43 -0500479};
480
Tristan Corrickd3856242018-11-01 03:03:29 +1300481static const unsigned short mc_pci_device_ids[] = {
482 0x0c00, /* Desktop */
483 0x0c04, /* Mobile */
484 0x0a04, /* ULT */
Iru Cai0766c982018-12-17 13:21:36 +0800485 0x0c08, /* Server */
Tristan Corrickd3856242018-11-01 03:03:29 +1300486 0
Tristan Corrick48170122018-10-31 02:21:41 +1300487};
488
Tristan Corrickd3856242018-11-01 03:03:29 +1300489static const struct pci_driver mc_driver_hsw __pci_driver = {
490 .ops = &mc_ops,
491 .vendor = PCI_VENDOR_ID_INTEL,
492 .devices = mc_pci_device_ids,
Duncan Lauriedf7be712012-12-17 11:22:57 -0800493};
494
Aaron Durbin76c37002012-10-30 09:03:43 -0500495static struct device_operations cpu_bus_ops = {
Edward O'Callaghan9f744622014-10-31 08:12:34 +1100496 .read_resources = DEVICE_NOOP,
497 .set_resources = DEVICE_NOOP,
498 .enable_resources = DEVICE_NOOP,
Kyösti Mälkkib3267e02019-08-13 16:44:04 +0300499 .init = mp_cpu_bus_init,
Aaron Durbin76c37002012-10-30 09:03:43 -0500500};
501
Elyes HAOUAS77f7a6e2018-05-09 17:47:59 +0200502static void enable_dev(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500503{
Angel Pons1db5bc72020-01-15 00:49:03 +0100504 /* Set the operations if it is a special bus type. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500505 if (dev->path.type == DEVICE_PATH_DOMAIN) {
506 dev->ops = &pci_domain_ops;
507 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
508 dev->ops = &cpu_bus_ops;
509 }
510}
511
512struct chip_operations northbridge_intel_haswell_ops = {
513 CHIP_NAME("Intel i7 (Haswell) integrated Northbridge")
514 .enable_dev = enable_dev,
515};