Debian Codegraph @

src/soc/intel/snowridge/acpi.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpi.h>
4 #include <acpi/acpi_device.h>
5 #include <acpi/acpigen.h>
6 #include <acpi/acpigen_pci.h>
7 #include <arch/hpet.h>
8 #include <arch/ioapic.h>
9 #include <assert.h>
10 #include <cbmem.h>
11 #include <commonlib/bsd/helpers.h>
12 #include <console/console.h>
13 #include <device/mmio.h>
14 #include <device/pci.h>
15 #include <device/pci_ids.h>
16 #include <device/pci_ops.h>
17 #include <device/pci_type.h>
18 #include <intelblocks/acpi.h>
19 #include <intelblocks/lpc_lib.h>
20 #include <intelblocks/p2sb.h>
21 #include <intelblocks/systemagent_server.h>
22 #include <soc/acpi.h>
23 #include <soc/iomap.h>
24 #include <soc/irq.h>
25 #include <soc/itss.h>
26 #include <soc/pci_devs.h>
27 #include <soc/pm.h>
28 #include <soc/systemagent.h>
29 #include <southbridge/intel/common/acpi_pirq_gen.h>
30 #include <static.h>
31 #include <string.h>
32
33 uint32_t soc_read_sci_irq_select(void)
34 {
35 return read32p(PCH_PWRM_BASE_ADDRESS + ACTL);
36 }
37
38 int soc_madt_sci_irq_polarity(int sci)
39 {
40 if (sci >= 20)
41 return MP_IRQ_POLARITY_LOW;
42
43 return MP_IRQ_POLARITY_HIGH;
44 }
45
46 void soc_fill_fadt(acpi_fadt_t *fadt)
47 {
48 /**
49 * The default field value is 0 if it's not set when calling this function.
50 */
51
52 fadt->pm2_cnt_blk = ACPI_BASE_ADDRESS + PM2_CNT;
53 fadt->pm_tmr_blk = ACPI_BASE_ADDRESS + PM1_TMR;
54 fadt->pm2_cnt_len = 1;
55 fadt->pm_tmr_len = 4;
56
57 fadt->duty_offset = 1;
58 fadt->duty_width = 0;
59
60 fill_fadt_extended_pm_io(fadt);
61 }
62
63 void soc_power_states_generation(int core_id, int cores_per_package)
64 {
65 generate_p_state_entries(core_id, cores_per_package);
66 }
67
68 static void acpigen_write_pci_prt(const struct device *dev)
69 {
70 unsigned int map_count = 0;
71 struct slot_pin_irq_map *pin_irq_map =
72 calloc(MAX_SLOTS * PCI_INT_MAX, sizeof(struct slot_pin_irq_map));
73 if (!pin_irq_map)
74 return;
75
76 struct device *child = NULL;
77 while ((child = dev_bus_each_child(dev->downstream, child))) {
78 if (!is_enabled_pci(child))
79 continue;
80
81 enum pci_pin pin = pci_read_config8(child, PCI_INTERRUPT_PIN);
82 if (pin < PCI_INT_A || pin > PCI_INT_D)
83 continue;
84
85 pin_irq_map[map_count].slot = PCI_SLOT(child->path.pci.devfn);
86 pin_irq_map[map_count].pin = pin;
87
88 /* `pic_pirq` is actually `enum pirq` type. */
89 pin_irq_map[map_count].pic_pirq = itss_soc_get_dev_pirq(child);
90 if (pin_irq_map[map_count].pic_pirq == PIRQ_INVALID)
91 continue;
92
93 /* PIRQA-H is hardwired to IRQ16-23. */
94 pin_irq_map[map_count].apic_gsi =
95 PCH_IRQ16 + pirq_idx(pin_irq_map[map_count].pic_pirq);
96
97 printk(BIOS_SPEW, "%s, slot_pin_irq_map[%2d]: {0x%2x, %s, PIRQ%c, IRQ%d}\n",
98 dev_path(child), map_count, pin_irq_map[map_count].slot,
99 pin_to_str(pin_irq_map[map_count].pin),
100 (unsigned int)pirq_idx(pin_irq_map[map_count].pic_pirq) + 'A',
101 pin_irq_map[map_count].apic_gsi);
102
103 map_count++;
104 }
105
106 size_t pirq_routes;
107 const uint8_t *legacy_pirq_routing = lpc_get_pic_pirq_routing(&pirq_routes);
108 struct pic_pirq_map pirq_map = {.type = PIRQ_GSI};
109 for (size_t i = 0; i < PIRQ_COUNT && i < pirq_routes; i++)
110 pirq_map.gsi[i] = legacy_pirq_routing[i];
111
112 intel_write_pci_PRT(acpi_device_path(dev), pin_irq_map, map_count, &pirq_map);
113 free(pin_irq_map);
114 }
115
116 void domain_fill_ssdt(const struct device *dev)
117 {
118 const char *acpi_scope = acpi_device_scope(dev);
119 const char *acpi_name = acpi_device_name(dev);
120 printk(BIOS_DEBUG, "%s ACPI scope: '%s', name: '%s'\n", __func__, acpi_scope,
121 acpi_name);
122
123 /**
124 * PCH domain is defined in uncore.asl.
125 *
126 * Generating accelerator domains dynamically since they are SKU-dependent.
127 */
128 if (!is_domain0(dev)) {
129 /**
130 * Device (PCIx)
131 * {
132 * Method (_STA) { Return (status) }
133 * Name (_HID, EISAID ("PNP0A08")) // PCI Express Bus
134 * Name (_CID, EISAID ("PNP0A03")) // PCI Bus
135 * Name (_UID, "PCIx")
136 * Name (_PXM, 0)
137 * Method (_OSC, 4) { Return (\_SB.DOSC (Arg0, Arg1, Arg2, Arg3)) }
138 * }
139 */
140 acpigen_write_scope(acpi_scope);
141 acpigen_write_device(acpi_name);
142
143 acpigen_write_STA(dev->enabled ? ACPI_STATUS_DEVICE_ALL_ON :
144 ACPI_STATUS_DEVICE_ALL_OFF);
145
146 acpigen_write_name("_HID");
147 acpigen_emit_eisaid("PNP0A08");
148
149 acpigen_write_name("_CID");
150 acpigen_emit_eisaid("PNP0A03");
151
152 acpigen_write_name("_UID");
153 acpigen_write_string(acpi_name);
154
155 acpigen_write_name("_PXM");
156 acpigen_write_integer(0);
157
158 acpigen_write_method("_OSC", 4);
159 acpigen_write_return_namestr("\\_SB.DOSC");
160 acpigen_emit_byte(ARG0_OP);
161 acpigen_emit_byte(ARG1_OP);
162 acpigen_emit_byte(ARG2_OP);
163 acpigen_emit_byte(ARG3_OP);
164 acpigen_write_method_end();
165
166 acpigen_write_device_end();
167 acpigen_write_scope_end();
168 }
169
170 pci_domain_fill_ssdt(dev);
171
172 acpigen_write_pci_prt(dev);
173 }
174
175 void pcie_rp_fill_ssdt(const struct device *dev)
176 {
177 const char *acpi_scope = acpi_device_scope(dev);
178 const char *acpi_name = acpi_device_name(dev);
179 printk(BIOS_DEBUG, "%s ACPI scope: '%s', name: '%s'\n", __func__, acpi_scope,
180 acpi_name);
181
182 acpigen_write_scope(acpi_scope);
183 acpigen_write_device(acpi_name);
184 acpigen_write_STA(dev->enabled ? ACPI_STATUS_DEVICE_ALL_ON :
185 ACPI_STATUS_DEVICE_ALL_OFF);
186 acpigen_write_ADR_pci_device(dev);
187 acpigen_write_device_end();
188 acpigen_write_scope_end();
189
190 acpigen_write_pci_prt(dev);
191 }
192
193 unsigned long acpi_create_srat_lapics(unsigned long current)
194 {
195 for (struct device *cpu = DEV_PTR(cpu_bus)->downstream->children; cpu != NULL;
196 cpu = cpu->next) {
197 if (!is_enabled_cpu(cpu))
198 continue;
199
200 printk(BIOS_DEBUG,
201 "SRAT: APIC ID = 0x%02x, package ID = 0x%02x, node ID = 0x%02x, core ID = 0x%02x, thread ID = 0x%02x\n",
202 cpu->path.apic.apic_id, cpu->path.apic.package_id,
203 cpu->path.apic.node_id, cpu->path.apic.core_id,
204 cpu->path.apic.thread_id);
205 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current,
206 cpu->path.apic.node_id,
207 cpu->path.apic.apic_id);
208 }
209
210 return current;
211 }
212
213 static unsigned long acpi_fill_srat(unsigned long current)
214 {
215 const uint32_t low_mem_end = sa_server_get_tolud();
216 const uint64_t hi_mem_end = sa_server_get_touud(), mem_4G = 4ull * GiB;
217
218 current = acpi_create_srat_lapics(current);
219
220 current += acpi_create_srat_mem((acpi_srat_mem_t *)current, 0, 0, low_mem_end >> 10,
221 ACPI_SRAT_MEMORY_ENABLED);
222
223 if (hi_mem_end > mem_4G)
224 current += acpi_create_srat_mem((acpi_srat_mem_t *)current, 0, mem_4G >> 10,
225 (hi_mem_end - mem_4G) >> 10,
226 ACPI_SRAT_MEMORY_ENABLED);
227
228 return current;
229 }
230
231 static unsigned long acpi_create_drhd(unsigned long current, struct device *dev)
232 {
233 unsigned long tmp = current;
234 const struct device *dev_domain = dev_get_domain(dev);
235 if (!dev_domain->enabled)
236 return current;
237
238 /**
239 * Bit 0 of VTBAR is read-write and it indicates whether VT-d base address is enabled.
240 */
241 uint32_t vtd_base = pci_read_config32(dev, VTBAR);
242 if (!(vtd_base & VTD_CHIPSET_BASE_ADDRESS_ENABLE))
243 return current;
244
245 vtd_base = ALIGN_DOWN(vtd_base, 4 * KiB);
246
247 const uint32_t pcie_seg = dev->upstream->segment_group;
248 if (is_dev_on_domain0(dev)) {
249 printk(BIOS_DEBUG,
250 "[DMA Remapping Hardware Unit Definition] Flags: 0x%x, PCI Segment: 0x%x, Register Base Address: 0x%x\n",
251 DRHD_INCLUDE_PCI_ALL, pcie_seg, vtd_base);
252 current += acpi_create_dmar_drhd_4k(current, DRHD_INCLUDE_PCI_ALL, pcie_seg,
253 vtd_base);
254
255 union p2sb_bdf ioapic_bdf = p2sb_get_ioapic_bdf();
256 printk(BIOS_DEBUG, "[IOAPIC] PCI Bus: 0x%x, PCI Path: 0x%x, 0x%x\n",
257 ioapic_bdf.bus, ioapic_bdf.dev, ioapic_bdf.fn);
258 current += acpi_create_dmar_ds_ioapic_from_hw(
259 current, IO_APIC_ADDR, ioapic_bdf.bus, ioapic_bdf.dev, ioapic_bdf.fn);
260
261 uint16_t num_hpets = (read32p(HPET_BASE_ADDRESS) >> HPET_NUM_TIM_CAP_SHIFT) &
262 HPET_NUM_TIM_CAP_MASK;
263 if (num_hpets && num_hpets != HPET_NUM_TIM_CAP_MASK) {
264 if (read32p(HPET_BASE_ADDRESS + HPET_TMR0_CNF_CAP) &
265 HPET_TIMER_FSB_EN_CNF_MASK) {
266 union p2sb_bdf hpet_bdf = p2sb_get_hpet_bdf();
267 printk(BIOS_DEBUG,
268 "[MSI_CAPABLE_HPET] Enumeration ID: 0x%x, PCI Bus: 0x%x, PCI Path: 0x%x, 0x%x\n",
269 0, hpet_bdf.bus, hpet_bdf.dev, hpet_bdf.fn);
270 current += acpi_create_dmar_ds_msi_hpet(
271 current, 0, hpet_bdf.bus, hpet_bdf.dev, hpet_bdf.fn);
272 }
273 }
274 } else {
275 printk(BIOS_DEBUG,
276 "[DMA Remapping Hardware Unit Definition] Flags: 0x%x, PCI Segment: 0x%x, Register Base Address: 0x%x\n",
277 0, pcie_seg, vtd_base);
278 current += acpi_create_dmar_drhd_4k(current, 0, pcie_seg, vtd_base);
279
280 if (dev == DEV_PTR(dlb_sa)) {
281 if (DEV_PTR(dlb)) {
282 printk(BIOS_DEBUG, "[PCI Endpoint Device] %s\n",
283 dev_path(DEV_PTR(dlb)));
284 current += acpi_create_dmar_ds_pci(
285 current, PCI_DEV2BUS(PCI_BDF(DEV_PTR(dlb))),
286 PCI_SLOT(DEV_PTR(dlb)->path.pci.devfn),
287 PCI_FUNC(DEV_PTR(dlb)->path.pci.devfn));
288 }
289 } else {
290 struct device *pci_bridge = NULL;
291 while ((pci_bridge = dev_bus_each_child(dev_get_domain(dev)->downstream,
292 pci_bridge))) {
293 if (pci_bridge->vendor != PCI_VID_INTEL)
294 continue;
295
296 switch (pci_bridge->device) {
297 case PCI_DID_INTEL_SNR_CPU_PCIE_RPA:
298 case PCI_DID_INTEL_SNR_CPU_PCIE_RPB:
299 case PCI_DID_INTEL_SNR_CPU_PCIE_RPC:
300 case PCI_DID_INTEL_SNR_CPU_PCIE_RPD:
301 case PCI_DID_INTEL_SNR_VRP4_NIS:
302 case PCI_DID_INTEL_SNR_VRP5_QAT_1_8:
303 printk(BIOS_DEBUG, "[PCI Sub-hierarchy] %s\n",
304 dev_path(pci_bridge));
305 current += acpi_create_dmar_ds_pci_br(
306 current, dev->upstream->secondary,
307 PCI_SLOT(pci_bridge->path.pci.devfn),
308 PCI_FUNC(pci_bridge->path.pci.devfn));
309 break;
310 default:
311 continue;
312 }
313 }
314 }
315 }
316
317 if (current != tmp)
318 acpi_dmar_drhd_fixup(tmp, current);
319
320 return current;
321 }
322
323 static unsigned long acpi_create_rmrr(unsigned long current)
324 {
325 const uint32_t MEM_BLK_COUNT = 0x140, MEM_BLK_SIZE = 32;
326 uint32_t size = ALIGN_UP(MEM_BLK_COUNT * MEM_BLK_SIZE, 0x1000);
327 const struct cbmem_entry *entry;
328 void *ptr;
329 unsigned long tmp = current;
330 struct device *dev = PCH_DEV_XHCI;
331
332 entry = cbmem_entry_find(CBMEM_ID_STORAGE_DATA);
333 if (!entry) {
334 ptr = cbmem_add(CBMEM_ID_STORAGE_DATA, size);
335 if (!ptr) {
336 printk(BIOS_ERR, "Failed to allocate reserved memory in cbmem!\n");
337 return current;
338 }
339
340 memset(ptr, 0, size);
341 } else {
342 ptr = cbmem_entry_start(entry);
343 size = cbmem_entry_size(entry);
344 }
345
346 printk(BIOS_DEBUG,
347 "[Reserved Memory Region Reporting] PCI Segment: 0x%x, Base: %p, Limit: %p\n",
348 dev->upstream->segment_group, ptr, ptr + size - 1);
349 current += acpi_create_dmar_rmrr(current, dev->upstream->segment_group, (uintptr_t)ptr,
350 (uintptr_t)(ptr + size - 1));
351
352 printk(BIOS_DEBUG, "[PCI Endpoint Device] %s\n", dev_path(dev));
353 current += acpi_create_dmar_ds_pci(current, PCI_DEV2BUS(PCI_BDF(dev)),
354 PCI_SLOT(dev->path.pci.devfn),
355 PCI_FUNC(dev->path.pci.devfn));
356
357 if (current != tmp)
358 acpi_dmar_rmrr_fixup(tmp, current);
359
360 return current;
361 }
362
363 static unsigned long acpi_create_atsr(unsigned long current, struct device *dev)
364 {
365 unsigned long tmp = current;
366
367 /**
368 * Bit 0 of VTBAR is read-write and it indicates whether VT-d base address is enabled.
369 */
370 uint32_t vtd_base = pci_read_config32(dev, VTBAR);
371 if (!(vtd_base & VTD_CHIPSET_BASE_ADDRESS_ENABLE))
372 return current;
373
374 vtd_base = ALIGN_DOWN(vtd_base, 4 * KiB);
375
376 uint64_t vtd_ext_cap = read64p(vtd_base + VTD_ECAP);
377 if (!(vtd_ext_cap & DEVICE_TLB))
378 return current;
379
380 printk(BIOS_DEBUG, "Domain 1 VT-d BAR: 0x%x, Extended Capability: 0x%llx\n", vtd_base,
381 vtd_ext_cap);
382
383 bool first = true;
384 struct device *cpu_pcie_rp = NULL;
385 while ((cpu_pcie_rp =
386 dev_bus_each_child(dev_get_domain(dev)->downstream, cpu_pcie_rp))) {
387 if (cpu_pcie_rp->vendor != PCI_VID_INTEL)
388 continue;
389
390 switch (cpu_pcie_rp->device) {
391 case PCI_DID_INTEL_SNR_CPU_PCIE_RPA:
392 case PCI_DID_INTEL_SNR_CPU_PCIE_RPB:
393 case PCI_DID_INTEL_SNR_CPU_PCIE_RPC:
394 case PCI_DID_INTEL_SNR_CPU_PCIE_RPD:
395 break;
396 default:
397 continue;
398 }
399
400 if (first) {
401 const uint32_t pcie_seg = dev->upstream->segment_group;
402 printk(BIOS_DEBUG,
403 "[Root Port ATS Capability] Flags: 0x%x, PCI Segment: 0x%x\n", 0,
404 pcie_seg);
405 current += acpi_create_dmar_atsr(current, 0, pcie_seg);
406 first = false;
407 }
408
409 printk(BIOS_DEBUG, "[PCI Sub-hierarchy] %s\n", dev_path(cpu_pcie_rp));
410 current += acpi_create_dmar_ds_pci_br(current,
411 PCI_DEV2BUS(PCI_BDF(cpu_pcie_rp)),
412 PCI_SLOT(cpu_pcie_rp->path.pci.devfn),
413 PCI_FUNC(cpu_pcie_rp->path.pci.devfn));
414 }
415
416 if (tmp != current)
417 acpi_dmar_atsr_fixup(tmp, current);
418
419 return current;
420 }
421
422 static unsigned long acpi_fill_dmar(unsigned long current)
423 {
424 assert(DEV_PTR(pch_sa));
425
426 /**
427 * Domain 0 hosts all PCH peripherals, and DRHD entry for this domain should be at last,
428 * thus we search from `DEV_PTR(pch_sa)` here.
429 */
430 struct device *dev = DEV_PTR(pch_sa);
431 while ((dev = dev_find_device(PCI_VID_INTEL, PCI_DID_INTEL_SNR_ID, dev)) != NULL)
432 current = acpi_create_drhd(current, dev);
433
434 current = acpi_create_drhd(current, DEV_PTR(pch_sa));
435
436 current = acpi_create_rmrr(current);
437
438 /* Only CPU PCIe root ports support address translation services (ATS). */
439 assert(DEV_PTR(cpu_sa));
440 current = acpi_create_atsr(current, DEV_PTR(cpu_sa));
441
442 return current;
443 }
444
445 unsigned long sa_write_acpi_tables(const struct device *dev, unsigned long current,
446 struct acpi_rsdp *rsdp)
447 {
448 acpi_srat_t *srat;
449 acpi_dmar_t *dmar;
450
451 /**
452 * Write only when calling from system agent in domain 0.
453 */
454 if (!sa_server_is_on_pch_domain(dev))
455 return current;
456
457 /* SRAT */
458 printk(BIOS_DEBUG, "ACPI: * SRAT at 0x%08lx\n", current);
459 srat = (acpi_srat_t *)current;
460 acpi_create_srat(srat, acpi_fill_srat);
461 acpi_add_table(rsdp, srat);
462 current += srat->header.length;
463 current = acpi_align_current(current);
464
465 printk(BIOS_DEBUG, "ACPI: * DMAR at 0x%08lx\n", current);
466 dmar = (acpi_dmar_t *)current;
467 acpi_create_dmar(dmar, DMAR_INTR_REMAP, acpi_fill_dmar);
468 acpi_add_table(rsdp, dmar);
469 current += dmar->header.length;
470
471 return current;
472 }
473
474 /**
475 * To use ACPI in common block, this function should be defined.
476 */
477 const acpi_cstate_t *soc_get_cstate_map(size_t *entries)
478 {
479 *entries = 0;
480 return NULL;
481 }