[PATCH 09/15] hyperv: unify Hyper-V msr definitions

From: Roman Kagan
Date: Tue Dec 20 2016 - 14:12:41 EST


Use the definitions already present in the uapi header. Besides, drop
all bitfields for the msr values and use bitwise operations instead.

Signed-off-by: Roman Kagan <rkagan@xxxxxxxxxxxxx>
---
drivers/hv/hyperv_vmbus.h | 88 ---------------------------
drivers/hv/hv.c | 150 ++++++++++++++++++----------------------------
2 files changed, 59 insertions(+), 179 deletions(-)

diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h
index 7bf1b10..ac73832 100644
--- a/drivers/hv/hyperv_vmbus.h
+++ b/drivers/hv/hyperv_vmbus.h
@@ -94,63 +94,6 @@ struct hv_connection_info {
};
};

-/*
- * Timer configuration register.
- */
-union hv_timer_config {
- u64 as_uint64;
- struct {
- u64 enable:1;
- u64 periodic:1;
- u64 lazy:1;
- u64 auto_enable:1;
- u64 reserved_z0:12;
- u64 sintx:4;
- u64 reserved_z1:44;
- };
-};
-
-/* Define SynIC control register. */
-union hv_synic_scontrol {
- u64 as_uint64;
- struct {
- u64 enable:1;
- u64 reserved:63;
- };
-};
-
-/* Define synthetic interrupt source. */
-union hv_synic_sint {
- u64 as_uint64;
- struct {
- u64 vector:8;
- u64 reserved1:8;
- u64 masked:1;
- u64 auto_eoi:1;
- u64 reserved2:46;
- };
-};
-
-/* Define the format of the SIMP register */
-union hv_synic_simp {
- u64 as_uint64;
- struct {
- u64 simp_enabled:1;
- u64 preserved:11;
- u64 base_simp_gpa:52;
- };
-};
-
-/* Define the format of the SIEFP register */
-union hv_synic_siefp {
- u64 as_uint64;
- struct {
- u64 siefp_enabled:1;
- u64 preserved:11;
- u64 base_siefp_gpa:52;
- };
-};
-
/* Definitions for the monitored notification facility */
union hv_monitor_trigger_group {
u64 as_uint64;
@@ -239,37 +182,6 @@ enum hv_guest_os_microsoft_ids {
HVGUESTOS_MICROSOFT_WINDOWSCE = 0x05
};

-/*
- * Declare the MSR used to identify the guest OS.
- */
-#define HV_X64_MSR_GUEST_OS_ID 0x40000000
-
-union hv_x64_msr_guest_os_id_contents {
- u64 as_uint64;
- struct {
- u64 build_number:16;
- u64 service_version:8; /* Service Pack, etc. */
- u64 minor_version:8;
- u64 major_version:8;
- u64 os_id:8; /* enum hv_guest_os_microsoft_ids (if Vendor=MS) */
- u64 vendor_id:16; /* enum hv_guest_os_vendor */
- };
-};
-
-/*
- * Declare the MSR used to setup pages used to communicate with the hypervisor.
- */
-#define HV_X64_MSR_HYPERCALL 0x40000001
-
-union hv_x64_msr_hypercall_contents {
- u64 as_uint64;
- struct {
- u64 enable:1;
- u64 reserved:11;
- u64 guest_physical_address:52;
- };
-};
-

enum {
VMBUS_MESSAGE_CONNECTION_ID = 1,
diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c
index dddba07..7d2a3d1 100644
--- a/drivers/hv/hv.c
+++ b/drivers/hv/hv.c
@@ -181,7 +181,7 @@ static struct clocksource hyperv_cs_tsc = {
int hv_init(void)
{
int max_leaf;
- union hv_x64_msr_hypercall_contents hypercall_msr;
+ u64 hypercall_msr = 0;
void *virtaddr = NULL;

memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
@@ -206,30 +206,24 @@ int hv_init(void)
hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);

- /* See if the hypercall page is already set */
- rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
-
virtaddr = (void *)get_zeroed_page(GFP_KERNEL);
if (!virtaddr || set_memory_x((unsigned long)virtaddr, 1))
goto cleanup;
hv_context.hypercall_page = virtaddr;

- hypercall_msr.enable = 1;
-
- hypercall_msr.guest_physical_address =
- virt_to_phys(virtaddr) >> PAGE_SHIFT;
- wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+ rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr);
+ hypercall_msr &= PAGE_MASK;
+ hypercall_msr = HV_X64_MSR_HYPERCALL_ENABLE | virt_to_phys(virtaddr);
+ wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr);

/* Confirm that hypercall page did get setup. */
- hypercall_msr.as_uint64 = 0;
- rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
-
- if (!hypercall_msr.enable)
+ rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr);
+ if (!(hypercall_msr & HV_X64_MSR_HYPERCALL_ENABLE))
goto cleanup;

