Skip to contentSkip to conversation

lib: sbi: Use AIA CSRs for local interrupts when available

PATCH Superseded Anup Patel 5 participants 24 messages Jan 4, 2022 – Feb 9, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 3/13 lib: sbi: Use AIA CSRs for local interrupts when available

We should use AIA CSRs to process local interrupts whenever AIA is available.

---
 lib/sbi/sbi_trap.c | 57 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 46 insertions(+), 11 deletions(-)
lib/sbi/sbi_trap.c +46 -11
index 8d20e04..bc8961f 100644
@@ -195,6 +195,44 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 	return 0;
 }
 
+static int sbi_trap_nonaia_irq(struct sbi_trap_regs *regs, ulong mcause)
+{
+	mcause &= ~(1UL << (__riscv_xlen - 1));
+	switch (mcause) {
+	case IRQ_M_TIMER:
+		sbi_timer_process();
+		break;
+	case IRQ_M_SOFT:
+		sbi_ipi_process();
+		break;
+	default:
+		return SBI_ENOENT;
+	};
+
+	return 0;
+}
+
+static int sbi_trap_aia_irq(struct sbi_trap_regs *regs, ulong mcause)
+{
+	unsigned long mtopi;
+
+	while ((mtopi = csr_read(CSR_MTOPI))) {
+		mtopi = mtopi >> TOPI_IID_SHIFT;
+		switch (mtopi) {
+		case IRQ_M_TIMER:
+			sbi_timer_process();
+			break;
+		case IRQ_M_SOFT:
+			sbi_ipi_process();
+			break;
+		default:
+			return SBI_ENOENT;
+		}
+	}
+
+	return 0;
+}
+
 /**
  * Handle trap/interrupt
  *
@@ -225,18 +263,15 @@ struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs)
 	}
 
 	if (mcause & (1UL << (__riscv_xlen - 1))) {
-		mcause &= ~(1UL << (__riscv_xlen - 1));
-		switch (mcause) {
-		case IRQ_M_TIMER:
-			sbi_timer_process();
-			break;
-		case IRQ_M_SOFT:
-			sbi_ipi_process();
-			break;
-		default:
-			msg = "unhandled external interrupt";
+		if (sbi_hart_has_feature(sbi_scratch_thishart_ptr(),
+					 SBI_HART_HAS_AIA))
+			rc = sbi_trap_aia_irq(regs, mcause);
+		else
+			rc = sbi_trap_nonaia_irq(regs, mcause);
+		if (rc) {
+			msg = "unhandled local interrupt";
 			goto trap_error;
-		};
+		}
 		return regs;
 	}
 
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 4/13 lib: sbi: Add sbi_trap_set_external_irqfn() API

This patch adds sbi_trap_set_external_irqfn() API which can be used by OpenSBI platform code to set a callback function for external interrupts. The RISC-V AIA IMSIC driver will use this API to implement inter-processor interrupts on-top-of MSIs.

---
 include/sbi/sbi_trap.h |  2 ++
 lib/sbi/sbi_trap.c     | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/sbi/sbi_trap.h +2 -0
index d205056..4f611fa 100644
@@ -205,6 +205,8 @@ struct sbi_trap_info {
 int sbi_trap_redirect(struct sbi_trap_regs *regs,
 		      struct sbi_trap_info *trap);
 
+void sbi_trap_set_external_irqfn(int (*fn)(struct sbi_trap_regs *regs));
+
 struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs);
 
 void __noreturn sbi_trap_exit(const struct sbi_trap_regs *regs);
lib/sbi/sbi_trap.c +29 -0
index bc8961f..faab90a 100644
@@ -195,6 +195,27 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 	return 0;
 }
 
+static int default_irqfn(struct sbi_trap_regs *regs)
+{
+	return SBI_ENODEV;
+}
+
+static int (*ext_irqfn)(struct sbi_trap_regs *regs) = default_irqfn;
+
+/**
+ * Set external irq handler function
+ *
+ * This function is called by OpenSBI platform code to set a handler for
+ * external interrupts
+ *
+ * @param fn function pointer for handling external irqs
+ */
+void sbi_trap_set_external_irqfn(int (*fn)(struct sbi_trap_regs *regs))
+{
+	if (fn)
+		ext_irqfn = fn;
+}
+
 static int sbi_trap_nonaia_irq(struct sbi_trap_regs *regs, ulong mcause)
 {
 	mcause &= ~(1UL << (__riscv_xlen - 1));
@@ -205,6 +226,8 @@ static int sbi_trap_nonaia_irq(struct sbi_trap_regs *regs, ulong mcause)
 	case IRQ_M_SOFT:
 		sbi_ipi_process();
 		break;
+	case IRQ_M_EXT:
+		return ext_irqfn(regs);
 	default:
 		return SBI_ENOENT;
 	};
@@ -214,6 +237,7 @@ static int sbi_trap_nonaia_irq(struct sbi_trap_regs *regs, ulong mcause)
 
 static int sbi_trap_aia_irq(struct sbi_trap_regs *regs, ulong mcause)
 {
+	int rc;
 	unsigned long mtopi;
 
 	while ((mtopi = csr_read(CSR_MTOPI))) {
@@ -225,6 +249,11 @@ static int sbi_trap_aia_irq(struct sbi_trap_regs *regs, ulong mcause)
 		case IRQ_M_SOFT:
 			sbi_ipi_process();
 			break;
+		case IRQ_M_EXT:
+			rc = ext_irqfn(regs);
+			if (rc)
+				return rc;
+			break;
 		default:
 			return SBI_ENOENT;
 		}
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH 5/13 lib: utils/irqchip: Allow multiple FDT irqchip drivers

In reply to Anup Patel

We can have multiple FDT irqchip drivers to be probed when a RISC-V system has different types of interrupt controller in a hierarchy.

This will be certainely the case when a RISC-V system has both RISC-V AIA IMSIC and RISC-V AIA APLIC implemented.

We extend simple FDT irqchip framework to allow multiple FDT irqchip drivers to be used for same RISC-V platform.

---
 lib/utils/irqchip/fdt_irqchip.c | 37 ++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)
lib/utils/irqchip/fdt_irqchip.c +30 -7
index 8dda661..bf6969a 100644
@@ -18,23 +18,40 @@ static struct fdt_irqchip *irqchip_drivers[] = {
 	&fdt_irqchip_plic
 };
 
-static struct fdt_irqchip *current_driver = NULL;
+#define FDT_IRQCHIP_MAX_DRIVERS	8
+
+static struct fdt_irqchip *current_drivers[FDT_IRQCHIP_MAX_DRIVERS] = {0};
+static int current_drivers_count;
 
 void fdt_irqchip_exit(void)
 {
-	if (current_driver && current_driver->exit)
-		current_driver->exit();
+	int i;
+
+	for (i = 0; i < current_drivers_count; i++) {
+		if (!current_drivers[i] || !current_drivers[i]->exit)
+			continue;
+		current_drivers[i]->exit();
+	}
 }
 
 static int fdt_irqchip_warm_init(void)
 {
-	if (current_driver && current_driver->warm_init)
-		return current_driver->warm_init();
+	int i, rc;
+
+	for (i = 0; i < current_drivers_count; i++) {
+		if (!current_drivers[i] || !current_drivers[i]->warm_init)
+			continue;
+		rc = current_drivers[i]->warm_init();
+		if (rc)
+			return rc;
+	}
+
 	return 0;
 }
 
 static int fdt_irqchip_cold_init(void)
 {
+	bool drv_added;
 	int pos, noff, rc;
 	struct fdt_irqchip *drv;
 	const struct fdt_match *match;
@@ -44,6 +61,7 @@ static int fdt_irqchip_cold_init(void)
 		drv = irqchip_drivers[pos];
 
 		noff = -1;
+		drv_added = false;
 		while ((noff = fdt_find_match(fdt, noff,
 					drv->match_table, &match)) >= 0) {
 			if (drv->cold_init) {
@@ -53,10 +71,15 @@ static int fdt_irqchip_cold_init(void)
 				if (rc)
 					return rc;
 			}
-			current_driver = drv;
+
+			if (drv_added)
+				continue;
+
+			current_drivers[current_drivers_count++] = drv;
+			drv_added = true;
 		}
 
-		if (current_driver)
+		if (FDT_IRQCHIP_MAX_DRIVERS <= current_drivers_count)
 			break;
 	}
 
Signature · 1 line
2.25.1
#3 lore raw

PATCH 5/13 lib: utils/irqchip: Allow multiple FDT irqchip drivers

Quoted 5 lines · Anup Patel

We can have multiple FDT irqchip drivers to be probed when a RISC-V system has different types of interrupt controller in a hierarchy.

This will be certainely the case when a RISC-V system has both

/s/certainely/certainly

Quoted 88 lines

RISC-V AIA IMSIC and RISC-V AIA APLIC implemented.

We extend simple FDT irqchip framework to allow multiple FDT irqchip drivers to be used for same RISC-V platform.

---
 lib/utils/irqchip/fdt_irqchip.c | 37 ++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)
lib/utils/irqchip/fdt_irqchip.c +30 -8
index 8dda661..bf6969a 100644
@@ -18,23 +18,40 @@ static struct fdt_irqchip *irqchip_drivers[] = {
        &fdt_irqchip_plic
 };
 
-static struct fdt_irqchip *current_driver = NULL;
+#define FDT_IRQCHIP_MAX_DRIVERS        8
+
+static struct fdt_irqchip *current_drivers[FDT_IRQCHIP_MAX_DRIVERS] = {0};
+static int current_drivers_count;
 
 void fdt_irqchip_exit(void)
 {
-       if (current_driver && current_driver->exit)
-               current_driver->exit();
+       int i;
+
+       for (i = 0; i < current_drivers_count; i++) {
+               if (!current_drivers[i] || !current_drivers[i]->exit)
+                       continue;
+               current_drivers[i]->exit();
+       }
 }
 
 static int fdt_irqchip_warm_init(void)
 {
-       if (current_driver && current_driver->warm_init)
-               return current_driver->warm_init();
+       int i, rc;
+
+       for (i = 0; i < current_drivers_count; i++) {
+               if (!current_drivers[i] || !current_drivers[i]->warm_init)
+                       continue;
+               rc = current_drivers[i]->warm_init();
+               if (rc)
+                       return rc;
+       }
+
        return 0;
 }
 
 static int fdt_irqchip_cold_init(void)
 {
+       bool drv_added;
        int pos, noff, rc;
        struct fdt_irqchip *drv;
        const struct fdt_match *match;
@@ -44,6 +61,7 @@ static int fdt_irqchip_cold_init(void)
                drv = irqchip_drivers[pos];
 
                noff = -1;
+               drv_added = false;
                while ((noff = fdt_find_match(fdt, noff,
                                        drv->match_table, &match)) >= 0) {
                        if (drv->cold_init) {
@@ -53,10 +71,15 @@ static int fdt_irqchip_cold_init(void)
                                if (rc)
                                        return rc;
                        }
-                       current_driver = drv;
+
+                       if (drv_added)
+                               continue;
+
+                       current_drivers[current_drivers_count++] = drv;
+                       drv_added = true;
                }
 
-               if (current_driver)
+               if (FDT_IRQCHIP_MAX_DRIVERS <= current_drivers_count)
                        break;
        }
 
--
Quoted 2 lines

2.25.1

Otherwise,

Signature · 2 lines
Regards,
Atish
author
#4 lore raw

PATCH 6/13 include: sbi: Introduce nascent_init() platform callback

In reply to Anup Patel

We introduce nascent_init() platform callback which will allow platforms to do very early initialization of platform specific per-HART CSRs and per-HART devices.

---
 include/sbi/sbi_platform.h | 17 +++++++++++++++++
 lib/sbi/sbi_init.c         |  8 ++++++++
 2 files changed, 25 insertions(+)
