processor_core.c 7.7 KB

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