tables.c 21 KB

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