[v4,6/6] x86/hyperv: Fix serial console interrupts for TDX guests

Message ID 20230408204759.14902-7-decui@microsoft.com
State New
Headers
Series Support TDX guests on Hyper-V |

Commit Message

Dexuan Cui April 8, 2023, 8:47 p.m. UTC
  When a TDX guest runs on Hyper-V, the UEFI firmware sets the HW_REDUCED
flag, and consequently ttyS0 interrupts can't work. Fix the issue by
overriding x86_init.acpi.reduced_hw_early_init().

Signed-off-by: Dexuan Cui <decui@microsoft.com>
---

Changes since v1:
    None.

 arch/x86/kernel/cpu/mshyperv.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
  

Comments

Michael Kelley (LINUX) April 11, 2023, 5:13 p.m. UTC | #1
From: Dexuan Cui <decui@microsoft.com> Sent: Saturday, April 8, 2023 1:48 PM
> 
> When a TDX guest runs on Hyper-V, the UEFI firmware sets the HW_REDUCED
> flag, and consequently ttyS0 interrupts can't work. Fix the issue by
> overriding x86_init.acpi.reduced_hw_early_init().
> 
> Signed-off-by: Dexuan Cui <decui@microsoft.com>
> ---
> 
> Changes since v1:
>     None.
> 
>  arch/x86/kernel/cpu/mshyperv.c | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
> index e9106c9d92f81..deedced0f2bb0 100644
> --- a/arch/x86/kernel/cpu/mshyperv.c
> +++ b/arch/x86/kernel/cpu/mshyperv.c
> @@ -318,6 +318,26 @@ static void __init hv_smp_prepare_cpus(unsigned int max_cpus)
>  }
>  #endif
> 
> +/*
> + * When a TDX guest runs on Hyper-V, the firmware sets the HW_REDUCED flag: see
> + * acpi_tb_create_local_fadt(). Consequently ttyS0 interrupts can't work because
> + * request_irq() -> ... -> irq_to_desc() returns NULL for ttyS0. This happens
> + * because mp_config_acpi_legacy_irqs() sees a nr_legacy_irqs() of 0, so it
> + * doesn't initialize the array 'mp_irqs[]', and later setup_IO_APIC_irqs() ->
> + * find_irq_entry() fails to find the legacy irqs from the array, and hence
> + * doesn't create the necessary irq description info.
> + *
> + * Copy arch/x86/kernel/acpi/boot.c: acpi_generic_reduced_hw_init() but doesn't
> + * change 'legacy_pic', so it keeps its default value 'default_legacy_pic' in
> + * mp_config_acpi_legacy_irqs(), which sees a non-zero nr_legacy_irqs(), and
> + * eventually serial console interrupts can work properly.

I had a little trouble parsing this comment.  Slightly better wording is:

    * Clone arch/x86/kernel/acpi/boot.c: acpi_generic_reduced_hw_init() here,
    * except don't change 'legacy_pic'.  It keeps its default value 'default_legacy_pic'.
    * mp_config_acpi_legacy_irqs() sees a non-zero nr_legacy_irqs(), and
    * eventually serial console interrupts can work properly.

Otherwise,

Reviewed-by: Michael Kelley <mikelley@microsoft.com>

> + */
> +static void __init reduced_hw_init(void)
> +{
> +	x86_init.timers.timer_init	= x86_init_noop;
> +	x86_init.irqs.pre_vector_init	= x86_init_noop;
> +}
> +
>  static void __init ms_hyperv_init_platform(void)
>  {
>  	int hv_max_functions_eax;
> @@ -425,6 +445,8 @@ static void __init ms_hyperv_init_platform(void)
> 
>  			/* A TDX VM must use x2APIC and doesn't use lazy EOI */
>  			ms_hyperv.hints &= ~HV_X64_APIC_ACCESS_RECOMMENDED;
> +
> +			x86_init.acpi.reduced_hw_early_init = reduced_hw_init;
>  		}
>  	}
> 
> --
> 2.25.1
  
Dexuan Cui April 21, 2023, 3:54 a.m. UTC | #2
> From: Michael Kelley (LINUX) <mikelley@microsoft.com>
> Sent: Tuesday, April 11, 2023 10:14 AM
> >  ...
> > + * Copy arch/x86/kernel/acpi/boot.c: acpi_generic_reduced_hw_init() but
> > doesn't change 'legacy_pic', so it keeps its default value 'default_legacy_pic' in
> > + * mp_config_acpi_legacy_irqs(), which sees a non-zero nr_legacy_irqs(),
> > and eventually serial console interrupts can work properly.
> 
> I had a little trouble parsing this comment.  Slightly better wording is:
> 
>  * Clone arch/x86/kernel/acpi/boot.c: acpi_generic_reduced_hw_init()
> here,
>     * except don't change 'legacy_pic'.  It keeps its default value
> 'default_legacy_pic'.
>     * mp_config_acpi_legacy_irqs() sees a non-zero nr_legacy_irqs(), and
>     * eventually serial console interrupts can work properly.
> 
> Otherwise,
> 
> Reviewed-by: Michael Kelley <mikelley@microsoft.com>

Thanks! Will use this in v5.
  

Patch

diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
index e9106c9d92f81..deedced0f2bb0 100644
--- a/arch/x86/kernel/cpu/mshyperv.c
+++ b/arch/x86/kernel/cpu/mshyperv.c
@@ -318,6 +318,26 @@  static void __init hv_smp_prepare_cpus(unsigned int max_cpus)
 }
 #endif
 
+/*
+ * When a TDX guest runs on Hyper-V, the firmware sets the HW_REDUCED flag: see
+ * acpi_tb_create_local_fadt(). Consequently ttyS0 interrupts can't work because
+ * request_irq() -> ... -> irq_to_desc() returns NULL for ttyS0. This happens
+ * because mp_config_acpi_legacy_irqs() sees a nr_legacy_irqs() of 0, so it
+ * doesn't initialize the array 'mp_irqs[]', and later setup_IO_APIC_irqs() ->
+ * find_irq_entry() fails to find the legacy irqs from the array, and hence
+ * doesn't create the necessary irq description info.
+ *
+ * Copy arch/x86/kernel/acpi/boot.c: acpi_generic_reduced_hw_init() but doesn't
+ * change 'legacy_pic', so it keeps its default value 'default_legacy_pic' in
+ * mp_config_acpi_legacy_irqs(), which sees a non-zero nr_legacy_irqs(), and
+ * eventually serial console interrupts can work properly.
+ */
+static void __init reduced_hw_init(void)
+{
+	x86_init.timers.timer_init	= x86_init_noop;
+	x86_init.irqs.pre_vector_init	= x86_init_noop;
+}
+
 static void __init ms_hyperv_init_platform(void)
 {
 	int hv_max_functions_eax;
@@ -425,6 +445,8 @@  static void __init ms_hyperv_init_platform(void)
 
 			/* A TDX VM must use x2APIC and doesn't use lazy EOI */
 			ms_hyperv.hints &= ~HV_X64_APIC_ACCESS_RECOMMENDED;
+
+			x86_init.acpi.reduced_hw_early_init = reduced_hw_init;
 		}
 	}