include/sbi/sbi_platform.h +17 -0
index 4d192f2..e097785 100644
@@ -64,6 +64,9 @@ enum sbi_platform_features {
 
 /** Platform functions */
 struct sbi_platform_operations {
+	/* Platform nascent initialization */
+	int (*nascent_init)(void);
+
 	/** Platform early initialization */
 	int (*early_init)(bool cold_boot);
 	/** Platform final initialization */
@@ -299,6 +302,20 @@ static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat,
 	return FALSE;
 }
 
+/**
+ * Nascent (very early) initialization for current HART
+ *
+ * @param plat pointer to struct sbi_platform
+ *
+ * @return 0 on success and negative error code on failure
+ */
+static inline int sbi_platform_nascent_init(const struct sbi_platform *plat)
+{
+	if (plat && sbi_platform_ops(plat)->nascent_init)
+		return sbi_platform_ops(plat)->nascent_init();
+	return 0;
+}
+
 /**
  * Early initialization for current HART
  *
lib/sbi/sbi_init.c +8 -0
index 83043c5..27d03a7 100644
@@ -494,6 +494,14 @@ void __noreturn sbi_init(struct sbi_scratch *scratch)
 	if (next_mode_supported && atomic_xchg(&coldboot_lottery, 1) == 0)
 		coldboot = TRUE;
 
+	/*
+	 * Do platform specific nascent (very early) initialization so
+	 * that platform can initialize platform specific per-HART CSRs
+	 * or per-HART devices.
+	 */
+	if (sbi_platform_nascent_init(plat))
+		sbi_hart_hang();
+
 	if (coldboot)
 		init_coldboot(scratch, hartid);
 	else
Signature · 1 line
2.25.1
#5 lore raw

PATCH 6/13 include: sbi: Introduce nascent_init() platform callback

Quoted 27 lines · Anup Patel

We introduce nascent_init() platform callback which will allow platforms to do very early initialization of platform specific per-HART CSRs and per-HART devices.

---
 include/sbi/sbi_platform.h | 17 +++++++++++++++++
 lib/sbi/sbi_init.c         |  8 ++++++++
 2 files changed, 25 insertions(+)
include/sbi/sbi_platform.h +3 -0
index 4d192f2..e097785 100644
@@ -64,6 +64,9 @@ enum sbi_platform_features {
 
 /** Platform functions */
 struct sbi_platform_operations {
+       /* Platform nascent initialization */
+       int (*nascent_init)(void);
+
        /** Platform early initialization */
        int (*early_init)(bool cold_boot);
        /** Platform final initialization */

include/sbi/sbi_platform.h static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat, · line 307

Quoted 6 lines
include/sbi/sbi_platform.h +3 -0
@@ -299,6 +302,20 @@ static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat,
        return FALSE;
 }
 
+/**
+ * Nascent (very early) initialization for current HART
+ *

We probably need a better comment to differentiate between Nascent & early init. Without that, platform vendors may just use this instead of early init.

Quoted 34 lines
include/sbi/sbi_platform.h +11 -0
+ * @param plat pointer to struct sbi_platform
+ *
+ * @return 0 on success and negative error code on failure
+ */
+static inline int sbi_platform_nascent_init(const struct sbi_platform *plat)
+{
+       if (plat && sbi_platform_ops(plat)->nascent_init)
+               return sbi_platform_ops(plat)->nascent_init();
+       return 0;
+}
+
 /**
  * Early initialization for current HART
  *
lib/sbi/sbi_init.c +8 -1
index 83043c5..27d03a7 100644
@@ -494,6 +494,14 @@ void __noreturn sbi_init(struct sbi_scratch *scratch)
        if (next_mode_supported && atomic_xchg(&coldboot_lottery, 1) == 0)
                coldboot = TRUE;
 
+       /*
+        * Do platform specific nascent (very early) initialization so
+        * that platform can initialize platform specific per-HART CSRs
+        * or per-HART devices.
+        */
+       if (sbi_platform_nascent_init(plat))
+               sbi_hart_hang();
+
        if (coldboot)
                init_coldboot(scratch, hartid);
        else
--
Quoted 2 lines

2.25.1

Other than that, LGTM.

Signature · 2 lines
Regards,
Atish
#6 lore raw

PATCH 6/13 include: sbi: Introduce nascent_init() platform callback

Quoted 32 lines · Atish Patra
Quoted 30 lines · Anup Patel

We introduce nascent_init() platform callback which will allow platforms to do very early initialization of platform specific per-HART CSRs and per-HART devices.

---
 include/sbi/sbi_platform.h | 17 +++++++++++++++++
 lib/sbi/sbi_init.c         |  8 ++++++++
 2 files changed, 25 insertions(+)
include/sbi/sbi_platform.h +3 -0
index 4d192f2..e097785 100644
@@ -64,6 +64,9 @@ enum sbi_platform_features {
 
 /** Platform functions */
 struct sbi_platform_operations {
+       /* Platform nascent initialization */
+       int (*nascent_init)(void);
+
        /** Platform early initialization */
        int (*early_init)(bool cold_boot);
        /** Platform final initialization */
@@ -299,6 +302,20 @@ static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat,
        return FALSE;
 }
 
Quoted 6 lines

include/sbi/sbi_platform.h static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat, · line 307

Quoted 3 lines
include/sbi/sbi_platform.h +3 -0
+/**
+ * Nascent (very early) initialization for current HART
+ *

We probably need a better comment to differentiate between Nascent & early init. Without that, platform vendors may just use this instead of early init.

Okay, I will add more details in the comment here.

Regards,
Anup

Quoted 45 lines
Quoted 35 lines
include/sbi/sbi_platform.h +11 -0
+ * @param plat pointer to struct sbi_platform
+ *
+ * @return 0 on success and negative error code on failure
+ */
+static inline int sbi_platform_nascent_init(const struct sbi_platform *plat)
+{
+       if (plat && sbi_platform_ops(plat)->nascent_init)
+               return sbi_platform_ops(plat)->nascent_init();
+       return 0;
+}
+
 /**
  * Early initialization for current HART
  *
lib/sbi/sbi_init.c +8 -1
index 83043c5..27d03a7 100644
@@ -494,6 +494,14 @@ void __noreturn sbi_init(struct sbi_scratch *scratch)
        if (next_mode_supported && atomic_xchg(&coldboot_lottery, 1) == 0)
                coldboot = TRUE;
 
+       /*
+        * Do platform specific nascent (very early) initialization so
+        * that platform can initialize platform specific per-HART CSRs
+        * or per-HART devices.
+        */
+       if (sbi_platform_nascent_init(plat))
+               sbi_hart_hang();
+
        if (coldboot)
                init_coldboot(scratch, hartid);
        else
--
Quoted 2 lines

2.25.1

Other than that, LGTM.

--
Regards,
Atish

author
#7 lore raw

PATCH 7/13 lib: sbi: Enable mie.MEIE bit for IPIs based on external interrupts.

In reply to Anup Patel

We can have IPIs based on external interrupts provided by devices such as AIA IMSIC so we should enable mie.MEIE bit at appropriate places in generic library.

---
 lib/sbi/sbi_hsm.c  | 4 ++--
 lib/sbi/sbi_init.c | 9 ++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
lib/sbi/sbi_hsm.c +2 -2
index ecd2e45..c4d2c6d 100644
@@ -113,8 +113,8 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)
 	/* Save MIE CSR */
 	saved_mie = csr_read(CSR_MIE);
 
-	/* Set MSIE bit to receive IPI */
-	csr_set(CSR_MIE, MIP_MSIP);
+	/* Set MSIE and MEIE bits to receive IPI */
+	csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP);
 
 	/* Wait for hart_add call*/
 	while (atomic_read(&hdata->state) != SBI_HSM_STATE_START_PENDING) {
lib/sbi/sbi_init.c +6 -3
index 27d03a7..6876eb2 100644
@@ -165,8 +165,8 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
 	/* Save MIE CSR */
 	saved_mie = csr_read(CSR_MIE);
 
-	/* Set MSIE bit to receive IPI */
-	csr_set(CSR_MIE, MIP_MSIP);
+	/* Set MSIE and MEIE bits to receive IPI */
+	csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP);
 
 	/* Acquire coldboot lock */
 	spin_lock(&coldboot_lock);
@@ -182,7 +182,7 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
 		do {
 			wfi();
 			cmip = csr_read(CSR_MIP);
-		 } while (!(cmip & MIP_MSIP));
+		 } while (!(cmip & (MIP_MSIP | MIP_MEIP)));
 	};
 
 	/* Acquire coldboot lock */
@@ -276,6 +276,7 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 			   __func__, rc);
 		sbi_hart_hang();
 	}
+	csr_set(CSR_MIE, MIP_MEIP);
 
 	rc = sbi_ipi_init(scratch, TRUE);
 	if (rc) {
@@ -376,6 +377,7 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid)
 	rc = sbi_platform_irqchip_init(plat, FALSE);
 	if (rc)
 		sbi_hart_hang();
+	csr_set(CSR_MIE, MIP_MEIP);
 
 	rc = sbi_ipi_init(scratch, FALSE);
 	if (rc)
@@ -550,6 +552,7 @@ void __noreturn sbi_exit(struct sbi_scratch *scratch)
 
 	sbi_ipi_exit(scratch);
 
+	csr_clear(CSR_MIE, MIP_MEIP);
 	sbi_platform_irqchip_exit(plat);
 
 	sbi_platform_final_exit(plat);
Signature · 1 line
2.25.1
#8 lore raw

PATCH 7/13 lib: sbi: Enable mie.MEIE bit for IPIs based on external interrupts.

Quoted 52 lines · Anup Patel

We can have IPIs based on external interrupts provided by devices such as AIA IMSIC so we should enable mie.MEIE bit at appropriate places in generic library.

---
 lib/sbi/sbi_hsm.c  | 4 ++--
 lib/sbi/sbi_init.c | 9 ++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
lib/sbi/sbi_hsm.c +2 -2
index ecd2e45..c4d2c6d 100644
@@ -113,8 +113,8 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)
        /* Save MIE CSR */
        saved_mie = csr_read(CSR_MIE);
 
-       /* Set MSIE bit to receive IPI */
-       csr_set(CSR_MIE, MIP_MSIP);
+       /* Set MSIE and MEIE bits to receive IPI */
+       csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP);
 
        /* Wait for hart_add call*/
        while (atomic_read(&hdata->state) != SBI_HSM_STATE_START_PENDING) {
lib/sbi/sbi_init.c +3 -3
index 27d03a7..6876eb2 100644
@@ -165,8 +165,8 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
        /* Save MIE CSR */
        saved_mie = csr_read(CSR_MIE);
 
-       /* Set MSIE bit to receive IPI */
-       csr_set(CSR_MIE, MIP_MSIP);
+       /* Set MSIE and MEIE bits to receive IPI */
+       csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP);
 
        /* Acquire coldboot lock */
        spin_lock(&coldboot_lock);
@@ -182,7 +182,7 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
                do {
                        wfi();
                        cmip = csr_read(CSR_MIP);
-                } while (!(cmip & MIP_MSIP));
+                } while (!(cmip & (MIP_MSIP | MIP_MEIP)));
        };
 
        /* Acquire coldboot lock */

lib/sbi/sbi_init.c static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) · line 280

Quoted 5 lines
lib/sbi/sbi_init.c +1 -0
@@ -276,6 +276,7 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                           __func__, rc);
                sbi_hart_hang();
        }
+       csr_set(CSR_MIE, MIP_MEIP);
 

This is only required for IPIs with IMSIC devices. Why not do it inside sbi_ipi_init along with MIP_MSIP set ?

Quoted 21 lines
lib/sbi/sbi_init.c +2 -1
        rc = sbi_ipi_init(scratch, TRUE);
        if (rc) {
@@ -376,6 +377,7 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid)
        rc = sbi_platform_irqchip_init(plat, FALSE);
        if (rc)
                sbi_hart_hang();
+       csr_set(CSR_MIE, MIP_MEIP);
 
        rc = sbi_ipi_init(scratch, FALSE);
        if (rc)
@@ -550,6 +552,7 @@ void __noreturn sbi_exit(struct sbi_scratch *scratch)
 
        sbi_ipi_exit(scratch);
 
+       csr_clear(CSR_MIE, MIP_MEIP);
        sbi_platform_irqchip_exit(plat);
 
        sbi_platform_final_exit(plat);
--

2.25.1

Signature · 2 lines
Regards,
Atish
#9 lore raw

PATCH 7/13 lib: sbi: Enable mie.MEIE bit for IPIs based on external interrupts.

