processor_core.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C) 2005 Intel Corporation
  3. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  4. *
  5. * Alex Chiang <achiang@hp.com>
  6. * - Unified x86/ia64 implementations
  7. *
  8. * I/O APIC hotplug support
  9. * Yinghai Lu <yinghai@kernel.org>
  10. * Jiang Liu <jiang.liu@intel.com>
  11. */
  12. #include <linux/export.h>
  13. #include <linux/acpi.h>
  14. #include <acpi/processor.h>
  15. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  16. ACPI_MODULE_NAME("processor_core");
  17. static struct acpi_table_madt *get_madt_table(void)
  18. {
  19. static struct acpi_table_madt *madt;
  20. static int read_madt;
  21. if (!read_madt) {
  22. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  23. (struct acpi_table_header **)&madt)))
  24. madt = NULL;
  25. read_madt++;
  26. }
  27. return madt;
  28. }
  29. static int map_lapic_id(struct acpi_subtable_header *entry,
  30. u32 acpi_id, phys_cpuid_t *apic_id)
  31. {
  32. struct acpi_madt_local_apic *lapic =
  33. container_of(entry, struct acpi_madt_local_apic, header);
  34. if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
  35. return -ENODEV;
  36. if (lapic->processor_id != acpi_id)
  37. return -EINVAL;
  38. *apic_id = lapic->id;
  39. return 0;
  40. }
  41. static int map_x2apic_id(struct acpi_subtable_header *entry,
  42. int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
  43. {
  44. struct acpi_madt_local_x2apic *apic =
  45. container_of(entry, struct acpi_madt_local_x2apic, header);
  46. if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
  47. return -ENODEV;
  48. if (device_declaration && (apic->uid == acpi_id)) {
  49. *apic_id = apic->local_apic_id;
  50. return 0;
  51. }
  52. return -EINVAL;
  53. }
  54. static int map_lsapic_id(struct acpi_subtable_header *entry,
  55. int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
  56. {
  57. struct acpi_madt_local_sapic *lsapic =
  58. container_of(entry, struct acpi_madt_local_sapic, header);
  59. if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
  60. return -ENODEV;
  61. if (device_declaration) {
  62. if ((entry->length < 16) || (lsapic->uid != acpi_id))
  63. return -EINVAL;
  64. } else if (lsapic->processor_id != acpi_id)
  65. return -EINVAL;
  66. *apic_id = (lsapic->id << 8) | lsapic->eid;
  67. return 0;
  68. }
  69. /*
  70. * Retrieve the ARM CPU physical identifier (MPIDR)
  71. */
  72. static int map_gicc_mpidr(struct acpi_subtable_header *entry,
  73. int device_declaration, u32 acpi_id, phys_cpuid_t *mpidr)
  74. {
  75. struct acpi_madt_generic_interrupt *gicc =
  76. container_of(entry, struct acpi_madt_generic_interrupt, header);
  77. if (!(gicc->flags & ACPI_MADT_ENABLED))
  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. static phys_cpuid_t map_madt_entry(int type, u32 acpi_id)
  91. {
  92. unsigned long madt_end, entry;
  93. phys_cpuid_t phys_id = PHYS_CPUID_INVALID; /* CPU hardware ID */
  94. struct acpi_table_madt *madt;
  95. madt = get_madt_table();
  96. if (!madt)
  97. return phys_id;
  98. entry = (unsigned long)madt;
  99. madt_end = entry + madt->header.length;
  100. /* Parse all entries looking for a match. */
  101. entry += sizeof(struct acpi_table_madt);
  102. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  103. struct acpi_subtable_header *header =
  104. (struct acpi_subtable_header *)entry;
  105. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  106. if (!map_lapic_id(header, acpi_id, &phys_id))
  107. break;
  108. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  109. if (!map_x2apic_id(header, type, acpi_id, &phys_id))
  110. break;
  111. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  112. if (!map_lsapic_id(header, type, acpi_id, &phys_id))
  113. break;
  114. } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
  115. if (!map_gicc_mpidr(header, type, acpi_id, &phys_id))
  116. break;
  117. }
  118. entry += header->length;
  119. }
  120. return phys_id;
  121. }
  122. static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
  123. {
  124. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  125. union acpi_object *obj;
  126. struct acpi_subtable_header *header;
  127. phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
  128. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  129. goto exit;
  130. if (!buffer.length || !buffer.pointer)
  131. goto exit;
  132. obj = buffer.pointer;
  133. if (obj->type != ACPI_TYPE_BUFFER ||
  134. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  135. goto exit;
  136. }
  137. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  138. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
  139. map_lapic_id(header, acpi_id, &phys_id);
  140. else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
  141. map_lsapic_id(header, type, acpi_id, &phys_id);
  142. else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
  143. map_x2apic_id(header, type, acpi_id, &phys_id);
  144. else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
  145. map_gicc_mpidr(header, type, acpi_id, &phys_id);
  146. exit:
  147. kfree(buffer.pointer);
  148. return phys_id;
  149. }
  150. phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
  151. {
  152. phys_cpuid_t phys_id;
  153. phys_id = map_mat_entry(handle, type, acpi_id);
  154. if (invalid_phys_cpuid(phys_id))
  155. phys_id = map_madt_entry(type, acpi_id);
  156. return phys_id;
  157. }
  158. int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
  159. {
  160. #ifdef CONFIG_SMP
  161. int i;
  162. #endif
  163. if (invalid_phys_cpuid(phys_id)) {
  164. /*
  165. * On UP processor, there is no _MAT or MADT table.
  166. * So above phys_id is always set to PHYS_CPUID_INVALID.
  167. *
  168. * BIOS may define multiple CPU handles even for UP processor.
  169. * For example,
  170. *
  171. * Scope (_PR)
  172. * {
  173. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  174. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  175. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  176. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  177. * }
  178. *
  179. * Ignores phys_id and always returns 0 for the processor
  180. * handle with acpi id 0 if nr_cpu_ids is 1.
  181. * This should be the case if SMP tables are not found.
  182. * Return -EINVAL for other CPU's handle.
  183. */
  184. if (nr_cpu_ids <= 1 && acpi_id == 0)
  185. return acpi_id;
  186. else
  187. return -EINVAL;
  188. }
  189. #ifdef CONFIG_SMP
  190. for_each_possible_cpu(i) {
  191. if (cpu_physical_id(i) == phys_id)
  192. return i;
  193. }
  194. #else
  195. /* In UP kernel, only processor 0 is valid */
  196. if (phys_id == 0)
  197. return phys_id;
  198. #endif
  199. return -ENODEV;
  200. }
  201. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  202. {
  203. phys_cpuid_t phys_id;
  204. phys_id = acpi_get_phys_id(handle, type, acpi_id);
  205. return acpi_map_cpuid(phys_id, acpi_id);
  206. }
  207. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  208. #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
  209. static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
  210. u64 *phys_addr, int *ioapic_id)
  211. {
  212. struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
  213. if (ioapic->global_irq_base != gsi_base)
  214. return 0;
  215. *phys_addr = ioapic->address;
  216. *ioapic_id = ioapic->id;
  217. return 1;
  218. }
  219. static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
  220. {
  221. struct acpi_subtable_header *hdr;
  222. unsigned long madt_end, entry;
  223. struct acpi_table_madt *madt;
  224. int apic_id = -1;
  225. madt = get_madt_table();
  226. if (!madt)
  227. return apic_id;
  228. entry = (unsigned long)madt;
  229. madt_end = entry + madt->header.length;
  230. /* Parse all entries looking for a match. */
  231. entry += sizeof(struct acpi_table_madt);
  232. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  233. hdr = (struct acpi_subtable_header *)entry;
  234. if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
  235. get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
  236. break;
  237. else
  238. entry += hdr->length;
  239. }
  240. return apic_id;
  241. }
  242. static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
  243. u64 *phys_addr)
  244. {
  245. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  246. struct acpi_subtable_header *header;
  247. union acpi_object *obj;
  248. int apic_id = -1;
  249. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  250. goto exit;
  251. if (!buffer.length || !buffer.pointer)
  252. goto exit;
  253. obj = buffer.pointer;
  254. if (obj->type != ACPI_TYPE_BUFFER ||
  255. obj->buffer.length < sizeof(struct acpi_subtable_header))
  256. goto exit;
  257. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  258. if (header->type == ACPI_MADT_TYPE_IO_APIC)
  259. get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
  260. exit:
  261. kfree(buffer.pointer);
  262. return apic_id;
  263. }
  264. /**
  265. * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
  266. * @handle: ACPI object for IOAPIC device
  267. * @gsi_base: GSI base to match with
  268. * @phys_addr: Pointer to store physical address of matching IOAPIC record
  269. *
  270. * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
  271. * for an ACPI IOAPIC record matching @gsi_base.
  272. * Return IOAPIC id and store physical address in @phys_addr if found a match,
  273. * otherwise return <0.
  274. */
  275. int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
  276. {
  277. int apic_id;
  278. apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
  279. if (apic_id == -1)
  280. apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
  281. return apic_id;
  282. }
  283. #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */