acpi.c 6.4 KB

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