diff mbox series

[v7,07/10] arm64: hyperv: Initialize hypervisor on boot

Message ID 1598287583-71762-8-git-send-email-mikelley@microsoft.com (mailing list archive)
State New, archived
Headers show
Series Enable Linux guests on Hyper-V on ARM64 | expand

Commit Message

Michael Kelley (LINUX) Aug. 24, 2020, 4:46 p.m. UTC
Add ARM64-specific code to initialize the Hyper-V
hypervisor when booting as a guest VM. Provide functions
and data structures indicating hypervisor status that
are needed by VMbus driver.

This code is built only when CONFIG_HYPERV is enabled.

Signed-off-by: Michael Kelley <mikelley@microsoft.com>
---
 arch/arm64/hyperv/hv_core.c | 144 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 144 insertions(+)

Comments

Arnd Bergmann Aug. 24, 2020, 6:33 p.m. UTC | #1
On Mon, Aug 24, 2020 at 6:48 PM Michael Kelley <mikelley@microsoft.com> wrote:
>
>  /*
> + * This function is invoked via the ACPI clocksource probe mechanism. We
> + * don't actually use any values from the ACPI GTDT table, but we set up
> + * the Hyper-V synthetic clocksource and do other initialization for
> + * interacting with Hyper-V the first time.  Using early_initcall to invoke
> + * this function is too late because interrupts are already enabled at that
> + * point, and hv_init_clocksource() must run before interrupts are enabled.
> + *
> + * 1. Setup the guest ID.
> + * 2. Get features and hints info from Hyper-V
> + * 3. Setup per-cpu VP indices.
> + * 4. Initialize the Hyper-V clocksource.
> + */
> +
> +static int __init hyperv_init(struct acpi_table_header *table)
> +{
> +       struct hv_get_vp_registers_output result;
> +       u32     a, b, c, d;
> +       u64     guest_id;
> +       int     i, cpuhp;
> +
> +       /*
> +        * If we're in a VM on Hyper-V, the ACPI hypervisor_id field will
> +        * have the string "MsHyperV".
> +        */
> +       if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
> +               return -EINVAL;
> +
> +       /* Setup the guest ID */
> +       guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
> +       hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
> +
> +       /* Get the features and hints from Hyper-V */
> +       hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
> +       ms_hyperv.features = result.as32.a;
> +       ms_hyperv.misc_features = result.as32.c;
> +
> +       hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
> +       ms_hyperv.hints = result.as32.a;
> +
> +       pr_info("Hyper-V: Features 0x%x, hints 0x%x, misc 0x%x\n",
> +               ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features);
> +
> +       /*
> +        * If Hyper-V has crash notifications, set crash_kexec_post_notifiers
> +        * so that we will report the panic to Hyper-V before running kdump.
> +        */
> +       if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE)
> +               crash_kexec_post_notifiers = true;
> +
> +       /* Get information about the Hyper-V host version */
> +       hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
> +       a = result.as32.a;
> +       b = result.as32.b;
> +       c = result.as32.c;
> +       d = result.as32.d;
> +       pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
> +               b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
> +
> +       /* Allocate and initialize percpu VP index array */
> +       hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
> +                                   GFP_KERNEL);
> +       if (!hv_vp_index)
> +               return -ENOMEM;
> +
> +       for (i = 0; i < num_possible_cpus(); i++)
> +               hv_vp_index[i] = VP_INVAL;
> +
> +       cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
> +                       "arm64/hyperv_init:online", hv_cpu_init, NULL);
> +       if (cpuhp < 0)
> +               goto free_vp_index;
> +
> +       hv_init_clocksource();
> +       if (hv_stimer_alloc())
> +               goto remove_cpuhp_state;
> +
> +       hyperv_initialized = true;
> +       return 0;
> +
> +remove_cpuhp_state:
> +       cpuhp_remove_state(cpuhp);
> +free_vp_index:
> +       kfree(hv_vp_index);
> +       hv_vp_index = NULL;
> +       return -EINVAL;
> +}
> +TIMER_ACPI_DECLARE(hyperv, ACPI_SIG_GTDT, hyperv_init);

I think this has come up before, and I still don't consider it an acceptable
hack to hook platform initialization code into the timer code.

