blob: 8729cf254080623803e526becb064fc40a989da4 [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 Zhang43b0ed72022-12-19 15:42:56 -0800149 * +--------------------------+ 1M aligned DPR base
150 * | Unused memory |
Jonathan Zhang8f895492020-01-16 11:16:45 -0800151 * +--------------------------+ cbmem_top
152 * | Reserved - CBMEM | (0x6fffe000 - 0x6fffffff, 0x2000)
153 * +--------------------------+
154 * | Reserved - FSP | (0x6fbfe000 - 0x6fffdfff, 0x400000)
155 * +--------------------------+ top_of_ram (0x6fbfdfff)
156 * | Low DRAM Memory |
157 * +--------------------------+ FFFFF (1MB)
158 * | E & F segments |
159 * +--------------------------+ E0000
160 * | C & D segments |
161 * +--------------------------+ C0000
162 * | VGA & SMM Memory |
163 * +--------------------------+ A0000
164 * | Conventional Memory |
165 * | (DOS Range) |
166 * +--------------------------+ 0
167 */
168
169static void mc_add_dram_resources(struct device *dev, int *res_count)
170{
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300171 const struct resource *res;
Jonathan Zhang8f895492020-01-16 11:16:45 -0800172 uint64_t mc_values[NUM_MAP_ENTRIES];
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800173 uint64_t top_of_ram;
Jonathan Zhang8f895492020-01-16 11:16:45 -0800174 int index = *res_count;
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800175 struct range_entry fsp_mem;
Jonathan Zhang8f895492020-01-16 11:16:45 -0800176
Marc Jones662ac542020-11-02 21:26:41 -0700177 /* Only add dram resources once. */
178 if (dev->bus->secondary != 0)
179 return;
180
Jonathan Zhang8f895492020-01-16 11:16:45 -0800181 /* Read in the MAP registers and report their values. */
182 mc_read_map_entries(dev, &mc_values[0]);
183 mc_report_map_entries(dev, &mc_values[0]);
184
Jonathan Zhang8f895492020-01-16 11:16:45 -0800185 /* Conventional Memory (DOS region, 0x0 to 0x9FFFF) */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300186 res = ram_from_to(dev, index++, 0, 0xa0000);
187 LOG_RESOURCE("legacy_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800188
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800189 /* 1MB -> top_of_ram */
190 fsp_find_reserved_memory(&fsp_mem);
191 top_of_ram = range_entry_base(&fsp_mem) - 1;
192 res = ram_from_to(dev, index++, 1 * MiB, top_of_ram);
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300193 LOG_RESOURCE("low_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800194
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800195 /* top_of_ram -> cbmem_top */
196 res = ram_from_to(dev, index++, top_of_ram, (uintptr_t)cbmem_top());
197 LOG_RESOURCE("cbmem_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800198
Jonathan Zhang2e495b02023-01-30 11:32:26 -0800199 /* Mark TSEG/SMM region as reserved */
200 res = reserved_ram_from_to(dev, index++, mc_values[TSEG_BASE_REG],
201 mc_values[TSEG_LIMIT_REG] + 1);
202 LOG_RESOURCE("mmio_tseg", dev, res);
203
Jonathan Zhangda538cb2022-12-19 15:49:33 -0800204 /* Reserve DPR region */
Arthur Heymans77509be2020-10-22 17:11:22 +0200205 union dpr_register dpr = { .raw = pci_read_config32(dev, VTD_LTDPR) };
206 if (dpr.size) {
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800207 /*
208 * cbmem_top -> DPR base:
209 * DPR has a 1M granularity so it's possible if cbmem_top is not 1M
210 * aligned that some memory does not get marked as assigned.
211 */
212 res = reserved_ram_from_to(dev, index++, (uintptr_t)cbmem_top(),
213 (dpr.top - dpr.size) * MiB);
214 LOG_RESOURCE("unused_dram", dev, res);
215
216 /* DPR base -> DPR top */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300217 res = reserved_ram_from_to(dev, index++, (dpr.top - dpr.size) * MiB,
218 dpr.top * MiB);
219 LOG_RESOURCE("dpr", dev, res);
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800220
Arthur Heymans77509be2020-10-22 17:11:22 +0200221 }
222
Jonathan Zhang43b0ed72022-12-19 15:42:56 -0800223 /* Mark TSEG/SMM region as reserved */
224 res = reserved_ram_from_to(dev, index++, mc_values[TSEG_BASE_REG],
225 mc_values[TSEG_LIMIT_REG] + 1);
226 LOG_RESOURCE("mmio_tseg", dev, res);
227
Jonathan Zhang8f895492020-01-16 11:16:45 -0800228 /* Mark region between TSEG - TOLM (eg. MESEG) as reserved */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300229 res = reserved_ram_from_to(dev, index++, mc_values[TSEG_LIMIT_REG] + 1,
230 mc_values[TOLM_REG]);
231 LOG_RESOURCE("mmio_tolm", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800232
233 /* 4GiB -> TOHM */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300234 res = upper_ram_end(dev, index++, mc_values[TOHM_REG] + 1);
235 LOG_RESOURCE("high_ram", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800236
237 /* add MMIO CFG resource */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300238 res = mmio_from_to(dev, index++, mc_values[MMCFG_BASE_REG],
239 mc_values[MMCFG_LIMIT_REG] + 1);
240 LOG_RESOURCE("mmiocfg_res", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800241
242 /* add Local APIC resource */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300243 res = mmio_range(dev, index++, LAPIC_DEFAULT_BASE, 0x00001000);
244 LOG_RESOURCE("apic_res", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800245
246 /*
247 * Add legacy region as reserved - 0xa000 - 1MB
248 * Reserve everything between A segment and 1MB:
249 *
250 * 0xa0000 - 0xbffff: legacy VGA
251 * 0xc0000 - 0xfffff: RAM
252 */
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300253 res = mmio_range(dev, index++, VGA_BASE_ADDRESS, VGA_BASE_SIZE);
254 LOG_RESOURCE("legacy_mmio", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800255
Kyösti Mälkki6f9c3572021-06-14 00:40:22 +0300256 res = reserved_ram_from_to(dev, index++, 0xc0000, 1 * MiB);
257 LOG_RESOURCE("legacy_write_protect", dev, res);
Jonathan Zhang8f895492020-01-16 11:16:45 -0800258
259 *res_count = index;
260}
261
262static void mmapvtd_read_resources(struct device *dev)
263{
264 int index = 0;
265
266 /* Read standard PCI resources. */
267 pci_dev_read_resources(dev);
268
Jonathan Zhangda538cb2022-12-19 15:49:33 -0800269 /* set up DPR */
270 configure_dpr(dev);
271
Jonathan Zhang8f895492020-01-16 11:16:45 -0800272 /* Calculate and add DRAM resources. */
273 mc_add_dram_resources(dev, &index);
274}
275
276static void mmapvtd_init(struct device *dev)
277{
278}
279
280static struct device_operations mmapvtd_ops = {
281 .read_resources = mmapvtd_read_resources,
282 .set_resources = pci_dev_set_resources,
283 .enable_resources = pci_dev_enable_resources,
284 .init = mmapvtd_init,
285 .ops_pci = &soc_pci_ops,
Marc Jones521a03f2020-10-19 13:46:59 -0600286#if CONFIG(HAVE_ACPI_TABLES)
287 .acpi_inject_dsdt = uncore_inject_dsdt,
288#endif
Jonathan Zhang8f895492020-01-16 11:16:45 -0800289};
290
291static const unsigned short mmapvtd_ids[] = {
292 MMAP_VTD_CFG_REG_DEVID, /* Memory Map/Intel® VT-d Configuration Registers */
293 0
294};
295
296static const struct pci_driver mmapvtd_driver __pci_driver = {
297 .ops = &mmapvtd_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100298 .vendor = PCI_VID_INTEL,
Jonathan Zhang8f895492020-01-16 11:16:45 -0800299 .devices = mmapvtd_ids
300};
Arthur Heymans77509be2020-10-22 17:11:22 +0200301
302static void vtd_read_resources(struct device *dev)
303{
304 pci_dev_read_resources(dev);
305
306 configure_dpr(dev);
307}
308
309static struct device_operations vtd_ops = {
310 .read_resources = vtd_read_resources,
311 .set_resources = pci_dev_set_resources,
312 .enable_resources = pci_dev_enable_resources,
313 .ops_pci = &soc_pci_ops,
314};
315
316/* VTD devices on other stacks */
317static const struct pci_driver vtd_driver __pci_driver = {
318 .ops = &vtd_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100319 .vendor = PCI_VID_INTEL,
Arthur Heymans77509be2020-10-22 17:11:22 +0200320 .device = MMAP_VTD_STACK_CFG_REG_DEVID,
321};
Arthur Heymans42a6f7e2020-11-10 16:46:18 +0100322
323static void dmi3_init(struct device *dev)
324{
Arthur Heymans9d8a4552021-02-02 19:21:24 +0100325 if (CONFIG(INTEL_TXT) && skip_intel_txt_lockdown())
326 return;
Arthur Heymans42a6f7e2020-11-10 16:46:18 +0100327 /* Disable error injection */
328 pci_or_config16(dev, ERRINJCON, 1 << 0);
329
330 /*
331 * DMIRCBAR registers are not TXT lockable, but the BAR enable
332 * bit is. TXT requires that DMIRCBAR be disabled for security.
333 */
334 pci_and_config32(dev, DMIRCBAR, ~(1 << 0));
335}
336
337static struct device_operations dmi3_ops = {
338 .read_resources = pci_dev_read_resources,
339 .set_resources = pci_dev_set_resources,
340 .enable_resources = pci_dev_enable_resources,
341 .init = dmi3_init,
342 .ops_pci = &soc_pci_ops,
343};
344
345static const struct pci_driver dmi3_driver __pci_driver = {
346 .ops = &dmi3_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100347 .vendor = PCI_VID_INTEL,
Arthur Heymans42a6f7e2020-11-10 16:46:18 +0100348 .device = DMI3_DEVID,
349};
Arthur Heymans7a36ca52020-11-10 15:55:31 +0100350
351static void iio_dfx_global_init(struct device *dev)
352{
Arthur Heymans9d8a4552021-02-02 19:21:24 +0100353 if (CONFIG(INTEL_TXT) && skip_intel_txt_lockdown())
354 return;
355
Arthur Heymans7a36ca52020-11-10 15:55:31 +0100356 uint16_t reg16;
357 pci_or_config16(dev, IIO_DFX_LCK_CTL, 0x3ff);
358 reg16 = pci_read_config16(dev, IIO_DFX_TSWCTL0);
359 reg16 &= ~(1 << 4); // allow ib mmio cfg
360 reg16 &= ~(1 << 5); // ignore acs p2p ma lpbk
361 reg16 |= (1 << 3); // me disable
362 pci_write_config16(dev, IIO_DFX_TSWCTL0, reg16);
363}
364
365static const unsigned short iio_dfx_global_ids[] = {
366 0x202d,
367 0x203d,
368 0
369};
370
371static struct device_operations iio_dfx_global_ops = {
372 .read_resources = pci_dev_read_resources,
373 .set_resources = pci_dev_set_resources,
374 .enable_resources = pci_dev_enable_resources,
375 .init = iio_dfx_global_init,
376 .ops_pci = &soc_pci_ops,
377};
378
379static const struct pci_driver iio_dfx_global_driver __pci_driver = {
380 .ops = &iio_dfx_global_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100381 .vendor = PCI_VID_INTEL,
Arthur Heymans7a36ca52020-11-10 15:55:31 +0100382 .devices = iio_dfx_global_ids,
383};