Quoted 52 lines · Atish Patra
Quoted 50 lines · Anup Patel

We can have IPIs based on external interrupts provided by devices such as AIA IMSIC so we should enable mie.MEIE bit at appropriate places in generic library.

---
 lib/sbi/sbi_hsm.c  | 4 ++--
 lib/sbi/sbi_init.c | 9 ++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
lib/sbi/sbi_hsm.c +2 -2
index ecd2e45..c4d2c6d 100644
@@ -113,8 +113,8 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)
        /* Save MIE CSR */
        saved_mie = csr_read(CSR_MIE);
 
-       /* Set MSIE bit to receive IPI */
-       csr_set(CSR_MIE, MIP_MSIP);
+       /* Set MSIE and MEIE bits to receive IPI */
+       csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP);
 
        /* Wait for hart_add call*/
        while (atomic_read(&hdata->state) != SBI_HSM_STATE_START_PENDING) {
lib/sbi/sbi_init.c +3 -3
index 27d03a7..6876eb2 100644
@@ -165,8 +165,8 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
        /* Save MIE CSR */
        saved_mie = csr_read(CSR_MIE);
 
-       /* Set MSIE bit to receive IPI */
-       csr_set(CSR_MIE, MIP_MSIP);
+       /* Set MSIE and MEIE bits to receive IPI */
+       csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP);
 
        /* Acquire coldboot lock */
        spin_lock(&coldboot_lock);
@@ -182,7 +182,7 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
                do {
                        wfi();
                        cmip = csr_read(CSR_MIP);
-                } while (!(cmip & MIP_MSIP));
+                } while (!(cmip & (MIP_MSIP | MIP_MEIP)));
        };
 
Quoted 10 lines

lib/sbi/sbi_init.c static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) · line 280

Quoted 7 lines
lib/sbi/sbi_init.c +1 -0
        /* Acquire coldboot lock */
@@ -276,6 +276,7 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                           __func__, rc);
                sbi_hart_hang();
        }
+       csr_set(CSR_MIE, MIP_MEIP);
 

This is only required for IPIs with IMSIC devices. Why not do it inside sbi_ipi_init along with MIP_MSIP set ?

Setting mie.MEIP does not fit in sbi_ipi_init() because in-future we might have a simple external interrupt handling framework in lib/sbi itself which can call sbi_platform_irqchip_init() and also set mie.MSIP bit.

For now, let mie.MSIP bit setting be in lib/sbi/sbi_init.c. This will keep reminding us to move it in proper place.

Regards,
Anup

Quoted 27 lines
Quoted 20 lines
lib/sbi/sbi_init.c +2 -1
        rc = sbi_ipi_init(scratch, TRUE);
        if (rc) {
@@ -376,6 +377,7 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid)
        rc = sbi_platform_irqchip_init(plat, FALSE);
        if (rc)
                sbi_hart_hang();
+       csr_set(CSR_MIE, MIP_MEIP);
 
        rc = sbi_ipi_init(scratch, FALSE);
        if (rc)
@@ -550,6 +552,7 @@ void __noreturn sbi_exit(struct sbi_scratch *scratch)
 
        sbi_ipi_exit(scratch);
 
+       csr_clear(CSR_MIE, MIP_MEIP);
        sbi_platform_irqchip_exit(plat);
 
        sbi_platform_final_exit(plat);
--
Quoted 2 lines

2.25.1

--
Regards,
Atish

author
#10 lore raw

PATCH 8/13 lib: utils/irqchip: Add IMSIC library

In reply to Anup Patel

We add simple IMSIC library which is independent of hardware description format (FDT or ACPI). This IMSIC library can be used by custom OpenSBI platform support to setup IMSIC for external interrupts.

---
 include/sbi_utils/irqchip/imsic.h |  50 ++++++
 lib/utils/irqchip/imsic.c         | 287 ++++++++++++++++++++++++++++++
 lib/utils/irqchip/objects.mk      |   1 +
 3 files changed, 338 insertions(+)
 create mode 100644 include/sbi_utils/irqchip/imsic.h
 create mode 100644 lib/utils/irqchip/imsic.c
include/sbi_utils/irqchip/imsic.h new file +50 -0
new file mode 100644
index 0000000..cffcb5a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __IRQCHIP_IMSIC_H__
+#define __IRQCHIP_IMSIC_H__
+
+#include <sbi/sbi_types.h>
+
+#define IMSIC_MMIO_PAGE_SHIFT		12
+#define IMSIC_MMIO_PAGE_SZ		(1UL << IMSIC_MMIO_PAGE_SHIFT)
+
+#define IMSIC_MAX_REGS			16
+
+struct imsic_regs {
+	unsigned long addr;
+	unsigned long size;
+};
+
+struct imsic_data {
+	bool targets_mmode;
+	u32 guest_index_bits;
+	u32 hart_index_bits;
+	u32 group_index_bits;
+	u32 group_index_shift;
+	unsigned long num_ids;
+	struct imsic_regs regs[IMSIC_MAX_REGS];
+};
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic, int file);
+
+struct imsic_data *imsic_get_data(u32 hartid);
+
+int imsic_get_target_file(u32 hartid);
+
+void imsic_local_irqchip_init(void);
+
+int imsic_warm_irqchip_init(void);
+
+int imsic_data_check(struct imsic_data *imsic);
+
+int imsic_cold_irqchip_init(struct imsic_data *imsic);
+
+#endif
lib/utils/irqchip/imsic.c new file +287 -0

287 lines not on this page · raw message

lib/utils/irqchip/objects.mk +1 -0
index 934f706..76a3c94 100644
@@ -9,4 +9,5 @@
 
 libsbiutils-objs-y += irqchip/fdt_irqchip.o
 libsbiutils-objs-y += irqchip/fdt_irqchip_plic.o
+libsbiutils-objs-y += irqchip/imsic.o
 libsbiutils-objs-y += irqchip/plic.o
Signature · 1 line
2.25.1
#11 lore raw

PATCH 8/13 lib: utils/irqchip: Add IMSIC library

? 2022-01-04???? 15:43 +0530?Anup Patel???

Quoted 176 lines

We add simple IMSIC library which is independent of hardware description
format (FDT or ACPI). This IMSIC library can be used by custom OpenSBI platform support to setup IMSIC for external interrupts.

---
?include/sbi_utils/irqchip/imsic.h |? 50 ++++++ ?lib/utils/irqchip/imsic.c???????? | 287
++++++++++++++++++++++++++++++
?lib/utils/irqchip/objects.mk????? |?? 1 +
?3 files changed, 338 insertions(+)
?create mode 100644 include/sbi_utils/irqchip/imsic.h ?create mode 100644 lib/utils/irqchip/imsic.c

include/sbi_utils/irqchip/imsic.h new file +36 -0
new file mode 100644
index 0000000..cffcb5a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *?? Anup Patel <[email protected]>
+ */
+
+#ifndef __IRQCHIP_IMSIC_H__
+#define __IRQCHIP_IMSIC_H__
+
+#include <sbi/sbi_types.h>
+
+#define IMSIC_MMIO_PAGE_SHIFT??????????12
+#define IMSIC_MMIO_PAGE_SZ?????????????(1UL << IMSIC_MMIO_PAGE_SHIFT)
+
+#define IMSIC_MAX_REGS?????????????????16
+
+struct imsic_regs {
+???????unsigned long addr;
+???????unsigned long size;
+};
+
+struct imsic_data {
+???????bool targets_mmode;
+???????u32 guest_index_bits;
+???????u32 hart_index_bits;
+???????u32 group_index_bits;
+???????u32 group_index_shift;
+???????unsigned long num_ids;
+???????struct imsic_regs regs[IMSIC_MAX_REGS];
+};
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic,

int file);

include/sbi_utils/irqchip/imsic.h new file +14 -0
+
+struct imsic_data *imsic_get_data(u32 hartid);
+
+int imsic_get_target_file(u32 hartid);
+
+void imsic_local_irqchip_init(void);
+
+int imsic_warm_irqchip_init(void);
+
+int imsic_data_check(struct imsic_data *imsic);
+
+int imsic_cold_irqchip_init(struct imsic_data *imsic);
+
+#endif
lib/utils/irqchip/imsic.c new file +69 -0
new file mode 100644
index 0000000..f87321f
@@ -0,0 +1,287 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *?? Anup Patel <[email protected]>
+ */
+
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
+#include <sbi/sbi_hartmask.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_trap.h>
+#include <sbi_utils/irqchip/imsic.h>
+
+#define IMSIC_MMIO_PAGE_LE?????????????0x00
+#define IMSIC_MMIO_PAGE_BE?????????????0x04
+
+#define IMSIC_MIN_ID???????????????????63
+#define IMSIC_MAX_ID???????????????????2047
+
+#define IMSIC_EIDELIVERY???????????????0x70
+
+#define IMSIC_EITHRESHOLD??????????????0x72
+
+#define IMSIC_TOPEI????????????????????0x76
+#define IMSIC_TOPEI_ID_SHIFT???????????16
+#define IMSIC_TOPEI_ID_MASK????????????0x7ff
+#define IMSIC_TOPEI_PRIO_MASK??????????0x7ff
+
+#define IMSIC_EIP0?????????????????????0x80
+
+#define IMSIC_EIP63????????????????????0xbf
+
+#define IMSIC_EIE0?????????????????????0xc0
+
+#define IMSIC_EIE63????????????????????0xff
+
+#define IMSIC_DISABLE_EIDELIVERY???????0
+#define IMSIC_ENABLE_EIDELIVERY????????1
+#define IMSIC_DISABLE_EITHRESHOLD??????0
+#define IMSIC_ENABLE_EITHRESHOLD???????IMSIC_MAX_ID
+
+#define IMSIC_IPI_ID???????????????????1
+
+#define imsic_csr_write(__c, __v)??????\
+do { \
+???????csr_write(CSR_MISELECT, __c); \
+???????csr_write(CSR_MIREG, __v); \
+} while (0)
+
+#define imsic_csr_read(__c)????\
+({ \
+???????unsigned long __v; \
+???????csr_write(CSR_MISELECT, __c); \
+???????__v = csr_read(CSR_MIREG); \
+???????__v; \
+})
+
+static struct imsic_data *imsic_hartid2data[SBI_HARTMASK_MAX_BITS];
+static int imsic_hartid2file[SBI_HARTMASK_MAX_BITS];
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic,

int file)

lib/utils/irqchip/imsic.c new file +26 -0
+{
+???????if (!imsic || !imsic->targets_mmode ||
+?????????? (SBI_HARTMASK_MAX_BITS <= hartid))
+???????????????return SBI_EINVAL;
+
+???????imsic_hartid2data[hartid] = imsic;
+???????imsic_hartid2file[hartid] = file;
+???????return 0;
+}
+
+struct imsic_data *imsic_get_data(u32 hartid)
+{
+???????if (SBI_HARTMASK_MAX_BITS <= hartid)
+???????????????return NULL;
+???????return imsic_hartid2data[hartid];
+}
+
+int imsic_get_target_file(u32 hartid)
+{
+???????if ((SBI_HARTMASK_MAX_BITS <= hartid) ||
+?????????? !imsic_hartid2data[hartid])
+???????????????return SBI_ENOENT;
+???????return imsic_hartid2file[hartid];
+}
+
+static int imsic_external_irqfn(struct sbi_trap_regs *regs)

lib/utils/irqchip/imsic.c