Please split out the timer into a standalone driver in drivers/clocksource
that can get reviewed by the clocksource maintainers.

      Arnd
Michael Kelley (LINUX) Aug. 25, 2020, 9:20 p.m. UTC | #2
From: Arnd Bergmann <arnd@arndb.de> Sent: Monday, August 24, 2020 11:34 AM
> 
> On Mon, Aug 24, 2020 at 6:48 PM Michael Kelley <mikelley@microsoft.com> wrote:
> >
> >  /*
> > + * This function is invoked via the ACPI clocksource probe mechanism. We
> > + * don't actually use any values from the ACPI GTDT table, but we set up
> > + * the Hyper-V synthetic clocksource and do other initialization for
> > + * interacting with Hyper-V the first time.  Using early_initcall to invoke
> > + * this function is too late because interrupts are already enabled at that
> > + * point, and hv_init_clocksource() must run before interrupts are enabled.
> > + *
> > + * 1. Setup the guest ID.
> > + * 2. Get features and hints info from Hyper-V
> > + * 3. Setup per-cpu VP indices.
> > + * 4. Initialize the Hyper-V clocksource.
> > + */
> > +
> > +static int __init hyperv_init(struct acpi_table_header *table)
> > +{
> > +       struct hv_get_vp_registers_output result;
> > +       u32     a, b, c, d;
> > +       u64     guest_id;
> > +       int     i, cpuhp;
> > +
> > +       /*
> > +        * If we're in a VM on Hyper-V, the ACPI hypervisor_id field will
> > +        * have the string "MsHyperV".
> > +        */
> > +       if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
> > +               return -EINVAL;
> > +
> > +       /* Setup the guest ID */
> > +       guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
> > +       hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
> > +
> > +       /* Get the features and hints from Hyper-V */
> > +       hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
> > +       ms_hyperv.features = result.as32.a;
> > +       ms_hyperv.misc_features = result.as32.c;
> > +
> > +       hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
> > +       ms_hyperv.hints = result.as32.a;
> > +
> > +       pr_info("Hyper-V: Features 0x%x, hints 0x%x, misc 0x%x\n",
> > +               ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features);
> > +
> > +       /*
> > +        * If Hyper-V has crash notifications, set crash_kexec_post_notifiers
> > +        * so that we will report the panic to Hyper-V before running kdump.
> > +        */
> > +       if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE)
> > +               crash_kexec_post_notifiers = true;
> > +
> > +       /* Get information about the Hyper-V host version */
> > +       hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
> > +       a = result.as32.a;
> > +       b = result.as32.b;
> > +       c = result.as32.c;
> > +       d = result.as32.d;
> > +       pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
> > +               b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
> > +
> > +       /* Allocate and initialize percpu VP index array */
> > +       hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
> > +                                   GFP_KERNEL);
> > +       if (!hv_vp_index)
> > +               return -ENOMEM;
> > +
> > +       for (i = 0; i < num_possible_cpus(); i++)
> > +               hv_vp_index[i] = VP_INVAL;
> > +
> > +       cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
> > +                       "arm64/hyperv_init:online", hv_cpu_init, NULL);
> > +       if (cpuhp < 0)
> > +               goto free_vp_index;
> > +
> > +       hv_init_clocksource();
> > +       if (hv_stimer_alloc())
> > +               goto remove_cpuhp_state;
> > +
> > +       hyperv_initialized = true;
> > +       return 0;
> > +
> > +remove_cpuhp_state:
> > +       cpuhp_remove_state(cpuhp);
> > +free_vp_index:
> > +       kfree(hv_vp_index);
> > +       hv_vp_index = NULL;
> > +       return -EINVAL;
> > +}
> > +TIMER_ACPI_DECLARE(hyperv, ACPI_SIG_GTDT, hyperv_init);
> 
> I think this has come up before, and I still don't consider it an acceptable
> hack to hook platform initialization code into the timer code.
> 
> Please split out the timer into a standalone driver in drivers/clocksource
> that can get reviewed by the clocksource maintainers.

