xref: /linux/arch/loongarch/kernel/acpi.c (revision bb3c90fe347a5321e7d176ed5b21367aa28be9ee)
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 
205 int __init parse_acpi_topology(void)
206 {
207 	int cpu, topology_id;
208 
209 	for_each_possible_cpu(cpu) {
210 		topology_id = find_acpi_cpu_topology(cpu, 0);
211 		if (topology_id < 0) {
212 			pr_warn("Invalid BIOS PPTT\n");
213 			return -ENOENT;
214 		}
215 
216 		if (acpi_pptt_cpu_is_thread(cpu) <= 0)
217 			cpu_data[cpu].core = topology_id;
218 		else {
219 			topology_id = find_acpi_cpu_topology(cpu, 1);
220 			if (topology_id < 0)
221 				return -ENOENT;
222 
223 			cpu_data[cpu].core = topology_id;
224 		}
225 	}
226 
227 	pptt_enabled = 1;
228 
229 	return 0;
230 }
231 
232 #ifndef CONFIG_SUSPEND
233 int (*acpi_suspend_lowlevel)(void);
234 #else
235 int (*acpi_suspend_lowlevel)(void) = loongarch_acpi_suspend;
236 #endif
237 
238 void __init acpi_boot_table_init(void)
239 {
240 	/*
241 	 * If acpi_disabled, bail out
242 	 */
243 	if (acpi_disabled)
244 		goto fdt_earlycon;
245 
246 	/*
247 	 * Initialize the ACPI boot-time table parser.
248 	 */
249 	if (acpi_table_init()) {
250 		disable_acpi();
251 		goto fdt_earlycon;
252 	}
253 
254 	loongson_sysconf.boot_cpu_id = read_csr_cpuid();
255 
256 	/*
257 	 * Process the Multiple APIC Description Table (MADT), if present
258 	 */
259 	acpi_process_madt();
260 
261 	/* Do not enable ACPI SPCR console by default */
262 	acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
263 
264 	if (IS_ENABLED(CONFIG_ACPI_BGRT))
265 		acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);
266 
267 	return;
268 
269 fdt_earlycon:
270 	if (earlycon_acpi_spcr_enable)
271 		early_init_dt_scan_chosen_stdout();
272 }
273 
274 #ifdef CONFIG_ACPI_NUMA
275 
276 /* Callback for Proximity Domain -> CPUID mapping */
277 void __init
278 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
279 {
280 	int pxm, node;
281 
282 	if (srat_disabled())
283 		return;
284 	if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
285 		bad_srat();
286 		return;
287 	}
288 	if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
289 		return;
290 	pxm = pa->proximity_domain_lo;
291 	if (acpi_srat_revision >= 2) {
292 		pxm |= (pa->proximity_domain_hi[0] << 8);
293 		pxm |= (pa->proximity_domain_hi[1] << 16);
294 		pxm |= (pa->proximity_domain_hi[2] << 24);
295 	}
296 	node = acpi_map_pxm_to_node(pxm);
297 	if (node < 0) {
298 		pr_err("SRAT: Too many proximity domains %x\n", pxm);
299 		bad_srat();
300 		return;
301 	}
302 
303 	if (pa->apic_id >= CONFIG_NR_CPUS) {
304 		pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
305 				pxm, pa->apic_id, node);
306 		return;
307 	}
308 
309 	early_numa_add_cpu(pa->apic_id, node);
310 
311 	set_cpuid_to_node(pa->apic_id, node);
312 	node_set(node, numa_nodes_parsed);
313 	pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
314 }
315 
316 void __init
317 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
318 {
319 	int pxm, node;
320 
321 	if (srat_disabled())
322 		return;
323 	if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
324 		bad_srat();
325 		return;
326 	}
327 	if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
328 		return;
329 	pxm = pa->proximity_domain;
330 	node = acpi_map_pxm_to_node(pxm);
331 	if (node < 0) {
332 		pr_err("SRAT: Too many proximity domains %x\n", pxm);
333 		bad_srat();
334 		return;
335 	}
336 
337 	if (pa->apic_id >= CONFIG_NR_CPUS) {
338 		pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
339 				pxm, pa->apic_id, node);
340 		return;
341 	}
342 
343 	early_numa_add_cpu(pa->apic_id, node);
344 
345 	set_cpuid_to_node(pa->apic_id, node);
346 	node_set(node, numa_nodes_parsed);
347 	pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
348 }
349 
350 #endif
351 
352 void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
353 {
354 	memblock_reserve(addr, size);
355 }
356 
357 #ifdef CONFIG_ACPI_HOTPLUG_CPU
358 
359 #include <acpi/processor.h>
360 
361 static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid)
362 {
363 #ifdef CONFIG_ACPI_NUMA
364 	int nid;
365 
366 	nid = acpi_get_node(handle);
367 
368 	if (nid != NUMA_NO_NODE)
369 		nid = early_cpu_to_node(cpu);
370 
371 	if (nid != NUMA_NO_NODE) {
372 		set_cpuid_to_node(physid, nid);
373 		node_set(nid, numa_nodes_parsed);
374 		set_cpu_numa_node(cpu, nid);
375 		cpumask_set_cpu(cpu, cpumask_of_node(nid));
376 	}
377 #endif
378 	return 0;
379 }
380 
381 int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu)
382 {
383 	int cpu;
384 
385 	cpu = cpu_number_map(physid);
386 	if (cpu < 0 || cpu >= nr_cpu_ids) {
387 		pr_info(PREFIX "Unable to map lapic to logical cpu number\n");
388 		return -ERANGE;
389 	}
390 
391 	num_processors++;
392 	set_cpu_present(cpu, true);
393 	acpi_map_cpu2node(handle, cpu, physid);
394 
395 	*pcpu = cpu;
396 
397 	return 0;
398 }
399 EXPORT_SYMBOL(acpi_map_cpu);
400 
401 int acpi_unmap_cpu(int cpu)
402 {
403 #ifdef CONFIG_ACPI_NUMA
404 	set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE);
405 #endif
406 	set_cpu_present(cpu, false);
407 	num_processors--;
408 
409 	pr_info("cpu%d hot remove!\n", cpu);
410 
411 	return 0;
412 }
413 EXPORT_SYMBOL(acpi_unmap_cpu);
414 
415 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
416 
417 int acpi_get_cpu_uid(unsigned int cpu, u32 *uid)
418 {
419 	if (cpu >= nr_cpu_ids)
420 		return -EINVAL;
421 	*uid = acpi_core_pic[cpu_logical_map(cpu)].processor_id;
422 	return 0;
423 }
424 EXPORT_SYMBOL_GPL(acpi_get_cpu_uid);
425