processor_core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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, bool ignore_disabled)
  31. {
  32. struct acpi_madt_local_apic *lapic =
  33. container_of(entry, struct acpi_madt_local_apic, header);
  34. if (ignore_disabled && !(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. bool ignore_disabled)
  44. {
  45. struct acpi_madt_local_x2apic *apic =
  46. container_of(entry, struct acpi_madt_local_x2apic, header);
  47. if (ignore_disabled && !(apic->lapic_flags & ACPI_MADT_ENABLED))
  48. return -ENODEV;
  49. if (device_declaration && (apic->uid == acpi_id)) {
  50. *apic_id = apic->local_apic_id;
  51. return 0;
  52. }
  53. return -EINVAL;
  54. }
  55. static int map_lsapic_id(struct acpi_subtable_header *entry,
  56. int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id,
  57. bool ignore_disabled)
  58. {
  59. struct acpi_madt_local_sapic *lsapic =
  60. container_of(entry, struct acpi_madt_local_sapic, header);
  61. if (ignore_disabled && !(lsapic->lapic_flags & ACPI_MADT_ENABLED))
  62. return -ENODEV;
  63. if (device_declaration) {
  64. if ((entry->length < 16) || (lsapic->uid != acpi_id))
  65. return -EINVAL;
  66. } else if (lsapic->processor_id != acpi_id)
  67. return -EINVAL;
  68. *apic_id = (lsapic->id << 8) | lsapic->eid;
  69. return 0;
  70. }
  71. /*
  72. * Retrieve the ARM CPU physical identifier (MPIDR)
  73. */
  74. static int map_gicc_mpidr(struct acpi_subtable_header *entry,
  75. int device_declaration, u32 acpi_id, phys_cpuid_t *mpidr,
  76. bool ignore_disabled)
  77. {
  78. struct acpi_madt_generic_interrupt *gicc =
  79. container_of(entry, struct acpi_madt_generic_interrupt, header);
  80. if (ignore_disabled && !(gicc->flags & ACPI_MADT_ENABLED))
  81. return -ENODEV;
  82. /* device_declaration means Device object in DSDT, in the
  83. * GIC interrupt model, logical processors are required to
  84. * have a Processor Device object in the DSDT, so we should
  85. * check device_declaration here
  86. */
  87. if (device_declaration && (gicc->uid == acpi_id)) {
  88. *mpidr = gicc->arm_mpidr;
  89. return 0;
  90. }
  91. return -EINVAL;
  92. }
  93. static phys_cpuid_t map_madt_entry(struct acpi_table_madt *madt,
  94. int type, u32 acpi_id, bool ignore_disabled)
  95. {
  96. unsigned long madt_end, entry;
  97. phys_cpuid_t phys_id = PHYS_CPUID_INVALID; /* CPU hardware ID */
  98. if (!madt)
  99. return phys_id;
  100. entry = (unsigned long)madt;
  101. madt_end = entry + madt->header.length;
  102. /* Parse all entries looking for a match. */
  103. entry += sizeof(struct acpi_table_madt);
  104. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  105. struct acpi_subtable_header *header =
  106. (struct acpi_subtable_header *)entry;
  107. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  108. if (!map_lapic_id(header, acpi_id, &phys_id,
  109. ignore_disabled))
  110. break;
  111. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  112. if (!map_x2apic_id(header, type, acpi_id, &phys_id,
  113. ignore_disabled))
  114. break;
  115. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  116. if (!map_lsapic_id(header, type, acpi_id, &phys_id,
  117. ignore_disabled))
  118. break;
  119. } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
  120. if (!map_gicc_mpidr(header, type, acpi_id, &phys_id,
  121. ignore_disabled))
  122. break;
  123. }
  124. entry += header->length;
  125. }
  126. return phys_id;
  127. }
  128. phys_cpuid_t __init acpi_map_madt_entry(u32 acpi_id)
  129. {
  130. struct acpi_table_madt *madt = NULL;
  131. acpi_size tbl_size;
  132. phys_cpuid_t rv;
  133. acpi_get_table_with_size(ACPI_SIG_MADT, 0,
  134. (struct acpi_table_header **)&madt,
  135. &tbl_size);
  136. if (!madt)
  137. return PHYS_CPUID_INVALID;
  138. rv = map_madt_entry(madt, 1, acpi_id, true);
  139. early_acpi_os_unmap_memory(madt, tbl_size);
  140. return rv;
  141. }
  142. static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id,
  143. bool ignore_disabled)
  144. {
  145. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  146. union acpi_object *obj;
  147. struct acpi_subtable_header *header;
  148. phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
  149. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  150. goto exit;
  151. if (!buffer.length || !buffer.pointer)
  152. goto exit;
  153. obj = buffer.pointer;
  154. if (obj->type != ACPI_TYPE_BUFFER ||
  155. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  156. goto exit;
  157. }
  158. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  159. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
  160. map_lapic_id(header, acpi_id, &phys_id, ignore_disabled);
  161. else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
  162. map_lsapic_id(header, type, acpi_id, &phys_id, ignore_disabled);
  163. else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
  164. map_x2apic_id(header, type, acpi_id, &phys_id, ignore_disabled);
  165. else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
  166. map_gicc_mpidr(header, type, acpi_id, &phys_id,
  167. ignore_disabled);
  168. exit:
  169. kfree(buffer.pointer);
  170. return phys_id;
  171. }
  172. static phys_cpuid_t __acpi_get_phys_id(acpi_handle handle, int type,
  173. u32 acpi_id, bool ignore_disabled)
  174. {
  175. phys_cpuid_t phys_id;
  176. phys_id = map_mat_entry(handle, type, acpi_id, ignore_disabled);
  177. if (invalid_phys_cpuid(phys_id))
  178. phys_id = map_madt_entry(get_madt_table(), type, acpi_id,
  179. ignore_disabled);
  180. return phys_id;
  181. }
  182. phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
  183. {
  184. return __acpi_get_phys_id(handle, type, acpi_id, true);
  185. }
  186. int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
  187. {
  188. #ifdef CONFIG_SMP
  189. int i;
  190. #endif
  191. if (invalid_phys_cpuid(phys_id)) {
  192. /*
  193. * On UP processor, there is no _MAT or MADT table.
  194. * So above phys_id is always set to PHYS_CPUID_INVALID.
  195. *
  196. * BIOS may define multiple CPU handles even for UP processor.
  197. * For example,
  198. *
  199. * Scope (_PR)
  200. * {
  201. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  202. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  203. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  204. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  205. * }
  206. *
  207. * Ignores phys_id and always returns 0 for the processor
  208. * handle with acpi id 0 if nr_cpu_ids is 1.
  209. * This should be the case if SMP tables are not found.
  210. * Return -EINVAL for other CPU's handle.
  211. */
  212. if (nr_cpu_ids <= 1 && acpi_id == 0)
  213. return acpi_id;
  214. else
  215. return -EINVAL;
  216. }
  217. #ifdef CONFIG_SMP
  218. for_each_possible_cpu(i) {
  219. if (cpu_physical_id(i) == phys_id)
  220. return i;
  221. }
  222. #else
  223. /* In UP kernel, only processor 0 is valid */
  224. if (phys_id == 0)
  225. return phys_id;
  226. #endif
  227. return -ENODEV;
  228. }
  229. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  230. {
  231. phys_cpuid_t phys_id;
  232. phys_id = acpi_get_phys_id(handle, type, acpi_id);
  233. return acpi_map_cpuid(phys_id, acpi_id);
  234. }
  235. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  236. #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
  237. static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
  238. u64 *phys_addr, int *ioapic_id)
  239. {
  240. struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
  241. if (ioapic->global_irq_base != gsi_base)
  242. return 0;
  243. *phys_addr = ioapic->address;
  244. *ioapic_id = ioapic->id;
  245. return 1;
  246. }
  247. static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
  248. {
  249. struct acpi_subtable_header *hdr;
  250. unsigned long madt_end, entry;
  251. struct acpi_table_madt *madt;
  252. int apic_id = -1;
  253. madt = get_madt_table();
  254. if (!madt)
  255. return apic_id;
  256. entry = (unsigned long)madt;
  257. madt_end = entry + madt->header.length;
  258. /* Parse all entries looking for a match. */
  259. entry += sizeof(struct acpi_table_madt);
  260. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  261. hdr = (struct acpi_subtable_header *)entry;
  262. if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
  263. get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
  264. break;
  265. else
  266. entry += hdr->length;
  267. }
  268. return apic_id;
  269. }
  270. static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
  271. u64 *phys_addr)
  272. {
  273. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  274. struct acpi_subtable_header *header;
  275. union acpi_object *obj;
  276. int apic_id = -1;
  277. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  278. goto exit;
  279. if (!buffer.length || !buffer.pointer)
  280. goto exit;
  281. obj = buffer.pointer;
  282. if (obj->type != ACPI_TYPE_BUFFER ||
  283. obj->buffer.length < sizeof(struct acpi_subtable_header))
  284. goto exit;
  285. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  286. if (header->type == ACPI_MADT_TYPE_IO_APIC)
  287. get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
  288. exit:
  289. kfree(buffer.pointer);
  290. return apic_id;
  291. }
  292. /**
  293. * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
  294. * @handle: ACPI object for IOAPIC device
  295. * @gsi_base: GSI base to match with
  296. * @phys_addr: Pointer to store physical address of matching IOAPIC record
  297. *
  298. * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
  299. * for an ACPI IOAPIC record matching @gsi_base.
  300. * Return IOAPIC id and store physical address in @phys_addr if found a match,
  301. * otherwise return <0.
  302. */
  303. int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
  304. {
  305. int apic_id;
  306. apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
  307. if (apic_id == -1)
  308. apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
  309. return apic_id;
  310. }
  311. #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */