xref: /openbmc/linux/arch/loongarch/kernel/acpi.c (revision 96d3c5a7d20ec546e44695983fe0508c6f904248)
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>
1588d4d957SBinbin Zhou #include <linux/of_fdt.h>
16628c3bb4SHuacai Chen #include <linux/serial_core.h>
17628c3bb4SHuacai Chen #include <asm/io.h>
18d4b6f156SHuacai Chen #include <asm/numa.h>
19628c3bb4SHuacai Chen #include <asm/loongson.h>
20628c3bb4SHuacai Chen 
21628c3bb4SHuacai Chen int acpi_disabled;
22628c3bb4SHuacai Chen EXPORT_SYMBOL(acpi_disabled);
23628c3bb4SHuacai Chen int acpi_noirq;
24628c3bb4SHuacai Chen int acpi_pci_disabled;
25628c3bb4SHuacai Chen EXPORT_SYMBOL(acpi_pci_disabled);
26628c3bb4SHuacai Chen int acpi_strict = 1; /* We have no workarounds on LoongArch */
27628c3bb4SHuacai Chen int num_processors;
28628c3bb4SHuacai Chen int disabled_cpus;
29628c3bb4SHuacai Chen 
30628c3bb4SHuacai Chen u64 acpi_saved_sp;
31628c3bb4SHuacai Chen 
32628c3bb4SHuacai Chen #define PREFIX			"ACPI: "
33628c3bb4SHuacai Chen 
34*88e189bdSHuacai Chen struct acpi_madt_core_pic acpi_core_pic[MAX_CORE_PIC];
35f6f0c9a7SHuacai Chen 
__acpi_map_table(unsigned long phys,unsigned long size)36628c3bb4SHuacai Chen void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size)
37628c3bb4SHuacai Chen {
38628c3bb4SHuacai Chen 
39628c3bb4SHuacai Chen 	if (!phys || !size)
40628c3bb4SHuacai Chen 		return NULL;
41628c3bb4SHuacai Chen 
42628c3bb4SHuacai Chen 	return early_memremap(phys, size);
43628c3bb4SHuacai Chen }
__acpi_unmap_table(void __iomem * map,unsigned long size)44628c3bb4SHuacai Chen void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
45628c3bb4SHuacai Chen {
46628c3bb4SHuacai Chen 	if (!map || !size)
47628c3bb4SHuacai Chen 		return;
48628c3bb4SHuacai Chen 
49628c3bb4SHuacai Chen 	early_memunmap(map, size);
50628c3bb4SHuacai Chen }
51628c3bb4SHuacai Chen 
acpi_os_ioremap(acpi_physical_address phys,acpi_size size)52e0fba87cSHuacai Chen void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
53628c3bb4SHuacai Chen {
54628c3bb4SHuacai Chen 	if (!memblock_is_memory(phys))
55628c3bb4SHuacai Chen 		return ioremap(phys, size);
56628c3bb4SHuacai Chen 	else
57628c3bb4SHuacai Chen 		return ioremap_cache(phys, size);
58628c3bb4SHuacai Chen }
59628c3bb4SHuacai Chen 
60255b4658SHuacai Chen #ifdef CONFIG_SMP
set_processor_mask(u32 id,u32 flags)6146859ac8SHuacai Chen static int set_processor_mask(u32 id, u32 flags)
6246859ac8SHuacai Chen {
6346859ac8SHuacai Chen 
6446859ac8SHuacai Chen 	int cpu, cpuid = id;
6546859ac8SHuacai Chen 
6646859ac8SHuacai Chen 	if (num_processors >= nr_cpu_ids) {
6746859ac8SHuacai Chen 		pr_warn(PREFIX "nr_cpus/possible_cpus limit of %i reached."
6846859ac8SHuacai Chen 			" processor 0x%x ignored.\n", nr_cpu_ids, cpuid);
6946859ac8SHuacai Chen 
7046859ac8SHuacai Chen 		return -ENODEV;
7146859ac8SHuacai Chen 
7246859ac8SHuacai Chen 	}
7346859ac8SHuacai Chen 	if (cpuid == loongson_sysconf.boot_cpu_id)
7446859ac8SHuacai Chen 		cpu = 0;
7546859ac8SHuacai Chen 	else
7646859ac8SHuacai Chen 		cpu = cpumask_next_zero(-1, cpu_present_mask);
7746859ac8SHuacai Chen 
7846859ac8SHuacai Chen 	if (flags & ACPI_MADT_ENABLED) {
7946859ac8SHuacai Chen 		num_processors++;
8046859ac8SHuacai Chen 		set_cpu_possible(cpu, true);
8146859ac8SHuacai Chen 		set_cpu_present(cpu, true);
8246859ac8SHuacai Chen 		__cpu_number_map[cpuid] = cpu;
8346859ac8SHuacai Chen 		__cpu_logical_map[cpu] = cpuid;
8446859ac8SHuacai Chen 	} else
8546859ac8SHuacai Chen 		disabled_cpus++;
8646859ac8SHuacai Chen 
8746859ac8SHuacai Chen 	return cpu;
8846859ac8SHuacai Chen }
89255b4658SHuacai Chen #endif
9046859ac8SHuacai Chen 
91e9e7ff16SHuacai Chen static int __init
acpi_parse_processor(union acpi_subtable_headers * header,const unsigned long end)92e9e7ff16SHuacai Chen acpi_parse_processor(union acpi_subtable_headers *header, const unsigned long end)
93e9e7ff16SHuacai Chen {
94e9e7ff16SHuacai Chen 	struct acpi_madt_core_pic *processor = NULL;
95e9e7ff16SHuacai Chen 
96e9e7ff16SHuacai Chen 	processor = (struct acpi_madt_core_pic *)header;
97e9e7ff16SHuacai Chen 	if (BAD_MADT_ENTRY(processor, end))
98e9e7ff16SHuacai Chen 		return -EINVAL;
99e9e7ff16SHuacai Chen 
100e9e7ff16SHuacai Chen 	acpi_table_print_madt_entry(&header->common);
101e9e7ff16SHuacai Chen #ifdef CONFIG_SMP
102f6f0c9a7SHuacai Chen 	acpi_core_pic[processor->core_id] = *processor;
103e9e7ff16SHuacai Chen 	set_processor_mask(processor->core_id, processor->flags);
104e9e7ff16SHuacai Chen #endif
105e9e7ff16SHuacai Chen 
106e9e7ff16SHuacai Chen 	return 0;
107e9e7ff16SHuacai Chen }
108e9e7ff16SHuacai Chen 
109e9e7ff16SHuacai Chen static int __init
acpi_parse_eio_master(union acpi_subtable_headers * header,const unsigned long end)110e9e7ff16SHuacai Chen acpi_parse_eio_master(union acpi_subtable_headers *header, const unsigned long end)
111e9e7ff16SHuacai Chen {
112e9e7ff16SHuacai Chen 	static int core = 0;
113e9e7ff16SHuacai Chen 	struct acpi_madt_eio_pic *eiointc = NULL;
114e9e7ff16SHuacai Chen 
115e9e7ff16SHuacai Chen 	eiointc = (struct acpi_madt_eio_pic *)header;
116e9e7ff16SHuacai Chen 	if (BAD_MADT_ENTRY(eiointc, end))
117e9e7ff16SHuacai Chen 		return -EINVAL;
118e9e7ff16SHuacai Chen 
119e9e7ff16SHuacai Chen 	core = eiointc->node * CORES_PER_EIO_NODE;
120e9e7ff16SHuacai Chen 	set_bit(core, &(loongson_sysconf.cores_io_master));
121e9e7ff16SHuacai Chen 
122e9e7ff16SHuacai Chen 	return 0;
123e9e7ff16SHuacai Chen }
124e9e7ff16SHuacai Chen 
acpi_process_madt(void)125628c3bb4SHuacai Chen static void __init acpi_process_madt(void)
126628c3bb4SHuacai Chen {
127255b4658SHuacai Chen #ifdef CONFIG_SMP
12846859ac8SHuacai Chen 	int i;
12946859ac8SHuacai Chen 
13046859ac8SHuacai Chen 	for (i = 0; i < NR_CPUS; i++) {
13146859ac8SHuacai Chen 		__cpu_number_map[i] = -1;
13246859ac8SHuacai Chen 		__cpu_logical_map[i] = -1;
13346859ac8SHuacai Chen 	}
134255b4658SHuacai Chen #endif
135e9e7ff16SHuacai Chen 	acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC,
136e9e7ff16SHuacai Chen 			acpi_parse_processor, MAX_CORE_PIC);
137e9e7ff16SHuacai Chen 
138e9e7ff16SHuacai Chen 	acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC,
139e9e7ff16SHuacai Chen 			acpi_parse_eio_master, MAX_IO_PICS);
14046859ac8SHuacai Chen 
141628c3bb4SHuacai Chen 	loongson_sysconf.nr_cpus = num_processors;
142628c3bb4SHuacai Chen }
143628c3bb4SHuacai Chen 
144f6f0c9a7SHuacai Chen int pptt_enabled;
145f6f0c9a7SHuacai Chen 
parse_acpi_topology(void)146f6f0c9a7SHuacai Chen int __init parse_acpi_topology(void)
147f6f0c9a7SHuacai Chen {
148f6f0c9a7SHuacai Chen 	int cpu, topology_id;
149f6f0c9a7SHuacai Chen 
150f6f0c9a7SHuacai Chen 	for_each_possible_cpu(cpu) {
151f6f0c9a7SHuacai Chen 		topology_id = find_acpi_cpu_topology(cpu, 0);
152f6f0c9a7SHuacai Chen 		if (topology_id < 0) {
153f6f0c9a7SHuacai Chen 			pr_warn("Invalid BIOS PPTT\n");
154f6f0c9a7SHuacai Chen 			return -ENOENT;
155f6f0c9a7SHuacai Chen 		}
156f6f0c9a7SHuacai Chen 
157f6f0c9a7SHuacai Chen 		if (acpi_pptt_cpu_is_thread(cpu) <= 0)
158f6f0c9a7SHuacai Chen 			cpu_data[cpu].core = topology_id;
159f6f0c9a7SHuacai Chen 		else {
160f6f0c9a7SHuacai Chen 			topology_id = find_acpi_cpu_topology(cpu, 1);
161f6f0c9a7SHuacai Chen 			if (topology_id < 0)
162f6f0c9a7SHuacai Chen 				return -ENOENT;
163f6f0c9a7SHuacai Chen 
164f6f0c9a7SHuacai Chen 			cpu_data[cpu].core = topology_id;
165f6f0c9a7SHuacai Chen 		}
166f6f0c9a7SHuacai Chen 	}
167f6f0c9a7SHuacai Chen 
168f6f0c9a7SHuacai Chen 	pptt_enabled = 1;
169f6f0c9a7SHuacai Chen 
170f6f0c9a7SHuacai Chen 	return 0;
171f6f0c9a7SHuacai Chen }
172f6f0c9a7SHuacai Chen 
173366bb35aSHuacai Chen #ifndef CONFIG_SUSPEND
174366bb35aSHuacai Chen int (*acpi_suspend_lowlevel)(void);
175366bb35aSHuacai Chen #else
176366bb35aSHuacai Chen int (*acpi_suspend_lowlevel)(void) = loongarch_acpi_suspend;
177366bb35aSHuacai Chen #endif
178366bb35aSHuacai Chen 
acpi_boot_table_init(void)179538eafc6SHuacai Chen void __init acpi_boot_table_init(void)
180628c3bb4SHuacai Chen {
181628c3bb4SHuacai Chen 	/*
182628c3bb4SHuacai Chen 	 * If acpi_disabled, bail out
183628c3bb4SHuacai Chen 	 */
184628c3bb4SHuacai Chen 	if (acpi_disabled)
18588d4d957SBinbin Zhou 		goto fdt_earlycon;
186538eafc6SHuacai Chen 
187538eafc6SHuacai Chen 	/*
188538eafc6SHuacai Chen 	 * Initialize the ACPI boot-time table parser.
189538eafc6SHuacai Chen 	 */
190538eafc6SHuacai Chen 	if (acpi_table_init()) {
191538eafc6SHuacai Chen 		disable_acpi();
19288d4d957SBinbin Zhou 		goto fdt_earlycon;
193538eafc6SHuacai Chen 	}
194628c3bb4SHuacai Chen 
195628c3bb4SHuacai Chen 	loongson_sysconf.boot_cpu_id = read_csr_cpuid();
196628c3bb4SHuacai Chen 
197628c3bb4SHuacai Chen 	/*
198628c3bb4SHuacai Chen 	 * Process the Multiple APIC Description Table (MADT), if present
199628c3bb4SHuacai Chen 	 */
200628c3bb4SHuacai Chen 	acpi_process_madt();
201628c3bb4SHuacai Chen 
202628c3bb4SHuacai Chen 	/* Do not enable ACPI SPCR console by default */
203628c3bb4SHuacai Chen 	acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
20488d4d957SBinbin Zhou 
20588d4d957SBinbin Zhou 	return;
20688d4d957SBinbin Zhou 
20788d4d957SBinbin Zhou fdt_earlycon:
20888d4d957SBinbin Zhou 	if (earlycon_acpi_spcr_enable)
20988d4d957SBinbin Zhou 		early_init_dt_scan_chosen_stdout();
210628c3bb4SHuacai Chen }
211628c3bb4SHuacai Chen 
212d4b6f156SHuacai Chen #ifdef CONFIG_ACPI_NUMA
213d4b6f156SHuacai Chen 
setup_node(int pxm)214d4b6f156SHuacai Chen static __init int setup_node(int pxm)
215d4b6f156SHuacai Chen {
216d4b6f156SHuacai Chen 	return acpi_map_pxm_to_node(pxm);
217d4b6f156SHuacai Chen }
218d4b6f156SHuacai Chen 
219d4b6f156SHuacai Chen /*
220d4b6f156SHuacai Chen  * Callback for SLIT parsing.  pxm_to_node() returns NUMA_NO_NODE for
221d4b6f156SHuacai Chen  * I/O localities since SRAT does not list them.  I/O localities are
222d4b6f156SHuacai Chen  * not supported at this point.
223d4b6f156SHuacai Chen  */
224d4b6f156SHuacai Chen unsigned int numa_distance_cnt;
225d4b6f156SHuacai Chen 
get_numa_distances_cnt(struct acpi_table_slit * slit)226d4b6f156SHuacai Chen static inline unsigned int get_numa_distances_cnt(struct acpi_table_slit *slit)
227d4b6f156SHuacai Chen {
228d4b6f156SHuacai Chen 	return slit->locality_count;
229d4b6f156SHuacai Chen }
230d4b6f156SHuacai Chen 
numa_set_distance(int from,int to,int distance)231d4b6f156SHuacai Chen void __init numa_set_distance(int from, int to, int distance)
232d4b6f156SHuacai Chen {
233d4b6f156SHuacai Chen 	if ((u8)distance != distance || (from == to && distance != LOCAL_DISTANCE)) {
234d4b6f156SHuacai Chen 		pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
235d4b6f156SHuacai Chen 				from, to, distance);
236d4b6f156SHuacai Chen 		return;
237d4b6f156SHuacai Chen 	}
238d4b6f156SHuacai Chen 
239d4b6f156SHuacai Chen 	node_distances[from][to] = distance;
240d4b6f156SHuacai Chen }
241d4b6f156SHuacai Chen 
242d4b6f156SHuacai Chen /* Callback for Proximity Domain -> CPUID mapping */
243d4b6f156SHuacai Chen void __init
acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity * pa)244d4b6f156SHuacai Chen acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
245d4b6f156SHuacai Chen {
246d4b6f156SHuacai Chen 	int pxm, node;
247d4b6f156SHuacai Chen 
248d4b6f156SHuacai Chen 	if (srat_disabled())
249d4b6f156SHuacai Chen 		return;
250d4b6f156SHuacai Chen 	if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
251d4b6f156SHuacai Chen 		bad_srat();
252d4b6f156SHuacai Chen 		return;
253d4b6f156SHuacai Chen 	}
254d4b6f156SHuacai Chen 	if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
255d4b6f156SHuacai Chen 		return;
256d4b6f156SHuacai Chen 	pxm = pa->proximity_domain_lo;
257d4b6f156SHuacai Chen 	if (acpi_srat_revision >= 2) {
258d4b6f156SHuacai Chen 		pxm |= (pa->proximity_domain_hi[0] << 8);
259d4b6f156SHuacai Chen 		pxm |= (pa->proximity_domain_hi[1] << 16);
260d4b6f156SHuacai Chen 		pxm |= (pa->proximity_domain_hi[2] << 24);
261d4b6f156SHuacai Chen 	}
262d4b6f156SHuacai Chen 	node = setup_node(pxm);
263d4b6f156SHuacai Chen 	if (node < 0) {
264d4b6f156SHuacai Chen 		pr_err("SRAT: Too many proximity domains %x\n", pxm);
265d4b6f156SHuacai Chen 		bad_srat();
266d4b6f156SHuacai Chen 		return;
267d4b6f156SHuacai Chen 	}
268d4b6f156SHuacai Chen 
269d4b6f156SHuacai Chen 	if (pa->apic_id >= CONFIG_NR_CPUS) {
270d4b6f156SHuacai Chen 		pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
271d4b6f156SHuacai Chen 				pxm, pa->apic_id, node);
272d4b6f156SHuacai Chen 		return;
273d4b6f156SHuacai Chen 	}
274d4b6f156SHuacai Chen 
275d4b6f156SHuacai Chen 	early_numa_add_cpu(pa->apic_id, node);
276d4b6f156SHuacai Chen 
277d4b6f156SHuacai Chen 	set_cpuid_to_node(pa->apic_id, node);
278d4b6f156SHuacai Chen 	node_set(node, numa_nodes_parsed);
279d4b6f156SHuacai Chen 	pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
280d4b6f156SHuacai Chen }
281d4b6f156SHuacai Chen 
282d4b6f156SHuacai Chen #endif
283d4b6f156SHuacai Chen 
arch_reserve_mem_area(acpi_physical_address addr,size_t size)284628c3bb4SHuacai Chen void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
285628c3bb4SHuacai Chen {
286628c3bb4SHuacai Chen 	memblock_reserve(addr, size);
287628c3bb4SHuacai Chen }
28846859ac8SHuacai Chen 
28946859ac8SHuacai Chen #ifdef CONFIG_ACPI_HOTPLUG_CPU
29046859ac8SHuacai Chen 
29146859ac8SHuacai Chen #include <acpi/processor.h>
29246859ac8SHuacai Chen 
acpi_map_cpu2node(acpi_handle handle,int cpu,int physid)293d4b6f156SHuacai Chen static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid)
294d4b6f156SHuacai Chen {
295d4b6f156SHuacai Chen #ifdef CONFIG_ACPI_NUMA
296d4b6f156SHuacai Chen 	int nid;
297d4b6f156SHuacai Chen 
298d4b6f156SHuacai Chen 	nid = acpi_get_node(handle);
299d4b6f156SHuacai Chen 	if (nid != NUMA_NO_NODE) {
300d4b6f156SHuacai Chen 		set_cpuid_to_node(physid, nid);
301d4b6f156SHuacai Chen 		node_set(nid, numa_nodes_parsed);
302d4b6f156SHuacai Chen 		set_cpu_numa_node(cpu, nid);
303d4b6f156SHuacai Chen 		cpumask_set_cpu(cpu, cpumask_of_node(nid));
304d4b6f156SHuacai Chen 	}
305d4b6f156SHuacai Chen #endif
306d4b6f156SHuacai Chen 	return 0;
307d4b6f156SHuacai Chen }
308d4b6f156SHuacai Chen 
acpi_map_cpu(acpi_handle handle,phys_cpuid_t physid,u32 acpi_id,int * pcpu)30946859ac8SHuacai Chen int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu)
31046859ac8SHuacai Chen {
31146859ac8SHuacai Chen 	int cpu;
31246859ac8SHuacai Chen 
31346859ac8SHuacai Chen 	cpu = set_processor_mask(physid, ACPI_MADT_ENABLED);
31446859ac8SHuacai Chen 	if (cpu < 0) {
31546859ac8SHuacai Chen 		pr_info(PREFIX "Unable to map lapic to logical cpu number\n");
31646859ac8SHuacai Chen 		return cpu;
31746859ac8SHuacai Chen 	}
31846859ac8SHuacai Chen 
319d4b6f156SHuacai Chen 	acpi_map_cpu2node(handle, cpu, physid);
320d4b6f156SHuacai Chen 
32146859ac8SHuacai Chen 	*pcpu = cpu;
32246859ac8SHuacai Chen 
32346859ac8SHuacai Chen 	return 0;
32446859ac8SHuacai Chen }
32546859ac8SHuacai Chen EXPORT_SYMBOL(acpi_map_cpu);
32646859ac8SHuacai Chen 
acpi_unmap_cpu(int cpu)32746859ac8SHuacai Chen int acpi_unmap_cpu(int cpu)
32846859ac8SHuacai Chen {
329d4b6f156SHuacai Chen #ifdef CONFIG_ACPI_NUMA
330d4b6f156SHuacai Chen 	set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE);
331d4b6f156SHuacai Chen #endif
33246859ac8SHuacai Chen 	set_cpu_present(cpu, false);
33346859ac8SHuacai Chen 	num_processors--;
33446859ac8SHuacai Chen 
33546859ac8SHuacai Chen 	pr_info("cpu%d hot remove!\n", cpu);
33646859ac8SHuacai Chen 
33746859ac8SHuacai Chen 	return 0;
33846859ac8SHuacai Chen }
33946859ac8SHuacai Chen EXPORT_SYMBOL(acpi_unmap_cpu);
34046859ac8SHuacai Chen 
34146859ac8SHuacai Chen #endif /* CONFIG_ACPI_HOTPLUG_CPU */
342