cstate.c 4.8 KB

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