tables.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. */
  25. /* Uncomment next line to get verbose printout */
  26. /* #define DEBUG */
  27. #define pr_fmt(fmt) "ACPI: " fmt
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/smp.h>
  31. #include <linux/string.h>
  32. #include <linux/types.h>
  33. #include <linux/irq.h>
  34. #include <linux/errno.h>
  35. #include <linux/acpi.h>
  36. #include <linux/bootmem.h>
  37. #define ACPI_MAX_TABLES 128
  38. static char *mps_inti_flags_polarity[] = { "dfl", "high", "res", "low" };
  39. static char *mps_inti_flags_trigger[] = { "dfl", "edge", "res", "level" };
  40. static struct acpi_table_desc initial_tables[ACPI_MAX_TABLES] __initdata;
  41. static int acpi_apic_instance __initdata;
  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 = 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[%p])\n",
  133. (void *)(unsigned long)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. int __init
  191. acpi_parse_entries(char *id, unsigned long table_size,
  192. acpi_tbl_entry_handler handler,
  193. struct acpi_table_header *table_header,
  194. int entry_id, unsigned int max_entries)
  195. {
  196. struct acpi_subtable_header *entry;
  197. int count = 0;
  198. unsigned long table_end;
  199. if (acpi_disabled)
  200. return -ENODEV;
  201. if (!id || !handler)
  202. return -EINVAL;
  203. if (!table_size)
  204. return -EINVAL;
  205. if (!table_header) {
  206. pr_warn("%4.4s not present\n", id);
  207. return -ENODEV;
  208. }
  209. table_end = (unsigned long)table_header + table_header->length;
  210. /* Parse all entries looking for a match. */
  211. entry = (struct acpi_subtable_header *)
  212. ((unsigned long)table_header + table_size);
  213. while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
  214. table_end) {
  215. if (entry->type == entry_id
  216. && (!max_entries || count < max_entries)) {
  217. if (handler(entry, table_end))
  218. return -EINVAL;
  219. count++;
  220. }
  221. /*
  222. * If entry->length is 0, break from this loop to avoid
  223. * infinite loop.
  224. */
  225. if (entry->length == 0) {
  226. pr_err("[%4.4s:0x%02x] Invalid zero length\n", id, entry_id);
  227. return -EINVAL;
  228. }
  229. entry = (struct acpi_subtable_header *)
  230. ((unsigned long)entry + entry->length);
  231. }
  232. if (max_entries && count > max_entries) {
  233. pr_warn("[%4.4s:0x%02x] ignored %i entries of %i found\n",
  234. id, entry_id, count - max_entries, count);
  235. }
  236. return count;
  237. }
  238. int __init
  239. acpi_table_parse_entries(char *id,
  240. unsigned long table_size,
  241. int entry_id,
  242. acpi_tbl_entry_handler handler,
  243. unsigned int max_entries)
  244. {
  245. struct acpi_table_header *table_header = NULL;
  246. acpi_size tbl_size;
  247. int count;
  248. u32 instance = 0;
  249. if (acpi_disabled)
  250. return -ENODEV;
  251. if (!id || !handler)
  252. return -EINVAL;
  253. if (!strncmp(id, ACPI_SIG_MADT, 4))
  254. instance = acpi_apic_instance;
  255. acpi_get_table_with_size(id, instance, &table_header, &tbl_size);
  256. if (!table_header) {
  257. pr_warn("%4.4s not present\n", id);
  258. return -ENODEV;
  259. }
  260. count = acpi_parse_entries(id, table_size, handler, table_header,
  261. entry_id, max_entries);
  262. early_acpi_os_unmap_memory((char *)table_header, tbl_size);
  263. return count;
  264. }
  265. int __init
  266. acpi_table_parse_madt(enum acpi_madt_type id,
  267. acpi_tbl_entry_handler handler, unsigned int max_entries)
  268. {
  269. return acpi_table_parse_entries(ACPI_SIG_MADT,
  270. sizeof(struct acpi_table_madt), id,
  271. handler, max_entries);
  272. }
  273. /**
  274. * acpi_table_parse - find table with @id, run @handler on it
  275. * @id: table id to find
  276. * @handler: handler to run
  277. *
  278. * Scan the ACPI System Descriptor Table (STD) for a table matching @id,
  279. * run @handler on it.
  280. *
  281. * Return 0 if table found, -errno if not.
  282. */
  283. int __init acpi_table_parse(char *id, acpi_tbl_table_handler handler)
  284. {
  285. struct acpi_table_header *table = NULL;
  286. acpi_size tbl_size;
  287. if (acpi_disabled)
  288. return -ENODEV;
  289. if (!id || !handler)
  290. return -EINVAL;
  291. if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
  292. acpi_get_table_with_size(id, acpi_apic_instance, &table, &tbl_size);
  293. else
  294. acpi_get_table_with_size(id, 0, &table, &tbl_size);
  295. if (table) {
  296. handler(table);
  297. early_acpi_os_unmap_memory(table, tbl_size);
  298. return 0;
  299. } else
  300. return -ENODEV;
  301. }
  302. /*
  303. * The BIOS is supposed to supply a single APIC/MADT,
  304. * but some report two. Provide a knob to use either.
  305. * (don't you wish instance 0 and 1 were not the same?)
  306. */
  307. static void __init check_multiple_madt(void)
  308. {
  309. struct acpi_table_header *table = NULL;
  310. acpi_size tbl_size;
  311. acpi_get_table_with_size(ACPI_SIG_MADT, 2, &table, &tbl_size);
  312. if (table) {
  313. pr_warn("BIOS bug: multiple APIC/MADT found, using %d\n",
  314. acpi_apic_instance);
  315. pr_warn("If \"acpi_apic_instance=%d\" works better, "
  316. "notify linux-acpi@vger.kernel.org\n",
  317. acpi_apic_instance ? 0 : 2);
  318. early_acpi_os_unmap_memory(table, tbl_size);
  319. } else
  320. acpi_apic_instance = 0;
  321. return;
  322. }
  323. /*
  324. * acpi_table_init()
  325. *
  326. * find RSDP, find and checksum SDT/XSDT.
  327. * checksum all tables, print SDT/XSDT
  328. *
  329. * result: sdt_entry[] is initialized
  330. */
  331. int __init acpi_table_init(void)
  332. {
  333. acpi_status status;
  334. if (acpi_verify_table_checksum) {
  335. pr_info("Early table checksum verification enabled\n");
  336. acpi_gbl_verify_table_checksum = TRUE;
  337. } else {
  338. pr_info("Early table checksum verification disabled\n");
  339. acpi_gbl_verify_table_checksum = FALSE;
  340. }
  341. status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
  342. if (ACPI_FAILURE(status))
  343. return -EINVAL;
  344. check_multiple_madt();
  345. return 0;
  346. }
  347. static int __init acpi_parse_apic_instance(char *str)
  348. {
  349. if (!str)
  350. return -EINVAL;
  351. if (kstrtoint(str, 0, &acpi_apic_instance))
  352. return -EINVAL;
  353. pr_notice("Shall use APIC/MADT table %d\n", acpi_apic_instance);
  354. return 0;
  355. }
  356. early_param("acpi_apic_instance", acpi_parse_apic_instance);
  357. static int __init acpi_force_table_verification_setup(char *s)
  358. {
  359. acpi_verify_table_checksum = true;
  360. return 0;
  361. }
  362. early_param("acpi_force_table_verification", acpi_force_table_verification_setup);