acpi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * ARM64 Specific Low-Level ACPI Boot Support
  3. *
  4. * Copyright (C) 2013-2014, Linaro Ltd.
  5. * Author: Al Stone <al.stone@linaro.org>
  6. * Author: Graeme Gregory <graeme.gregory@linaro.org>
  7. * Author: Hanjun Guo <hanjun.guo@linaro.org>
  8. * Author: Tomasz Nowicki <tomasz.nowicki@linaro.org>
  9. * Author: Naresh Bhat <naresh.bhat@linaro.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #define pr_fmt(fmt) "ACPI: " fmt
  16. #include <linux/acpi.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/init.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/memblock.h>
  23. #include <linux/of_fdt.h>
  24. #include <linux/smp.h>
  25. #include <asm/cputype.h>
  26. #include <asm/cpu_ops.h>
  27. #include <asm/smp_plat.h>
  28. int acpi_noirq = 1; /* skip ACPI IRQ initialization */
  29. int acpi_disabled = 1;
  30. EXPORT_SYMBOL(acpi_disabled);
  31. int acpi_pci_disabled = 1; /* skip ACPI PCI scan and IRQ initialization */
  32. EXPORT_SYMBOL(acpi_pci_disabled);
  33. /* Processors with enabled flag and sane MPIDR */
  34. static int enabled_cpus;
  35. /* Boot CPU is valid or not in MADT */
  36. static bool bootcpu_valid __initdata;
  37. static bool param_acpi_off __initdata;
  38. static bool param_acpi_force __initdata;
  39. static int __init parse_acpi(char *arg)
  40. {
  41. if (!arg)
  42. return -EINVAL;
  43. /* "acpi=off" disables both ACPI table parsing and interpreter */
  44. if (strcmp(arg, "off") == 0)
  45. param_acpi_off = true;
  46. else if (strcmp(arg, "force") == 0) /* force ACPI to be enabled */
  47. param_acpi_force = true;
  48. else
  49. return -EINVAL; /* Core will print when we return error */
  50. return 0;
  51. }
  52. early_param("acpi", parse_acpi);
  53. static int __init dt_scan_depth1_nodes(unsigned long node,
  54. const char *uname, int depth,
  55. void *data)
  56. {
  57. /*
  58. * Return 1 as soon as we encounter a node at depth 1 that is
  59. * not the /chosen node.
  60. */
  61. if (depth == 1 && (strcmp(uname, "chosen") != 0))
  62. return 1;
  63. return 0;
  64. }
  65. /*
  66. * __acpi_map_table() will be called before page_init(), so early_ioremap()
  67. * or early_memremap() should be called here to for ACPI table mapping.
  68. */
  69. char *__init __acpi_map_table(unsigned long phys, unsigned long size)
  70. {
  71. if (!size)
  72. return NULL;
  73. return early_memremap(phys, size);
  74. }
  75. void __init __acpi_unmap_table(char *map, unsigned long size)
  76. {
  77. if (!map || !size)
  78. return;
  79. early_memunmap(map, size);
  80. }
  81. /**
  82. * acpi_map_gic_cpu_interface - generates a logical cpu number
  83. * and map to MPIDR represented by GICC structure
  84. */
  85. static void __init
  86. acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt *processor)
  87. {
  88. int i;
  89. u64 mpidr = processor->arm_mpidr & MPIDR_HWID_BITMASK;
  90. bool enabled = !!(processor->flags & ACPI_MADT_ENABLED);
  91. if (mpidr == INVALID_HWID) {
  92. pr_info("Skip MADT cpu entry with invalid MPIDR\n");
  93. return;
  94. }
  95. total_cpus++;
  96. if (!enabled)
  97. return;
  98. if (enabled_cpus >= NR_CPUS) {
  99. pr_warn("NR_CPUS limit of %d reached, Processor %d/0x%llx ignored.\n",
  100. NR_CPUS, total_cpus, mpidr);
  101. return;
  102. }
  103. /* Check if GICC structure of boot CPU is available in the MADT */
  104. if (cpu_logical_map(0) == mpidr) {
  105. if (bootcpu_valid) {
  106. pr_err("Firmware bug, duplicate CPU MPIDR: 0x%llx in MADT\n",
  107. mpidr);
  108. return;
  109. }
  110. bootcpu_valid = true;
  111. }
  112. /*
  113. * Duplicate MPIDRs are a recipe for disaster. Scan
  114. * all initialized entries and check for
  115. * duplicates. If any is found just ignore the CPU.
  116. */
  117. for (i = 1; i < enabled_cpus; i++) {
  118. if (cpu_logical_map(i) == mpidr) {
  119. pr_err("Firmware bug, duplicate CPU MPIDR: 0x%llx in MADT\n",
  120. mpidr);
  121. return;
  122. }
  123. }
  124. if (!acpi_psci_present())
  125. return;
  126. cpu_ops[enabled_cpus] = cpu_get_ops("psci");
  127. /* CPU 0 was already initialized */
  128. if (enabled_cpus) {
  129. if (!cpu_ops[enabled_cpus])
  130. return;
  131. if (cpu_ops[enabled_cpus]->cpu_init(NULL, enabled_cpus))
  132. return;
  133. /* map the logical cpu id to cpu MPIDR */
  134. cpu_logical_map(enabled_cpus) = mpidr;
  135. }
  136. enabled_cpus++;
  137. }
  138. static int __init
  139. acpi_parse_gic_cpu_interface(struct acpi_subtable_header *header,
  140. const unsigned long end)
  141. {
  142. struct acpi_madt_generic_interrupt *processor;
  143. processor = (struct acpi_madt_generic_interrupt *)header;
  144. if (BAD_MADT_ENTRY(processor, end))
  145. return -EINVAL;
  146. acpi_table_print_madt_entry(header);
  147. acpi_map_gic_cpu_interface(processor);
  148. return 0;
  149. }
  150. /* Parse GIC cpu interface entries in MADT for SMP init */
  151. void __init acpi_init_cpus(void)
  152. {
  153. int count, i;
  154. /*
  155. * do a partial walk of MADT to determine how many CPUs
  156. * we have including disabled CPUs, and get information
  157. * we need for SMP init
  158. */
  159. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  160. acpi_parse_gic_cpu_interface, 0);
  161. if (!count) {
  162. pr_err("No GIC CPU interface entries present\n");
  163. return;
  164. } else if (count < 0) {
  165. pr_err("Error parsing GIC CPU interface entry\n");
  166. return;
  167. }
  168. if (!bootcpu_valid) {
  169. pr_err("MADT missing boot CPU MPIDR, not enabling secondaries\n");
  170. return;
  171. }
  172. for (i = 0; i < enabled_cpus; i++)
  173. set_cpu_possible(i, true);
  174. /* Make boot-up look pretty */
  175. pr_info("%d CPUs enabled, %d CPUs total\n", enabled_cpus, total_cpus);
  176. }
  177. /*
  178. * acpi_fadt_sanity_check() - Check FADT presence and carry out sanity
  179. * checks on it
  180. *
  181. * Return 0 on success, <0 on failure
  182. */
  183. static int __init acpi_fadt_sanity_check(void)
  184. {
  185. struct acpi_table_header *table;
  186. struct acpi_table_fadt *fadt;
  187. acpi_status status;
  188. acpi_size tbl_size;
  189. int ret = 0;
  190. /*
  191. * FADT is required on arm64; retrieve it to check its presence
  192. * and carry out revision and ACPI HW reduced compliancy tests
  193. */
  194. status = acpi_get_table_with_size(ACPI_SIG_FADT, 0, &table, &tbl_size);
  195. if (ACPI_FAILURE(status)) {
  196. const char *msg = acpi_format_exception(status);
  197. pr_err("Failed to get FADT table, %s\n", msg);
  198. return -ENODEV;
  199. }
  200. fadt = (struct acpi_table_fadt *)table;
  201. /*
  202. * Revision in table header is the FADT Major revision, and there
  203. * is a minor revision of FADT which was introduced by ACPI 5.1,
  204. * we only deal with ACPI 5.1 or newer revision to get GIC and SMP
  205. * boot protocol configuration data.
  206. */
  207. if (table->revision < 5 ||
  208. (table->revision == 5 && fadt->minor_revision < 1)) {
  209. pr_err("Unsupported FADT revision %d.%d, should be 5.1+\n",
  210. table->revision, fadt->minor_revision);
  211. ret = -EINVAL;
  212. goto out;
  213. }
  214. if (!(fadt->flags & ACPI_FADT_HW_REDUCED)) {
  215. pr_err("FADT not ACPI hardware reduced compliant\n");
  216. ret = -EINVAL;
  217. }
  218. out:
  219. /*
  220. * acpi_get_table_with_size() creates FADT table mapping that
  221. * should be released after parsing and before resuming boot
  222. */
  223. early_acpi_os_unmap_memory(table, tbl_size);
  224. return ret;
  225. }
  226. /*
  227. * acpi_boot_table_init() called from setup_arch(), always.
  228. * 1. find RSDP and get its address, and then find XSDT
  229. * 2. extract all tables and checksums them all
  230. * 3. check ACPI FADT revision
  231. * 4. check ACPI FADT HW reduced flag
  232. *
  233. * We can parse ACPI boot-time tables such as MADT after
  234. * this function is called.
  235. *
  236. * On return ACPI is enabled if either:
  237. *
  238. * - ACPI tables are initialized and sanity checks passed
  239. * - acpi=force was passed in the command line and ACPI was not disabled
  240. * explicitly through acpi=off command line parameter
  241. *
  242. * ACPI is disabled on function return otherwise
  243. */
  244. void __init acpi_boot_table_init(void)
  245. {
  246. /*
  247. * Enable ACPI instead of device tree unless
  248. * - ACPI has been disabled explicitly (acpi=off), or
  249. * - the device tree is not empty (it has more than just a /chosen node)
  250. * and ACPI has not been force enabled (acpi=force)
  251. */
  252. if (param_acpi_off ||
  253. (!param_acpi_force && of_scan_flat_dt(dt_scan_depth1_nodes, NULL)))
  254. return;
  255. /*
  256. * ACPI is disabled at this point. Enable it in order to parse
  257. * the ACPI tables and carry out sanity checks
  258. */
  259. enable_acpi();
  260. /*
  261. * If ACPI tables are initialized and FADT sanity checks passed,
  262. * leave ACPI enabled and carry on booting; otherwise disable ACPI
  263. * on initialization error.
  264. * If acpi=force was passed on the command line it forces ACPI
  265. * to be enabled even if its initialization failed.
  266. */
  267. if (acpi_table_init() || acpi_fadt_sanity_check()) {
  268. pr_err("Failed to init ACPI tables\n");
  269. if (!param_acpi_force)
  270. disable_acpi();
  271. }
  272. }
  273. void __init acpi_gic_init(void)
  274. {
  275. struct acpi_table_header *table;
  276. acpi_status status;
  277. acpi_size tbl_size;
  278. int err;
  279. if (acpi_disabled)
  280. return;
  281. status = acpi_get_table_with_size(ACPI_SIG_MADT, 0, &table, &tbl_size);
  282. if (ACPI_FAILURE(status)) {
  283. const char *msg = acpi_format_exception(status);
  284. pr_err("Failed to get MADT table, %s\n", msg);
  285. return;
  286. }
  287. err = gic_v2_acpi_init(table);
  288. if (err)
  289. pr_err("Failed to initialize GIC IRQ controller");
  290. early_acpi_os_unmap_memory((char *)table, tbl_size);
  291. }