processor_core.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  8. * - Added _PDC for platforms with Intel CPUs
  9. */
  10. #include <linux/export.h>
  11. #include <linux/dmi.h>
  12. #include <linux/slab.h>
  13. #include <linux/acpi.h>
  14. #include <acpi/processor.h>
  15. #include "internal.h"
  16. #define PREFIX "ACPI: "
  17. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  18. ACPI_MODULE_NAME("processor_core");
  19. static int map_lapic_id(struct acpi_subtable_header *entry,
  20. u32 acpi_id, int *apic_id)
  21. {
  22. struct acpi_madt_local_apic *lapic =
  23. (struct acpi_madt_local_apic *)entry;
  24. if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
  25. return -ENODEV;
  26. if (lapic->processor_id != acpi_id)
  27. return -EINVAL;
  28. *apic_id = lapic->id;
  29. return 0;
  30. }
  31. static int map_x2apic_id(struct acpi_subtable_header *entry,
  32. int device_declaration, u32 acpi_id, int *apic_id)
  33. {
  34. struct acpi_madt_local_x2apic *apic =
  35. (struct acpi_madt_local_x2apic *)entry;
  36. if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
  37. return -ENODEV;
  38. if (device_declaration && (apic->uid == acpi_id)) {
  39. *apic_id = apic->local_apic_id;
  40. return 0;
  41. }
  42. return -EINVAL;
  43. }
  44. static int map_lsapic_id(struct acpi_subtable_header *entry,
  45. int device_declaration, u32 acpi_id, int *apic_id)
  46. {
  47. struct acpi_madt_local_sapic *lsapic =
  48. (struct acpi_madt_local_sapic *)entry;
  49. if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
  50. return -ENODEV;
  51. if (device_declaration) {
  52. if ((entry->length < 16) || (lsapic->uid != acpi_id))
  53. return -EINVAL;
  54. } else if (lsapic->processor_id != acpi_id)
  55. return -EINVAL;
  56. *apic_id = (lsapic->id << 8) | lsapic->eid;
  57. return 0;
  58. }
  59. static int map_madt_entry(int type, u32 acpi_id)
  60. {
  61. unsigned long madt_end, entry;
  62. static struct acpi_table_madt *madt;
  63. static int read_madt;
  64. int apic_id = -1;
  65. if (!read_madt) {
  66. if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
  67. (struct acpi_table_header **)&madt)))
  68. madt = NULL;
  69. read_madt++;
  70. }
  71. if (!madt)
  72. return apic_id;
  73. entry = (unsigned long)madt;
  74. madt_end = entry + madt->header.length;
  75. /* Parse all entries looking for a match. */
  76. entry += sizeof(struct acpi_table_madt);
  77. while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
  78. struct acpi_subtable_header *header =
  79. (struct acpi_subtable_header *)entry;
  80. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  81. if (!map_lapic_id(header, acpi_id, &apic_id))
  82. break;
  83. } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
  84. if (!map_x2apic_id(header, type, acpi_id, &apic_id))
  85. break;
  86. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  87. if (!map_lsapic_id(header, type, acpi_id, &apic_id))
  88. break;
  89. }
  90. entry += header->length;
  91. }
  92. return apic_id;
  93. }
  94. static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
  95. {
  96. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  97. union acpi_object *obj;
  98. struct acpi_subtable_header *header;
  99. int apic_id = -1;
  100. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  101. goto exit;
  102. if (!buffer.length || !buffer.pointer)
  103. goto exit;
  104. obj = buffer.pointer;
  105. if (obj->type != ACPI_TYPE_BUFFER ||
  106. obj->buffer.length < sizeof(struct acpi_subtable_header)) {
  107. goto exit;
  108. }
  109. header = (struct acpi_subtable_header *)obj->buffer.pointer;
  110. if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
  111. map_lapic_id(header, acpi_id, &apic_id);
  112. } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
  113. map_lsapic_id(header, type, acpi_id, &apic_id);
  114. }
  115. exit:
  116. kfree(buffer.pointer);
  117. return apic_id;
  118. }
  119. int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
  120. {
  121. int apic_id;
  122. apic_id = map_mat_entry(handle, type, acpi_id);
  123. if (apic_id == -1)
  124. apic_id = map_madt_entry(type, acpi_id);
  125. return apic_id;
  126. }
  127. int acpi_map_cpuid(int apic_id, u32 acpi_id)
  128. {
  129. #ifdef CONFIG_SMP
  130. int i;
  131. #endif
  132. if (apic_id == -1) {
  133. /*
  134. * On UP processor, there is no _MAT or MADT table.
  135. * So above apic_id is always set to -1.
  136. *
  137. * BIOS may define multiple CPU handles even for UP processor.
  138. * For example,
  139. *
  140. * Scope (_PR)
  141. * {
  142. * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
  143. * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
  144. * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
  145. * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
  146. * }
  147. *
  148. * Ignores apic_id and always returns 0 for the processor
  149. * handle with acpi id 0 if nr_cpu_ids is 1.
  150. * This should be the case if SMP tables are not found.
  151. * Return -1 for other CPU's handle.
  152. */
  153. if (nr_cpu_ids <= 1 && acpi_id == 0)
  154. return acpi_id;
  155. else
  156. return apic_id;
  157. }
  158. #ifdef CONFIG_SMP
  159. for_each_possible_cpu(i) {
  160. if (cpu_physical_id(i) == apic_id)
  161. return i;
  162. }
  163. #else
  164. /* In UP kernel, only processor 0 is valid */
  165. if (apic_id == 0)
  166. return apic_id;
  167. #endif
  168. return -1;
  169. }
  170. int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
  171. {
  172. int apic_id;
  173. apic_id = acpi_get_apicid(handle, type, acpi_id);
  174. return acpi_map_cpuid(apic_id, acpi_id);
  175. }
  176. EXPORT_SYMBOL_GPL(acpi_get_cpuid);
  177. static bool __init processor_physically_present(acpi_handle handle)
  178. {
  179. int cpuid, type;
  180. u32 acpi_id;
  181. acpi_status status;
  182. acpi_object_type acpi_type;
  183. unsigned long long tmp;
  184. union acpi_object object = { 0 };
  185. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  186. status = acpi_get_type(handle, &acpi_type);
  187. if (ACPI_FAILURE(status))
  188. return false;
  189. switch (acpi_type) {
  190. case ACPI_TYPE_PROCESSOR:
  191. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  192. if (ACPI_FAILURE(status))
  193. return false;
  194. acpi_id = object.processor.proc_id;
  195. break;
  196. case ACPI_TYPE_DEVICE:
  197. status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
  198. if (ACPI_FAILURE(status))
  199. return false;
  200. acpi_id = tmp;
  201. break;
  202. default:
  203. return false;
  204. }
  205. type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
  206. cpuid = acpi_get_cpuid(handle, type, acpi_id);
  207. if (cpuid == -1)
  208. return false;
  209. return true;
  210. }
  211. static void acpi_set_pdc_bits(u32 *buf)
  212. {
  213. buf[0] = ACPI_PDC_REVISION_ID;
  214. buf[1] = 1;
  215. /* Enable coordination with firmware's _TSD info */
  216. buf[2] = ACPI_PDC_SMP_T_SWCOORD;
  217. /* Twiddle arch-specific bits needed for _PDC */
  218. arch_acpi_set_pdc_bits(buf);
  219. }
  220. static struct acpi_object_list *acpi_processor_alloc_pdc(void)
  221. {
  222. struct acpi_object_list *obj_list;
  223. union acpi_object *obj;
  224. u32 *buf;
  225. /* allocate and initialize pdc. It will be used later. */
  226. obj_list = kmalloc(sizeof(struct acpi_object_list), GFP_KERNEL);
  227. if (!obj_list) {
  228. printk(KERN_ERR "Memory allocation error\n");
  229. return NULL;
  230. }
  231. obj = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
  232. if (!obj) {
  233. printk(KERN_ERR "Memory allocation error\n");
  234. kfree(obj_list);
  235. return NULL;
  236. }
  237. buf = kmalloc(12, GFP_KERNEL);
  238. if (!buf) {
  239. printk(KERN_ERR "Memory allocation error\n");
  240. kfree(obj);
  241. kfree(obj_list);
  242. return NULL;
  243. }
  244. acpi_set_pdc_bits(buf);
  245. obj->type = ACPI_TYPE_BUFFER;
  246. obj->buffer.length = 12;
  247. obj->buffer.pointer = (u8 *) buf;
  248. obj_list->count = 1;
  249. obj_list->pointer = obj;
  250. return obj_list;
  251. }
  252. /*
  253. * _PDC is required for a BIOS-OS handshake for most of the newer
  254. * ACPI processor features.
  255. */
  256. static acpi_status
  257. acpi_processor_eval_pdc(acpi_handle handle, struct acpi_object_list *pdc_in)
  258. {
  259. acpi_status status = AE_OK;
  260. if (boot_option_idle_override == IDLE_NOMWAIT) {
  261. /*
  262. * If mwait is disabled for CPU C-states, the C2C3_FFH access
  263. * mode will be disabled in the parameter of _PDC object.
  264. * Of course C1_FFH access mode will also be disabled.
  265. */
  266. union acpi_object *obj;
  267. u32 *buffer = NULL;
  268. obj = pdc_in->pointer;
  269. buffer = (u32 *)(obj->buffer.pointer);
  270. buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
  271. }
  272. status = acpi_evaluate_object(handle, "_PDC", pdc_in, NULL);
  273. if (ACPI_FAILURE(status))
  274. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  275. "Could not evaluate _PDC, using legacy perf. control.\n"));
  276. return status;
  277. }
  278. void acpi_processor_set_pdc(acpi_handle handle)
  279. {
  280. struct acpi_object_list *obj_list;
  281. if (arch_has_acpi_pdc() == false)
  282. return;
  283. obj_list = acpi_processor_alloc_pdc();
  284. if (!obj_list)
  285. return;
  286. acpi_processor_eval_pdc(handle, obj_list);
  287. kfree(obj_list->pointer->buffer.pointer);
  288. kfree(obj_list->pointer);
  289. kfree(obj_list);
  290. }
  291. static acpi_status __init
  292. early_init_pdc(acpi_handle handle, u32 lvl, void *context, void **rv)
  293. {
  294. if (processor_physically_present(handle) == false)
  295. return AE_OK;
  296. acpi_processor_set_pdc(handle);
  297. return AE_OK;
  298. }
  299. #if defined(CONFIG_X86) || defined(CONFIG_IA64)
  300. static int __init set_no_mwait(const struct dmi_system_id *id)
  301. {
  302. pr_notice(PREFIX "%s detected - disabling mwait for CPU C-states\n",
  303. id->ident);
  304. boot_option_idle_override = IDLE_NOMWAIT;
  305. return 0;
  306. }
  307. static struct dmi_system_id processor_idle_dmi_table[] __initdata = {
  308. {
  309. set_no_mwait, "Extensa 5220", {
  310. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  311. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  312. DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
  313. DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
  314. {},
  315. };
  316. static void __init processor_dmi_check(void)
  317. {
  318. /*
  319. * Check whether the system is DMI table. If yes, OSPM
  320. * should not use mwait for CPU-states.
  321. */
  322. dmi_check_system(processor_idle_dmi_table);
  323. }
  324. #else
  325. static inline void processor_dmi_check(void) {}
  326. #endif
  327. void __init acpi_early_processor_set_pdc(void)
  328. {
  329. processor_dmi_check();
  330. acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  331. ACPI_UINT32_MAX,
  332. early_init_pdc, NULL, NULL, NULL);
  333. acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, early_init_pdc, NULL, NULL);
  334. }