Quoted 10 lines
lib/utils/irqchip/imsic.c new file +10 -0
+{
+???????ulong mirq;
+
+???????while ((mirq = csr_swap(CSR_MTOPEI, 0))) {
+???????????????mirq = (mirq >> IMSIC_TOPEI_ID_SHIFT);
+
+???????????????switch (mirq) {
+???????????????case IMSIC_IPI_ID:
+???????????????????????sbi_ipi_process();
+???????????????????????break;

Why not add a timer interrupt?

Quoted 24 lines

+???????????????default:
+???????????????????????sbi_printf("%s: unhandled IRQ%d\n", +????????????????????????????????? __func__, (u32)mirq); +???????????????????????break;
+???????????????}
+???????}

lib/utils/irqchip/imsic.c new file +16 -0
+
+???????return 0;
+}
+
+static void imsic_ipi_send(u32 target_hart)
+{
+???????unsigned long reloff;
+???????struct imsic_regs *regs;
+???????struct imsic_data *data = imsic_hartid2data[target_hart];
+???????int file = imsic_hartid2file[target_hart];
+
+???????if (!data || !data->targets_mmode)
+???????????????return;
+
+???????regs = &data->regs[0];
+???????reloff = file * (1UL << data->guest_index_bits) *

IMSIC_MMIO_PAGE_SZ;
+???????while (regs->size && (regs->size <= reloff)) {

Quoted 8 lines

+???????????????reloff -= regs->size; +???????????????regs++;
+???????}
+
+???????if (regs->size && (reloff < regs->size)) +???????????????writel(IMSIC_IPI_ID, +????????????????????? (void *)(regs->addr + reloff + IMSIC_MMIO_PAGE_LE));

Using "void *" in arithmetic causes errors with -Werror=pointer-arith

Regards?
Xiang W

Quoted 170 lines
lib/utils/irqchip/imsic.c new file +32 -0
+}
+
+static struct sbi_ipi_device imsic_ipi_device = {
+???????.name???????????= "aia-imsic",
+???????.ipi_send???????= imsic_ipi_send
+};
+
+void imsic_local_irqchip_init(void)
+{
+???????/*
+??????? * This function is expected to be called from:
+??????? * 1) nascent_init() platform callback which is called
+??????? *??? very early on each HART in boot-up path and and
+??????? *??? HSM resume path.
+??????? * 2) irqchip_init() platform callback which is called
+??????? *??? in boot-up path.
+??????? */
+
+???????/* Setup threshold to allow all enabled interrupts */
+???????imsic_csr_write(IMSIC_EITHRESHOLD, IMSIC_ENABLE_EITHRESHOLD);
+
+???????/* Enable interrupt delivery */
+???????imsic_csr_write(IMSIC_EIDELIVERY, IMSIC_ENABLE_EIDELIVERY);
+
+???????/* Enable IPI */
+???????csr_write(CSR_MSETEIENUM, IMSIC_IPI_ID);
+}
+
+int imsic_warm_irqchip_init(void)
+{
+???????unsigned long i;
+???????struct imsic_data *imsic =

imsic_hartid2data[current_hartid()];

lib/utils/irqchip/imsic.c new file +71 -0
+
+???????/* Sanity checks */
+???????if (!imsic || !imsic->targets_mmode)
+???????????????return SBI_EINVAL;
+
+???????/* Disable all interrupts */
+???????for (i = 1; i <= imsic->num_ids; i++)
+???????????????csr_write(CSR_MCLREIENUM, i);
+
+???????/* Clear IPI */
+???????csr_write(CSR_MCLREIPNUM, IMSIC_IPI_ID);
+
+???????/* Local IMSIC initialization */
+???????imsic_local_irqchip_init();
+
+???????return 0;
+}
+
+int imsic_data_check(struct imsic_data *imsic)
+{
+???????u32 i, tmp;
+???????unsigned long base_addr, addr, mask;
+
+???????/* Sanity checks */
+???????if (!imsic ||
+?????????? (imsic->num_ids < IMSIC_MIN_ID) ||
+?????????? (IMSIC_MAX_ID < imsic->num_ids))
+???????????????return SBI_EINVAL;
+
+???????tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT;
+???????if (tmp < imsic->guest_index_bits)
+???????????????return SBI_EINVAL;
+
+???????tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
+???????????? imsic->guest_index_bits;
+???????if (tmp < imsic->hart_index_bits)
+???????????????return SBI_EINVAL;
+
+???????tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
+???????????? imsic->guest_index_bits - imsic->hart_index_bits;
+???????if (tmp < imsic->group_index_bits)
+???????????????return SBI_EINVAL;
+
+???????tmp = IMSIC_MMIO_PAGE_SHIFT + imsic->guest_index_bits +
+???????????? imsic->hart_index_bits;
+???????if (imsic->group_index_shift < tmp)
+???????????????return SBI_EINVAL;
+???????tmp = imsic->group_index_bits + imsic->group_index_shift - 1;
+???????if (tmp >= BITS_PER_LONG)
+???????????????return SBI_EINVAL;
+
+???????/*
+??????? * Number of interrupt identities should be 1 less than
+??????? * multiple of 63
+??????? */
+???????if ((imsic->num_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID)
+???????????????return SBI_EINVAL;
+
+???????/* We should have at least one regset */
+???????if (!imsic->regs[0].size)
+???????????????return SBI_EINVAL;
+
+???????/* Match patter of each regset */
+???????base_addr = imsic->regs[0].addr;
+???????base_addr &= ~((1UL << (imsic->guest_index_bits +
+??????????????????????????????? imsic->hart_index_bits +
+??????????????????????????????? IMSIC_MMIO_PAGE_SHIFT)) - 1);
+???????base_addr &= ~(((1UL << imsic->group_index_bits) - 1) <<
+???????????????????????imsic->group_index_shift);
+???????for (i = 0; i < IMSIC_MAX_REGS && imsic->regs[i].size; i++) {
+???????????????mask = (1UL << imsic->guest_index_bits) *

IMSIC_MMIO_PAGE_SZ;
+???????????????mask -= 1UL;
+???????????????if (imsic->regs[i].size & mask)
+???????????????????????return SBI_EINVAL;

lib/utils/irqchip/imsic.c new file +36 -0
+
+???????????????addr = imsic->regs[i].addr;
+???????????????addr &= ~((1UL << (imsic->guest_index_bits +
+??????????????????????????????????????? imsic->hart_index_bits +
+??????????????????????????????????????? IMSIC_MMIO_PAGE_SHIFT)) - 1);
+???????????????addr &= ~(((1UL << imsic->group_index_bits) - 1) <<
+???????????????????????????????imsic->group_index_shift);
+???????????????if (base_addr != addr)
+???????????????????????return SBI_EINVAL;
+???????}
+
+???????return 0;
+}
+
+int imsic_cold_irqchip_init(struct imsic_data *imsic)
+{
+???????int i, rc;
+???????struct sbi_domain_memregion reg;
+
+???????/* Sanity checks */
+???????rc = imsic_data_check(imsic);
+???????if (rc)
+???????????????return rc;
+
+???????/* We only initialize M-mode IMSIC */
+???????if (!imsic->targets_mmode)
+???????????????return SBI_EINVAL;
+
+???????/* Setup external interrupt function for IMSIC */
+???????sbi_trap_set_external_irqfn(imsic_external_irqfn);
+
+???????/* Add IMSIC regions to the root domain */
+???????for (i = 0; i < IMSIC_MAX_REGS && imsic->regs[i].size; i++) {
+???????????????sbi_domain_memregion_init(imsic->regs[i].addr,
+???????????????????????????????????????? imsic->regs[i].size,
+???????????????????????????????????????? SBI_DOMAIN_MEMREGION_MMIO,

&reg);
+???????????????rc = sbi_domain_root_add_memregion(&reg); +???????????????if (rc) +???????????????????????return rc;
+???????}

lib/utils/irqchip/imsic.c new file +6 -0
+
+???????/* Register IPI device */
+???????sbi_ipi_set_device(&imsic_ipi_device);
+
+???????return 0;
+}
lib/utils/irqchip/objects.mk +0 -0
index 934f706..76a3c94 100644
@@ -9,4 +9,5 @@

?
?libsbiutils-objs-y += irqchip/fdt_irqchip.o
?libsbiutils-objs-y += irqchip/fdt_irqchip_plic.o
+libsbiutils-objs-y += irqchip/imsic.o
?libsbiutils-objs-y += irqchip/plic.o

Signature · 1 line
2.25.1
#12 lore raw

PATCH 8/13 lib: utils/irqchip: Add IMSIC library

Quoted 214 lines · Xiang W

? 2022-01-04???? 15:43 +0530?Anup Patel???

Quoted 176 lines

We add simple IMSIC library which is independent of hardware description
format (FDT or ACPI). This IMSIC library can be used by custom OpenSBI platform support to setup IMSIC for external interrupts.

---
 include/sbi_utils/irqchip/imsic.h |  50 ++++++
 lib/utils/irqchip/imsic.c         | 287
++++++++++++++++++++++++++++++
 lib/utils/irqchip/objects.mk      |   1 +
 3 files changed, 338 insertions(+)
 create mode 100644 include/sbi_utils/irqchip/imsic.h
 create mode 100644 lib/utils/irqchip/imsic.c
include/sbi_utils/irqchip/imsic.h new file +36 -0
new file mode 100644
index 0000000..cffcb5a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __IRQCHIP_IMSIC_H__
+#define __IRQCHIP_IMSIC_H__
+
+#include <sbi/sbi_types.h>
+
+#define IMSIC_MMIO_PAGE_SHIFT          12
+#define IMSIC_MMIO_PAGE_SZ             (1UL << IMSIC_MMIO_PAGE_SHIFT)
+
+#define IMSIC_MAX_REGS                 16
+
+struct imsic_regs {
+       unsigned long addr;
+       unsigned long size;
+};
+
+struct imsic_data {
+       bool targets_mmode;
+       u32 guest_index_bits;
+       u32 hart_index_bits;
+       u32 group_index_bits;
+       u32 group_index_shift;
+       unsigned long num_ids;
+       struct imsic_regs regs[IMSIC_MAX_REGS];
+};
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic,

int file);

include/sbi_utils/irqchip/imsic.h new file +14 -0
+
+struct imsic_data *imsic_get_data(u32 hartid);
+
+int imsic_get_target_file(u32 hartid);
+
+void imsic_local_irqchip_init(void);
+
+int imsic_warm_irqchip_init(void);
+
+int imsic_data_check(struct imsic_data *imsic);
+
+int imsic_cold_irqchip_init(struct imsic_data *imsic);
+
+#endif
lib/utils/irqchip/imsic.c new file +69 -0
new file mode 100644
index 0000000..f87321f
@@ -0,0 +1,287 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
+#include <sbi/sbi_hartmask.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_trap.h>
+#include <sbi_utils/irqchip/imsic.h>
+
+#define IMSIC_MMIO_PAGE_LE             0x00
+#define IMSIC_MMIO_PAGE_BE             0x04
+
+#define IMSIC_MIN_ID                   63
+#define IMSIC_MAX_ID                   2047
+
+#define IMSIC_EIDELIVERY               0x70
+
+#define IMSIC_EITHRESHOLD              0x72
+
+#define IMSIC_TOPEI                    0x76
+#define IMSIC_TOPEI_ID_SHIFT           16
+#define IMSIC_TOPEI_ID_MASK            0x7ff
+#define IMSIC_TOPEI_PRIO_MASK          0x7ff
+
+#define IMSIC_EIP0                     0x80
+
+#define IMSIC_EIP63                    0xbf
+
+#define IMSIC_EIE0                     0xc0
+
+#define IMSIC_EIE63                    0xff
+
+#define IMSIC_DISABLE_EIDELIVERY       0
+#define IMSIC_ENABLE_EIDELIVERY        1
+#define IMSIC_DISABLE_EITHRESHOLD      0
+#define IMSIC_ENABLE_EITHRESHOLD       IMSIC_MAX_ID
+
+#define IMSIC_IPI_ID                   1
+
+#define imsic_csr_write(__c, __v)      \
+do { \
+       csr_write(CSR_MISELECT, __c); \
+       csr_write(CSR_MIREG, __v); \
+} while (0)
+
+#define imsic_csr_read(__c)    \
+({ \
+       unsigned long __v; \
+       csr_write(CSR_MISELECT, __c); \
+       __v = csr_read(CSR_MIREG); \
+       __v; \
+})
+
+static struct imsic_data *imsic_hartid2data[SBI_HARTMASK_MAX_BITS];
+static int imsic_hartid2file[SBI_HARTMASK_MAX_BITS];
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic,

int file)

