blob: b4856120a7866158bd287fd24a552010a39dfbb2 [file] [log] [blame]
Marc Jones97321db2020-09-28 23:35:08 -06001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
3#include <acpi/acpigen.h>
Felix Held4b2464f2022-02-23 17:54:20 +01004#include <arch/hpet.h>
Patrick Rudolph57ddd682023-02-28 09:17:40 +01005#include <arch/ioapic.h>
Marc Jones97321db2020-09-28 23:35:08 -06006#include <assert.h>
7#include <cbmem.h>
Naresh Solanki559f9ed2023-01-20 19:38:07 +01008#include <cpu/x86/lapic.h>
Arthur Heymans36e6f9b2022-10-27 15:11:05 +02009#include <commonlib/sort.h>
Marc Jones97321db2020-09-28 23:35:08 -060010#include <device/mmio.h>
11#include <device/pci.h>
Tim Chu5c196402022-12-13 12:09:44 +000012#include <device/pciexp.h>
Patrick Rudolph425e4212024-02-15 16:30:16 +010013#include <device/pci_ids.h>
Marc Jones97321db2020-09-28 23:35:08 -060014#include <soc/acpi.h>
Patrick Rudolph425e4212024-02-15 16:30:16 +010015#include <soc/chip_common.h>
Rocky Phagurad4db36e2021-04-03 08:49:32 -070016#include <soc/hest.h>
Marc Jones97321db2020-09-28 23:35:08 -060017#include <soc/iomap.h>
Tim Chu5c196402022-12-13 12:09:44 +000018#include <soc/numa.h>
Marc Jones97321db2020-09-28 23:35:08 -060019#include <soc/pci_devs.h>
20#include <soc/soc_util.h>
Marc Jones18960ce2020-11-02 12:41:12 -070021#include <soc/util.h>
Arthur Heymans695dd292020-11-12 21:05:09 +010022#include <intelblocks/p2sb.h>
Marc Jones97321db2020-09-28 23:35:08 -060023#include "chip.h"
24
Tim Chu5c196402022-12-13 12:09:44 +000025/* NUMA related ACPI table generation. SRAT, SLIT, etc */
Marc Jones97321db2020-09-28 23:35:08 -060026
Arthur Heymans36e6f9b2022-10-27 15:11:05 +020027/* Increase if necessary. Currently all x86 CPUs only have 2 SMP threads */
28#define MAX_THREAD 2
29
Marc Jones97321db2020-09-28 23:35:08 -060030unsigned long acpi_create_srat_lapics(unsigned long current)
31{
32 struct device *cpu;
Arthur Heymans36e6f9b2022-10-27 15:11:05 +020033 unsigned int num_cpus = 0;
34 int apic_ids[CONFIG_MAX_CPUS] = {};
Marc Jones97321db2020-09-28 23:35:08 -060035
Arthur Heymans36e6f9b2022-10-27 15:11:05 +020036 unsigned int sort_start = 0;
37 for (unsigned int thread_id = 0; thread_id < MAX_THREAD; thread_id++) {
38 for (cpu = all_devices; cpu; cpu = cpu->next) {
39 if (!is_enabled_cpu(cpu))
40 continue;
41 if (num_cpus >= ARRAY_SIZE(apic_ids))
42 break;
43 if (cpu->path.apic.thread_id != thread_id)
44 continue;
45 apic_ids[num_cpus++] = cpu->path.apic.apic_id;
46 }
47 bubblesort(&apic_ids[sort_start], num_cpus - sort_start, NUM_ASCENDING);
48 sort_start = num_cpus;
49 }
50
51 for (unsigned int i = 0; i < num_cpus; i++) {
52 /* Match the sorted apic_ids to a struct device */
53 for (cpu = all_devices; cpu; cpu = cpu->next) {
54 if (!is_enabled_cpu(cpu))
55 continue;
56 if (cpu->path.apic.apic_id == apic_ids[i])
57 break;
58 }
59 if (!cpu)
Marc Jones97321db2020-09-28 23:35:08 -060060 continue;
Naresh Solanki559f9ed2023-01-20 19:38:07 +010061
62 if (is_x2apic_mode()) {
Arthur Heymans36e6f9b2022-10-27 15:11:05 +020063 printk(BIOS_DEBUG, "SRAT: x2apic cpu_index=%04x, node_id=%02x, apic_id=%08x\n",
64 i, cpu->path.apic.node_id, cpu->path.apic.apic_id);
Naresh Solanki559f9ed2023-01-20 19:38:07 +010065
66 current += acpi_create_srat_x2apic((acpi_srat_x2apic_t *)current,
67 cpu->path.apic.node_id, cpu->path.apic.apic_id);
68 } else {
69 printk(BIOS_DEBUG, "SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n",
Arthur Heymans36e6f9b2022-10-27 15:11:05 +020070 i, cpu->path.apic.node_id, cpu->path.apic.apic_id);
Naresh Solanki559f9ed2023-01-20 19:38:07 +010071
72 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current,
73 cpu->path.apic.node_id, cpu->path.apic.apic_id);
74 }
Marc Jones97321db2020-09-28 23:35:08 -060075 }
76 return current;
77}
78
79static unsigned int get_srat_memory_entries(acpi_srat_mem_t *srat_mem)
80{
81 const struct SystemMemoryMapHob *memory_map;
82 unsigned int mmap_index;
83
84 memory_map = get_system_memory_map();
Elyes Haouasf1ba7d62022-09-13 10:03:44 +020085 assert(memory_map);
Marc Jones97321db2020-09-28 23:35:08 -060086 printk(BIOS_DEBUG, "memory_map: %p\n", memory_map);
87
88 mmap_index = 0;
89 for (int e = 0; e < memory_map->numberEntries; ++e) {
90 const struct SystemMemoryMapElement *mem_element = &memory_map->Element[e];
91 uint64_t addr =
Elyes Haouas9018dee2022-11-18 15:07:33 +010092 (uint64_t)((uint64_t)mem_element->BaseAddress <<
Marc Jones97321db2020-09-28 23:35:08 -060093 MEM_ADDR_64MB_SHIFT_BITS);
94 uint64_t size =
Elyes Haouas9018dee2022-11-18 15:07:33 +010095 (uint64_t)((uint64_t)mem_element->ElementSize <<
Marc Jones97321db2020-09-28 23:35:08 -060096 MEM_ADDR_64MB_SHIFT_BITS);
97
98 printk(BIOS_DEBUG, "memory_map %d addr: 0x%llx, BaseAddress: 0x%x, size: 0x%llx, "
Tim Chu5c196402022-12-13 12:09:44 +000099 "ElementSize: 0x%x, type: %d, reserved: %d\n",
Marc Jones97321db2020-09-28 23:35:08 -0600100 e, addr, mem_element->BaseAddress, size,
Tim Chu5c196402022-12-13 12:09:44 +0000101 mem_element->ElementSize, mem_element->Type,
102 (mem_element->Type & MEM_TYPE_RESERVED));
Marc Jones97321db2020-09-28 23:35:08 -0600103
104 assert(mmap_index < MAX_ACPI_MEMORY_AFFINITY_COUNT);
105
106 /* skip reserved memory region */
107 if (mem_element->Type & MEM_TYPE_RESERVED)
108 continue;
Tim Chu5c196402022-12-13 12:09:44 +0000109#if CONFIG(SOC_INTEL_SAPPHIRERAPIDS_SP)
110 /* Skip all non processor attached memory regions */
111 /* In other words, skip all the types >= MemTypeCxlAccVolatileMem */
112 if (mem_element->Type >= MemTypeCxlAccVolatileMem)
113 continue;
114#endif
Marc Jones97321db2020-09-28 23:35:08 -0600115
116 /* skip if this address is already added */
117 bool skip = false;
118 for (int idx = 0; idx < mmap_index; ++idx) {
119 uint64_t base_addr = ((uint64_t)srat_mem[idx].base_address_high << 32) +
120 srat_mem[idx].base_address_low;
121 if (addr == base_addr) {
122 skip = true;
123 break;
124 }
125 }
126 if (skip)
127 continue;
128
129 srat_mem[mmap_index].type = 1; /* Memory affinity structure */
130 srat_mem[mmap_index].length = sizeof(acpi_srat_mem_t);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100131 srat_mem[mmap_index].base_address_low = (uint32_t)(addr & 0xffffffff);
132 srat_mem[mmap_index].base_address_high = (uint32_t)(addr >> 32);
133 srat_mem[mmap_index].length_low = (uint32_t)(size & 0xffffffff);
134 srat_mem[mmap_index].length_high = (uint32_t)(size >> 32);
Marc Jones97321db2020-09-28 23:35:08 -0600135 srat_mem[mmap_index].proximity_domain = mem_element->SocketId;
Shuo Liu3108ba52022-07-05 22:56:28 +0800136 srat_mem[mmap_index].flags = ACPI_SRAT_MEMORY_ENABLED;
Marc Jones97321db2020-09-28 23:35:08 -0600137 if ((mem_element->Type & MEMTYPE_VOLATILE_MASK) == 0)
Shuo Liu3108ba52022-07-05 22:56:28 +0800138 srat_mem[mmap_index].flags |= ACPI_SRAT_MEMORY_NONVOLATILE;
Marc Jones97321db2020-09-28 23:35:08 -0600139 ++mmap_index;
140 }
141
142 return mmap_index;
143}
144
145static unsigned long acpi_fill_srat(unsigned long current)
146{
147 acpi_srat_mem_t srat_mem[MAX_ACPI_MEMORY_AFFINITY_COUNT];
148 unsigned int mem_count;
149
150 /* create all subtables for processors */
151 current = acpi_create_srat_lapics(current);
152
Naresh Solanki9fd5c692023-05-22 16:47:47 +0200153 memset(srat_mem, 0, sizeof(srat_mem));
Marc Jones97321db2020-09-28 23:35:08 -0600154 mem_count = get_srat_memory_entries(srat_mem);
155 for (int i = 0; i < mem_count; ++i) {
156 printk(BIOS_DEBUG, "adding srat memory %d entry length: %d, addr: 0x%x%x, "
157 "length: 0x%x%x, proximity_domain: %d, flags: %x\n",
158 i, srat_mem[i].length,
159 srat_mem[i].base_address_high, srat_mem[i].base_address_low,
160 srat_mem[i].length_high, srat_mem[i].length_low,
161 srat_mem[i].proximity_domain, srat_mem[i].flags);
162 memcpy((acpi_srat_mem_t *)current, &srat_mem[i], sizeof(srat_mem[i]));
163 current += srat_mem[i].length;
164 }
165
Tim Chu5c196402022-12-13 12:09:44 +0000166 if (CONFIG(SOC_INTEL_HAS_CXL))
167 current = cxl_fill_srat(current);
168
Marc Jones97321db2020-09-28 23:35:08 -0600169 return current;
170}
171
Tim Chu5c196402022-12-13 12:09:44 +0000172#if CONFIG(SOC_INTEL_SAPPHIRERAPIDS_SP)
173/*
174Because pds.num_pds comes from spr/numa.c function fill_pds().
175pds.num_pds = soc_get_num_cpus() + get_cxl_node_count().
176*/
177/* SPR-SP platform has Generic Initiator domain in addition to processor domain */
178static unsigned long acpi_fill_slit(unsigned long current)
179{
180 uint8_t *p = (uint8_t *)current;
181 /* According to table 5.60 of ACPI 6.4 spec, "Number of System Localities" field takes
182 up 8 bytes. Following that, each matrix entry takes up 1 byte. */
183 memset(p, 0, 8 + pds.num_pds * pds.num_pds);
184 *p = (uint8_t)pds.num_pds;
185 p += 8;
186
187 for (int i = 0; i < pds.num_pds; i++) {
188 for (int j = 0; j < pds.num_pds; j++)
189 p[i * pds.num_pds + j] = pds.pds[i].distances[j];
190 }
191
192 current += 8 + pds.num_pds * pds.num_pds;
193 return current;
194}
195#else
Marc Jones97321db2020-09-28 23:35:08 -0600196static unsigned long acpi_fill_slit(unsigned long current)
197{
Marc Jones70907b02020-10-28 17:00:31 -0600198 unsigned int nodes = soc_get_num_cpus();
Marc Jones97321db2020-09-28 23:35:08 -0600199
200 uint8_t *p = (uint8_t *)current;
201 memset(p, 0, 8 + nodes * nodes);
202 *p = (uint8_t)nodes;
203 p += 8;
204
205 /* this assumes fully connected socket topology */
206 for (int i = 0; i < nodes; i++) {
207 for (int j = 0; j < nodes; j++) {
208 if (i == j)
209 p[i*nodes+j] = 10;
210 else
211 p[i*nodes+j] = 16;
212 }
213 }
214
215 current += 8 + nodes * nodes;
216 return current;
217}
Tim Chu5c196402022-12-13 12:09:44 +0000218#endif
Marc Jones97321db2020-09-28 23:35:08 -0600219
220/*
Marc Jones97321db2020-09-28 23:35:08 -0600221 * This function adds PCIe bridge device entry in DMAR table. If it is called
222 * in the context of ATSR subtable, it adds ATSR subtable when it is first called.
223 */
224static unsigned long acpi_create_dmar_ds_pci_br_for_port(unsigned long current,
Tim Chu5c196402022-12-13 12:09:44 +0000225 const struct device *bridge_dev,
226 uint32_t pcie_seg,
227 bool is_atsr, bool *first)
Marc Jones97321db2020-09-28 23:35:08 -0600228{
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200229 const uint32_t bus = bridge_dev->upstream->secondary;
Tim Chu5c196402022-12-13 12:09:44 +0000230 const uint32_t dev = PCI_SLOT(bridge_dev->path.pci.devfn);
231 const uint32_t func = PCI_FUNC(bridge_dev->path.pci.devfn);
Marc Jones97321db2020-09-28 23:35:08 -0600232
Tim Chu5c196402022-12-13 12:09:44 +0000233 if (bus == 0)
234 return current;
Marc Jones97321db2020-09-28 23:35:08 -0600235
236 unsigned long atsr_size = 0;
237 unsigned long pci_br_size = 0;
Tim Chu5c196402022-12-13 12:09:44 +0000238 if (is_atsr == true && first && *first == true) {
Marc Jones97321db2020-09-28 23:35:08 -0600239 printk(BIOS_DEBUG, "[Root Port ATS Capability] Flags: 0x%x, "
240 "PCI Segment Number: 0x%x\n", 0, pcie_seg);
241 atsr_size = acpi_create_dmar_atsr(current, 0, pcie_seg);
242 *first = false;
243 }
244
245 printk(BIOS_DEBUG, " [PCI Bridge Device] Enumeration ID: 0x%x, "
246 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
247 0, bus, dev, func);
248 pci_br_size = acpi_create_dmar_ds_pci_br(current + atsr_size, bus, dev, func);
249
250 return (atsr_size + pci_br_size);
251}
252
253static unsigned long acpi_create_drhd(unsigned long current, int socket,
254 int stack, const IIO_UDS *hob)
255{
Marc Jones97321db2020-09-28 23:35:08 -0600256 unsigned long tmp = current;
Tim Chu5c196402022-12-13 12:09:44 +0000257 const STACK_RES *ri = &hob->PlatformData.IIO_resource[socket].StackRes[stack];
258 const uint32_t bus = ri->BusBase;
259 const uint32_t pcie_seg = hob->PlatformData.CpuQpiInfo[socket].PcieSegment;
260 const uint32_t reg_base = ri->VtdBarAddress;
Marc Jones97321db2020-09-28 23:35:08 -0600261 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, pcie_seg: 0x%x, reg_base: 0x%x\n",
262 __func__, socket, stack, bus, pcie_seg, reg_base);
263
264 /* Do not generate DRHD for non-PCIe stack */
265 if (!reg_base)
266 return current;
267
Arthur Heymansa1c4ad32021-05-04 18:40:28 +0200268 // Add DRHD Hardware Unit
Tim Chu5c196402022-12-13 12:09:44 +0000269
270 if (socket == 0 && stack == IioStack0) {
Arthur Heymansa1c4ad32021-05-04 18:40:28 +0200271 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
272 "Register Base Address: 0x%x\n",
273 DRHD_INCLUDE_PCI_ALL, pcie_seg, reg_base);
274 current += acpi_create_dmar_drhd(current, DRHD_INCLUDE_PCI_ALL,
275 pcie_seg, reg_base);
276 } else {
277 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
278 "Register Base Address: 0x%x\n", 0, pcie_seg, reg_base);
279 current += acpi_create_dmar_drhd(current, 0, pcie_seg, reg_base);
280 }
281
Marc Jones97321db2020-09-28 23:35:08 -0600282 // Add PCH IOAPIC
Tim Chu5c196402022-12-13 12:09:44 +0000283 if (socket == 0 && stack == IioStack0) {
Arthur Heymans6e425e12020-11-12 21:12:05 +0100284 union p2sb_bdf ioapic_bdf = p2sb_get_ioapic_bdf();
Marc Jones97321db2020-09-28 23:35:08 -0600285 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
Felix Held0d192892024-02-06 16:55:29 +0100286 "PCI Path: 0x%x, 0x%x\n", get_ioapic_id(IO_APIC_ADDR), ioapic_bdf.bus,
Patrick Rudolph57ddd682023-02-28 09:17:40 +0100287 ioapic_bdf.dev, ioapic_bdf.fn);
288 current += acpi_create_dmar_ds_ioapic_from_hw(current,
289 IO_APIC_ADDR, ioapic_bdf.bus, ioapic_bdf.dev, ioapic_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600290 }
291
Tim Chu5c196402022-12-13 12:09:44 +0000292/* SPR has no per stack IOAPIC or CBDMA devices */
293#if CONFIG(SOC_INTEL_SKYLAKE_SP) || CONFIG(SOC_INTEL_COOPERLAKE_SP)
294 uint32_t enum_id;
Marc Jones97321db2020-09-28 23:35:08 -0600295 // Add IOAPIC entry
Arthur Heymansa1cc5572020-11-06 12:53:33 +0100296 enum_id = soc_get_iio_ioapicid(socket, stack);
Marc Jones97321db2020-09-28 23:35:08 -0600297 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
298 "PCI Path: 0x%x, 0x%x\n", enum_id, bus, APIC_DEV_NUM, APIC_FUNC_NUM);
299 current += acpi_create_dmar_ds_ioapic(current, enum_id, bus,
300 APIC_DEV_NUM, APIC_FUNC_NUM);
301
302 // Add CBDMA devices for CSTACK
303 if (socket != 0 && stack == CSTACK) {
304 for (int cbdma_func_id = 0; cbdma_func_id < 8; ++cbdma_func_id) {
305 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
306 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
307 0, bus, CBDMA_DEV_NUM, cbdma_func_id);
308 current += acpi_create_dmar_ds_pci(current,
309 bus, CBDMA_DEV_NUM, cbdma_func_id);
310 }
311 }
Tim Chu5c196402022-12-13 12:09:44 +0000312#endif
Marc Jones97321db2020-09-28 23:35:08 -0600313
314 // Add PCIe Ports
Tim Chu5c196402022-12-13 12:09:44 +0000315 if (socket != 0 || stack != IioStack0) {
316 struct device *dev = pcidev_path_on_bus(bus, PCI_DEVFN(0, 0));
317 while (dev) {
318 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
319 current +=
320 acpi_create_dmar_ds_pci_br_for_port(
321 current, dev, pcie_seg, false, NULL);
Marc Jones97321db2020-09-28 23:35:08 -0600322
Tim Chu5c196402022-12-13 12:09:44 +0000323 dev = dev->sibling;
324 }
325
326#if CONFIG(SOC_INTEL_SKYLAKE_SP) || CONFIG(SOC_INTEL_COOPERLAKE_SP)
Marc Jones97321db2020-09-28 23:35:08 -0600327 // Add VMD
328 if (hob->PlatformData.VMDStackEnable[socket][stack] &&
329 stack >= PSTACK0 && stack <= PSTACK2) {
330 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
331 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
332 0, bus, VMD_DEV_NUM, VMD_FUNC_NUM);
333 current += acpi_create_dmar_ds_pci(current,
334 bus, VMD_DEV_NUM, VMD_FUNC_NUM);
335 }
Tim Chu5c196402022-12-13 12:09:44 +0000336#endif
Marc Jones97321db2020-09-28 23:35:08 -0600337 }
338
Shuo Liu08f1f052024-01-20 02:52:17 +0800339 // Add IOAT End Points (with memory resources. We don't report every End Point device.)
Shuo Liu313b18a2024-02-21 18:54:31 +0800340 if (CONFIG(HAVE_IOAT_DOMAINS) && is_ioat_iio_stack_res(ri)) {
Tim Chu5c196402022-12-13 12:09:44 +0000341 for (int b = ri->BusBase; b <= ri->BusLimit; ++b) {
342 struct device *dev = pcidev_path_on_bus(b, PCI_DEVFN(0, 0));
343 while (dev) {
344 /* This may also require a check for IORESOURCE_PREFETCH,
345 * but that would not include the FPU (4942/0) */
346 if ((dev->resource_list->flags &
347 (IORESOURCE_MEM | IORESOURCE_PCI64 | IORESOURCE_ASSIGNED)) ==
348 (IORESOURCE_MEM | IORESOURCE_PCI64 | IORESOURCE_ASSIGNED)) {
349 const uint32_t d = PCI_SLOT(dev->path.pci.devfn);
350 const uint32_t f = PCI_FUNC(dev->path.pci.devfn);
351 printk(BIOS_DEBUG, " [PCIE Endpoint Device] "
352 "Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
353 " PCI Path: 0x%x, 0x%x\n", 0, b, d, f);
354 current += acpi_create_dmar_ds_pci(current, b, d, f);
355 }
356 dev = dev->sibling;
357 }
358 }
359 }
Tim Chu5c196402022-12-13 12:09:44 +0000360
Marc Jones97321db2020-09-28 23:35:08 -0600361 // Add HPET
Tim Chu5c196402022-12-13 12:09:44 +0000362 if (socket == 0 && stack == IioStack0) {
Elyes Haouas167b7fcd2022-12-11 10:38:35 +0100363 uint16_t hpet_capid = read16p(HPET_BASE_ADDRESS);
Marc Jones97321db2020-09-28 23:35:08 -0600364 uint16_t num_hpets = (hpet_capid >> 0x08) & 0x1F; // Bits [8:12] has hpet count
365 printk(BIOS_SPEW, "%s hpet_capid: 0x%x, num_hpets: 0x%x\n",
366 __func__, hpet_capid, num_hpets);
367 //BIT 15
368 if (num_hpets && (num_hpets != 0x1f) &&
Elyes Haouas167b7fcd2022-12-11 10:38:35 +0100369 (read32p(HPET_BASE_ADDRESS + 0x100) & (0x00008000))) {
Arthur Heymans695dd292020-11-12 21:05:09 +0100370 union p2sb_bdf hpet_bdf = p2sb_get_hpet_bdf();
Marc Jones97321db2020-09-28 23:35:08 -0600371 printk(BIOS_DEBUG, " [Message-capable HPET Device] Enumeration ID: 0x%x, "
372 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
Arthur Heymans695dd292020-11-12 21:05:09 +0100373 0, hpet_bdf.bus, hpet_bdf.dev, hpet_bdf.fn);
374 current += acpi_create_dmar_ds_msi_hpet(current, 0, hpet_bdf.bus,
375 hpet_bdf.dev, hpet_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600376 }
377 }
378
379 acpi_dmar_drhd_fixup(tmp, current);
380
381 return current;
382}
383
384static unsigned long acpi_create_atsr(unsigned long current, const IIO_UDS *hob)
385{
Patrick Rudolph425e4212024-02-15 16:30:16 +0100386 struct device *child, *dev;
387 struct resource *resource;
388
389 /*
390 * The assumption made here is that the host bridges on a socket share the
391 * PCI segment group and thus only one ATSR header needs to be emitted for
392 * a single socket.
393 * This is easier than to sort the host bridges by PCI segment group first
394 * and then generate one ATSR header for every new segment.
395 */
Patrick Rudolphac028572023-07-14 17:44:33 +0200396 for (int socket = 0, iio = 0; iio < hob->PlatformData.numofIIO; ++socket) {
397 if (!soc_cpu_is_enabled(socket))
398 continue;
399 iio++;
Marc Jones97321db2020-09-28 23:35:08 -0600400 unsigned long tmp = current;
401 bool first = true;
Marc Jones97321db2020-09-28 23:35:08 -0600402
Patrick Rudolph425e4212024-02-15 16:30:16 +0100403 dev = NULL;
404 while ((dev = dev_find_device(PCI_VID_INTEL, MMAP_VTD_CFG_REG_DEVID, dev))) {
405 /* Only add devices for the current socket */
406 if (iio_pci_domain_socket_from_dev(dev) != socket)
Marc Jones97321db2020-09-28 23:35:08 -0600407 continue;
Patrick Rudolph425e4212024-02-15 16:30:16 +0100408 /* See if there is a resource with the appropriate index. */
409 resource = probe_resource(dev, VTD_BAR_CSR);
410 if (!resource)
411 continue;
412 int stack = iio_pci_domain_stack_from_dev(dev);
413
414 uint64_t vtd_mmio_cap = read64(res2mmio(resource, VTD_EXT_CAP_LOW, 0));
415 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, vtd_base: %p, "
Marc Jones97321db2020-09-28 23:35:08 -0600416 "vtd_mmio_cap: 0x%llx\n",
Patrick Rudolph425e4212024-02-15 16:30:16 +0100417 __func__, socket, stack, dev->upstream->secondary,
418 res2mmio(resource, 0, 0), vtd_mmio_cap);
Marc Jones97321db2020-09-28 23:35:08 -0600419
420 // ATSR is applicable only for platform supporting device IOTLBs
421 // through the VT-d extended capability register
422 assert(vtd_mmio_cap != 0xffffffffffffffff);
423 if ((vtd_mmio_cap & 0x4) == 0) // BIT 2
424 continue;
425
Patrick Rudolph425e4212024-02-15 16:30:16 +0100426 if (dev->upstream->secondary == 0 && dev->upstream->segment_group == 0)
Tim Chu5c196402022-12-13 12:09:44 +0000427 continue;
428
Patrick Rudolph425e4212024-02-15 16:30:16 +0100429 for (child = dev->upstream->children; child; child = child->sibling) {
430 if ((child->hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
431 continue;
432 current +=
Tim Chu5c196402022-12-13 12:09:44 +0000433 acpi_create_dmar_ds_pci_br_for_port(
Patrick Rudolph425e4212024-02-15 16:30:16 +0100434 current, child, child->upstream->segment_group, true, &first);
Marc Jones97321db2020-09-28 23:35:08 -0600435 }
436 }
437 if (tmp != current)
438 acpi_dmar_atsr_fixup(tmp, current);
439 }
440
441 return current;
442}
443
444static unsigned long acpi_create_rmrr(unsigned long current)
445{
446 uint32_t size = ALIGN_UP(MEM_BLK_COUNT * sizeof(MEM_BLK), 0x1000);
447
448 uint32_t *ptr;
449
450 // reserve memory
451 ptr = cbmem_find(CBMEM_ID_STORAGE_DATA);
452 if (!ptr) {
453 ptr = cbmem_add(CBMEM_ID_STORAGE_DATA, size);
Elyes Haouasf1ba7d62022-09-13 10:03:44 +0200454 assert(ptr);
Marc Jones97321db2020-09-28 23:35:08 -0600455 memset(ptr, 0, size);
456 }
457
458 unsigned long tmp = current;
459 printk(BIOS_DEBUG, "[Reserved Memory Region] PCI Segment Number: 0x%x, Base Address: 0x%x, "
460 "End Address (limit): 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100461 0, (uint32_t)ptr, (uint32_t)((uint32_t)ptr + size - 1));
462 current += acpi_create_dmar_rmrr(current, 0, (uint32_t)ptr,
463 (uint32_t)((uint32_t)ptr + size - 1));
Marc Jones97321db2020-09-28 23:35:08 -0600464
465 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
466 "PCI Path: 0x%x, 0x%x\n",
467 0, XHCI_BUS_NUMBER, PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
468 current += acpi_create_dmar_ds_pci(current, XHCI_BUS_NUMBER,
469 PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
470
471 acpi_dmar_rmrr_fixup(tmp, current);
472
473 return current;
474}
475
476static unsigned long acpi_create_rhsa(unsigned long current)
477{
Patrick Rudolph425e4212024-02-15 16:30:16 +0100478 struct device *dev = NULL;
479 struct resource *resource;
480 int socket;
Marc Jones97321db2020-09-28 23:35:08 -0600481
Patrick Rudolph425e4212024-02-15 16:30:16 +0100482 while ((dev = dev_find_device(PCI_VID_INTEL, MMAP_VTD_CFG_REG_DEVID, dev))) {
483 /* See if there is a resource with the appropriate index. */
484 resource = probe_resource(dev, VTD_BAR_CSR);
485 if (!resource)
Patrick Rudolphac028572023-07-14 17:44:33 +0200486 continue;
Patrick Rudolphac028572023-07-14 17:44:33 +0200487
Patrick Rudolph425e4212024-02-15 16:30:16 +0100488 socket = iio_pci_domain_socket_from_dev(dev);
Marc Jones97321db2020-09-28 23:35:08 -0600489
Patrick Rudolph425e4212024-02-15 16:30:16 +0100490 printk(BIOS_DEBUG, "[Remapping Hardware Static Affinity] Base Address: %p, "
491 "Proximity Domain: 0x%x\n", res2mmio(resource, 0, 0), socket);
492 current += acpi_create_dmar_rhsa(current, (uintptr_t)res2mmio(resource, 0, 0), socket);
Marc Jones97321db2020-09-28 23:35:08 -0600493 }
494
495 return current;
496}
497
Shuo Liu08f1f052024-01-20 02:52:17 +0800498static unsigned long xeonsp_create_satc_ioat(unsigned long current, const STACK_RES *ri)
Tim Chu5c196402022-12-13 12:09:44 +0000499{
500 for (int b = ri->BusBase; b <= ri->BusLimit; ++b) {
501 struct device *dev = pcidev_path_on_bus(b, PCI_DEVFN(0, 0));
502 while (dev) {
503 if (pciexp_find_extended_cap(dev, PCIE_EXT_CAP_ID_ATS, 0)) {
504 const uint32_t d = PCI_SLOT(dev->path.pci.devfn);
505 const uint32_t f = PCI_FUNC(dev->path.pci.devfn);
506 printk(BIOS_DEBUG, " [SATC Endpoint Device] "
507 "Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
508 " PCI Path: 0x%x, 0x%x\n", 0, b, d, f);
509 current += acpi_create_dmar_ds_pci(current, b, d, f);
510 }
511 dev = dev->sibling;
512 }
513 }
514 return current;
515}
516
517/* SoC Integrated Address Translation Cache */
518static unsigned long acpi_create_satc(unsigned long current, const IIO_UDS *hob)
519{
Tim Chu5c196402022-12-13 12:09:44 +0000520 const unsigned long tmp = current;
521
522 // Add the SATC header
523 current += acpi_create_dmar_satc(current, 0, 0);
524
Shuo Liu08f1f052024-01-20 02:52:17 +0800525 // Find the IOAT devices on each socket
Patrick Rudolphac028572023-07-14 17:44:33 +0200526 for (int socket = CONFIG_MAX_SOCKET - 1; socket >= 0; --socket) {
527 if (!soc_cpu_is_enabled(socket))
528 continue;
Tim Chu5c196402022-12-13 12:09:44 +0000529 for (int stack = (MAX_LOGIC_IIO_STACK - 1); stack >= 0; --stack) {
530 const STACK_RES *ri = &hob->PlatformData.IIO_resource[socket].StackRes[stack];
Shuo Liu08f1f052024-01-20 02:52:17 +0800531 // Add the IOAT ATS devices to the SATC
Shuo Liu313b18a2024-02-21 18:54:31 +0800532 if (CONFIG(HAVE_IOAT_DOMAINS) && is_ioat_iio_stack_res(ri))
Shuo Liu08f1f052024-01-20 02:52:17 +0800533 current = xeonsp_create_satc_ioat(current, ri);
Tim Chu5c196402022-12-13 12:09:44 +0000534 }
535 }
536
537 acpi_dmar_satc_fixup(tmp, current);
538 return current;
539}
Tim Chu5c196402022-12-13 12:09:44 +0000540
Marc Jones97321db2020-09-28 23:35:08 -0600541static unsigned long acpi_fill_dmar(unsigned long current)
542{
Arthur Heymans83b26222020-11-06 11:50:55 +0100543 const IIO_UDS *hob = get_iio_uds();
Marc Jones97321db2020-09-28 23:35:08 -0600544
Tim Chu5c196402022-12-13 12:09:44 +0000545 // DRHD - socket 0 stack 0 must be the last DRHD entry.
Patrick Rudolphac028572023-07-14 17:44:33 +0200546 for (int socket = (CONFIG_MAX_SOCKET - 1); socket >= 0; --socket) {
547 if (!soc_cpu_is_enabled(socket))
548 continue;
Tim Chu5c196402022-12-13 12:09:44 +0000549 for (int stack = (MAX_LOGIC_IIO_STACK - 1); stack >= 0; --stack)
550 current = acpi_create_drhd(current, socket, stack, hob);
Marc Jones97321db2020-09-28 23:35:08 -0600551 }
552
553 // RMRR
554 current = acpi_create_rmrr(current);
555
556 // Root Port ATS Capability
557 current = acpi_create_atsr(current, hob);
558
559 // RHSA
560 current = acpi_create_rhsa(current);
561
Tim Chu5c196402022-12-13 12:09:44 +0000562 // SATC
Shuo Liu08f1f052024-01-20 02:52:17 +0800563 if (CONFIG(HAVE_IOAT_DOMAINS))
564 current = acpi_create_satc(current, hob);
Tim Chu5c196402022-12-13 12:09:44 +0000565
Marc Jones97321db2020-09-28 23:35:08 -0600566 return current;
567}
568
Tim Chu5c196402022-12-13 12:09:44 +0000569unsigned long northbridge_write_acpi_tables(const struct device *device, unsigned long current,
Marc Jones97321db2020-09-28 23:35:08 -0600570 struct acpi_rsdp *rsdp)
571{
572 acpi_srat_t *srat;
573 acpi_slit_t *slit;
574 acpi_dmar_t *dmar;
Tim Chu5c196402022-12-13 12:09:44 +0000575 acpi_hmat_t *hmat;
576 acpi_cedt_t *cedt;
Marc Jones97321db2020-09-28 23:35:08 -0600577
578 const config_t *const config = config_of(device);
579
580 /* SRAT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200581 current = ALIGN_UP(current, 8);
Marc Jones97321db2020-09-28 23:35:08 -0600582 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100583 srat = (acpi_srat_t *)current;
Marc Jones97321db2020-09-28 23:35:08 -0600584 acpi_create_srat(srat, acpi_fill_srat);
585 current += srat->header.length;
586 acpi_add_table(rsdp, srat);
587
588 /* SLIT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200589 current = ALIGN_UP(current, 8);
Marc Jones97321db2020-09-28 23:35:08 -0600590 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100591 slit = (acpi_slit_t *)current;
Marc Jones97321db2020-09-28 23:35:08 -0600592 acpi_create_slit(slit, acpi_fill_slit);
593 current += slit->header.length;
594 acpi_add_table(rsdp, slit);
595
Tim Chu5c196402022-12-13 12:09:44 +0000596 if (CONFIG(SOC_INTEL_HAS_CXL)) {
597 /* HMAT*/
598 current = ALIGN_UP(current, 8);
599 printk(BIOS_DEBUG, "ACPI: * HMAT at %lx\n", current);
600 hmat = (acpi_hmat_t *)current;
601 acpi_create_hmat(hmat, acpi_fill_hmat);
602 current += hmat->header.length;
603 acpi_add_table(rsdp, hmat);
604 }
605
Marc Jones97321db2020-09-28 23:35:08 -0600606 /* DMAR */
607 if (config->vtd_support) {
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200608 current = ALIGN_UP(current, 8);
Marc Jones97321db2020-09-28 23:35:08 -0600609 dmar = (acpi_dmar_t *)current;
Marc Jonesb7e591e2020-11-13 15:55:31 -0700610 enum dmar_flags flags = DMAR_INTR_REMAP;
611
612 /* SKX FSP doesn't support X2APIC, but CPX FSP does */
613 if (CONFIG(SOC_INTEL_SKYLAKE_SP))
614 flags |= DMAR_X2APIC_OPT_OUT;
615
Tim Chu5c196402022-12-13 12:09:44 +0000616 printk(BIOS_DEBUG, "ACPI: * DMAR at %lx\n", current);
Marc Jonesb7e591e2020-11-13 15:55:31 -0700617 printk(BIOS_DEBUG, "[DMA Remapping table] Flags: 0x%x\n", flags);
618 acpi_create_dmar(dmar, flags, acpi_fill_dmar);
Marc Jones97321db2020-09-28 23:35:08 -0600619 current += dmar->header.length;
620 current = acpi_align_current(current);
621 acpi_add_table(rsdp, dmar);
622 }
623
Tim Chu5c196402022-12-13 12:09:44 +0000624 if (CONFIG(SOC_INTEL_HAS_CXL)) {
625 /* CEDT: CXL Early Discovery Table */
626 if (get_cxl_node_count() > 0) {
627 current = ALIGN_UP(current, 8);
628 printk(BIOS_DEBUG, "ACPI: * CEDT at %lx\n", current);
629 cedt = (acpi_cedt_t *)current;
630 acpi_create_cedt(cedt, acpi_fill_cedt);
631 current += cedt->header.length;
632 acpi_add_table(rsdp, cedt);
633 }
634 }
635
636 if (CONFIG(SOC_ACPI_HEST)) {
637 printk(BIOS_DEBUG, "ACPI: * HEST at %lx\n", current);
Rocky Phagurad4db36e2021-04-03 08:49:32 -0700638 current = hest_create(current, rsdp);
Tim Chu5c196402022-12-13 12:09:44 +0000639 }
Rocky Phagurad4db36e2021-04-03 08:49:32 -0700640
Marc Jones97321db2020-09-28 23:35:08 -0600641 return current;
642}