blob: 0519d4451bf97a86082da24701dfff1c49bdd0f9 [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>
Marc Jones97321db2020-09-28 23:35:08 -06005#include <assert.h>
6#include <cbmem.h>
7#include <device/mmio.h>
8#include <device/pci.h>
Marc Jones97321db2020-09-28 23:35:08 -06009#include <soc/acpi.h>
10#include <soc/cpu.h>
Rocky Phagurad4db36e2021-04-03 08:49:32 -070011#include <soc/hest.h>
Marc Jones97321db2020-09-28 23:35:08 -060012#include <soc/iomap.h>
13#include <soc/pci_devs.h>
14#include <soc/soc_util.h>
Marc Jones18960ce2020-11-02 12:41:12 -070015#include <soc/util.h>
Arthur Heymans695dd292020-11-12 21:05:09 +010016#include <intelblocks/p2sb.h>
Marc Jones97321db2020-09-28 23:35:08 -060017
18#include "chip.h"
19
20/* Northbridge(NUMA) ACPI table generation. SRAT, SLIT, etc */
21
22unsigned long acpi_create_srat_lapics(unsigned long current)
23{
24 struct device *cpu;
25 unsigned int cpu_index = 0;
26
27 for (cpu = all_devices; cpu; cpu = cpu->next) {
28 if ((cpu->path.type != DEVICE_PATH_APIC) ||
29 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
30 continue;
31 }
32 if (!cpu->enabled)
33 continue;
34 printk(BIOS_DEBUG, "SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n",
35 cpu_index, cpu->path.apic.node_id, cpu->path.apic.apic_id);
36 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current,
37 cpu->path.apic.node_id, cpu->path.apic.apic_id);
38 cpu_index++;
39 }
40 return current;
41}
42
43static unsigned int get_srat_memory_entries(acpi_srat_mem_t *srat_mem)
44{
45 const struct SystemMemoryMapHob *memory_map;
46 unsigned int mmap_index;
47
48 memory_map = get_system_memory_map();
49 assert(memory_map != NULL);
50 printk(BIOS_DEBUG, "memory_map: %p\n", memory_map);
51
52 mmap_index = 0;
53 for (int e = 0; e < memory_map->numberEntries; ++e) {
54 const struct SystemMemoryMapElement *mem_element = &memory_map->Element[e];
55 uint64_t addr =
56 (uint64_t) ((uint64_t)mem_element->BaseAddress <<
57 MEM_ADDR_64MB_SHIFT_BITS);
58 uint64_t size =
59 (uint64_t) ((uint64_t)mem_element->ElementSize <<
60 MEM_ADDR_64MB_SHIFT_BITS);
61
62 printk(BIOS_DEBUG, "memory_map %d addr: 0x%llx, BaseAddress: 0x%x, size: 0x%llx, "
63 "ElementSize: 0x%x, reserved: %d\n",
64 e, addr, mem_element->BaseAddress, size,
65 mem_element->ElementSize, (mem_element->Type & MEM_TYPE_RESERVED));
66
67 assert(mmap_index < MAX_ACPI_MEMORY_AFFINITY_COUNT);
68
69 /* skip reserved memory region */
70 if (mem_element->Type & MEM_TYPE_RESERVED)
71 continue;
72
73 /* skip if this address is already added */
74 bool skip = false;
75 for (int idx = 0; idx < mmap_index; ++idx) {
76 uint64_t base_addr = ((uint64_t)srat_mem[idx].base_address_high << 32) +
77 srat_mem[idx].base_address_low;
78 if (addr == base_addr) {
79 skip = true;
80 break;
81 }
82 }
83 if (skip)
84 continue;
85
86 srat_mem[mmap_index].type = 1; /* Memory affinity structure */
87 srat_mem[mmap_index].length = sizeof(acpi_srat_mem_t);
88 srat_mem[mmap_index].base_address_low = (uint32_t) (addr & 0xffffffff);
89 srat_mem[mmap_index].base_address_high = (uint32_t) (addr >> 32);
90 srat_mem[mmap_index].length_low = (uint32_t) (size & 0xffffffff);
91 srat_mem[mmap_index].length_high = (uint32_t) (size >> 32);
92 srat_mem[mmap_index].proximity_domain = mem_element->SocketId;
93 srat_mem[mmap_index].flags = SRAT_ACPI_MEMORY_ENABLED;
94 if ((mem_element->Type & MEMTYPE_VOLATILE_MASK) == 0)
95 srat_mem[mmap_index].flags |= SRAT_ACPI_MEMORY_NONVOLATILE;
96 ++mmap_index;
97 }
98
99 return mmap_index;
100}
101
102static unsigned long acpi_fill_srat(unsigned long current)
103{
104 acpi_srat_mem_t srat_mem[MAX_ACPI_MEMORY_AFFINITY_COUNT];
105 unsigned int mem_count;
106
107 /* create all subtables for processors */
108 current = acpi_create_srat_lapics(current);
109
110 mem_count = get_srat_memory_entries(srat_mem);
111 for (int i = 0; i < mem_count; ++i) {
112 printk(BIOS_DEBUG, "adding srat memory %d entry length: %d, addr: 0x%x%x, "
113 "length: 0x%x%x, proximity_domain: %d, flags: %x\n",
114 i, srat_mem[i].length,
115 srat_mem[i].base_address_high, srat_mem[i].base_address_low,
116 srat_mem[i].length_high, srat_mem[i].length_low,
117 srat_mem[i].proximity_domain, srat_mem[i].flags);
118 memcpy((acpi_srat_mem_t *)current, &srat_mem[i], sizeof(srat_mem[i]));
119 current += srat_mem[i].length;
120 }
121
122 return current;
123}
124
125static unsigned long acpi_fill_slit(unsigned long current)
126{
Marc Jones70907b02020-10-28 17:00:31 -0600127 unsigned int nodes = soc_get_num_cpus();
Marc Jones97321db2020-09-28 23:35:08 -0600128
129 uint8_t *p = (uint8_t *)current;
130 memset(p, 0, 8 + nodes * nodes);
131 *p = (uint8_t)nodes;
132 p += 8;
133
134 /* this assumes fully connected socket topology */
135 for (int i = 0; i < nodes; i++) {
136 for (int j = 0; j < nodes; j++) {
137 if (i == j)
138 p[i*nodes+j] = 10;
139 else
140 p[i*nodes+j] = 16;
141 }
142 }
143
144 current += 8 + nodes * nodes;
145 return current;
146}
147
148/*
Marc Jones97321db2020-09-28 23:35:08 -0600149 * This function adds PCIe bridge device entry in DMAR table. If it is called
150 * in the context of ATSR subtable, it adds ATSR subtable when it is first called.
151 */
152static unsigned long acpi_create_dmar_ds_pci_br_for_port(unsigned long current,
Jacob Garber6df38702020-10-24 16:23:45 -0600153 int port, int stack, const IIO_RESOURCE_INSTANCE *iio_resource, uint32_t pcie_seg,
Marc Jones97321db2020-09-28 23:35:08 -0600154 bool is_atsr, bool *first)
155{
156
Marc Jones995a7e22020-10-28 17:08:54 -0600157 if (soc_get_stack_for_port(port) != stack)
Marc Jones97321db2020-09-28 23:35:08 -0600158 return 0;
159
Jacob Garber6df38702020-10-24 16:23:45 -0600160 const uint32_t bus = iio_resource->StackRes[stack].BusBase;
161 const uint32_t dev = iio_resource->PcieInfo.PortInfo[port].Device;
162 const uint32_t func = iio_resource->PcieInfo.PortInfo[port].Function;
Marc Jones97321db2020-09-28 23:35:08 -0600163
Nico Huberf4f365f2021-10-14 18:16:39 +0200164 const uint32_t id = pci_s_read_config32(PCI_DEV(bus, dev, func),
Marc Jones97321db2020-09-28 23:35:08 -0600165 PCI_VENDOR_ID);
166 if (id == 0xffffffff)
167 return 0;
168
169 unsigned long atsr_size = 0;
170 unsigned long pci_br_size = 0;
Elyes HAOUASfa999822022-01-27 14:27:05 +0100171 if (is_atsr && first && *first) {
Marc Jones97321db2020-09-28 23:35:08 -0600172 printk(BIOS_DEBUG, "[Root Port ATS Capability] Flags: 0x%x, "
173 "PCI Segment Number: 0x%x\n", 0, pcie_seg);
174 atsr_size = acpi_create_dmar_atsr(current, 0, pcie_seg);
175 *first = false;
176 }
177
178 printk(BIOS_DEBUG, " [PCI Bridge Device] Enumeration ID: 0x%x, "
179 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
180 0, bus, dev, func);
181 pci_br_size = acpi_create_dmar_ds_pci_br(current + atsr_size, bus, dev, func);
182
183 return (atsr_size + pci_br_size);
184}
185
186static unsigned long acpi_create_drhd(unsigned long current, int socket,
187 int stack, const IIO_UDS *hob)
188{
Marc Jones97321db2020-09-28 23:35:08 -0600189 uint32_t enum_id;
190 unsigned long tmp = current;
191
192 uint32_t bus = hob->PlatformData.IIO_resource[socket].StackRes[stack].BusBase;
193 uint32_t pcie_seg = hob->PlatformData.CpuQpiInfo[socket].PcieSegment;
194 uint32_t reg_base =
195 hob->PlatformData.IIO_resource[socket].StackRes[stack].VtdBarAddress;
196 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, pcie_seg: 0x%x, reg_base: 0x%x\n",
197 __func__, socket, stack, bus, pcie_seg, reg_base);
198
199 /* Do not generate DRHD for non-PCIe stack */
200 if (!reg_base)
201 return current;
202
Arthur Heymansa1c4ad32021-05-04 18:40:28 +0200203 // Add DRHD Hardware Unit
204 if (socket == 0 && stack == CSTACK) {
205 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
206 "Register Base Address: 0x%x\n",
207 DRHD_INCLUDE_PCI_ALL, pcie_seg, reg_base);
208 current += acpi_create_dmar_drhd(current, DRHD_INCLUDE_PCI_ALL,
209 pcie_seg, reg_base);
210 } else {
211 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
212 "Register Base Address: 0x%x\n", 0, pcie_seg, reg_base);
213 current += acpi_create_dmar_drhd(current, 0, pcie_seg, reg_base);
214 }
215
Marc Jones97321db2020-09-28 23:35:08 -0600216 // Add PCH IOAPIC
217 if (socket == 0 && stack == CSTACK) {
Arthur Heymans6e425e12020-11-12 21:12:05 +0100218 union p2sb_bdf ioapic_bdf = p2sb_get_ioapic_bdf();
Marc Jones97321db2020-09-28 23:35:08 -0600219 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
220 "PCI Path: 0x%x, 0x%x\n",
Arthur Heymans6e425e12020-11-12 21:12:05 +0100221 PCH_IOAPIC_ID, ioapic_bdf.bus, ioapic_bdf.dev, ioapic_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600222 current += acpi_create_dmar_ds_ioapic(current, PCH_IOAPIC_ID,
Arthur Heymans6e425e12020-11-12 21:12:05 +0100223 ioapic_bdf.bus, ioapic_bdf.dev, ioapic_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600224 }
225
226 // Add IOAPIC entry
Arthur Heymansa1cc5572020-11-06 12:53:33 +0100227 enum_id = soc_get_iio_ioapicid(socket, stack);
Marc Jones97321db2020-09-28 23:35:08 -0600228 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
229 "PCI Path: 0x%x, 0x%x\n", enum_id, bus, APIC_DEV_NUM, APIC_FUNC_NUM);
230 current += acpi_create_dmar_ds_ioapic(current, enum_id, bus,
231 APIC_DEV_NUM, APIC_FUNC_NUM);
232
233 // Add CBDMA devices for CSTACK
234 if (socket != 0 && stack == CSTACK) {
235 for (int cbdma_func_id = 0; cbdma_func_id < 8; ++cbdma_func_id) {
236 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
237 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
238 0, bus, CBDMA_DEV_NUM, cbdma_func_id);
239 current += acpi_create_dmar_ds_pci(current,
240 bus, CBDMA_DEV_NUM, cbdma_func_id);
241 }
242 }
243
244 // Add PCIe Ports
245 if (socket != 0 || stack != CSTACK) {
246 IIO_RESOURCE_INSTANCE iio_resource =
247 hob->PlatformData.IIO_resource[socket];
248 for (int p = PORT_0; p < MAX_PORTS; ++p)
249 current += acpi_create_dmar_ds_pci_br_for_port(current, p, stack,
Jacob Garber6df38702020-10-24 16:23:45 -0600250 &iio_resource, pcie_seg, false, NULL);
Marc Jones97321db2020-09-28 23:35:08 -0600251
252 // Add VMD
253 if (hob->PlatformData.VMDStackEnable[socket][stack] &&
254 stack >= PSTACK0 && stack <= PSTACK2) {
255 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
256 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
257 0, bus, VMD_DEV_NUM, VMD_FUNC_NUM);
258 current += acpi_create_dmar_ds_pci(current,
259 bus, VMD_DEV_NUM, VMD_FUNC_NUM);
260 }
261 }
262
263 // Add HPET
264 if (socket == 0 && stack == CSTACK) {
265 uint16_t hpet_capid = read16((void *)HPET_BASE_ADDRESS);
266 uint16_t num_hpets = (hpet_capid >> 0x08) & 0x1F; // Bits [8:12] has hpet count
267 printk(BIOS_SPEW, "%s hpet_capid: 0x%x, num_hpets: 0x%x\n",
268 __func__, hpet_capid, num_hpets);
269 //BIT 15
270 if (num_hpets && (num_hpets != 0x1f) &&
271 (read32((void *)(HPET_BASE_ADDRESS + 0x100)) & (0x00008000))) {
Arthur Heymans695dd292020-11-12 21:05:09 +0100272 union p2sb_bdf hpet_bdf = p2sb_get_hpet_bdf();
Marc Jones97321db2020-09-28 23:35:08 -0600273 printk(BIOS_DEBUG, " [Message-capable HPET Device] Enumeration ID: 0x%x, "
274 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
Arthur Heymans695dd292020-11-12 21:05:09 +0100275 0, hpet_bdf.bus, hpet_bdf.dev, hpet_bdf.fn);
276 current += acpi_create_dmar_ds_msi_hpet(current, 0, hpet_bdf.bus,
277 hpet_bdf.dev, hpet_bdf.fn);
Marc Jones97321db2020-09-28 23:35:08 -0600278 }
279 }
280
281 acpi_dmar_drhd_fixup(tmp, current);
282
283 return current;
284}
285
286static unsigned long acpi_create_atsr(unsigned long current, const IIO_UDS *hob)
287{
288 for (int socket = 0; socket < hob->PlatformData.numofIIO; ++socket) {
289 uint32_t pcie_seg = hob->PlatformData.CpuQpiInfo[socket].PcieSegment;
290 unsigned long tmp = current;
291 bool first = true;
292 IIO_RESOURCE_INSTANCE iio_resource =
293 hob->PlatformData.IIO_resource[socket];
294
295 for (int stack = 0; stack <= PSTACK2; ++stack) {
296 uint32_t bus = iio_resource.StackRes[stack].BusBase;
297 uint32_t vtd_base = iio_resource.StackRes[stack].VtdBarAddress;
298 if (!vtd_base)
299 continue;
300 uint64_t vtd_mmio_cap = read64((void *)(vtd_base + VTD_EXT_CAP_LOW));
301 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, vtd_base: 0x%x, "
302 "vtd_mmio_cap: 0x%llx\n",
303 __func__, socket, stack, bus, vtd_base, vtd_mmio_cap);
304
305 // ATSR is applicable only for platform supporting device IOTLBs
306 // through the VT-d extended capability register
307 assert(vtd_mmio_cap != 0xffffffffffffffff);
308 if ((vtd_mmio_cap & 0x4) == 0) // BIT 2
309 continue;
310
311 for (int p = PORT_0; p < MAX_PORTS; ++p) {
312 if (socket == 0 && p == PORT_0)
313 continue;
314 current += acpi_create_dmar_ds_pci_br_for_port(current, p,
Jacob Garber6df38702020-10-24 16:23:45 -0600315 stack, &iio_resource, pcie_seg, true, &first);
Marc Jones97321db2020-09-28 23:35:08 -0600316 }
317 }
318 if (tmp != current)
319 acpi_dmar_atsr_fixup(tmp, current);
320 }
321
322 return current;
323}
324
325static unsigned long acpi_create_rmrr(unsigned long current)
326{
327 uint32_t size = ALIGN_UP(MEM_BLK_COUNT * sizeof(MEM_BLK), 0x1000);
328
329 uint32_t *ptr;
330
331 // reserve memory
332 ptr = cbmem_find(CBMEM_ID_STORAGE_DATA);
333 if (!ptr) {
334 ptr = cbmem_add(CBMEM_ID_STORAGE_DATA, size);
335 assert(ptr != NULL);
336 memset(ptr, 0, size);
337 }
338
339 unsigned long tmp = current;
340 printk(BIOS_DEBUG, "[Reserved Memory Region] PCI Segment Number: 0x%x, Base Address: 0x%x, "
341 "End Address (limit): 0x%x\n",
342 0, (uint32_t) ptr, (uint32_t) ((uint32_t) ptr + size - 1));
343 current += acpi_create_dmar_rmrr(current, 0, (uint32_t) ptr,
344 (uint32_t) ((uint32_t) ptr + size - 1));
345
346 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
347 "PCI Path: 0x%x, 0x%x\n",
348 0, XHCI_BUS_NUMBER, PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
349 current += acpi_create_dmar_ds_pci(current, XHCI_BUS_NUMBER,
350 PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
351
352 acpi_dmar_rmrr_fixup(tmp, current);
353
354 return current;
355}
356
357static unsigned long acpi_create_rhsa(unsigned long current)
358{
Arthur Heymans83b26222020-11-06 11:50:55 +0100359 const IIO_UDS *hob = get_iio_uds();
Marc Jones97321db2020-09-28 23:35:08 -0600360
361 for (int socket = 0; socket < hob->PlatformData.numofIIO; ++socket) {
362 IIO_RESOURCE_INSTANCE iio_resource =
363 hob->PlatformData.IIO_resource[socket];
364 for (int stack = 0; stack <= PSTACK2; ++stack) {
365 uint32_t vtd_base = iio_resource.StackRes[stack].VtdBarAddress;
366 if (!vtd_base)
367 continue;
368
369 printk(BIOS_DEBUG, "[Remapping Hardware Static Affinity] Base Address: 0x%x, "
370 "Proximity Domain: 0x%x\n", vtd_base, socket);
371 current += acpi_create_dmar_rhsa(current, vtd_base, socket);
372 }
373 }
374
375 return current;
376}
377
378static unsigned long acpi_fill_dmar(unsigned long current)
379{
Arthur Heymans83b26222020-11-06 11:50:55 +0100380 const IIO_UDS *hob = get_iio_uds();
Marc Jones97321db2020-09-28 23:35:08 -0600381
382 // DRHD
383 for (int iio = 1; iio <= hob->PlatformData.numofIIO; ++iio) {
384 int socket = iio;
385 if (socket == hob->PlatformData.numofIIO) // socket 0 should be last DRHD entry
386 socket = 0;
387
388 if (socket == 0) {
389 for (int stack = 1; stack <= PSTACK2; ++stack)
390 current = acpi_create_drhd(current, socket, stack, hob);
391 current = acpi_create_drhd(current, socket, CSTACK, hob);
392 } else {
393 for (int stack = 0; stack <= PSTACK2; ++stack)
394 current = acpi_create_drhd(current, socket, stack, hob);
395 }
396 }
397
398 // RMRR
399 current = acpi_create_rmrr(current);
400
401 // Root Port ATS Capability
402 current = acpi_create_atsr(current, hob);
403
404 // RHSA
405 current = acpi_create_rhsa(current);
406
407 return current;
408}
409
410unsigned long northbridge_write_acpi_tables(const struct device *device,
411 unsigned long current,
412 struct acpi_rsdp *rsdp)
413{
414 acpi_srat_t *srat;
415 acpi_slit_t *slit;
416 acpi_dmar_t *dmar;
417
418 const config_t *const config = config_of(device);
419
420 /* SRAT */
421 current = ALIGN(current, 8);
422 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
423 srat = (acpi_srat_t *) current;
424 acpi_create_srat(srat, acpi_fill_srat);
425 current += srat->header.length;
426 acpi_add_table(rsdp, srat);
427
428 /* SLIT */
429 current = ALIGN(current, 8);
430 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
431 slit = (acpi_slit_t *) current;
432 acpi_create_slit(slit, acpi_fill_slit);
433 current += slit->header.length;
434 acpi_add_table(rsdp, slit);
435
436 /* DMAR */
437 if (config->vtd_support) {
438 current = ALIGN(current, 8);
439 dmar = (acpi_dmar_t *)current;
Marc Jonesb7e591e2020-11-13 15:55:31 -0700440 enum dmar_flags flags = DMAR_INTR_REMAP;
441
442 /* SKX FSP doesn't support X2APIC, but CPX FSP does */
443 if (CONFIG(SOC_INTEL_SKYLAKE_SP))
444 flags |= DMAR_X2APIC_OPT_OUT;
445
Marc Jones97321db2020-09-28 23:35:08 -0600446 printk(BIOS_DEBUG, "ACPI: * DMAR\n");
Marc Jonesb7e591e2020-11-13 15:55:31 -0700447 printk(BIOS_DEBUG, "[DMA Remapping table] Flags: 0x%x\n", flags);
448 acpi_create_dmar(dmar, flags, acpi_fill_dmar);
Marc Jones97321db2020-09-28 23:35:08 -0600449 current += dmar->header.length;
450 current = acpi_align_current(current);
451 acpi_add_table(rsdp, dmar);
452 }
453
Rocky Phagurad4db36e2021-04-03 08:49:32 -0700454 if (CONFIG(SOC_ACPI_HEST))
455 current = hest_create(current, rsdp);
456
Marc Jones97321db2020-09-28 23:35:08 -0600457 return current;
458}