#ifdef CONFIG_X86_64
if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
- union hv_x64_msr_hypercall_contents tsc_msr;
+ u64 tsc_msr;
void *va_tsc;

va_tsc = (void *)get_zeroed_page(GFP_KERNEL);
@@ -237,21 +231,19 @@ int hv_init(void)
goto cleanup;
hv_context.tsc_page = va_tsc;

- rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
-
- tsc_msr.enable = 1;
- tsc_msr.guest_physical_address =
- virt_to_phys(va_tsc) >> PAGE_SHIFT;
-
- wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
+ rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr);
+ tsc_msr &= PAGE_MASK;
+ tsc_msr |= HV_X64_MSR_TSC_REFERENCE_ENABLE |
+ virt_to_phys(va_tsc);
+ wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr);
clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
}
#endif
return 0;

cleanup:
- hypercall_msr.as_uint64 = 0;
- wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+ hypercall_msr &= ~HV_X64_MSR_HYPERCALL_ENABLE;
+ wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr);
free_page((unsigned long)virtaddr);

return -ENOTSUPP;
@@ -264,13 +256,14 @@ int hv_init(void)
*/
void hv_cleanup(bool crash)
{
- union hv_x64_msr_hypercall_contents hypercall_msr;
+ u64 msr;

/* Reset our OS id */
wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);

- hypercall_msr.as_uint64 = 0;
- wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+ rdmsrl(HV_X64_MSR_HYPERCALL, msr);
+ msr &= ~HV_X64_MSR_HYPERCALL_ENABLE;
+ wrmsrl(HV_X64_MSR_HYPERCALL, msr);
if (!crash)
free_page((unsigned long)hv_context.hypercall_page);
hv_context.hypercall_page = NULL;
@@ -289,8 +282,9 @@ void hv_cleanup(bool crash)
clocksource_unregister(&hyperv_cs_tsc);
}

- hypercall_msr.as_uint64 = 0;
- wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
+ rdmsrl(HV_X64_MSR_REFERENCE_TSC, msr);
+ msr &= ~HV_X64_MSR_TSC_REFERENCE_ENABLE;
+ wrmsrl(HV_X64_MSR_REFERENCE_TSC, msr);
if (!crash)
free_page((unsigned long)hv_context.tsc_page);
hv_context.tsc_page = NULL;
@@ -352,12 +346,10 @@ static int hv_ce_shutdown(struct clock_event_device *evt)

