blob: e7e24f442ddd28e639d5dcf2604856bcff053bd7 [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,
Shuo Liua5bdf8e2024-02-20 01:06:10 +0800102 is_memtype_reserved(mem_element->Type));
Marc Jones97321db2020-09-28 23:35:08 -0600103
104 assert(mmap_index < MAX_ACPI_MEMORY_AFFINITY_COUNT);
105
106 /* skip reserved memory region */
Shuo Liua5bdf8e2024-02-20 01:06:10 +0800107 if (is_memtype_reserved(mem_element->Type))
Marc Jones97321db2020-09-28 23:35:08 -0600108 continue;
Shuo Liua5bdf8e2024-02-20 01:06:10 +0800109 /* skip all non processor attached memory regions */
110 if (CONFIG(SOC_INTEL_HAS_CXL) &&
111 (!is_memtype_processor_attached(mem_element->Type)))
Tim Chu5c196402022-12-13 12:09:44 +0000112 continue;
Marc Jones97321db2020-09-28 23:35:08 -0600113
114 /* skip if this address is already added */
115 bool skip = false;
116 for (int idx = 0; idx < mmap_index; ++idx) {
117 uint64_t base_addr = ((uint64_t)srat_mem[idx].base_address_high << 32) +
118 srat_mem[idx].base_address_low;
119 if (addr == base_addr) {
120 skip = true;
121 break;
122 }
123 }
124 if (skip)
125 continue;
126
127 srat_mem[mmap_index].type = 1; /* Memory affinity structure */
128 srat_mem[mmap_index].length = sizeof(acpi_srat_mem_t);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100129 srat_mem[mmap_index].base_address_low = (uint32_t)(addr & 0xffffffff);
130 srat_mem[mmap_index].base_address_high = (uint32_t)(addr >> 32);
131 srat_mem[mmap_index].length_low = (uint32_t)(size & 0xffffffff);
132 srat_mem[mmap_index].length_high = (uint32_t)(size >> 32);
Marc Jones97321db2020-09-28 23:35:08 -0600133 srat_mem[mmap_index].proximity_domain = mem_element->SocketId;
Shuo Liu3108ba52022-07-05 22:56:28 +0800134 srat_mem[mmap_index].flags = ACPI_SRAT_MEMORY_ENABLED;
Shuo Liua5bdf8e2024-02-20 01:06:10 +0800135 if (is_memtype_non_volatile(mem_element->Type))
Shuo Liu3108ba52022-07-05 22:56:28 +0800136 srat_mem[mmap_index].flags |= ACPI_SRAT_MEMORY_NONVOLATILE;
Marc Jones97321db2020-09-28 23:35:08 -0600137 ++mmap_index;
138 }
139
140 return mmap_index;
141}
142
143static unsigned long acpi_fill_srat(unsigned long current)
144{
145 acpi_srat_mem_t srat_mem[MAX_ACPI_MEMORY_AFFINITY_COUNT];
146 unsigned int mem_count;
147
148 /* create all subtables for processors */
149 current = acpi_create_srat_lapics(current);
150
Naresh Solanki9fd5c692023-05-22 16:47:47 +0200151 memset(srat_mem, 0, sizeof(srat_mem));
Marc Jones97321db2020-09-28 23:35:08 -0600152 mem_count = get_srat_memory_entries(srat_mem);
153 for (int i = 0; i < mem_count; ++i) {
154 printk(BIOS_DEBUG, "adding srat memory %d entry length: %d, addr: 0x%x%x, "
155 "length: 0x%x%x, proximity_domain: %d, flags: %x\n",
156 i, srat_mem[i].length,
157 srat_mem[i].base_address_high, srat_mem[i].base_address_low,
158 srat_mem[i].length_high, srat_mem[i].length_low,
159 srat_mem[i].proximity_domain, srat_mem[i].flags);
160 memcpy((acpi_srat_mem_t *)current, &srat_mem[i], sizeof(srat_mem[i]));
161 current += srat_mem[i].length;
162 }
163
Tim Chu5c196402022-12-13 12:09:44 +0000164 if (CONFIG(SOC_INTEL_HAS_CXL))
165 current = cxl_fill_srat(current);
166
Marc Jones97321db2020-09-28 23:35:08 -0600167 return current;
168}
169
Tim Chu5c196402022-12-13 12:09:44 +0000170#if CONFIG(SOC_INTEL_SAPPHIRERAPIDS_SP)
171/*
172Because pds.num_pds comes from spr/numa.c function fill_pds().
173pds.num_pds = soc_get_num_cpus() + get_cxl_node_count().
174*/
175/* SPR-SP platform has Generic Initiator domain in addition to processor domain */
176static unsigned long acpi_fill_slit(unsigned long current)
177{
178 uint8_t *p = (uint8_t *)current;
179 /* According to table 5.60 of ACPI 6.4 spec, "Number of System Localities" field takes
180 up 8 bytes. Following that, each matrix entry takes up 1 byte. */
181 memset(p, 0, 8 + pds.num_pds * pds.num_pds);
182 *p = (uint8_t)pds.num_pds;
183 p += 8;
184
185 for (int i = 0; i < pds.num_pds; i++) {
186 for (int j = 0; j < pds.num_pds; j++)
187 p[i * pds.num_pds + j] = pds.pds[i].distances[j];
188 }
189
190 current += 8 + pds.num_pds * pds.num_pds;
191 return current;
192}
193#else
Marc Jones97321db2020-09-28 23:35:08 -0600194static unsigned long acpi_fill_slit(unsigned long current)
195{
Marc Jones70907b02020-10-28 17:00:31 -0600196 unsigned int nodes = soc_get_num_cpus();
Marc Jones97321db2020-09-28 23:35:08 -0600197
198 uint8_t *p = (uint8_t *)current;
199 memset(p, 0, 8 + nodes * nodes);
200 *p = (uint8_t)nodes;
201 p += 8;
202
203 /* this assumes fully connected socket topology */
204 for (int i = 0; i < nodes; i++) {
205 for (int j = 0; j < nodes; j++) {
206 if (i == j)
207 p[i*nodes+j] = 10;
208 else
209 p[i*nodes+j] = 16;
210 }
211 }
212
213 current += 8 + nodes * nodes;
214 return current;
215}
Tim Chu5c196402022-12-13 12:09:44 +0000216#endif
Marc Jones97321db2020-09-28 23:35:08 -0600217
Shuo Liu6747acb2024-03-08 19:15:28 +0800218static struct device *dev_find_iommu_on_stack(uint8_t socket, uint8_t stack)
219{
220 return dev_find_all_devices_on_stack(socket, stack, PCI_VID_INTEL,
221 MMAP_VTD_CFG_REG_DEVID, NULL);
222}
223
Marc Jones97321db2020-09-28 23:35:08 -0600224/*
Marc Jones97321db2020-09-28 23:35:08 -0600225 * This function adds PCIe bridge device entry in DMAR table. If it is called
226 * in the context of ATSR subtable, it adds ATSR subtable when it is first called.
227 */
228static unsigned long acpi_create_dmar_ds_pci_br_for_port(unsigned long current,
Tim Chu5c196402022-12-13 12:09:44 +0000229 const struct device *bridge_dev,
230 uint32_t pcie_seg,
231 bool is_atsr, bool *first)
Marc Jones97321db2020-09-28 23:35:08 -0600232{
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200233 const uint32_t bus = bridge_dev->upstream->secondary;
Tim Chu5c196402022-12-13 12:09:44 +0000234 const uint32_t dev = PCI_SLOT(bridge_dev->path.pci.devfn);
235 const uint32_t func = PCI_FUNC(bridge_dev->path.pci.devfn);
Marc Jones97321db2020-09-28 23:35:08 -0600236
Tim Chu5c196402022-12-13 12:09:44 +0000237 if (bus == 0)
238 return current;
Marc Jones97321db2020-09-28 23:35:08 -0600239
240 unsigned long atsr_size = 0;
241 unsigned long pci_br_size = 0;
Tim Chu5c196402022-12-13 12:09:44 +0000242 if (is_atsr == true && first && *first == true) {
Marc Jones97321db2020-09-28 23:35:08 -0600243 printk(BIOS_DEBUG, "[Root Port ATS Capability] Flags: 0x%x, "
244 "PCI Segment Number: 0x%x\n", 0, pcie_seg);
245 atsr_size = acpi_create_dmar_atsr(current, 0, pcie_seg);
246 *first = false;
247 }
248
249 printk(BIOS_DEBUG, " [PCI Bridge Device] Enumeration ID: 0x%x, "
250 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
251 0, bus, dev, func);
252 pci_br_size = acpi_create_dmar_ds_pci_br(current + atsr_size, bus, dev, func);
253
254 return (atsr_size + pci_br_size);
255}
256
Martin L Roth014ec7c2024-03-13 17:02:25 +0000257static unsigned long acpi_create_drhd(unsigned long current, int socket,
258 int stack, const IIO_UDS *hob)
Marc Jones97321db2020-09-28 23:35:08 -0600259{
Marc Jones97321db2020-09-28 23:35:08 -0600260 unsigned long tmp = current;
Shuo Liu6995efb2024-03-08 19:15:28 +0800261
Shuo Liu6747acb2024-03-08 19:15:28 +0800262 struct device *iommu = dev_find_iommu_on_stack(socket, stack);
263 if (!iommu)
264 return current;
265
266 struct resource *resource;
267 resource = probe_resource(iommu, VTD_BAR_CSR);
268 if (!resource)
269 return current;
270
271 uint32_t reg_base = resource->base;
Martin L Roth092a1392024-03-13 17:03:13 +0000272 if (!reg_base)
273 return current;
274
Shuo Liu6747acb2024-03-08 19:15:28 +0800275 const uint32_t bus = iommu->upstream->secondary;
276 uint32_t pcie_seg = iommu->upstream->segment_group;
277
278 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, pcie_seg: 0x%x, reg_base: 0x%x\n",
279 __func__, socket, stack, bus, pcie_seg, reg_base);
280
Arthur Heymansa1c4ad32021-05-04 18:40:28 +0200281 // Add DRHD Hardware Unit
Tim Chu5c196402022-12-13 12:09:44 +0000282
Shuo Liu6747acb2024-03-08 19:15:28 +0800283 if (is_stack0(socket, stack)) {
Arthur Heymansa1c4ad32021-05-04 18:40:28 +0200284 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
285 "Register Base Address: 0x%x\n",
286 DRHD_INCLUDE_PCI_ALL, pcie_seg, reg_base);
287 current += acpi_create_dmar_drhd(current, DRHD_INCLUDE_PCI_ALL,
288 pcie_seg, reg_base);
289 } else {
290 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
291 "Register Base Address: 0x%x\n", 0, pcie_seg, reg_base);
292 current += acpi_create_dmar_drhd(current, 0, pcie_seg, reg_base);
293 }
294
Marc Jones97321db2020-09-28 23:35:08 -0600295 // Add PCH IOAPIC
Shuo Liu6747acb2024-03-08 19:15:28 +0800296 if (is_stack0(socket, stack)) {
Arthur Heymans6e425e12020-11-12 21:12:05 +0100297 union p2sb_bdf ioapic_bdf = p2sb_get_ioapic_bdf();
Marc Jones97321db2020-09-28 23:35:08 -0600298 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
Felix Held0d192892024-02-06 16:55:29 +0100299 "PCI Path: 0x%x, 0x%x\n", get_ioapic_id(IO_APIC_ADDR), ioapic_bdf.bus,
Patrick Rudolph57ddd682023-02-28 09:17:40 +0100300 ioapic_bdf.dev, ioapic_bdf.fn);
301 current += acpi_create_dmar_ds_ioapic_from_hw(current,
302 IO_APIC_ADDR, ioapic_bdf.bus, ioapic_bdf.dev, ioapic_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600303 }
304
Tim Chu5c196402022-12-13 12:09:44 +0000305/* SPR has no per stack IOAPIC or CBDMA devices */
306#if CONFIG(SOC_INTEL_SKYLAKE_SP) || CONFIG(SOC_INTEL_COOPERLAKE_SP)
307 uint32_t enum_id;
Marc Jones97321db2020-09-28 23:35:08 -0600308 // Add IOAPIC entry
Arthur Heymansa1cc5572020-11-06 12:53:33 +0100309 enum_id = soc_get_iio_ioapicid(socket, stack);
Marc Jones97321db2020-09-28 23:35:08 -0600310 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
311 "PCI Path: 0x%x, 0x%x\n", enum_id, bus, APIC_DEV_NUM, APIC_FUNC_NUM);
312 current += acpi_create_dmar_ds_ioapic(current, enum_id, bus,
313 APIC_DEV_NUM, APIC_FUNC_NUM);
314
315 // Add CBDMA devices for CSTACK
316 if (socket != 0 && stack == CSTACK) {
317 for (int cbdma_func_id = 0; cbdma_func_id < 8; ++cbdma_func_id) {
318 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
319 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
320 0, bus, CBDMA_DEV_NUM, cbdma_func_id);
321 current += acpi_create_dmar_ds_pci(current,
322 bus, CBDMA_DEV_NUM, cbdma_func_id);
323 }
324 }
Tim Chu5c196402022-12-13 12:09:44 +0000325#endif
Marc Jones97321db2020-09-28 23:35:08 -0600326
327 // Add PCIe Ports
Shuo Liu6747acb2024-03-08 19:15:28 +0800328 if (!is_stack0(socket, stack)) {
329 struct device *domain = dev_get_pci_domain(iommu);
330 struct device *dev = NULL;
331 while ((dev = dev_bus_each_child(domain->downstream, dev)))
Tim Chu5c196402022-12-13 12:09:44 +0000332 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
333 current +=
334 acpi_create_dmar_ds_pci_br_for_port(
335 current, dev, pcie_seg, false, NULL);
Marc Jones97321db2020-09-28 23:35:08 -0600336
Tim Chu5c196402022-12-13 12:09:44 +0000337#if CONFIG(SOC_INTEL_SKYLAKE_SP) || CONFIG(SOC_INTEL_COOPERLAKE_SP)
Marc Jones97321db2020-09-28 23:35:08 -0600338 // Add VMD
339 if (hob->PlatformData.VMDStackEnable[socket][stack] &&
340 stack >= PSTACK0 && stack <= PSTACK2) {
341 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
342 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
343 0, bus, VMD_DEV_NUM, VMD_FUNC_NUM);
344 current += acpi_create_dmar_ds_pci(current,
345 bus, VMD_DEV_NUM, VMD_FUNC_NUM);
346 }
Tim Chu5c196402022-12-13 12:09:44 +0000347#endif
Marc Jones97321db2020-09-28 23:35:08 -0600348 }
349
Shuo Liu08f1f052024-01-20 02:52:17 +0800350 // Add IOAT End Points (with memory resources. We don't report every End Point device.)
Shuo Liu6747acb2024-03-08 19:15:28 +0800351 if (CONFIG(HAVE_IOAT_DOMAINS) && is_dev_on_ioat_domain(iommu)) {
352 struct device *dev = NULL;
353 while ((dev = dev_find_all_devices_on_stack(socket, stack,
354 XEONSP_VENDOR_MAX, XEONSP_DEVICE_MAX, dev)))
355 /* This may also require a check for IORESOURCE_PREFETCH,
356 * but that would not include the FPU (4942/0) */
357 if ((dev->resource_list->flags &
358 (IORESOURCE_MEM | IORESOURCE_PCI64 | IORESOURCE_ASSIGNED)) ==
359 (IORESOURCE_MEM | IORESOURCE_PCI64 | IORESOURCE_ASSIGNED)) {
360 const uint32_t b = dev->upstream->secondary;
361 const uint32_t d = PCI_SLOT(dev->path.pci.devfn);
362 const uint32_t f = PCI_FUNC(dev->path.pci.devfn);
363 printk(BIOS_DEBUG, " [PCIE Endpoint Device] "
364 "Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
365 " PCI Path: 0x%x, 0x%x\n", 0, b, d, f);
366 current += acpi_create_dmar_ds_pci(current, b, d, f);
Tim Chu5c196402022-12-13 12:09:44 +0000367 }
Tim Chu5c196402022-12-13 12:09:44 +0000368 }
Tim Chu5c196402022-12-13 12:09:44 +0000369
Marc Jones97321db2020-09-28 23:35:08 -0600370 // Add HPET
Shuo Liu6747acb2024-03-08 19:15:28 +0800371 if (is_stack0(socket, stack)) {
Elyes Haouas167b7fcd2022-12-11 10:38:35 +0100372 uint16_t hpet_capid = read16p(HPET_BASE_ADDRESS);
Marc Jones97321db2020-09-28 23:35:08 -0600373 uint16_t num_hpets = (hpet_capid >> 0x08) & 0x1F; // Bits [8:12] has hpet count
374 printk(BIOS_SPEW, "%s hpet_capid: 0x%x, num_hpets: 0x%x\n",
375 __func__, hpet_capid, num_hpets);
376 //BIT 15
377 if (num_hpets && (num_hpets != 0x1f) &&
Elyes Haouas167b7fcd2022-12-11 10:38:35 +0100378 (read32p(HPET_BASE_ADDRESS + 0x100) & (0x00008000))) {
Arthur Heymans695dd292020-11-12 21:05:09 +0100379 union p2sb_bdf hpet_bdf = p2sb_get_hpet_bdf();
Marc Jones97321db2020-09-28 23:35:08 -0600380 printk(BIOS_DEBUG, " [Message-capable HPET Device] Enumeration ID: 0x%x, "
381 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
Arthur Heymans695dd292020-11-12 21:05:09 +0100382 0, hpet_bdf.bus, hpet_bdf.dev, hpet_bdf.fn);
383 current += acpi_create_dmar_ds_msi_hpet(current, 0, hpet_bdf.bus,
384 hpet_bdf.dev, hpet_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600385 }
386 }
387
388 acpi_dmar_drhd_fixup(tmp, current);
389
390 return current;
391}
392
Patrick Rudolphabc27442024-03-12 14:48:16 +0100393static unsigned long acpi_create_atsr(unsigned long current)
Marc Jones97321db2020-09-28 23:35:08 -0600394{
Patrick Rudolph425e4212024-02-15 16:30:16 +0100395 struct device *child, *dev;
396 struct resource *resource;
397
398 /*
399 * The assumption made here is that the host bridges on a socket share the
400 * PCI segment group and thus only one ATSR header needs to be emitted for
401 * a single socket.
402 * This is easier than to sort the host bridges by PCI segment group first
403 * and then generate one ATSR header for every new segment.
404 */
Patrick Rudolphabc27442024-03-12 14:48:16 +0100405 for (int socket = 0; socket < CONFIG_MAX_SOCKET; ++socket) {
Patrick Rudolphac028572023-07-14 17:44:33 +0200406 if (!soc_cpu_is_enabled(socket))
407 continue;
Marc Jones97321db2020-09-28 23:35:08 -0600408 unsigned long tmp = current;
409 bool first = true;
Marc Jones97321db2020-09-28 23:35:08 -0600410
Patrick Rudolph425e4212024-02-15 16:30:16 +0100411 dev = NULL;
412 while ((dev = dev_find_device(PCI_VID_INTEL, MMAP_VTD_CFG_REG_DEVID, dev))) {
413 /* Only add devices for the current socket */
414 if (iio_pci_domain_socket_from_dev(dev) != socket)
Marc Jones97321db2020-09-28 23:35:08 -0600415 continue;
Patrick Rudolph425e4212024-02-15 16:30:16 +0100416 /* See if there is a resource with the appropriate index. */
417 resource = probe_resource(dev, VTD_BAR_CSR);
418 if (!resource)
419 continue;
420 int stack = iio_pci_domain_stack_from_dev(dev);
421
422 uint64_t vtd_mmio_cap = read64(res2mmio(resource, VTD_EXT_CAP_LOW, 0));
423 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, vtd_base: %p, "
Marc Jones97321db2020-09-28 23:35:08 -0600424 "vtd_mmio_cap: 0x%llx\n",
Patrick Rudolph425e4212024-02-15 16:30:16 +0100425 __func__, socket, stack, dev->upstream->secondary,
426 res2mmio(resource, 0, 0), vtd_mmio_cap);
Marc Jones97321db2020-09-28 23:35:08 -0600427
428 // ATSR is applicable only for platform supporting device IOTLBs
429 // through the VT-d extended capability register
430 assert(vtd_mmio_cap != 0xffffffffffffffff);
431 if ((vtd_mmio_cap & 0x4) == 0) // BIT 2
432 continue;
433
Patrick Rudolph425e4212024-02-15 16:30:16 +0100434 if (dev->upstream->secondary == 0 && dev->upstream->segment_group == 0)
Tim Chu5c196402022-12-13 12:09:44 +0000435 continue;
436
Patrick Rudolph425e4212024-02-15 16:30:16 +0100437 for (child = dev->upstream->children; child; child = child->sibling) {
438 if ((child->hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
439 continue;
440 current +=
Tim Chu5c196402022-12-13 12:09:44 +0000441 acpi_create_dmar_ds_pci_br_for_port(
Patrick Rudolph425e4212024-02-15 16:30:16 +0100442 current, child, child->upstream->segment_group, true, &first);
Marc Jones97321db2020-09-28 23:35:08 -0600443 }
444 }
445 if (tmp != current)
446 acpi_dmar_atsr_fixup(tmp, current);
447 }
448
449 return current;
450}
451
452static unsigned long acpi_create_rmrr(unsigned long current)
453{
454 uint32_t size = ALIGN_UP(MEM_BLK_COUNT * sizeof(MEM_BLK), 0x1000);
455
456 uint32_t *ptr;
457
458 // reserve memory
459 ptr = cbmem_find(CBMEM_ID_STORAGE_DATA);
460 if (!ptr) {
461 ptr = cbmem_add(CBMEM_ID_STORAGE_DATA, size);
Elyes Haouasf1ba7d62022-09-13 10:03:44 +0200462 assert(ptr);
Marc Jones97321db2020-09-28 23:35:08 -0600463 memset(ptr, 0, size);
464 }
465
466 unsigned long tmp = current;
467 printk(BIOS_DEBUG, "[Reserved Memory Region] PCI Segment Number: 0x%x, Base Address: 0x%x, "
468 "End Address (limit): 0x%x\n",
Elyes Haouas9018dee2022-11-18 15:07:33 +0100469 0, (uint32_t)ptr, (uint32_t)((uint32_t)ptr + size - 1));
470 current += acpi_create_dmar_rmrr(current, 0, (uint32_t)ptr,
471 (uint32_t)((uint32_t)ptr + size - 1));
Marc Jones97321db2020-09-28 23:35:08 -0600472
473 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
474 "PCI Path: 0x%x, 0x%x\n",
475 0, XHCI_BUS_NUMBER, PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
476 current += acpi_create_dmar_ds_pci(current, XHCI_BUS_NUMBER,
477 PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
478
479 acpi_dmar_rmrr_fixup(tmp, current);
480
481 return current;
482}
483
484static unsigned long acpi_create_rhsa(unsigned long current)
485{
Patrick Rudolph425e4212024-02-15 16:30:16 +0100486 struct device *dev = NULL;
487 struct resource *resource;
488 int socket;
Marc Jones97321db2020-09-28 23:35:08 -0600489
Patrick Rudolph425e4212024-02-15 16:30:16 +0100490 while ((dev = dev_find_device(PCI_VID_INTEL, MMAP_VTD_CFG_REG_DEVID, dev))) {
491 /* See if there is a resource with the appropriate index. */
492 resource = probe_resource(dev, VTD_BAR_CSR);
493 if (!resource)
Patrick Rudolphac028572023-07-14 17:44:33 +0200494 continue;
Patrick Rudolphac028572023-07-14 17:44:33 +0200495
Patrick Rudolph425e4212024-02-15 16:30:16 +0100496 socket = iio_pci_domain_socket_from_dev(dev);
Marc Jones97321db2020-09-28 23:35:08 -0600497
Patrick Rudolph425e4212024-02-15 16:30:16 +0100498 printk(BIOS_DEBUG, "[Remapping Hardware Static Affinity] Base Address: %p, "
499 "Proximity Domain: 0x%x\n", res2mmio(resource, 0, 0), socket);
500 current += acpi_create_dmar_rhsa(current, (uintptr_t)res2mmio(resource, 0, 0), socket);
Marc Jones97321db2020-09-28 23:35:08 -0600501 }
502
503 return current;
504}
505
Shuo Liua0b7c062024-03-06 00:24:02 +0800506static unsigned long xeonsp_create_satc(unsigned long current, struct device *domain)
Tim Chu5c196402022-12-13 12:09:44 +0000507{
Shuo Liua0b7c062024-03-06 00:24:02 +0800508 struct device *dev = NULL;
509 while ((dev = dev_bus_each_child(domain->downstream, dev))) {
510 if (pciexp_find_extended_cap(dev, PCIE_EXT_CAP_ID_ATS, 0)) {
511 const uint32_t b = domain->downstream->secondary;
512 const uint32_t d = PCI_SLOT(dev->path.pci.devfn);
513 const uint32_t f = PCI_FUNC(dev->path.pci.devfn);
514 printk(BIOS_DEBUG, " [SATC Endpoint Device] "
515 "Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
516 " PCI Path: 0x%x, 0x%x\n", 0, b, d, f);
517 current += acpi_create_dmar_ds_pci(current, b, d, f);
Tim Chu5c196402022-12-13 12:09:44 +0000518 }
519 }
520 return current;
521}
522
523/* SoC Integrated Address Translation Cache */
Shuo Liua0b7c062024-03-06 00:24:02 +0800524static unsigned long acpi_create_satc(unsigned long current)
Tim Chu5c196402022-12-13 12:09:44 +0000525{
Tim Chu5c196402022-12-13 12:09:44 +0000526 const unsigned long tmp = current;
527
528 // Add the SATC header
529 current += acpi_create_dmar_satc(current, 0, 0);
530
Shuo Liua0b7c062024-03-06 00:24:02 +0800531 struct device *dev = NULL;
532 while ((dev = dev_find_path(dev, DEVICE_PATH_DOMAIN)))
533 current = xeonsp_create_satc(current, dev);
Tim Chu5c196402022-12-13 12:09:44 +0000534
535 acpi_dmar_satc_fixup(tmp, current);
536 return current;
537}
Tim Chu5c196402022-12-13 12:09:44 +0000538
Marc Jones97321db2020-09-28 23:35:08 -0600539static unsigned long acpi_fill_dmar(unsigned long current)
540{
Arthur Heymans83b26222020-11-06 11:50:55 +0100541 const IIO_UDS *hob = get_iio_uds();
Marc Jones97321db2020-09-28 23:35:08 -0600542
Martin L Roth014ec7c2024-03-13 17:02:25 +0000543 // DRHD - socket 0 stack 0 must be the last DRHD entry.
544 for (int socket = (CONFIG_MAX_SOCKET - 1); socket >= 0; --socket) {
545 if (!soc_cpu_is_enabled(socket))
Patrick Rudolphac028572023-07-14 17:44:33 +0200546 continue;
Martin L Roth014ec7c2024-03-13 17:02:25 +0000547 for (int stack = (MAX_LOGIC_IIO_STACK - 1); stack >= 0; --stack)
548 current = acpi_create_drhd(current, socket, stack, hob);
Marc Jones97321db2020-09-28 23:35:08 -0600549 }
550
551 // RMRR
552 current = acpi_create_rmrr(current);
553
554 // Root Port ATS Capability
Patrick Rudolphabc27442024-03-12 14:48:16 +0100555 current = acpi_create_atsr(current);
Marc Jones97321db2020-09-28 23:35:08 -0600556
557 // RHSA
558 current = acpi_create_rhsa(current);
559
Tim Chu5c196402022-12-13 12:09:44 +0000560 // SATC
Shuo Liua0b7c062024-03-06 00:24:02 +0800561 current = acpi_create_satc(current);
Tim Chu5c196402022-12-13 12:09:44 +0000562
Marc Jones97321db2020-09-28 23:35:08 -0600563 return current;
564}
565
Tim Chu5c196402022-12-13 12:09:44 +0000566unsigned long northbridge_write_acpi_tables(const struct device *device, unsigned long current,
Marc Jones97321db2020-09-28 23:35:08 -0600567 struct acpi_rsdp *rsdp)
568{
Shuo Liu255f9272023-03-29 20:14:11 +0800569 /* Only write uncore ACPI tables for domain0 */
570 if (device->path.domain.domain != 0)
571 return current;
572
Marc Jones97321db2020-09-28 23:35:08 -0600573 acpi_srat_t *srat;
574 acpi_slit_t *slit;
575 acpi_dmar_t *dmar;
Tim Chu5c196402022-12-13 12:09:44 +0000576 acpi_hmat_t *hmat;
577 acpi_cedt_t *cedt;
Marc Jones97321db2020-09-28 23:35:08 -0600578
579 const config_t *const config = config_of(device);
580
581 /* SRAT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200582 current = ALIGN_UP(current, 8);
Marc Jones97321db2020-09-28 23:35:08 -0600583 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100584 srat = (acpi_srat_t *)current;
Marc Jones97321db2020-09-28 23:35:08 -0600585 acpi_create_srat(srat, acpi_fill_srat);
586 current += srat->header.length;
587 acpi_add_table(rsdp, srat);
588
589 /* SLIT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200590 current = ALIGN_UP(current, 8);
Marc Jones97321db2020-09-28 23:35:08 -0600591 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Elyes Haouas9018dee2022-11-18 15:07:33 +0100592 slit = (acpi_slit_t *)current;
Marc Jones97321db2020-09-28 23:35:08 -0600593 acpi_create_slit(slit, acpi_fill_slit);
594 current += slit->header.length;
595 acpi_add_table(rsdp, slit);
596
Tim Chu5c196402022-12-13 12:09:44 +0000597 if (CONFIG(SOC_INTEL_HAS_CXL)) {
598 /* HMAT*/
599 current = ALIGN_UP(current, 8);
600 printk(BIOS_DEBUG, "ACPI: * HMAT at %lx\n", current);
601 hmat = (acpi_hmat_t *)current;
602 acpi_create_hmat(hmat, acpi_fill_hmat);
603 current += hmat->header.length;
604 acpi_add_table(rsdp, hmat);
605 }
606
Marc Jones97321db2020-09-28 23:35:08 -0600607 /* DMAR */
608 if (config->vtd_support) {
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200609 current = ALIGN_UP(current, 8);
Marc Jones97321db2020-09-28 23:35:08 -0600610 dmar = (acpi_dmar_t *)current;
Marc Jonesb7e591e2020-11-13 15:55:31 -0700611 enum dmar_flags flags = DMAR_INTR_REMAP;
612
613 /* SKX FSP doesn't support X2APIC, but CPX FSP does */
614 if (CONFIG(SOC_INTEL_SKYLAKE_SP))
615 flags |= DMAR_X2APIC_OPT_OUT;
616
Tim Chu5c196402022-12-13 12:09:44 +0000617 printk(BIOS_DEBUG, "ACPI: * DMAR at %lx\n", current);
Marc Jonesb7e591e2020-11-13 15:55:31 -0700618 printk(BIOS_DEBUG, "[DMA Remapping table] Flags: 0x%x\n", flags);
619 acpi_create_dmar(dmar, flags, acpi_fill_dmar);
Marc Jones97321db2020-09-28 23:35:08 -0600620 current += dmar->header.length;
621 current = acpi_align_current(current);
622 acpi_add_table(rsdp, dmar);
623 }
624
Tim Chu5c196402022-12-13 12:09:44 +0000625 if (CONFIG(SOC_INTEL_HAS_CXL)) {
626 /* CEDT: CXL Early Discovery Table */
627 if (get_cxl_node_count() > 0) {
628 current = ALIGN_UP(current, 8);
629 printk(BIOS_DEBUG, "ACPI: * CEDT at %lx\n", current);
630 cedt = (acpi_cedt_t *)current;
631 acpi_create_cedt(cedt, acpi_fill_cedt);
632 current += cedt->header.length;
633 acpi_add_table(rsdp, cedt);
634 }
635 }
636
637 if (CONFIG(SOC_ACPI_HEST)) {
638 printk(BIOS_DEBUG, "ACPI: * HEST at %lx\n", current);
Rocky Phagurad4db36e2021-04-03 08:49:32 -0700639 current = hest_create(current, rsdp);
Tim Chu5c196402022-12-13 12:09:44 +0000640 }
Rocky Phagurad4db36e2021-04-03 08:49:32 -0700641
Marc Jones97321db2020-09-28 23:35:08 -0600642 return current;
643}