0001
0002 #include <linux/syscore_ops.h>
0003 #include <linux/suspend.h>
0004 #include <linux/cpu.h>
0005
0006 #include <asm/msr.h>
0007 #include <asm/mwait.h>
0008
0009 #define UMWAIT_C02_ENABLE 0
0010
0011 #define UMWAIT_CTRL_VAL(max_time, c02_disable) \
0012 (((max_time) & MSR_IA32_UMWAIT_CONTROL_TIME_MASK) | \
0013 ((c02_disable) & MSR_IA32_UMWAIT_CONTROL_C02_DISABLE))
0014
0015
0016
0017
0018
0019 static u32 umwait_control_cached = UMWAIT_CTRL_VAL(100000, UMWAIT_C02_ENABLE);
0020
0021
0022
0023
0024
0025 static u32 orig_umwait_control_cached __ro_after_init;
0026
0027
0028
0029
0030
0031 static DEFINE_MUTEX(umwait_lock);
0032
0033 static void umwait_update_control_msr(void * unused)
0034 {
0035 lockdep_assert_irqs_disabled();
0036 wrmsr(MSR_IA32_UMWAIT_CONTROL, READ_ONCE(umwait_control_cached), 0);
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 static int umwait_cpu_online(unsigned int cpu)
0055 {
0056 local_irq_disable();
0057 umwait_update_control_msr(NULL);
0058 local_irq_enable();
0059 return 0;
0060 }
0061
0062
0063
0064
0065
0066 static int umwait_cpu_offline(unsigned int cpu)
0067 {
0068
0069
0070
0071
0072
0073
0074 wrmsr(MSR_IA32_UMWAIT_CONTROL, orig_umwait_control_cached, 0);
0075
0076 return 0;
0077 }
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 static void umwait_syscore_resume(void)
0090 {
0091 umwait_update_control_msr(NULL);
0092 }
0093
0094 static struct syscore_ops umwait_syscore_ops = {
0095 .resume = umwait_syscore_resume,
0096 };
0097
0098
0099
0100
0101
0102
0103
0104 static inline bool umwait_ctrl_c02_enabled(u32 ctrl)
0105 {
0106 return !(ctrl & MSR_IA32_UMWAIT_CONTROL_C02_DISABLE);
0107 }
0108
0109 static inline u32 umwait_ctrl_max_time(u32 ctrl)
0110 {
0111 return ctrl & MSR_IA32_UMWAIT_CONTROL_TIME_MASK;
0112 }
0113
0114 static inline void umwait_update_control(u32 maxtime, bool c02_enable)
0115 {
0116 u32 ctrl = maxtime & MSR_IA32_UMWAIT_CONTROL_TIME_MASK;
0117
0118 if (!c02_enable)
0119 ctrl |= MSR_IA32_UMWAIT_CONTROL_C02_DISABLE;
0120
0121 WRITE_ONCE(umwait_control_cached, ctrl);
0122
0123 on_each_cpu(umwait_update_control_msr, NULL, 1);
0124 }
0125
0126 static ssize_t
0127 enable_c02_show(struct device *dev, struct device_attribute *attr, char *buf)
0128 {
0129 u32 ctrl = READ_ONCE(umwait_control_cached);
0130
0131 return sprintf(buf, "%d\n", umwait_ctrl_c02_enabled(ctrl));
0132 }
0133
0134 static ssize_t enable_c02_store(struct device *dev,
0135 struct device_attribute *attr,
0136 const char *buf, size_t count)
0137 {
0138 bool c02_enable;
0139 u32 ctrl;
0140 int ret;
0141
0142 ret = kstrtobool(buf, &c02_enable);
0143 if (ret)
0144 return ret;
0145
0146 mutex_lock(&umwait_lock);
0147
0148 ctrl = READ_ONCE(umwait_control_cached);
0149 if (c02_enable != umwait_ctrl_c02_enabled(ctrl))
0150 umwait_update_control(ctrl, c02_enable);
0151
0152 mutex_unlock(&umwait_lock);
0153
0154 return count;
0155 }
0156 static DEVICE_ATTR_RW(enable_c02);
0157
0158 static ssize_t
0159 max_time_show(struct device *kobj, struct device_attribute *attr, char *buf)
0160 {
0161 u32 ctrl = READ_ONCE(umwait_control_cached);
0162
0163 return sprintf(buf, "%u\n", umwait_ctrl_max_time(ctrl));
0164 }
0165
0166 static ssize_t max_time_store(struct device *kobj,
0167 struct device_attribute *attr,
0168 const char *buf, size_t count)
0169 {
0170 u32 max_time, ctrl;
0171 int ret;
0172
0173 ret = kstrtou32(buf, 0, &max_time);
0174 if (ret)
0175 return ret;
0176
0177
0178 if (max_time & ~MSR_IA32_UMWAIT_CONTROL_TIME_MASK)
0179 return -EINVAL;
0180
0181 mutex_lock(&umwait_lock);
0182
0183 ctrl = READ_ONCE(umwait_control_cached);
0184 if (max_time != umwait_ctrl_max_time(ctrl))
0185 umwait_update_control(max_time, umwait_ctrl_c02_enabled(ctrl));
0186
0187 mutex_unlock(&umwait_lock);
0188
0189 return count;
0190 }
0191 static DEVICE_ATTR_RW(max_time);
0192
0193 static struct attribute *umwait_attrs[] = {
0194 &dev_attr_enable_c02.attr,
0195 &dev_attr_max_time.attr,
0196 NULL
0197 };
0198
0199 static struct attribute_group umwait_attr_group = {
0200 .attrs = umwait_attrs,
0201 .name = "umwait_control",
0202 };
0203
0204 static int __init umwait_init(void)
0205 {
0206 struct device *dev;
0207 int ret;
0208
0209 if (!boot_cpu_has(X86_FEATURE_WAITPKG))
0210 return -ENODEV;
0211
0212
0213
0214
0215
0216
0217 rdmsrl(MSR_IA32_UMWAIT_CONTROL, orig_umwait_control_cached);
0218
0219 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "umwait:online",
0220 umwait_cpu_online, umwait_cpu_offline);
0221 if (ret < 0) {
0222
0223
0224
0225
0226 return ret;
0227 }
0228
0229 register_syscore_ops(&umwait_syscore_ops);
0230
0231
0232
0233
0234
0235 dev = cpu_subsys.dev_root;
0236 return sysfs_create_group(&dev->kobj, &umwait_attr_group);
0237 }
0238 device_initcall(umwait_init);