processor_core.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
  170. {
  171. #ifdef CONFIG_SMP
  172. int i;
  173. #endif
  174. if (invalid_phys_cpuid(phys_id)) {
  175. /*
  176. * On UP processor, there is no _MAT or MADT table.
  177. * So above phys_id is always set to PHYS_CPUID_INVALID.
  178. *
  179. * BIOS may define multiple CPU handles even for UP processor.
  180. * For example,
  181. *
  182. * Scope (_PR)
  183. * {
  184. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  185. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  186. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  187. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  188. * }
  189. *
  190. * Ignores phys_id and always returns 0 for the processor
  191. * handle with acpi id 0 if nr_cpu_ids is 1.
  192. * This should be the case if SMP tables are not found.
  193. * Return -EINVAL for other CPU's handle.
  194. */
  195. if (nr_cpu_ids <= 1 && acpi_id == 0)
  196. return acpi_id;
  197. else
  198. return -EINVAL;
  199. }
  200. #ifdef CONFIG_SMP
  201. for_each_possible_cpu(i) {
  202. if (cpu_physical_id(i) == phys_id)
  203. return i;
  204. }
  205. #else
  206. /* In UP kernel, only processor 0 is valid */
  207. if (phys_id == 0)
  208. return phys_id;
  209. #endif
  210. return -ENODEV;
  211. }
  212. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  213. {
  214. phys_cpuid_t phys_id;
  215. phys_id = acpi_get_phys_id(handle, type, acpi_id);
  216. return acpi_map_cpuid(phys_id, acpi_id);
  217. }
  218. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  219. #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
  220. static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
  221. u64 *phys_addr, int *ioapic_id)
  222. {
  223. struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
  224. if (ioapic->global_irq_base != gsi_base)
  225. return 0;
  226. *phys_addr = ioapic->address;
  227. *ioapic_id = ioapic->id;
  228. return 1;
  229. }
  230. static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
  231. {
  232. struct acpi_subtable_header *hdr;
  233. unsigned long madt_end, entry;
  234. struct acpi_table_madt *madt;
  235. int apic_id = -1;
  236. madt = get_madt_table();
  237. if (!madt)
  238. return apic_id;
  239. entry = (unsigned long)madt;
  240. madt_end = entry + madt->header.length;
  241. /* Parse all entries looking for a match. */
  242. entry += sizeof(struct acpi_table_madt);
  243. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  244. hdr = (struct acpi_subtable_header *)entry;
  245. if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
  246. get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
  247. break;
  248. else
  249. entry += hdr->length;
  250. }
  251. return apic_id;
  252. }
  253. static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
  254. u64 *phys_addr)
  255. {
  256. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  257. struct acpi_subtable_header *header;
  258. union acpi_object *obj;
  259. int apic_id = -1;
  260. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  261. goto exit;
  262. if (!buffer.length || !buffer.pointer)
  263. goto exit;
  264. obj = buffer.pointer;
  265. if (obj->type != ACPI_TYPE_BUFFER ||
  266. obj->buffer.length < sizeof(struct acpi_subtable_header))
  267. goto exit;
  268. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  269. if (header->type == ACPI_MADT_TYPE_IO_APIC)
  270. get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
  271. exit:
  272. kfree(buffer.pointer);
  273. return apic_id;
  274. }
  275. /**
  276. * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
  277. * @handle: ACPI object for IOAPIC device
  278. * @gsi_base: GSI base to match with
  279. * @phys_addr: Pointer to store physical address of matching IOAPIC record
  280. *
  281. * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
  282. * for an ACPI IOAPIC record matching @gsi_base.
  283. * Return IOAPIC id and store physical address in @phys_addr if found a match,
  284. * otherwise return <0.
  285. */
  286. int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
  287. {
  288. int apic_id;
  289. apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
  290. if (apic_id == -1)
  291. apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
  292. return apic_id;
  293. }
  294. #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */