tables.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #define pr_fmt(fmt) "ACPI: " fmt
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/smp.h>
  29. #include <linux/string.h>
  30. #include <linux/types.h>
  31. #include <linux/irq.h>
  32. #include <linux/errno.h>
  33. #include <linux/acpi.h>
  34. #include <linux/bootmem.h>
  35. #define ACPI_MAX_TABLES 128
  36. static char *mps_inti_flags_polarity[] = { "dfl", "high", "res", "low" };
  37. static char *mps_inti_flags_trigger[] = { "dfl", "edge", "res", "level" };
  38. static struct acpi_table_desc initial_tables[ACPI_MAX_TABLES] __initdata;
  39. static int acpi_apic_instance __initdata;
  40. void acpi_table_print_madt_entry(struct acpi_subtable_header *header)
  41. {
  42. if (!header)
  43. return;
  44. switch (header->type) {
  45. case ACPI_MADT_TYPE_LOCAL_APIC:
  46. {
  47. struct acpi_madt_local_apic *p =
  48. (struct acpi_madt_local_apic *)header;
  49. pr_info("LAPIC (acpi_id[0x%02x] lapic_id[0x%02x] %s)\n",
  50. p->processor_id, p->id,
  51. (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  52. }
  53. break;
  54. case ACPI_MADT_TYPE_LOCAL_X2APIC:
  55. {
  56. struct acpi_madt_local_x2apic *p =
  57. (struct acpi_madt_local_x2apic *)header;
  58. pr_info("X2APIC (apic_id[0x%02x] uid[0x%02x] %s)\n",
  59. p->local_apic_id, p->uid,
  60. (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  61. }
  62. break;
  63. case ACPI_MADT_TYPE_IO_APIC:
  64. {
  65. struct acpi_madt_io_apic *p =
  66. (struct acpi_madt_io_apic *)header;
  67. pr_info("IOAPIC (id[0x%02x] address[0x%08x] gsi_base[%d])\n",
  68. p->id, p->address, p->global_irq_base);
  69. }
  70. break;
  71. case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
  72. {
  73. struct acpi_madt_interrupt_override *p =
  74. (struct acpi_madt_interrupt_override *)header;
  75. pr_info("INT_SRC_OVR (bus %d bus_irq %d global_irq %d %s %s)\n",
  76. p->bus, p->source_irq, p->global_irq,
  77. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
  78. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2]);
  79. if (p->inti_flags &
  80. ~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK))
  81. pr_info("INT_SRC_OVR unexpected reserved flags: 0x%x\n",
  82. p->inti_flags &
  83. ~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK));
  84. }
  85. break;
  86. case ACPI_MADT_TYPE_NMI_SOURCE:
  87. {
  88. struct acpi_madt_nmi_source *p =
  89. (struct acpi_madt_nmi_source *)header;
  90. pr_info("NMI_SRC (%s %s global_irq %d)\n",
  91. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
  92. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
  93. p->global_irq);
  94. }
  95. break;
  96. case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
  97. {
  98. struct acpi_madt_local_apic_nmi *p =
  99. (struct acpi_madt_local_apic_nmi *)header;
  100. pr_info("LAPIC_NMI (acpi_id[0x%02x] %s %s lint[0x%x])\n",
  101. p->processor_id,
  102. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK ],
  103. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
  104. p->lint);
  105. }
  106. break;
  107. case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
  108. {
  109. u16 polarity, trigger;
  110. struct acpi_madt_local_x2apic_nmi *p =
  111. (struct acpi_madt_local_x2apic_nmi *)header;
  112. polarity = p->inti_flags & ACPI_MADT_POLARITY_MASK;
  113. trigger = (p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
  114. pr_info("X2APIC_NMI (uid[0x%02x] %s %s lint[0x%x])\n",
  115. p->uid,
  116. mps_inti_flags_polarity[polarity],
  117. mps_inti_flags_trigger[trigger],
  118. p->lint);
  119. }
  120. break;
  121. case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
  122. {
  123. struct acpi_madt_local_apic_override *p =
  124. (struct acpi_madt_local_apic_override *)header;
  125. pr_info("LAPIC_ADDR_OVR (address[%p])\n",
  126. (void *)(unsigned long)p->address);
  127. }
  128. break;
  129. case ACPI_MADT_TYPE_IO_SAPIC:
  130. {
  131. struct acpi_madt_io_sapic *p =
  132. (struct acpi_madt_io_sapic *)header;
  133. pr_info("IOSAPIC (id[0x%x] address[%p] gsi_base[%d])\n",
  134. p->id, (void *)(unsigned long)p->address,
  135. p->global_irq_base);
  136. }
  137. break;
  138. case ACPI_MADT_TYPE_LOCAL_SAPIC:
  139. {
  140. struct acpi_madt_local_sapic *p =
  141. (struct acpi_madt_local_sapic *)header;
  142. pr_info("LSAPIC (acpi_id[0x%02x] lsapic_id[0x%02x] lsapic_eid[0x%02x] %s)\n",
  143. p->processor_id, p->id, p->eid,
  144. (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
  145. }
  146. break;
  147. case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
  148. {
  149. struct acpi_madt_interrupt_source *p =
  150. (struct acpi_madt_interrupt_source *)header;
  151. 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",
  152. mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
  153. mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
  154. p->type, p->id, p->eid, p->io_sapic_vector,
  155. p->global_irq);
  156. }
  157. break;
  158. default:
  159. pr_warn("Found unsupported MADT entry (type = 0x%x)\n",
  160. header->type);
  161. break;
  162. }
  163. }
  164. int __init
  165. acpi_table_parse_entries(char *id,
  166. unsigned long table_size,
  167. int entry_id,
  168. acpi_tbl_entry_handler handler,
  169. unsigned int max_entries)
  170. {
  171. struct acpi_table_header *table_header = NULL;
  172. struct acpi_subtable_header *entry;
  173. unsigned int count = 0;
  174. unsigned long table_end;
  175. acpi_size tbl_size;
  176. if (acpi_disabled)
  177. return -ENODEV;
  178. if (!handler)
  179. return -EINVAL;
  180. if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
  181. acpi_get_table_with_size(id, acpi_apic_instance, &table_header, &tbl_size);
  182. else
  183. acpi_get_table_with_size(id, 0, &table_header, &tbl_size);
  184. if (!table_header) {
  185. pr_warn("%4.4s not present\n", id);
  186. return -ENODEV;
  187. }
  188. table_end = (unsigned long)table_header + table_header->length;
  189. /* Parse all entries looking for a match. */
  190. entry = (struct acpi_subtable_header *)
  191. ((unsigned long)table_header + table_size);
  192. while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
  193. table_end) {
  194. if (entry->type == entry_id
  195. && (!max_entries || count++ < max_entries))
  196. if (handler(entry, table_end))
  197. goto err;
  198. /*
  199. * If entry->length is 0, break from this loop to avoid
  200. * infinite loop.
  201. */
  202. if (entry->length == 0) {
  203. pr_err("[%4.4s:0x%02x] Invalid zero length\n", id, entry_id);
  204. goto err;
  205. }
  206. entry = (struct acpi_subtable_header *)
  207. ((unsigned long)entry + entry->length);
  208. }
  209. if (max_entries && count > max_entries) {
  210. pr_warn("[%4.4s:0x%02x] ignored %i entries of %i found\n",
  211. id, entry_id, count - max_entries, count);
  212. }
  213. early_acpi_os_unmap_memory((char *)table_header, tbl_size);
  214. return count;
  215. err:
  216. early_acpi_os_unmap_memory((char *)table_header, tbl_size);
  217. return -EINVAL;
  218. }
  219. int __init
  220. acpi_table_parse_madt(enum acpi_madt_type id,
  221. acpi_tbl_entry_handler handler, unsigned int max_entries)
  222. {
  223. return acpi_table_parse_entries(ACPI_SIG_MADT,
  224. sizeof(struct acpi_table_madt), id,
  225. handler, max_entries);
  226. }
  227. /**
  228. * acpi_table_parse - find table with @id, run @handler on it
  229. * @id: table id to find
  230. * @handler: handler to run
  231. *
  232. * Scan the ACPI System Descriptor Table (STD) for a table matching @id,
  233. * run @handler on it.
  234. *
  235. * Return 0 if table found, -errno if not.
  236. */
  237. int __init acpi_table_parse(char *id, acpi_tbl_table_handler handler)
  238. {
  239. struct acpi_table_header *table = NULL;
  240. acpi_size tbl_size;
  241. if (acpi_disabled)
  242. return -ENODEV;
  243. if (!id || !handler)
  244. return -EINVAL;
  245. if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
  246. acpi_get_table_with_size(id, acpi_apic_instance, &table, &tbl_size);
  247. else
  248. acpi_get_table_with_size(id, 0, &table, &tbl_size);
  249. if (table) {
  250. handler(table);
  251. early_acpi_os_unmap_memory(table, tbl_size);
  252. return 0;
  253. } else
  254. return -ENODEV;
  255. }
  256. /*
  257. * The BIOS is supposed to supply a single APIC/MADT,
  258. * but some report two. Provide a knob to use either.
  259. * (don't you wish instance 0 and 1 were not the same?)
  260. */
  261. static void __init check_multiple_madt(void)
  262. {
  263. struct acpi_table_header *table = NULL;
  264. acpi_size tbl_size;
  265. acpi_get_table_with_size(ACPI_SIG_MADT, 2, &table, &tbl_size);
  266. if (table) {
  267. pr_warn("BIOS bug: multiple APIC/MADT found, using %d\n",
  268. acpi_apic_instance);
  269. pr_warn("If \"acpi_apic_instance=%d\" works better, "
  270. "notify linux-acpi@vger.kernel.org\n",
  271. acpi_apic_instance ? 0 : 2);
  272. early_acpi_os_unmap_memory(table, tbl_size);
  273. } else
  274. acpi_apic_instance = 0;
  275. return;
  276. }
  277. /*
  278. * acpi_table_init()
  279. *
  280. * find RSDP, find and checksum SDT/XSDT.
  281. * checksum all tables, print SDT/XSDT
  282. *
  283. * result: sdt_entry[] is initialized
  284. */
  285. int __init acpi_table_init(void)
  286. {
  287. acpi_status status;
  288. status = acpi_initialize_tables(initial_tables, ACPI_MAX_TABLES, 0);
  289. if (ACPI_FAILURE(status))
  290. return -EINVAL;
  291. check_multiple_madt();
  292. return 0;
  293. }
  294. static int __init acpi_parse_apic_instance(char *str)
  295. {
  296. if (!str)
  297. return -EINVAL;
  298. acpi_apic_instance = simple_strtoul(str, NULL, 0);
  299. pr_notice("Shall use APIC/MADT table %d\n", acpi_apic_instance);
  300. return 0;
  301. }
  302. early_param("acpi_apic_instance", acpi_parse_apic_instance);