Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Jonathan Zhang | 8f89549 | 2020-01-16 11:16:45 -0800 | [diff] [blame] | 2 | |
| 3 | #include <cbmem.h> |
| 4 | #include <console/console.h> |
Elyes HAOUAS | 32da343 | 2020-05-17 17:15:31 +0200 | [diff] [blame] | 5 | #include <cpu/x86/lapic_def.h> |
Jonathan Zhang | 8f89549 | 2020-01-16 11:16:45 -0800 | [diff] [blame] | 6 | #include <device/pci.h> |
| 7 | #include <device/pci_ids.h> |
Marc Jones | 521a03f | 2020-10-19 13:46:59 -0600 | [diff] [blame] | 8 | #include <soc/acpi.h> |
Jonathan Zhang | 8f89549 | 2020-01-16 11:16:45 -0800 | [diff] [blame] | 9 | #include <soc/iomap.h> |
| 10 | #include <soc/pci_devs.h> |
| 11 | #include <soc/ramstage.h> |
Andrey Petrov | 662da6c | 2020-03-16 22:46:57 -0700 | [diff] [blame] | 12 | #include <soc/util.h> |
| 13 | #include <fsp/util.h> |
Jonathan Zhang | 8f89549 | 2020-01-16 11:16:45 -0800 | [diff] [blame] | 14 | |
| 15 | struct map_entry { |
| 16 | uint32_t reg; |
| 17 | int is_64_bit; |
| 18 | int is_limit; |
| 19 | int mask_bits; |
| 20 | const char *description; |
| 21 | }; |
| 22 | |
| 23 | enum { |
| 24 | TOHM_REG, |
| 25 | MMIOL_REG, |
| 26 | MMCFG_BASE_REG, |
| 27 | MMCFG_LIMIT_REG, |
| 28 | TOLM_REG, |
| 29 | ME_BASE_REG, |
| 30 | ME_LIMIT_REG, |
| 31 | TSEG_BASE_REG, |
| 32 | TSEG_LIMIT_REG, |
| 33 | /* Must be last. */ |
| 34 | NUM_MAP_ENTRIES |
| 35 | }; |
| 36 | |
| 37 | static struct map_entry memory_map[NUM_MAP_ENTRIES] = { |
| 38 | [TOHM_REG] = MAP_ENTRY_LIMIT_64(VTD_TOHM_CSR, 26, "TOHM"), |
| 39 | [MMIOL_REG] = MAP_ENTRY_BASE_32(VTD_MMIOL_CSR, "MMIOL"), |
| 40 | [MMCFG_BASE_REG] = MAP_ENTRY_BASE_64(VTD_MMCFG_BASE_CSR, "MMCFG_BASE"), |
| 41 | [MMCFG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_MMCFG_LIMIT_CSR, 26, "MMCFG_LIMIT"), |
| 42 | [TOLM_REG] = MAP_ENTRY_LIMIT_32(VTD_TOLM_CSR, 26, "TOLM"), |
| 43 | [ME_BASE_REG] = MAP_ENTRY_BASE_64(VTD_ME_BASE_CSR, "ME_BASE"), |
| 44 | [ME_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_ME_LIMIT_CSR, 19, "ME_LIMIT"), |
| 45 | [TSEG_BASE_REG] = MAP_ENTRY_BASE_32(VTD_TSEG_BASE_CSR, "TSEGMB_BASE"), |
| 46 | [TSEG_LIMIT_REG] = MAP_ENTRY_LIMIT_32(VTD_TSEG_LIMIT_CSR, 20, "TSEGMB_LIMIT"), |
| 47 | }; |
| 48 | |
| 49 | static void read_map_entry(struct device *dev, struct map_entry *entry, |
| 50 | uint64_t *result) |
| 51 | { |
| 52 | uint64_t value; |
| 53 | uint64_t mask; |
| 54 | |
| 55 | /* All registers are on a 1MiB granularity. */ |
| 56 | mask = ((1ULL << entry->mask_bits) - 1); |
| 57 | mask = ~mask; |
| 58 | |
| 59 | value = 0; |
| 60 | |
| 61 | if (entry->is_64_bit) { |
| 62 | value = pci_read_config32(dev, entry->reg + sizeof(uint32_t)); |
| 63 | value <<= 32; |
| 64 | } |
| 65 | |
| 66 | value |= (uint64_t)pci_read_config32(dev, entry->reg); |
| 67 | value &= mask; |
| 68 | |
| 69 | if (entry->is_limit) |
| 70 | value |= ~mask; |
| 71 | |
| 72 | *result = value; |
| 73 | } |
| 74 | |
| 75 | static void mc_read_map_entries(struct device *dev, uint64_t *values) |
| 76 | { |
| 77 | int i; |
| 78 | for (i = 0; i < NUM_MAP_ENTRIES; i++) |
| 79 | read_map_entry(dev, &memory_map[i], &values[i]); |
| 80 | } |
| 81 | |
| 82 | static void mc_report_map_entries(struct device *dev, uint64_t *values) |
| 83 | { |
| 84 | int i; |
| 85 | for (i = 0; i < NUM_MAP_ENTRIES; i++) { |
| 86 | printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n", |
| 87 | memory_map[i].description, values[i]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Host Memory Map: |
| 93 | * |
| 94 | * +--------------------------+ TOCM (2 pow 46 - 1) |
| 95 | * | Reserved | |
| 96 | * +--------------------------+ |
| 97 | * | MMIOH (relocatable) | |
| 98 | * +--------------------------+ |
| 99 | * | PCISeg | |
| 100 | * +--------------------------+ TOHM |
| 101 | * | High DRAM Memory | |
| 102 | * +--------------------------+ 4GiB (0x100000000) |
| 103 | * +--------------------------+ 0xFFFF_FFFF |
| 104 | * | Firmware | |
| 105 | * +--------------------------+ 0xFF00_0000 |
| 106 | * | Reserved | |
| 107 | * +--------------------------+ 0xFEF0_0000 |
| 108 | * | Local xAPIC | |
| 109 | * +--------------------------+ 0xFEE0_0000 |
| 110 | * | HPET/LT/TPM/Others | |
| 111 | * +--------------------------+ 0xFED0_0000 |
| 112 | * | I/O xAPIC | |
| 113 | * +--------------------------+ 0xFEC0_0000 |
| 114 | * | Reserved | |
| 115 | * +--------------------------+ 0xFEB8_0000 |
| 116 | * | Reserved | |
| 117 | * +--------------------------+ 0xFEB0_0000 |
| 118 | * | Reserved | |
| 119 | * +--------------------------+ 0xFE00_0000 |
| 120 | * | MMIOL (relocatable) | |
| 121 | * | P2SB PCR cfg BAR | (0xfd000000 - 0xfdffffff |
| 122 | * | BAR space | [mem 0x90000000-0xfcffffff] available for PCI devices |
| 123 | * +--------------------------+ 0x9000_0000 |
| 124 | * |PCIe MMCFG (relocatable) | CONFIG_MMCONF_BASE_ADDRESS 64 or 256MB |
| 125 | * | | (0x80000000 - 0x8fffffff, 0x40000) |
| 126 | * +--------------------------+ TOLM |
| 127 | * | MEseg (relocatable) | 32, 64, 128 or 256 MB (0x78000000 - 0x7fffffff, 0x20000) |
| 128 | * +--------------------------+ |
| 129 | * | Tseg (relocatable) | N x 8MB (0x70000000 - 0x77ffffff, 0x20000) |
| 130 | * +--------------------------+ cbmem_top |
| 131 | * | Reserved - CBMEM | (0x6fffe000 - 0x6fffffff, 0x2000) |
| 132 | * +--------------------------+ |
| 133 | * | Reserved - FSP | (0x6fbfe000 - 0x6fffdfff, 0x400000) |
| 134 | * +--------------------------+ top_of_ram (0x6fbfdfff) |
| 135 | * | Low DRAM Memory | |
| 136 | * +--------------------------+ FFFFF (1MB) |
| 137 | * | E & F segments | |
| 138 | * +--------------------------+ E0000 |
| 139 | * | C & D segments | |
| 140 | * +--------------------------+ C0000 |
| 141 | * | VGA & SMM Memory | |
| 142 | * +--------------------------+ A0000 |
| 143 | * | Conventional Memory | |
| 144 | * | (DOS Range) | |
| 145 | * +--------------------------+ 0 |
| 146 | */ |
| 147 | |
| 148 | static void mc_add_dram_resources(struct device *dev, int *res_count) |
| 149 | { |
| 150 | struct range_entry fsp_mem; |
| 151 | uint64_t base_kb; |
| 152 | uint64_t size_kb; |
| 153 | uint64_t top_of_ram; |
| 154 | uint64_t mc_values[NUM_MAP_ENTRIES]; |
| 155 | struct resource *resource; |
| 156 | int index = *res_count; |
| 157 | |
Marc Jones | 662ac54 | 2020-11-02 21:26:41 -0700 | [diff] [blame] | 158 | /* Only add dram resources once. */ |
| 159 | if (dev->bus->secondary != 0) |
| 160 | return; |
| 161 | |
Jonathan Zhang | 8f89549 | 2020-01-16 11:16:45 -0800 | [diff] [blame] | 162 | fsp_find_reserved_memory(&fsp_mem); |
| 163 | |
| 164 | /* Read in the MAP registers and report their values. */ |
| 165 | mc_read_map_entries(dev, &mc_values[0]); |
| 166 | mc_report_map_entries(dev, &mc_values[0]); |
| 167 | |
| 168 | top_of_ram = range_entry_base(&fsp_mem) - 1; |
| 169 | printk(BIOS_SPEW, "cbmem_top: 0x%lx, fsp range: [0x%llx - 0x%llx], top_of_ram: 0x%llx\n", |
| 170 | (uintptr_t) cbmem_top(), range_entry_base(&fsp_mem), |
| 171 | range_entry_end(&fsp_mem), top_of_ram); |
| 172 | |
| 173 | /* Conventional Memory (DOS region, 0x0 to 0x9FFFF) */ |
| 174 | base_kb = 0; |
| 175 | size_kb = (0xa0000 >> 10); |
| 176 | LOG_MEM_RESOURCE("legacy_ram", dev, index, base_kb, size_kb); |
| 177 | ram_resource(dev, index++, base_kb, size_kb); |
| 178 | |
| 179 | /* 1MB -> top_of_ram i.e., fsp_mem_base+1*/ |
| 180 | base_kb = (0x100000 >> 10); |
| 181 | size_kb = (top_of_ram - 0xfffff) >> 10; |
| 182 | LOG_MEM_RESOURCE("low_ram", dev, index, base_kb, size_kb); |
| 183 | ram_resource(dev, index++, base_kb, size_kb); |
| 184 | |
| 185 | /* |
| 186 | * FSP meomoy, CBMem regions are already added as reserved |
| 187 | * Add TSEG and MESEG Regions as reserved memory |
| 188 | * src/drivers/intel/fsp2_0/memory_init.c sets CBMEM reserved size |
| 189 | * arch_upd->BootLoaderTolumSize = cbmem_overhead_size(); == 2 * CBMEM_ROOT_MIN_SIZE |
| 190 | * typically 0x2000 |
| 191 | * Example config: |
| 192 | * FSP_RESERVED_MEMORY_RESOURCE_HOB |
| 193 | * FspReservedMemoryResource Base : 6FBFE000 |
| 194 | * FspReservedMemoryResource Size : 400000 |
| 195 | * FSP_BOOT_LOADER_TOLUM_HOB |
| 196 | * FspBootLoaderTolum Base : 6FFFE000 |
| 197 | * FspBootLoaderTolum Size : 2000 |
| 198 | */ |
| 199 | |
| 200 | /* Mark TSEG/SMM region as reserved */ |
| 201 | base_kb = (mc_values[TSEG_BASE_REG] >> 10); |
| 202 | size_kb = (mc_values[TSEG_LIMIT_REG] - mc_values[TSEG_BASE_REG] + 1) >> 10; |
| 203 | LOG_MEM_RESOURCE("mmio_tseg", dev, index, base_kb, size_kb); |
| 204 | reserved_ram_resource(dev, index++, base_kb, size_kb); |
| 205 | |
| 206 | /* Mark region between TSEG - TOLM (eg. MESEG) as reserved */ |
| 207 | if (mc_values[TSEG_LIMIT_REG] < mc_values[TOLM_REG]) { |
| 208 | base_kb = ((mc_values[TSEG_LIMIT_REG] + 1) >> 10); |
| 209 | size_kb = (mc_values[TOLM_REG] - mc_values[TSEG_LIMIT_REG]) >> 10; |
| 210 | LOG_MEM_RESOURCE("mmio_tolm", dev, index, base_kb, size_kb); |
| 211 | reserved_ram_resource(dev, index++, base_kb, size_kb); |
| 212 | } |
| 213 | |
| 214 | /* 4GiB -> TOHM */ |
| 215 | if (mc_values[TOHM_REG] > 0x100000000) { |
| 216 | base_kb = (0x100000000 >> 10); |
| 217 | size_kb = (mc_values[TOHM_REG] - 0x100000000 + 1) >> 10; |
| 218 | LOG_MEM_RESOURCE("high_ram", dev, index, base_kb, size_kb); |
| 219 | ram_resource(dev, index++, base_kb, size_kb); |
| 220 | } |
| 221 | |
| 222 | /* add MMIO CFG resource */ |
| 223 | resource = new_resource(dev, index++); |
| 224 | resource->base = (resource_t) mc_values[MMCFG_BASE_REG]; |
| 225 | resource->size = (resource_t) (mc_values[MMCFG_LIMIT_REG] - |
| 226 | mc_values[MMCFG_BASE_REG] + 1); |
| 227 | resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE | |
| 228 | IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
| 229 | LOG_MEM_RESOURCE("mmiocfg_res", dev, index-1, (resource->base >> 10), |
| 230 | (resource->size >> 10)); |
| 231 | |
| 232 | /* add Local APIC resource */ |
| 233 | resource = new_resource(dev, index++); |
| 234 | resource->base = LAPIC_DEFAULT_BASE; |
| 235 | resource->size = 0x00001000; |
| 236 | resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE | |
| 237 | IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
| 238 | LOG_MEM_RESOURCE("apic_res", dev, index-1, (resource->base >> 10), |
| 239 | (resource->size >> 10)); |
| 240 | |
| 241 | /* |
| 242 | * Add legacy region as reserved - 0xa000 - 1MB |
| 243 | * Reserve everything between A segment and 1MB: |
| 244 | * |
| 245 | * 0xa0000 - 0xbffff: legacy VGA |
| 246 | * 0xc0000 - 0xfffff: RAM |
| 247 | */ |
| 248 | base_kb = VGA_BASE_ADDRESS >> 10; |
| 249 | size_kb = VGA_BASE_SIZE >> 10; |
| 250 | LOG_MEM_RESOURCE("legacy_mmio", dev, index, base_kb, size_kb); |
| 251 | mmio_resource(dev, index++, base_kb, size_kb); |
| 252 | |
| 253 | base_kb = (0xc0000 >> 10); |
| 254 | size_kb = (0x100000 - 0xc0000) >> 10; |
| 255 | LOG_MEM_RESOURCE("legacy_write_protect", dev, index, base_kb, size_kb); |
| 256 | reserved_ram_resource(dev, index++, base_kb, size_kb); |
| 257 | |
| 258 | *res_count = index; |
| 259 | } |
| 260 | |
| 261 | static void mmapvtd_read_resources(struct device *dev) |
| 262 | { |
| 263 | int index = 0; |
| 264 | |
| 265 | /* Read standard PCI resources. */ |
| 266 | pci_dev_read_resources(dev); |
| 267 | |
| 268 | /* Calculate and add DRAM resources. */ |
| 269 | mc_add_dram_resources(dev, &index); |
| 270 | } |
| 271 | |
| 272 | static void mmapvtd_init(struct device *dev) |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | static struct device_operations mmapvtd_ops = { |
| 277 | .read_resources = mmapvtd_read_resources, |
| 278 | .set_resources = pci_dev_set_resources, |
| 279 | .enable_resources = pci_dev_enable_resources, |
| 280 | .init = mmapvtd_init, |
| 281 | .ops_pci = &soc_pci_ops, |
Marc Jones | 521a03f | 2020-10-19 13:46:59 -0600 | [diff] [blame] | 282 | #if CONFIG(HAVE_ACPI_TABLES) |
| 283 | .acpi_inject_dsdt = uncore_inject_dsdt, |
| 284 | #endif |
Jonathan Zhang | 8f89549 | 2020-01-16 11:16:45 -0800 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | static const unsigned short mmapvtd_ids[] = { |
| 288 | MMAP_VTD_CFG_REG_DEVID, /* Memory Map/IntelĀ® VT-d Configuration Registers */ |
| 289 | 0 |
| 290 | }; |
| 291 | |
| 292 | static const struct pci_driver mmapvtd_driver __pci_driver = { |
| 293 | .ops = &mmapvtd_ops, |
| 294 | .vendor = PCI_VENDOR_ID_INTEL, |
| 295 | .devices = mmapvtd_ids |
| 296 | }; |