xref: /linux/arch/x86/boot/compressed/acpi.c (revision b8d312aa075f33282565467662c4628dae0a2aff)
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 /*
24  * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex
25  * digits, and '\0' for termination.
26  */
27 #define MAX_ADDR_LEN 19
28 
29 static acpi_physical_address get_acpi_rsdp(void)
30 {
31 	acpi_physical_address addr = 0;
32 
33 #ifdef CONFIG_KEXEC
34 	char val[MAX_ADDR_LEN] = { };
35 	int ret;
36 
37 	ret = cmdline_find_option("acpi_rsdp", val, MAX_ADDR_LEN);
38 	if (ret < 0)
39 		return 0;
40 
41 	if (kstrtoull(val, 16, &addr))
42 		return 0;
43 #endif
44 	return addr;
45 }
46 
47 /*
48  * Search EFI system tables for RSDP.  If both ACPI_20_TABLE_GUID and
49  * ACPI_TABLE_GUID are found, take the former, which has more features.
50  */
51 static acpi_physical_address
52 __efi_get_rsdp_addr(unsigned long config_tables, unsigned int nr_tables,
53 		    bool efi_64)
54 {
55 	acpi_physical_address rsdp_addr = 0;
56 
57 #ifdef CONFIG_EFI
58 	int i;
59 
60 	/* Get EFI tables from systab. */
61 	for (i = 0; i < nr_tables; i++) {
62 		acpi_physical_address table;
63 		efi_guid_t guid;
64 
65 		if (efi_64) {
66 			efi_config_table_64_t *tbl = (efi_config_table_64_t *)config_tables + i;
67 
68 			guid  = tbl->guid;
69 			table = tbl->table;
70 
71 			if (!IS_ENABLED(CONFIG_X86_64) && table >> 32) {
72 				debug_putstr("Error getting RSDP address: EFI config table located above 4GB.\n");
73 				return 0;
74 			}
75 		} else {
76 			efi_config_table_32_t *tbl = (efi_config_table_32_t *)config_tables + i;
77 
78 			guid  = tbl->guid;
79 			table = tbl->table;
80 		}
81 
82 		if (!(efi_guidcmp(guid, ACPI_TABLE_GUID)))
83 			rsdp_addr = table;
84 		else if (!(efi_guidcmp(guid, ACPI_20_TABLE_GUID)))
85 			return table;
86 	}
87 #endif
88 	return rsdp_addr;
89 }
90 
91 /* EFI/kexec support is 64-bit only. */
92 #ifdef CONFIG_X86_64
93 static struct efi_setup_data *get_kexec_setup_data_addr(void)
94 {
95 	struct setup_data *data;
96 	u64 pa_data;
97 
98 	pa_data = boot_params->hdr.setup_data;
99 	while (pa_data) {
100 		data = (struct setup_data *)pa_data;
101 		if (data->type == SETUP_EFI)
102 			return (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
103 
104 		pa_data = data->next;
105 	}
106 	return NULL;
107 }
108 
109 static acpi_physical_address kexec_get_rsdp_addr(void)
110 {
111 	efi_system_table_64_t *systab;
112 	struct efi_setup_data *esd;
113 	struct efi_info *ei;
114 	char *sig;
115 
116 	esd = (struct efi_setup_data *)get_kexec_setup_data_addr();
117 	if (!esd)
118 		return 0;
119 
120 	if (!esd->tables) {
121 		debug_putstr("Wrong kexec SETUP_EFI data.\n");
122 		return 0;
123 	}
124 
125 	ei = &boot_params->efi_info;
126 	sig = (char *)&ei->efi_loader_signature;
127 	if (strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
128 		debug_putstr("Wrong kexec EFI loader signature.\n");
129 		return 0;
130 	}
131 
132 	/* Get systab from boot params. */
133 	systab = (efi_system_table_64_t *) (ei->efi_systab | ((__u64)ei->efi_systab_hi << 32));
134 	if (!systab)
135 		error("EFI system table not found in kexec boot_params.");
136 
137 	return __efi_get_rsdp_addr((unsigned long)esd->tables, systab->nr_tables, true);
138 }
139 #else
140 static acpi_physical_address kexec_get_rsdp_addr(void) { return 0; }
141 #endif /* CONFIG_X86_64 */
142 
143 static acpi_physical_address efi_get_rsdp_addr(void)
144 {
145 #ifdef CONFIG_EFI
146 	unsigned long systab, config_tables;
147 	unsigned int nr_tables;
148 	struct efi_info *ei;
149 	bool efi_64;
150 	char *sig;
151 
152 	ei = &boot_params->efi_info;
153 	sig = (char *)&ei->efi_loader_signature;
154 
155 	if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
156 		efi_64 = true;
157 	} else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) {
158 		efi_64 = false;
159 	} else {
160 		debug_putstr("Wrong EFI loader signature.\n");
161 		return 0;
162 	}
163 
164 	/* Get systab from boot params. */
165 #ifdef CONFIG_X86_64
166 	systab = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32);
167 #else
168 	if (ei->efi_systab_hi || ei->efi_memmap_hi) {
169 		debug_putstr("Error getting RSDP address: EFI system table located above 4GB.\n");
170 		return 0;
171 	}
172 	systab = ei->efi_systab;
173 #endif
174 	if (!systab)
175 		error("EFI system table not found.");
176 
177 	/* Handle EFI bitness properly */
178 	if (efi_64) {
179 		efi_system_table_64_t *stbl = (efi_system_table_64_t *)systab;
180 
181 		config_tables	= stbl->tables;
182 		nr_tables	= stbl->nr_tables;
183 	} else {
184 		efi_system_table_32_t *stbl = (efi_system_table_32_t *)systab;
185 
186 		config_tables	= stbl->tables;
187 		nr_tables	= stbl->nr_tables;
188 	}
189 
190 	if (!config_tables)
191 		error("EFI config tables not found.");
192 
193 	return __efi_get_rsdp_addr(config_tables, nr_tables, efi_64);
194 #else
195 	return 0;
196 #endif
197 }
198 
199 static u8 compute_checksum(u8 *buffer, u32 length)
200 {
201 	u8 *end = buffer + length;
202 	u8 sum = 0;
203 
204 	while (buffer < end)
205 		sum += *(buffer++);
206 
207 	return sum;
208 }
209 
210 /* Search a block of memory for the RSDP signature. */
211 static u8 *scan_mem_for_rsdp(u8 *start, u32 length)
212 {
213 	struct acpi_table_rsdp *rsdp;
214 	u8 *address, *end;
215 
216 	end = start + length;
217 
218 	/* Search from given start address for the requested length */
219 	for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) {
220 		/*
221 		 * Both RSDP signature and checksum must be correct.
222 		 * Note: Sometimes there exists more than one RSDP in memory;
223 		 * the valid RSDP has a valid checksum, all others have an
224 		 * invalid checksum.
225 		 */
226 		rsdp = (struct acpi_table_rsdp *)address;
227 
228 		/* BAD Signature */
229 		if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature))
230 			continue;
231 
232 		/* Check the standard checksum */
233 		if (compute_checksum((u8 *)rsdp, ACPI_RSDP_CHECKSUM_LENGTH))
234 			continue;
235 
236 		/* Check extended checksum if table version >= 2 */
237 		if ((rsdp->revision >= 2) &&
238 		    (compute_checksum((u8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)))
239 			continue;
240 
241 		/* Signature and checksum valid, we have found a real RSDP */
242 		return address;
243 	}
244 	return NULL;
245 }
246 
247 /* Search RSDP address in EBDA. */
248 static acpi_physical_address bios_get_rsdp_addr(void)
249 {
250 	unsigned long address;
251 	u8 *rsdp;
252 
253 	/* Get the location of the Extended BIOS Data Area (EBDA) */
254 	address = *(u16 *)ACPI_EBDA_PTR_LOCATION;
255 	address <<= 4;
256 
257 	/*
258 	 * Search EBDA paragraphs (EBDA is required to be a minimum of
259 	 * 1K length)
260 	 */
261 	if (address > 0x400) {
262 		rsdp = scan_mem_for_rsdp((u8 *)address, ACPI_EBDA_WINDOW_SIZE);
263 		if (rsdp)
264 			return (acpi_physical_address)(unsigned long)rsdp;
265 	}
266 
267 	/* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */
268 	rsdp = scan_mem_for_rsdp((u8 *) ACPI_HI_RSDP_WINDOW_BASE,
269 					ACPI_HI_RSDP_WINDOW_SIZE);
270 	if (rsdp)
271 		return (acpi_physical_address)(unsigned long)rsdp;
272 
273 	return 0;
274 }
275 
276 /* Return RSDP address on success, otherwise 0. */
277 acpi_physical_address get_rsdp_addr(void)
278 {
279 	acpi_physical_address pa;
280 
281 	pa = get_acpi_rsdp();
282 
283 	if (!pa)
284 		pa = boot_params->acpi_rsdp_addr;
285 
286 	/*
287 	 * Try to get EFI data from setup_data. This can happen when we're a
288 	 * kexec'ed kernel and kexec(1) has passed all the required EFI info to
289 	 * us.
290 	 */
291 	if (!pa)
292 		pa = kexec_get_rsdp_addr();
293 
294 	if (!pa)
295 		pa = efi_get_rsdp_addr();
296 
297 	if (!pa)
298 		pa = bios_get_rsdp_addr();
299 
300 	return pa;
301 }
302 
303 #if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE)
304 /* Compute SRAT address from RSDP. */
305 static unsigned long get_acpi_srat_table(void)
306 {
307 	unsigned long root_table, acpi_table;
308 	struct acpi_table_header *header;
309 	struct acpi_table_rsdp *rsdp;
310 	u32 num_entries, size, len;
311 	char arg[10];
312 	u8 *entry;
313 
314 	rsdp = (struct acpi_table_rsdp *)(long)boot_params->acpi_rsdp_addr;
315 	if (!rsdp)
316 		return 0;
317 
318 	/* Get ACPI root table from RSDP.*/
319 	if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 &&
320 	    !strncmp(arg, "rsdt", 4)) &&
321 	    rsdp->xsdt_physical_address &&
322 	    rsdp->revision > 1) {
323 		root_table = rsdp->xsdt_physical_address;
324 		size = ACPI_XSDT_ENTRY_SIZE;
325 	} else {
326 		root_table = rsdp->rsdt_physical_address;
327 		size = ACPI_RSDT_ENTRY_SIZE;
328 	}
329 
330 	if (!root_table)
331 		return 0;
332 
333 	header = (struct acpi_table_header *)root_table;
334 	len = header->length;
335 	if (len < sizeof(struct acpi_table_header) + size)
336 		return 0;
337 
338 	num_entries = (len - sizeof(struct acpi_table_header)) / size;
339 	entry = (u8 *)(root_table + sizeof(struct acpi_table_header));
340 
341 	while (num_entries--) {
342 		if (size == ACPI_RSDT_ENTRY_SIZE)
343 			acpi_table = *(u32 *)entry;
344 		else
345 			acpi_table = *(u64 *)entry;
346 
347 		if (acpi_table) {
348 			header = (struct acpi_table_header *)acpi_table;
349 
350 			if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT))
351 				return acpi_table;
352 		}
353 		entry += size;
354 	}
355 	return 0;
356 }
357 
358 /**
359  * count_immovable_mem_regions - Parse SRAT and cache the immovable
360  * memory regions into the immovable_mem array.
361  *
362  * Return the number of immovable memory regions on success, 0 on failure:
363  *
364  * - Too many immovable memory regions
365  * - ACPI off or no SRAT found
366  * - No immovable memory region found.
367  */
368 int count_immovable_mem_regions(void)
369 {
370 	unsigned long table_addr, table_end, table;
371 	struct acpi_subtable_header *sub_table;
372 	struct acpi_table_header *table_header;
373 	char arg[MAX_ACPI_ARG_LENGTH];
374 	int num = 0;
375 
376 	if (cmdline_find_option("acpi", arg, sizeof(arg)) == 3 &&
377 	    !strncmp(arg, "off", 3))
378 		return 0;
379 
380 	table_addr = get_acpi_srat_table();
381 	if (!table_addr)
382 		return 0;
383 
384 	table_header = (struct acpi_table_header *)table_addr;
385 	table_end = table_addr + table_header->length;
386 	table = table_addr + sizeof(struct acpi_table_srat);
387 
388 	while (table + sizeof(struct acpi_subtable_header) < table_end) {
389 		sub_table = (struct acpi_subtable_header *)table;
390 		if (sub_table->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) {
391 			struct acpi_srat_mem_affinity *ma;
392 
393 			ma = (struct acpi_srat_mem_affinity *)sub_table;
394 			if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && ma->length) {
395 				immovable_mem[num].start = ma->base_address;
396 				immovable_mem[num].size = ma->length;
397 				num++;
398 			}
399 
400 			if (num >= MAX_NUMNODES*2) {
401 				debug_putstr("Too many immovable memory regions, aborting.\n");
402 				return 0;
403 			}
404 		}
405 		table += sub_table->length;
406 	}
407 	return num;
408 }
409 #endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */
410