tables.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #define ACPI_MAX_TABLES 128
  34. static char *mps_inti_flags_polarity[] = { "dfl", "high", "res", "low" };
  35. static char *mps_inti_flags_trigger[] = { "dfl", "edge", "res", "level" };
  36. static struct acpi_table_desc initial_tables[ACPI_MAX_TABLES] __initdata;
  37. static int acpi_apic_instance __initdata;
  38. /*
  39. * Disable table checksum verification for the early stage due to the size
  40. * limitation of the current x86 early mapping implementation.
  41. */
  42. static bool acpi_verify_table_checksum __initdata = false;
  43. void acpi_table_print_madt_entry(struct acpi_subtable_header *header)
  44. {
  45. if (!header)
  46. return;
  47. switch (header->type) {
  48. case ACPI_MADT_TYPE_LOCAL_APIC:
  49. {
  50. struct acpi_madt_local_apic *p =
  51. (struct acpi_madt_local_apic *)header;
  52. pr_debug("LAPIC (acpi_id[0x%02x] lapic_id[0x%02x] %s)\n",
  53. p->processor_id, p->id,
  54. (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  55. }
  56. break;
  57. case ACPI_MADT_TYPE_LOCAL_X2APIC:
  58. {
  59. struct acpi_madt_local_x2apic *p =
  60. (struct acpi_madt_local_x2apic *)header;
  61. pr_debug("X2APIC (apic_id[0x%02x] uid[0x%02x] %s)\n",
  62. p->local_apic_id, p->uid,
  63. (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  64. }
  65. break;
  66. case ACPI_MADT_TYPE_IO_APIC:
  67. {
  68. struct acpi_madt_io_apic *p =
  69. (struct acpi_madt_io_apic *)header;
  70. pr_debug("IOAPIC (id[0x%02x] address[0x%08x] gsi_base[%d])\n",
  71. p->id, p->address, p->global_irq_base);
  72. }
  73. break;
  74. case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
  75. {
  76. struct acpi_madt_interrupt_override *p =
  77. (struct acpi_madt_interrupt_override *)header;
  78. pr_info("INT_SRC_OVR (bus %d bus_irq %d global_irq %d %s %s)\n",
  79. p->bus, p->source_irq, p->global_irq,
  80. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
  81. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2]);
  82. if (p->inti_flags &
  83. ~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK))
  84. pr_info("INT_SRC_OVR unexpected reserved flags: 0x%x\n",
  85. p->inti_flags &
  86. ~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK));
  87. }
  88. break;
  89. case ACPI_MADT_TYPE_NMI_SOURCE:
  90. {
  91. struct acpi_madt_nmi_source *p =
  92. (struct acpi_madt_nmi_source *)header;
  93. pr_info("NMI_SRC (%s %s global_irq %d)\n",
  94. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
  95. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
  96. p->global_irq);
  97. }
  98. break;
  99. case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
  100. {
  101. struct acpi_madt_local_apic_nmi *p =
  102. (struct acpi_madt_local_apic_nmi *)header;
  103. pr_info("LAPIC_NMI (acpi_id[0x%02x] %s %s lint[0x%x])\n",
  104. p->processor_id,
  105. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK ],
  106. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
  107. p->lint);
  108. }
  109. break;
  110. case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
  111. {
  112. u16 polarity, trigger;
  113. struct acpi_madt_local_x2apic_nmi *p =
  114. (struct acpi_madt_local_x2apic_nmi *)header;
  115. polarity = p->inti_flags & ACPI_MADT_POLARITY_MASK;
  116. trigger = (p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
  117. pr_info("X2APIC_NMI (uid[0x%02x] %s %s lint[0x%x])\n",
  118. p->uid,
  119. mps_inti_flags_polarity[polarity],
  120. mps_inti_flags_trigger[trigger],
  121. p->lint);
  122. }
  123. break;
  124. case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
  125. {
  126. struct acpi_madt_local_apic_override *p =
  127. (struct acpi_madt_local_apic_override *)header;
  128. pr_info("LAPIC_ADDR_OVR (address[%p])\n",
  129. (void *)(unsigned long)p->address);
  130. }
  131. break;
  132. case ACPI_MADT_TYPE_IO_SAPIC:
  133. {
  134. struct acpi_madt_io_sapic *p =
  135. (struct acpi_madt_io_sapic *)header;
  136. pr_debug("IOSAPIC (id[0x%x] address[%p] gsi_base[%d])\n",
  137. p->id, (void *)(unsigned long)p->address,
  138. p->global_irq_base);
  139. }
  140. break;
  141. case ACPI_MADT_TYPE_LOCAL_SAPIC:
  142. {
  143. struct acpi_madt_local_sapic *p =
  144. (struct acpi_madt_local_sapic *)header;
  145. pr_debug("LSAPIC (acpi_id[0x%02x] lsapic_id[0x%02x] lsapic_eid[0x%02x] %s)\n",
  146. p->processor_id, p->id, p->eid,
  147. (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  148. }
  149. break;
  150. case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
  151. {
  152. struct acpi_madt_interrupt_source *p =
  153. (struct acpi_madt_interrupt_source *)header;
  154. 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",
  155. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
  156. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
  157. p->type, p->id, p->eid, p->io_sapic_vector,
  158. p->global_irq);
  159. }
  160. break;
  161. case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
  162. {
  163. struct acpi_madt_generic_interrupt *p =
  164. (struct acpi_madt_generic_interrupt *)header;
  165. pr_debug("GICC (acpi_id[0x%04x] address[%llx] MPIDR[0x%llx] %s)\n",
  166. p->uid, p->base_address,
  167. p->arm_mpidr,
  168. (p->flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  169. }
  170. break;
  171. case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
  172. {
  173. struct acpi_madt_generic_distributor *p =
  174. (struct acpi_madt_generic_distributor *)header;
  175. pr_debug("GIC Distributor (gic_id[0x%04x] address[%llx] gsi_base[%d])\n",
  176. p->gic_id, p->base_address,
  177. p->global_irq_base);
  178. }
  179. break;
  180. default:
  181. pr_warn("Found unsupported MADT entry (type = 0x%x)\n",
  182. header->type);
  183. break;
  184. }
  185. }
  186. /**
  187. * acpi_parse_entries_array - for each proc_num find a suitable subtable
  188. *
  189. * @id: table id (for debugging purposes)
  190. * @table_size: single entry size
  191. * @table_header: where does the table start?
  192. * @proc: array of acpi_subtable_proc struct containing entry id
  193. * and associated handler with it
  194. * @proc_num: how big proc is?
  195. * @max_entries: how many entries can we process?
  196. *
  197. * For each proc_num find a subtable with proc->id and run proc->handler
  198. * on it. Assumption is that there's only single handler for particular
  199. * entry id.
  200. *
  201. * On success returns sum of all matching entries for all proc handlers.
  202. * Otherwise, -ENODEV or -EINVAL is returned.
  203. */
  204. static int __init
  205. acpi_parse_entries_array(char *id, unsigned long table_size,
  206. struct acpi_table_header *table_header,
  207. struct acpi_subtable_proc *proc, int proc_num,
  208. unsigned int max_entries)
  209. {
  210. struct acpi_subtable_header *entry;
  211. unsigned long table_end;
  212. int count = 0;
  213. int i;
  214. if (acpi_disabled)
  215. return -ENODEV;
  216. if (!id)
  217. return -EINVAL;
  218. if (!table_size)
  219. return -EINVAL;
  220. if (!table_header) {
  221. pr_warn("%4.4s not present\n", id);
  222. return -ENODEV;
  223. }
  224. table_end = (unsigned long)table_header + table_header->length;
  225. /* Parse all entries looking for a match. */
  226. entry = (struct acpi_subtable_header *)
  227. ((unsigned long)table_header + table_size);
  228. while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
  229. table_end) {
  230. if (max_entries && count >= max_entries)
  231. break;
  232. for (i = 0; i < proc_num; i++) {
  233. if (entry->type != proc[i].id)
  234. continue;
  235. if (!proc[i].handler ||
  236. proc[i].handler(entry, table_end))
  237. return -EINVAL;
  238. proc->count++;
  239. break;
  240. }
  241. if (i != proc_num)
  242. count++;
  243. /*
  244. * If entry->length is 0, break from this loop to avoid
  245. * infinite loop.
  246. */
  247. if (entry->length == 0) {
  248. pr_err("[%4.4s:0x%02x] Invalid zero length\n", id, proc->id);
  249. return -EINVAL;
  250. }
  251. entry = (struct acpi_subtable_header *)
  252. ((unsigned long)entry + entry->length);
  253. }
  254. if (max_entries && count > max_entries) {
  255. pr_warn("[%4.4s:0x%02x] ignored %i entries of %i found\n",
  256. id, proc->id, count - max_entries, count);
  257. }
  258. return count;
  259. }
  260. int __init
  261. acpi_parse_entries(char *id,
  262. unsigned long table_size,
  263. acpi_tbl_entry_handler handler,
  264. struct acpi_table_header *table_header,
  265. int entry_id, unsigned int max_entries)
  266. {
  267. struct acpi_subtable_proc proc = {
  268. .id = entry_id,
  269. .handler = handler,
  270. };
  271. return acpi_parse_entries_array(id, table_size, table_header,
  272. &proc, 1, max_entries);
  273. }
  274. int __init
  275. acpi_table_parse_entries_array(char *id,
  276. unsigned long table_size,
  277. struct acpi_subtable_proc *proc, int proc_num,
  278. unsigned int max_entries)
  279. {
  280. struct acpi_table_header *table_header = NULL;
  281. acpi_size tbl_size;
  282. int count;
  283. u32 instance = 0;
  284. if (acpi_disabled)
  285. return -ENODEV;
  286. if (!id)
  287. return -EINVAL;
  288. if (!strncmp(id, ACPI_SIG_MADT, 4))
  289. instance = acpi_apic_instance;
  290. acpi_get_table_with_size(id, instance, &table_header, &tbl_size);
  291. if (!table_header) {
  292. pr_warn("%4.4s not present\n", id);
  293. return -ENODEV;
  294. }
  295. count = acpi_parse_entries_array(id, table_size, table_header,
  296. proc, proc_num, max_entries);
  297. early_acpi_os_unmap_memory((char *)table_header, tbl_size);
  298. return count;
  299. }
  300. int __init
  301. acpi_table_parse_entries(char *id,
  302. unsigned long table_size,
  303. int entry_id,
  304. acpi_tbl_entry_handler handler,
  305. unsigned int max_entries)
  306. {
  307. struct acpi_subtable_proc proc = {
  308. .id = entry_id,
  309. .handler = handler,
  310. };
  311. return acpi_table_parse_entries_array(id, table_size, &proc, 1,
  312. max_entries);
  313. }
  314. int __init
  315. acpi_table_parse_madt(enum acpi_madt_type id,
  316. acpi_tbl_entry_handler handler, unsigned int max_entries)
  317. {
  318. return acpi_table_parse_entries(ACPI_SIG_MADT,
  319. sizeof(struct acpi_table_madt), id,
  320. handler, max_entries);
  321. }
  322. /**
  323. * acpi_table_parse - find table with @id, run @handler on it
  324. * @id: table id to find
  325. * @handler: handler to run
  326. *
  327. * Scan the ACPI System Descriptor Table (STD) for a table matching @id,
  328. * run @handler on it.
  329. *
  330. * Return 0 if table found, -errno if not.
  331. */
  332. int __init acpi_table_parse(char *id, acpi_tbl_table_handler handler)
  333. {
  334. struct acpi_table_header *table = NULL;
  335. acpi_size tbl_size;
  336. if (acpi_disabled)
  337. return -ENODEV;
  338. if (!id || !handler)
  339. return -EINVAL;
  340. if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
  341. acpi_get_table_with_size(id, acpi_apic_instance, &table, &tbl_size);
  342. else
  343. acpi_get_table_with_size(id, 0, &table, &tbl_size);
  344. if (table) {
  345. handler(table);
  346. early_acpi_os_unmap_memory(table, tbl_size);
  347. return 0;
  348. } else
  349. return -ENODEV;
  350. }
  351. /*
  352. * The BIOS is supposed to supply a single APIC/MADT,
  353. * but some report two. Provide a knob to use either.
  354. * (don't you wish instance 0 and 1 were not the same?)
  355. */
  356. static void __init check_multiple_madt(void)
  357. {
  358. struct acpi_table_header *table = NULL;
  359. acpi_size tbl_size;
  360. acpi_get_table_with_size(ACPI_SIG_MADT, 2, &table, &tbl_size);
  361. if (table) {
  362. pr_warn("BIOS bug: multiple APIC/MADT found, using %d\n",
  363. acpi_apic_instance);
  364. pr_warn("If \"acpi_apic_instance=%d\" works better, "
  365. "notify linux-acpi@vger.kernel.org\n",
  366. acpi_apic_instance ? 0 : 2);
  367. early_acpi_os_unmap_memory(table, tbl_size);
  368. } else
  369. acpi_apic_instance = 0;
  370. return;
  371. }
  372. /*
  373. * acpi_table_init()
  374. *
  375. * find RSDP, find and checksum SDT/XSDT.
  376. * checksum all tables, print SDT/XSDT
  377. *
  378. * result: sdt_entry[] is initialized
  379. */
  380. int __init acpi_table_init(void)
  381. {
  382. acpi_status status;
  383. if (acpi_verify_table_checksum) {
  384. pr_info("Early table checksum verification enabled\n");
  385. acpi_gbl_verify_table_checksum = TRUE;
  386. } else {
  387. pr_info("Early table checksum verification disabled\n");
  388. acpi_gbl_verify_table_checksum = FALSE;
  389. }
  390. status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
  391. if (ACPI_FAILURE(status))
  392. return -EINVAL;
  393. check_multiple_madt();
  394. return 0;
  395. }
  396. static int __init acpi_parse_apic_instance(char *str)
  397. {
  398. if (!str)
  399. return -EINVAL;
  400. if (kstrtoint(str, 0, &acpi_apic_instance))
  401. return -EINVAL;
  402. pr_notice("Shall use APIC/MADT table %d\n", acpi_apic_instance);
  403. return 0;
  404. }
  405. early_param("acpi_apic_instance", acpi_parse_apic_instance);
  406. static int __init acpi_force_table_verification_setup(char *s)
  407. {
  408. acpi_verify_table_checksum = true;
  409. return 0;
  410. }
  411. early_param("acpi_force_table_verification", acpi_force_table_verification_setup);