[v5,04/34] x86/traps: add external_interrupt() to dispatch external interrupts

Message ID 20230307023946.14516-5-xin3.li@intel.com
State New
Headers
Series x86: enable FRED for x86-64 |

Commit Message

Li, Xin3 March 7, 2023, 2:39 a.m. UTC
  From: "H. Peter Anvin (Intel)" <hpa@zytor.com>

Add external_interrupt() to dispatch external interrupts to their
handlers. If an external interrupt is a system interrupt, dipatch
it through system_interrupt_handler_table, otherwise call into
dispatch_common_interrupt().

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Co-developed-by: Xin Li <xin3.li@intel.com>
Tested-by: Shan Kang <shan.kang@intel.com>
Signed-off-by: Xin Li <xin3.li@intel.com>
---
 arch/x86/kernel/traps.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
  

Comments

Peter Zijlstra March 20, 2023, 3:36 p.m. UTC | #1
On Mon, Mar 06, 2023 at 06:39:16PM -0800, Xin Li wrote:

> +#ifndef CONFIG_X86_LOCAL_APIC
> +/*
> + * Used when local APIC is not compiled into the kernel, but
> + * external_interrupt() needs dispatch_spurious_interrupt().
> + */
> +DEFINE_IDTENTRY_IRQ(spurious_interrupt)
> +{
> +	pr_info("Spurious interrupt (vector 0x%x) on CPU#%d, should never happen.\n",
> +		vector, smp_processor_id());
> +}
> +#endif
> +
> +/*
> + * External interrupt dispatch function.
> + *
> + * Until/unless dispatch_common_interrupt() can be taught to deal with the
> + * special system vectors, split the dispatch.
> + *
> + * Note: dispatch_common_interrupt() already deals with IRQ_MOVE_CLEANUP_VECTOR.
> + */
> +int external_interrupt(struct pt_regs *regs, unsigned int vector)
> +{
> +	unsigned int sysvec = vector - FIRST_SYSTEM_VECTOR;
> +
> +	if (vector < FIRST_EXTERNAL_VECTOR) {
> +		pr_err("invalid external interrupt vector %d\n", vector);
> +		return -EINVAL;
> +	}
> +
> +	if (sysvec < NR_SYSTEM_VECTORS) {
> +		if (system_interrupt_handlers[sysvec])
> +			system_interrupt_handlers[sysvec](regs);
> +		else
> +			dispatch_spurious_interrupt(regs, vector);

ISTR suggesting you can get rid of this branch if you stuff
system_interrupt_handlers[] with dispatch_spurious_interrupt instead of
NULL.

> +	} else {
> +		dispatch_common_interrupt(regs, vector);
> +	}
> +
> +	return 0;
> +}
  
