1628c3bb4SHuacai Chen // SPDX-License-Identifier: GPL-2.0 2628c3bb4SHuacai Chen /* 3628c3bb4SHuacai Chen * acpi.c - Architecture-Specific Low-Level ACPI Boot Support 4628c3bb4SHuacai Chen * 5628c3bb4SHuacai Chen * Author: Jianmin Lv <lvjianmin@loongson.cn> 6628c3bb4SHuacai Chen * Huacai Chen <chenhuacai@loongson.cn> 7628c3bb4SHuacai Chen * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 8628c3bb4SHuacai Chen */ 9628c3bb4SHuacai Chen 10628c3bb4SHuacai Chen #include <linux/init.h> 11628c3bb4SHuacai Chen #include <linux/acpi.h> 12628c3bb4SHuacai Chen #include <linux/irq.h> 13628c3bb4SHuacai Chen #include <linux/irqdomain.h> 14628c3bb4SHuacai Chen #include <linux/memblock.h> 15628c3bb4SHuacai Chen #include <linux/serial_core.h> 16628c3bb4SHuacai Chen #include <asm/io.h> 17d4b6f156SHuacai Chen #include <asm/numa.h> 18628c3bb4SHuacai Chen #include <asm/loongson.h> 19628c3bb4SHuacai Chen 20628c3bb4SHuacai Chen int acpi_disabled; 21628c3bb4SHuacai Chen EXPORT_SYMBOL(acpi_disabled); 22628c3bb4SHuacai Chen int acpi_noirq; 23628c3bb4SHuacai Chen int acpi_pci_disabled; 24628c3bb4SHuacai Chen EXPORT_SYMBOL(acpi_pci_disabled); 25628c3bb4SHuacai Chen int acpi_strict = 1; /* We have no workarounds on LoongArch */ 26628c3bb4SHuacai Chen int num_processors; 27628c3bb4SHuacai Chen int disabled_cpus; 28628c3bb4SHuacai Chen 29628c3bb4SHuacai Chen u64 acpi_saved_sp; 30628c3bb4SHuacai Chen 31628c3bb4SHuacai Chen #define MAX_CORE_PIC 256 32628c3bb4SHuacai Chen 33628c3bb4SHuacai Chen #define PREFIX "ACPI: " 34628c3bb4SHuacai Chen 35628c3bb4SHuacai Chen void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size) 36628c3bb4SHuacai Chen { 37628c3bb4SHuacai Chen 38628c3bb4SHuacai Chen if (!phys || !size) 39628c3bb4SHuacai Chen return NULL; 40628c3bb4SHuacai Chen 41628c3bb4SHuacai Chen return early_memremap(phys, size); 42628c3bb4SHuacai Chen } 43628c3bb4SHuacai Chen void __init __acpi_unmap_table(void __iomem *map, unsigned long size) 44628c3bb4SHuacai Chen { 45628c3bb4SHuacai Chen if (!map || !size) 46628c3bb4SHuacai Chen return; 47628c3bb4SHuacai Chen 48628c3bb4SHuacai Chen early_memunmap(map, size); 49628c3bb4SHuacai Chen } 50628c3bb4SHuacai Chen 51628c3bb4SHuacai Chen void __init __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size) 52628c3bb4SHuacai Chen { 53628c3bb4SHuacai Chen if (!memblock_is_memory(phys)) 54628c3bb4SHuacai Chen return ioremap(phys, size); 55628c3bb4SHuacai Chen else 56628c3bb4SHuacai Chen return ioremap_cache(phys, size); 57628c3bb4SHuacai Chen } 58628c3bb4SHuacai Chen 59628c3bb4SHuacai Chen void __init acpi_boot_table_init(void) 60628c3bb4SHuacai Chen { 61628c3bb4SHuacai Chen /* 62628c3bb4SHuacai Chen * If acpi_disabled, bail out 63628c3bb4SHuacai Chen */ 64628c3bb4SHuacai Chen if (acpi_disabled) 65628c3bb4SHuacai Chen return; 66628c3bb4SHuacai Chen 67628c3bb4SHuacai Chen /* 68628c3bb4SHuacai Chen * Initialize the ACPI boot-time table parser. 69628c3bb4SHuacai Chen */ 70628c3bb4SHuacai Chen if (acpi_table_init()) { 71628c3bb4SHuacai Chen disable_acpi(); 72628c3bb4SHuacai Chen return; 73628c3bb4SHuacai Chen } 74628c3bb4SHuacai Chen } 75628c3bb4SHuacai Chen 76255b4658SHuacai Chen #ifdef CONFIG_SMP 7746859ac8SHuacai Chen static int set_processor_mask(u32 id, u32 flags) 7846859ac8SHuacai Chen { 7946859ac8SHuacai Chen 8046859ac8SHuacai Chen int cpu, cpuid = id; 8146859ac8SHuacai Chen 8246859ac8SHuacai Chen if (num_processors >= nr_cpu_ids) { 8346859ac8SHuacai Chen pr_warn(PREFIX "nr_cpus/possible_cpus limit of %i reached." 8446859ac8SHuacai Chen " processor 0x%x ignored.\n", nr_cpu_ids, cpuid); 8546859ac8SHuacai Chen 8646859ac8SHuacai Chen return -ENODEV; 8746859ac8SHuacai Chen 8846859ac8SHuacai Chen } 8946859ac8SHuacai Chen if (cpuid == loongson_sysconf.boot_cpu_id) 9046859ac8SHuacai Chen cpu = 0; 9146859ac8SHuacai Chen else 9246859ac8SHuacai Chen cpu = cpumask_next_zero(-1, cpu_present_mask); 9346859ac8SHuacai Chen 9446859ac8SHuacai Chen if (flags & ACPI_MADT_ENABLED) { 9546859ac8SHuacai Chen num_processors++; 9646859ac8SHuacai Chen set_cpu_possible(cpu, true); 9746859ac8SHuacai Chen set_cpu_present(cpu, true); 9846859ac8SHuacai Chen __cpu_number_map[cpuid] = cpu; 9946859ac8SHuacai Chen __cpu_logical_map[cpu] = cpuid; 10046859ac8SHuacai Chen } else 10146859ac8SHuacai Chen disabled_cpus++; 10246859ac8SHuacai Chen 10346859ac8SHuacai Chen return cpu; 10446859ac8SHuacai Chen } 105255b4658SHuacai Chen #endif 10646859ac8SHuacai Chen 107*e9e7ff16SHuacai Chen static int __init 108*e9e7ff16SHuacai Chen acpi_parse_processor(union acpi_subtable_headers *header, const unsigned long end) 109*e9e7ff16SHuacai Chen { 110*e9e7ff16SHuacai Chen struct acpi_madt_core_pic *processor = NULL; 111*e9e7ff16SHuacai Chen 112*e9e7ff16SHuacai Chen processor = (struct acpi_madt_core_pic *)header; 113*e9e7ff16SHuacai Chen if (BAD_MADT_ENTRY(processor, end)) 114*e9e7ff16SHuacai Chen return -EINVAL; 115*e9e7ff16SHuacai Chen 116*e9e7ff16SHuacai Chen acpi_table_print_madt_entry(&header->common); 117*e9e7ff16SHuacai Chen #ifdef CONFIG_SMP 118*e9e7ff16SHuacai Chen set_processor_mask(processor->core_id, processor->flags); 119*e9e7ff16SHuacai Chen #endif 120*e9e7ff16SHuacai Chen 121*e9e7ff16SHuacai Chen return 0; 122*e9e7ff16SHuacai Chen } 123*e9e7ff16SHuacai Chen 124*e9e7ff16SHuacai Chen static int __init 125*e9e7ff16SHuacai Chen acpi_parse_eio_master(union acpi_subtable_headers *header, const unsigned long end) 126*e9e7ff16SHuacai Chen { 127*e9e7ff16SHuacai Chen static int core = 0; 128*e9e7ff16SHuacai Chen struct acpi_madt_eio_pic *eiointc = NULL; 129*e9e7ff16SHuacai Chen 130*e9e7ff16SHuacai Chen eiointc = (struct acpi_madt_eio_pic *)header; 131*e9e7ff16SHuacai Chen if (BAD_MADT_ENTRY(eiointc, end)) 132*e9e7ff16SHuacai Chen return -EINVAL; 133*e9e7ff16SHuacai Chen 134*e9e7ff16SHuacai Chen core = eiointc->node * CORES_PER_EIO_NODE; 135*e9e7ff16SHuacai Chen set_bit(core, &(loongson_sysconf.cores_io_master)); 136*e9e7ff16SHuacai Chen 137*e9e7ff16SHuacai Chen return 0; 138*e9e7ff16SHuacai Chen } 139*e9e7ff16SHuacai Chen 140628c3bb4SHuacai Chen static void __init acpi_process_madt(void) 141628c3bb4SHuacai Chen { 142255b4658SHuacai Chen #ifdef CONFIG_SMP 14346859ac8SHuacai Chen int i; 14446859ac8SHuacai Chen 14546859ac8SHuacai Chen for (i = 0; i < NR_CPUS; i++) { 14646859ac8SHuacai Chen __cpu_number_map[i] = -1; 14746859ac8SHuacai Chen __cpu_logical_map[i] = -1; 14846859ac8SHuacai Chen } 149255b4658SHuacai Chen #endif 150*e9e7ff16SHuacai Chen acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC, 151*e9e7ff16SHuacai Chen acpi_parse_processor, MAX_CORE_PIC); 152*e9e7ff16SHuacai Chen 153*e9e7ff16SHuacai Chen acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC, 154*e9e7ff16SHuacai Chen acpi_parse_eio_master, MAX_IO_PICS); 15546859ac8SHuacai Chen 156628c3bb4SHuacai Chen loongson_sysconf.nr_cpus = num_processors; 157628c3bb4SHuacai Chen } 158628c3bb4SHuacai Chen 159628c3bb4SHuacai Chen int __init acpi_boot_init(void) 160628c3bb4SHuacai Chen { 161628c3bb4SHuacai Chen /* 162628c3bb4SHuacai Chen * If acpi_disabled, bail out 163628c3bb4SHuacai Chen */ 164628c3bb4SHuacai Chen if (acpi_disabled) 165628c3bb4SHuacai Chen return -1; 166628c3bb4SHuacai Chen 167628c3bb4SHuacai Chen loongson_sysconf.boot_cpu_id = read_csr_cpuid(); 168628c3bb4SHuacai Chen 169628c3bb4SHuacai Chen /* 170628c3bb4SHuacai Chen * Process the Multiple APIC Description Table (MADT), if present 171628c3bb4SHuacai Chen */ 172628c3bb4SHuacai Chen acpi_process_madt(); 173628c3bb4SHuacai Chen 174628c3bb4SHuacai Chen /* Do not enable ACPI SPCR console by default */ 175628c3bb4SHuacai Chen acpi_parse_spcr(earlycon_acpi_spcr_enable, false); 176628c3bb4SHuacai Chen 177628c3bb4SHuacai Chen return 0; 178628c3bb4SHuacai Chen } 179628c3bb4SHuacai Chen 180d4b6f156SHuacai Chen #ifdef CONFIG_ACPI_NUMA 181d4b6f156SHuacai Chen 182d4b6f156SHuacai Chen static __init int setup_node(int pxm) 183d4b6f156SHuacai Chen { 184d4b6f156SHuacai Chen return acpi_map_pxm_to_node(pxm); 185d4b6f156SHuacai Chen } 186d4b6f156SHuacai Chen 187d4b6f156SHuacai Chen /* 188d4b6f156SHuacai Chen * Callback for SLIT parsing. pxm_to_node() returns NUMA_NO_NODE for 189d4b6f156SHuacai Chen * I/O localities since SRAT does not list them. I/O localities are 190d4b6f156SHuacai Chen * not supported at this point. 191d4b6f156SHuacai Chen */ 192d4b6f156SHuacai Chen unsigned int numa_distance_cnt; 193d4b6f156SHuacai Chen 194d4b6f156SHuacai Chen static inline unsigned int get_numa_distances_cnt(struct acpi_table_slit *slit) 195d4b6f156SHuacai Chen { 196d4b6f156SHuacai Chen return slit->locality_count; 197d4b6f156SHuacai Chen } 198d4b6f156SHuacai Chen 199d4b6f156SHuacai Chen void __init numa_set_distance(int from, int to, int distance) 200d4b6f156SHuacai Chen { 201d4b6f156SHuacai Chen if ((u8)distance != distance || (from == to && distance != LOCAL_DISTANCE)) { 202d4b6f156SHuacai Chen pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n", 203d4b6f156SHuacai Chen from, to, distance); 204d4b6f156SHuacai Chen return; 205d4b6f156SHuacai Chen } 206d4b6f156SHuacai Chen 207d4b6f156SHuacai Chen node_distances[from][to] = distance; 208d4b6f156SHuacai Chen } 209d4b6f156SHuacai Chen 210d4b6f156SHuacai Chen /* Callback for Proximity Domain -> CPUID mapping */ 211d4b6f156SHuacai Chen void __init 212d4b6f156SHuacai Chen acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa) 213d4b6f156SHuacai Chen { 214d4b6f156SHuacai Chen int pxm, node; 215d4b6f156SHuacai Chen 216d4b6f156SHuacai Chen if (srat_disabled()) 217d4b6f156SHuacai Chen return; 218d4b6f156SHuacai Chen if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) { 219d4b6f156SHuacai Chen bad_srat(); 220d4b6f156SHuacai Chen return; 221d4b6f156SHuacai Chen } 222d4b6f156SHuacai Chen if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0) 223d4b6f156SHuacai Chen return; 224d4b6f156SHuacai Chen pxm = pa->proximity_domain_lo; 225d4b6f156SHuacai Chen if (acpi_srat_revision >= 2) { 226d4b6f156SHuacai Chen pxm |= (pa->proximity_domain_hi[0] << 8); 227d4b6f156SHuacai Chen pxm |= (pa->proximity_domain_hi[1] << 16); 228d4b6f156SHuacai Chen pxm |= (pa->proximity_domain_hi[2] << 24); 229d4b6f156SHuacai Chen } 230d4b6f156SHuacai Chen node = setup_node(pxm); 231d4b6f156SHuacai Chen if (node < 0) { 232d4b6f156SHuacai Chen pr_err("SRAT: Too many proximity domains %x\n", pxm); 233d4b6f156SHuacai Chen bad_srat(); 234d4b6f156SHuacai Chen return; 235d4b6f156SHuacai Chen } 236d4b6f156SHuacai Chen 237d4b6f156SHuacai Chen if (pa->apic_id >= CONFIG_NR_CPUS) { 238d4b6f156SHuacai Chen pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n", 239d4b6f156SHuacai Chen pxm, pa->apic_id, node); 240d4b6f156SHuacai Chen return; 241d4b6f156SHuacai Chen } 242d4b6f156SHuacai Chen 243d4b6f156SHuacai Chen early_numa_add_cpu(pa->apic_id, node); 244d4b6f156SHuacai Chen 245d4b6f156SHuacai Chen set_cpuid_to_node(pa->apic_id, node); 246d4b6f156SHuacai Chen node_set(node, numa_nodes_parsed); 247d4b6f156SHuacai Chen pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node); 248d4b6f156SHuacai Chen } 249d4b6f156SHuacai Chen 250d4b6f156SHuacai Chen void __init acpi_numa_arch_fixup(void) {} 251d4b6f156SHuacai Chen #endif 252d4b6f156SHuacai Chen 253628c3bb4SHuacai Chen void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size) 254628c3bb4SHuacai Chen { 255628c3bb4SHuacai Chen memblock_reserve(addr, size); 256628c3bb4SHuacai Chen } 25746859ac8SHuacai Chen 25846859ac8SHuacai Chen #ifdef CONFIG_ACPI_HOTPLUG_CPU 25946859ac8SHuacai Chen 26046859ac8SHuacai Chen #include <acpi/processor.h> 26146859ac8SHuacai Chen 262d4b6f156SHuacai Chen static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid) 263d4b6f156SHuacai Chen { 264d4b6f156SHuacai Chen #ifdef CONFIG_ACPI_NUMA 265d4b6f156SHuacai Chen int nid; 266d4b6f156SHuacai Chen 267d4b6f156SHuacai Chen nid = acpi_get_node(handle); 268d4b6f156SHuacai Chen if (nid != NUMA_NO_NODE) { 269d4b6f156SHuacai Chen set_cpuid_to_node(physid, nid); 270d4b6f156SHuacai Chen node_set(nid, numa_nodes_parsed); 271d4b6f156SHuacai Chen set_cpu_numa_node(cpu, nid); 272d4b6f156SHuacai Chen cpumask_set_cpu(cpu, cpumask_of_node(nid)); 273d4b6f156SHuacai Chen } 274d4b6f156SHuacai Chen #endif 275d4b6f156SHuacai Chen return 0; 276d4b6f156SHuacai Chen } 277d4b6f156SHuacai Chen 27846859ac8SHuacai Chen int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu) 27946859ac8SHuacai Chen { 28046859ac8SHuacai Chen int cpu; 28146859ac8SHuacai Chen 28246859ac8SHuacai Chen cpu = set_processor_mask(physid, ACPI_MADT_ENABLED); 28346859ac8SHuacai Chen if (cpu < 0) { 28446859ac8SHuacai Chen pr_info(PREFIX "Unable to map lapic to logical cpu number\n"); 28546859ac8SHuacai Chen return cpu; 28646859ac8SHuacai Chen } 28746859ac8SHuacai Chen 288d4b6f156SHuacai Chen acpi_map_cpu2node(handle, cpu, physid); 289d4b6f156SHuacai Chen 29046859ac8SHuacai Chen *pcpu = cpu; 29146859ac8SHuacai Chen 29246859ac8SHuacai Chen return 0; 29346859ac8SHuacai Chen } 29446859ac8SHuacai Chen EXPORT_SYMBOL(acpi_map_cpu); 29546859ac8SHuacai Chen 29646859ac8SHuacai Chen int acpi_unmap_cpu(int cpu) 29746859ac8SHuacai Chen { 298d4b6f156SHuacai Chen #ifdef CONFIG_ACPI_NUMA 299d4b6f156SHuacai Chen set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE); 300d4b6f156SHuacai Chen #endif 30146859ac8SHuacai Chen set_cpu_present(cpu, false); 30246859ac8SHuacai Chen num_processors--; 30346859ac8SHuacai Chen 30446859ac8SHuacai Chen pr_info("cpu%d hot remove!\n", cpu); 30546859ac8SHuacai Chen 30646859ac8SHuacai Chen return 0; 30746859ac8SHuacai Chen } 30846859ac8SHuacai Chen EXPORT_SYMBOL(acpi_unmap_cpu); 30946859ac8SHuacai Chen 31046859ac8SHuacai Chen #endif /* CONFIG_ACPI_HOTPLUG_CPU */ 311