xref: /linux/drivers/acpi/tables.c (revision 073e62fd33fe9cec754cb89e60c0ebbab781a50a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  acpi_tables.c - ACPI Boot-Time Table Parsing
4  *
5  *  Copyright (C) 2001 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  */
7 
8 /* Uncomment next line to get verbose printout */
9 /* #define DEBUG */
10 #define pr_fmt(fmt) "ACPI: " fmt
11 
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/irq.h>
18 #include <linux/errno.h>
19 #include <linux/acpi.h>
20 #include <linux/memblock.h>
21 #include <linux/earlycpio.h>
22 #include <linux/initrd.h>
23 #include <linux/security.h>
24 #include <linux/kmemleak.h>
25 #include <asm/fixmap.h>
26 #include "internal.h"
27 
28 #ifdef CONFIG_ACPI_CUSTOM_DSDT
29 #include CONFIG_ACPI_CUSTOM_DSDT_FILE
30 #endif
31 
32 #define ACPI_MAX_TABLES		128
33 
34 static char *mps_inti_flags_polarity[] = { "dfl", "high", "res", "low" };
35 static char *mps_inti_flags_trigger[] = { "dfl", "edge", "res", "level" };
36 
37 static struct acpi_table_desc initial_tables[ACPI_MAX_TABLES] __initdata;
38 
39 static int acpi_apic_instance __initdata_or_acpilib;
40 
41 /*
42  * Disable table checksum verification for the early stage due to the size
43  * limitation of the current x86 early mapping implementation.
44  */
45 static bool acpi_verify_table_checksum __initdata_or_acpilib = false;
46 
acpi_table_print_madt_entry(struct acpi_subtable_header * header)47 void acpi_table_print_madt_entry(struct acpi_subtable_header *header)
48 {
49 	if (!header)
50 		return;
51 
52 	switch (header->type) {
53 
54 	case ACPI_MADT_TYPE_LOCAL_APIC:
55 		{
56 			struct acpi_madt_local_apic *p =
57 			    (struct acpi_madt_local_apic *)header;
58 			pr_debug("LAPIC (acpi_id[0x%02x] lapic_id[0x%02x] %s)\n",
59 				 p->processor_id, p->id,
60 				 str_enabled_disabled(p->lapic_flags & ACPI_MADT_ENABLED));
61 		}
62 		break;
63 
64 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
65 		{
66 			struct acpi_madt_local_x2apic *p =
67 			    (struct acpi_madt_local_x2apic *)header;
68 			pr_debug("X2APIC (apic_id[0x%02x] uid[0x%02x] %s)\n",
69 				 p->local_apic_id, p->uid,
70 				 str_enabled_disabled(p->lapic_flags & ACPI_MADT_ENABLED));
71 		}
72 		break;
73 
74 	case ACPI_MADT_TYPE_IO_APIC:
75 		{
76 			struct acpi_madt_io_apic *p =
77 			    (struct acpi_madt_io_apic *)header;
78 			pr_debug("IOAPIC (id[0x%02x] address[0x%08x] gsi_base[%d])\n",
79 				 p->id, p->address, p->global_irq_base);
80 		}
81 		break;
82 
83 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
84 		{
85 			struct acpi_madt_interrupt_override *p =
86 			    (struct acpi_madt_interrupt_override *)header;
87 			pr_info("INT_SRC_OVR (bus %d bus_irq %d global_irq %d %s %s)\n",
88 				p->bus, p->source_irq, p->global_irq,
89 				mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
90 				mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2]);
91 			if (p->inti_flags  &
92 			    ~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK))
93 				pr_info("INT_SRC_OVR unexpected reserved flags: 0x%x\n",
94 					p->inti_flags  &
95 					~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK));
96 		}
97 		break;
98 
99 	case ACPI_MADT_TYPE_NMI_SOURCE:
100 		{
101 			struct acpi_madt_nmi_source *p =
102 			    (struct acpi_madt_nmi_source *)header;
103 			pr_info("NMI_SRC (%s %s global_irq %d)\n",
104 				mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
105 				mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
106 				p->global_irq);
107 		}
108 		break;
109 
110 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
111 		{
112 			struct acpi_madt_local_apic_nmi *p =
113 			    (struct acpi_madt_local_apic_nmi *)header;
114 			pr_info("LAPIC_NMI (acpi_id[0x%02x] %s %s lint[0x%x])\n",
115 				p->processor_id,
116 				mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK	],
117 				mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
118 				p->lint);
119 		}
120 		break;
121 
122 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
123 		{
124 			u16 polarity, trigger;
125 			struct acpi_madt_local_x2apic_nmi *p =
126 			    (struct acpi_madt_local_x2apic_nmi *)header;
127 
128 			polarity = p->inti_flags & ACPI_MADT_POLARITY_MASK;
129 			trigger = (p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
130 
131 			pr_info("X2APIC_NMI (uid[0x%02x] %s %s lint[0x%x])\n",
132 				p->uid,
133 				mps_inti_flags_polarity[polarity],
134 				mps_inti_flags_trigger[trigger],
135 				p->lint);
136 		}
137 		break;
138 
139 	case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
140 		{
141 			struct acpi_madt_local_apic_override *p =
142 			    (struct acpi_madt_local_apic_override *)header;
143 			pr_info("LAPIC_ADDR_OVR (address[0x%llx])\n",
144 				p->address);
145 		}
146 		break;
147 
148 	case ACPI_MADT_TYPE_IO_SAPIC:
149 		{
150 			struct acpi_madt_io_sapic *p =
151 			    (struct acpi_madt_io_sapic *)header;
152 			pr_debug("IOSAPIC (id[0x%x] address[%p] gsi_base[%d])\n",
153 				 p->id, (void *)(unsigned long)p->address,
154 				 p->global_irq_base);
155 		}
156 		break;
157 
158 	case ACPI_MADT_TYPE_LOCAL_SAPIC:
159 		{
160 			struct acpi_madt_local_sapic *p =
161 			    (struct acpi_madt_local_sapic *)header;
162 			pr_debug("LSAPIC (acpi_id[0x%02x] lsapic_id[0x%02x] lsapic_eid[0x%02x] %s)\n",
163 				 p->processor_id, p->id, p->eid,
164 				 str_enabled_disabled(p->lapic_flags & ACPI_MADT_ENABLED));
165 		}
166 		break;
167 
168 	case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
169 		{
170 			struct acpi_madt_interrupt_source *p =
171 			    (struct acpi_madt_interrupt_source *)header;
172 			pr_info("PLAT_INT_SRC (%s %s type[0x%x] id[0x%04x] eid[0x%x] iosapic_vector[0x%x] global_irq[0x%x]\n",
173 				mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
174 				mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
175 				p->type, p->id, p->eid, p->io_sapic_vector,
176 				p->global_irq);
177 		}
178 		break;
179 
180 	case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
181 		{
182 			struct acpi_madt_generic_interrupt *p =
183 				(struct acpi_madt_generic_interrupt *)header;
184 			pr_debug("GICC (acpi_id[0x%04x] address[%llx] MPIDR[0x%llx] %s)\n",
185 				 p->uid, p->base_address,
186 				 p->arm_mpidr,
187 				 str_enabled_disabled(p->flags & ACPI_MADT_ENABLED));
188 
189 		}
190 		break;
191 
192 	case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
193 		{
194 			struct acpi_madt_generic_distributor *p =
195 				(struct acpi_madt_generic_distributor *)header;
196 			pr_debug("GIC Distributor (gic_id[0x%04x] address[%llx] gsi_base[%d])\n",
197 				 p->gic_id, p->base_address,
198 				 p->global_irq_base);
199 		}
200 		break;
201 
202 	case ACPI_MADT_TYPE_MULTIPROC_WAKEUP:
203 		{
204 			struct acpi_madt_multiproc_wakeup *p =
205 				(struct acpi_madt_multiproc_wakeup *)header;
206 			u64 reset_vector = 0;
207 
208 			if (p->version >= ACPI_MADT_MP_WAKEUP_VERSION_V1)
209 				reset_vector = p->reset_vector;
210 
211 			pr_debug("MP Wakeup (version[%d], mailbox[%#llx], reset[%#llx])\n",
212 				 p->version, p->mailbox_address, reset_vector);
213 		}
214 		break;
215 
216 	case ACPI_MADT_TYPE_CORE_PIC:
217 		{
218 			struct acpi_madt_core_pic *p = (struct acpi_madt_core_pic *)header;
219 
220 			pr_debug("CORE PIC (processor_id[0x%02x] core_id[0x%02x] %s)\n",
221 				 p->processor_id, p->core_id,
222 				 str_enabled_disabled(p->flags & ACPI_MADT_ENABLED));
223 		}
224 		break;
225 
226 	case ACPI_MADT_TYPE_RINTC:
227 		{
228 			struct acpi_madt_rintc *p = (struct acpi_madt_rintc *)header;
229 
230 			pr_debug("RISC-V INTC (acpi_uid[0x%04x] hart_id[0x%llx] %s)\n",
231 				 p->uid, p->hart_id,
232 				 str_enabled_disabled(p->flags & ACPI_MADT_ENABLED));
233 		}
234 		break;
235 
236 	default:
237 		pr_warn("Found unsupported MADT entry (type = 0x%x)\n",
238 			header->type);
239 		break;
240 	}
241 }
242 
acpi_table_parse_entries_array(char * id,unsigned long table_size,struct acpi_subtable_proc * proc,int proc_num,unsigned int max_entries)243 int __init_or_acpilib acpi_table_parse_entries_array(
244 	char *id, unsigned long table_size, struct acpi_subtable_proc *proc,
245 	int proc_num, unsigned int max_entries)
246 {
247 	struct acpi_table_header *table_header = NULL;
248 	int count;
249 	u32 instance = 0;
250 
251 	if (acpi_disabled)
252 		return -ENODEV;
253 
254 	if (!id)
255 		return -EINVAL;
256 
257 	if (!table_size)
258 		return -EINVAL;
259 
260 	if (!strncmp(id, ACPI_SIG_MADT, 4))
261 		instance = acpi_apic_instance;
262 
263 	acpi_get_table(id, instance, &table_header);
264 	if (!table_header) {
265 		pr_debug("%4.4s not present\n", id);
266 		return -ENODEV;
267 	}
268 
269 	count = acpi_parse_entries_array(id, table_size,
270 					 (union fw_table_header *)table_header,
271 					 0, proc, proc_num, max_entries);
272 
273 	acpi_put_table(table_header);
274 	return count;
275 }
276 
__acpi_table_parse_entries(char * id,unsigned long table_size,int entry_id,acpi_tbl_entry_handler handler,acpi_tbl_entry_handler_arg handler_arg,void * arg,unsigned int max_entries)277 static int __init_or_acpilib __acpi_table_parse_entries(
278 	char *id, unsigned long table_size, int entry_id,
279 	acpi_tbl_entry_handler handler, acpi_tbl_entry_handler_arg handler_arg,
280 	void *arg, unsigned int max_entries)
281 {
282 	struct acpi_subtable_proc proc = {
283 		.id		= entry_id,
284 		.handler	= handler,
285 		.handler_arg	= handler_arg,
286 		.arg		= arg,
287 	};
288 
289 	return acpi_table_parse_entries_array(id, table_size, &proc, 1,
290 						max_entries);
291 }
292 
293 int __init_or_acpilib
acpi_table_parse_cedt(enum acpi_cedt_type id,acpi_tbl_entry_handler_arg handler_arg,void * arg)294 acpi_table_parse_cedt(enum acpi_cedt_type id,
295 		      acpi_tbl_entry_handler_arg handler_arg, void *arg)
296 {
297 	return __acpi_table_parse_entries(ACPI_SIG_CEDT,
298 					  sizeof(struct acpi_table_cedt), id,
299 					  NULL, handler_arg, arg, 0);
300 }
301 EXPORT_SYMBOL_ACPI_LIB(acpi_table_parse_cedt);
302 
acpi_table_parse_entries(char * id,unsigned long table_size,int entry_id,acpi_tbl_entry_handler handler,unsigned int max_entries)303 int __init acpi_table_parse_entries(char *id, unsigned long table_size,
304 				    int entry_id,
305 				    acpi_tbl_entry_handler handler,
306 				    unsigned int max_entries)
307 {
308 	return __acpi_table_parse_entries(id, table_size, entry_id, handler,
309 					  NULL, NULL, max_entries);
310 }
311 
acpi_table_parse_madt(enum acpi_madt_type id,acpi_tbl_entry_handler handler,unsigned int max_entries)312 int __init acpi_table_parse_madt(enum acpi_madt_type id,
313 		      acpi_tbl_entry_handler handler, unsigned int max_entries)
314 {
315 	return acpi_table_parse_entries(ACPI_SIG_MADT,
316 					    sizeof(struct acpi_table_madt), id,
317 					    handler, max_entries);
318 }
319 
320 /**
321  * acpi_table_parse - find table with @id, run @handler on it
322  * @id: table id to find
323  * @handler: handler to run
324  *
325  * Scan the ACPI System Descriptor Table (STD) for a table matching @id,
326  * run @handler on it.
327  *
328  * Return 0 if table found, -errno if not.
329  */
acpi_table_parse(char * id,acpi_tbl_table_handler handler)330 int __init acpi_table_parse(char *id, acpi_tbl_table_handler handler)
331 {
332 	struct acpi_table_header *table = NULL;
333 
334 	if (acpi_disabled)
335 		return -ENODEV;
336 
337 	if (!id || !handler)
338 		return -EINVAL;
339 
340 	if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
341 		acpi_get_table(id, acpi_apic_instance, &table);
342 	else
343 		acpi_get_table(id, 0, &table);
344 
345 	if (table) {
346 		handler(table);
347 		acpi_put_table(table);
348 		return 0;
349 	} else
350 		return -ENODEV;
351 }
352 
353 /*
354  * The BIOS is supposed to supply a single APIC/MADT,
355  * but some report two.  Provide a knob to use either.
356  * (don't you wish instance 0 and 1 were not the same?)
357  */
check_multiple_madt(void)358 static void __init check_multiple_madt(void)
359 {
360 	struct acpi_table_header *table = NULL;
361 
362 	acpi_get_table(ACPI_SIG_MADT, 2, &table);
363 	if (table) {
364 		pr_warn("BIOS bug: multiple APIC/MADT found, using %d\n",
365 			acpi_apic_instance);
366 		pr_warn("If \"acpi_apic_instance=%d\" works better, "
367 			"notify linux-acpi@vger.kernel.org\n",
368 			acpi_apic_instance ? 0 : 2);
369 		acpi_put_table(table);
370 
371 	} else
372 		acpi_apic_instance = 0;
373 
374 	return;
375 }
376 
acpi_table_taint(struct acpi_table_header * table)377 static void acpi_table_taint(struct acpi_table_header *table)
378 {
379 	pr_warn("Override [%4.4s-%8.8s], this is unsafe: tainting kernel\n",
380 		table->signature, table->oem_table_id);
381 	add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
382 }
383 
384 #ifdef CONFIG_ACPI_TABLE_UPGRADE
385 static u64 acpi_tables_addr;
386 static int all_tables_size;
387 
388 /* Copied from acpica/tbutils.c:acpi_tb_checksum() */
acpi_table_checksum(u8 * buffer,u32 length)389 static u8 __init acpi_table_checksum(u8 *buffer, u32 length)
390 {
391 	u8 sum = 0;
392 	u8 *end = buffer + length;
393 
394 	while (buffer < end)
395 		sum = (u8) (sum + *(buffer++));
396 	return sum;
397 }
398 
399 /* All but ACPI_SIG_RSDP and ACPI_SIG_FACS: */
400 static const char table_sigs[][ACPI_NAMESEG_SIZE] __nonstring_array __initconst = {
401 	ACPI_SIG_BERT, ACPI_SIG_BGRT, ACPI_SIG_CPEP, ACPI_SIG_ECDT,
402 	ACPI_SIG_EINJ, ACPI_SIG_ERST, ACPI_SIG_HEST, ACPI_SIG_MADT,
403 	ACPI_SIG_MSCT, ACPI_SIG_SBST, ACPI_SIG_SLIT, ACPI_SIG_SRAT,
404 	ACPI_SIG_ASF,  ACPI_SIG_BOOT, ACPI_SIG_DBGP, ACPI_SIG_DMAR,
405 	ACPI_SIG_HPET, ACPI_SIG_IBFT, ACPI_SIG_IVRS, ACPI_SIG_MCFG,
406 	ACPI_SIG_MCHI, ACPI_SIG_SLIC, ACPI_SIG_SPCR, ACPI_SIG_SPMI,
407 	ACPI_SIG_TCPA, ACPI_SIG_UEFI, ACPI_SIG_WAET, ACPI_SIG_WDAT,
408 	ACPI_SIG_WDDT, ACPI_SIG_WDRT, ACPI_SIG_DSDT, ACPI_SIG_FADT,
409 	ACPI_SIG_PSDT, ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT,
410 	ACPI_SIG_IORT, ACPI_SIG_NFIT, ACPI_SIG_HMAT, ACPI_SIG_PPTT,
411 	ACPI_SIG_NHLT, ACPI_SIG_AEST, ACPI_SIG_CEDT, ACPI_SIG_AGDI,
412 	ACPI_SIG_NBFT, ACPI_SIG_SWFT, ACPI_SIG_MPAM};
413 
414 #define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
415 
416 #define NR_ACPI_INITRD_TABLES 64
417 static struct cpio_data __initdata acpi_initrd_files[NR_ACPI_INITRD_TABLES];
418 static DECLARE_BITMAP(acpi_initrd_installed, NR_ACPI_INITRD_TABLES);
419 
420 #define MAP_CHUNK_SIZE   (NR_FIX_BTMAPS << PAGE_SHIFT)
421 
acpi_table_upgrade(void)422 void __init acpi_table_upgrade(void)
423 {
424 	void *data;
425 	size_t size;
426 	int sig, no, table_nr = 0, total_offset = 0;
427 	long offset = 0;
428 	struct acpi_table_header *table;
429 	char cpio_path[32] = "kernel/firmware/acpi/";
430 	struct cpio_data file;
431 
432 	if (IS_ENABLED(CONFIG_ACPI_TABLE_OVERRIDE_VIA_BUILTIN_INITRD)) {
433 		data = __initramfs_start;
434 		size = __initramfs_size;
435 	} else {
436 		data = (void *)initrd_start;
437 		size = initrd_end - initrd_start;
438 	}
439 
440 	if (data == NULL || size == 0)
441 		return;
442 
443 	for (no = 0; no < NR_ACPI_INITRD_TABLES; no++) {
444 		file = find_cpio_data(cpio_path, data, size, &offset);
445 		if (!file.data)
446 			break;
447 
448 		data += offset;
449 		size -= offset;
450 
451 		if (file.size < sizeof(struct acpi_table_header)) {
452 			pr_err("ACPI OVERRIDE: Table smaller than ACPI header [%s%s]\n",
453 				cpio_path, file.name);
454 			continue;
455 		}
456 
457 		table = file.data;
458 
459 		for (sig = 0; sig < ARRAY_SIZE(table_sigs); sig++)
460 			if (!memcmp(table->signature, table_sigs[sig], 4))
461 				break;
462 
463 		if (sig >= ARRAY_SIZE(table_sigs)) {
464 			pr_err("ACPI OVERRIDE: Unknown signature [%s%s]\n",
465 				cpio_path, file.name);
466 			continue;
467 		}
468 		if (file.size != table->length) {
469 			pr_err("ACPI OVERRIDE: File length does not match table length [%s%s]\n",
470 				cpio_path, file.name);
471 			continue;
472 		}
473 		if (acpi_table_checksum(file.data, table->length)) {
474 			pr_err("ACPI OVERRIDE: Bad table checksum [%s%s]\n",
475 				cpio_path, file.name);
476 			continue;
477 		}
478 
479 		pr_info("%4.4s ACPI table found in initrd [%s%s][0x%x]\n",
480 			table->signature, cpio_path, file.name, table->length);
481 
482 		all_tables_size += table->length;
483 		acpi_initrd_files[table_nr].data = file.data;
484 		acpi_initrd_files[table_nr].size = file.size;
485 		table_nr++;
486 	}
487 	if (table_nr == 0)
488 		return;
489 
490 	if (security_locked_down(LOCKDOWN_ACPI_TABLES)) {
491 		pr_notice("kernel is locked down, ignoring table override\n");
492 		return;
493 	}
494 
495 	acpi_tables_addr =
496 		memblock_phys_alloc_range(all_tables_size, PAGE_SIZE,
497 					  0, ACPI_TABLE_UPGRADE_MAX_PHYS);
498 	if (!acpi_tables_addr) {
499 		WARN_ON(1);
500 		return;
501 	}
502 	/*
503 	 * Only calling e820_add_reserve does not work and the
504 	 * tables are invalid (memory got used) later.
505 	 * memblock_reserve works as expected and the tables won't get modified.
506 	 * But it's not enough on X86 because ioremap will
507 	 * complain later (used by acpi_os_map_memory) that the pages
508 	 * that should get mapped are not marked "reserved".
509 	 * Both memblock_reserve and e820__range_add (via arch_reserve_mem_area)
510 	 * works fine.
511 	 */
512 	arch_reserve_mem_area(acpi_tables_addr, all_tables_size);
513 
514 	kmemleak_ignore_phys(acpi_tables_addr);
515 
516 	/*
517 	 * early_ioremap only can remap 256k one time. If we map all
518 	 * tables one time, we will hit the limit. Need to map chunks
519 	 * one by one during copying the same as that in relocate_initrd().
520 	 */
521 	for (no = 0; no < table_nr; no++) {
522 		unsigned char *src_p = acpi_initrd_files[no].data;
523 		phys_addr_t size = acpi_initrd_files[no].size;
524 		phys_addr_t dest_addr = acpi_tables_addr + total_offset;
525 		phys_addr_t slop, clen;
526 		char *dest_p;
527 
528 		total_offset += size;
529 
530 		while (size) {
531 			slop = dest_addr & ~PAGE_MASK;
532 			clen = size;
533 			if (clen > MAP_CHUNK_SIZE - slop)
534 				clen = MAP_CHUNK_SIZE - slop;
535 			dest_p = early_memremap(dest_addr & PAGE_MASK,
536 						clen + slop);
537 			memcpy(dest_p + slop, src_p, clen);
538 			early_memunmap(dest_p, clen + slop);
539 			src_p += clen;
540 			dest_addr += clen;
541 			size -= clen;
542 		}
543 	}
544 }
545 
546 static acpi_status
acpi_table_initrd_override(struct acpi_table_header * existing_table,acpi_physical_address * address,u32 * length)547 acpi_table_initrd_override(struct acpi_table_header *existing_table,
548 			   acpi_physical_address *address, u32 *length)
549 {
550 	int table_offset = 0;
551 	int table_index = 0;
552 	struct acpi_table_header *table;
553 	u32 table_length;
554 
555 	*length = 0;
556 	*address = 0;
557 	if (!acpi_tables_addr)
558 		return AE_OK;
559 
560 	while (table_offset + ACPI_HEADER_SIZE <= all_tables_size) {
561 		table = acpi_os_map_memory(acpi_tables_addr + table_offset,
562 					   ACPI_HEADER_SIZE);
563 		if (table_offset + table->length > all_tables_size) {
564 			acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
565 			WARN_ON(1);
566 			return AE_OK;
567 		}
568 
569 		table_length = table->length;
570 
571 		/* Only override tables matched */
572 		if (memcmp(existing_table->signature, table->signature, 4) ||
573 		    memcmp(table->oem_id, existing_table->oem_id,
574 			   ACPI_OEM_ID_SIZE) ||
575 		    memcmp(table->oem_table_id, existing_table->oem_table_id,
576 			   ACPI_OEM_TABLE_ID_SIZE)) {
577 			acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
578 			goto next_table;
579 		}
580 		/*
581 		 * Mark the table to avoid being used in
582 		 * acpi_table_initrd_scan() and check the revision.
583 		 */
584 		if (test_and_set_bit(table_index, acpi_initrd_installed) ||
585 		    existing_table->oem_revision >= table->oem_revision) {
586 			acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
587 			goto next_table;
588 		}
589 
590 		*length = table_length;
591 		*address = acpi_tables_addr + table_offset;
592 		pr_info("Table Upgrade: override [%4.4s-%6.6s-%8.8s]\n",
593 			table->signature, table->oem_id,
594 			table->oem_table_id);
595 		acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
596 		break;
597 
598 next_table:
599 		table_offset += table_length;
600 		table_index++;
601 	}
602 	return AE_OK;
603 }
604 
acpi_table_initrd_scan(void)605 static void __init acpi_table_initrd_scan(void)
606 {
607 	int table_offset = 0;
608 	int table_index = 0;
609 	u32 table_length;
610 	struct acpi_table_header *table;
611 
612 	if (!acpi_tables_addr)
613 		return;
614 
615 	while (table_offset + ACPI_HEADER_SIZE <= all_tables_size) {
616 		table = acpi_os_map_memory(acpi_tables_addr + table_offset,
617 					   ACPI_HEADER_SIZE);
618 		if (table_offset + table->length > all_tables_size) {
619 			acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
620 			WARN_ON(1);
621 			return;
622 		}
623 
624 		table_length = table->length;
625 
626 		/* Skip RSDT/XSDT which should only be used for override */
627 		if (ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_RSDT) ||
628 		    ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_XSDT)) {
629 			acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
630 			goto next_table;
631 		}
632 		/*
633 		 * Mark the table to avoid being used in
634 		 * acpi_table_initrd_override(). Though this is not possible
635 		 * because override is disabled in acpi_install_physical_table().
636 		 */
637 		if (test_and_set_bit(table_index, acpi_initrd_installed)) {
638 			acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
639 			goto next_table;
640 		}
641 
642 		pr_info("Table Upgrade: install [%4.4s-%6.6s-%8.8s]\n",
643 			table->signature, table->oem_id,
644 			table->oem_table_id);
645 		acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
646 		acpi_install_physical_table(acpi_tables_addr + table_offset);
647 next_table:
648 		table_offset += table_length;
649 		table_index++;
650 	}
651 }
652 #else
653 static acpi_status
acpi_table_initrd_override(struct acpi_table_header * existing_table,acpi_physical_address * address,u32 * table_length)654 acpi_table_initrd_override(struct acpi_table_header *existing_table,
655 			   acpi_physical_address *address,
656 			   u32 *table_length)
657 {
658 	*table_length = 0;
659 	*address = 0;
660 	return AE_OK;
661 }
662 
acpi_table_initrd_scan(void)663 static void __init acpi_table_initrd_scan(void)
664 {
665 }
666 #endif /* CONFIG_ACPI_TABLE_UPGRADE */
667 
668 acpi_status
acpi_os_physical_table_override(struct acpi_table_header * existing_table,acpi_physical_address * address,u32 * table_length)669 acpi_os_physical_table_override(struct acpi_table_header *existing_table,
670 				acpi_physical_address *address,
671 				u32 *table_length)
672 {
673 	return acpi_table_initrd_override(existing_table, address,
674 					  table_length);
675 }
676 
677 #ifdef CONFIG_ACPI_CUSTOM_DSDT
678 static void *amlcode __attribute__ ((weakref("AmlCode")));
679 static void *dsdt_amlcode __attribute__ ((weakref("dsdt_aml_code")));
680 #endif
681 
acpi_os_table_override(struct acpi_table_header * existing_table,struct acpi_table_header ** new_table)682 acpi_status acpi_os_table_override(struct acpi_table_header *existing_table,
683 		       struct acpi_table_header **new_table)
684 {
685 	if (!existing_table || !new_table)
686 		return AE_BAD_PARAMETER;
687 
688 	*new_table = NULL;
689 
690 #ifdef CONFIG_ACPI_CUSTOM_DSDT
691 	if (!strncmp(existing_table->signature, "DSDT", 4)) {
692 		*new_table = (struct acpi_table_header *)&amlcode;
693 		if (!(*new_table))
694 			*new_table = (struct acpi_table_header *)&dsdt_amlcode;
695 	}
696 #endif
697 	if (*new_table != NULL)
698 		acpi_table_taint(existing_table);
699 	return AE_OK;
700 }
701 
702 /*
703  * acpi_locate_initial_tables()
704  *
705  * Get the RSDP, then find and checksum all the ACPI tables.
706  *
707  * result: initial_tables[] is initialized, and points to
708  * a list of ACPI tables.
709  */
acpi_locate_initial_tables(void)710 int __init acpi_locate_initial_tables(void)
711 {
712 	acpi_status status;
713 
714 	if (acpi_verify_table_checksum) {
715 		pr_info("Early table checksum verification enabled\n");
716 		acpi_gbl_enable_table_validation = TRUE;
717 	} else {
718 		pr_info("Early table checksum verification disabled\n");
719 		acpi_gbl_enable_table_validation = FALSE;
720 	}
721 
722 	status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
723 	if (ACPI_FAILURE(status)) {
724 		const char *msg = acpi_format_exception(status);
725 
726 		pr_warn("Failed to initialize tables, status=0x%x (%s)", status, msg);
727 		return -EINVAL;
728 	}
729 
730 	return 0;
731 }
732 
acpi_reserve_initial_tables(void)733 void __init acpi_reserve_initial_tables(void)
734 {
735 	int i;
736 
737 	for (i = 0; i < ACPI_MAX_TABLES; i++) {
738 		struct acpi_table_desc *table_desc = &initial_tables[i];
739 		u64 start = table_desc->address;
740 		u64 size = table_desc->length;
741 
742 		if (!start || !size)
743 			break;
744 
745 		pr_info("Reserving %4s table memory at [mem 0x%llx-0x%llx]\n",
746 			table_desc->signature.ascii, start, start + size - 1);
747 
748 		memblock_reserve(start, size);
749 	}
750 }
751 
acpi_table_init_complete(void)752 void __init acpi_table_init_complete(void)
753 {
754 	acpi_table_initrd_scan();
755 	check_multiple_madt();
756 }
757 
acpi_table_init(void)758 int __init acpi_table_init(void)
759 {
760 	int ret;
761 
762 	ret = acpi_locate_initial_tables();
763 	if (ret)
764 		return ret;
765 
766 	acpi_table_init_complete();
767 
768 	return 0;
769 }
770 
acpi_parse_apic_instance(char * str)771 static int __init acpi_parse_apic_instance(char *str)
772 {
773 	if (!str)
774 		return -EINVAL;
775 
776 	if (kstrtoint(str, 0, &acpi_apic_instance))
777 		return -EINVAL;
778 
779 	pr_notice("Shall use APIC/MADT table %d\n", acpi_apic_instance);
780 
781 	return 0;
782 }
783 early_param("acpi_apic_instance", acpi_parse_apic_instance);
784 
acpi_force_table_verification_setup(char * s)785 static int __init acpi_force_table_verification_setup(char *s)
786 {
787 	acpi_verify_table_checksum = true;
788 
789 	return 0;
790 }
791 early_param("acpi_force_table_verification", acpi_force_table_verification_setup);
792 
acpi_force_32bit_fadt_addr(char * s)793 static int __init acpi_force_32bit_fadt_addr(char *s)
794 {
795 	pr_info("Forcing 32 Bit FADT addresses\n");
796 	acpi_gbl_use32_bit_fadt_addresses = TRUE;
797 
798 	return 0;
799 }
800 early_param("acpi_force_32bit_fadt_addr", acpi_force_32bit_fadt_addr);
801