tables.c 23 KB

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