Peter Zijlstra March 20, 2023, 5:42 p.m. UTC | #2
On Mon, Mar 20, 2023 at 04:36:30PM +0100, Peter Zijlstra wrote:
> On Mon, Mar 06, 2023 at 06:39:16PM -0800, Xin Li wrote:
> 
> > +#ifndef CONFIG_X86_LOCAL_APIC
> > +/*
> > + * Used when local APIC is not compiled into the kernel, but
> > + * external_interrupt() needs dispatch_spurious_interrupt().
> > + */
> > +DEFINE_IDTENTRY_IRQ(spurious_interrupt)
> > +{
> > +	pr_info("Spurious interrupt (vector 0x%x) on CPU#%d, should never happen.\n",
> > +		vector, smp_processor_id());
> > +}
> > +#endif
> > +
> > +/*
> > + * External interrupt dispatch function.
> > + *
> > + * Until/unless dispatch_common_interrupt() can be taught to deal with the
> > + * special system vectors, split the dispatch.
> > + *
> > + * Note: dispatch_common_interrupt() already deals with IRQ_MOVE_CLEANUP_VECTOR.
> > + */
> > +int external_interrupt(struct pt_regs *regs, unsigned int vector)
> > +{
> > +	unsigned int sysvec = vector - FIRST_SYSTEM_VECTOR;
> > +
> > +	if (vector < FIRST_EXTERNAL_VECTOR) {
> > +		pr_err("invalid external interrupt vector %d\n", vector);
> > +		return -EINVAL;
> > +	}
> > +
> > +	if (sysvec < NR_SYSTEM_VECTORS) {
> > +		if (system_interrupt_handlers[sysvec])
> > +			system_interrupt_handlers[sysvec](regs);
> > +		else
> > +			dispatch_spurious_interrupt(regs, vector);
> 
> ISTR suggesting you can get rid of this branch if you stuff
> system_interrupt_handlers[] with dispatch_spurious_interrupt instead of
> NULL.

Ah, I suggested that for another function vector, but it applies here
too I suppose :-)
  
Li, Xin3 March 20, 2023, 5:53 p.m. UTC | #3
> > +	if (sysvec < NR_SYSTEM_VECTORS) {
> > +		if (system_interrupt_handlers[sysvec])
> > +			system_interrupt_handlers[sysvec](regs);
> > +		else
> > +			dispatch_spurious_interrupt(regs, vector);
> 
> ISTR suggesting you can get rid of this branch if you stuff
> system_interrupt_handlers[] with dispatch_spurious_interrupt instead of NULL.

You're right, however I only fixed one.
  
Li, Xin3 March 20, 2023, 11:47 p.m. UTC | #4
> > > +	if (sysvec < NR_SYSTEM_VECTORS) {
> > > +		if (system_interrupt_handlers[sysvec])
> > > +			system_interrupt_handlers[sysvec](regs);
> > > +		else
> > > +			dispatch_spurious_interrupt(regs, vector);
> >
> > ISTR suggesting you can get rid of this branch if you stuff
> > system_interrupt_handlers[] with dispatch_spurious_interrupt instead
> > of NULL.
> 
> Ah, I suggested that for another function vector, but it applies here too I suppose :-)

Of course!

We just need to use a wrapper as dispatch_spurious_interrupt() takes an extra
parameter "vector".

Thanks!
  Xin
  

Patch

diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index c0f7666140da..31ad645be2fb 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -1499,6 +1499,47 @@  void __init install_system_interrupt_handler(unsigned int n, const void *asm_add
 	alloc_intr_gate(n, asm_addr);
 }
 
+#ifndef CONFIG_X86_LOCAL_APIC
+/*
+ * Used when local APIC is not compiled into the kernel, but
+ * external_interrupt() needs dispatch_spurious_interrupt().
+ */
+DEFINE_IDTENTRY_IRQ(spurious_interrupt)
+{
+	pr_info("Spurious interrupt (vector 0x%x) on CPU#%d, should never happen.\n",
+		vector, smp_processor_id());
+}
+#endif
+
+/*
+ * External interrupt dispatch function.
+ *
+ * Until/unless dispatch_common_interrupt() can be taught to deal with the
+ * special system vectors, split the dispatch.
+ *
+ * Note: dispatch_common_interrupt() already deals with IRQ_MOVE_CLEANUP_VECTOR.
+ */
+int external_interrupt(struct pt_regs *regs, unsigned int vector)
+{
+	unsigned int sysvec = vector - FIRST_SYSTEM_VECTOR;
+
+	if (vector < FIRST_EXTERNAL_VECTOR) {
+		pr_err("invalid external interrupt vector %d\n", vector);
+		return -EINVAL;
+	}
+
+	if (sysvec < NR_SYSTEM_VECTORS) {
+		if (system_interrupt_handlers[sysvec])
+			system_interrupt_handlers[sysvec](regs);
+		else
+			dispatch_spurious_interrupt(regs, vector);
+	} else {
+		dispatch_common_interrupt(regs, vector);
+	}
+
+	return 0;
+}
+
 void __init trap_init(void)
 {
 	/* Init cpu_entry_area before IST entries are set up */