1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * acpi.c - Architecture-Specific Low-Level ACPI Boot Support 4 * 5 * Author: Jianmin Lv <lvjianmin@loongson.cn> 6 * Huacai Chen <chenhuacai@loongson.cn> 7 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 8 */ 9 10 #include <linux/init.h> 11 #include <linux/acpi.h> 12 #include <linux/efi-bgrt.h> 13 #include <linux/export.h> 14 #include <linux/irq.h> 15 #include <linux/irqdomain.h> 16 #include <linux/memblock.h> 17 #include <linux/of_fdt.h> 18 #include <linux/serial_core.h> 19 #include <linux/vmalloc.h> 20 #include <asm/io.h> 21 #include <asm/numa.h> 22 #include <asm/loongson.h> 23 24 int acpi_disabled; 25 EXPORT_SYMBOL(acpi_disabled); 26 int acpi_noirq; 27 int acpi_pci_disabled; 28 EXPORT_SYMBOL(acpi_pci_disabled); 29 int acpi_strict = 1; /* We have no workarounds on LoongArch */ 30 int num_processors; 31 int disabled_cpus; 32 33 u64 acpi_saved_sp; 34 35 #define PREFIX "ACPI: " 36 37 struct acpi_madt_core_pic acpi_core_pic[MAX_CORE_PIC]; 38 39 void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size) 40 { 41 42 if (!phys || !size) 43 return NULL; 44 45 return early_memremap(phys, size); 46 } 47 void __init __acpi_unmap_table(void __iomem *map, unsigned long size) 48 { 49 if (!map || !size) 50 return; 51 52 early_memunmap(map, size); 53 } 54 55 void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size) 56 { 57 if (!memblock_is_memory(phys)) 58 return ioremap(phys, size); 59 else 60 return ioremap_cache(phys, size); 61 } 62 63 #define PIO_BASE (unsigned long)PCI_IOBASE 64 #define PIO_SIZE ALIGN(ISA_IOSIZE, PAGE_SIZE) 65 66 static bool acpi_pio; 67 68 /* Add PIO for early access */ 69 void acpi_add_early_pio(void) 70 { 71 if (!acpi_disabled) { 72 acpi_pio = true; 73 vmap_page_range(PIO_BASE, PIO_BASE + PIO_SIZE, 74 LOONGSON_LIO_BASE, pgprot_device(PAGE_KERNEL)); 75 } 76 } 77 78 /* Remove PIO for PCI register */ 79 void acpi_remove_early_pio(void) 80 { 81 if (!acpi_pio) 82 return; 83 84 if (!acpi_disabled) { 85 acpi_pio = false; 86 vunmap_range(PIO_BASE, PIO_BASE + PIO_SIZE); 87 } 88 } 89 90 #ifdef CONFIG_SMP 91 static int set_processor_mask(u32 id, u32 pass) 92 { 93 int cpu = -1, cpuid = id; 94 95 if (num_processors >= NR_CPUS) { 96 pr_warn(PREFIX "nr_cpus limit of %i reached." 97 " processor 0x%x ignored.\n", NR_CPUS, cpuid); 98 99 return -ENODEV; 100 101 } 102 103 if (cpuid == loongson_sysconf.boot_cpu_id) 104 cpu = 0; 105 106 switch (pass) { 107 case 1: /* Pass 1 handle enabled processors */ 108 if (cpu < 0) 109 cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS); 110 num_processors++; 111 set_cpu_present(cpu, true); 112 break; 113 case 2: /* Pass 2 handle disabled processors */ 114 if (cpu < 0) 115 cpu = find_first_zero_bit(cpumask_bits(cpu_possible_mask), NR_CPUS); 116 disabled_cpus++; 117 break; 118 default: 119 return cpu; 120 } 121 122 set_cpu_possible(cpu, true); 123 __cpu_number_map[cpuid] = cpu; 124 __cpu_logical_map[cpu] = cpuid; 125 126 return cpu; 127 } 128 #endif 129 130 static int __init 131 acpi_parse_p1_processor(union acpi_subtable_headers *header, const unsigned long end) 132 { 133 struct acpi_madt_core_pic *processor = NULL; 134 135 processor = (struct acpi_madt_core_pic *)header; 136 if (BAD_MADT_ENTRY(processor, end)) 137 return -EINVAL; 138 139 acpi_table_print_madt_entry(&header->common); 140 #ifdef CONFIG_SMP 141 acpi_core_pic[processor->core_id] = *processor; 142 if (processor->flags & ACPI_MADT_ENABLED) 143 set_processor_mask(processor->core_id, 1); 144 #endif 145 146 return 0; 147 } 148 149 static int __init 150 acpi_parse_p2_processor(union acpi_subtable_headers *header, const unsigned long end) 151 { 152 struct acpi_madt_core_pic *processor = NULL; 153 154 processor = (struct acpi_madt_core_pic *)header; 155 if (BAD_MADT_ENTRY(processor, end)) 156 return -EINVAL; 157 158 #ifdef CONFIG_SMP 159 if (!(processor->flags & ACPI_MADT_ENABLED)) 160 set_processor_mask(processor->core_id, 2); 161 #endif 162 163 return 0; 164 } 165 static int __init 166 acpi_parse_eio_master(union acpi_subtable_headers *header, const unsigned long end) 167 { 168 static int core = 0; 169 struct acpi_madt_eio_pic *eiointc = NULL; 170 171 eiointc = (struct acpi_madt_eio_pic *)header; 172 if (BAD_MADT_ENTRY(eiointc, end)) 173 return -EINVAL; 174 175 core = eiointc->node * CORES_PER_EIO_NODE; 176 set_bit(core, loongson_sysconf.cores_io_master); 177 178 return 0; 179 } 180 181 static void __init acpi_process_madt(void) 182 { 183 #ifdef CONFIG_SMP 184 int i; 185 186 for (i = 0; i < NR_CPUS; i++) { 187 __cpu_number_map[i] = -1; 188 __cpu_logical_map[i] = -1; 189 } 190 #endif 191 acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC, 192 acpi_parse_p1_processor, MAX_CORE_PIC); 193 194 acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC, 195 acpi_parse_p2_processor, MAX_CORE_PIC); 196 197 acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC, 198 acpi_parse_eio_master, MAX_IO_PICS); 199 200 loongson_sysconf.nr_cpus = num_processors; 201 } 202 203 int pptt_enabled; 204 static int acpi_nr_packages; 205 static int acpi_package_ids[MAX_PACKAGES]; 206 207 int __init parse_acpi_topology(void) 208 { 209 int i, cpu, topology_id; 210 211 for_each_possible_cpu(cpu) { 212 topology_id = find_acpi_cpu_topology(cpu, 0); 213 if (topology_id < 0) { 214 pr_warn("Invalid BIOS PPTT\n"); 215 return -ENOENT; 216 } 217 218 if (acpi_pptt_cpu_is_thread(cpu) <= 0) 219 cpu_data[cpu].core = topology_id; 220 else { 221 topology_id = find_acpi_cpu_topology(cpu, 1); 222 if (topology_id < 0) 223 return -ENOENT; 224 225 cpu_data[cpu].core = topology_id; 226 } 227 228 topology_id = find_acpi_cpu_topology_package(cpu); 229 if (topology_id < 0) { 230 pr_warn("Invalid BIOS PPTT\n"); 231 return -ENOENT; 232 } 233 234 for (i = 0; i < acpi_nr_packages; i++) 235 if (acpi_package_ids[i] == topology_id) 236 break; 237 238 if (i == acpi_nr_packages) 239 acpi_package_ids[acpi_nr_packages++] = topology_id; 240 241 cpu_data[cpu].package = topology_id; 242 } 243 244 for_each_possible_cpu(cpu) { 245 for (i = 0; i < acpi_nr_packages; i++) 246 if (cpu_data[cpu].package == acpi_package_ids[i]) { 247 cpu_data[cpu].package = i; /* Canonicalize */ 248 break; 249 } 250 } 251 252 pptt_enabled = 1; 253 254 return 0; 255 } 256 257 #ifndef CONFIG_SUSPEND 258 int (*acpi_suspend_lowlevel)(void); 259 #else 260 int (*acpi_suspend_lowlevel)(void) = loongarch_acpi_suspend; 261 #endif 262 263 void __init acpi_boot_table_init(void) 264 { 265 /* 266 * If acpi_disabled, bail out 267 */ 268 if (acpi_disabled) 269 goto fdt_earlycon; 270 271 /* 272 * Initialize the ACPI boot-time table parser. 273 */ 274 if (acpi_table_init()) { 275 disable_acpi(); 276 goto fdt_earlycon; 277 } 278 279 loongson_sysconf.boot_cpu_id = read_csr_cpuid(); 280 281 /* 282 * Process the Multiple APIC Description Table (MADT), if present 283 */ 284 acpi_process_madt(); 285 286 /* Do not enable ACPI SPCR console by default */ 287 acpi_parse_spcr(earlycon_acpi_spcr_enable, false); 288 289 if (IS_ENABLED(CONFIG_ACPI_BGRT)) 290 acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt); 291 292 return; 293 294 fdt_earlycon: 295 if (earlycon_acpi_spcr_enable) 296 early_init_dt_scan_chosen_stdout(); 297 } 298 299 #ifdef CONFIG_ACPI_NUMA 300 301 /* Callback for Proximity Domain -> CPUID mapping */ 302 void __init 303 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa) 304 { 305 int pxm, node; 306 307 if (srat_disabled()) 308 return; 309 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) { 310 bad_srat(); 311 return; 312 } 313 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0) 314 return; 315 pxm = pa->proximity_domain_lo; 316 if (acpi_srat_revision >= 2) { 317 pxm |= (pa->proximity_domain_hi[0] << 8); 318 pxm |= (pa->proximity_domain_hi[1] << 16); 319 pxm |= (pa->proximity_domain_hi[2] << 24); 320 } 321 node = acpi_map_pxm_to_node(pxm); 322 if (node < 0) { 323 pr_err("SRAT: Too many proximity domains %x\n", pxm); 324 bad_srat(); 325 return; 326 } 327 328 if (pa->apic_id >= CONFIG_NR_CPUS) { 329 pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n", 330 pxm, pa->apic_id, node); 331 return; 332 } 333 334 early_numa_add_cpu(pa->apic_id, node); 335 336 set_cpuid_to_node(pa->apic_id, node); 337 node_set(node, numa_nodes_parsed); 338 pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node); 339 } 340 341 void __init 342 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa) 343 { 344 int pxm, node; 345 346 if (srat_disabled()) 347 return; 348 if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) { 349 bad_srat(); 350 return; 351 } 352 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0) 353 return; 354 pxm = pa->proximity_domain; 355 node = acpi_map_pxm_to_node(pxm); 356 if (node < 0) { 357 pr_err("SRAT: Too many proximity domains %x\n", pxm); 358 bad_srat(); 359 return; 360 } 361 362 if (pa->apic_id >= CONFIG_NR_CPUS) { 363 pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n", 364 pxm, pa->apic_id, node); 365 return; 366 } 367 368 early_numa_add_cpu(pa->apic_id, node); 369 370 set_cpuid_to_node(pa->apic_id, node); 371 node_set(node, numa_nodes_parsed); 372 pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node); 373 } 374 375 #endif 376 377 void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size) 378 { 379 memblock_reserve(addr, size); 380 } 381 382 #ifdef CONFIG_ACPI_HOTPLUG_CPU 383 384 #include <acpi/processor.h> 385 386 static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid) 387 { 388 #ifdef CONFIG_ACPI_NUMA 389 int nid; 390 391 nid = acpi_get_node(handle); 392 393 if (nid != NUMA_NO_NODE) 394 nid = early_cpu_to_node(cpu); 395 396 if (nid != NUMA_NO_NODE) { 397 set_cpuid_to_node(physid, nid); 398 node_set(nid, numa_nodes_parsed); 399 set_cpu_numa_node(cpu, nid); 400 cpumask_set_cpu(cpu, cpumask_of_node(nid)); 401 } 402 #endif 403 return 0; 404 } 405 406 int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu) 407 { 408 int cpu; 409 410 cpu = cpu_number_map(physid); 411 if (cpu < 0 || cpu >= nr_cpu_ids) { 412 pr_info(PREFIX "Unable to map lapic to logical cpu number\n"); 413 return -ERANGE; 414 } 415 416 num_processors++; 417 set_cpu_present(cpu, true); 418 acpi_map_cpu2node(handle, cpu, physid); 419 420 *pcpu = cpu; 421 422 return 0; 423 } 424 EXPORT_SYMBOL(acpi_map_cpu); 425 426 int acpi_unmap_cpu(int cpu) 427 { 428 #ifdef CONFIG_ACPI_NUMA 429 set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE); 430 #endif 431 set_cpu_present(cpu, false); 432 num_processors--; 433 434 pr_info("cpu%d hot remove!\n", cpu); 435 436 return 0; 437 } 438 EXPORT_SYMBOL(acpi_unmap_cpu); 439 440 #endif /* CONFIG_ACPI_HOTPLUG_CPU */ 441 442 int acpi_get_cpu_uid(unsigned int cpu, u32 *uid) 443 { 444 if (cpu >= nr_cpu_ids) 445 return -EINVAL; 446 *uid = acpi_core_pic[cpu_logical_map(cpu)].processor_id; 447 return 0; 448 } 449 EXPORT_SYMBOL_GPL(acpi_get_cpu_uid); 450