tables.c 11 KB

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