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