cstate.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/export.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/mwait.h>
  14. #include <asm/special_insns.h>
  15. /*
  16. * Initialize bm_flags based on the CPU cache properties
  17. * On SMP it depends on cache configuration
  18. * - When cache is not shared among all CPUs, we flush cache
  19. * before entering C3.
  20. * - When cache is shared among all CPUs, we use bm_check
  21. * mechanism as in UP case
  22. *
  23. * This routine is called only after all the CPUs are online
  24. */
  25. void acpi_processor_power_init_bm_check(struct acpi_processor_flags *flags,
  26. unsigned int cpu)
  27. {
  28. struct cpuinfo_x86 *c = &cpu_data(cpu);
  29. flags->bm_check = 0;
  30. if (num_online_cpus() == 1)
  31. flags->bm_check = 1;
  32. else if (c->x86_vendor == X86_VENDOR_INTEL) {
  33. /*
  34. * Today all MP CPUs that support C3 share cache.
  35. * And caches should not be flushed by software while
  36. * entering C3 type state.
  37. */
  38. flags->bm_check = 1;
  39. }
  40. /*
  41. * On all recent Intel platforms, ARB_DISABLE is a nop.
  42. * So, set bm_control to zero to indicate that ARB_DISABLE
  43. * is not required while entering C3 type state on
  44. * P4, Core and beyond CPUs
  45. */
  46. if (c->x86_vendor == X86_VENDOR_INTEL &&
  47. (c->x86 > 0xf || (c->x86 == 6 && c->x86_model >= 0x0f)))
  48. flags->bm_control = 0;
  49. }
  50. EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
  51. /* The code below handles cstate entry with monitor-mwait pair on Intel*/
  52. struct cstate_entry {
  53. struct {
  54. unsigned int eax;
  55. unsigned int ecx;
  56. } states[ACPI_PROCESSOR_MAX_POWER];
  57. };
  58. static struct cstate_entry __percpu *cpu_cstate_entry; /* per CPU ptr */
  59. static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
  60. #define NATIVE_CSTATE_BEYOND_HALT (2)
  61. static long acpi_processor_ffh_cstate_probe_cpu(void *_cx)
  62. {
  63. struct acpi_processor_cx *cx = _cx;
  64. long retval;
  65. unsigned int eax, ebx, ecx, edx;
  66. unsigned int edx_part;
  67. unsigned int cstate_type; /* C-state type and not ACPI C-state type */
  68. unsigned int num_cstate_subtype;
  69. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  70. /* Check whether this particular cx_type (in CST) is supported or not */
  71. cstate_type = ((cx->address >> MWAIT_SUBSTATE_SIZE) &
  72. MWAIT_CSTATE_MASK) + 1;
  73. edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
  74. num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
  75. retval = 0;
  76. /* If the HW does not support any sub-states in this C-state */
  77. if (num_cstate_subtype == 0) {
  78. pr_warn(FW_BUG "ACPI MWAIT C-state 0x%x not supported by HW (0x%x)\n",
  79. cx->address, edx_part);
  80. retval = -1;
  81. goto out;
  82. }
  83. /* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
  84. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  85. !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
  86. retval = -1;
  87. goto out;
  88. }
  89. if (!mwait_supported[cstate_type]) {
  90. mwait_supported[cstate_type] = 1;
  91. printk(KERN_DEBUG
  92. "Monitor-Mwait will be used to enter C-%d state\n",
  93. cx->type);
  94. }
  95. snprintf(cx->desc,
  96. ACPI_CX_DESC_LEN, "ACPI FFH MWAIT 0x%x",
  97. cx->address);
  98. out:
  99. return retval;
  100. }
  101. int acpi_processor_ffh_cstate_probe(unsigned int cpu,
  102. struct acpi_processor_cx *cx, struct acpi_power_register *reg)
  103. {
  104. struct cstate_entry *percpu_entry;
  105. struct cpuinfo_x86 *c = &cpu_data(cpu);
  106. long retval;
  107. if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF)
  108. return -1;
  109. if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
  110. return -1;
  111. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  112. percpu_entry->states[cx->index].eax = 0;
  113. percpu_entry->states[cx->index].ecx = 0;
  114. /* Make sure we are running on right CPU */
  115. retval = call_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx,
  116. false);
  117. if (retval == 0) {
  118. /* Use the hint in CST */
  119. percpu_entry->states[cx->index].eax = cx->address;
  120. percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
  121. }
  122. /*
  123. * For _CST FFH on Intel, if GAS.access_size bit 1 is cleared,
  124. * then we should skip checking BM_STS for this C-state.
  125. * ref: "Intel Processor Vendor-Specific ACPI Interface Specification"
  126. */
  127. if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
  128. cx->bm_sts_skip = 1;
  129. return retval;
  130. }
  131. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
  132. void __cpuidle acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
  133. {
  134. unsigned int cpu = smp_processor_id();
  135. struct cstate_entry *percpu_entry;
  136. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  137. mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
  138. percpu_entry->states[cx->index].ecx);
  139. }
  140. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
  141. static int __init ffh_cstate_init(void)
  142. {
  143. struct cpuinfo_x86 *c = &boot_cpu_data;
  144. if (c->x86_vendor != X86_VENDOR_INTEL &&
  145. c->x86_vendor != X86_VENDOR_AMD)
  146. return -1;
  147. cpu_cstate_entry = alloc_percpu(struct cstate_entry);
  148. return 0;
  149. }
  150. static void __exit ffh_cstate_exit(void)
  151. {
  152. free_percpu(cpu_cstate_entry);
  153. cpu_cstate_entry = NULL;
  154. }
  155. arch_initcall(ffh_cstate_init);
  156. __exitcall(ffh_cstate_exit);