tables.c 25 KB

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