acpi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define BOOT_CTYPE_H
  3. #include "misc.h"
  4. #include "error.h"
  5. #include "../string.h"
  6. #include "efi.h"
  7. #include <linux/numa.h>
  8. /*
  9. * Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0'
  10. * for termination.
  11. */
  12. #define MAX_ACPI_ARG_LENGTH 10
  13. /*
  14. * Immovable memory regions representation. Max amount of memory regions is
  15. * MAX_NUMNODES*2.
  16. */
  17. struct mem_vector immovable_mem[MAX_NUMNODES*2];
  18. static acpi_physical_address
  19. __efi_get_rsdp_addr(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len)
  20. {
  21. #ifdef CONFIG_EFI
  22. unsigned long rsdp_addr;
  23. int ret;
  24. /*
  25. * Search EFI system tables for RSDP. Preferred is ACPI_20_TABLE_GUID to
  26. * ACPI_TABLE_GUID because it has more features.
  27. */
  28. rsdp_addr = efi_find_vendor_table(boot_params, cfg_tbl_pa, cfg_tbl_len,
  29. ACPI_20_TABLE_GUID);
  30. if (rsdp_addr)
  31. return (acpi_physical_address)rsdp_addr;
  32. /* No ACPI_20_TABLE_GUID found, fallback to ACPI_TABLE_GUID. */
  33. rsdp_addr = efi_find_vendor_table(boot_params, cfg_tbl_pa, cfg_tbl_len,
  34. ACPI_TABLE_GUID);
  35. if (rsdp_addr)
  36. return (acpi_physical_address)rsdp_addr;
  37. debug_putstr("Error getting RSDP address.\n");
  38. #endif
  39. return 0;
  40. }
  41. static acpi_physical_address efi_get_rsdp_addr(void)
  42. {
  43. #ifdef CONFIG_EFI
  44. unsigned long cfg_tbl_pa = 0;
  45. unsigned int cfg_tbl_len;
  46. unsigned long systab_pa;
  47. unsigned int nr_tables;
  48. enum efi_type et;
  49. int ret;
  50. et = efi_get_type(boot_params);
  51. if (et == EFI_TYPE_NONE)
  52. return 0;
  53. systab_pa = efi_get_system_table(boot_params);
  54. if (!systab_pa)
  55. error("EFI support advertised, but unable to locate system table.");
  56. ret = efi_get_conf_table(boot_params, &cfg_tbl_pa, &cfg_tbl_len);
  57. if (ret || !cfg_tbl_pa)
  58. error("EFI config table not found.");
  59. return __efi_get_rsdp_addr(cfg_tbl_pa, cfg_tbl_len);
  60. #else
  61. return 0;
  62. #endif
  63. }
  64. static u8 compute_checksum(u8 *buffer, u32 length)
  65. {
  66. u8 *end = buffer + length;
  67. u8 sum = 0;
  68. while (buffer < end)
  69. sum += *(buffer++);
  70. return sum;
  71. }
  72. /* Search a block of memory for the RSDP signature. */
  73. static u8 *scan_mem_for_rsdp(u8 *start, u32 length)
  74. {
  75. struct acpi_table_rsdp *rsdp;
  76. u8 *address, *end;
  77. end = start + length;
  78. /* Search from given start address for the requested length */
  79. for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) {
  80. /*
  81. * Both RSDP signature and checksum must be correct.
  82. * Note: Sometimes there exists more than one RSDP in memory;
  83. * the valid RSDP has a valid checksum, all others have an
  84. * invalid checksum.
  85. */
  86. rsdp = (struct acpi_table_rsdp *)address;
  87. /* BAD Signature */
  88. if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature))
  89. continue;
  90. /* Check the standard checksum */
  91. if (compute_checksum((u8 *)rsdp, ACPI_RSDP_CHECKSUM_LENGTH))
  92. continue;
  93. /* Check extended checksum if table version >= 2 */
  94. if ((rsdp->revision >= 2) &&
  95. (compute_checksum((u8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)))
  96. continue;
  97. /* Signature and checksum valid, we have found a real RSDP */
  98. return address;
  99. }
  100. return NULL;
  101. }
  102. /* Search RSDP address in EBDA. */
  103. static acpi_physical_address bios_get_rsdp_addr(void)
  104. {
  105. unsigned long address;
  106. u8 *rsdp;
  107. /* Get the location of the Extended BIOS Data Area (EBDA) */
  108. address = *(u16 *)ACPI_EBDA_PTR_LOCATION;
  109. address <<= 4;
  110. /*
  111. * Search EBDA paragraphs (EBDA is required to be a minimum of
  112. * 1K length)
  113. */
  114. if (address > 0x400) {
  115. rsdp = scan_mem_for_rsdp((u8 *)address, ACPI_EBDA_WINDOW_SIZE);
  116. if (rsdp)
  117. return (acpi_physical_address)(unsigned long)rsdp;
  118. }
  119. /* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */
  120. rsdp = scan_mem_for_rsdp((u8 *) ACPI_HI_RSDP_WINDOW_BASE,
  121. ACPI_HI_RSDP_WINDOW_SIZE);
  122. if (rsdp)
  123. return (acpi_physical_address)(unsigned long)rsdp;
  124. return 0;
  125. }
  126. /* Return RSDP address on success, otherwise 0. */
  127. acpi_physical_address get_rsdp_addr(void)
  128. {
  129. acpi_physical_address pa;
  130. pa = boot_params->acpi_rsdp_addr;
  131. if (!pa)
  132. pa = efi_get_rsdp_addr();
  133. if (!pa)
  134. pa = bios_get_rsdp_addr();
  135. return pa;
  136. }
  137. #if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE)
  138. /*
  139. * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex
  140. * digits, and '\0' for termination.
  141. */
  142. #define MAX_ADDR_LEN 19
  143. static unsigned long get_cmdline_acpi_rsdp(void)
  144. {
  145. unsigned long addr = 0;
  146. #ifdef CONFIG_KEXEC
  147. char val[MAX_ADDR_LEN] = { };
  148. int ret;
  149. ret = cmdline_find_option("acpi_rsdp", val, MAX_ADDR_LEN);
  150. if (ret < 0)
  151. return 0;
  152. if (boot_kstrtoul(val, 16, &addr))
  153. return 0;
  154. #endif
  155. return addr;
  156. }
  157. /* Compute SRAT address from RSDP. */
  158. static unsigned long get_acpi_srat_table(void)
  159. {
  160. unsigned long root_table, acpi_table;
  161. struct acpi_table_header *header;
  162. struct acpi_table_rsdp *rsdp;
  163. u32 num_entries, size, len;
  164. char arg[10];
  165. u8 *entry;
  166. /*
  167. * Check whether we were given an RSDP on the command line. We don't
  168. * stash this in boot params because the kernel itself may have
  169. * different ideas about whether to trust a command-line parameter.
  170. */
  171. rsdp = (struct acpi_table_rsdp *)get_cmdline_acpi_rsdp();
  172. if (!rsdp)
  173. rsdp = (struct acpi_table_rsdp *)(long)
  174. boot_params->acpi_rsdp_addr;
  175. if (!rsdp)
  176. return 0;
  177. /* Get ACPI root table from RSDP.*/
  178. if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 &&
  179. !strncmp(arg, "rsdt", 4)) &&
  180. rsdp->xsdt_physical_address &&
  181. rsdp->revision > 1) {
  182. root_table = rsdp->xsdt_physical_address;
  183. size = ACPI_XSDT_ENTRY_SIZE;
  184. } else {
  185. root_table = rsdp->rsdt_physical_address;
  186. size = ACPI_RSDT_ENTRY_SIZE;
  187. }
  188. if (!root_table)
  189. return 0;
  190. header = (struct acpi_table_header *)root_table;
  191. len = header->length;
  192. if (len < sizeof(struct acpi_table_header) + size)
  193. return 0;
  194. num_entries = (len - sizeof(struct acpi_table_header)) / size;
  195. entry = (u8 *)(root_table + sizeof(struct acpi_table_header));
  196. while (num_entries--) {
  197. if (size == ACPI_RSDT_ENTRY_SIZE)
  198. acpi_table = *(u32 *)entry;
  199. else
  200. acpi_table = *(u64 *)entry;
  201. if (acpi_table) {
  202. header = (struct acpi_table_header *)acpi_table;
  203. if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT))
  204. return acpi_table;
  205. }
  206. entry += size;
  207. }
  208. return 0;
  209. }
  210. /**
  211. * count_immovable_mem_regions - Parse SRAT and cache the immovable
  212. * memory regions into the immovable_mem array.
  213. *
  214. * Return the number of immovable memory regions on success, 0 on failure:
  215. *
  216. * - Too many immovable memory regions
  217. * - ACPI off or no SRAT found
  218. * - No immovable memory region found.
  219. */
  220. int count_immovable_mem_regions(void)
  221. {
  222. unsigned long table_addr, table_end, table;
  223. struct acpi_subtable_header *sub_table;
  224. struct acpi_table_header *table_header;
  225. char arg[MAX_ACPI_ARG_LENGTH];
  226. int num = 0;
  227. if (cmdline_find_option("acpi", arg, sizeof(arg)) == 3 &&
  228. !strncmp(arg, "off", 3))
  229. return 0;
  230. table_addr = get_acpi_srat_table();
  231. if (!table_addr)
  232. return 0;
  233. table_header = (struct acpi_table_header *)table_addr;
  234. table_end = table_addr + table_header->length;
  235. table = table_addr + sizeof(struct acpi_table_srat);
  236. while (table + sizeof(struct acpi_subtable_header) < table_end) {
  237. sub_table = (struct acpi_subtable_header *)table;
  238. if (!sub_table->length) {
  239. debug_putstr("Invalid zero length SRAT subtable.\n");
  240. return 0;
  241. }
  242. if (sub_table->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) {
  243. struct acpi_srat_mem_affinity *ma;
  244. ma = (struct acpi_srat_mem_affinity *)sub_table;
  245. if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && ma->length) {
  246. immovable_mem[num].start = ma->base_address;
  247. immovable_mem[num].size = ma->length;
  248. num++;
  249. }
  250. if (num >= MAX_NUMNODES*2) {
  251. debug_putstr("Too many immovable memory regions, aborting.\n");
  252. return 0;
  253. }
  254. }
  255. table += sub_table->length;
  256. }
  257. return num;
  258. }
  259. #endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */