processor_core.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <linux/export.h>
  9. #include <linux/acpi.h>
  10. #include <acpi/processor.h>
  11. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  12. ACPI_MODULE_NAME("processor_core");
  13. static int map_lapic_id(struct acpi_subtable_header *entry,
  14. u32 acpi_id, int *apic_id)
  15. {
  16. struct acpi_madt_local_apic *lapic =
  17. container_of(entry, struct acpi_madt_local_apic, header);
  18. if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
  19. return -ENODEV;
  20. if (lapic->processor_id != acpi_id)
  21. return -EINVAL;
  22. *apic_id = lapic->id;
  23. return 0;
  24. }
  25. static int map_x2apic_id(struct acpi_subtable_header *entry,
  26. int device_declaration, u32 acpi_id, int *apic_id)
  27. {
  28. struct acpi_madt_local_x2apic *apic =
  29. container_of(entry, struct acpi_madt_local_x2apic, header);
  30. if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
  31. return -ENODEV;
  32. if (device_declaration && (apic->uid == acpi_id)) {
  33. *apic_id = apic->local_apic_id;
  34. return 0;
  35. }
  36. return -EINVAL;
  37. }
  38. static int map_lsapic_id(struct acpi_subtable_header *entry,
  39. int device_declaration, u32 acpi_id, int *apic_id)
  40. {
  41. struct acpi_madt_local_sapic *lsapic =
  42. container_of(entry, struct acpi_madt_local_sapic, header);
  43. if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
  44. return -ENODEV;
  45. if (device_declaration) {
  46. if ((entry->length < 16) || (lsapic->uid != acpi_id))
  47. return -EINVAL;
  48. } else if (lsapic->processor_id != acpi_id)
  49. return -EINVAL;
  50. *apic_id = (lsapic->id << 8) | lsapic->eid;
  51. return 0;
  52. }
  53. static int map_madt_entry(int type, u32 acpi_id)
  54. {
  55. unsigned long madt_end, entry;
  56. static struct acpi_table_madt *madt;
  57. static int read_madt;
  58. int apic_id = -1;
  59. if (!read_madt) {
  60. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  61. (struct acpi_table_header **)&madt)))
  62. madt = NULL;
  63. read_madt++;
  64. }
  65. if (!madt)
  66. return apic_id;
  67. entry = (unsigned long)madt;
  68. madt_end = entry + madt->header.length;
  69. /* Parse all entries looking for a match. */
  70. entry += sizeof(struct acpi_table_madt);
  71. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  72. struct acpi_subtable_header *header =
  73. (struct acpi_subtable_header *)entry;
  74. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  75. if (!map_lapic_id(header, acpi_id, &apic_id))
  76. break;
  77. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  78. if (!map_x2apic_id(header, type, acpi_id, &apic_id))
  79. break;
  80. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  81. if (!map_lsapic_id(header, type, acpi_id, &apic_id))
  82. break;
  83. }
  84. entry += header->length;
  85. }
  86. return apic_id;
  87. }
  88. static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
  89. {
  90. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  91. union acpi_object *obj;
  92. struct acpi_subtable_header *header;
  93. int apic_id = -1;
  94. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  95. goto exit;
  96. if (!buffer.length || !buffer.pointer)
  97. goto exit;
  98. obj = buffer.pointer;
  99. if (obj->type != ACPI_TYPE_BUFFER ||
  100. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  101. goto exit;
  102. }
  103. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  104. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  105. map_lapic_id(header, acpi_id, &apic_id);
  106. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  107. map_lsapic_id(header, type, acpi_id, &apic_id);
  108. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  109. map_x2apic_id(header, type, acpi_id, &apic_id);
  110. }
  111. exit:
  112. kfree(buffer.pointer);
  113. return apic_id;
  114. }
  115. int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
  116. {
  117. int apic_id;
  118. apic_id = map_mat_entry(handle, type, acpi_id);
  119. if (apic_id == -1)
  120. apic_id = map_madt_entry(type, acpi_id);
  121. return apic_id;
  122. }
  123. int acpi_map_cpuid(int apic_id, u32 acpi_id)
  124. {
  125. #ifdef CONFIG_SMP
  126. int i;
  127. #endif
  128. if (apic_id == -1) {
  129. /*
  130. * On UP processor, there is no _MAT or MADT table.
  131. * So above apic_id is always set to -1.
  132. *
  133. * BIOS may define multiple CPU handles even for UP processor.
  134. * For example,
  135. *
  136. * Scope (_PR)
  137. * {
  138. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  139. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  140. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  141. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  142. * }
  143. *
  144. * Ignores apic_id and always returns 0 for the processor
  145. * handle with acpi id 0 if nr_cpu_ids is 1.
  146. * This should be the case if SMP tables are not found.
  147. * Return -1 for other CPU's handle.
  148. */
  149. if (nr_cpu_ids <= 1 && acpi_id == 0)
  150. return acpi_id;
  151. else
  152. return apic_id;
  153. }
  154. #ifdef CONFIG_SMP
  155. for_each_possible_cpu(i) {
  156. if (cpu_physical_id(i) == apic_id)
  157. return i;
  158. }
  159. #else
  160. /* In UP kernel, only processor 0 is valid */
  161. if (apic_id == 0)
  162. return apic_id;
  163. #endif
  164. return -1;
  165. }
  166. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  167. {
  168. int apic_id;
  169. apic_id = acpi_get_apicid(handle, type, acpi_id);
  170. return acpi_map_cpuid(apic_id, acpi_id);
  171. }
  172. EXPORT_SYMBOL_GPL(acpi_get_cpuid);