processor_core.c 9.4 KB

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