// SPDX-License-Identifier: GPL-2.0-only1/*2* RISC-V Specific Low-Level ACPI Boot Support3*4* Copyright (C) 2013-2014, Linaro Ltd.5* Author: Al Stone <[email protected]>6* Author: Graeme Gregory <[email protected]>7* Author: Hanjun Guo <[email protected]>8* Author: Tomasz Nowicki <[email protected]>9* Author: Naresh Bhat <[email protected]>10*11* Copyright (C) 2021-2023, Ventana Micro Systems Inc.12* Author: Sunil V L <[email protected]>13*/1415#include <linux/acpi.h>16#include <linux/efi-bgrt.h>17#include <linux/efi.h>18#include <linux/io.h>19#include <linux/memblock.h>20#include <linux/of_fdt.h>21#include <linux/pci.h>22#include <linux/serial_core.h>2324int acpi_noirq = 1; /* skip ACPI IRQ initialization */25int acpi_disabled = 1;26EXPORT_SYMBOL(acpi_disabled);2728int acpi_pci_disabled = 1; /* skip ACPI PCI scan and IRQ initialization */29EXPORT_SYMBOL(acpi_pci_disabled);3031static bool param_acpi_off __initdata;32static bool param_acpi_on __initdata;33static bool param_acpi_force __initdata;3435static struct acpi_madt_rintc cpu_madt_rintc[NR_CPUS];3637static int __init parse_acpi(char *arg)38{39if (!arg)40return -EINVAL;4142/* "acpi=off" disables both ACPI table parsing and interpreter */43if (strcmp(arg, "off") == 0)44param_acpi_off = true;45else if (strcmp(arg, "on") == 0) /* prefer ACPI over DT */46param_acpi_on = true;47else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */48param_acpi_force = true;49else50return -EINVAL; /* Core will print when we return error */5152return 0;53}54early_param("acpi", parse_acpi);5556/*57* acpi_fadt_sanity_check() - Check FADT presence and carry out sanity58* checks on it59*60* Return 0 on success, <0 on failure61*/62static int __init acpi_fadt_sanity_check(void)63{64struct acpi_table_header *table;65struct acpi_table_fadt *fadt;66acpi_status status;67int ret = 0;6869/*70* FADT is required on riscv; retrieve it to check its presence71* and carry out revision and ACPI HW reduced compliancy tests72*/73status = acpi_get_table(ACPI_SIG_FADT, 0, &table);74if (ACPI_FAILURE(status)) {75const char *msg = acpi_format_exception(status);7677pr_err("Failed to get FADT table, %s\n", msg);78return -ENODEV;79}8081fadt = (struct acpi_table_fadt *)table;8283/*84* The revision in the table header is the FADT's Major revision. The85* FADT also has a minor revision, which is stored in the FADT itself.86*87* TODO: Currently, we check for 6.5 as the minimum version to check88* for HW_REDUCED flag. However, once RISC-V updates are released in89* the ACPI spec, we need to update this check for exact minor revision90*/91if (table->revision < 6 || (table->revision == 6 && fadt->minor_revision < 5))92pr_err(FW_BUG "Unsupported FADT revision %d.%d, should be 6.5+\n",93table->revision, fadt->minor_revision);9495if (!(fadt->flags & ACPI_FADT_HW_REDUCED)) {96pr_err("FADT not ACPI hardware reduced compliant\n");97ret = -EINVAL;98}99100/*101* acpi_get_table() creates FADT table mapping that102* should be released after parsing and before resuming boot103*/104acpi_put_table(table);105return ret;106}107108/*109* acpi_boot_table_init() called from setup_arch(), always.110* 1. find RSDP and get its address, and then find XSDT111* 2. extract all tables and checksums them all112* 3. check ACPI FADT HW reduced flag113*114* We can parse ACPI boot-time tables such as MADT after115* this function is called.116*117* On return ACPI is enabled if either:118*119* - ACPI tables are initialized and sanity checks passed120* - acpi=force was passed in the command line and ACPI was not disabled121* explicitly through acpi=off command line parameter122*123* ACPI is disabled on function return otherwise124*/125void __init acpi_boot_table_init(void)126{127/*128* Enable ACPI instead of device tree unless129* - ACPI has been disabled explicitly (acpi=off), or130* - firmware has not populated ACPI ptr in EFI system table131* and ACPI has not been [force] enabled (acpi=on|force)132*/133if (param_acpi_off ||134(!param_acpi_on && !param_acpi_force &&135efi.acpi20 == EFI_INVALID_TABLE_ADDR))136goto done;137138/*139* ACPI is disabled at this point. Enable it in order to parse140* the ACPI tables and carry out sanity checks141*/142enable_acpi();143144/*145* If ACPI tables are initialized and FADT sanity checks passed,146* leave ACPI enabled and carry on booting; otherwise disable ACPI147* on initialization error.148* If acpi=force was passed on the command line it forces ACPI149* to be enabled even if its initialization failed.150*/151if (acpi_table_init() || acpi_fadt_sanity_check()) {152pr_err("Failed to init ACPI tables\n");153if (!param_acpi_force)154disable_acpi();155}156157done:158if (acpi_disabled) {159if (earlycon_acpi_spcr_enable)160early_init_dt_scan_chosen_stdout();161} else {162acpi_parse_spcr(earlycon_acpi_spcr_enable, true);163if (IS_ENABLED(CONFIG_ACPI_BGRT))164acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);165}166}167168static int acpi_parse_madt_rintc(union acpi_subtable_headers *header, const unsigned long end)169{170struct acpi_madt_rintc *rintc = (struct acpi_madt_rintc *)header;171int cpuid;172173if (!(rintc->flags & ACPI_MADT_ENABLED))174return 0;175176cpuid = riscv_hartid_to_cpuid(rintc->hart_id);177/*178* When CONFIG_SMP is disabled, mapping won't be created for179* all cpus.180* CPUs more than num_possible_cpus, will be ignored.181*/182if (cpuid >= 0 && cpuid < num_possible_cpus())183cpu_madt_rintc[cpuid] = *rintc;184185return 0;186}187188/*189* Instead of parsing (and freeing) the ACPI table, cache190* the RINTC structures since they are frequently used191* like in cpuinfo.192*/193void __init acpi_init_rintc_map(void)194{195if (acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_madt_rintc, 0) <= 0) {196pr_err("No valid RINTC entries exist\n");197BUG();198}199}200201struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu)202{203return &cpu_madt_rintc[cpu];204}205206/*207* __acpi_map_table() will be called before paging_init(), so early_ioremap()208* or early_memremap() should be called here to for ACPI table mapping.209*/210void __init __iomem *__acpi_map_table(unsigned long phys, unsigned long size)211{212if (!size)213return NULL;214215return early_memremap(phys, size);216}217218void __init __acpi_unmap_table(void __iomem *map, unsigned long size)219{220if (!map || !size)221return;222223early_memunmap(map, size);224}225226void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)227{228efi_memory_desc_t *md, *region = NULL;229pgprot_t prot;230231if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP)))232return NULL;233234for_each_efi_memory_desc(md) {235u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);236237if (phys < md->phys_addr || phys >= end)238continue;239240if (phys + size > end) {241pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n");242return NULL;243}244region = md;245break;246}247248/*249* It is fine for AML to remap regions that are not represented in the250* EFI memory map at all, as it only describes normal memory, and MMIO251* regions that require a virtual mapping to make them accessible to252* the EFI runtime services.253*/254prot = PAGE_KERNEL_IO;255if (region) {256switch (region->type) {257case EFI_LOADER_CODE:258case EFI_LOADER_DATA:259case EFI_BOOT_SERVICES_CODE:260case EFI_BOOT_SERVICES_DATA:261case EFI_CONVENTIONAL_MEMORY:262case EFI_PERSISTENT_MEMORY:263if (memblock_is_map_memory(phys) ||264!memblock_is_region_memory(phys, size)) {265pr_warn(FW_BUG "requested region covers kernel memory\n");266return NULL;267}268269/*270* Mapping kernel memory is permitted if the region in271* question is covered by a single memblock with the272* NOMAP attribute set: this enables the use of ACPI273* table overrides passed via initramfs.274* This particular use case only requires read access.275*/276fallthrough;277278case EFI_RUNTIME_SERVICES_CODE:279/*280* This would be unusual, but not problematic per se,281* as long as we take care not to create a writable282* mapping for executable code.283*/284prot = PAGE_KERNEL_RO;285break;286287case EFI_ACPI_RECLAIM_MEMORY:288/*289* ACPI reclaim memory is used to pass firmware tables290* and other data that is intended for consumption by291* the OS only, which may decide it wants to reclaim292* that memory and use it for something else. We never293* do that, but we usually add it to the linear map294* anyway, in which case we should use the existing295* mapping.296*/297if (memblock_is_map_memory(phys))298return (void __iomem *)__va(phys);299fallthrough;300301default:302if (region->attribute & EFI_MEMORY_WB)303prot = PAGE_KERNEL;304else if ((region->attribute & EFI_MEMORY_WC) ||305(region->attribute & EFI_MEMORY_WT))306prot = pgprot_writecombine(PAGE_KERNEL);307}308}309310return ioremap_prot(phys, size, prot);311}312313#ifdef CONFIG_PCI314315/*316* raw_pci_read/write - Platform-specific PCI config space access.317*/318int raw_pci_read(unsigned int domain, unsigned int bus,319unsigned int devfn, int reg, int len, u32 *val)320{321struct pci_bus *b = pci_find_bus(domain, bus);322323if (!b)324return PCIBIOS_DEVICE_NOT_FOUND;325return b->ops->read(b, devfn, reg, len, val);326}327328int raw_pci_write(unsigned int domain, unsigned int bus,329unsigned int devfn, int reg, int len, u32 val)330{331struct pci_bus *b = pci_find_bus(domain, bus);332333if (!b)334return PCIBIOS_DEVICE_NOT_FOUND;335return b->ops->write(b, devfn, reg, len, val);336}337338#endif /* CONFIG_PCI */339340341