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