I see two related topics here.  First, the Hyper-V clocksource driver is
drivers/clocksource/hyperv_timer.c.  The code is architecture independent
and is used today on the x86 side and for ARM64 in this patch series.  A few
architecture specific calls are satisfied by code under arch/x86, and in this
patch series, under arch/arm64.  Is there some aspect of this driver that
needs reconsideration?  I just want to make sure to understand what you
are getting at.

Second is the question of where/how to do Hyper-V specific initialization.
I agree that hanging it off the timer initialization isn't a great approach.
Should I add a Hyper-V specific initialization call at the appropriate point
in the ARM64 init sequence?  The x86 side has some structure for handling
multiple hypervisors, and the Hyper-V initialization code naturally plugs into
that structure.  I'm certainly open to suggestions on the best way to handle
it for ARM64.

Michael

> 
>       Arnd
Arnd Bergmann Aug. 26, 2020, 7:18 a.m. UTC | #3
On Tue, Aug 25, 2020 at 11:20 PM Michael Kelley <mikelley@microsoft.com> wrote:
> From: Arnd Bergmann <arnd@arndb.de> Sent: Monday, August 24, 2020 11:34 AM
> > On Mon, Aug 24, 2020 at 6:48 PM Michael Kelley <mikelley@microsoft.com> wrote:
> >
> > I think this has come up before, and I still don't consider it an acceptable
> > hack to hook platform initialization code into the timer code.
> >
> > Please split out the timer into a standalone driver in drivers/clocksource
> > that can get reviewed by the clocksource maintainers.
>
> I see two related topics here.

Agreed

>  First, the Hyper-V clocksource driver is
> drivers/clocksource/hyperv_timer.c.  The code is architecture independent
> and is used today on the x86 side and for ARM64 in this patch series.  A few
> architecture specific calls are satisfied by code under arch/x86, and in this
> patch series, under arch/arm64.  Is there some aspect of this driver that
> needs reconsideration?  I just want to make sure to understand what you
> are getting at.

For the clocksource driver, I would like to see the arm64 specific bits
(the code you add in arch/arm64 that are only relevant to this driver)
moved out of arch/arm64 and into drivers/clocksource, in whatever
form the clocksource maintainers prefer. I would suggest having a
separate file that can get linked along with the architecture-independent
part of that driver.

> Second is the question of where/how to do Hyper-V specific initialization.
> I agree that hanging it off the timer initialization isn't a great approach.
> Should I add a Hyper-V specific initialization call at the appropriate point
> in the ARM64 init sequence?  The x86 side has some structure for handling
> multiple hypervisors, and the Hyper-V initialization code naturally plugs into
> that structure.  I'm certainly open to suggestions on the best way to handle
> it for ARM64.

Yes, that is where I was getting at. Maybe the x86 abstraction for handling
multiple hypervisors can be lifted out of arch/x86/ into common code?

       Arnd
diff mbox series

Patch

diff --git a/arch/arm64/hyperv/hv_core.c b/arch/arm64/hyperv/hv_core.c
index 966d815..831a69c 100644
--- a/arch/arm64/hyperv/hv_core.c
+++ b/arch/arm64/hyperv/hv_core.c
@@ -18,10 +18,41 @@ 
 #include <linux/slab.h>
 #include <linux/hyperv.h>
 #include <linux/arm-smccc.h>
+#include <linux/vmalloc.h>
+#include <linux/acpi.h>
+#include <linux/module.h>
+#include <linux/cpuhotplug.h>
+#include <linux/psci.h>
 #include <asm-generic/bug.h>
 #include <asm/hyperv-tlfs.h>
 #include <asm/mshyperv.h>
+#include <asm/sysreg.h>
+#include <clocksource/hyperv_timer.h>
 
