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