| 1 |
|
| 2 |
|
| 3 |
#include <arch/vga.h> |
| 4 |
#include <cbmem.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <cpu/x86/lapic_def.h> |
| 7 |
#include <cpu/x86/mtrr.h> |
| 8 |
#include <device/pci.h> |
| 9 |
#include <device/pci_ids.h> |
| 10 |
#include <intelblocks/msr.h> |
| 11 |
#include <soc/acpi.h> |
| 12 |
#include <soc/chip_common.h> |
| 13 |
#include <soc/iomap.h> |
| 14 |
#include <soc/pci_devs.h> |
| 15 |
#include <soc/ramstage.h> |
| 16 |
#include <soc/util.h> |
| 17 |
#include <fsp/util.h> |
| 18 |
#include <security/intel/txt/txt_platform.h> |
| 19 |
#include <security/intel/txt/txt.h> |
| 20 |
#include <soc/config.h> |
| 21 |
#include <soc/numa.h> |
| 22 |
#include <soc/soc_util.h> |
| 23 |
#include <stdint.h> |
| 24 |
|
| 25 |
struct proximity_domains pds = { |
| 26 |
.num_pds = 0, |
| 27 |
.pds = NULL, |
| 28 |
}; |
| 29 |
|
| 30 |
struct map_entry { |
| 31 |
uint32_t reg; |
| 32 |
int is_64_bit; |
| 33 |
int is_limit; |
| 34 |
int mask_bits; |
| 35 |
const char *description; |
| 36 |
}; |
| 37 |
|
| 38 |
enum { |
| 39 |
TOHM_REG, |
| 40 |
MMIOL_REG, |
| 41 |
MMCFG_BASE_REG, |
| 42 |
MMCFG_LIMIT_REG, |
| 43 |
TOLM_REG, |
| 44 |
|
| 45 |
NCMEM_BASE_REG, |
| 46 |
NCMEM_LIMIT_REG, |
| 47 |
ME_BASE_REG, |
| 48 |
ME_LIMIT_REG, |
| 49 |
TSEG_BASE_REG, |
| 50 |
TSEG_LIMIT_REG, |
| 51 |
VTDBAR_REG, |
| 52 |
|
| 53 |
NUM_MAP_ENTRIES |
| 54 |
}; |
| 55 |
|
| 56 |
size_t vtd_probe_bar_size(struct device *dev) |
| 57 |
{ |
| 58 |
uint32_t id = pci_read_config32(dev, PCI_VENDOR_ID); |
| 59 |
assert((id == (PCI_VID_INTEL | (MMAP_VTD_CFG_REG_DEVID << 16))) || |
| 60 |
(id == (PCI_VID_INTEL | (MMAP_VTD_STACK_CFG_REG_DEVID << 16)))); |
| 61 |
|
| 62 |
uint32_t val = pci_read_config32(dev, VTD_BAR_CSR); |
| 63 |
pci_write_config32(dev, VTD_BAR_CSR, (uint32_t)(-4 * KiB)); |
| 64 |
size_t size = (~(pci_read_config32(dev, VTD_BAR_CSR) & ((uint32_t)(-4 * KiB)))) + 1; |
| 65 |
assert(size != 0); |
| 66 |
pci_write_config32(dev, VTD_BAR_CSR, val); |
| 67 |
|
| 68 |
return size; |
| 69 |
} |
| 70 |
|
| 71 |
static struct map_entry memory_map[NUM_MAP_ENTRIES] = { |
| 72 |
[TOHM_REG] = MAP_ENTRY_LIMIT_64(VTD_TOHM_CSR, 26, "TOHM"), |
| 73 |
[MMIOL_REG] = MAP_ENTRY_BASE_32(VTD_MMIOL_CSR, "MMIOL"), |
| 74 |
[MMCFG_BASE_REG] = MAP_ENTRY_BASE_64(VTD_MMCFG_BASE_CSR, "MMCFG_BASE"), |
| 75 |
[MMCFG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_MMCFG_LIMIT_CSR, 26, "MMCFG_LIMIT"), |
| 76 |
[TOLM_REG] = MAP_ENTRY_LIMIT_32(VTD_TOLM_CSR, 26, "TOLM"), |
| 77 |
#if CONFIG(SOC_INTEL_HAS_NCMEM) |
| 78 |
[NCMEM_BASE_REG] = MAP_ENTRY_BASE_64(VTD_NCMEM_BASE_CSR, "NCMEM_BASE"), |
| 79 |
[NCMEM_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_NCMEM_LIMIT_CSR, 19, "NCMEM_LIMIT"), |
| 80 |
#else |
| 81 |
[ME_BASE_REG] = MAP_ENTRY_BASE_64(VTD_ME_BASE_CSR, "ME_BASE"), |
| 82 |
[ME_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_ME_LIMIT_CSR, 19, "ME_LIMIT"), |
| 83 |
#endif |
| 84 |
[TSEG_BASE_REG] = MAP_ENTRY_BASE_32(VTD_TSEG_BASE_CSR, "TSEGMB_BASE"), |
| 85 |
[TSEG_LIMIT_REG] = MAP_ENTRY_LIMIT_32(VTD_TSEG_LIMIT_CSR, 20, "TSEGMB_LIMIT"), |
| 86 |
[VTDBAR_REG] = MAP_ENTRY_BASE_32(VTD_BAR_CSR, "VTD_BAR"), |
| 87 |
}; |
| 88 |
|
| 89 |
static void read_map_entry(struct device *dev, struct map_entry *entry, |
| 90 |
uint64_t *result) |
| 91 |
{ |
| 92 |
uint64_t value; |
| 93 |
uint64_t mask; |
| 94 |
|
| 95 |
if (!entry->reg) { |
| 96 |
*result = 0; |
| 97 |
return; |
| 98 |
} |
| 99 |
if (entry->reg == VTD_BAR_CSR && !(pci_read_config32(dev, entry->reg) & 1)) { |
| 100 |
|
| 101 |
*result = 0; |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
mask = ((1ULL << entry->mask_bits) - 1); |
| 106 |
mask = ~mask; |
| 107 |
|
| 108 |
value = 0; |
| 109 |
|
| 110 |
if (entry->is_64_bit) { |
| 111 |
value = pci_read_config32(dev, entry->reg + sizeof(uint32_t)); |
| 112 |
value <<= 32; |
| 113 |
} |
| 114 |
|
| 115 |
value |= (uint64_t)pci_read_config32(dev, entry->reg); |
| 116 |
value &= mask; |
| 117 |
|
| 118 |
if (entry->is_limit) |
| 119 |
value |= ~mask; |
| 120 |
|
| 121 |
*result = value; |
| 122 |
} |
| 123 |
|
| 124 |
static void mc_read_map_entries(struct device *dev, uint64_t *values) |
| 125 |
{ |
| 126 |
int i; |
| 127 |
for (i = 0; i < NUM_MAP_ENTRIES; i++) |
| 128 |
read_map_entry(dev, &memory_map[i], &values[i]); |
| 129 |
} |
| 130 |
|
| 131 |
static void mc_report_map_entries(struct device *dev, uint64_t *values) |
| 132 |
{ |
| 133 |
int i; |
| 134 |
for (i = 0; i < NUM_MAP_ENTRIES; i++) { |
| 135 |
if (!memory_map[i].description) |
| 136 |
continue; |
| 137 |
|
| 138 |
printk(BIOS_DEBUG, "%s: MC MAP: %s: 0x%llx\n", |
| 139 |
dev_path(dev), memory_map[i].description, values[i]); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static void configure_dpr(struct device *dev) |
| 144 |
{ |
| 145 |
const uintptr_t cbmem_top_mb = ALIGN_UP(cbmem_top(), MiB) / MiB; |
| 146 |
union dpr_register dpr = { .raw = pci_read_config32(dev, VTD_LTDPR) }; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
dpr.lock = 1; |
| 152 |
dpr.epm = 1; |
| 153 |
dpr.size = dpr.top - cbmem_top_mb; |
| 154 |
pci_write_config32(dev, VTD_LTDPR, dpr.raw); |
| 155 |
} |
| 156 |
|
| 157 |
#define MC_DRAM_RESOURCE_MMIO_HIGH 0x1000 |
| 158 |
#define MC_DRAM_RESOURCE_ANON_START 0x1001 |
| 159 |
|
| 160 |
__weak unsigned int get_prmrr_count(void) |
| 161 |
{ |
| 162 |
return 0x0; |
| 163 |
} |
| 164 |
|
| 165 |
static bool get_prmrr_region(unsigned int msr_addr, uint64_t *base, uint64_t *size) |
| 166 |
{ |
| 167 |
|
| 168 |
msr_t msr1 = rdmsr(MTRR_CAP_MSR); |
| 169 |
if (!(msr1.lo & MTRR_CAP_PRMRR)) { |
| 170 |
printk(BIOS_ERR, "%s(): PRMRR is not supported.\n", __func__); |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
*base = msr_read(msr_addr) & ~((1 << RANGE_SHIFT) - 1); |
| 176 |
|
| 177 |
uint64_t mask = msr_read(MSR_PRMRR_PHYS_MASK); |
| 178 |
*size = calculate_var_mtrr_size(mask); |
| 179 |
|
| 180 |
return (*base && *size); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
static void mc_add_dram_resources(struct device *dev, int *res_count) |
| 245 |
{ |
| 246 |
const struct resource *res; |
| 247 |
uint64_t mc_values[NUM_MAP_ENTRIES]; |
| 248 |
uint64_t top_of_ram; |
| 249 |
int index = *res_count; |
| 250 |
struct range_entry fsp_mem; |
| 251 |
|
| 252 |
|
| 253 |
mc_read_map_entries(dev, &mc_values[0]); |
| 254 |
mc_report_map_entries(dev, &mc_values[0]); |
| 255 |
|
| 256 |
if (mc_values[VTDBAR_REG]) { |
| 257 |
res = mmio_range(dev, VTD_BAR_CSR, mc_values[VTDBAR_REG], |
| 258 |
vtd_probe_bar_size(dev)); |
| 259 |
LOG_RESOURCE("vtd_bar", dev, res); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
if (dev->upstream->secondary != 0 || dev->upstream->segment_group != 0) |
| 264 |
return; |
| 265 |
|
| 266 |
|
| 267 |
res = ram_from_to(dev, index++, 0, 0xa0000); |
| 268 |
LOG_RESOURCE("legacy_ram", dev, res); |
| 269 |
|
| 270 |
|
| 271 |
fsp_find_reserved_memory(&fsp_mem); |
| 272 |
top_of_ram = range_entry_base(&fsp_mem); |
| 273 |
res = ram_from_to(dev, index++, 1 * MiB, top_of_ram); |
| 274 |
LOG_RESOURCE("low_ram", dev, res); |
| 275 |
|
| 276 |
|
| 277 |
res = ram_from_to(dev, index++, top_of_ram, cbmem_top()); |
| 278 |
LOG_RESOURCE("cbmem_ram", dev, res); |
| 279 |
|
| 280 |
|
| 281 |
union dpr_register dpr = { .raw = pci_read_config32(dev, VTD_LTDPR) }; |
| 282 |
if (dpr.size) { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
res = reserved_ram_from_to(dev, index++, cbmem_top(), |
| 289 |
(dpr.top - dpr.size) * MiB); |
| 290 |
LOG_RESOURCE("unused_dram", dev, res); |
| 291 |
|
| 292 |
|
| 293 |
res = reserved_ram_from_to(dev, index++, (dpr.top - dpr.size) * MiB, |
| 294 |
dpr.top * MiB); |
| 295 |
LOG_RESOURCE("dpr", dev, res); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
res = reserved_ram_from_to(dev, index++, mc_values[TSEG_BASE_REG], |
| 300 |
mc_values[TSEG_LIMIT_REG] + 1); |
| 301 |
LOG_RESOURCE("mmio_tseg", dev, res); |
| 302 |
|
| 303 |
|
| 304 |
res = reserved_ram_from_to(dev, index++, mc_values[TSEG_LIMIT_REG] + 1, |
| 305 |
mc_values[TOLM_REG]); |
| 306 |
LOG_RESOURCE("mmio_tolm", dev, res); |
| 307 |
|
| 308 |
|
| 309 |
const struct SystemMemoryMapHob *mm = get_system_memory_map(); |
| 310 |
|
| 311 |
for (int i = 0; i < mm->numberEntries; i++) { |
| 312 |
const struct SystemMemoryMapElement *e = &mm->Element[i]; |
| 313 |
uint64_t addr = ((uint64_t)e->BaseAddress << MEM_ADDR_64MB_SHIFT_BITS); |
| 314 |
uint64_t size = ((uint64_t)e->ElementSize << MEM_ADDR_64MB_SHIFT_BITS); |
| 315 |
if (addr < 4ULL * GiB) |
| 316 |
continue; |
| 317 |
if (!is_memtype_processor_attached(e->Type)) |
| 318 |
continue; |
| 319 |
if (is_memtype_reserved(e->Type)) |
| 320 |
continue; |
| 321 |
|
| 322 |
res = ram_range(dev, index++, addr, size); |
| 323 |
LOG_RESOURCE("high_ram", dev, res); |
| 324 |
} |
| 325 |
|
| 326 |
uint64_t prmrr_base, prmrr_size; |
| 327 |
for (unsigned int i = 0; i < get_prmrr_count(); i++) { |
| 328 |
if (get_prmrr_region(MSR_PRMRR_BASE(i), &prmrr_base, &prmrr_size)) { |
| 329 |
res = reserved_ram_range(dev, index++, prmrr_base, prmrr_size); |
| 330 |
LOG_RESOURCE("prmrr", dev, res); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if (CONFIG(SOC_INTEL_HAS_CXL)) { |
| 335 |
|
| 336 |
uint8_t i; |
| 337 |
for (i = 0; i < pds.num_pds; i++) { |
| 338 |
if (pds.pds[i].pd_type != PD_TYPE_GENERIC_INITIATOR) |
| 339 |
continue; |
| 340 |
|
| 341 |
unsigned long flags = IORESOURCE_CACHEABLE; |
| 342 |
int cxl_mode = get_cxl_mode(); |
| 343 |
if (cxl_mode == XEONSP_CXL_SP_MEM) |
| 344 |
flags |= IORESOURCE_SOFT_RESERVE; |
| 345 |
else |
| 346 |
flags |= IORESOURCE_STORED; |
| 347 |
|
| 348 |
res = fixed_mem_range_flags(dev, index++, |
| 349 |
(uint64_t)pds.pds[i].base << 26, |
| 350 |
(uint64_t)pds.pds[i].size << 26, flags); |
| 351 |
if (cxl_mode == XEONSP_CXL_SP_MEM) |
| 352 |
LOG_RESOURCE("specific_purpose_memory", dev, res); |
| 353 |
else |
| 354 |
LOG_RESOURCE("CXL_memory", dev, res); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
res = mmio_from_to(dev, index++, mc_values[MMCFG_BASE_REG], |
| 360 |
mc_values[MMCFG_LIMIT_REG] + 1); |
| 361 |
LOG_RESOURCE("mmiocfg_res", dev, res); |
| 362 |
|
| 363 |
|
| 364 |
res = mmio_range(dev, index++, LAPIC_DEFAULT_BASE, 0x00001000); |
| 365 |
LOG_RESOURCE("apic_res", dev, res); |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
res = mmio_range(dev, index++, VGA_MMIO_BASE, VGA_MMIO_SIZE); |
| 375 |
LOG_RESOURCE("legacy_mmio", dev, res); |
| 376 |
|
| 377 |
res = reserved_ram_from_to(dev, index++, 0xc0000, 1 * MiB); |
| 378 |
LOG_RESOURCE("legacy_write_protect", dev, res); |
| 379 |
|
| 380 |
*res_count = index; |
| 381 |
} |
| 382 |
|
| 383 |
static void mmapvtd_read_resources(struct device *dev) |
| 384 |
{ |
| 385 |
int index = MC_DRAM_RESOURCE_ANON_START; |
| 386 |
|
| 387 |
|
| 388 |
pci_dev_read_resources(dev); |
| 389 |
|
| 390 |
|
| 391 |
configure_dpr(dev); |
| 392 |
|
| 393 |
|
| 394 |
mc_add_dram_resources(dev, &index); |
| 395 |
} |
| 396 |
|
| 397 |
static void mmapvtd_set_resources(struct device *dev) |
| 398 |
{ |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
if (is_domain0(dev->upstream->dev)) { |
| 406 |
resource_t mmio64_base, mmio64_size; |
| 407 |
if (get_mmio_high_base_size(&mmio64_base, &mmio64_size)) { |
| 408 |
assert(!probe_resource(dev, MC_DRAM_RESOURCE_MMIO_HIGH)); |
| 409 |
fixed_mem_range_flags(dev, MC_DRAM_RESOURCE_MMIO_HIGH, |
| 410 |
mmio64_base, mmio64_size, IORESOURCE_STORED); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
pci_dev_set_resources(dev); |
| 415 |
} |
| 416 |
|
| 417 |
static void mmapvtd_init(struct device *dev) |
| 418 |
{ |
| 419 |
} |
| 420 |
|
| 421 |
static struct device_operations mmapvtd_ops = { |
| 422 |
.read_resources = mmapvtd_read_resources, |
| 423 |
.set_resources = mmapvtd_set_resources, |
| 424 |
.enable_resources = pci_dev_enable_resources, |
| 425 |
.init = mmapvtd_init, |
| 426 |
.ops_pci = &soc_pci_ops, |
| 427 |
}; |
| 428 |
|
| 429 |
static const unsigned short mmapvtd_ids[] = { |
| 430 |
MMAP_VTD_CFG_REG_DEVID, / |
| 431 |
MMAP_VTD_STACK_CFG_REG_DEVID, |
| 432 |
0 |
| 433 |
}; |
| 434 |
|
| 435 |
static const struct pci_driver mmapvtd_driver __pci_driver = { |
| 436 |
.ops = &mmapvtd_ops, |
| 437 |
.vendor = PCI_VID_INTEL, |
| 438 |
.devices = mmapvtd_ids |
| 439 |
}; |
| 440 |
|
| 441 |
static void dmi3_init(struct device *dev) |
| 442 |
{ |
| 443 |
if (CONFIG(INTEL_TXT) && skip_intel_txt_lockdown()) |
| 444 |
return; |
| 445 |
|
| 446 |
pci_or_config16(dev, ERRINJCON, 1 << 0); |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
pci_and_config32(dev, DMIRCBAR, ~(1 << 0)); |
| 453 |
} |
| 454 |
|
| 455 |
static struct device_operations dmi3_ops = { |
| 456 |
.read_resources = pci_dev_read_resources, |
| 457 |
.set_resources = pci_dev_set_resources, |
| 458 |
.enable_resources = pci_dev_enable_resources, |
| 459 |
.init = dmi3_init, |
| 460 |
.ops_pci = &soc_pci_ops, |
| 461 |
}; |
| 462 |
|
| 463 |
static const struct pci_driver dmi3_driver __pci_driver = { |
| 464 |
.ops = &dmi3_ops, |
| 465 |
.vendor = PCI_VID_INTEL, |
| 466 |
.device = DMI3_DEVID, |
| 467 |
}; |
| 468 |
|
| 469 |
static void iio_dfx_global_init(struct device *dev) |
| 470 |
{ |
| 471 |
if (CONFIG(INTEL_TXT) && skip_intel_txt_lockdown()) |
| 472 |
return; |
| 473 |
|
| 474 |
uint16_t reg16; |
| 475 |
pci_or_config16(dev, IIO_DFX_LCK_CTL, 0x3ff); |
| 476 |
reg16 = pci_read_config16(dev, IIO_DFX_TSWCTL0); |
| 477 |
reg16 &= ~(1 << 4); |
| 478 |
reg16 &= ~(1 << 5); |
| 479 |
reg16 |= (1 << 3); |
| 480 |
pci_write_config16(dev, IIO_DFX_TSWCTL0, reg16); |
| 481 |
} |
| 482 |
|
| 483 |
static const unsigned short iio_dfx_global_ids[] = { |
| 484 |
0x202d, |
| 485 |
0x203d, |
| 486 |
0 |
| 487 |
}; |
| 488 |
|
| 489 |
static struct device_operations iio_dfx_global_ops = { |
| 490 |
.read_resources = pci_dev_read_resources, |
| 491 |
.set_resources = pci_dev_set_resources, |
| 492 |
.enable_resources = pci_dev_enable_resources, |
| 493 |
.init = iio_dfx_global_init, |
| 494 |
.ops_pci = &soc_pci_ops, |
| 495 |
}; |
| 496 |
|
| 497 |
static const struct pci_driver iio_dfx_global_driver __pci_driver = { |
| 498 |
.ops = &iio_dfx_global_ops, |
| 499 |
.vendor = PCI_VID_INTEL, |
| 500 |
.devices = iio_dfx_global_ids, |
| 501 |
}; |