blob: 1a60b60e257932812dfaf9c733cb7cae01539d7b [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>
Arthur Heymans9d8a4552021-02-02 19:21:24 +010015#include <security/intel/txt/txt.h>
Arthur Heymans63660592022-01-06 12:28:44 +010016#include <stdint.h>
Jonathan Zhang8f895492020-01-16 11:16:45 -080017
18struct map_entry {
19 uint32_t reg;
20 int is_64_bit;
21 int is_limit;
22 int mask_bits;
23 const char *description;
24};
25
26enum {
27 TOHM_REG,
28 MMIOL_REG,
29 MMCFG_BASE_REG,
30 MMCFG_LIMIT_REG,
31 TOLM_REG,
32 ME_BASE_REG,
33 ME_LIMIT_REG,
34 TSEG_BASE_REG,
35 TSEG_LIMIT_REG,
36 /* Must be last. */
37 NUM_MAP_ENTRIES
38};
39
40static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
41 [TOHM_REG] = MAP_ENTRY_LIMIT_64(VTD_TOHM_CSR, 26, "TOHM"),
42 [MMIOL_REG] = MAP_ENTRY_BASE_32(VTD_MMIOL_CSR, "MMIOL"),
43 [MMCFG_BASE_REG] = MAP_ENTRY_BASE_64(VTD_MMCFG_BASE_CSR, "MMCFG_BASE"),
44 [MMCFG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_MMCFG_LIMIT_CSR, 26, "MMCFG_LIMIT"),
45 [TOLM_REG] = MAP_ENTRY_LIMIT_32(VTD_TOLM_CSR, 26, "TOLM"),
46 [ME_BASE_REG] = MAP_ENTRY_BASE_64(VTD_ME_BASE_CSR, "ME_BASE"),
47 [ME_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_ME_LIMIT_CSR, 19, "ME_LIMIT"),
48 [TSEG_BASE_REG] = MAP_ENTRY_BASE_32(VTD_TSEG_BASE_CSR, "TSEGMB_BASE"),
49 [TSEG_LIMIT_REG] = MAP_ENTRY_LIMIT_32(VTD_TSEG_LIMIT_CSR, 20, "TSEGMB_LIMIT"),
50};
51
52static void read_map_entry(struct device *dev, struct map_entry *entry,
53 uint64_t *result)
54{
55 uint64_t value;
56 uint64_t mask;
57
58 /* All registers are on a 1MiB granularity. */
59 mask = ((1ULL << entry->mask_bits) - 1);
60 mask = ~mask;
61
62 value = 0;
63
64 if (entry->is_64_bit) {
65 value = pci_read_config32(dev, entry->reg + sizeof(uint32_t));
66 value <<= 32;
67 }
68
69 value |= (uint64_t)pci_read_config32(dev, entry->reg);
70 value &= mask;
71
72 if (entry->is_limit)
73 value |= ~mask;
74
75 *result = value;
76}
77
78static void mc_read_map_entries(struct device *dev, uint64_t *values)
79{
80 int i;
81 for (i = 0; i < NUM_MAP_ENTRIES; i++)
82 read_map_entry(dev, &memory_map[i], &values[i]);
83}
84
85static void mc_report_map_entries(struct device *dev, uint64_t *values)
86{
87 int i;
88 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
89 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
90 memory_map[i].description, values[i]);
91 }
92}
93
Arthur Heymans77509be2020-10-22 17:11:22 +020094static void configure_dpr(struct device *dev)
95{
96 const uintptr_t cbmem_top_mb = ALIGN_UP((uintptr_t)cbmem_top(), MiB) / MiB;
97 union dpr_register dpr = { .raw = pci_read_config32(dev, VTD_LTDPR) };
98
99 /* The DPR lock bit has to be set sufficiently early. It looks like
100 * it cannot be set anymore after FSP-S.
101 */
102 dpr.lock = 1;
103 dpr.epm = 1;
104 dpr.size = dpr.top - cbmem_top_mb;
105 pci_write_config32(dev, VTD_LTDPR, dpr.raw);
106}
107
Jonathan Zhang8f895492020-01-16 11:16:45 -0800108/*
109 * Host Memory Map:
110 *
111 * +--------------------------+ TOCM (2 pow 46 - 1)
112 * | Reserved |
113 * +--------------------------+
114 * | MMIOH (relocatable) |
115 * +--------------------------+
116 * | PCISeg |
117 * +--------------------------+ TOHM
118 * | High DRAM Memory |
119 * +--------------------------+ 4GiB (0x100000000)
120 * +--------------------------+ 0xFFFF_FFFF
121 * | Firmware |
122 * +--------------------------+ 0xFF00_0000
123 * | Reserved |
124 * +--------------------------+ 0xFEF0_0000
125 * | Local xAPIC |
126 * +--------------------------+ 0xFEE0_0000
127 * | HPET/LT/TPM/Others |
128 * +--------------------------+ 0xFED0_0000
129 * | I/O xAPIC |
130 * +--------------------------+ 0xFEC0_0000
131 * | Reserved |
132 * +--------------------------+ 0xFEB8_0000
133 * | Reserved |
134 * +--------------------------+ 0xFEB0_0000
135 * | Reserved |
136 * +--------------------------+ 0xFE00_0000
137 * | MMIOL (relocatable) |
138 * | P2SB PCR cfg BAR | (0xfd000000 - 0xfdffffff
139 * | BAR space | [mem 0x90000000-0xfcffffff] available for PCI devices
140 * +--------------------------+ 0x9000_0000
Shelley Chen4e9bb332021-10-20 15:43:45 -0700141 * |PCIe MMCFG (relocatable) | CONFIG_ECAM_MMCONF_BASE_ADDRESS 64 or 256MB
Jonathan Zhang8f895492020-01-16 11:16:45 -0800142 * | | (0x80000000 - 0x8fffffff, 0x40000)
143 * +--------------------------+ TOLM
144 * | MEseg (relocatable) | 32, 64, 128 or 256 MB (0x78000000 - 0x7fffffff, 0x20000)
145 * +--------------------------+
146 * | Tseg (relocatable) | N x 8MB (0x70000000 - 0x77ffffff, 0x20000)
Arthur Heymans77509be2020-10-22 17:11:22 +0200147 * +--------------------------+
148 * | DPR |
Jonathan Zhang8f895492020-01-16 11:16:45 -0800149 * +--------------------------+ cbmem_top
150 * | Reserved - CBMEM | (0x6fffe000 - 0x6fffffff, 0x2000)
151 * +--------------------------+
152 * | Reserved - FSP | (0x6fbfe000 - 0x6fffdfff, 0x400000)
153 * +--------------------------+ top_of_ram (0x6fbfdfff)
154 * | Low DRAM Memory |
155 * +--------------------------+ FFFFF (1MB)
156 * | E & F segments |
157 * +--------------------------+ E0000
158 * | C & D segments |
159 * +--------------------------+ C0000
160 * | VGA & SMM Memory |
161 * +--------------------------+ A0000
162 * | Conventional Memory |
163 * | (DOS Range) |
164 * +--------------------------+ 0
165 */
166
167static void mc_add_dram_resources(struct device *dev, int *res_count)
168{
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300169 const struct resource *res;
Jonathan Zhang8f895492020-01-16 11:16:45 -0800170 uint64_t mc_values[NUM_MAP_ENTRIES];
Jonathan Zhang8f895492020-01-16 11:16:45 -0800171 int index = *res_count;
172
Marc Jones662ac542020-11-02 21:26:41 -0700173 /* Only add dram resources once. */
174 if (dev->bus->secondary != 0)
175 return;
176
Jonathan Zhang8f895492020-01-16 11:16:45 -0800177 /* Read in the MAP registers and report their values. */
178 mc_read_map_entries(dev, &mc_values[0]);
179 mc_report_map_entries(dev, &mc_values[0]);
180
Jonathan Zhang8f895492020-01-16 11:16:45 -0800181 /* Conventional Memory (DOS region, 0x0 to 0x9FFFF) */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300182 res = ram_from_to(dev, index++, 0, 0xa0000);
183 LOG_RESOURCE("legacy_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800184
Arthur Heymans63660592022-01-06 12:28:44 +0100185 /* 1MB -> top_of_ram i.e., cbmem_top */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300186 res = ram_from_to(dev, index++, 1 * MiB, (uintptr_t)cbmem_top());
187 LOG_RESOURCE("low_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800188
Jonathan Zhang8f895492020-01-16 11:16:45 -0800189 /* Mark TSEG/SMM region as reserved */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300190 res = reserved_ram_from_to(dev, index++, mc_values[TSEG_BASE_REG],
191 mc_values[TSEG_LIMIT_REG] + 1);
192 LOG_RESOURCE("mmio_tseg", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800193
Arthur Heymans77509be2020-10-22 17:11:22 +0200194 /* Reserve and set up DPR */
195 configure_dpr(dev);
196 union dpr_register dpr = { .raw = pci_read_config32(dev, VTD_LTDPR) };
197 if (dpr.size) {
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300198 res = reserved_ram_from_to(dev, index++, (dpr.top - dpr.size) * MiB,
199 dpr.top * MiB);
200 LOG_RESOURCE("dpr", dev, res);
Arthur Heymans77509be2020-10-22 17:11:22 +0200201 }
202
Jonathan Zhang8f895492020-01-16 11:16:45 -0800203 /* Mark region between TSEG - TOLM (eg. MESEG) as reserved */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300204 res = reserved_ram_from_to(dev, index++, mc_values[TSEG_LIMIT_REG] + 1,
205 mc_values[TOLM_REG]);
206 LOG_RESOURCE("mmio_tolm", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800207
208 /* 4GiB -> TOHM */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300209 res = upper_ram_end(dev, index++, mc_values[TOHM_REG] + 1);
210 LOG_RESOURCE("high_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800211
212 /* add MMIO CFG resource */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300213 res = mmio_from_to(dev, index++, mc_values[MMCFG_BASE_REG],
214 mc_values[MMCFG_LIMIT_REG] + 1);
215 LOG_RESOURCE("mmiocfg_res", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800216
217 /* add Local APIC resource */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300218 res = mmio_range(dev, index++, LAPIC_DEFAULT_BASE, 0x00001000);
219 LOG_RESOURCE("apic_res", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800220
221 /*
222 * Add legacy region as reserved - 0xa000 - 1MB
223 * Reserve everything between A segment and 1MB:
224 *
225 * 0xa0000 - 0xbffff: legacy VGA
226 * 0xc0000 - 0xfffff: RAM
227 */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300228 res = mmio_range(dev, index++, VGA_BASE_ADDRESS, VGA_BASE_SIZE);
229 LOG_RESOURCE("legacy_mmio", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800230
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300231 res = reserved_ram_from_to(dev, index++, 0xc0000, 1 * MiB);
232 LOG_RESOURCE("legacy_write_protect", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800233
234 *res_count = index;
235}
236
237static void mmapvtd_read_resources(struct device *dev)
238{
239 int index = 0;
240
241 /* Read standard PCI resources. */
242 pci_dev_read_resources(dev);
243
244 /* Calculate and add DRAM resources. */
245 mc_add_dram_resources(dev, &index);
246}
247
248static void mmapvtd_init(struct device *dev)
249{
250}
251
252static struct device_operations mmapvtd_ops = {
253 .read_resources = mmapvtd_read_resources,
254 .set_resources = pci_dev_set_resources,
255 .enable_resources = pci_dev_enable_resources,
256 .init = mmapvtd_init,
257 .ops_pci = &soc_pci_ops,
Marc Jones521a03f2020-10-19 13:46:59 -0600258#if CONFIG(HAVE_ACPI_TABLES)
259 .acpi_inject_dsdt = uncore_inject_dsdt,
260#endif
Jonathan Zhang8f895492020-01-16 11:16:45 -0800261};
262
263static const unsigned short mmapvtd_ids[] = {
264 MMAP_VTD_CFG_REG_DEVID, /* Memory Map/Intel® VT-d Configuration Registers */
265 0
266};
267
268static const struct pci_driver mmapvtd_driver __pci_driver = {
269 .ops = &mmapvtd_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100270 .vendor = PCI_VID_INTEL,
Jonathan Zhang8f895492020-01-16 11:16:45 -0800271 .devices = mmapvtd_ids
272};
Arthur Heymans77509be2020-10-22 17:11:22 +0200273
274static void vtd_read_resources(struct device *dev)
275{
276 pci_dev_read_resources(dev);
277
278 configure_dpr(dev);
279}
280
281static struct device_operations vtd_ops = {
282 .read_resources = vtd_read_resources,
283 .set_resources = pci_dev_set_resources,
284 .enable_resources = pci_dev_enable_resources,
285 .ops_pci = &soc_pci_ops,
286};
287
288/* VTD devices on other stacks */
289static const struct pci_driver vtd_driver __pci_driver = {
290 .ops = &vtd_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100291 .vendor = PCI_VID_INTEL,
Arthur Heymans77509be2020-10-22 17:11:22 +0200292 .device = MMAP_VTD_STACK_CFG_REG_DEVID,
293};
Arthur Heymans42a6f7e2020-11-10 16:46:18 +0100294
295static void dmi3_init(struct device *dev)
296{
Arthur Heymans9d8a4552021-02-02 19:21:24 +0100297 if (CONFIG(INTEL_TXT) && skip_intel_txt_lockdown())
298 return;
Arthur Heymans42a6f7e2020-11-10 16:46:18 +0100299 /* Disable error injection */
300 pci_or_config16(dev, ERRINJCON, 1 << 0);
301
302 /*
303 * DMIRCBAR registers are not TXT lockable, but the BAR enable
304 * bit is. TXT requires that DMIRCBAR be disabled for security.
305 */
306 pci_and_config32(dev, DMIRCBAR, ~(1 << 0));
307}
308
309static struct device_operations dmi3_ops = {
310 .read_resources = pci_dev_read_resources,
311 .set_resources = pci_dev_set_resources,
312 .enable_resources = pci_dev_enable_resources,
313 .init = dmi3_init,
314 .ops_pci = &soc_pci_ops,
315};
316
317static const struct pci_driver dmi3_driver __pci_driver = {
318 .ops = &dmi3_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100319 .vendor = PCI_VID_INTEL,
Arthur Heymans42a6f7e2020-11-10 16:46:18 +0100320 .device = DMI3_DEVID,
321};
Arthur Heymans7a36ca52020-11-10 15:55:31 +0100322
323static void iio_dfx_global_init(struct device *dev)
324{
Arthur Heymans9d8a4552021-02-02 19:21:24 +0100325 if (CONFIG(INTEL_TXT) && skip_intel_txt_lockdown())
326 return;
327
Arthur Heymans7a36ca52020-11-10 15:55:31 +0100328 uint16_t reg16;
329 pci_or_config16(dev, IIO_DFX_LCK_CTL, 0x3ff);
330 reg16 = pci_read_config16(dev, IIO_DFX_TSWCTL0);
331 reg16 &= ~(1 << 4); // allow ib mmio cfg
332 reg16 &= ~(1 << 5); // ignore acs p2p ma lpbk
333 reg16 |= (1 << 3); // me disable
334 pci_write_config16(dev, IIO_DFX_TSWCTL0, reg16);
335}
336
337static const unsigned short iio_dfx_global_ids[] = {
338 0x202d,
339 0x203d,
340 0
341};
342
343static struct device_operations iio_dfx_global_ops = {
344 .read_resources = pci_dev_read_resources,
345 .set_resources = pci_dev_set_resources,
346 .enable_resources = pci_dev_enable_resources,
347 .init = iio_dfx_global_init,
348 .ops_pci = &soc_pci_ops,
349};
350
351static const struct pci_driver iio_dfx_global_driver __pci_driver = {
352 .ops = &iio_dfx_global_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100353 .vendor = PCI_VID_INTEL,
Arthur Heymans7a36ca52020-11-10 15:55:31 +0100354 .devices = iio_dfx_global_ids,
355};