tables.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * acpi_tables.c - ACPI Boot-Time Table Parsing
  3. *
  4. * Copyright (C) 2001 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. *
  20. */
  21. /* Uncomment next line to get verbose printout */
  22. /* #define DEBUG */
  23. #define pr_fmt(fmt) "ACPI: " fmt
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/smp.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include <linux/irq.h>
  30. #include <linux/errno.h>
  31. #include <linux/acpi.h>
  32. #include <linux/bootmem.h>
  33. #include "internal.h"
  34. #define ACPI_MAX_TABLES 128
  35. static char *mps_inti_flags_polarity[] = { "dfl", "high", "res", "low" };
  36. static char *mps_inti_flags_trigger[] = { "dfl", "edge", "res", "level" };
  37. static struct acpi_table_desc initial_tables[ACPI_MAX_TABLES] __initdata;
  38. static int acpi_apic_instance __initdata;
  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. /**
  188. * acpi_parse_entries_array - for each proc_num find a suitable subtable
  189. *
  190. * @id: table id (for debugging purposes)
  191. * @table_size: single entry size
  192. * @table_header: where does the table start?
  193. * @proc: array of acpi_subtable_proc struct containing entry id
  194. * and associated handler with it
  195. * @proc_num: how big proc is?
  196. * @max_entries: how many entries can we process?
  197. *
  198. * For each proc_num find a subtable with proc->id and run proc->handler
  199. * on it. Assumption is that there's only single handler for particular
  200. * entry id.
  201. *
  202. * On success returns sum of all matching entries for all proc handlers.
  203. * Otherwise, -ENODEV or -EINVAL is returned.
  204. */
  205. static int __init
  206. acpi_parse_entries_array(char *id, unsigned long table_size,
  207. struct acpi_table_header *table_header,
  208. struct acpi_subtable_proc *proc, int proc_num,
  209. unsigned int max_entries)
  210. {
  211. struct acpi_subtable_header *entry;
  212. unsigned long table_end;
  213. int count = 0;
  214. int i;
  215. if (acpi_disabled)
  216. return -ENODEV;
  217. if (!id)
  218. return -EINVAL;
  219. if (!table_size)
  220. return -EINVAL;
  221. if (!table_header) {
  222. pr_warn("%4.4s not present\n", id);
  223. return -ENODEV;
  224. }
  225. table_end = (unsigned long)table_header + table_header->length;
  226. /* Parse all entries looking for a match. */
  227. entry = (struct acpi_subtable_header *)
  228. ((unsigned long)table_header + table_size);
  229. while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
  230. table_end) {
  231. if (max_entries && count >= max_entries)
  232. break;
  233. for (i = 0; i < proc_num; i++) {
  234. if (entry->type != proc[i].id)
  235. continue;
  236. if (!proc[i].handler ||
  237. proc[i].handler(entry, table_end))
  238. return -EINVAL;
  239. proc->count++;
  240. break;
  241. }
  242. if (i != proc_num)
  243. count++;
  244. /*
  245. * If entry->length is 0, break from this loop to avoid
  246. * infinite loop.
  247. */
  248. if (entry->length == 0) {
  249. pr_err("[%4.4s:0x%02x] Invalid zero length\n", id, proc->id);
  250. return -EINVAL;
  251. }
  252. entry = (struct acpi_subtable_header *)
  253. ((unsigned long)entry + entry->length);
  254. }
  255. if (max_entries && count > max_entries) {
  256. pr_warn("[%4.4s:0x%02x] ignored %i entries of %i found\n",
  257. id, proc->id, count - max_entries, count);
  258. }
  259. return count;
  260. }
  261. int __init
  262. acpi_parse_entries(char *id,
  263. unsigned long table_size,
  264. acpi_tbl_entry_handler handler,
  265. struct acpi_table_header *table_header,
  266. int entry_id, unsigned int max_entries)
  267. {
  268. struct acpi_subtable_proc proc = {
  269. .id = entry_id,
  270. .handler = handler,
  271. };
  272. return acpi_parse_entries_array(id, table_size, table_header,
  273. &proc, 1, max_entries);
  274. }
  275. int __init
  276. acpi_table_parse_entries_array(char *id,
  277. unsigned long table_size,
  278. struct acpi_subtable_proc *proc, int proc_num,
  279. unsigned int max_entries)
  280. {
  281. struct acpi_table_header *table_header = NULL;
  282. acpi_size tbl_size;
  283. int count;
  284. u32 instance = 0;
  285. if (acpi_disabled)
  286. return -ENODEV;
  287. if (!id)
  288. return -EINVAL;
  289. if (!strncmp(id, ACPI_SIG_MADT, 4))
  290. instance = acpi_apic_instance;
  291. acpi_get_table_with_size(id, instance, &table_header, &tbl_size);
  292. if (!table_header) {
  293. pr_warn("%4.4s not present\n", id);
  294. return -ENODEV;
  295. }
  296. count = acpi_parse_entries_array(id, table_size, table_header,
  297. proc, proc_num, max_entries);
  298. early_acpi_os_unmap_memory((char *)table_header, tbl_size);
  299. return count;
  300. }
  301. int __init
  302. acpi_table_parse_entries(char *id,
  303. unsigned long table_size,
  304. int entry_id,
  305. acpi_tbl_entry_handler handler,
  306. unsigned int max_entries)
  307. {
  308. struct acpi_subtable_proc proc = {
  309. .id = entry_id,
  310. .handler = handler,
  311. };
  312. return acpi_table_parse_entries_array(id, table_size, &proc, 1,
  313. max_entries);
  314. }
  315. int __init
  316. acpi_table_parse_madt(enum acpi_madt_type id,
  317. acpi_tbl_entry_handler handler, unsigned int max_entries)
  318. {
  319. return acpi_table_parse_entries(ACPI_SIG_MADT,
  320. sizeof(struct acpi_table_madt), id,
  321. handler, max_entries);
  322. }
  323. /**
  324. * acpi_table_parse - find table with @id, run @handler on it
  325. * @id: table id to find
  326. * @handler: handler to run
  327. *
  328. * Scan the ACPI System Descriptor Table (STD) for a table matching @id,
  329. * run @handler on it.
  330. *
  331. * Return 0 if table found, -errno if not.
  332. */
  333. int __init acpi_table_parse(char *id, acpi_tbl_table_handler handler)
  334. {
  335. struct acpi_table_header *table = NULL;
  336. acpi_size tbl_size;
  337. if (acpi_disabled)
  338. return -ENODEV;
  339. if (!id || !handler)
  340. return -EINVAL;
  341. if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
  342. acpi_get_table_with_size(id, acpi_apic_instance, &table, &tbl_size);
  343. else
  344. acpi_get_table_with_size(id, 0, &table, &tbl_size);
  345. if (table) {
  346. handler(table);
  347. early_acpi_os_unmap_memory(table, tbl_size);
  348. return 0;
  349. } else
  350. return -ENODEV;
  351. }
  352. /*
  353. * The BIOS is supposed to supply a single APIC/MADT,
  354. * but some report two. Provide a knob to use either.
  355. * (don't you wish instance 0 and 1 were not the same?)
  356. */
  357. static void __init check_multiple_madt(void)
  358. {
  359. struct acpi_table_header *table = NULL;
  360. acpi_size tbl_size;
  361. acpi_get_table_with_size(ACPI_SIG_MADT, 2, &table, &tbl_size);
  362. if (table) {
  363. pr_warn("BIOS bug: multiple APIC/MADT found, using %d\n",
  364. acpi_apic_instance);
  365. pr_warn("If \"acpi_apic_instance=%d\" works better, "
  366. "notify linux-acpi@vger.kernel.org\n",
  367. acpi_apic_instance ? 0 : 2);
  368. early_acpi_os_unmap_memory(table, tbl_size);
  369. } else
  370. acpi_apic_instance = 0;
  371. return;
  372. }
  373. /*
  374. * acpi_table_init()
  375. *
  376. * find RSDP, find and checksum SDT/XSDT.
  377. * checksum all tables, print SDT/XSDT
  378. *
  379. * result: sdt_entry[] is initialized
  380. */
  381. int __init acpi_table_init(void)
  382. {
  383. acpi_status status;
  384. if (acpi_verify_table_checksum) {
  385. pr_info("Early table checksum verification enabled\n");
  386. acpi_gbl_verify_table_checksum = TRUE;
  387. } else {
  388. pr_info("Early table checksum verification disabled\n");
  389. acpi_gbl_verify_table_checksum = FALSE;
  390. }
  391. status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
  392. if (ACPI_FAILURE(status))
  393. return -EINVAL;
  394. acpi_initrd_initialize_tables();
  395. check_multiple_madt();
  396. return 0;
  397. }
  398. static int __init acpi_parse_apic_instance(char *str)
  399. {
  400. if (!str)
  401. return -EINVAL;
  402. if (kstrtoint(str, 0, &acpi_apic_instance))
  403. return -EINVAL;
  404. pr_notice("Shall use APIC/MADT table %d\n", acpi_apic_instance);
  405. return 0;
  406. }
  407. early_param("acpi_apic_instance", acpi_parse_apic_instance);
  408. static int __init acpi_force_table_verification_setup(char *s)
  409. {
  410. acpi_verify_table_checksum = true;
  411. return 0;
  412. }
  413. early_param("acpi_force_table_verification", acpi_force_table_verification_setup);
  414. static int __init acpi_force_32bit_fadt_addr(char *s)
  415. {
  416. pr_info("Forcing 32 Bit FADT addresses\n");
  417. acpi_gbl_use32_bit_fadt_addresses = TRUE;
  418. return 0;
  419. }
  420. early_param("acpi_force_32bit_fadt_addr", acpi_force_32bit_fadt_addr);