cstate.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * arch/i386/kernel/acpi/cstate.c
  3. *
  4. * Copyright (C) 2005 Intel Corporation
  5. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  6. * - Added _PDC for SMP C-states on Intel CPUs
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/acpi.h>
  12. #include <linux/cpu.h>
  13. #include <acpi/processor.h>
  14. #include <asm/acpi.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 CPUs that support C3 share cache.
  35. * TBD: This needs to look at cache shared map, once
  36. * multi-core detection patch makes to the base.
  37. */
  38. flags->bm_check = 1;
  39. }
  40. }
  41. EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
  42. /* The code below handles cstate entry with monitor-mwait pair on Intel*/
  43. struct cstate_entry_s {
  44. struct {
  45. unsigned int eax;
  46. unsigned int ecx;
  47. } states[ACPI_PROCESSOR_MAX_POWER];
  48. };
  49. static struct cstate_entry_s *cpu_cstate_entry; /* per CPU ptr */
  50. static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
  51. #define MWAIT_SUBSTATE_MASK (0xf)
  52. #define MWAIT_SUBSTATE_SIZE (4)
  53. #define CPUID_MWAIT_LEAF (5)
  54. #define CPUID5_ECX_EXTENSIONS_SUPPORTED (0x1)
  55. #define CPUID5_ECX_INTERRUPT_BREAK (0x2)
  56. #define MWAIT_ECX_INTERRUPT_BREAK (0x1)
  57. #define NATIVE_CSTATE_BEYOND_HALT (2)
  58. int acpi_processor_ffh_cstate_probe(unsigned int cpu,
  59. struct acpi_processor_cx *cx, struct acpi_power_register *reg)
  60. {
  61. struct cstate_entry_s *percpu_entry;
  62. struct cpuinfo_x86 *c = cpu_data + cpu;
  63. cpumask_t saved_mask;
  64. int 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. if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF )
  70. return -1;
  71. if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
  72. return -1;
  73. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  74. percpu_entry->states[cx->index].eax = 0;
  75. percpu_entry->states[cx->index].ecx = 0;
  76. /* Make sure we are running on right CPU */
  77. saved_mask = current->cpus_allowed;
  78. retval = set_cpus_allowed(current, cpumask_of_cpu(cpu));
  79. if (retval)
  80. return -1;
  81. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  82. /* Check whether this particular cx_type (in CST) is supported or not */
  83. cstate_type = (cx->address >> MWAIT_SUBSTATE_SIZE) + 1;
  84. edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
  85. num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
  86. retval = 0;
  87. if (num_cstate_subtype < (cx->address & MWAIT_SUBSTATE_MASK)) {
  88. retval = -1;
  89. goto out;
  90. }
  91. /* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
  92. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  93. !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
  94. retval = -1;
  95. goto out;
  96. }
  97. percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
  98. /* Use the hint in CST */
  99. percpu_entry->states[cx->index].eax = cx->address;
  100. if (!mwait_supported[cstate_type]) {
  101. mwait_supported[cstate_type] = 1;
  102. printk(KERN_DEBUG "Monitor-Mwait will be used to enter C-%d "
  103. "state\n", cx->type);
  104. }
  105. out:
  106. set_cpus_allowed(current, saved_mask);
  107. return retval;
  108. }
  109. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
  110. void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
  111. {
  112. unsigned int cpu = smp_processor_id();
  113. struct cstate_entry_s *percpu_entry;
  114. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  115. mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
  116. percpu_entry->states[cx->index].ecx);
  117. }
  118. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
  119. static int __init ffh_cstate_init(void)
  120. {
  121. struct cpuinfo_x86 *c = &boot_cpu_data;
  122. if (c->x86_vendor != X86_VENDOR_INTEL)
  123. return -1;
  124. cpu_cstate_entry = alloc_percpu(struct cstate_entry_s);
  125. return 0;
  126. }
  127. static void __exit ffh_cstate_exit(void)
  128. {
  129. if (cpu_cstate_entry) {
  130. free_percpu(cpu_cstate_entry);
  131. cpu_cstate_entry = NULL;
  132. }
  133. }
  134. arch_initcall(ffh_cstate_init);
  135. __exitcall(ffh_cstate_exit);