cstate.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005 Intel Corporation
  4. * Venkatesh Pallipadi <[email protected]>
  5. * - Added _PDC for SMP C-states on Intel CPUs
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/export.h>
  9. #include <linux/init.h>
  10. #include <linux/acpi.h>
  11. #include <linux/cpu.h>
  12. #include <linux/sched.h>
  13. #include <acpi/processor.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. * For all recent Centaur CPUs, the ucode will make sure that each
  52. * core can keep cache coherence with each other while entering C3
  53. * type state. So, set bm_check to 1 to indicate that the kernel
  54. * doesn't need to execute a cache flush operation (WBINVD) when
  55. * entering C3 type state.
  56. */
  57. if (c->x86_vendor == X86_VENDOR_CENTAUR) {
  58. if (c->x86 > 6 || (c->x86 == 6 && c->x86_model == 0x0f &&
  59. c->x86_stepping >= 0x0e))
  60. flags->bm_check = 1;
  61. }
  62. if (c->x86_vendor == X86_VENDOR_ZHAOXIN) {
  63. /*
  64. * All Zhaoxin CPUs that support C3 share cache.
  65. * And caches should not be flushed by software while
  66. * entering C3 type state.
  67. */
  68. flags->bm_check = 1;
  69. /*
  70. * On all recent Zhaoxin platforms, ARB_DISABLE is a nop.
  71. * So, set bm_control to zero to indicate that ARB_DISABLE
  72. * is not required while entering C3 type state.
  73. */
  74. flags->bm_control = 0;
  75. }
  76. if (c->x86_vendor == X86_VENDOR_AMD && c->x86 >= 0x17) {
  77. /*
  78. * For all AMD Zen or newer CPUs that support C3, caches
  79. * should not be flushed by software while entering C3
  80. * type state. Set bm->check to 1 so that kernel doesn't
  81. * need to execute cache flush operation.
  82. */
  83. flags->bm_check = 1;
  84. /*
  85. * In current AMD C state implementation ARB_DIS is no longer
  86. * used. So set bm_control to zero to indicate ARB_DIS is not
  87. * required while entering C3 type state.
  88. */
  89. flags->bm_control = 0;
  90. }
  91. }
  92. EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
  93. /* The code below handles cstate entry with monitor-mwait pair on Intel*/
  94. struct cstate_entry {
  95. struct {
  96. unsigned int eax;
  97. unsigned int ecx;
  98. } states[ACPI_PROCESSOR_MAX_POWER];
  99. };
  100. static struct cstate_entry __percpu *cpu_cstate_entry; /* per CPU ptr */
  101. static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
  102. #define NATIVE_CSTATE_BEYOND_HALT (2)
  103. static long acpi_processor_ffh_cstate_probe_cpu(void *_cx)
  104. {
  105. struct acpi_processor_cx *cx = _cx;
  106. long retval;
  107. unsigned int eax, ebx, ecx, edx;
  108. unsigned int edx_part;
  109. unsigned int cstate_type; /* C-state type and not ACPI C-state type */
  110. unsigned int num_cstate_subtype;
  111. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  112. /* Check whether this particular cx_type (in CST) is supported or not */
  113. cstate_type = ((cx->address >> MWAIT_SUBSTATE_SIZE) &
  114. MWAIT_CSTATE_MASK) + 1;
  115. edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
  116. num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
  117. retval = 0;
  118. /* If the HW does not support any sub-states in this C-state */
  119. if (num_cstate_subtype == 0) {
  120. pr_warn(FW_BUG "ACPI MWAIT C-state 0x%x not supported by HW (0x%x)\n",
  121. cx->address, edx_part);
  122. retval = -1;
  123. goto out;
  124. }
  125. /* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
  126. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  127. !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
  128. retval = -1;
  129. goto out;
  130. }
  131. if (!mwait_supported[cstate_type]) {
  132. mwait_supported[cstate_type] = 1;
  133. printk(KERN_DEBUG
  134. "Monitor-Mwait will be used to enter C-%d state\n",
  135. cx->type);
  136. }
  137. snprintf(cx->desc,
  138. ACPI_CX_DESC_LEN, "ACPI FFH MWAIT 0x%x",
  139. cx->address);
  140. out:
  141. return retval;
  142. }
  143. int acpi_processor_ffh_cstate_probe(unsigned int cpu,
  144. struct acpi_processor_cx *cx, struct acpi_power_register *reg)
  145. {
  146. struct cstate_entry *percpu_entry;
  147. struct cpuinfo_x86 *c = &cpu_data(cpu);
  148. long retval;
  149. if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF)
  150. return -1;
  151. if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
  152. return -1;
  153. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  154. percpu_entry->states[cx->index].eax = 0;
  155. percpu_entry->states[cx->index].ecx = 0;
  156. /* Make sure we are running on right CPU */
  157. retval = call_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx,
  158. false);
  159. if (retval == 0) {
  160. /* Use the hint in CST */
  161. percpu_entry->states[cx->index].eax = cx->address;
  162. percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
  163. }
  164. /*
  165. * For _CST FFH on Intel, if GAS.access_size bit 1 is cleared,
  166. * then we should skip checking BM_STS for this C-state.
  167. * ref: "Intel Processor Vendor-Specific ACPI Interface Specification"
  168. */
  169. if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
  170. cx->bm_sts_skip = 1;
  171. return retval;
  172. }
  173. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
  174. void __cpuidle acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
  175. {
  176. unsigned int cpu = smp_processor_id();
  177. struct cstate_entry *percpu_entry;
  178. percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
  179. mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
  180. percpu_entry->states[cx->index].ecx);
  181. }
  182. EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
  183. static int __init ffh_cstate_init(void)
  184. {
  185. struct cpuinfo_x86 *c = &boot_cpu_data;
  186. if (c->x86_vendor != X86_VENDOR_INTEL &&
  187. c->x86_vendor != X86_VENDOR_AMD &&
  188. c->x86_vendor != X86_VENDOR_HYGON)
  189. return -1;
  190. cpu_cstate_entry = alloc_percpu(struct cstate_entry);
  191. return 0;
  192. }
  193. static void __exit ffh_cstate_exit(void)
  194. {
  195. free_percpu(cpu_cstate_entry);
  196. cpu_cstate_entry = NULL;
  197. }
  198. arch_initcall(ffh_cstate_init);
  199. __exitcall(ffh_cstate_exit);