static int hv_ce_set_oneshot(struct clock_event_device *evt)
{
- union hv_timer_config timer_cfg;
+ u64 timer_cfg = HV_STIMER_ENABLE | HV_STIMER_AUTOENABLE |
+ (VMBUS_MESSAGE_SINT << 16);

- timer_cfg.enable = 1;
- timer_cfg.auto_enable = 1;
- timer_cfg.sintx = VMBUS_MESSAGE_SINT;
- wrmsrl(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
+ wrmsrl(HV_X64_MSR_STIMER0_CONFIG, timer_cfg);

return 0;
}
@@ -474,52 +466,39 @@ void hv_synic_free(void)
*/
void hv_synic_init(void *arg)
{
- u64 version;
- union hv_synic_simp simp;
- union hv_synic_siefp siefp;
- union hv_synic_sint shared_sint;
- union hv_synic_scontrol sctrl;
- u64 vp_index;
-
+ u64 msr;
int cpu = smp_processor_id();

if (!hv_context.hypercall_page)
return;

/* Check the version */
- rdmsrl(HV_X64_MSR_SVERSION, version);
+ rdmsrl(HV_X64_MSR_SVERSION, msr);

/* Setup the Synic's message page */
- rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
- simp.simp_enabled = 1;
- simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
- >> PAGE_SHIFT;
-
- wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
+ rdmsrl(HV_X64_MSR_SIMP, msr);
+ msr &= PAGE_MASK;
+ msr |= virt_to_phys(hv_context.synic_message_page[cpu]) |
+ HV_SYNIC_SIMP_ENABLE;
+ wrmsrl(HV_X64_MSR_SIMP, msr);

/* Setup the Synic's event page */
- rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
- siefp.siefp_enabled = 1;
- siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
- >> PAGE_SHIFT;
-
- wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
+ rdmsrl(HV_X64_MSR_SIEFP, msr);
+ msr &= PAGE_MASK;
+ msr |= virt_to_phys(hv_context.synic_event_page[cpu]) |
+ HV_SYNIC_SIEFP_ENABLE;
+ wrmsrl(HV_X64_MSR_SIEFP, msr);

/* Setup the shared SINT. */
- rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
-
- shared_sint.as_uint64 = 0;
- shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
- shared_sint.masked = false;
- shared_sint.auto_eoi = true;
-
- wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
+ rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, msr);
+ msr &= ~(HV_SYNIC_SINT_MASKED | HV_SYNIC_SINT_VECTOR_MASK);
+ msr |= HYPERVISOR_CALLBACK_VECTOR | HV_SYNIC_SINT_AUTO_EOI;
+ wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, msr);

/* Enable the global synic bit */
- rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
- sctrl.enable = 1;
-
- wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
+ rdmsrl(HV_X64_MSR_SCONTROL, msr);
+ msr |= HV_SYNIC_CONTROL_ENABLE;
+ wrmsrl(HV_X64_MSR_SCONTROL, msr);

hv_context.synic_initialized = true;

@@ -528,8 +507,8 @@ void hv_synic_init(void *arg)
* of cpuid and Linux' notion of cpuid.
* This array will be indexed using Linux cpuid.
*/
- rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
- hv_context.vp_index[cpu] = (u32)vp_index;
+ rdmsrl(HV_X64_MSR_VP_INDEX, msr);
+ hv_context.vp_index[cpu] = (u32)msr;

INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);

@@ -563,10 +542,7 @@ void hv_synic_clockevents_cleanup(void)
*/
void hv_synic_cleanup(void *arg)
{
- union hv_synic_sint shared_sint;
- union hv_synic_simp simp;
- union hv_synic_siefp siefp;
- union hv_synic_scontrol sctrl;
+ u64 msr;
int cpu = smp_processor_id();

if (!hv_context.synic_initialized)
@@ -578,28 +554,20 @@ void hv_synic_cleanup(void *arg)
hv_ce_shutdown(hv_context.clk_evt[cpu]);
}

- rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
-
- shared_sint.masked = 1;
-
- /* Need to correctly cleanup in the case of SMP!!! */
- /* Disable the interrupt */
- wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
-
- rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
- simp.simp_enabled = 0;
- simp.base_simp_gpa = 0;
-
- wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
+ rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, msr);
+ msr |= HV_SYNIC_SINT_MASKED;
+ wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, msr);

- rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
- siefp.siefp_enabled = 0;
- siefp.base_siefp_gpa = 0;
+ rdmsrl(HV_X64_MSR_SIMP, msr);
+ msr &= ~HV_SYNIC_SIMP_ENABLE;
+ wrmsrl(HV_X64_MSR_SIMP, msr);

- wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
+ rdmsrl(HV_X64_MSR_SIEFP, msr);
+ msr &= ~HV_SYNIC_SIEFP_ENABLE;
+ wrmsrl(HV_X64_MSR_SIEFP, msr);

/* Disable the global synic bit */
- rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
- sctrl.enable = 0;
- wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
+ rdmsrl(HV_X64_MSR_SCONTROL, msr);
+ msr &= ~HV_SYNIC_CONTROL_ENABLE;
+ wrmsrl(HV_X64_MSR_SCONTROL, msr);
}
--
2.9.3