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