lib/utils/irqchip/imsic.c new file +26 -0
+{
+       if (!imsic || !imsic->targets_mmode ||
+           (SBI_HARTMASK_MAX_BITS <= hartid))
+               return SBI_EINVAL;
+
+       imsic_hartid2data[hartid] = imsic;
+       imsic_hartid2file[hartid] = file;
+       return 0;
+}
+
+struct imsic_data *imsic_get_data(u32 hartid)
+{
+       if (SBI_HARTMASK_MAX_BITS <= hartid)
+               return NULL;
+       return imsic_hartid2data[hartid];
+}
+
+int imsic_get_target_file(u32 hartid)
+{
+       if ((SBI_HARTMASK_MAX_BITS <= hartid) ||
+           !imsic_hartid2data[hartid])
+               return SBI_ENOENT;
+       return imsic_hartid2file[hartid];
+}
+
+static int imsic_external_irqfn(struct sbi_trap_regs *regs)

lib/utils/irqchip/imsic.c line 105

Quoted 10 lines
lib/utils/irqchip/imsic.c new file +10 -0
+{
+       ulong mirq;
+
+       while ((mirq = csr_swap(CSR_MTOPEI, 0))) {
+               mirq = (mirq >> IMSIC_TOPEI_ID_SHIFT);
+
+               switch (mirq) {
+               case IMSIC_IPI_ID:
+                       sbi_ipi_process();
+                       break;

Why not add a timer interrupt?

Quoted 25 lines
lib/utils/irqchip/imsic.c new file +22 -0
+               default:
+                       sbi_printf("%s: unhandled IRQ%d\n",
+                                  __func__, (u32)mirq);
+                       break;
+               }
+       }
+
+       return 0;
+}
+
+static void imsic_ipi_send(u32 target_hart)
+{
+       unsigned long reloff;
+       struct imsic_regs *regs;
+       struct imsic_data *data = imsic_hartid2data[target_hart];
+       int file = imsic_hartid2file[target_hart];
+
+       if (!data || !data->targets_mmode)
+               return;
+
+       regs = &data->regs[0];
+       reloff = file * (1UL << data->guest_index_bits) *

IMSIC_MMIO_PAGE_SZ;

lib/utils/irqchip/imsic.c new file +2 -0
+       while (regs->size && (regs->size <= reloff)) {
+               reloff -= regs->size;
Quoted 8 lines
Quoted 7 lines
lib/utils/irqchip/imsic.c new file +6 -0
+               regs++;
+       }
+
+       if (regs->size && (reloff < regs->size))
+               writel(IMSIC_IPI_ID,
+                      (void *)(regs->addr + reloff +

IMSIC_MMIO_PAGE_LE));

Using "void *" in arithmetic causes errors with -Werror=pointer-arith

The cast is outside the parentheses, the arithmetic happens first.

Jess

#13 lore raw

PATCH 8/13 lib: utils/irqchip: Add IMSIC library

In reply to Xiang W

? 2022-01-20???? 00:08 +0800?Xiang W???

Quoted 217 lines

? 2022-01-04???? 15:43 +0530?Anup Patel???

Quoted 180 lines

We add simple IMSIC library which is independent of hardware description
format (FDT or ACPI). This IMSIC library can be used by custom OpenSBI
platform support to setup IMSIC for external interrupts.

---
?include/sbi_utils/irqchip/imsic.h |? 50 ++++++ ?lib/utils/irqchip/imsic.c???????? | 287
++++++++++++++++++++++++++++++
?lib/utils/irqchip/objects.mk????? |?? 1 +
?3 files changed, 338 insertions(+)
?create mode 100644 include/sbi_utils/irqchip/imsic.h ?create mode 100644 lib/utils/irqchip/imsic.c

include/sbi_utils/irqchip/imsic.h new file +4 -0
new file mode 100644
index 0000000..cffcb5a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its

affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *?? Anup Patel <[email protected]>
+ */

include/sbi_utils/irqchip/imsic.h new file +8 -0
+
+#ifndef __IRQCHIP_IMSIC_H__
+#define __IRQCHIP_IMSIC_H__
+
+#include <sbi/sbi_types.h>
+
+#define IMSIC_MMIO_PAGE_SHIFT??????????12
+#define IMSIC_MMIO_PAGE_SZ?????????????(1UL <<

IMSIC_MMIO_PAGE_SHIFT)

include/sbi_utils/irqchip/imsic.h new file +19 -0
+
+#define IMSIC_MAX_REGS?????????????????16
+
+struct imsic_regs {
+???????unsigned long addr;
+???????unsigned long size;
+};
+
+struct imsic_data {
+???????bool targets_mmode;
+???????u32 guest_index_bits;
+???????u32 hart_index_bits;
+???????u32 group_index_bits;
+???????u32 group_index_shift;
+???????unsigned long num_ids;
+???????struct imsic_regs regs[IMSIC_MAX_REGS];
+};
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic,

int file);

include/sbi_utils/irqchip/imsic.h new file +14 -0
+
+struct imsic_data *imsic_get_data(u32 hartid);
+
+int imsic_get_target_file(u32 hartid);
+
+void imsic_local_irqchip_init(void);
+
+int imsic_warm_irqchip_init(void);
+
+int imsic_data_check(struct imsic_data *imsic);
+
+int imsic_cold_irqchip_init(struct imsic_data *imsic);
+
+#endif
lib/utils/irqchip/imsic.c new file +4 -0
new file mode 100644
index 0000000..f87321f
@@ -0,0 +1,287 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its

affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *?? Anup Patel <[email protected]>
+ */

lib/utils/irqchip/imsic.c new file +60 -0
+
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
+#include <sbi/sbi_hartmask.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_trap.h>
+#include <sbi_utils/irqchip/imsic.h>
+
+#define IMSIC_MMIO_PAGE_LE?????????????0x00
+#define IMSIC_MMIO_PAGE_BE?????????????0x04
+
+#define IMSIC_MIN_ID???????????????????63
+#define IMSIC_MAX_ID???????????????????2047
+
+#define IMSIC_EIDELIVERY???????????????0x70
+
+#define IMSIC_EITHRESHOLD??????????????0x72
+
+#define IMSIC_TOPEI????????????????????0x76
+#define IMSIC_TOPEI_ID_SHIFT???????????16
+#define IMSIC_TOPEI_ID_MASK????????????0x7ff
+#define IMSIC_TOPEI_PRIO_MASK??????????0x7ff
+
+#define IMSIC_EIP0?????????????????????0x80
+
+#define IMSIC_EIP63????????????????????0xbf
+
+#define IMSIC_EIE0?????????????????????0xc0
+
+#define IMSIC_EIE63????????????????????0xff
+
+#define IMSIC_DISABLE_EIDELIVERY???????0
+#define IMSIC_ENABLE_EIDELIVERY????????1
+#define IMSIC_DISABLE_EITHRESHOLD??????0
+#define IMSIC_ENABLE_EITHRESHOLD???????IMSIC_MAX_ID
+
+#define IMSIC_IPI_ID???????????????????1
+
+#define imsic_csr_write(__c, __v)??????\
+do { \
+???????csr_write(CSR_MISELECT, __c); \
+???????csr_write(CSR_MIREG, __v); \
+} while (0)
+
+#define imsic_csr_read(__c)????\
+({ \
+???????unsigned long __v; \
+???????csr_write(CSR_MISELECT, __c); \
+???????__v = csr_read(CSR_MIREG); \
+???????__v; \
+})
+
+static struct imsic_data *imsic_hartid2data[SBI_HARTMASK_MAX_BITS];
+static int imsic_hartid2file[SBI_HARTMASK_MAX_BITS];
+
+int imsic_map_hartid_to_data(u32 hartid, struct imsic_data *imsic,

int file)

lib/utils/irqchip/imsic.c new file +26 -0
+{
+???????if (!imsic || !imsic->targets_mmode ||
+?????????? (SBI_HARTMASK_MAX_BITS <= hartid))
+???????????????return SBI_EINVAL;
+
+???????imsic_hartid2data[hartid] = imsic;
+???????imsic_hartid2file[hartid] = file;
+???????return 0;
+}
+
+struct imsic_data *imsic_get_data(u32 hartid)
+{
+???????if (SBI_HARTMASK_MAX_BITS <= hartid)
+???????????????return NULL;
+???????return imsic_hartid2data[hartid];
+}
+
+int imsic_get_target_file(u32 hartid)
+{
+???????if ((SBI_HARTMASK_MAX_BITS <= hartid) ||
+?????????? !imsic_hartid2data[hartid])
+???????????????return SBI_ENOENT;
+???????return imsic_hartid2file[hartid];
+}
+
+static int imsic_external_irqfn(struct sbi_trap_regs *regs)

lib/utils/irqchip/imsic.c

