processor_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. phys_cpuid_t rv;
  132. acpi_get_table(ACPI_SIG_MADT, 0,
  133. (struct acpi_table_header **)&madt);
  134. if (!madt)
  135. return PHYS_CPUID_INVALID;
  136. rv = map_madt_entry(madt, 1, acpi_id, true);
  137. acpi_put_table((struct acpi_table_header *)madt);
  138. return rv;
  139. }
  140. static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id,
  141. bool ignore_disabled)
  142. {
  143. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  144. union acpi_object *obj;
  145. struct acpi_subtable_header *header;
  146. phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
  147. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  148. goto exit;
  149. if (!buffer.length || !buffer.pointer)
  150. goto exit;
  151. obj = buffer.pointer;
  152. if (obj->type != ACPI_TYPE_BUFFER ||
  153. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  154. goto exit;
  155. }
  156. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  157. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
  158. map_lapic_id(header, acpi_id, &phys_id, ignore_disabled);
  159. else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
  160. map_lsapic_id(header, type, acpi_id, &phys_id, ignore_disabled);
  161. else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
  162. map_x2apic_id(header, type, acpi_id, &phys_id, ignore_disabled);
  163. else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
  164. map_gicc_mpidr(header, type, acpi_id, &phys_id,
  165. ignore_disabled);
  166. exit:
  167. kfree(buffer.pointer);
  168. return phys_id;
  169. }
  170. static phys_cpuid_t __acpi_get_phys_id(acpi_handle handle, int type,
  171. u32 acpi_id, bool ignore_disabled)
  172. {
  173. phys_cpuid_t phys_id;
  174. phys_id = map_mat_entry(handle, type, acpi_id, ignore_disabled);
  175. if (invalid_phys_cpuid(phys_id))
  176. phys_id = map_madt_entry(get_madt_table(), type, acpi_id,
  177. ignore_disabled);
  178. return phys_id;
  179. }
  180. phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
  181. {
  182. return __acpi_get_phys_id(handle, type, acpi_id, true);
  183. }
  184. int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
  185. {
  186. #ifdef CONFIG_SMP
  187. int i;
  188. #endif
  189. if (invalid_phys_cpuid(phys_id)) {
  190. /*
  191. * On UP processor, there is no _MAT or MADT table.
  192. * So above phys_id is always set to PHYS_CPUID_INVALID.
  193. *
  194. * BIOS may define multiple CPU handles even for UP processor.
  195. * For example,
  196. *
  197. * Scope (_PR)
  198. * {
  199. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  200. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  201. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  202. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  203. * }
  204. *
  205. * Ignores phys_id and always returns 0 for the processor
  206. * handle with acpi id 0 if nr_cpu_ids is 1.
  207. * This should be the case if SMP tables are not found.
  208. * Return -EINVAL for other CPU's handle.
  209. */
  210. if (nr_cpu_ids <= 1 && acpi_id == 0)
  211. return acpi_id;
  212. else
  213. return -EINVAL;
  214. }
  215. #ifdef CONFIG_SMP
  216. for_each_possible_cpu(i) {
  217. if (cpu_physical_id(i) == phys_id)
  218. return i;
  219. }
  220. #else
  221. /* In UP kernel, only processor 0 is valid */
  222. if (phys_id == 0)
  223. return phys_id;
  224. #endif
  225. return -ENODEV;
  226. }
  227. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  228. {
  229. phys_cpuid_t phys_id;
  230. phys_id = acpi_get_phys_id(handle, type, acpi_id);
  231. return acpi_map_cpuid(phys_id, acpi_id);
  232. }
  233. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  234. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  235. static bool __init
  236. map_processor(acpi_handle handle, phys_cpuid_t *phys_id, int *cpuid)
  237. {
  238. int type, id;
  239. u32 acpi_id;
  240. acpi_status status;
  241. acpi_object_type acpi_type;
  242. unsigned long long tmp;
  243. union acpi_object object = { 0 };
  244. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  245. status = acpi_get_type(handle, &acpi_type);
  246. if (ACPI_FAILURE(status))
  247. return false;
  248. switch (acpi_type) {
  249. case ACPI_TYPE_PROCESSOR:
  250. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  251. if (ACPI_FAILURE(status))
  252. return false;
  253. acpi_id = object.processor.proc_id;
  254. /* validate the acpi_id */
  255. if(acpi_processor_validate_proc_id(acpi_id))
  256. return false;
  257. break;
  258. case ACPI_TYPE_DEVICE:
  259. status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
  260. if (ACPI_FAILURE(status))
  261. return false;
  262. acpi_id = tmp;
  263. break;
  264. default:
  265. return false;
  266. }
  267. type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
  268. *phys_id = __acpi_get_phys_id(handle, type, acpi_id, false);
  269. id = acpi_map_cpuid(*phys_id, acpi_id);
  270. if (id < 0)
  271. return false;
  272. *cpuid = id;
  273. return true;
  274. }
  275. static acpi_status __init
  276. set_processor_node_mapping(acpi_handle handle, u32 lvl, void *context,
  277. void **rv)
  278. {
  279. phys_cpuid_t phys_id;
  280. int cpu_id;
  281. if (!map_processor(handle, &phys_id, &cpu_id))
  282. return AE_ERROR;
  283. acpi_map_cpu2node(handle, cpu_id, phys_id);
  284. return AE_OK;
  285. }
  286. void __init acpi_set_processor_mapping(void)
  287. {
  288. /* Set persistent cpu <-> node mapping for all processors. */
  289. acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  290. ACPI_UINT32_MAX, set_processor_node_mapping,
  291. NULL, NULL, NULL);
  292. }
  293. #else
  294. void __init acpi_set_processor_mapping(void) {}
  295. #endif /* CONFIG_ACPI_HOTPLUG_CPU */
  296. #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
  297. static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
  298. u64 *phys_addr, int *ioapic_id)
  299. {
  300. struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
  301. if (ioapic->global_irq_base != gsi_base)
  302. return 0;
  303. *phys_addr = ioapic->address;
  304. *ioapic_id = ioapic->id;
  305. return 1;
  306. }
  307. static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
  308. {
  309. struct acpi_subtable_header *hdr;
  310. unsigned long madt_end, entry;
  311. struct acpi_table_madt *madt;
  312. int apic_id = -1;
  313. madt = get_madt_table();
  314. if (!madt)
  315. return apic_id;
  316. entry = (unsigned long)madt;
  317. madt_end = entry + madt->header.length;
  318. /* Parse all entries looking for a match. */
  319. entry += sizeof(struct acpi_table_madt);
  320. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  321. hdr = (struct acpi_subtable_header *)entry;
  322. if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
  323. get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
  324. break;
  325. else
  326. entry += hdr->length;
  327. }
  328. return apic_id;
  329. }
  330. static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
  331. u64 *phys_addr)
  332. {
  333. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  334. struct acpi_subtable_header *header;
  335. union acpi_object *obj;
  336. int apic_id = -1;
  337. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  338. goto exit;
  339. if (!buffer.length || !buffer.pointer)
  340. goto exit;
  341. obj = buffer.pointer;
  342. if (obj->type != ACPI_TYPE_BUFFER ||
  343. obj->buffer.length < sizeof(struct acpi_subtable_header))
  344. goto exit;
  345. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  346. if (header->type == ACPI_MADT_TYPE_IO_APIC)
  347. get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
  348. exit:
  349. kfree(buffer.pointer);
  350. return apic_id;
  351. }
  352. /**
  353. * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
  354. * @handle: ACPI object for IOAPIC device
  355. * @gsi_base: GSI base to match with
  356. * @phys_addr: Pointer to store physical address of matching IOAPIC record
  357. *
  358. * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
  359. * for an ACPI IOAPIC record matching @gsi_base.
  360. * Return IOAPIC id and store physical address in @phys_addr if found a match,
  361. * otherwise return <0.
  362. */
  363. int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
  364. {
  365. int apic_id;
  366. apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
  367. if (apic_id == -1)
  368. apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
  369. return apic_id;
  370. }
  371. #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */