blob: 1329feb0d65ceb831bf535d8acba632b804de39c [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>
4#include <assert.h>
5#include <cbmem.h>
6#include <device/mmio.h>
7#include <device/pci.h>
8#include <intelblocks/acpi.h>
9#include <soc/acpi.h>
10#include <soc/cpu.h>
11#include <soc/iomap.h>
12#include <soc/pci_devs.h>
13#include <soc/soc_util.h>
14
15#include "chip.h"
16
17/* Northbridge(NUMA) ACPI table generation. SRAT, SLIT, etc */
18
19unsigned long acpi_create_srat_lapics(unsigned long current)
20{
21 struct device *cpu;
22 unsigned int cpu_index = 0;
23
24 for (cpu = all_devices; cpu; cpu = cpu->next) {
25 if ((cpu->path.type != DEVICE_PATH_APIC) ||
26 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER)) {
27 continue;
28 }
29 if (!cpu->enabled)
30 continue;
31 printk(BIOS_DEBUG, "SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n",
32 cpu_index, cpu->path.apic.node_id, cpu->path.apic.apic_id);
33 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current,
34 cpu->path.apic.node_id, cpu->path.apic.apic_id);
35 cpu_index++;
36 }
37 return current;
38}
39
40static unsigned int get_srat_memory_entries(acpi_srat_mem_t *srat_mem)
41{
42 const struct SystemMemoryMapHob *memory_map;
43 unsigned int mmap_index;
44
45 memory_map = get_system_memory_map();
46 assert(memory_map != NULL);
47 printk(BIOS_DEBUG, "memory_map: %p\n", memory_map);
48
49 mmap_index = 0;
50 for (int e = 0; e < memory_map->numberEntries; ++e) {
51 const struct SystemMemoryMapElement *mem_element = &memory_map->Element[e];
52 uint64_t addr =
53 (uint64_t) ((uint64_t)mem_element->BaseAddress <<
54 MEM_ADDR_64MB_SHIFT_BITS);
55 uint64_t size =
56 (uint64_t) ((uint64_t)mem_element->ElementSize <<
57 MEM_ADDR_64MB_SHIFT_BITS);
58
59 printk(BIOS_DEBUG, "memory_map %d addr: 0x%llx, BaseAddress: 0x%x, size: 0x%llx, "
60 "ElementSize: 0x%x, reserved: %d\n",
61 e, addr, mem_element->BaseAddress, size,
62 mem_element->ElementSize, (mem_element->Type & MEM_TYPE_RESERVED));
63
64 assert(mmap_index < MAX_ACPI_MEMORY_AFFINITY_COUNT);
65
66 /* skip reserved memory region */
67 if (mem_element->Type & MEM_TYPE_RESERVED)
68 continue;
69
70 /* skip if this address is already added */
71 bool skip = false;
72 for (int idx = 0; idx < mmap_index; ++idx) {
73 uint64_t base_addr = ((uint64_t)srat_mem[idx].base_address_high << 32) +
74 srat_mem[idx].base_address_low;
75 if (addr == base_addr) {
76 skip = true;
77 break;
78 }
79 }
80 if (skip)
81 continue;
82
83 srat_mem[mmap_index].type = 1; /* Memory affinity structure */
84 srat_mem[mmap_index].length = sizeof(acpi_srat_mem_t);
85 srat_mem[mmap_index].base_address_low = (uint32_t) (addr & 0xffffffff);
86 srat_mem[mmap_index].base_address_high = (uint32_t) (addr >> 32);
87 srat_mem[mmap_index].length_low = (uint32_t) (size & 0xffffffff);
88 srat_mem[mmap_index].length_high = (uint32_t) (size >> 32);
89 srat_mem[mmap_index].proximity_domain = mem_element->SocketId;
90 srat_mem[mmap_index].flags = SRAT_ACPI_MEMORY_ENABLED;
91 if ((mem_element->Type & MEMTYPE_VOLATILE_MASK) == 0)
92 srat_mem[mmap_index].flags |= SRAT_ACPI_MEMORY_NONVOLATILE;
93 ++mmap_index;
94 }
95
96 return mmap_index;
97}
98
99static unsigned long acpi_fill_srat(unsigned long current)
100{
101 acpi_srat_mem_t srat_mem[MAX_ACPI_MEMORY_AFFINITY_COUNT];
102 unsigned int mem_count;
103
104 /* create all subtables for processors */
105 current = acpi_create_srat_lapics(current);
106
107 mem_count = get_srat_memory_entries(srat_mem);
108 for (int i = 0; i < mem_count; ++i) {
109 printk(BIOS_DEBUG, "adding srat memory %d entry length: %d, addr: 0x%x%x, "
110 "length: 0x%x%x, proximity_domain: %d, flags: %x\n",
111 i, srat_mem[i].length,
112 srat_mem[i].base_address_high, srat_mem[i].base_address_low,
113 srat_mem[i].length_high, srat_mem[i].length_low,
114 srat_mem[i].proximity_domain, srat_mem[i].flags);
115 memcpy((acpi_srat_mem_t *)current, &srat_mem[i], sizeof(srat_mem[i]));
116 current += srat_mem[i].length;
117 }
118
119 return current;
120}
121
122static unsigned long acpi_fill_slit(unsigned long current)
123{
124#if (CONFIG(SOC_INTEL_COOPERLAKE_SP))
125 unsigned int nodes = xeon_sp_get_socket_count();
126#endif /* SOC_INTEL_COOPERLAKE_SP */
127
128#if (CONFIG(SOC_INTEL_SKYLAKE_SP))
129 int nodes = get_cpu_count();
130#endif /* SOC_INTEL_SKYLAKE_SP */
131
132 uint8_t *p = (uint8_t *)current;
133 memset(p, 0, 8 + nodes * nodes);
134 *p = (uint8_t)nodes;
135 p += 8;
136
137 /* this assumes fully connected socket topology */
138 for (int i = 0; i < nodes; i++) {
139 for (int j = 0; j < nodes; j++) {
140 if (i == j)
141 p[i*nodes+j] = 10;
142 else
143 p[i*nodes+j] = 16;
144 }
145 }
146
147 current += 8 + nodes * nodes;
148 return current;
149}
150
151/*
152 * EX: CPX-SP
153 * Ports Stack Stack(HOB) IioConfigIou
154 * ==========================================
155 * 0 CSTACK stack 0 IOU0
156 * 1A..1D PSTACKZ stack 1 IOU1
157 * 2A..2D PSTACK1 stack 2 IOU2
158 * 3A..3D PSTACK2 stack 4 IOU3
159 */
160static int get_stack_for_port(int port)
161{
162#if (CONFIG(SOC_INTEL_COOPERLAKE_SP))
163 if (port == PORT_0)
164 return CSTACK;
165 else if (port >= PORT_1A && port <= PORT_1D)
166 return PSTACK0;
167 else if (port >= PORT_2A && port <= PORT_2D)
168 return PSTACK1;
169 else if (port >= PORT_3A && port <= PORT_3D)
170 return PSTACK2;
171 else
172 return -1;
173#endif /* SOC_INTEL_COOPERLAKE_SP */
174
175#if (CONFIG(SOC_INTEL_SKYLAKE_SP))
176 if (port == PORT_0)
177 return CSTACK;
178 else if (port >= PORT_1A && port <= PORT_1D)
179 return PSTACK0;
180 else if (port >= PORT_2A && port <= PORT_2D)
181 return PSTACK1;
182 else if (port >= PORT_3A && port <= PORT_3D)
183 return PSTACK2;
184 else if (port >= PORT_4A && port <= PORT_4D)
185 return PSTACK3; // MCP0
186 else if (port >= PORT_5A && port <= PORT_5D)
187 return PSTACK4; // MCP1
188 else
189 return -1;
190#endif /* SOC_INTEL_SKYLAKE_SP */
191}
192
193/*
194 * This function adds PCIe bridge device entry in DMAR table. If it is called
195 * in the context of ATSR subtable, it adds ATSR subtable when it is first called.
196 */
197static unsigned long acpi_create_dmar_ds_pci_br_for_port(unsigned long current,
198 int port, int stack, IIO_RESOURCE_INSTANCE iio_resource, uint32_t pcie_seg,
199 bool is_atsr, bool *first)
200{
201
202 if (get_stack_for_port(port) != stack)
203 return 0;
204
205 const uint32_t bus = iio_resource.StackRes[stack].BusBase;
206 const uint32_t dev = iio_resource.PcieInfo.PortInfo[port].Device;
207 const uint32_t func = iio_resource.PcieInfo.PortInfo[port].Function;
208
209 const uint32_t id = pci_mmio_read_config32(PCI_DEV(bus, dev, func),
210 PCI_VENDOR_ID);
211 if (id == 0xffffffff)
212 return 0;
213
214 unsigned long atsr_size = 0;
215 unsigned long pci_br_size = 0;
216 if (is_atsr == true && first && *first == true) {
217 printk(BIOS_DEBUG, "[Root Port ATS Capability] Flags: 0x%x, "
218 "PCI Segment Number: 0x%x\n", 0, pcie_seg);
219 atsr_size = acpi_create_dmar_atsr(current, 0, pcie_seg);
220 *first = false;
221 }
222
223 printk(BIOS_DEBUG, " [PCI Bridge Device] Enumeration ID: 0x%x, "
224 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
225 0, bus, dev, func);
226 pci_br_size = acpi_create_dmar_ds_pci_br(current + atsr_size, bus, dev, func);
227
228 return (atsr_size + pci_br_size);
229}
230
231static unsigned long acpi_create_drhd(unsigned long current, int socket,
232 int stack, const IIO_UDS *hob)
233{
234 int IoApicID[] = {
235 // socket 0
236 PC00_IOAPIC_ID, PC01_IOAPIC_ID, PC02_IOAPIC_ID, PC03_IOAPIC_ID,
237 PC04_IOAPIC_ID, PC05_IOAPIC_ID,
238 // socket 1
239 PC06_IOAPIC_ID, PC07_IOAPIC_ID, PC08_IOAPIC_ID, PC09_IOAPIC_ID,
240 PC10_IOAPIC_ID, PC11_IOAPIC_ID,
241 };
242
243 uint32_t enum_id;
244 unsigned long tmp = current;
245
246 uint32_t bus = hob->PlatformData.IIO_resource[socket].StackRes[stack].BusBase;
247 uint32_t pcie_seg = hob->PlatformData.CpuQpiInfo[socket].PcieSegment;
248 uint32_t reg_base =
249 hob->PlatformData.IIO_resource[socket].StackRes[stack].VtdBarAddress;
250 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, pcie_seg: 0x%x, reg_base: 0x%x\n",
251 __func__, socket, stack, bus, pcie_seg, reg_base);
252
253 /* Do not generate DRHD for non-PCIe stack */
254 if (!reg_base)
255 return current;
256
257 // Add DRHD Hardware Unit
258 if (socket == 0 && stack == CSTACK) {
259 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
260 "Register Base Address: 0x%x\n",
261 DRHD_INCLUDE_PCI_ALL, pcie_seg, reg_base);
262 current += acpi_create_dmar_drhd(current, DRHD_INCLUDE_PCI_ALL,
263 pcie_seg, reg_base);
264 } else {
265 printk(BIOS_DEBUG, "[Hardware Unit Definition] Flags: 0x%x, PCI Segment Number: 0x%x, "
266 "Register Base Address: 0x%x\n", 0, pcie_seg, reg_base);
267 current += acpi_create_dmar_drhd(current, 0, pcie_seg, reg_base);
268 }
269
270 // Add PCH IOAPIC
271 if (socket == 0 && stack == CSTACK) {
272 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
273 "PCI Path: 0x%x, 0x%x\n",
274 PCH_IOAPIC_ID, PCH_IOAPIC_BUS_NUMBER,
275 PCH_IOAPIC_DEV_NUM, PCH_IOAPIC_FUNC_NUM);
276 current += acpi_create_dmar_ds_ioapic(current, PCH_IOAPIC_ID,
277 PCH_IOAPIC_BUS_NUMBER, PCH_IOAPIC_DEV_NUM, PCH_IOAPIC_FUNC_NUM);
278 }
279
280 // Add IOAPIC entry
281 enum_id = IoApicID[(socket*MAX_IIO_STACK)+stack];
282 printk(BIOS_DEBUG, " [IOAPIC Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
283 "PCI Path: 0x%x, 0x%x\n", enum_id, bus, APIC_DEV_NUM, APIC_FUNC_NUM);
284 current += acpi_create_dmar_ds_ioapic(current, enum_id, bus,
285 APIC_DEV_NUM, APIC_FUNC_NUM);
286
287 // Add CBDMA devices for CSTACK
288 if (socket != 0 && stack == CSTACK) {
289 for (int cbdma_func_id = 0; cbdma_func_id < 8; ++cbdma_func_id) {
290 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
291 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
292 0, bus, CBDMA_DEV_NUM, cbdma_func_id);
293 current += acpi_create_dmar_ds_pci(current,
294 bus, CBDMA_DEV_NUM, cbdma_func_id);
295 }
296 }
297
298 // Add PCIe Ports
299 if (socket != 0 || stack != CSTACK) {
300 IIO_RESOURCE_INSTANCE iio_resource =
301 hob->PlatformData.IIO_resource[socket];
302 for (int p = PORT_0; p < MAX_PORTS; ++p)
303 current += acpi_create_dmar_ds_pci_br_for_port(current, p, stack,
304 iio_resource, pcie_seg, false, NULL);
305
306 // Add VMD
307 if (hob->PlatformData.VMDStackEnable[socket][stack] &&
308 stack >= PSTACK0 && stack <= PSTACK2) {
309 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, "
310 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
311 0, bus, VMD_DEV_NUM, VMD_FUNC_NUM);
312 current += acpi_create_dmar_ds_pci(current,
313 bus, VMD_DEV_NUM, VMD_FUNC_NUM);
314 }
315 }
316
317 // Add HPET
318 if (socket == 0 && stack == CSTACK) {
319 uint16_t hpet_capid = read16((void *)HPET_BASE_ADDRESS);
320 uint16_t num_hpets = (hpet_capid >> 0x08) & 0x1F; // Bits [8:12] has hpet count
321 printk(BIOS_SPEW, "%s hpet_capid: 0x%x, num_hpets: 0x%x\n",
322 __func__, hpet_capid, num_hpets);
323 //BIT 15
324 if (num_hpets && (num_hpets != 0x1f) &&
325 (read32((void *)(HPET_BASE_ADDRESS + 0x100)) & (0x00008000))) {
326 printk(BIOS_DEBUG, " [Message-capable HPET Device] Enumeration ID: 0x%x, "
327 "PCI Bus Number: 0x%x, PCI Path: 0x%x, 0x%x\n",
328 0, HPET_BUS_NUM, HPET_DEV_NUM, HPET0_FUNC_NUM);
329 current += acpi_create_dmar_ds_msi_hpet(current, 0, HPET_BUS_NUM,
330 HPET_DEV_NUM, HPET0_FUNC_NUM);
331 }
332 }
333
334 acpi_dmar_drhd_fixup(tmp, current);
335
336 return current;
337}
338
339static unsigned long acpi_create_atsr(unsigned long current, const IIO_UDS *hob)
340{
341 for (int socket = 0; socket < hob->PlatformData.numofIIO; ++socket) {
342 uint32_t pcie_seg = hob->PlatformData.CpuQpiInfo[socket].PcieSegment;
343 unsigned long tmp = current;
344 bool first = true;
345 IIO_RESOURCE_INSTANCE iio_resource =
346 hob->PlatformData.IIO_resource[socket];
347
348 for (int stack = 0; stack <= PSTACK2; ++stack) {
349 uint32_t bus = iio_resource.StackRes[stack].BusBase;
350 uint32_t vtd_base = iio_resource.StackRes[stack].VtdBarAddress;
351 if (!vtd_base)
352 continue;
353 uint64_t vtd_mmio_cap = read64((void *)(vtd_base + VTD_EXT_CAP_LOW));
354 printk(BIOS_SPEW, "%s socket: %d, stack: %d, bus: 0x%x, vtd_base: 0x%x, "
355 "vtd_mmio_cap: 0x%llx\n",
356 __func__, socket, stack, bus, vtd_base, vtd_mmio_cap);
357
358 // ATSR is applicable only for platform supporting device IOTLBs
359 // through the VT-d extended capability register
360 assert(vtd_mmio_cap != 0xffffffffffffffff);
361 if ((vtd_mmio_cap & 0x4) == 0) // BIT 2
362 continue;
363
364 for (int p = PORT_0; p < MAX_PORTS; ++p) {
365 if (socket == 0 && p == PORT_0)
366 continue;
367 current += acpi_create_dmar_ds_pci_br_for_port(current, p,
368 stack, iio_resource, pcie_seg, true, &first);
369 }
370 }
371 if (tmp != current)
372 acpi_dmar_atsr_fixup(tmp, current);
373 }
374
375 return current;
376}
377
378static unsigned long acpi_create_rmrr(unsigned long current)
379{
380 uint32_t size = ALIGN_UP(MEM_BLK_COUNT * sizeof(MEM_BLK), 0x1000);
381
382 uint32_t *ptr;
383
384 // reserve memory
385 ptr = cbmem_find(CBMEM_ID_STORAGE_DATA);
386 if (!ptr) {
387 ptr = cbmem_add(CBMEM_ID_STORAGE_DATA, size);
388 assert(ptr != NULL);
389 memset(ptr, 0, size);
390 }
391
392 unsigned long tmp = current;
393 printk(BIOS_DEBUG, "[Reserved Memory Region] PCI Segment Number: 0x%x, Base Address: 0x%x, "
394 "End Address (limit): 0x%x\n",
395 0, (uint32_t) ptr, (uint32_t) ((uint32_t) ptr + size - 1));
396 current += acpi_create_dmar_rmrr(current, 0, (uint32_t) ptr,
397 (uint32_t) ((uint32_t) ptr + size - 1));
398
399 printk(BIOS_DEBUG, " [PCI Endpoint Device] Enumeration ID: 0x%x, PCI Bus Number: 0x%x, "
400 "PCI Path: 0x%x, 0x%x\n",
401 0, XHCI_BUS_NUMBER, PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
402 current += acpi_create_dmar_ds_pci(current, XHCI_BUS_NUMBER,
403 PCH_DEV_SLOT_XHCI, XHCI_FUNC_NUM);
404
405 acpi_dmar_rmrr_fixup(tmp, current);
406
407 return current;
408}
409
410static unsigned long acpi_create_rhsa(unsigned long current)
411{
412 size_t hob_size;
413 const uint8_t uds_guid[16] = FSP_HOB_IIO_UNIVERSAL_DATA_GUID;
414 const IIO_UDS *hob = fsp_find_extension_hob_by_guid(uds_guid, &hob_size);
415 assert(hob != NULL && hob_size != 0);
416
417 for (int socket = 0; socket < hob->PlatformData.numofIIO; ++socket) {
418 IIO_RESOURCE_INSTANCE iio_resource =
419 hob->PlatformData.IIO_resource[socket];
420 for (int stack = 0; stack <= PSTACK2; ++stack) {
421 uint32_t vtd_base = iio_resource.StackRes[stack].VtdBarAddress;
422 if (!vtd_base)
423 continue;
424
425 printk(BIOS_DEBUG, "[Remapping Hardware Static Affinity] Base Address: 0x%x, "
426 "Proximity Domain: 0x%x\n", vtd_base, socket);
427 current += acpi_create_dmar_rhsa(current, vtd_base, socket);
428 }
429 }
430
431 return current;
432}
433
434static unsigned long acpi_fill_dmar(unsigned long current)
435{
436 size_t hob_size;
437 const uint8_t uds_guid[16] = FSP_HOB_IIO_UNIVERSAL_DATA_GUID;
438 const IIO_UDS *hob = fsp_find_extension_hob_by_guid(uds_guid, &hob_size);
439 assert(hob != NULL && hob_size != 0);
440
441 // DRHD
442 for (int iio = 1; iio <= hob->PlatformData.numofIIO; ++iio) {
443 int socket = iio;
444 if (socket == hob->PlatformData.numofIIO) // socket 0 should be last DRHD entry
445 socket = 0;
446
447 if (socket == 0) {
448 for (int stack = 1; stack <= PSTACK2; ++stack)
449 current = acpi_create_drhd(current, socket, stack, hob);
450 current = acpi_create_drhd(current, socket, CSTACK, hob);
451 } else {
452 for (int stack = 0; stack <= PSTACK2; ++stack)
453 current = acpi_create_drhd(current, socket, stack, hob);
454 }
455 }
456
457 // RMRR
458 current = acpi_create_rmrr(current);
459
460 // Root Port ATS Capability
461 current = acpi_create_atsr(current, hob);
462
463 // RHSA
464 current = acpi_create_rhsa(current);
465
466 return current;
467}
468
469unsigned long northbridge_write_acpi_tables(const struct device *device,
470 unsigned long current,
471 struct acpi_rsdp *rsdp)
472{
473 acpi_srat_t *srat;
474 acpi_slit_t *slit;
475 acpi_dmar_t *dmar;
476
477 const config_t *const config = config_of(device);
478
479 /* SRAT */
480 current = ALIGN(current, 8);
481 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
482 srat = (acpi_srat_t *) current;
483 acpi_create_srat(srat, acpi_fill_srat);
484 current += srat->header.length;
485 acpi_add_table(rsdp, srat);
486
487 /* SLIT */
488 current = ALIGN(current, 8);
489 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
490 slit = (acpi_slit_t *) current;
491 acpi_create_slit(slit, acpi_fill_slit);
492 current += slit->header.length;
493 acpi_add_table(rsdp, slit);
494
495 /* DMAR */
496 if (config->vtd_support) {
497 current = ALIGN(current, 8);
498 dmar = (acpi_dmar_t *)current;
499 printk(BIOS_DEBUG, "ACPI: * DMAR\n");
500 printk(BIOS_DEBUG, "[DMA Remapping table] Flags: 0x%x\n", DMAR_INTR_REMAP);
501 acpi_create_dmar(dmar, DMAR_INTR_REMAP, acpi_fill_dmar);
502 current += dmar->header.length;
503 current = acpi_align_current(current);
504 acpi_add_table(rsdp, dmar);
505 }
506
507 return current;
508}