processor_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005 Intel Corporation
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. *
  6. * Alex Chiang <achiang@hp.com>
  7. * - Unified x86/ia64 implementations
  8. *
  9. * I/O APIC hotplug support
  10. * Yinghai Lu <yinghai@kernel.org>
  11. * Jiang Liu <jiang.liu@intel.com>
  12. */
  13. #include <linux/export.h>
  14. #include <linux/acpi.h>
  15. #include <acpi/processor.h>
  16. static struct acpi_table_madt *get_madt_table(void)
  17. {
  18. static struct acpi_table_madt *madt;
  19. static int read_madt;
  20. if (!read_madt) {
  21. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  22. (struct acpi_table_header **)&madt)))
  23. madt = NULL;
  24. read_madt++;
  25. }
  26. return madt;
  27. }
  28. static int map_lapic_id(struct acpi_subtable_header *entry,
  29. u32 acpi_id, phys_cpuid_t *apic_id)
  30. {
  31. struct acpi_madt_local_apic *lapic =
  32. container_of(entry, struct acpi_madt_local_apic, header);
  33. if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
  34. return -ENODEV;
  35. if (lapic->processor_id != acpi_id)
  36. return -EINVAL;
  37. *apic_id = lapic->id;
  38. return 0;
  39. }
  40. static int map_x2apic_id(struct acpi_subtable_header *entry,
  41. int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
  42. {
  43. struct acpi_madt_local_x2apic *apic =
  44. container_of(entry, struct acpi_madt_local_x2apic, header);
  45. if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
  46. return -ENODEV;
  47. if (apic->uid == acpi_id && (device_declaration || acpi_id < 255)) {
  48. *apic_id = apic->local_apic_id;
  49. return 0;
  50. }
  51. return -EINVAL;
  52. }
  53. static int map_lsapic_id(struct acpi_subtable_header *entry,
  54. int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
  55. {
  56. struct acpi_madt_local_sapic *lsapic =
  57. container_of(entry, struct acpi_madt_local_sapic, header);
  58. if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
  59. return -ENODEV;
  60. if (device_declaration) {
  61. if ((entry->length < 16) || (lsapic->uid != acpi_id))
  62. return -EINVAL;
  63. } else if (lsapic->processor_id != acpi_id)
  64. return -EINVAL;
  65. *apic_id = (lsapic->id << 8) | lsapic->eid;
  66. return 0;
  67. }
  68. /*
  69. * Retrieve the ARM CPU physical identifier (MPIDR)
  70. */
  71. static int map_gicc_mpidr(struct acpi_subtable_header *entry,
  72. int device_declaration, u32 acpi_id, phys_cpuid_t *mpidr)
  73. {
  74. struct acpi_madt_generic_interrupt *gicc =
  75. container_of(entry, struct acpi_madt_generic_interrupt, header);
  76. if (!(gicc->flags &
  77. (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE)))
  78. return -ENODEV;
  79. /* device_declaration means Device object in DSDT, in the
  80. * GIC interrupt model, logical processors are required to
  81. * have a Processor Device object in the DSDT, so we should
  82. * check device_declaration here
  83. */
  84. if (device_declaration && (gicc->uid == acpi_id)) {
  85. *mpidr = gicc->arm_mpidr;
  86. return 0;
  87. }
  88. return -EINVAL;
  89. }
  90. /*
  91. * Retrieve the RISC-V hartid for the processor
  92. */
  93. static int map_rintc_hartid(struct acpi_subtable_header *entry,
  94. int device_declaration, u32 acpi_id,
  95. phys_cpuid_t *hartid)
  96. {
  97. struct acpi_madt_rintc *rintc =
  98. container_of(entry, struct acpi_madt_rintc, header);
  99. if (!(rintc->flags & ACPI_MADT_ENABLED))
  100. return -ENODEV;
  101. /* device_declaration means Device object in DSDT, in the
  102. * RISC-V, logical processors are required to
  103. * have a Processor Device object in the DSDT, so we should
  104. * check device_declaration here
  105. */
  106. if (device_declaration && rintc->uid == acpi_id) {
  107. *hartid = rintc->hart_id;
  108. return 0;
  109. }
  110. return -EINVAL;
  111. }
  112. /*
  113. * Retrieve LoongArch CPU physical id
  114. */
  115. static int map_core_pic_id(struct acpi_subtable_header *entry,
  116. int device_declaration, u32 acpi_id, phys_cpuid_t *phys_id)
  117. {
  118. struct acpi_madt_core_pic *core_pic =
  119. container_of(entry, struct acpi_madt_core_pic, header);
  120. if (!(core_pic->flags & ACPI_MADT_ENABLED))
  121. return -ENODEV;
  122. /* device_declaration means Device object in DSDT, in LoongArch
  123. * system, logical processor acpi_id is required in _UID property
  124. * of DSDT table, so we should check device_declaration here
  125. */
  126. if (device_declaration && (core_pic->processor_id == acpi_id)) {
  127. *phys_id = core_pic->core_id;
  128. return 0;
  129. }
  130. return -EINVAL;
  131. }
  132. static phys_cpuid_t map_madt_entry(struct acpi_table_madt *madt,
  133. int type, u32 acpi_id)
  134. {
  135. unsigned long madt_end, entry;
  136. phys_cpuid_t phys_id = PHYS_CPUID_INVALID; /* CPU hardware ID */
  137. if (!madt)
  138. return phys_id;
  139. entry = (unsigned long)madt;
  140. madt_end = entry + madt->header.length;
  141. /* Parse all entries looking for a match. */
  142. entry += sizeof(struct acpi_table_madt);
  143. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  144. struct acpi_subtable_header *header =
  145. (struct acpi_subtable_header *)entry;
  146. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  147. if (!map_lapic_id(header, acpi_id, &phys_id))
  148. break;
  149. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  150. if (!map_x2apic_id(header, type, acpi_id, &phys_id))
  151. break;
  152. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  153. if (!map_lsapic_id(header, type, acpi_id, &phys_id))
  154. break;
  155. } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
  156. if (!map_gicc_mpidr(header, type, acpi_id, &phys_id))
  157. break;
  158. } else if (header->type == ACPI_MADT_TYPE_RINTC) {
  159. if (!map_rintc_hartid(header, type, acpi_id, &phys_id))
  160. break;
  161. } else if (header->type == ACPI_MADT_TYPE_CORE_PIC) {
  162. if (!map_core_pic_id(header, type, acpi_id, &phys_id))
  163. break;
  164. }
  165. entry += header->length;
  166. }
  167. return phys_id;
  168. }
  169. phys_cpuid_t __init acpi_map_madt_entry(u32 acpi_id)
  170. {
  171. struct acpi_table_madt *madt = NULL;
  172. phys_cpuid_t rv;
  173. acpi_get_table(ACPI_SIG_MADT, 0,
  174. (struct acpi_table_header **)&madt);
  175. if (!madt)
  176. return PHYS_CPUID_INVALID;
  177. rv = map_madt_entry(madt, 1, acpi_id);
  178. acpi_put_table((struct acpi_table_header *)madt);
  179. return rv;
  180. }
  181. int __init acpi_get_madt_revision(void)
  182. {
  183. struct acpi_table_header *madt = NULL;
  184. int revision;
  185. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0, &madt)))
  186. return -EINVAL;
  187. revision = madt->revision;
  188. acpi_put_table(madt);
  189. return revision;
  190. }
  191. static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
  192. {
  193. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  194. union acpi_object *obj;
  195. struct acpi_subtable_header *header;
  196. phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
  197. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  198. goto exit;
  199. if (!buffer.length || !buffer.pointer)
  200. goto exit;
  201. obj = buffer.pointer;
  202. if (obj->type != ACPI_TYPE_BUFFER ||
  203. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  204. goto exit;
  205. }
  206. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  207. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
  208. map_lapic_id(header, acpi_id, &phys_id);
  209. else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
  210. map_lsapic_id(header, type, acpi_id, &phys_id);
  211. else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
  212. map_x2apic_id(header, type, acpi_id, &phys_id);
  213. else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
  214. map_gicc_mpidr(header, type, acpi_id, &phys_id);
  215. else if (header->type == ACPI_MADT_TYPE_CORE_PIC)
  216. map_core_pic_id(header, type, acpi_id, &phys_id);
  217. exit:
  218. kfree(buffer.pointer);
  219. return phys_id;
  220. }
  221. phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
  222. {
  223. phys_cpuid_t phys_id;
  224. phys_id = map_mat_entry(handle, type, acpi_id);
  225. if (invalid_phys_cpuid(phys_id))
  226. phys_id = map_madt_entry(get_madt_table(), type, acpi_id);
  227. return phys_id;
  228. }
  229. EXPORT_SYMBOL_GPL(acpi_get_phys_id);
  230. int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
  231. {
  232. #ifdef CONFIG_SMP
  233. int i;
  234. #endif
  235. if (invalid_phys_cpuid(phys_id)) {
  236. /*
  237. * On UP processor, there is no _MAT or MADT table.
  238. * So above phys_id is always set to PHYS_CPUID_INVALID.
  239. *
  240. * BIOS may define multiple CPU handles even for UP processor.
  241. * For example,
  242. *
  243. * Scope (_PR)
  244. * {
  245. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  246. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  247. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  248. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  249. * }
  250. *
  251. * Ignores phys_id and always returns 0 for the processor
  252. * handle with acpi id 0 if nr_cpu_ids is 1.
  253. * This should be the case if SMP tables are not found.
  254. * Return -EINVAL for other CPU's handle.
  255. */
  256. if (nr_cpu_ids <= 1 && acpi_id == 0)
  257. return acpi_id;
  258. else
  259. return -EINVAL;
  260. }
  261. #ifdef CONFIG_SMP
  262. for_each_possible_cpu(i) {
  263. if (cpu_physical_id(i) == phys_id)
  264. return i;
  265. }
  266. #else
  267. /* In UP kernel, only processor 0 is valid */
  268. if (phys_id == 0)
  269. return phys_id;
  270. #endif
  271. return -ENODEV;
  272. }
  273. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  274. {
  275. phys_cpuid_t phys_id;
  276. phys_id = acpi_get_phys_id(handle, type, acpi_id);
  277. return acpi_map_cpuid(phys_id, acpi_id);
  278. }
  279. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  280. #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
  281. static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
  282. u64 *phys_addr, int *ioapic_id)
  283. {
  284. struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
  285. if (ioapic->global_irq_base != gsi_base)
  286. return 0;
  287. *phys_addr = ioapic->address;
  288. *ioapic_id = ioapic->id;
  289. return 1;
  290. }
  291. static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
  292. {
  293. struct acpi_subtable_header *hdr;
  294. unsigned long madt_end, entry;
  295. struct acpi_table_madt *madt;
  296. int apic_id = -1;
  297. madt = get_madt_table();
  298. if (!madt)
  299. return apic_id;
  300. entry = (unsigned long)madt;
  301. madt_end = entry + madt->header.length;
  302. /* Parse all entries looking for a match. */
  303. entry += sizeof(struct acpi_table_madt);
  304. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  305. hdr = (struct acpi_subtable_header *)entry;
  306. if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
  307. get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
  308. break;
  309. else
  310. entry += hdr->length;
  311. }
  312. return apic_id;
  313. }
  314. static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
  315. u64 *phys_addr)
  316. {
  317. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  318. struct acpi_subtable_header *header;
  319. union acpi_object *obj;
  320. int apic_id = -1;
  321. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  322. goto exit;
  323. if (!buffer.length || !buffer.pointer)
  324. goto exit;
  325. obj = buffer.pointer;
  326. if (obj->type != ACPI_TYPE_BUFFER ||
  327. obj->buffer.length < sizeof(struct acpi_subtable_header))
  328. goto exit;
  329. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  330. if (header->type == ACPI_MADT_TYPE_IO_APIC)
  331. get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
  332. exit:
  333. kfree(buffer.pointer);
  334. return apic_id;
  335. }
  336. /**
  337. * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
  338. * @handle: ACPI object for IOAPIC device
  339. * @gsi_base: GSI base to match with
  340. * @phys_addr: Pointer to store physical address of matching IOAPIC record
  341. *
  342. * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
  343. * for an ACPI IOAPIC record matching @gsi_base.
  344. * Return IOAPIC id and store physical address in @phys_addr if found a match,
  345. * otherwise return <0.
  346. */
  347. int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
  348. {
  349. int apic_id;
  350. apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
  351. if (apic_id == -1)
  352. apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
  353. return apic_id;
  354. }
  355. #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */