processor_core.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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(struct acpi_table_madt *madt,
  91. int type, u32 acpi_id)
  92. {
  93. unsigned long madt_end, entry;
  94. phys_cpuid_t phys_id = PHYS_CPUID_INVALID; /* CPU hardware ID */
  95. if (!madt)
  96. return phys_id;
  97. entry = (unsigned long)madt;
  98. madt_end = entry + madt->header.length;
  99. /* Parse all entries looking for a match. */
  100. entry += sizeof(struct acpi_table_madt);
  101. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  102. struct acpi_subtable_header *header =
  103. (struct acpi_subtable_header *)entry;
  104. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  105. if (!map_lapic_id(header, acpi_id, &phys_id))
  106. break;
  107. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  108. if (!map_x2apic_id(header, type, acpi_id, &phys_id))
  109. break;
  110. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  111. if (!map_lsapic_id(header, type, acpi_id, &phys_id))
  112. break;
  113. } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
  114. if (!map_gicc_mpidr(header, type, acpi_id, &phys_id))
  115. break;
  116. }
  117. entry += header->length;
  118. }
  119. return phys_id;
  120. }
  121. phys_cpuid_t __init acpi_map_madt_entry(u32 acpi_id)
  122. {
  123. struct acpi_table_madt *madt = NULL;
  124. phys_cpuid_t rv;
  125. acpi_get_table(ACPI_SIG_MADT, 0,
  126. (struct acpi_table_header **)&madt);
  127. if (!madt)
  128. return PHYS_CPUID_INVALID;
  129. rv = map_madt_entry(madt, 1, acpi_id);
  130. acpi_put_table((struct acpi_table_header *)madt);
  131. return rv;
  132. }
  133. static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
  134. {
  135. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  136. union acpi_object *obj;
  137. struct acpi_subtable_header *header;
  138. phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
  139. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  140. goto exit;
  141. if (!buffer.length || !buffer.pointer)
  142. goto exit;
  143. obj = buffer.pointer;
  144. if (obj->type != ACPI_TYPE_BUFFER ||
  145. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  146. goto exit;
  147. }
  148. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  149. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
  150. map_lapic_id(header, acpi_id, &phys_id);
  151. else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
  152. map_lsapic_id(header, type, acpi_id, &phys_id);
  153. else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
  154. map_x2apic_id(header, type, acpi_id, &phys_id);
  155. else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
  156. map_gicc_mpidr(header, type, acpi_id, &phys_id);
  157. exit:
  158. kfree(buffer.pointer);
  159. return phys_id;
  160. }
  161. phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
  162. {
  163. phys_cpuid_t phys_id;
  164. phys_id = map_mat_entry(handle, type, acpi_id);
  165. if (invalid_phys_cpuid(phys_id))
  166. phys_id = map_madt_entry(get_madt_table(), type, acpi_id);
  167. return phys_id;
  168. }
  169. EXPORT_SYMBOL_GPL(acpi_get_phys_id);
  170. int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
  171. {
  172. #ifdef CONFIG_SMP
  173. int i;
  174. #endif
  175. if (invalid_phys_cpuid(phys_id)) {
  176. /*
  177. * On UP processor, there is no _MAT or MADT table.
  178. * So above phys_id is always set to PHYS_CPUID_INVALID.
  179. *
  180. * BIOS may define multiple CPU handles even for UP processor.
  181. * For example,
  182. *
  183. * Scope (_PR)
  184. * {
  185. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  186. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  187. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  188. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  189. * }
  190. *
  191. * Ignores phys_id and always returns 0 for the processor
  192. * handle with acpi id 0 if nr_cpu_ids is 1.
  193. * This should be the case if SMP tables are not found.
  194. * Return -EINVAL for other CPU's handle.
  195. */
  196. if (nr_cpu_ids <= 1 && acpi_id == 0)
  197. return acpi_id;
  198. else
  199. return -EINVAL;
  200. }
  201. #ifdef CONFIG_SMP
  202. for_each_possible_cpu(i) {
  203. if (cpu_physical_id(i) == phys_id)
  204. return i;
  205. }
  206. #else
  207. /* In UP kernel, only processor 0 is valid */
  208. if (phys_id == 0)
  209. return phys_id;
  210. #endif
  211. return -ENODEV;
  212. }
  213. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  214. {
  215. phys_cpuid_t phys_id;
  216. phys_id = acpi_get_phys_id(handle, type, acpi_id);
  217. return acpi_map_cpuid(phys_id, acpi_id);
  218. }
  219. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  220. #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
  221. static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
  222. u64 *phys_addr, int *ioapic_id)
  223. {
  224. struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
  225. if (ioapic->global_irq_base != gsi_base)
  226. return 0;
  227. *phys_addr = ioapic->address;
  228. *ioapic_id = ioapic->id;
  229. return 1;
  230. }
  231. static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
  232. {
  233. struct acpi_subtable_header *hdr;
  234. unsigned long madt_end, entry;
  235. struct acpi_table_madt *madt;
  236. int apic_id = -1;
  237. madt = get_madt_table();
  238. if (!madt)
  239. return apic_id;
  240. entry = (unsigned long)madt;
  241. madt_end = entry + madt->header.length;
  242. /* Parse all entries looking for a match. */
  243. entry += sizeof(struct acpi_table_madt);
  244. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  245. hdr = (struct acpi_subtable_header *)entry;
  246. if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
  247. get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
  248. break;
  249. else
  250. entry += hdr->length;
  251. }
  252. return apic_id;
  253. }
  254. static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
  255. u64 *phys_addr)
  256. {
  257. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  258. struct acpi_subtable_header *header;
  259. union acpi_object *obj;
  260. int apic_id = -1;
  261. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  262. goto exit;
  263. if (!buffer.length || !buffer.pointer)
  264. goto exit;
  265. obj = buffer.pointer;
  266. if (obj->type != ACPI_TYPE_BUFFER ||
  267. obj->buffer.length < sizeof(struct acpi_subtable_header))
  268. goto exit;
  269. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  270. if (header->type == ACPI_MADT_TYPE_IO_APIC)
  271. get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
  272. exit:
  273. kfree(buffer.pointer);
  274. return apic_id;
  275. }
  276. /**
  277. * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
  278. * @handle: ACPI object for IOAPIC device
  279. * @gsi_base: GSI base to match with
  280. * @phys_addr: Pointer to store physical address of matching IOAPIC record
  281. *
  282. * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
  283. * for an ACPI IOAPIC record matching @gsi_base.
  284. * Return IOAPIC id and store physical address in @phys_addr if found a match,
  285. * otherwise return <0.
  286. */
  287. int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
  288. {
  289. int apic_id;
  290. apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
  291. if (apic_id == -1)
  292. apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
  293. return apic_id;
  294. }
  295. #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */