xref: /linux/arch/riscv/kernel/acpi.c (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  RISC-V Specific Low-Level ACPI Boot Support
4  *
5  *  Copyright (C) 2013-2014, Linaro Ltd.
6  *	Author: Al Stone <al.stone@linaro.org>
7  *	Author: Graeme Gregory <graeme.gregory@linaro.org>
8  *	Author: Hanjun Guo <hanjun.guo@linaro.org>
9  *	Author: Tomasz Nowicki <tomasz.nowicki@linaro.org>
10  *	Author: Naresh Bhat <naresh.bhat@linaro.org>
11  *
12  *  Copyright (C) 2021-2023, Ventana Micro Systems Inc.
13  *	Author: Sunil V L <sunilvl@ventanamicro.com>
14  */
15 
16 #include <linux/acpi.h>
17 #include <linux/efi.h>
18 #include <linux/io.h>
19 #include <linux/memblock.h>
20 #include <linux/of_fdt.h>
21 #include <linux/pci.h>
22 #include <linux/serial_core.h>
23 
24 int acpi_noirq = 1;		/* skip ACPI IRQ initialization */
25 int acpi_disabled = 1;
26 EXPORT_SYMBOL(acpi_disabled);
27 
28 int acpi_pci_disabled = 1;	/* skip ACPI PCI scan and IRQ initialization */
29 EXPORT_SYMBOL(acpi_pci_disabled);
30 
31 static bool param_acpi_off __initdata;
32 static bool param_acpi_on __initdata;
33 static bool param_acpi_force __initdata;
34 
35 static struct acpi_madt_rintc cpu_madt_rintc[NR_CPUS];
36 
37 static int __init parse_acpi(char *arg)
38 {
39 	if (!arg)
40 		return -EINVAL;
41 
42 	/* "acpi=off" disables both ACPI table parsing and interpreter */
43 	if (strcmp(arg, "off") == 0)
44 		param_acpi_off = true;
45 	else if (strcmp(arg, "on") == 0) /* prefer ACPI over DT */
46 		param_acpi_on = true;
47 	else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */
48 		param_acpi_force = true;
49 	else
50 		return -EINVAL;	/* Core will print when we return error */
51 
52 	return 0;
53 }
54 early_param("acpi", parse_acpi);
55 
56 /*
57  * acpi_fadt_sanity_check() - Check FADT presence and carry out sanity
58  *			      checks on it
59  *
60  * Return 0 on success,  <0 on failure
61  */
62 static int __init acpi_fadt_sanity_check(void)
63 {
64 	struct acpi_table_header *table;
65 	struct acpi_table_fadt *fadt;
66 	acpi_status status;
67 	int ret = 0;
68 
69 	/*
70 	 * FADT is required on riscv; retrieve it to check its presence
71 	 * and carry out revision and ACPI HW reduced compliancy tests
72 	 */
73 	status = acpi_get_table(ACPI_SIG_FADT, 0, &table);
74 	if (ACPI_FAILURE(status)) {
75 		const char *msg = acpi_format_exception(status);
76 
77 		pr_err("Failed to get FADT table, %s\n", msg);
78 		return -ENODEV;
79 	}
80 
81 	fadt = (struct acpi_table_fadt *)table;
82 
83 	/*
84 	 * The revision in the table header is the FADT's Major revision. The
85 	 * FADT also has a minor revision, which is stored in the FADT itself.
86 	 *
87 	 * TODO: Currently, we check for 6.5 as the minimum version to check
88 	 * for HW_REDUCED flag. However, once RISC-V updates are released in
89 	 * the ACPI spec, we need to update this check for exact minor revision
90 	 */
91 	if (table->revision < 6 || (table->revision == 6 && fadt->minor_revision < 5))
92 		pr_err(FW_BUG "Unsupported FADT revision %d.%d, should be 6.5+\n",
93 		       table->revision, fadt->minor_revision);
94 
95 	if (!(fadt->flags & ACPI_FADT_HW_REDUCED)) {
96 		pr_err("FADT not ACPI hardware reduced compliant\n");
97 		ret = -EINVAL;
98 	}
99 
100 	/*
101 	 * acpi_get_table() creates FADT table mapping that
102 	 * should be released after parsing and before resuming boot
103 	 */
104 	acpi_put_table(table);
105 	return ret;
106 }
107 
108 /*
109  * acpi_boot_table_init() called from setup_arch(), always.
110  *	1. find RSDP and get its address, and then find XSDT
111  *	2. extract all tables and checksums them all
112  *	3. check ACPI FADT HW reduced flag
113  *
114  * We can parse ACPI boot-time tables such as MADT after
115  * this function is called.
116  *
117  * On return ACPI is enabled if either:
118  *
119  * - ACPI tables are initialized and sanity checks passed
120  * - acpi=force was passed in the command line and ACPI was not disabled
121  *   explicitly through acpi=off command line parameter
122  *
123  * ACPI is disabled on function return otherwise
124  */
125 void __init acpi_boot_table_init(void)
126 {
127 	/*
128 	 * Enable ACPI instead of device tree unless
129 	 * - ACPI has been disabled explicitly (acpi=off), or
130 	 * - firmware has not populated ACPI ptr in EFI system table
131 	 *   and ACPI has not been [force] enabled (acpi=on|force)
132 	 */
133 	if (param_acpi_off ||
134 	    (!param_acpi_on && !param_acpi_force &&
135 	     efi.acpi20 == EFI_INVALID_TABLE_ADDR))
136 		goto done;
137 
138 	/*
139 	 * ACPI is disabled at this point. Enable it in order to parse
140 	 * the ACPI tables and carry out sanity checks
141 	 */
142 	enable_acpi();
143 
144 	/*
145 	 * If ACPI tables are initialized and FADT sanity checks passed,
146 	 * leave ACPI enabled and carry on booting; otherwise disable ACPI
147 	 * on initialization error.
148 	 * If acpi=force was passed on the command line it forces ACPI
149 	 * to be enabled even if its initialization failed.
150 	 */
151 	if (acpi_table_init() || acpi_fadt_sanity_check()) {
152 		pr_err("Failed to init ACPI tables\n");
153 		if (!param_acpi_force)
154 			disable_acpi();
155 	}
156 
157 done:
158 	if (acpi_disabled) {
159 		if (earlycon_acpi_spcr_enable)
160 			early_init_dt_scan_chosen_stdout();
161 	} else {
162 		acpi_parse_spcr(earlycon_acpi_spcr_enable, true);
163 	}
164 }
165 
166 static int acpi_parse_madt_rintc(union acpi_subtable_headers *header, const unsigned long end)
167 {
168 	struct acpi_madt_rintc *rintc = (struct acpi_madt_rintc *)header;
169 	int cpuid;
170 
171 	if (!(rintc->flags & ACPI_MADT_ENABLED))
172 		return 0;
173 
174 	cpuid = riscv_hartid_to_cpuid(rintc->hart_id);
175 	/*
176 	 * When CONFIG_SMP is disabled, mapping won't be created for
177 	 * all cpus.
178 	 * CPUs more than num_possible_cpus, will be ignored.
179 	 */
180 	if (cpuid >= 0 && cpuid < num_possible_cpus())
181 		cpu_madt_rintc[cpuid] = *rintc;
182 
183 	return 0;
184 }
185 
186 /*
187  * Instead of parsing (and freeing) the ACPI table, cache
188  * the RINTC structures since they are frequently used
189  * like in  cpuinfo.
190  */
191 void __init acpi_init_rintc_map(void)
192 {
193 	if (acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, acpi_parse_madt_rintc, 0) <= 0) {
194 		pr_err("No valid RINTC entries exist\n");
195 		BUG();
196 	}
197 }
198 
199 struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu)
200 {
201 	return &cpu_madt_rintc[cpu];
202 }
203 
204 /*
205  * __acpi_map_table() will be called before paging_init(), so early_ioremap()
206  * or early_memremap() should be called here to for ACPI table mapping.
207  */
208 void __init __iomem *__acpi_map_table(unsigned long phys, unsigned long size)
209 {
210 	if (!size)
211 		return NULL;
212 
213 	return early_ioremap(phys, size);
214 }
215 
216 void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
217 {
218 	if (!map || !size)
219 		return;
220 
221 	early_iounmap(map, size);
222 }
223 
224 void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
225 {
226 	efi_memory_desc_t *md, *region = NULL;
227 	pgprot_t prot;
228 
229 	if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP)))
230 		return NULL;
231 
232 	for_each_efi_memory_desc(md) {
233 		u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
234 
235 		if (phys < md->phys_addr || phys >= end)
236 			continue;
237 
238 		if (phys + size > end) {
239 			pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n");
240 			return NULL;
241 		}
242 		region = md;
243 		break;
244 	}
245 
246 	/*
247 	 * It is fine for AML to remap regions that are not represented in the
248 	 * EFI memory map at all, as it only describes normal memory, and MMIO
249 	 * regions that require a virtual mapping to make them accessible to
250 	 * the EFI runtime services.
251 	 */
252 	prot = PAGE_KERNEL_IO;
253 	if (region) {
254 		switch (region->type) {
255 		case EFI_LOADER_CODE:
256 		case EFI_LOADER_DATA:
257 		case EFI_BOOT_SERVICES_CODE:
258 		case EFI_BOOT_SERVICES_DATA:
259 		case EFI_CONVENTIONAL_MEMORY:
260 		case EFI_PERSISTENT_MEMORY:
261 			if (memblock_is_map_memory(phys) ||
262 			    !memblock_is_region_memory(phys, size)) {
263 				pr_warn(FW_BUG "requested region covers kernel memory\n");
264 				return NULL;
265 			}
266 
267 			/*
268 			 * Mapping kernel memory is permitted if the region in
269 			 * question is covered by a single memblock with the
270 			 * NOMAP attribute set: this enables the use of ACPI
271 			 * table overrides passed via initramfs.
272 			 * This particular use case only requires read access.
273 			 */
274 			fallthrough;
275 
276 		case EFI_RUNTIME_SERVICES_CODE:
277 			/*
278 			 * This would be unusual, but not problematic per se,
279 			 * as long as we take care not to create a writable
280 			 * mapping for executable code.
281 			 */
282 			prot = PAGE_KERNEL_RO;
283 			break;
284 
285 		case EFI_ACPI_RECLAIM_MEMORY:
286 			/*
287 			 * ACPI reclaim memory is used to pass firmware tables
288 			 * and other data that is intended for consumption by
289 			 * the OS only, which may decide it wants to reclaim
290 			 * that memory and use it for something else. We never
291 			 * do that, but we usually add it to the linear map
292 			 * anyway, in which case we should use the existing
293 			 * mapping.
294 			 */
295 			if (memblock_is_map_memory(phys))
296 				return (void __iomem *)__va(phys);
297 			fallthrough;
298 
299 		default:
300 			if (region->attribute & EFI_MEMORY_WB)
301 				prot = PAGE_KERNEL;
302 			else if ((region->attribute & EFI_MEMORY_WC) ||
303 				 (region->attribute & EFI_MEMORY_WT))
304 				prot = pgprot_writecombine(PAGE_KERNEL);
305 		}
306 	}
307 
308 	return ioremap_prot(phys, size, pgprot_val(prot));
309 }
310 
311 #ifdef CONFIG_PCI
312 
313 /*
314  * These interfaces are defined just to enable building ACPI core.
315  * TODO: Update it with actual implementation when external interrupt
316  * controller support is added in RISC-V ACPI.
317  */
318 int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
319 		 int reg, int len, u32 *val)
320 {
321 	return PCIBIOS_DEVICE_NOT_FOUND;
322 }
323 
324 int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
325 		  int reg, int len, u32 val)
326 {
327 	return PCIBIOS_DEVICE_NOT_FOUND;
328 }
329 
330 int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
331 {
332 	return -1;
333 }
334 
335 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
336 {
337 	return NULL;
338 }
339 #endif	/* CONFIG_PCI */
340