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