xref: /linux/arch/loongarch/kernel/acpi.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/memblock.h>
16 #include <linux/of_fdt.h>
17 #include <linux/serial_core.h>
18 #include <asm/io.h>
19 #include <asm/numa.h>
20 #include <asm/loongson.h>
21 
22 int acpi_disabled;
23 EXPORT_SYMBOL(acpi_disabled);
24 int acpi_noirq;
25 int acpi_pci_disabled;
26 EXPORT_SYMBOL(acpi_pci_disabled);
27 int acpi_strict = 1; /* We have no workarounds on LoongArch */
28 int num_processors;
29 int disabled_cpus;
30 
31 u64 acpi_saved_sp;
32 
33 #define PREFIX			"ACPI: "
34 
35 struct acpi_madt_core_pic acpi_core_pic[MAX_CORE_PIC];
36 
37 void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size)
38 {
39 
40 	if (!phys || !size)
41 		return NULL;
42 
43 	return early_memremap(phys, size);
44 }
45 void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
46 {
47 	if (!map || !size)
48 		return;
49 
50 	early_memunmap(map, size);
51 }
52 
53 void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
54 {
55 	if (!memblock_is_memory(phys))
56 		return ioremap(phys, size);
57 	else
58 		return ioremap_cache(phys, size);
59 }
60 
61 static int cpu_enumerated = 0;
62 
63 #ifdef CONFIG_SMP
64 static int set_processor_mask(u32 id, u32 flags)
65 {
66 	int nr_cpus;
67 	int cpu, cpuid = id;
68 
69 	if (!cpu_enumerated)
70 		nr_cpus = NR_CPUS;
71 	else
72 		nr_cpus = nr_cpu_ids;
73 
74 	if (num_processors >= nr_cpus) {
75 		pr_warn(PREFIX "nr_cpus limit of %i reached."
76 			" processor 0x%x ignored.\n", nr_cpus, cpuid);
77 
78 		return -ENODEV;
79 
80 	}
81 	if (cpuid == loongson_sysconf.boot_cpu_id)
82 		cpu = 0;
83 	else
84 		cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
85 
86 	if (!cpu_enumerated)
87 		set_cpu_possible(cpu, true);
88 
89 	if (flags & ACPI_MADT_ENABLED) {
90 		num_processors++;
91 		set_cpu_present(cpu, true);
92 		__cpu_number_map[cpuid] = cpu;
93 		__cpu_logical_map[cpu] = cpuid;
94 	} else
95 		disabled_cpus++;
96 
97 	return cpu;
98 }
99 #endif
100 
101 static int __init
102 acpi_parse_processor(union acpi_subtable_headers *header, const unsigned long end)
103 {
104 	struct acpi_madt_core_pic *processor = NULL;
105 
106 	processor = (struct acpi_madt_core_pic *)header;
107 	if (BAD_MADT_ENTRY(processor, end))
108 		return -EINVAL;
109 
110 	acpi_table_print_madt_entry(&header->common);
111 #ifdef CONFIG_SMP
112 	acpi_core_pic[processor->core_id] = *processor;
113 	set_processor_mask(processor->core_id, processor->flags);
114 #endif
115 
116 	return 0;
117 }
118 
119 static int __init
120 acpi_parse_eio_master(union acpi_subtable_headers *header, const unsigned long end)
121 {
122 	static int core = 0;
123 	struct acpi_madt_eio_pic *eiointc = NULL;
124 
125 	eiointc = (struct acpi_madt_eio_pic *)header;
126 	if (BAD_MADT_ENTRY(eiointc, end))
127 		return -EINVAL;
128 
129 	core = eiointc->node * CORES_PER_EIO_NODE;
130 	set_bit(core, loongson_sysconf.cores_io_master);
131 
132 	return 0;
133 }
134 
135 static void __init acpi_process_madt(void)
136 {
137 #ifdef CONFIG_SMP
138 	int i;
139 
140 	for (i = 0; i < NR_CPUS; i++) {
141 		__cpu_number_map[i] = -1;
142 		__cpu_logical_map[i] = -1;
143 	}
144 #endif
145 	acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC,
146 			acpi_parse_processor, MAX_CORE_PIC);
147 
148 	acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC,
149 			acpi_parse_eio_master, MAX_IO_PICS);
150 
151 	cpu_enumerated = 1;
152 	loongson_sysconf.nr_cpus = num_processors;
153 }
154 
155 int pptt_enabled;
156 
157 int __init parse_acpi_topology(void)
158 {
159 	int cpu, topology_id;
160 
161 	for_each_possible_cpu(cpu) {
162 		topology_id = find_acpi_cpu_topology(cpu, 0);
163 		if (topology_id < 0) {
164 			pr_warn("Invalid BIOS PPTT\n");
165 			return -ENOENT;
166 		}
167 
168 		if (acpi_pptt_cpu_is_thread(cpu) <= 0)
169 			cpu_data[cpu].core = topology_id;
170 		else {
171 			topology_id = find_acpi_cpu_topology(cpu, 1);
172 			if (topology_id < 0)
173 				return -ENOENT;
174 
175 			cpu_data[cpu].core = topology_id;
176 		}
177 	}
178 
179 	pptt_enabled = 1;
180 
181 	return 0;
182 }
183 
184 #ifndef CONFIG_SUSPEND
185 int (*acpi_suspend_lowlevel)(void);
186 #else
187 int (*acpi_suspend_lowlevel)(void) = loongarch_acpi_suspend;
188 #endif
189 
190 void __init acpi_boot_table_init(void)
191 {
192 	/*
193 	 * If acpi_disabled, bail out
194 	 */
195 	if (acpi_disabled)
196 		goto fdt_earlycon;
197 
198 	/*
199 	 * Initialize the ACPI boot-time table parser.
200 	 */
201 	if (acpi_table_init()) {
202 		disable_acpi();
203 		goto fdt_earlycon;
204 	}
205 
206 	loongson_sysconf.boot_cpu_id = read_csr_cpuid();
207 
208 	/*
209 	 * Process the Multiple APIC Description Table (MADT), if present
210 	 */
211 	acpi_process_madt();
212 
213 	/* Do not enable ACPI SPCR console by default */
214 	acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
215 
216 	if (IS_ENABLED(CONFIG_ACPI_BGRT))
217 		acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);
218 
219 	return;
220 
221 fdt_earlycon:
222 	if (earlycon_acpi_spcr_enable)
223 		early_init_dt_scan_chosen_stdout();
224 }
225 
226 #ifdef CONFIG_ACPI_NUMA
227 
228 static __init int setup_node(int pxm)
229 {
230 	return acpi_map_pxm_to_node(pxm);
231 }
232 
233 /*
234  * Callback for SLIT parsing.  pxm_to_node() returns NUMA_NO_NODE for
235  * I/O localities since SRAT does not list them.  I/O localities are
236  * not supported at this point.
237  */
238 unsigned int numa_distance_cnt;
239 
240 static inline unsigned int get_numa_distances_cnt(struct acpi_table_slit *slit)
241 {
242 	return slit->locality_count;
243 }
244 
245 void __init numa_set_distance(int from, int to, int distance)
246 {
247 	if ((u8)distance != distance || (from == to && distance != LOCAL_DISTANCE)) {
248 		pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
249 				from, to, distance);
250 		return;
251 	}
252 
253 	node_distances[from][to] = distance;
254 }
255 
256 /* Callback for Proximity Domain -> CPUID mapping */
257 void __init
258 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
259 {
260 	int pxm, node;
261 
262 	if (srat_disabled())
263 		return;
264 	if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
265 		bad_srat();
266 		return;
267 	}
268 	if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
269 		return;
270 	pxm = pa->proximity_domain_lo;
271 	if (acpi_srat_revision >= 2) {
272 		pxm |= (pa->proximity_domain_hi[0] << 8);
273 		pxm |= (pa->proximity_domain_hi[1] << 16);
274 		pxm |= (pa->proximity_domain_hi[2] << 24);
275 	}
276 	node = setup_node(pxm);
277 	if (node < 0) {
278 		pr_err("SRAT: Too many proximity domains %x\n", pxm);
279 		bad_srat();
280 		return;
281 	}
282 
283 	if (pa->apic_id >= CONFIG_NR_CPUS) {
284 		pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
285 				pxm, pa->apic_id, node);
286 		return;
287 	}
288 
289 	early_numa_add_cpu(pa->apic_id, node);
290 
291 	set_cpuid_to_node(pa->apic_id, node);
292 	node_set(node, numa_nodes_parsed);
293 	pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
294 }
295 
296 #endif
297 
298 void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
299 {
300 	memblock_reserve(addr, size);
301 }
302 
303 #ifdef CONFIG_ACPI_HOTPLUG_CPU
304 
305 #include <acpi/processor.h>
306 
307 static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid)
308 {
309 #ifdef CONFIG_ACPI_NUMA
310 	int nid;
311 
312 	nid = acpi_get_node(handle);
313 	if (nid != NUMA_NO_NODE) {
314 		set_cpuid_to_node(physid, nid);
315 		node_set(nid, numa_nodes_parsed);
316 		set_cpu_numa_node(cpu, nid);
317 		cpumask_set_cpu(cpu, cpumask_of_node(nid));
318 	}
319 #endif
320 	return 0;
321 }
322 
323 int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu)
324 {
325 	int cpu;
326 
327 	cpu = set_processor_mask(physid, ACPI_MADT_ENABLED);
328 	if (cpu < 0) {
329 		pr_info(PREFIX "Unable to map lapic to logical cpu number\n");
330 		return cpu;
331 	}
332 
333 	acpi_map_cpu2node(handle, cpu, physid);
334 
335 	*pcpu = cpu;
336 
337 	return 0;
338 }
339 EXPORT_SYMBOL(acpi_map_cpu);
340 
341 int acpi_unmap_cpu(int cpu)
342 {
343 #ifdef CONFIG_ACPI_NUMA
344 	set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE);
345 #endif
346 	set_cpu_present(cpu, false);
347 	num_processors--;
348 
349 	pr_info("cpu%d hot remove!\n", cpu);
350 
351 	return 0;
352 }
353 EXPORT_SYMBOL(acpi_unmap_cpu);
354 
355 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
356