1 // SPDX-License-Identifier: GPL-2.0-only
3 * RISC-V Specific Low-Level ACPI Boot Support
5 * Copyright (C) 2013-2014, Linaro Ltd.
6 * Author: Al Stone <al.stone@linaro.org>
7 * Author: Graeme Gregory <graeme.gregory@linaro.org>
8 * Author: Hanjun Guo <hanjun.guo@linaro.org>
9 * Author: Tomasz Nowicki <tomasz.nowicki@linaro.org>
10 * Author: Naresh Bhat <naresh.bhat@linaro.org>
12 * Copyright (C) 2021-2023, Ventana Micro Systems Inc.
13 * Author: Sunil V L <sunilvl@ventanamicro.com>
16 #include <linux/acpi.h>
17 #include <linux/efi.h>
19 #include <linux/memblock.h>
20 #include <linux/pci.h>
22 int acpi_noirq = 1; /* skip ACPI IRQ initialization */
23 int acpi_disabled = 1;
24 EXPORT_SYMBOL(acpi_disabled);
26 int acpi_pci_disabled = 1; /* skip ACPI PCI scan and IRQ initialization */
27 EXPORT_SYMBOL(acpi_pci_disabled);
29 static bool param_acpi_off __initdata;
30 static bool param_acpi_on __initdata;
31 static bool param_acpi_force __initdata;
33 static struct acpi_madt_rintc cpu_madt_rintc[NR_CPUS];
35 static int __init parse_acpi(char *arg)
40 /* "acpi=off" disables both ACPI table parsing and interpreter */
41 if (strcmp(arg, "off") == 0)
42 param_acpi_off = true;
43 else if (strcmp(arg, "on") == 0) /* prefer ACPI over DT */
45 else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */
46 param_acpi_force = true;
48 return -EINVAL; /* Core will print when we return error */
52 early_param("acpi", parse_acpi);
55 * acpi_fadt_sanity_check() - Check FADT presence and carry out sanity
58 * Return 0 on success, <0 on failure
60 static int __init acpi_fadt_sanity_check(void)
62 struct acpi_table_header *table;
63 struct acpi_table_fadt *fadt;
68 * FADT is required on riscv; retrieve it to check its presence
69 * and carry out revision and ACPI HW reduced compliancy tests
71 status = acpi_get_table(ACPI_SIG_FADT, 0, &table);
72 if (ACPI_FAILURE(status)) {
73 const char *msg = acpi_format_exception(status);
75 pr_err("Failed to get FADT table, %s\n", msg);
79 fadt = (struct acpi_table_fadt *)table;
82 * The revision in the table header is the FADT's Major revision. The
83 * FADT also has a minor revision, which is stored in the FADT itself.
85 * TODO: Currently, we check for 6.5 as the minimum version to check
86 * for HW_REDUCED flag. However, once RISC-V updates are released in
87 * the ACPI spec, we need to update this check for exact minor revision
89 if (table->revision < 6 || (table->revision == 6 && fadt->minor_revision < 5))
90 pr_err(FW_BUG "Unsupported FADT revision %d.%d, should be 6.5+\n",
91 table->revision, fadt->minor_revision);
93 if (!(fadt->flags & ACPI_FADT_HW_REDUCED)) {
94 pr_err("FADT not ACPI hardware reduced compliant\n");
99 * acpi_get_table() creates FADT table mapping that
100 * should be released after parsing and before resuming boot
102 acpi_put_table(table);
107 * acpi_boot_table_init() called from setup_arch(), always.
108 * 1. find RSDP and get its address, and then find XSDT
109 * 2. extract all tables and checksums them all
110 * 3. check ACPI FADT HW reduced flag
112 * We can parse ACPI boot-time tables such as MADT after
113 * this function is called.
115 * On return ACPI is enabled if either:
117 * - ACPI tables are initialized and sanity checks passed
118 * - acpi=force was passed in the command line and ACPI was not disabled
119 * explicitly through acpi=off command line parameter
121 * ACPI is disabled on function return otherwise
123 void __init acpi_boot_table_init(void)
126 * Enable ACPI instead of device tree unless
127 * - ACPI has been disabled explicitly (acpi=off), or
128 * - firmware has not populated ACPI ptr in EFI system table
129 * and ACPI has not been [force] enabled (acpi=on|force)
131 if (param_acpi_off ||
132 (!param_acpi_on && !param_acpi_force &&
133 efi.acpi20 == EFI_INVALID_TABLE_ADDR))
137 * ACPI is disabled at this point. Enable it in order to parse
138 * the ACPI tables and carry out sanity checks
143 * If ACPI tables are initialized and FADT sanity checks passed,
144 * leave ACPI enabled and carry on booting; otherwise disable ACPI
145 * on initialization error.
146 * If acpi=force was passed on the command line it forces ACPI
147 * to be enabled even if its initialization failed.
149 if (acpi_table_init() || acpi_fadt_sanity_check()) {
150 pr_err("Failed to init ACPI tables\n");
151 if (!param_acpi_force)
156 static int acpi_parse_madt_rintc(union acpi_subtable_headers *header, const unsigned long end)
158 struct acpi_madt_rintc *rintc = (struct acpi_madt_rintc *)header;
161 if (!(rintc->flags & ACPI_MADT_ENABLED))
164 cpuid = riscv_hartid_to_cpuid(rintc->hart_id);
166 * When CONFIG_SMP is disabled, mapping won't be created for
168 * CPUs more than num_possible_cpus, will be ignored.
170 if (cpuid >= 0 && cpuid < num_possible_cpus())
171 cpu_madt_rintc[cpuid] = *rintc;
177 * Instead of parsing (and freeing) the ACPI table, cache
178 * the RINTC structures since they are frequently used
181 void __init acpi_init_rintc_map(void)
183 if (acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_madt_rintc, 0) <= 0) {
184 pr_err("No valid RINTC entries exist\n");
189 struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu)
191 return &cpu_madt_rintc[cpu];
194 u32 get_acpi_id_for_cpu(int cpu)
196 return acpi_cpu_get_madt_rintc(cpu)->uid;
200 * __acpi_map_table() will be called before paging_init(), so early_ioremap()
201 * or early_memremap() should be called here to for ACPI table mapping.
203 void __init __iomem *__acpi_map_table(unsigned long phys, unsigned long size)
208 return early_ioremap(phys, size);
211 void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
216 early_iounmap(map, size);
219 void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
221 efi_memory_desc_t *md, *region = NULL;
224 if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP)))
227 for_each_efi_memory_desc(md) {
228 u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
230 if (phys < md->phys_addr || phys >= end)
233 if (phys + size > end) {
234 pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n");
242 * It is fine for AML to remap regions that are not represented in the
243 * EFI memory map at all, as it only describes normal memory, and MMIO
244 * regions that require a virtual mapping to make them accessible to
245 * the EFI runtime services.
247 prot = PAGE_KERNEL_IO;
249 switch (region->type) {
250 case EFI_LOADER_CODE:
251 case EFI_LOADER_DATA:
252 case EFI_BOOT_SERVICES_CODE:
253 case EFI_BOOT_SERVICES_DATA:
254 case EFI_CONVENTIONAL_MEMORY:
255 case EFI_PERSISTENT_MEMORY:
256 if (memblock_is_map_memory(phys) ||
257 !memblock_is_region_memory(phys, size)) {
258 pr_warn(FW_BUG "requested region covers kernel memory\n");
263 * Mapping kernel memory is permitted if the region in
264 * question is covered by a single memblock with the
265 * NOMAP attribute set: this enables the use of ACPI
266 * table overrides passed via initramfs.
267 * This particular use case only requires read access.
271 case EFI_RUNTIME_SERVICES_CODE:
273 * This would be unusual, but not problematic per se,
274 * as long as we take care not to create a writable
275 * mapping for executable code.
277 prot = PAGE_KERNEL_RO;
280 case EFI_ACPI_RECLAIM_MEMORY:
282 * ACPI reclaim memory is used to pass firmware tables
283 * and other data that is intended for consumption by
284 * the OS only, which may decide it wants to reclaim
285 * that memory and use it for something else. We never
286 * do that, but we usually add it to the linear map
287 * anyway, in which case we should use the existing
290 if (memblock_is_map_memory(phys))
291 return (void __iomem *)__va(phys);
295 if (region->attribute & EFI_MEMORY_WB)
297 else if ((region->attribute & EFI_MEMORY_WC) ||
298 (region->attribute & EFI_MEMORY_WT))
299 prot = pgprot_writecombine(PAGE_KERNEL);
303 return ioremap_prot(phys, size, pgprot_val(prot));
309 * These interfaces are defined just to enable building ACPI core.
310 * TODO: Update it with actual implementation when external interrupt
311 * controller support is added in RISC-V ACPI.
313 int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
314 int reg, int len, u32 *val)
316 return PCIBIOS_DEVICE_NOT_FOUND;
319 int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
320 int reg, int len, u32 val)
322 return PCIBIOS_DEVICE_NOT_FOUND;
325 int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
330 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
334 #endif /* CONFIG_PCI */