|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | #include <linux/types.h> |
| 21 | +#include <asm/apic.h> |
| 22 | +#include <asm/desc.h> |
21 | 23 | #include <asm/hypervisor.h> |
22 | 24 | #include <asm/hyperv.h> |
23 | 25 | #include <asm/mshyperv.h> |
@@ -102,6 +104,93 @@ static int hv_cpu_init(unsigned int cpu) |
102 | 104 | return 0; |
103 | 105 | } |
104 | 106 |
|
| 107 | +static void (*hv_reenlightenment_cb)(void); |
| 108 | + |
| 109 | +static void hv_reenlightenment_notify(struct work_struct *dummy) |
| 110 | +{ |
| 111 | + struct hv_tsc_emulation_status emu_status; |
| 112 | + |
| 113 | + rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 114 | + |
| 115 | + /* Don't issue the callback if TSC accesses are not emulated */ |
| 116 | + if (hv_reenlightenment_cb && emu_status.inprogress) |
| 117 | + hv_reenlightenment_cb(); |
| 118 | +} |
| 119 | +static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); |
| 120 | + |
| 121 | +void hyperv_stop_tsc_emulation(void) |
| 122 | +{ |
| 123 | + u64 freq; |
| 124 | + struct hv_tsc_emulation_status emu_status; |
| 125 | + |
| 126 | + rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 127 | + emu_status.inprogress = 0; |
| 128 | + wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 129 | + |
| 130 | + rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); |
| 131 | + tsc_khz = div64_u64(freq, 1000); |
| 132 | +} |
| 133 | +EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); |
| 134 | + |
| 135 | +static inline bool hv_reenlightenment_available(void) |
| 136 | +{ |
| 137 | + /* |
| 138 | + * Check for required features and priviliges to make TSC frequency |
| 139 | + * change notifications work. |
| 140 | + */ |
| 141 | + return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS && |
| 142 | + ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && |
| 143 | + ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT; |
| 144 | +} |
| 145 | + |
| 146 | +__visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs) |
| 147 | +{ |
| 148 | + entering_ack_irq(); |
| 149 | + |
| 150 | + schedule_delayed_work(&hv_reenlightenment_work, HZ/10); |
| 151 | + |
| 152 | + exiting_irq(); |
| 153 | +} |
| 154 | + |
| 155 | +void set_hv_tscchange_cb(void (*cb)(void)) |
| 156 | +{ |
| 157 | + struct hv_reenlightenment_control re_ctrl = { |
| 158 | + .vector = HYPERV_REENLIGHTENMENT_VECTOR, |
| 159 | + .enabled = 1, |
| 160 | + .target_vp = hv_vp_index[smp_processor_id()] |
| 161 | + }; |
| 162 | + struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; |
| 163 | + |
| 164 | + if (!hv_reenlightenment_available()) { |
| 165 | + pr_warn("Hyper-V: reenlightenment support is unavailable\n"); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + hv_reenlightenment_cb = cb; |
| 170 | + |
| 171 | + /* Make sure callback is registered before we write to MSRs */ |
| 172 | + wmb(); |
| 173 | + |
| 174 | + wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 175 | + wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); |
| 176 | +} |
| 177 | +EXPORT_SYMBOL_GPL(set_hv_tscchange_cb); |
| 178 | + |
| 179 | +void clear_hv_tscchange_cb(void) |
| 180 | +{ |
| 181 | + struct hv_reenlightenment_control re_ctrl; |
| 182 | + |
| 183 | + if (!hv_reenlightenment_available()) |
| 184 | + return; |
| 185 | + |
| 186 | + rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 187 | + re_ctrl.enabled = 0; |
| 188 | + wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 189 | + |
| 190 | + hv_reenlightenment_cb = NULL; |
| 191 | +} |
| 192 | +EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); |
| 193 | + |
105 | 194 | /* |
106 | 195 | * This function is to be invoked early in the boot sequence after the |
107 | 196 | * hypervisor has been detected. |
|
0 commit comments