blob: afb60380d0595e8bd07a2f09054d44b38bb201b9 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2014 Google Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015 */
16
17#include <console/console.h>
18#include <arch/acpi.h>
19#include <arch/io.h>
20#include <stdint.h>
21#include <delay.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
25#include <stdlib.h>
26#include <string.h>
27#include <cbmem.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070028#include <vendorcode/google/chromeos/chromeos.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070029#include <soc/cpu.h>
30#include <soc/iomap.h>
31#include <soc/pci_devs.h>
32#include <soc/ramstage.h>
33#include <soc/systemagent.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070034
Duncan Laurie84b9cf42014-07-31 10:46:57 -070035u8 systemagent_revision(void)
36{
37 return pci_read_config8(SA_DEV_ROOT, PCI_REVISION_ID);
38}
39
Elyes HAOUAS040aff22018-05-27 16:30:36 +020040static int get_pcie_bar(struct device *dev, unsigned int index, u32 *base,
41 u32 *len)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070042{
43 u32 pciexbar_reg;
44
45 *base = 0;
46 *len = 0;
47
48 pciexbar_reg = pci_read_config32(dev, index);
49
50 if (!(pciexbar_reg & (1 << 0)))
51 return 0;
52
53 switch ((pciexbar_reg >> 1) & 3) {
54 case 0: // 256MB
55 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|
56 (1 << 28));
57 *len = 256 * 1024 * 1024;
58 return 1;
59 case 1: // 128M
60 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|
61 (1 << 28)|(1 << 27));
62 *len = 128 * 1024 * 1024;
63 return 1;
64 case 2: // 64M
65 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|
66 (1 << 28)|(1 << 27)|(1 << 26));
67 *len = 64 * 1024 * 1024;
68 return 1;
69 }
70
71 return 0;
72}
73
Elyes HAOUAS040aff22018-05-27 16:30:36 +020074static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070075{
76 u32 bar;
77
78 bar = pci_read_config32(dev, index);
79
80 /* If not enabled don't report it. */
81 if (!(bar & 0x1))
82 return 0;
83
84 /* Knock down the enable bit. */
85 *base = bar & ~1;
86
87 return 1;
88}
89
90/* There are special BARs that actually are programmed in the MCHBAR. These
91 * Intel special features, but they do consume resources that need to be
92 * accounted for. */
Elyes HAOUAS040aff22018-05-27 16:30:36 +020093static int get_bar_in_mchbar(struct device *dev, unsigned int index, u32 *base,
Lee Leahy26b7cd02017-03-16 18:47:55 -070094 u32 *len)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070095{
96 u32 bar;
97
98 bar = MCHBAR32(index);
99
100 /* If not enabled don't report it. */
101 if (!(bar & 0x1))
102 return 0;
103
104 /* Knock down the enable bit. */
105 *base = bar & ~1;
106
107 return 1;
108}
109
110struct fixed_mmio_descriptor {
111 unsigned int index;
112 u32 size;
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200113 int (*get_resource)(struct device *dev, unsigned int index,
Lee Leahy26b7cd02017-03-16 18:47:55 -0700114 u32 *base, u32 *size);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700115 const char *description;
116};
117
118struct fixed_mmio_descriptor mc_fixed_resources[] = {
119 { PCIEXBAR, 0, get_pcie_bar, "PCIEXBAR" },
120 { MCHBAR, MCH_BASE_SIZE, get_bar, "MCHBAR" },
121 { DMIBAR, DMI_BASE_SIZE, get_bar, "DMIBAR" },
122 { EPBAR, EP_BASE_SIZE, get_bar, "EPBAR" },
123 { GDXCBAR, GDXC_BASE_SIZE, get_bar_in_mchbar, "GDXCBAR" },
124 { EDRAMBAR, EDRAM_BASE_SIZE, get_bar_in_mchbar, "EDRAMBAR" },
125};
126
127/*
128 * Add all known fixed MMIO ranges that hang off the host bridge/memory
129 * controller device.
130 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200131static void mc_add_fixed_mmio_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700132{
133 int i;
134
135 for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
136 u32 base;
137 u32 size;
138 struct resource *resource;
139 unsigned int index;
140
141 size = mc_fixed_resources[i].size;
142 index = mc_fixed_resources[i].index;
143 if (!mc_fixed_resources[i].get_resource(dev, index,
Lee Leahy26b7cd02017-03-16 18:47:55 -0700144 &base, &size))
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700145 continue;
146
147 resource = new_resource(dev, mc_fixed_resources[i].index);
148 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700149 IORESOURCE_STORED | IORESOURCE_RESERVE |
150 IORESOURCE_ASSIGNED;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700151 resource->base = base;
152 resource->size = size;
153 printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
154 __func__, mc_fixed_resources[i].description, index,
155 (unsigned long)base, (unsigned long)(base + size - 1));
156 }
157}
158
159/* Host Memory Map:
160 *
161 * +--------------------------+ TOUUD
162 * | |
163 * +--------------------------+ 4GiB
164 * | PCI Address Space |
165 * +--------------------------+ TOLUD (also maps into MC address space)
166 * | iGD |
167 * +--------------------------+ BDSM
168 * | GTT |
169 * +--------------------------+ BGSM
170 * | TSEG |
171 * +--------------------------+ TSEGMB
172 * | Usage DRAM |
173 * +--------------------------+ 0
174 *
175 * Some of the base registers above can be equal making the size of those
176 * regions 0. The reason is because the memory controller internally subtracts
177 * the base registers from each other to determine sizes of the regions. In
178 * other words, the memory map is in a fixed order no matter what.
179 */
180
181struct map_entry {
182 int reg;
183 int is_64_bit;
184 int is_limit;
185 const char *description;
186};
187
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200188static void read_map_entry(struct device *dev, struct map_entry *entry,
Lee Leahy26b7cd02017-03-16 18:47:55 -0700189 uint64_t *result)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700190{
191 uint64_t value;
192 uint64_t mask;
193
194 /* All registers are on a 1MiB granularity. */
195 mask = ((1ULL<<20)-1);
196 mask = ~mask;
197
198 value = 0;
199
200 if (entry->is_64_bit) {
201 value = pci_read_config32(dev, entry->reg + 4);
202 value <<= 32;
203 }
204
205 value |= pci_read_config32(dev, entry->reg);
206 value &= mask;
207
208 if (entry->is_limit)
209 value |= ~mask;
210
211 *result = value;
212}
213
214#define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
215 { \
216 .reg = reg_, \
217 .is_64_bit = is_64_, \
218 .is_limit = is_limit_, \
219 .description = desc_, \
220 }
221
222#define MAP_ENTRY_BASE_64(reg_, desc_) \
223 MAP_ENTRY(reg_, 1, 0, desc_)
224#define MAP_ENTRY_LIMIT_64(reg_, desc_) \
225 MAP_ENTRY(reg_, 1, 1, desc_)
226#define MAP_ENTRY_BASE_32(reg_, desc_) \
227 MAP_ENTRY(reg_, 0, 0, desc_)
228
229enum {
230 TOM_REG,
231 TOUUD_REG,
232 MESEG_BASE_REG,
233 MESEG_LIMIT_REG,
234 REMAP_BASE_REG,
235 REMAP_LIMIT_REG,
236 TOLUD_REG,
237 BGSM_REG,
238 BDSM_REG,
239 TSEG_REG,
240 // Must be last.
241 NUM_MAP_ENTRIES
242};
243
244static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
245 [TOM_REG] = MAP_ENTRY_BASE_64(TOM, "TOM"),
246 [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
247 [MESEG_BASE_REG] = MAP_ENTRY_BASE_64(MESEG_BASE, "MESEG_BASE"),
248 [MESEG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT, "MESEG_LIMIT"),
249 [REMAP_BASE_REG] = MAP_ENTRY_BASE_64(REMAPBASE, "REMAP_BASE"),
250 [REMAP_LIMIT_REG] = MAP_ENTRY_LIMIT_64(REMAPLIMIT, "REMAP_LIMIT"),
251 [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
252 [BDSM_REG] = MAP_ENTRY_BASE_32(BDSM, "BDSM"),
253 [BGSM_REG] = MAP_ENTRY_BASE_32(BGSM, "BGSM"),
254 [TSEG_REG] = MAP_ENTRY_BASE_32(TSEG, "TESGMB"),
255};
256
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200257static void mc_read_map_entries(struct device *dev, uint64_t *values)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700258{
259 int i;
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700260 for (i = 0; i < NUM_MAP_ENTRIES; i++)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700261 read_map_entry(dev, &memory_map[i], &values[i]);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700262}
263
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200264static void mc_report_map_entries(struct device *dev, uint64_t *values)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700265{
266 int i;
267 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
268 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
269 memory_map[i].description, values[i]);
270 }
271 /* One can validate the BDSM and BGSM against the GGC. */
272 printk(BIOS_DEBUG, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev, GGC));
273}
274
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200275static void mc_add_dram_resources(struct device *dev, int *resource_cnt)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700276{
277 unsigned long base_k, size_k;
278 unsigned long touud_k;
279 unsigned long index;
280 struct resource *resource;
281 uint64_t mc_values[NUM_MAP_ENTRIES];
Duncan Laurie61680272014-05-05 12:42:35 -0500282 unsigned long dpr_size = 0;
283 u32 dpr_reg;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700284
285 /* Read in the MAP registers and report their values. */
286 mc_read_map_entries(dev, &mc_values[0]);
287 mc_report_map_entries(dev, &mc_values[0]);
288
289 /*
Duncan Laurie61680272014-05-05 12:42:35 -0500290 * DMA Protected Range can be reserved below TSEG for PCODE patch
291 * or TXT/BootGuard related data. Rather than report a base address
292 * the DPR register reports the TOP of the region, which is the same
293 * as TSEG base. The region size is reported in MiB in bits 11:4.
294 */
295 dpr_reg = pci_read_config32(SA_DEV_ROOT, DPR);
296 if (dpr_reg & DPR_EPM) {
297 dpr_size = (dpr_reg & DPR_SIZE_MASK) << 16;
298 printk(BIOS_INFO, "DPR SIZE: 0x%lx\n", dpr_size);
299 }
300
301 /*
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700302 * These are the host memory ranges that should be added:
303 * - 0 -> 0xa0000: cacheable
304 * - 0xc0000 -> TSEG : cacheable
305 * - TESG -> BGSM: cacheable with standard MTRRs and reserved
306 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
307 * - 4GiB -> TOUUD: cacheable
308 *
309 * The default SMRAM space is reserved so that the range doesn't
310 * have to be saved during S3 Resume. Once marked reserved the OS
311 * cannot use the memory. This is a bit of an odd place to reserve
312 * the region, but the CPU devices don't have dev_ops->read_resources()
313 * called on them.
314 *
315 * The range 0xa0000 -> 0xc0000 does not have any resources
316 * associated with it to handle legacy VGA memory. If this range
317 * is not omitted the mtrr code will setup the area as cacheable
318 * causing VGA access to not work.
319 *
320 * The TSEG region is mapped as cacheable so that one can perform
321 * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
322 * precedence over the existing MTRRs covering this region.
323 *
324 * It should be noted that cacheable entry types need to be added in
325 * order. The reason is that the current MTRR code assumes this and
326 * falls over itself if it isn't.
327 *
328 * The resource index starts low and should not meet or exceed
329 * PCI_BASE_ADDRESS_0.
330 */
Matt DeVillier81a6f102018-02-19 17:33:48 -0600331 index = *resource_cnt;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700332
333 /* 0 - > 0xa0000 */
334 base_k = 0;
335 size_k = (0xa0000 >> 10) - base_k;
336 ram_resource(dev, index++, base_k, size_k);
337
Duncan Laurie61680272014-05-05 12:42:35 -0500338 /* 0xc0000 -> TSEG - DPR */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700339 base_k = 0xc0000 >> 10;
340 size_k = (unsigned long)(mc_values[TSEG_REG] >> 10) - base_k;
Duncan Laurie61680272014-05-05 12:42:35 -0500341 size_k -= dpr_size >> 10;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700342 ram_resource(dev, index++, base_k, size_k);
343
Duncan Laurie61680272014-05-05 12:42:35 -0500344 /* TSEG - DPR -> BGSM */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700345 resource = new_resource(dev, index++);
Duncan Laurie61680272014-05-05 12:42:35 -0500346 resource->base = mc_values[TSEG_REG] - dpr_size;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700347 resource->size = mc_values[BGSM_REG] - resource->base;
348 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700349 IORESOURCE_STORED | IORESOURCE_RESERVE |
350 IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700351
352 /* BGSM -> TOLUD */
353 resource = new_resource(dev, index++);
354 resource->base = mc_values[BGSM_REG];
355 resource->size = mc_values[TOLUD_REG] - resource->base;
356 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700357 IORESOURCE_STORED | IORESOURCE_RESERVE |
358 IORESOURCE_ASSIGNED;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700359
360 /* 4GiB -> TOUUD */
361 base_k = 4096 * 1024; /* 4GiB */
362 touud_k = mc_values[TOUUD_REG] >> 10;
363 size_k = touud_k - base_k;
364 if (touud_k > base_k)
365 ram_resource(dev, index++, base_k, size_k);
366
367 /* Reserve everything between A segment and 1MB:
368 *
369 * 0xa0000 - 0xbffff: legacy VGA
370 * 0xc0000 - 0xfffff: RAM
371 */
372 mmio_resource(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
373 reserved_ram_resource(dev, index++, (0xc0000 >> 10),
Lee Leahy26b7cd02017-03-16 18:47:55 -0700374 (0x100000 - 0xc0000) >> 10);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700375
376 chromeos_reserve_ram_oops(dev, index++);
Matt DeVillier81a6f102018-02-19 17:33:48 -0600377
378 *resource_cnt = index;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700379}
380
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200381static void systemagent_read_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700382{
Matt DeVillier81a6f102018-02-19 17:33:48 -0600383 int index = 0;
384 const bool vtd_capable =
385 !(pci_read_config32(dev, CAPID0_A) & VTD_DISABLE);
386
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700387 /* Read standard PCI resources. */
388 pci_dev_read_resources(dev);
389
390 /* Add all fixed MMIO resources. */
391 mc_add_fixed_mmio_resources(dev);
392
Matt DeVillier81a6f102018-02-19 17:33:48 -0600393 /* Add VT-d MMIO resources if capable */
394 if (vtd_capable) {
395 mmio_resource(dev, index++, GFXVT_BASE_ADDRESS / KiB,
396 GFXVT_BASE_SIZE / KiB);
397 mmio_resource(dev, index++, VTVC0_BASE_ADDRESS / KiB,
398 VTVC0_BASE_SIZE / KiB);
399 }
400
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700401 /* Calculate and add DRAM resources. */
Matt DeVillier81a6f102018-02-19 17:33:48 -0600402 mc_add_dram_resources(dev, &index);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700403}
404
405static void systemagent_init(struct device *dev)
406{
407 u8 bios_reset_cpl, pair;
408
409 /* Enable Power Aware Interrupt Routing */
410 pair = MCHBAR8(MCH_PAIR);
411 pair &= ~0x7; /* Clear 2:0 */
412 pair |= 0x4; /* Fixed Priority */
413 MCHBAR8(MCH_PAIR) = pair;
414
415 /*
416 * Set bits 0+1 of BIOS_RESET_CPL to indicate to the CPU
417 * that BIOS has initialized memory and power management
418 */
419 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
420 bios_reset_cpl |= 3;
421 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
422 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
423
424 /* Configure turbo power limits 1ms after reset complete bit */
425 mdelay(1);
426 set_power_limits(28);
427}
428
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700429static struct device_operations systemagent_ops = {
Marc Jonesa6354a12014-12-26 22:11:14 -0700430 .read_resources = &systemagent_read_resources,
431 .acpi_fill_ssdt_generator = &generate_cpu_entries,
432 .set_resources = &pci_dev_set_resources,
433 .enable_resources = &pci_dev_enable_resources,
434 .init = &systemagent_init,
Marc Jonesa6354a12014-12-26 22:11:14 -0700435 .ops_pci = &broadwell_pci_ops,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700436};
437
438static const unsigned short systemagent_ids[] = {
439 0x0a04, /* Haswell ULT */
440 0x1604, /* Broadwell-U/Y */
441 0x1610, /* Broadwell-H Desktop */
442 0x1614, /* Broadwell-H Mobile */
443 0
444};
445
446static const struct pci_driver systemagent_driver __pci_driver = {
447 .ops = &systemagent_ops,
448 .vendor = PCI_VENDOR_ID_INTEL,
449 .devices = systemagent_ids
450};