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