Quoted 10 lines
lib/utils/irqchip/imsic.c new file +10 -0
+{
+???????ulong mirq;
+
+???????while ((mirq = csr_swap(CSR_MTOPEI, 0))) {
+???????????????mirq = (mirq >> IMSIC_TOPEI_ID_SHIFT);
+
+???????????????switch (mirq) {
+???????????????case IMSIC_IPI_ID:
+???????????????????????sbi_ipi_process();
+???????????????????????break;

Why not add a timer interrupt?

Quoted 25 lines

+???????????????default:
+???????????????????????sbi_printf("%s: unhandled IRQ%d\n", +????????????????????????????????? __func__, (u32)mirq); +???????????????????????break;
+???????????????}
+???????}

lib/utils/irqchip/imsic.c new file +16 -0
+
+???????return 0;
+}
+
+static void imsic_ipi_send(u32 target_hart)
+{
+???????unsigned long reloff;
+???????struct imsic_regs *regs;
+???????struct imsic_data *data = imsic_hartid2data[target_hart];
+???????int file = imsic_hartid2file[target_hart];
+
+???????if (!data || !data->targets_mmode)
+???????????????return;
+
+???????regs = &data->regs[0];
+???????reloff = file * (1UL << data->guest_index_bits) *

IMSIC_MMIO_PAGE_SZ;
+???????while (regs->size && (regs->size <= reloff)) { +???????????????reloff -= regs->size;

Quoted 9 lines
Quoted 7 lines

+???????????????regs++;
+???????}
+
+???????if (regs->size && (reloff < regs->size)) +???????????????writel(IMSIC_IPI_ID, +????????????????????? (void *)(regs->addr + reloff + IMSIC_MMIO_PAGE_LE));

Using "void *" in arithmetic causes errors with -Werror=pointer-arith

Sorry, I didn't realize that the type of regs->addr is unsigned long

Quoted 180 lines

Regards?
Xiang W

Quoted 175 lines
lib/utils/irqchip/imsic.c new file +20 -0
+}
+
+static struct sbi_ipi_device imsic_ipi_device = {
+???????.name???????????= "aia-imsic",
+???????.ipi_send???????= imsic_ipi_send
+};
+
+void imsic_local_irqchip_init(void)
+{
+???????/*
+??????? * This function is expected to be called from:
+??????? * 1) nascent_init() platform callback which is called
+??????? *??? very early on each HART in boot-up path and and
+??????? *??? HSM resume path.
+??????? * 2) irqchip_init() platform callback which is called
+??????? *??? in boot-up path.
+??????? */
+
+???????/* Setup threshold to allow all enabled interrupts */
+???????imsic_csr_write(IMSIC_EITHRESHOLD,

IMSIC_ENABLE_EITHRESHOLD);

lib/utils/irqchip/imsic.c new file +12 -0
+
+???????/* Enable interrupt delivery */
+???????imsic_csr_write(IMSIC_EIDELIVERY, IMSIC_ENABLE_EIDELIVERY);
+
+???????/* Enable IPI */
+???????csr_write(CSR_MSETEIENUM, IMSIC_IPI_ID);
+}
+
+int imsic_warm_irqchip_init(void)
+{
+???????unsigned long i;
+???????struct imsic_data *imsic =

imsic_hartid2data[current_hartid()];

lib/utils/irqchip/imsic.c new file +48 -0
+
+???????/* Sanity checks */
+???????if (!imsic || !imsic->targets_mmode)
+???????????????return SBI_EINVAL;
+
+???????/* Disable all interrupts */
+???????for (i = 1; i <= imsic->num_ids; i++)
+???????????????csr_write(CSR_MCLREIENUM, i);
+
+???????/* Clear IPI */
+???????csr_write(CSR_MCLREIPNUM, IMSIC_IPI_ID);
+
+???????/* Local IMSIC initialization */
+???????imsic_local_irqchip_init();
+
+???????return 0;
+}
+
+int imsic_data_check(struct imsic_data *imsic)
+{
+???????u32 i, tmp;
+???????unsigned long base_addr, addr, mask;
+
+???????/* Sanity checks */
+???????if (!imsic ||
+?????????? (imsic->num_ids < IMSIC_MIN_ID) ||
+?????????? (IMSIC_MAX_ID < imsic->num_ids))
+???????????????return SBI_EINVAL;
+
+???????tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT;
+???????if (tmp < imsic->guest_index_bits)
+???????????????return SBI_EINVAL;
+
+???????tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
+???????????? imsic->guest_index_bits;
+???????if (tmp < imsic->hart_index_bits)
+???????????????return SBI_EINVAL;
+
+???????tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
+???????????? imsic->guest_index_bits - imsic->hart_index_bits;
+???????if (tmp < imsic->group_index_bits)
+???????????????return SBI_EINVAL;
+
+???????tmp = IMSIC_MMIO_PAGE_SHIFT + imsic->guest_index_bits +
+???????????? imsic->hart_index_bits;
+???????if (imsic->group_index_shift < tmp)
+???????????????return SBI_EINVAL;
+???????tmp = imsic->group_index_bits + imsic->group_index_shift -

1;
+???????if (tmp >= BITS_PER_LONG)
+???????????????return SBI_EINVAL;

lib/utils/irqchip/imsic.c new file +20 -0
+
+???????/*
+??????? * Number of interrupt identities should be 1 less than
+??????? * multiple of 63
+??????? */
+???????if ((imsic->num_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID)
+???????????????return SBI_EINVAL;
+
+???????/* We should have at least one regset */
+???????if (!imsic->regs[0].size)
+???????????????return SBI_EINVAL;
+
+???????/* Match patter of each regset */
+???????base_addr = imsic->regs[0].addr;
+???????base_addr &= ~((1UL << (imsic->guest_index_bits +
+??????????????????????????????? imsic->hart_index_bits +
+??????????????????????????????? IMSIC_MMIO_PAGE_SHIFT)) - 1);
+???????base_addr &= ~(((1UL << imsic->group_index_bits) - 1) <<
+???????????????????????imsic->group_index_shift);
+???????for (i = 0; i < IMSIC_MAX_REGS && imsic->regs[i].size; i++)

{
+???????????????mask = (1UL << imsic->guest_index_bits) * IMSIC_MMIO_PAGE_SZ;
+???????????????mask -= 1UL;
+???????????????if (imsic->regs[i].size & mask) +???????????????????????return SBI_EINVAL;
+
+???????????????addr = imsic->regs[i].addr; +???????????????addr &= ~((1UL << (imsic->guest_index_bits + +??????????????????????????????????????? imsic->hart_index_bits + +??????????????????????????????????????? IMSIC_MMIO_PAGE_SHIFT)) - 1);
+???????????????addr &= ~(((1UL << imsic->group_index_bits) - 1) << +???????????????????????????????imsic->group_index_shift); +???????????????if (base_addr != addr) +???????????????????????return SBI_EINVAL;
+???????}

lib/utils/irqchip/imsic.c new file +23 -0
+
+???????return 0;
+}
+
+int imsic_cold_irqchip_init(struct imsic_data *imsic)
+{
+???????int i, rc;
+???????struct sbi_domain_memregion reg;
+
+???????/* Sanity checks */
+???????rc = imsic_data_check(imsic);
+???????if (rc)
+???????????????return rc;
+
+???????/* We only initialize M-mode IMSIC */
+???????if (!imsic->targets_mmode)
+???????????????return SBI_EINVAL;
+
+???????/* Setup external interrupt function for IMSIC */
+???????sbi_trap_set_external_irqfn(imsic_external_irqfn);
+
+???????/* Add IMSIC regions to the root domain */
+???????for (i = 0; i < IMSIC_MAX_REGS && imsic->regs[i].size; i++)

{ +???????????????sbi_domain_memregion_init(imsic->regs[i].addr, +???????????????????????????????????????? imsic->regs[i].size, +???????????????????????????????????????? SBI_DOMAIN_MEMREGION_MMIO, &reg);
+???????????????rc = sbi_domain_root_add_memregion(&reg); +???????????????if (rc)
+???????????????????????return rc;
+???????}

lib/utils/irqchip/imsic.c new file +6 -0
+
+???????/* Register IPI device */
+???????sbi_ipi_set_device(&imsic_ipi_device);
+
+???????return 0;
+}
lib/utils/irqchip/objects.mk +0 -0
index 934f706..76a3c94 100644
@@ -9,4 +9,5 @@

?
?libsbiutils-objs-y += irqchip/fdt_irqchip.o
?libsbiutils-objs-y += irqchip/fdt_irqchip_plic.o
+libsbiutils-objs-y += irqchip/imsic.o
?libsbiutils-objs-y += irqchip/plic.o

Signature · 1 line
2.25.1
author
#14 lore raw

PATCH 9/13 lib: utils/irqchip: Add FDT based driver for IMSIC

In reply to Anup Patel

We add simple FDT irqchip driver for IMSIC so that generic platform (and other FDT based platforms) can utilize common IMIC library.

---
 include/sbi_utils/fdt/fdt_helper.h    |   6 ++
 lib/utils/fdt/fdt_helper.c            | 103 +++++++++++++++++++++++++
 lib/utils/irqchip/fdt_irqchip.c       |   2 +
 lib/utils/irqchip/fdt_irqchip_imsic.c | 106 ++++++++++++++++++++++++++
 lib/utils/irqchip/objects.mk          |   1 +
 platform/generic/platform.c           |  12 +++
 6 files changed, 230 insertions(+)
 create mode 100644 lib/utils/irqchip/fdt_irqchip_imsic.c
include/sbi_utils/fdt/fdt_helper.h +6 -0
index 24fee7a..4c8d29e 100644
@@ -68,6 +68,12 @@ int fdt_parse_uart8250_node(void *fdt, int nodeoffset,
 int fdt_parse_uart8250(void *fdt, struct platform_uart_data *uart,
 		       const char *compatible);
 
+struct imsic_data;
+
+bool fdt_check_imsic_mlevel(void *fdt);
+
+int fdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic);
+
 struct plic_data;
 
 int fdt_parse_plic_node(void *fdt, int nodeoffset, struct plic_data *plic);
lib/utils/fdt/fdt_helper.c +103 -0

115 lines not on this page · raw message

lib/utils/irqchip/fdt_irqchip.c +2 -0
index bf6969a..cf64a2e 100644
@@ -12,9 +12,11 @@
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/irqchip/fdt_irqchip.h>
 
+extern struct fdt_irqchip fdt_irqchip_imsic;
 extern struct fdt_irqchip fdt_irqchip_plic;
 
 static struct fdt_irqchip *irqchip_drivers[] = {
+	&fdt_irqchip_imsic,
 	&fdt_irqchip_plic
 };
 
lib/utils/irqchip/fdt_irqchip_imsic.c new file +106 -0

106 lines not on this page · raw message

lib/utils/irqchip/objects.mk +1 -0
index 76a3c94..ae6f255 100644
@@ -8,6 +8,7 @@
 #
 
 libsbiutils-objs-y += irqchip/fdt_irqchip.o
+libsbiutils-objs-y += irqchip/fdt_irqchip_imsic.o
 libsbiutils-objs-y += irqchip/fdt_irqchip_plic.o
 libsbiutils-objs-y += irqchip/imsic.o
 libsbiutils-objs-y += irqchip/plic.o
platform/generic/platform.c +12 -0
index bc6e761..8a4fb70 100644
@@ -18,6 +18,7 @@
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/fdt/fdt_pmu.h>
 #include <sbi_utils/irqchip/fdt_irqchip.h>
+#include <sbi_utils/irqchip/imsic.h>
 #include <sbi_utils/serial/fdt_serial.h>
 #include <sbi_utils/timer/fdt_timer.h>
 #include <sbi_utils/ipi/fdt_ipi.h>
@@ -56,6 +57,7 @@ static void fw_platform_lookup_special(void *fdt, int root_offset)
 }
 
 extern struct sbi_platform platform;
+static bool platform_has_mlevel_imsic = false;
 static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
 
 /*
@@ -110,6 +112,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 
 	platform.hart_count = hart_count;
 
+	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+
 	/* Return original FDT pointer */
 	return arg1;
 
@@ -118,6 +122,13 @@ fail:
 		wfi();
 }
 
