acpi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * acpi.c - Architecture-Specific Low-Level ACPI Boot Support
  4. *
  5. * Author: Jianmin Lv <lvjianmin@loongson.cn>
  6. * Huacai Chen <chenhuacai@loongson.cn>
  7. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  8. */
  9. #include <linux/init.h>
  10. #include <linux/acpi.h>
  11. #include <linux/efi-bgrt.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/memblock.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/serial_core.h>
  17. #include <asm/io.h>
  18. #include <asm/numa.h>
  19. #include <asm/loongson.h>
  20. int acpi_disabled;
  21. EXPORT_SYMBOL(acpi_disabled);
  22. int acpi_noirq;
  23. int acpi_pci_disabled;
  24. EXPORT_SYMBOL(acpi_pci_disabled);
  25. int acpi_strict = 1; /* We have no workarounds on LoongArch */
  26. int num_processors;
  27. int disabled_cpus;
  28. u64 acpi_saved_sp;
  29. #define PREFIX "ACPI: "
  30. struct acpi_madt_core_pic acpi_core_pic[MAX_CORE_PIC];
  31. void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size)
  32. {
  33. if (!phys || !size)
  34. return NULL;
  35. return early_memremap(phys, size);
  36. }
  37. void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
  38. {
  39. if (!map || !size)
  40. return;
  41. early_memunmap(map, size);
  42. }
  43. void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
  44. {
  45. if (!memblock_is_memory(phys))
  46. return ioremap(phys, size);
  47. else
  48. return ioremap_cache(phys, size);
  49. }
  50. #ifdef CONFIG_SMP
  51. static int set_processor_mask(u32 id, u32 pass)
  52. {
  53. int cpu = -1, cpuid = id;
  54. if (num_processors >= NR_CPUS) {
  55. pr_warn(PREFIX "nr_cpus limit of %i reached."
  56. " processor 0x%x ignored.\n", NR_CPUS, cpuid);
  57. return -ENODEV;
  58. }
  59. if (cpuid == loongson_sysconf.boot_cpu_id)
  60. cpu = 0;
  61. switch (pass) {
  62. case 1: /* Pass 1 handle enabled processors */
  63. if (cpu < 0)
  64. cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
  65. num_processors++;
  66. set_cpu_present(cpu, true);
  67. break;
  68. case 2: /* Pass 2 handle disabled processors */
  69. if (cpu < 0)
  70. cpu = find_first_zero_bit(cpumask_bits(cpu_possible_mask), NR_CPUS);
  71. disabled_cpus++;
  72. break;
  73. default:
  74. return cpu;
  75. }
  76. set_cpu_possible(cpu, true);
  77. __cpu_number_map[cpuid] = cpu;
  78. __cpu_logical_map[cpu] = cpuid;
  79. return cpu;
  80. }
  81. #endif
  82. static int __init
  83. acpi_parse_p1_processor(union acpi_subtable_headers *header, const unsigned long end)
  84. {
  85. struct acpi_madt_core_pic *processor = NULL;
  86. processor = (struct acpi_madt_core_pic *)header;
  87. if (BAD_MADT_ENTRY(processor, end))
  88. return -EINVAL;
  89. acpi_table_print_madt_entry(&header->common);
  90. #ifdef CONFIG_SMP
  91. acpi_core_pic[processor->core_id] = *processor;
  92. if (processor->flags & ACPI_MADT_ENABLED)
  93. set_processor_mask(processor->core_id, 1);
  94. #endif
  95. return 0;
  96. }
  97. static int __init
  98. acpi_parse_p2_processor(union acpi_subtable_headers *header, const unsigned long end)
  99. {
  100. struct acpi_madt_core_pic *processor = NULL;
  101. processor = (struct acpi_madt_core_pic *)header;
  102. if (BAD_MADT_ENTRY(processor, end))
  103. return -EINVAL;
  104. #ifdef CONFIG_SMP
  105. if (!(processor->flags & ACPI_MADT_ENABLED))
  106. set_processor_mask(processor->core_id, 2);
  107. #endif
  108. return 0;
  109. }
  110. static int __init
  111. acpi_parse_eio_master(union acpi_subtable_headers *header, const unsigned long end)
  112. {
  113. static int core = 0;
  114. struct acpi_madt_eio_pic *eiointc = NULL;
  115. eiointc = (struct acpi_madt_eio_pic *)header;
  116. if (BAD_MADT_ENTRY(eiointc, end))
  117. return -EINVAL;
  118. core = eiointc->node * CORES_PER_EIO_NODE;
  119. set_bit(core, loongson_sysconf.cores_io_master);
  120. return 0;
  121. }
  122. static void __init acpi_process_madt(void)
  123. {
  124. #ifdef CONFIG_SMP
  125. int i;
  126. for (i = 0; i < NR_CPUS; i++) {
  127. __cpu_number_map[i] = -1;
  128. __cpu_logical_map[i] = -1;
  129. }
  130. #endif
  131. acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC,
  132. acpi_parse_p1_processor, MAX_CORE_PIC);
  133. acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC,
  134. acpi_parse_p2_processor, MAX_CORE_PIC);
  135. acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC,
  136. acpi_parse_eio_master, MAX_IO_PICS);
  137. loongson_sysconf.nr_cpus = num_processors;
  138. }
  139. int pptt_enabled;
  140. int __init parse_acpi_topology(void)
  141. {
  142. int cpu, topology_id;
  143. for_each_possible_cpu(cpu) {
  144. topology_id = find_acpi_cpu_topology(cpu, 0);
  145. if (topology_id < 0) {
  146. pr_warn("Invalid BIOS PPTT\n");
  147. return -ENOENT;
  148. }
  149. if (acpi_pptt_cpu_is_thread(cpu) <= 0)
  150. cpu_data[cpu].core = topology_id;
  151. else {
  152. topology_id = find_acpi_cpu_topology(cpu, 1);
  153. if (topology_id < 0)
  154. return -ENOENT;
  155. cpu_data[cpu].core = topology_id;
  156. }
  157. }
  158. pptt_enabled = 1;
  159. return 0;
  160. }
  161. #ifndef CONFIG_SUSPEND
  162. int (*acpi_suspend_lowlevel)(void);
  163. #else
  164. int (*acpi_suspend_lowlevel)(void) = loongarch_acpi_suspend;
  165. #endif
  166. void __init acpi_boot_table_init(void)
  167. {
  168. /*
  169. * If acpi_disabled, bail out
  170. */
  171. if (acpi_disabled)
  172. goto fdt_earlycon;
  173. /*
  174. * Initialize the ACPI boot-time table parser.
  175. */
  176. if (acpi_table_init()) {
  177. disable_acpi();
  178. goto fdt_earlycon;
  179. }
  180. loongson_sysconf.boot_cpu_id = read_csr_cpuid();
  181. /*
  182. * Process the Multiple APIC Description Table (MADT), if present
  183. */
  184. acpi_process_madt();
  185. /* Do not enable ACPI SPCR console by default */
  186. acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
  187. if (IS_ENABLED(CONFIG_ACPI_BGRT))
  188. acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);
  189. return;
  190. fdt_earlycon:
  191. if (earlycon_acpi_spcr_enable)
  192. early_init_dt_scan_chosen_stdout();
  193. }
  194. #ifdef CONFIG_ACPI_NUMA
  195. static __init int setup_node(int pxm)
  196. {
  197. return acpi_map_pxm_to_node(pxm);
  198. }
  199. void __init numa_set_distance(int from, int to, int distance)
  200. {
  201. if ((u8)distance != distance || (from == to && distance != LOCAL_DISTANCE)) {
  202. pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
  203. from, to, distance);
  204. return;
  205. }
  206. node_distances[from][to] = distance;
  207. }
  208. /* Callback for Proximity Domain -> CPUID mapping */
  209. void __init
  210. acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
  211. {
  212. int pxm, node;
  213. if (srat_disabled())
  214. return;
  215. if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
  216. bad_srat();
  217. return;
  218. }
  219. if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
  220. return;
  221. pxm = pa->proximity_domain_lo;
  222. if (acpi_srat_revision >= 2) {
  223. pxm |= (pa->proximity_domain_hi[0] << 8);
  224. pxm |= (pa->proximity_domain_hi[1] << 16);
  225. pxm |= (pa->proximity_domain_hi[2] << 24);
  226. }
  227. node = setup_node(pxm);
  228. if (node < 0) {
  229. pr_err("SRAT: Too many proximity domains %x\n", pxm);
  230. bad_srat();
  231. return;
  232. }
  233. if (pa->apic_id >= CONFIG_NR_CPUS) {
  234. pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
  235. pxm, pa->apic_id, node);
  236. return;
  237. }
  238. early_numa_add_cpu(pa->apic_id, node);
  239. set_cpuid_to_node(pa->apic_id, node);
  240. node_set(node, numa_nodes_parsed);
  241. pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
  242. }
  243. #endif
  244. void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
  245. {
  246. memblock_reserve(addr, size);
  247. }
  248. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  249. #include <acpi/processor.h>
  250. static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid)
  251. {
  252. #ifdef CONFIG_ACPI_NUMA
  253. int nid;
  254. nid = acpi_get_node(handle);
  255. if (nid != NUMA_NO_NODE)
  256. nid = early_cpu_to_node(cpu);
  257. if (nid != NUMA_NO_NODE) {
  258. set_cpuid_to_node(physid, nid);
  259. node_set(nid, numa_nodes_parsed);
  260. set_cpu_numa_node(cpu, nid);
  261. cpumask_set_cpu(cpu, cpumask_of_node(nid));
  262. }
  263. #endif
  264. return 0;
  265. }
  266. int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu)
  267. {
  268. int cpu;
  269. cpu = cpu_number_map(physid);
  270. if (cpu < 0 || cpu >= nr_cpu_ids) {
  271. pr_info(PREFIX "Unable to map lapic to logical cpu number\n");
  272. return -ERANGE;
  273. }
  274. num_processors++;
  275. set_cpu_present(cpu, true);
  276. acpi_map_cpu2node(handle, cpu, physid);
  277. *pcpu = cpu;
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(acpi_map_cpu);
  281. int acpi_unmap_cpu(int cpu)
  282. {
  283. #ifdef CONFIG_ACPI_NUMA
  284. set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE);
  285. #endif
  286. set_cpu_present(cpu, false);
  287. num_processors--;
  288. pr_info("cpu%d hot remove!\n", cpu);
  289. return 0;
  290. }
  291. EXPORT_SYMBOL(acpi_unmap_cpu);
  292. #endif /* CONFIG_ACPI_HOTPLUG_CPU */