+static bool    hyperv_initialized;
+
+struct         ms_hyperv_info ms_hyperv __ro_after_init;
+EXPORT_SYMBOL_GPL(ms_hyperv);
+
+u32            *hv_vp_index;
+EXPORT_SYMBOL_GPL(hv_vp_index);
+
+u32            hv_max_vp_index;
+EXPORT_SYMBOL_GPL(hv_max_vp_index);
+
+static int hv_cpu_init(unsigned int cpu)
+{
+	u64 msr_vp_index;
+
+	msr_vp_index = hv_get_vpreg(HV_REGISTER_VPINDEX);
+
+	hv_vp_index[smp_processor_id()] = msr_vp_index;
+
+	if (msr_vp_index > hv_max_vp_index)
+		hv_max_vp_index = msr_vp_index;
+
+	return 0;
+}
 
 /*
  * Functions for allocating and freeing memory with size and
@@ -67,6 +98,107 @@  void hv_free_hyperv_page(unsigned long addr)
 
 
 /*
+ * This function is invoked via the ACPI clocksource probe mechanism. We
+ * don't actually use any values from the ACPI GTDT table, but we set up
+ * the Hyper-V synthetic clocksource and do other initialization for
+ * interacting with Hyper-V the first time.  Using early_initcall to invoke
+ * this function is too late because interrupts are already enabled at that
+ * point, and hv_init_clocksource() must run before interrupts are enabled.
+ *
+ * 1. Setup the guest ID.
+ * 2. Get features and hints info from Hyper-V
+ * 3. Setup per-cpu VP indices.
+ * 4. Initialize the Hyper-V clocksource.
+ */
+
+static int __init hyperv_init(struct acpi_table_header *table)
+{
+	struct hv_get_vp_registers_output result;
+	u32	a, b, c, d;
+	u64	guest_id;
+	int	i, cpuhp;
+
+	/*
+	 * If we're in a VM on Hyper-V, the ACPI hypervisor_id field will
+	 * have the string "MsHyperV".
+	 */
+	if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
+		return -EINVAL;
+
+	/* Setup the guest ID */
+	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
+	hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
+
+	/* Get the features and hints from Hyper-V */
+	hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
+	ms_hyperv.features = result.as32.a;
+	ms_hyperv.misc_features = result.as32.c;
+
+	hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
+	ms_hyperv.hints = result.as32.a;
+
+	pr_info("Hyper-V: Features 0x%x, hints 0x%x, misc 0x%x\n",
+		ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features);
+
+	/*
+	 * If Hyper-V has crash notifications, set crash_kexec_post_notifiers
+	 * so that we will report the panic to Hyper-V before running kdump.
+	 */
+	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE)
+		crash_kexec_post_notifiers = true;
+
+	/* Get information about the Hyper-V host version */
+	hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
+	a = result.as32.a;
+	b = result.as32.b;
+	c = result.as32.c;
+	d = result.as32.d;
+	pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
+		b >> 16, b & 0xFFFF, a,	d & 0xFFFFFF, c, d >> 24);
+
+	/* Allocate and initialize percpu VP index array */
+	hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
+				    GFP_KERNEL);
+	if (!hv_vp_index)
+		return -ENOMEM;
+
+	for (i = 0; i < num_possible_cpus(); i++)
+		hv_vp_index[i] = VP_INVAL;
+
+	cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
+			"arm64/hyperv_init:online", hv_cpu_init, NULL);
+	if (cpuhp < 0)
+		goto free_vp_index;
+
+	hv_init_clocksource();
+	if (hv_stimer_alloc())
+		goto remove_cpuhp_state;
+
+	hyperv_initialized = true;
+	return 0;
+
+remove_cpuhp_state:
+	cpuhp_remove_state(cpuhp);
+free_vp_index:
+	kfree(hv_vp_index);
+	hv_vp_index = NULL;
+	return -EINVAL;
+}
+TIMER_ACPI_DECLARE(hyperv, ACPI_SIG_GTDT, hyperv_init);
+
+/*
+ * This routine is called before kexec/kdump, it does the required cleanup.
+ */
+void hyperv_cleanup(void)
+{
+	/* Reset our OS id */
+	hv_set_vpreg(HV_REGISTER_GUEST_OSID, 0);
+
+}
+EXPORT_SYMBOL_GPL(hyperv_cleanup);
+
+
+/*
  * hv_do_hypercall- Invoke the specified hypercall
  */
 u64 hv_do_hypercall(u64 control, void *input, void *output)
@@ -291,3 +423,15 @@  void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
 	       (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
 }
 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
+
+bool hv_is_hyperv_initialized(void)
+{
+	return hyperv_initialized;
+}
+EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
+
+bool hv_is_hibernation_supported(void)
+{
+	return false;
+}
+EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);