xref: /linux/arch/x86/boot/compressed/acpi.c (revision fce96cf0443083e37455eff8f78fd240c621dae3)
1 // SPDX-License-Identifier: GPL-2.0
2 #define BOOT_CTYPE_H
3 #include "misc.h"
4 #include "error.h"
5 #include "../string.h"
6 
7 #include <linux/numa.h>
8 #include <linux/efi.h>
9 #include <asm/efi.h>
10 
11 /*
12  * Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0'
13  * for termination.
14  */
15 #define MAX_ACPI_ARG_LENGTH 10
16 
17 /*
18  * Immovable memory regions representation. Max amount of memory regions is
19  * MAX_NUMNODES*2.
20  */
21 struct mem_vector immovable_mem[MAX_NUMNODES*2];
22 
23 static acpi_physical_address
24 __efi_get_rsdp_addr(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len)
25 {
26 #ifdef CONFIG_EFI
27 	unsigned long rsdp_addr;
28 	int ret;
29 
30 	/*
31 	 * Search EFI system tables for RSDP. Preferred is ACPI_20_TABLE_GUID to
32 	 * ACPI_TABLE_GUID because it has more features.
33 	 */
34 	rsdp_addr = efi_find_vendor_table(boot_params, cfg_tbl_pa, cfg_tbl_len,
35 					  ACPI_20_TABLE_GUID);
36 	if (rsdp_addr)
37 		return (acpi_physical_address)rsdp_addr;
38 
39 	/* No ACPI_20_TABLE_GUID found, fallback to ACPI_TABLE_GUID. */
40 	rsdp_addr = efi_find_vendor_table(boot_params, cfg_tbl_pa, cfg_tbl_len,
41 					  ACPI_TABLE_GUID);
42 	if (rsdp_addr)
43 		return (acpi_physical_address)rsdp_addr;
44 
45 	debug_putstr("Error getting RSDP address.\n");
46 #endif
47 	return 0;
48 }
49 
50 static acpi_physical_address efi_get_rsdp_addr(void)
51 {
52 #ifdef CONFIG_EFI
53 	unsigned long cfg_tbl_pa = 0;
54 	unsigned int cfg_tbl_len;
55 	unsigned long systab_pa;
56 	unsigned int nr_tables;
57 	enum efi_type et;
58 	int ret;
59 
60 	et = efi_get_type(boot_params);
61 	if (et == EFI_TYPE_NONE)
62 		return 0;
63 
64 	systab_pa = efi_get_system_table(boot_params);
65 	if (!systab_pa)
66 		error("EFI support advertised, but unable to locate system table.");
67 
68 	ret = efi_get_conf_table(boot_params, &cfg_tbl_pa, &cfg_tbl_len);
69 	if (ret || !cfg_tbl_pa)
70 		error("EFI config table not found.");
71 
72 	return __efi_get_rsdp_addr(cfg_tbl_pa, cfg_tbl_len);
73 #else
74 	return 0;
75 #endif
76 }
77 
78 static u8 compute_checksum(u8 *buffer, u32 length)
79 {
80 	u8 *end = buffer + length;
81 	u8 sum = 0;
82 
83 	while (buffer < end)
84 		sum += *(buffer++);
85 
86 	return sum;
87 }
88 
89 /* Search a block of memory for the RSDP signature. */
90 static u8 *scan_mem_for_rsdp(u8 *start, u32 length)
91 {
92 	struct acpi_table_rsdp *rsdp;
93 	u8 *address, *end;
94 
95 	end = start + length;
96 
97 	/* Search from given start address for the requested length */
98 	for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) {
99 		/*
100 		 * Both RSDP signature and checksum must be correct.
101 		 * Note: Sometimes there exists more than one RSDP in memory;
102 		 * the valid RSDP has a valid checksum, all others have an
103 		 * invalid checksum.
104 		 */
105 		rsdp = (struct acpi_table_rsdp *)address;
106 
107 		/* BAD Signature */
108 		if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature))
109 			continue;
110 
111 		/* Check the standard checksum */
112 		if (compute_checksum((u8 *)rsdp, ACPI_RSDP_CHECKSUM_LENGTH))
113 			continue;
114 
115 		/* Check extended checksum if table version >= 2 */
116 		if ((rsdp->revision >= 2) &&
117 		    (compute_checksum((u8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)))
118 			continue;
119 
120 		/* Signature and checksum valid, we have found a real RSDP */
121 		return address;
122 	}
123 	return NULL;
124 }
125 
126 /* Search RSDP address in EBDA. */
127 static acpi_physical_address bios_get_rsdp_addr(void)
128 {
129 	unsigned long address;
130 	u8 *rsdp;
131 
132 	/* Get the location of the Extended BIOS Data Area (EBDA) */
133 	address = *(u16 *)ACPI_EBDA_PTR_LOCATION;
134 	address <<= 4;
135 
136 	/*
137 	 * Search EBDA paragraphs (EBDA is required to be a minimum of
138 	 * 1K length)
139 	 */
140 	if (address > 0x400) {
141 		rsdp = scan_mem_for_rsdp((u8 *)address, ACPI_EBDA_WINDOW_SIZE);
142 		if (rsdp)
143 			return (acpi_physical_address)(unsigned long)rsdp;
144 	}
145 
146 	/* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */
147 	rsdp = scan_mem_for_rsdp((u8 *) ACPI_HI_RSDP_WINDOW_BASE,
148 					ACPI_HI_RSDP_WINDOW_SIZE);
149 	if (rsdp)
150 		return (acpi_physical_address)(unsigned long)rsdp;
151 
152 	return 0;
153 }
154 
155 /* Return RSDP address on success, otherwise 0. */
156 acpi_physical_address get_rsdp_addr(void)
157 {
158 	acpi_physical_address pa;
159 
160 	pa = boot_params->acpi_rsdp_addr;
161 
162 	if (!pa)
163 		pa = efi_get_rsdp_addr();
164 
165 	if (!pa)
166 		pa = bios_get_rsdp_addr();
167 
168 	return pa;
169 }
170 
171 #if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE)
172 /*
173  * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex
174  * digits, and '\0' for termination.
175  */
176 #define MAX_ADDR_LEN 19
177 
178 static unsigned long get_cmdline_acpi_rsdp(void)
179 {
180 	unsigned long addr = 0;
181 
182 #ifdef CONFIG_KEXEC
183 	char val[MAX_ADDR_LEN] = { };
184 	int ret;
185 
186 	ret = cmdline_find_option("acpi_rsdp", val, MAX_ADDR_LEN);
187 	if (ret < 0)
188 		return 0;
189 
190 	if (boot_kstrtoul(val, 16, &addr))
191 		return 0;
192 #endif
193 	return addr;
194 }
195 
196 /* Compute SRAT address from RSDP. */
197 static unsigned long get_acpi_srat_table(void)
198 {
199 	unsigned long root_table, acpi_table;
200 	struct acpi_table_header *header;
201 	struct acpi_table_rsdp *rsdp;
202 	u32 num_entries, size, len;
203 	char arg[10];
204 	u8 *entry;
205 
206 	/*
207 	 * Check whether we were given an RSDP on the command line. We don't
208 	 * stash this in boot params because the kernel itself may have
209 	 * different ideas about whether to trust a command-line parameter.
210 	 */
211 	rsdp = (struct acpi_table_rsdp *)get_cmdline_acpi_rsdp();
212 	if (!rsdp)
213 		rsdp = (struct acpi_table_rsdp *)(long)
214 			boot_params->acpi_rsdp_addr;
215 
216 	if (!rsdp)
217 		return 0;
218 
219 	/* Get ACPI root table from RSDP.*/
220 	if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 &&
221 	    !strncmp(arg, "rsdt", 4)) &&
222 	    rsdp->xsdt_physical_address &&
223 	    rsdp->revision > 1) {
224 		root_table = rsdp->xsdt_physical_address;
225 		size = ACPI_XSDT_ENTRY_SIZE;
226 	} else {
227 		root_table = rsdp->rsdt_physical_address;
228 		size = ACPI_RSDT_ENTRY_SIZE;
229 	}
230 
231 	if (!root_table)
232 		return 0;
233 
234 	header = (struct acpi_table_header *)root_table;
235 	len = header->length;
236 	if (len < sizeof(struct acpi_table_header) + size)
237 		return 0;
238 
239 	num_entries = (len - sizeof(struct acpi_table_header)) / size;
240 	entry = (u8 *)(root_table + sizeof(struct acpi_table_header));
241 
242 	while (num_entries--) {
243 		if (size == ACPI_RSDT_ENTRY_SIZE)
244 			acpi_table = *(u32 *)entry;
245 		else
246 			acpi_table = *(u64 *)entry;
247 
248 		if (acpi_table) {
249 			header = (struct acpi_table_header *)acpi_table;
250 
251 			if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT))
252 				return acpi_table;
253 		}
254 		entry += size;
255 	}
256 	return 0;
257 }
258 
259 /**
260  * count_immovable_mem_regions - Parse SRAT and cache the immovable
261  * memory regions into the immovable_mem array.
262  *
263  * Return the number of immovable memory regions on success, 0 on failure:
264  *
265  * - Too many immovable memory regions
266  * - ACPI off or no SRAT found
267  * - No immovable memory region found.
268  */
269 int count_immovable_mem_regions(void)
270 {
271 	unsigned long table_addr, table_end, table;
272 	struct acpi_subtable_header *sub_table;
273 	struct acpi_table_header *table_header;
274 	char arg[MAX_ACPI_ARG_LENGTH];
275 	int num = 0;
276 
277 	if (cmdline_find_option("acpi", arg, sizeof(arg)) == 3 &&
278 	    !strncmp(arg, "off", 3))
279 		return 0;
280 
281 	table_addr = get_acpi_srat_table();
282 	if (!table_addr)
283 		return 0;
284 
285 	table_header = (struct acpi_table_header *)table_addr;
286 	table_end = table_addr + table_header->length;
287 	table = table_addr + sizeof(struct acpi_table_srat);
288 
289 	while (table + sizeof(struct acpi_subtable_header) < table_end) {
290 
291 		sub_table = (struct acpi_subtable_header *)table;
292 		if (!sub_table->length) {
293 			debug_putstr("Invalid zero length SRAT subtable.\n");
294 			return 0;
295 		}
296 
297 		if (sub_table->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) {
298 			struct acpi_srat_mem_affinity *ma;
299 
300 			ma = (struct acpi_srat_mem_affinity *)sub_table;
301 			if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && ma->length) {
302 				immovable_mem[num].start = ma->base_address;
303 				immovable_mem[num].size = ma->length;
304 				num++;
305 			}
306 
307 			if (num >= MAX_NUMNODES*2) {
308 				debug_putstr("Too many immovable memory regions, aborting.\n");
309 				return 0;
310 			}
311 		}
312 		table += sub_table->length;
313 	}
314 	return num;
315 }
316 #endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */
317