cstate.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2005 Intel Corporation
  3. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  4. * - Added _PDC for SMP C-states on Intel CPUs
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/acpi.h>
  10. #include <linux/cpu.h>
  11. #include <linux/sched.h>
  12. #include <acpi/processor.h>
  13. #include <asm/acpi.h>
  14. #include <asm/mwait.h>
  15. #include <asm/special_insns.h>
  16. /*
  17. * Initialize bm_flags based on the CPU cache properties
  18. * On SMP it depends on cache configuration
  19. * - When cache is not shared among all CPUs, we flush cache
  20. * before entering C3.
  21. * - When cache is shared among all CPUs, we use bm_check
  22. * mechanism as in UP case
  23. *
  24. * This routine is called only after all the CPUs are online
  25. */
  26. void acpi_processor_power_init_bm_check(struct acpi_processor_flags *flags,
  27. unsigned int cpu)
  28. {
  29. struct cpuinfo_x86 *c = &cpu_data(cpu);
  30. flags->bm_check = 0;
  31. if (num_online_cpus() == 1)
  32. flags->bm_check = 1;
  33. else if (c->x86_vendor == X86_VENDOR_INTEL) {
  34. /*
  35. * Today all MP CPUs that support C3 share cache.
  36. * And caches should not be flushed by software while
  37. * entering C3 type state.
  38. */
  39. flags->bm_check = 1;
  40. }
  41. /*
  42. * On all recent Intel platforms, ARB_DISABLE is a nop.
  43. * So, set bm_control to zero to indicate that ARB_DISABLE
  44. * is not required while entering C3 type state on
  45. * P4, Core and beyond CPUs
  46. */
  47. if (c->x86_vendor == X86_VENDOR_INTEL &&
  48. (c->x86 > 0xf || (c->x86 == 6 && c->x86_model >= 0x0f)))
  49. flags->bm_control = 0;
  50. }
  51. EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
  52. /* The code below handles cstate entry with monitor-mwait pair on Intel*/
  53. struct cstate_entry {
  54. struct {
  55. unsigned int eax;
  56. unsigned int ecx;
  57. } states[ACPI_PROCESSOR_MAX_POWER];
  58. };
  59. static struct cstate_entry __percpu *cpu_cstate_entry; /* per CPU ptr */
  60. static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
  61. #define NATIVE_CSTATE_BEYOND_HALT (2)
  62. static long acpi_processor_ffh_cstate_probe_cpu(void *_cx)
  63. {
  64. struct acpi_processor_cx *cx = _cx;
  65. long retval;
  66. unsigned int eax, ebx, ecx, edx;
  67. unsigned int edx_part;
  68. unsigned int cstate_type; /* C-state type and not ACPI C-state type */
  69. unsigned int num_cstate_subtype;
  70. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  71. /* Check whether this particular cx_type (in CST) is supported or not */
  72. cstate_type = ((cx->address >> MWAIT_SUBSTATE_SIZE) &
  73. MWAIT_CSTATE_MASK) + 1;
  74. edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
  75. num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
  76. retval = 0;
  77. if (num_cstate_subtype < (cx->address & MWAIT_SUBSTATE_MASK)) {
  78. retval = -1;
  79. goto out;
  80. }
  81. /* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
  82. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  83. !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
  84. retval = -1;
  85. goto out;
  86. }
  87. if (!mwait_supported[cstate_type]) {
  88. mwait_supported[cstate_type] = 1;
  89. printk(KERN_DEBUG
  90. "Monitor-Mwait will be used to enter C-%d "
  91. "state\n", cx->type);
  92. }
  93. snprintf(cx->desc,
  94. ACPI_CX_DESC_LEN, "ACPI FFH INTEL MWAIT 0x%x",
  95. cx->address);
  96. out:
  97. return retval;
  98. }
  99. int acpi_processor_ffh_cstate_probe(unsigned int cpu,
  100. struct acpi_processor_cx *cx, struct acpi_power_register *reg)
  101. {
  102. struct cstate_entry *percpu_entry;
  103. struct cpuinfo_x86 *c = &cpu_data(cpu);
  104. long retval;
  105. if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF)
  106. return -1;
  107. if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
  108. return -1;
  109. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  110. percpu_entry->states[cx->index].eax = 0;
  111. percpu_entry->states[cx->index].ecx = 0;
  112. /* Make sure we are running on right CPU */
  113. retval = work_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx);
  114. if (retval == 0) {
  115. /* Use the hint in CST */
  116. percpu_entry->states[cx->index].eax = cx->address;
  117. percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
  118. }
  119. /*
  120. * For _CST FFH on Intel, if GAS.access_size bit 1 is cleared,
  121. * then we should skip checking BM_STS for this C-state.
  122. * ref: "Intel Processor Vendor-Specific ACPI Interface Specification"
  123. */
  124. if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
  125. cx->bm_sts_skip = 1;
  126. return retval;
  127. }
  128. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
  129. /*
  130. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  131. * which can obviate IPI to trigger checking of need_resched.
  132. * We execute MONITOR against need_resched and enter optimized wait state
  133. * through MWAIT. Whenever someone changes need_resched, we would be woken
  134. * up from MWAIT (without an IPI).
  135. *
  136. * New with Core Duo processors, MWAIT can take some hints based on CPU
  137. * capability.
  138. */
  139. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  140. {
  141. if (!need_resched()) {
  142. if (this_cpu_has(X86_FEATURE_CLFLUSH_MONITOR))
  143. clflush((void *)&current_thread_info()->flags);
  144. __monitor((void *)&current_thread_info()->flags, 0, 0);
  145. smp_mb();
  146. if (!need_resched())
  147. __mwait(ax, cx);
  148. }
  149. }
  150. void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
  151. {
  152. unsigned int cpu = smp_processor_id();
  153. struct cstate_entry *percpu_entry;
  154. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  155. mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
  156. percpu_entry->states[cx->index].ecx);
  157. }
  158. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
  159. static int __init ffh_cstate_init(void)
  160. {
  161. struct cpuinfo_x86 *c = &boot_cpu_data;
  162. if (c->x86_vendor != X86_VENDOR_INTEL)
  163. return -1;
  164. cpu_cstate_entry = alloc_percpu(struct cstate_entry);
  165. return 0;
  166. }
  167. static void __exit ffh_cstate_exit(void)
  168. {
  169. free_percpu(cpu_cstate_entry);
  170. cpu_cstate_entry = NULL;
  171. }
  172. arch_initcall(ffh_cstate_init);
  173. __exitcall(ffh_cstate_exit);