nixos-riscv/patches/opensbi-test-plic.patch

201 lines
5.8 KiB
Diff
Raw Permalink Normal View History

2024-08-21 09:22:43 +02:00
diff --git a/lib/sbi/sbi_irqchip.c b/lib/sbi/sbi_irqchip.c
2024-08-29 17:14:04 +02:00
index 0ae604a..e34e90c 100644
2024-08-21 09:22:43 +02:00
--- a/lib/sbi/sbi_irqchip.c
+++ b/lib/sbi/sbi_irqchip.c
@@ -9,6 +9,9 @@
#include <sbi/sbi_irqchip.h>
#include <sbi/sbi_platform.h>
+#include <sbi/sbi_console.h>
+
+static void do_plic_test(void);
static int default_irqfn(void)
{
@@ -37,8 +40,10 @@ int sbi_irqchip_init(struct sbi_scratch *scratch, bool cold_boot)
if (rc)
return rc;
- if (ext_irqfn != default_irqfn)
- csr_set(CSR_MIE, MIP_MEIP);
+ //csr_set(CSR_MIE, MIP_SEIP);
+ //csr_set(CSR_MSTATUS, MSTATUS_SIE);
+
+ do_plic_test();
return 0;
}
2024-08-29 17:14:04 +02:00
@@ -47,8 +52,170 @@ void sbi_irqchip_exit(struct sbi_scratch *scratch)
2024-08-21 09:22:43 +02:00
{
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
- if (ext_irqfn != default_irqfn)
- csr_clear(CSR_MIE, MIP_MEIP);
+ //csr_clear(CSR_MIE, MIP_SEIP);
+ //csr_clear(CSR_MSTATUS, MSTATUS_SIE);
sbi_platform_irqchip_exit(plat);
}
+
+
+/* ----------------- PLIC tests ---------------- */
+
+
+#define MIE_MEIE (1UL << 11) // Machine External Interrupt Enable
+#define SIE_SEIE (1UL << 9)
+#define MIDELEG_SEIE (1UL << 9) // Delegate Machine External Interrupt to Supervisor
+#define PLIC_TIMER_PORT 4
+// Base address of PLIC
2024-08-29 17:14:04 +02:00
+#define PLIC_BASE 0x40800000UL
2024-08-21 09:22:43 +02:00
+#define PLIC_PRIORITY_OFFSET 0x0UL
+#define PLIC_PENDING_OFFSET 0x1000UL
2024-08-29 17:14:04 +02:00
+#define PLIC_ENABLE_OFFSET 0x2080UL
+#define PLIC_THRESHOLD_OFFSET 0x201000UL
+#define PLIC_CLAIM_OFFSET 0x201004UL
2024-08-21 09:22:43 +02:00
+
+// Aux timer
+#define AUX_TIMER_BASE 0x40010000UL
+#define MTIMECMP_OFFSET 0x4000UL
+#define MTIME_OFFSET 0xBFF8UL
+
+#define MSTATUS_MPP_MASK (3 << 11)
+#define MSTATUS_MPP_SUPERVISOR (1 << 11)
+
2024-08-21 12:28:17 +02:00
+static volatile unsigned long *mtime = (unsigned long *)(AUX_TIMER_BASE + MTIME_OFFSET);
+static volatile unsigned long *mtimecmp = (unsigned long *)(AUX_TIMER_BASE + MTIMECMP_OFFSET);
+
+
2024-08-21 11:42:12 +02:00
+static void dumpregs(int machine)
2024-08-21 09:44:53 +02:00
+{
+ char *prefix = "\t";
+ char *suffix = "\t";
+ sbi_printf("Registers:\n");
2024-08-21 11:42:12 +02:00
+ if (machine) {
+ sbi_printf("%sMIE%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_MIE));
+ sbi_printf("%sMIP%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_MIP));
+ sbi_printf("%sMSTATUS%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_MSTATUS));
+ sbi_printf("%sMIDELEG%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_MIDELEG));
+ }
2024-08-21 09:44:53 +02:00
+ sbi_printf("%sSIE%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_SIE));
+ sbi_printf("%sSIP%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_SIP));
+ sbi_printf("%sSSTATUS%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_SSTATUS));
2024-08-29 17:14:04 +02:00
+ sbi_printf("%sSTVEC%s: 0x%" PRILX "\n",
+ prefix, suffix, csr_read(CSR_STVEC));
2024-08-21 09:44:53 +02:00
+}
+
2024-08-21 09:22:43 +02:00
+static void __attribute__((optimize("O0"))) switch_to_supervisor_mode(int (*target_address)(void))
+{
+ unsigned long mstatus;
+
+ // Read the current mstatus
+ asm volatile("csrr %0, mstatus" : "=r"(mstatus));
+
+ // Set the MPP field to supervisor mode
+ mstatus = (mstatus & ~MSTATUS_MPP_MASK) | MSTATUS_MPP_SUPERVISOR;
+
+ // Write back the modified mstatus
+ asm volatile("csrw mstatus, %0" : : "r"(mstatus));
+
+ // Set the mepc to the target address
+ asm volatile("csrw mepc, %0" : : "r"(target_address));
+
+ // Use mret to return to the specified address in supervisor mode
+ asm volatile("mret");
+}
+
+static int supervisor_mode_code(void)
+{
2024-08-21 09:44:53 +02:00
+ sbi_printf("Hello from supervisor\n");
2024-08-21 11:42:12 +02:00
+ dumpregs(0);
2024-08-21 12:28:17 +02:00
+
+ /* Enable timer interrupt */
+ *mtimecmp = *mtime + 10000;
+
+ sbi_printf("Timer alarm programmed\n");
2024-08-21 09:44:53 +02:00
+ sbi_printf("Waiting for interrupt...\n");
2024-08-21 09:31:09 +02:00
+ int i = 0;
+ char *s = "-\\|/";
2024-08-21 09:22:43 +02:00
+ while (1) {
2024-08-21 09:44:53 +02:00
+ for (volatile unsigned long j = 0; j < 100000; j++);
2024-08-21 09:31:09 +02:00
+ sbi_printf("\r%c", s[i++]);
+ if (i >= 4)
+ i = 0;
2024-08-21 09:22:43 +02:00
+ }
+ return 0;
+}
+
2024-08-29 17:14:04 +02:00
+static void __attribute__((aligned(4))) __attribute__((interrupt ("supervisor"))) supervisor_trap_entry(void)
2024-08-21 10:03:19 +02:00
+{
2024-08-29 17:14:04 +02:00
+ sbi_printf("\nSupervisor Trap Entry Reached!\n");
+ sbi_printf("\nTEST-RESULT-OK\n");
+ while (1) {
+ }
2024-08-21 10:03:19 +02:00
+}
2024-08-21 09:22:43 +02:00
+
+static void do_plic_test(void)
+{
+ sbi_printf("--- TESTING PLIC ---\n");
+
+ /* Disable auxiliar timer interrupt */
+ *mtimecmp = 0xffffffffUL;
+ sbi_printf("Timer interrupt disabled\n");
+
+
+ /* Enable supervisor interrupt delegation */
2024-08-29 17:14:04 +02:00
+
2024-08-21 09:22:43 +02:00
+ csr_set(CSR_SIE, SIE_SEIE); // Enable supervisor external interrupts
+ csr_set(CSR_SSTATUS, SSTATUS_SIE); // Enable global interrupts in supervisor mode
+ csr_set(CSR_MIDELEG, MIDELEG_SEIE); // Delegate machine interrupts to supervisor mode
2024-08-29 17:14:04 +02:00
+ csr_write(CSR_STVEC, &supervisor_trap_entry);
+
2024-08-21 09:22:43 +02:00
+ sbi_printf("Enabled supervisor delegation:\n");
+
2024-08-21 11:42:12 +02:00
+ dumpregs(1);
2024-08-21 09:22:43 +02:00
+
+ /* Configure PLIC aux timer input */
+ volatile unsigned *plic_priority = (unsigned *)(PLIC_BASE + PLIC_PRIORITY_OFFSET + PLIC_TIMER_PORT * 4);
+ volatile unsigned *plic_enable = (unsigned *)(PLIC_BASE + PLIC_ENABLE_OFFSET);
+ volatile unsigned *plic_threshold = (unsigned *)(PLIC_BASE + PLIC_THRESHOLD_OFFSET);
2024-08-21 11:29:12 +02:00
+ volatile unsigned *plic_claim = (unsigned *)(PLIC_BASE + PLIC_CLAIM_OFFSET);
+ volatile unsigned *plic_pending = (unsigned *)(PLIC_BASE + PLIC_PENDING_OFFSET);
2024-08-21 09:22:43 +02:00
+
2024-08-21 11:29:12 +02:00
+ sbi_printf("Enabling timer in PLIC\n");
2024-08-21 09:22:43 +02:00
+ *plic_priority = PLIC_TIMER_PORT;
+ *plic_threshold = PLIC_TIMER_PORT - 1;
+ *plic_enable |= (1 << PLIC_TIMER_PORT);
+
2024-08-21 11:29:12 +02:00
+ /* Clear interrupt */
+ sbi_printf("Pending: %d\n", *plic_pending);
+ unsigned claim = *plic_claim;
+ sbi_printf("Claim: %d\n", claim);
+ *plic_claim = claim;
+ sbi_printf("Pending: %d\n", *plic_pending);
2024-08-21 09:22:43 +02:00
+
2024-08-21 11:29:12 +02:00
+ sbi_printf("Clearing MIP\n");
2024-08-21 10:09:06 +02:00
+ csr_write(CSR_MIP, 0);
+
2024-08-21 10:20:11 +02:00
+ /* Enable external timer interrupts */
+ //sbi_printf("Enabling MEIE in MIE register\n");
+ //csr_set(CSR_MIE, MIE_MEIE); /* Needed? */
+ //sbi_printf("Enabling MIE in MSTATUS register\n");
+ //csr_set(CSR_MSTATUS, MSTATUS_MIE); /* Needed? */
2024-08-21 10:20:11 +02:00
+
2024-08-21 09:22:43 +02:00
+ sbi_printf("Switching to supervisor\n");
+
2024-08-21 11:42:12 +02:00
+ dumpregs(1);
2024-08-21 09:44:53 +02:00
+
2024-08-21 09:22:43 +02:00
+ switch_to_supervisor_mode(&supervisor_mode_code);
+
+ /* Never reached */
2024-08-21 11:29:12 +02:00
+ while (1);
2024-08-21 09:22:43 +02:00
+}
+
+