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