umwait.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/syscore_ops.h>
  3. #include <linux/suspend.h>
  4. #include <linux/cpu.h>
  5. #include <asm/msr.h>
  6. #include <asm/mwait.h>
  7. #define UMWAIT_C02_ENABLE 0
  8. #define UMWAIT_CTRL_VAL(max_time, c02_disable) \
  9. (((max_time) & MSR_IA32_UMWAIT_CONTROL_TIME_MASK) | \
  10. ((c02_disable) & MSR_IA32_UMWAIT_CONTROL_C02_DISABLE))
  11. /*
  12. * Cache IA32_UMWAIT_CONTROL MSR. This is a systemwide control. By default,
  13. * umwait max time is 100000 in TSC-quanta and C0.2 is enabled
  14. */
  15. static u32 umwait_control_cached = UMWAIT_CTRL_VAL(100000, UMWAIT_C02_ENABLE);
  16. /*
  17. * Cache the original IA32_UMWAIT_CONTROL MSR value which is configured by
  18. * hardware or BIOS before kernel boot.
  19. */
  20. static u32 orig_umwait_control_cached __ro_after_init;
  21. /*
  22. * Serialize access to umwait_control_cached and IA32_UMWAIT_CONTROL MSR in
  23. * the sysfs write functions.
  24. */
  25. static DEFINE_MUTEX(umwait_lock);
  26. static void umwait_update_control_msr(void * unused)
  27. {
  28. lockdep_assert_irqs_disabled();
  29. wrmsr(MSR_IA32_UMWAIT_CONTROL, READ_ONCE(umwait_control_cached), 0);
  30. }
  31. /*
  32. * The CPU hotplug callback sets the control MSR to the global control
  33. * value.
  34. *
  35. * Disable interrupts so the read of umwait_control_cached and the WRMSR
  36. * are protected against a concurrent sysfs write. Otherwise the sysfs
  37. * write could update the cached value after it had been read on this CPU
  38. * and issue the IPI before the old value had been written. The IPI would
  39. * interrupt, write the new value and after return from IPI the previous
  40. * value would be written by this CPU.
  41. *
  42. * With interrupts disabled the upcoming CPU either sees the new control
  43. * value or the IPI is updating this CPU to the new control value after
  44. * interrupts have been reenabled.
  45. */
  46. static int umwait_cpu_online(unsigned int cpu)
  47. {
  48. local_irq_disable();
  49. umwait_update_control_msr(NULL);
  50. local_irq_enable();
  51. return 0;
  52. }
  53. /*
  54. * The CPU hotplug callback sets the control MSR to the original control
  55. * value.
  56. */
  57. static int umwait_cpu_offline(unsigned int cpu)
  58. {
  59. /*
  60. * This code is protected by the CPU hotplug already and
  61. * orig_umwait_control_cached is never changed after it caches
  62. * the original control MSR value in umwait_init(). So there
  63. * is no race condition here.
  64. */
  65. wrmsr(MSR_IA32_UMWAIT_CONTROL, orig_umwait_control_cached, 0);
  66. return 0;
  67. }
  68. /*
  69. * On resume, restore IA32_UMWAIT_CONTROL MSR on the boot processor which
  70. * is the only active CPU at this time. The MSR is set up on the APs via the
  71. * CPU hotplug callback.
  72. *
  73. * This function is invoked on resume from suspend and hibernation. On
  74. * resume from suspend the restore should be not required, but we neither
  75. * trust the firmware nor does it matter if the same value is written
  76. * again.
  77. */
  78. static void umwait_syscore_resume(void)
  79. {
  80. umwait_update_control_msr(NULL);
  81. }
  82. static struct syscore_ops umwait_syscore_ops = {
  83. .resume = umwait_syscore_resume,
  84. };
  85. /* sysfs interface */
  86. /*
  87. * When bit 0 in IA32_UMWAIT_CONTROL MSR is 1, C0.2 is disabled.
  88. * Otherwise, C0.2 is enabled.
  89. */
  90. static inline bool umwait_ctrl_c02_enabled(u32 ctrl)
  91. {
  92. return !(ctrl & MSR_IA32_UMWAIT_CONTROL_C02_DISABLE);
  93. }
  94. static inline u32 umwait_ctrl_max_time(u32 ctrl)
  95. {
  96. return ctrl & MSR_IA32_UMWAIT_CONTROL_TIME_MASK;
  97. }
  98. static inline void umwait_update_control(u32 maxtime, bool c02_enable)
  99. {
  100. u32 ctrl = maxtime & MSR_IA32_UMWAIT_CONTROL_TIME_MASK;
  101. if (!c02_enable)
  102. ctrl |= MSR_IA32_UMWAIT_CONTROL_C02_DISABLE;
  103. WRITE_ONCE(umwait_control_cached, ctrl);
  104. /* Propagate to all CPUs */
  105. on_each_cpu(umwait_update_control_msr, NULL, 1);
  106. }
  107. static ssize_t
  108. enable_c02_show(struct device *dev, struct device_attribute *attr, char *buf)
  109. {
  110. u32 ctrl = READ_ONCE(umwait_control_cached);
  111. return sprintf(buf, "%d\n", umwait_ctrl_c02_enabled(ctrl));
  112. }
  113. static ssize_t enable_c02_store(struct device *dev,
  114. struct device_attribute *attr,
  115. const char *buf, size_t count)
  116. {
  117. bool c02_enable;
  118. u32 ctrl;
  119. int ret;
  120. ret = kstrtobool(buf, &c02_enable);
  121. if (ret)
  122. return ret;
  123. mutex_lock(&umwait_lock);
  124. ctrl = READ_ONCE(umwait_control_cached);
  125. if (c02_enable != umwait_ctrl_c02_enabled(ctrl))
  126. umwait_update_control(ctrl, c02_enable);
  127. mutex_unlock(&umwait_lock);
  128. return count;
  129. }
  130. static DEVICE_ATTR_RW(enable_c02);
  131. static ssize_t
  132. max_time_show(struct device *kobj, struct device_attribute *attr, char *buf)
  133. {
  134. u32 ctrl = READ_ONCE(umwait_control_cached);
  135. return sprintf(buf, "%u\n", umwait_ctrl_max_time(ctrl));
  136. }
  137. static ssize_t max_time_store(struct device *kobj,
  138. struct device_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. u32 max_time, ctrl;
  142. int ret;
  143. ret = kstrtou32(buf, 0, &max_time);
  144. if (ret)
  145. return ret;
  146. /* bits[1:0] must be zero */
  147. if (max_time & ~MSR_IA32_UMWAIT_CONTROL_TIME_MASK)
  148. return -EINVAL;
  149. mutex_lock(&umwait_lock);
  150. ctrl = READ_ONCE(umwait_control_cached);
  151. if (max_time != umwait_ctrl_max_time(ctrl))
  152. umwait_update_control(max_time, umwait_ctrl_c02_enabled(ctrl));
  153. mutex_unlock(&umwait_lock);
  154. return count;
  155. }
  156. static DEVICE_ATTR_RW(max_time);
  157. static struct attribute *umwait_attrs[] = {
  158. &dev_attr_enable_c02.attr,
  159. &dev_attr_max_time.attr,
  160. NULL
  161. };
  162. static struct attribute_group umwait_attr_group = {
  163. .attrs = umwait_attrs,
  164. .name = "umwait_control",
  165. };
  166. static int __init umwait_init(void)
  167. {
  168. struct device *dev;
  169. int ret;
  170. if (!boot_cpu_has(X86_FEATURE_WAITPKG))
  171. return -ENODEV;
  172. /*
  173. * Cache the original control MSR value before the control MSR is
  174. * changed. This is the only place where orig_umwait_control_cached
  175. * is modified.
  176. */
  177. rdmsrl(MSR_IA32_UMWAIT_CONTROL, orig_umwait_control_cached);
  178. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "umwait:online",
  179. umwait_cpu_online, umwait_cpu_offline);
  180. if (ret < 0) {
  181. /*
  182. * On failure, the control MSR on all CPUs has the
  183. * original control value.
  184. */
  185. return ret;
  186. }
  187. register_syscore_ops(&umwait_syscore_ops);
  188. /*
  189. * Add umwait control interface. Ignore failure, so at least the
  190. * default values are set up in case the machine manages to boot.
  191. */
  192. dev = cpu_subsys.dev_root;
  193. return sysfs_create_group(&dev->kobj, &umwait_attr_group);
  194. }
  195. device_initcall(umwait_init);