+static int generic_nascent_init(void)
+{
+	if (platform_has_mlevel_imsic)
+		imsic_local_irqchip_init();
+	return 0;
+}
+
 static int generic_early_init(bool cold_boot)
 {
 	if (!generic_plat || !generic_plat->early_init)
@@ -210,6 +221,7 @@ static uint64_t generic_pmu_xlate_to_mhpmevent(uint32_t event_idx,
 }
 
 const struct sbi_platform_operations platform_ops = {
+	.nascent_init		= generic_nascent_init,
 	.early_init		= generic_early_init,
 	.final_init		= generic_final_init,
 	.early_exit		= generic_early_exit,
Signature · 1 line
2.25.1
author
#15 lore raw

PATCH 10/13 lib: utils: Disable appropriate IMSIC DT nodes in fdt_fixups()

In reply to Anup Patel

We should disable IMSIC DT nodes in fdt_fixups() which are not accessible to the next booting stage based on currently assigned domain.

---
 include/sbi_utils/fdt/fdt_fixup.h | 17 ++++++++++++++--
 lib/utils/fdt/fdt_fixup.c         | 32 +++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)
include/sbi_utils/fdt/fdt_fixup.h +15 -2
index c38e5d9..77575bb 100644
@@ -21,6 +21,18 @@
  */
 void fdt_cpu_fixup(void *fdt);
 
+/**
+ * Fix up the IMSIC nodes in the device tree
+ *
+ * This routine disables IMSIC nodes which are not accessible to the next
+ * booting stage based on currently assigned domain.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_imsic_fixup(void *fdt);
+
 /**
  * Fix up the PLIC node in the device tree
  *
@@ -64,8 +76,9 @@ int fdt_reserved_memory_nomap_fixup(void *fdt);
  * General device tree fix-up
  *
  * This routine do all required device tree fix-ups for a typical platform.
- * It fixes up the PLIC node and the reserved memory node in the device tree
- * by calling the corresponding helper routines to accomplish the task.
+ * It fixes up the PLIC node, IMSIC nodes, and the reserved memory node in
+ * the device tree by calling the corresponding helper routines to accomplish
+ * the task.
  *
  * It is recommended that platform codes call this helper in their final_init()
  *
lib/utils/fdt/fdt_fixup.c +30 -2
index ac01ba3..c2460b8 100644
@@ -52,6 +52,34 @@ void fdt_cpu_fixup(void *fdt)
 	}
 }
 
+static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
+{
+	int rc;
+	uint64_t reg_addr, reg_size;
+	struct sbi_domain *dom = sbi_domain_thishart_ptr();
+
+	rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &reg_addr, &reg_size);
+	if (rc < 0 || !reg_addr || !reg_size)
+		return;
+
+	if (!sbi_domain_check_addr(dom, reg_addr, dom->next_mode,
+				    SBI_DOMAIN_READ | SBI_DOMAIN_WRITE)) {
+		rc = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
+		if (rc < 0)
+			return;
+		fdt_setprop_string(fdt, nodeoff, "status", "disabled");
+	}
+}
+
+void fdt_imsic_fixup(void *fdt)
+{
+	int noff = 0;
+
+	while ((noff = fdt_node_offset_by_compatible(fdt, noff,
+						     "riscv,imsics")) >= 0)
+		fdt_domain_based_fixup_one(fdt, noff);
+}
+
 void fdt_plic_fixup(void *fdt)
 {
 	u32 *cells;
@@ -261,10 +289,10 @@ int fdt_reserved_memory_nomap_fixup(void *fdt)
 
 void fdt_fixups(void *fdt)
 {
+	fdt_imsic_fixup(fdt);
+
 	fdt_plic_fixup(fdt);
 
 	fdt_reserved_memory_fixup(fdt);
 	fdt_pmu_fixup(fdt);
 }
-
-
Signature · 1 line
2.25.1
#16 lore raw

PATCH 10/13 lib: utils: Disable appropriate IMSIC DT nodes in fdt_fixups()

Quoted 64 lines · Anup Patel

We should disable IMSIC DT nodes in fdt_fixups() which are not accessible to the next booting stage based on currently assigned domain.

---
 include/sbi_utils/fdt/fdt_fixup.h | 17 ++++++++++++++--
 lib/utils/fdt/fdt_fixup.c         | 32 +++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)
include/sbi_utils/fdt/fdt_fixup.h +15 -2
index c38e5d9..77575bb 100644
@@ -21,6 +21,18 @@
  */
 void fdt_cpu_fixup(void *fdt);
 
+/**
+ * Fix up the IMSIC nodes in the device tree
+ *
+ * This routine disables IMSIC nodes which are not accessible to the next
+ * booting stage based on currently assigned domain.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_imsic_fixup(void *fdt);
+
 /**
  * Fix up the PLIC node in the device tree
  *
@@ -64,8 +76,9 @@ int fdt_reserved_memory_nomap_fixup(void *fdt);
  * General device tree fix-up
  *
  * This routine do all required device tree fix-ups for a typical platform.
- * It fixes up the PLIC node and the reserved memory node in the device tree
- * by calling the corresponding helper routines to accomplish the task.
+ * It fixes up the PLIC node, IMSIC nodes, and the reserved memory node in
+ * the device tree by calling the corresponding helper routines to accomplish
+ * the task.
  *
  * It is recommended that platform codes call this helper in their final_init()
  *
lib/utils/fdt/fdt_fixup.c +9 -0
index ac01ba3..c2460b8 100644
@@ -52,6 +52,34 @@ void fdt_cpu_fixup(void *fdt)
        }
 }
 
+static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
+{
+       int rc;
+       uint64_t reg_addr, reg_size;
+       struct sbi_domain *dom = sbi_domain_thishart_ptr();
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &reg_addr, &reg_size);
+       if (rc < 0 || !reg_addr || !reg_size)
+               return;

lib/utils/fdt/fdt_fixup.c void fdt_cpu_fixup(void *fdt) · line 73

Quoted 10 lines
lib/utils/fdt/fdt_fixup.c +10 -0
+
+       if (!sbi_domain_check_addr(dom, reg_addr, dom->next_mode,
+                                   SBI_DOMAIN_READ | SBI_DOMAIN_WRITE)) {
+               rc = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
+               if (rc < 0)
+                       return;
+               fdt_setprop_string(fdt, nodeoff, "status", "disabled");
+       }
+}
+

This should be in a separate patch as it is a generic function and doesn't match the commit text.

Quoted 26 lines
lib/utils/fdt/fdt_fixup.c +11 -3
+void fdt_imsic_fixup(void *fdt)
+{
+       int noff = 0;
+
+       while ((noff = fdt_node_offset_by_compatible(fdt, noff,
+                                                    "riscv,imsics")) >= 0)
+               fdt_domain_based_fixup_one(fdt, noff);
+}
+
 void fdt_plic_fixup(void *fdt)
 {
        u32 *cells;
@@ -261,10 +289,10 @@ int fdt_reserved_memory_nomap_fixup(void *fdt)
 
 void fdt_fixups(void *fdt)
 {
+       fdt_imsic_fixup(fdt);
+
        fdt_plic_fixup(fdt);
 
        fdt_reserved_memory_fixup(fdt);
        fdt_pmu_fixup(fdt);
 }
-
-
--
Quoted 2 lines

2.25.1

Otherwise, LGTM.

Signature · 2 lines
Regards,
Atish
#17 lore raw

PATCH 10/13 lib: utils: Disable appropriate IMSIC DT nodes in fdt_fixups()

Quoted 71 lines · Atish Patra
Quoted 69 lines · Anup Patel

We should disable IMSIC DT nodes in fdt_fixups() which are not accessible to the next booting stage based on currently assigned domain.

---
 include/sbi_utils/fdt/fdt_fixup.h | 17 ++++++++++++++--
 lib/utils/fdt/fdt_fixup.c         | 32 +++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)
include/sbi_utils/fdt/fdt_fixup.h +15 -2
index c38e5d9..77575bb 100644
@@ -21,6 +21,18 @@
  */
 void fdt_cpu_fixup(void *fdt);
 
+/**
+ * Fix up the IMSIC nodes in the device tree
+ *
+ * This routine disables IMSIC nodes which are not accessible to the next
+ * booting stage based on currently assigned domain.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_imsic_fixup(void *fdt);
+
 /**
  * Fix up the PLIC node in the device tree
  *
@@ -64,8 +76,9 @@ int fdt_reserved_memory_nomap_fixup(void *fdt);
  * General device tree fix-up
  *
  * This routine do all required device tree fix-ups for a typical platform.
- * It fixes up the PLIC node and the reserved memory node in the device tree
- * by calling the corresponding helper routines to accomplish the task.
+ * It fixes up the PLIC node, IMSIC nodes, and the reserved memory node in
+ * the device tree by calling the corresponding helper routines to accomplish
+ * the task.
  *
  * It is recommended that platform codes call this helper in their final_init()
  *
lib/utils/fdt/fdt_fixup.c +14 -0
index ac01ba3..c2460b8 100644
@@ -52,6 +52,34 @@ void fdt_cpu_fixup(void *fdt)
        }
 }
 
+static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
+{
+       int rc;
+       uint64_t reg_addr, reg_size;
+       struct sbi_domain *dom = sbi_domain_thishart_ptr();
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &reg_addr, &reg_size);
+       if (rc < 0 || !reg_addr || !reg_size)
+               return;
+
+       if (!sbi_domain_check_addr(dom, reg_addr, dom->next_mode,
+                                   SBI_DOMAIN_READ | SBI_DOMAIN_WRITE)) {
+               rc = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
+               if (rc < 0)
Quoted 8 lines

lib/utils/fdt/fdt_fixup.c void fdt_cpu_fixup(void *fdt) · line 73

Quoted 5 lines
lib/utils/fdt/fdt_fixup.c +5 -0
+                       return;
+               fdt_setprop_string(fdt, nodeoff, "status", "disabled");
+       }
+}
+

This should be in a separate patch as it is a generic function and doesn't match the commit text.

This is a static function only used here. If it were a public function then I would have moved it to a separate patch.

Regards,
Anup

Quoted 38 lines
Quoted 27 lines
lib/utils/fdt/fdt_fixup.c +11 -3
+void fdt_imsic_fixup(void *fdt)
+{
+       int noff = 0;
+
+       while ((noff = fdt_node_offset_by_compatible(fdt, noff,
+                                                    "riscv,imsics")) >= 0)
+               fdt_domain_based_fixup_one(fdt, noff);
+}
+
 void fdt_plic_fixup(void *fdt)
 {
        u32 *cells;
@@ -261,10 +289,10 @@ int fdt_reserved_memory_nomap_fixup(void *fdt)
 
 void fdt_fixups(void *fdt)
 {
+       fdt_imsic_fixup(fdt);
+
        fdt_plic_fixup(fdt);
 
        fdt_reserved_memory_fixup(fdt);
        fdt_pmu_fixup(fdt);
 }
-
-
--
Quoted 2 lines

2.25.1

Otherwise, LGTM.

--
Regards,
Atish

author
#18 lore raw

PATCH 11/13 lib: utils/irqchip: Add APLIC initialization library

In reply to Anup Patel

We add simple APLIC initialization library which is independent of hardware description format (FDT or ACPI). This APLIC initialization library can be used by custom OpenSBI platform support to setup APLIC domains.

---
 include/sbi_utils/irqchip/aplic.h |  47 +++++
 lib/utils/irqchip/aplic.c         | 279 ++++++++++++++++++++++++++++++
 lib/utils/irqchip/objects.mk      |   1 +
 3 files changed, 327 insertions(+)
 create mode 100644 include/sbi_utils/irqchip/aplic.h
 create mode 100644 lib/utils/irqchip/aplic.c
include/sbi_utils/irqchip/aplic.h new file +47 -0
new file mode 100644
index 0000000..82682e8
@@ -0,0 +1,47 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ * Copyright (c) 2022 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __IRQCHIP_APLIC_H__
+#define __IRQCHIP_APLIC_H__
+
+#include <sbi/sbi_types.h>
+
+#define APLIC_MAX_DELEGATE	16
+
+struct aplic_msicfg_data {
+	unsigned long lhxs;
+	unsigned long lhxw;
+	unsigned long hhxs;
+	unsigned long hhxw;
+	unsigned long base_addr;
+};
+
+struct aplic_delegate_data {
+	u32 first_irq;
+	u32 last_irq;
+	u32 child_index;
+};
+
+struct aplic_data {
+	unsigned long addr;
+	unsigned long size;
+	unsigned long num_idc;
+	unsigned long num_source;
+	bool targets_mmode;
+	bool has_msicfg_mmode;
+	struct aplic_msicfg_data msicfg_mmode;
+	bool has_msicfg_smode;
+	struct aplic_msicfg_data msicfg_smode;
+	struct aplic_delegate_data delegate[APLIC_MAX_DELEGATE];
+};
+
+int aplic_cold_irqchip_init(struct aplic_data *aplic);
+
+#endif
lib/utils/irqchip/aplic.c new file +279 -0

279 lines not on this page · raw message

lib/utils/irqchip/objects.mk +1 -0
index ae6f255..b2b3f79 100644
@@ -10,5 +10,6 @@
 libsbiutils-objs-y += irqchip/fdt_irqchip.o
 libsbiutils-objs-y += irqchip/fdt_irqchip_imsic.o
 libsbiutils-objs-y += irqchip/fdt_irqchip_plic.o
+libsbiutils-objs-y += irqchip/aplic.o
 libsbiutils-objs-y += irqchip/imsic.o
 libsbiutils-objs-y += irqchip/plic.o
Signature · 1 line
2.25.1
author
#19 lore raw

PATCH 12/13 lib: utils/irqchip: Add FDT based driver for APLIC

In reply to Anup Patel

We add simple FDT irqchip driver for APLIC so that generic platform (and other FDT based platforms) can utilize common APLIC initialization library.

---
 include/sbi_utils/fdt/fdt_helper.h    |   4 +
 lib/utils/fdt/fdt_helper.c            | 159 ++++++++++++++++++++++++++
 lib/utils/irqchip/fdt_irqchip.c       |   2 +
 lib/utils/irqchip/fdt_irqchip_aplic.c |  56 +++++++++
 lib/utils/irqchip/objects.mk          |   1 +
 5 files changed, 222 insertions(+)
 create mode 100644 lib/utils/irqchip/fdt_irqchip_aplic.c
include/sbi_utils/fdt/fdt_helper.h +4 -0
index 4c8d29e..1232b26 100644
@@ -68,6 +68,10 @@ int fdt_parse_uart8250_node(void *fdt, int nodeoffset,
 int fdt_parse_uart8250(void *fdt, struct platform_uart_data *uart,
 		       const char *compatible);
 
+struct aplic_data;
+
+int fdt_parse_aplic_node(void *fdt, int nodeoff, struct aplic_data *aplic);
+
 struct imsic_data;
 
 bool fdt_check_imsic_mlevel(void *fdt);
lib/utils/fdt/fdt_helper.c +159 -0

171 lines not on this page · raw message

lib/utils/irqchip/fdt_irqchip.c +2 -0
index cf64a2e..6007755 100644
@@ -12,10 +12,12 @@
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/irqchip/fdt_irqchip.h>
 
+extern struct fdt_irqchip fdt_irqchip_aplic;
 extern struct fdt_irqchip fdt_irqchip_imsic;
 extern struct fdt_irqchip fdt_irqchip_plic;
 
 static struct fdt_irqchip *irqchip_drivers[] = {
+	&fdt_irqchip_aplic,
 	&fdt_irqchip_imsic,
 	&fdt_irqchip_plic
 };
lib/utils/irqchip/fdt_irqchip_aplic.c new file +56 -0

56 lines not on this page · raw message

lib/utils/irqchip/objects.mk +1 -0
index b2b3f79..fad4344 100644
@@ -8,6 +8,7 @@
 #
 
 libsbiutils-objs-y += irqchip/fdt_irqchip.o
+libsbiutils-objs-y += irqchip/fdt_irqchip_aplic.o
 libsbiutils-objs-y += irqchip/fdt_irqchip_imsic.o
 libsbiutils-objs-y += irqchip/fdt_irqchip_plic.o
 libsbiutils-objs-y += irqchip/aplic.o
Signature · 1 line
2.25.1
author
#20 lore raw

PATCH 13/13 lib: utils: Disable appropriate APLIC DT nodes in fdt_fixups()

In reply to Anup Patel

We should disable APLIC DT nodes in fdt_fixups() which are not accessible to the next booting stage based on currently assigned domain.

---
 include/sbi_utils/fdt/fdt_fixup.h | 18 +++++++++++++++---
 lib/utils/fdt/fdt_fixup.c         | 11 +++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
include/sbi_utils/fdt/fdt_fixup.h +15 -3
index 77575bb..fb076ba 100644
@@ -21,6 +21,18 @@
  */
 void fdt_cpu_fixup(void *fdt);
 
+/**
+ * Fix up the APLIC nodes in the device tree
+ *
+ * This routine disables APLIC nodes which are not accessible to the next
+ * booting stage based on currently assigned domain.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_aplic_fixup(void *fdt);
+
 /**
  * Fix up the IMSIC nodes in the device tree
  *
@@ -76,9 +88,9 @@ int fdt_reserved_memory_nomap_fixup(void *fdt);
  * General device tree fix-up
  *
  * This routine do all required device tree fix-ups for a typical platform.
- * It fixes up the PLIC node, IMSIC nodes, and the reserved memory node in
- * the device tree by calling the corresponding helper routines to accomplish
- * the task.
+ * It fixes up the PLIC node, IMSIC nodes, APLIC nodes, and the reserved
+ * memory node in the device tree by calling the corresponding helper
+ * routines to accomplish the task.
  *
  * It is recommended that platform codes call this helper in their final_init()
  *
lib/utils/fdt/fdt_fixup.c +11 -0
index c2460b8..a80bd82 100644
@@ -71,6 +71,15 @@ static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
 	}
 }
 
+void fdt_aplic_fixup(void *fdt)
+{
+	int noff = 0;
+
+	while ((noff = fdt_node_offset_by_compatible(fdt, noff,
+						     "riscv,aplic")) >= 0)
+		fdt_domain_based_fixup_one(fdt, noff);
+}
+
 void fdt_imsic_fixup(void *fdt)
 {
 	int noff = 0;
@@ -289,6 +298,8 @@ int fdt_reserved_memory_nomap_fixup(void *fdt)
 
 void fdt_fixups(void *fdt)
 {
+	fdt_aplic_fixup(fdt);
+
 	fdt_imsic_fixup(fdt);
 
 	fdt_plic_fixup(fdt);
Signature · 1 line
2.25.1
#21 lore raw

PATCH 13/13 lib: utils: Disable appropriate APLIC DT nodes in fdt_fixups()

Quoted 78 lines · Anup Patel

We should disable APLIC DT nodes in fdt_fixups() which are not accessible to the next booting stage based on currently assigned domain.

---
 include/sbi_utils/fdt/fdt_fixup.h | 18 +++++++++++++++---
 lib/utils/fdt/fdt_fixup.c         | 11 +++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
include/sbi_utils/fdt/fdt_fixup.h +15 -3
index 77575bb..fb076ba 100644
@@ -21,6 +21,18 @@
  */
 void fdt_cpu_fixup(void *fdt);
 
+/**
+ * Fix up the APLIC nodes in the device tree
+ *
+ * This routine disables APLIC nodes which are not accessible to the next
+ * booting stage based on currently assigned domain.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_aplic_fixup(void *fdt);
+
 /**
  * Fix up the IMSIC nodes in the device tree
  *
@@ -76,9 +88,9 @@ int fdt_reserved_memory_nomap_fixup(void *fdt);
  * General device tree fix-up
  *
  * This routine do all required device tree fix-ups for a typical platform.
- * It fixes up the PLIC node, IMSIC nodes, and the reserved memory node in
- * the device tree by calling the corresponding helper routines to accomplish
- * the task.
+ * It fixes up the PLIC node, IMSIC nodes, APLIC nodes, and the reserved
+ * memory node in the device tree by calling the corresponding helper
+ * routines to accomplish the task.
  *
  * It is recommended that platform codes call this helper in their final_init()
  *
lib/utils/fdt/fdt_fixup.c +11 -1
index c2460b8..a80bd82 100644
@@ -71,6 +71,15 @@ static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
        }
 }
 
+void fdt_aplic_fixup(void *fdt)
+{
+       int noff = 0;
+
+       while ((noff = fdt_node_offset_by_compatible(fdt, noff,
+                                                    "riscv,aplic")) >= 0)
+               fdt_domain_based_fixup_one(fdt, noff);
+}
+
 void fdt_imsic_fixup(void *fdt)
 {
        int noff = 0;
@@ -289,6 +298,8 @@ int fdt_reserved_memory_nomap_fixup(void *fdt)
 
 void fdt_fixups(void *fdt)
 {
+       fdt_aplic_fixup(fdt);
+
        fdt_imsic_fixup(fdt);
 
        fdt_plic_fixup(fdt);
--
Quoted 2 lines

2.25.1

Signature · 2 lines
Regards,
Atish
#22 lore raw

PATCH 3/13 lib: sbi: Use AIA CSRs for local interrupts when available

In reply to Anup Patel

? 2022-01-04???? 15:43 +0530?Anup Patel???

Quoted 5 lines

We should use AIA CSRs to process local interrupts whenever AIA is available.

The interrupts triggered by AIA should all be external interrupts. For platforms that support AIA, mcause should only receive external interrupts, and then handle timers and software interrupts in external interrupts. We shouldn't need two versions of sbi_trap_irq

Regards,
Xiang W

Quoted 87 lines

---
?lib/sbi/sbi_trap.c | 57 +++++++++++++++++++++++++++++++++++++-------- -
?1 file changed, 46 insertions(+), 11 deletions(-)

lib/sbi/sbi_trap.c +0 -0
index 8d20e04..bc8961f 100644
@@ -195,6 +195,44 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 ????????return 0;
 ?}

?
+static int sbi_trap_nonaia_irq(struct sbi_trap_regs *regs, ulong mcause)

lib/sbi/sbi_trap.c +37 -0
+{
+???????mcause &= ~(1UL << (__riscv_xlen - 1));
+???????switch (mcause) {
+???????case IRQ_M_TIMER:
+???????????????sbi_timer_process();
+???????????????break;
+???????case IRQ_M_SOFT:
+???????????????sbi_ipi_process();
+???????????????break;
+???????default:
+???????????????return SBI_ENOENT;
+???????};
+
+???????return 0;
+}
+
+static int sbi_trap_aia_irq(struct sbi_trap_regs *regs, ulong mcause)
+{
+???????unsigned long mtopi;
+
+???????while ((mtopi = csr_read(CSR_MTOPI))) {
+???????????????mtopi = mtopi >> TOPI_IID_SHIFT;
+???????????????switch (mtopi) {
+???????????????case IRQ_M_TIMER:
+???????????????????????sbi_timer_process();
+???????????????????????break;
+???????????????case IRQ_M_SOFT:
+???????????????????????sbi_ipi_process();
+???????????????????????break;
+???????????????default:
+???????????????????????return SBI_ENOENT;
+???????????????}
+???????}
+
+???????return 0;
+}
+

?/**
? * Handle trap/interrupt
? *

lib/sbi/sbi_trap.c +0 -0
@@ -225,18 +263,15 @@ struct sbi_trap_regs *sbi_trap_handler(struct
 sbi_trap_regs *regs)
 ????????}

?
????????if (mcause & (1UL << (__riscv_xlen - 1))) { -???????????????mcause &= ~(1UL << (__riscv_xlen - 1)); -???????????????switch (mcause) {
-???????????????case IRQ_M_TIMER:
-???????????????????????sbi_timer_process(); -???????????????????????break;
-???????????????case IRQ_M_SOFT:
-???????????????????????sbi_ipi_process(); -???????????????????????break;
-???????????????default:
-???????????????????????msg = "unhandled external interrupt"; +???????????????if (sbi_hart_has_feature(sbi_scratch_thishart_ptr(), +??????????????????????????????????????? SBI_HART_HAS_AIA)) +???????????????????????rc = sbi_trap_aia_irq(regs, mcause); +???????????????else
+???????????????????????rc = sbi_trap_nonaia_irq(regs, mcause); +???????????????if (rc) {
+???????????????????????msg = "unhandled local interrupt"; ????????????????????????goto trap_error; -???????????????};
+???????????????}
????????????????return regs;
????????}
?

Signature · 1 line
2.25.1
#23 lore raw

PATCH 3/13 lib: sbi: Use AIA CSRs for local interrupts when available

Quoted 11 lines · Xiang W

? 2022-01-04???? 15:43 +0530?Anup Patel???

Quoted 5 lines

We should use AIA CSRs to process local interrupts whenever AIA is available.

The interrupts triggered by AIA should all be external interrupts. For platforms that support AIA, mcause should only receive external interrupts, and then handle timers and software interrupts in external interrupts. We shouldn't need two versions of sbi_trap_irq

The way external interrupts are to be handled using AIA local interrupt CSRs is different from traditional (i.e. non-AIA) way of handling external interrupt. With AIA local interrupt CSRs, we can handle multiple local interrupts (using MTOPI) in the same interrupt handling context whereas without AIA we can only handle one local interrupt (using MCAUSE) in the same interrupt handle context.

Also with AIA IMSIC, the IPIs can be handles via IMSIC as external interrupts and platforms don't need to provide special M-mode IPI device when AIA IMSIC is present.

The AIA IMSIC does not provide any alternative way for timer interrupts so these have to be handled as local interrupts.

Regards,
Anup

Quoted 92 lines

Regards,
Xiang W

Quoted 87 lines
---
 lib/sbi/sbi_trap.c | 57 +++++++++++++++++++++++++++++++++++++--------
(quoted diff) +0 -1
-
 1 file changed, 46 insertions(+), 11 deletions(-)
lib/sbi/sbi_trap.c +38 -0
index 8d20e04..bc8961f 100644
@@ -195,6 +195,44 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
        return 0;
 }
 
+static int sbi_trap_nonaia_irq(struct sbi_trap_regs *regs, ulong
 mcause)
+{
+       mcause &= ~(1UL << (__riscv_xlen - 1));
+       switch (mcause) {
+       case IRQ_M_TIMER:
+               sbi_timer_process();
+               break;
+       case IRQ_M_SOFT:
+               sbi_ipi_process();
+               break;
+       default:
+               return SBI_ENOENT;
+       };
+
+       return 0;
+}
+
+static int sbi_trap_aia_irq(struct sbi_trap_regs *regs, ulong mcause)
+{
+       unsigned long mtopi;
+
+       while ((mtopi = csr_read(CSR_MTOPI))) {
+               mtopi = mtopi >> TOPI_IID_SHIFT;
+               switch (mtopi) {
+               case IRQ_M_TIMER:
+                       sbi_timer_process();
+                       break;
+               case IRQ_M_SOFT:
+                       sbi_ipi_process();
+                       break;
+               default:
+                       return SBI_ENOENT;
+               }
+       }
+
+       return 0;
+}
+
 /**
  * Handle trap/interrupt

*

lib/sbi/sbi_trap.c +8 -12
@@ -225,18 +263,15 @@ struct sbi_trap_regs *sbi_trap_handler(struct
 sbi_trap_regs *regs)
        }
 
        if (mcause & (1UL << (__riscv_xlen - 1))) {
-               mcause &= ~(1UL << (__riscv_xlen - 1));
-               switch (mcause) {
-               case IRQ_M_TIMER:
-                       sbi_timer_process();
-                       break;
-               case IRQ_M_SOFT:
-                       sbi_ipi_process();
-                       break;
-               default:
-                       msg = "unhandled external interrupt";
+               if (sbi_hart_has_feature(sbi_scratch_thishart_ptr(),
+                                        SBI_HART_HAS_AIA))
+                       rc = sbi_trap_aia_irq(regs, mcause);
+               else
+                       rc = sbi_trap_nonaia_irq(regs, mcause);
+               if (rc) {
+                       msg = "unhandled local interrupt";
                        goto trap_error;
-               };
+               }
                return regs;
        }
 
--

2.25.1