The branch main has been updated by br:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=7ac65902d8ba0a85e5fe95d097f7fbd52cbda12a

commit 7ac65902d8ba0a85e5fe95d097f7fbd52cbda12a
Author:     Ruslan Bukin <b...@freebsd.org>
AuthorDate: 2025-02-03 11:44:15 +0000
Commit:     Ruslan Bukin <b...@freebsd.org>
CommitDate: 2025-02-03 11:44:18 +0000

    bhyve/riscv: fix interrupts-extended property.
    
    Construct interrupts-extended (property of APLIC) properly.
    
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D48713
---
 usr.sbin/bhyve/riscv/bhyverun_machdep.c |  2 +-
 usr.sbin/bhyve/riscv/fdt.c              | 27 ++++++++++++++++++---------
 usr.sbin/bhyve/riscv/fdt.h              |  2 +-
 3 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/usr.sbin/bhyve/riscv/bhyverun_machdep.c 
b/usr.sbin/bhyve/riscv/bhyverun_machdep.c
index d06b517a6624..8ee505e86679 100644
--- a/usr.sbin/bhyve/riscv/bhyverun_machdep.c
+++ b/usr.sbin/bhyve/riscv/bhyverun_machdep.c
@@ -337,7 +337,7 @@ bhyve_init_platform(struct vmctx *ctx, struct vcpu *bsp)
        error = vm_set_register(bsp, VM_REG_GUEST_A1, fdt_gpa);
        assert(error == 0);
 
-       fdt_add_aplic(APLIC_MEM_BASE, APLIC_MEM_SIZE);
+       fdt_add_aplic(APLIC_MEM_BASE, APLIC_MEM_SIZE, guest_ncpus);
        error = vm_attach_aplic(ctx, APLIC_MEM_BASE, APLIC_MEM_SIZE);
        if (error != 0) {
                warn("vm_attach_aplic()");
diff --git a/usr.sbin/bhyve/riscv/fdt.c b/usr.sbin/bhyve/riscv/fdt.c
index bf5ec114cda0..3ccd7ae5dc79 100644
--- a/usr.sbin/bhyve/riscv/fdt.c
+++ b/usr.sbin/bhyve/riscv/fdt.c
@@ -2,7 +2,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2022 The FreeBSD Foundation
- * Copyright (c) 2024 Ruslan Bukin <b...@bsdpad.com>
+ * Copyright (c) 2024-2025 Ruslan Bukin <b...@bsdpad.com>
  *
  * This software was developed by Andrew Turner under sponsorship from
  * the FreeBSD Foundation.
@@ -44,6 +44,8 @@
 #include <libfdt.h>
 #include <vmmapi.h>
 
+#include <machine/intr.h>
+
 #include "config.h"
 #include "bhyverun.h"
 #include "fdt.h"
@@ -58,7 +60,7 @@
 
 static void *fdtroot;
 static uint32_t aplic_phandle = 0;
-static uint32_t intc0_phandle = 0;
+static uint32_t *intc_phandles = NULL;
 
 static uint32_t
 assign_phandle(void *fdt)
@@ -84,7 +86,7 @@ set_single_reg(void *fdt, uint64_t start, uint64_t len)
 }
 
 static void
-add_cpu(void *fdt, int cpuid, const char *isa)
+add_cpu(void *fdt, int cpuid, const char *isa, uint32_t *intc_phandle)
 {
        char node_name[16];
 
@@ -98,7 +100,7 @@ add_cpu(void *fdt, int cpuid, const char *isa)
        fdt_property_string(fdt, "mmu-type", "riscv,sv39");
 
        fdt_begin_node(fdt, "interrupt-controller");
-       intc0_phandle = assign_phandle(fdt);
+       *intc_phandle = assign_phandle(fdt);
        fdt_property_u32(fdt, "#address-cells", 2);
        fdt_property_u32(fdt, "#interrupt-cells", 1);
        fdt_property(fdt, "interrupt-controller", NULL, 0);
@@ -120,8 +122,9 @@ add_cpus(void *fdt, int ncpu, const char *isa)
        /* TODO: take timebase from kernel? */
        fdt_property_u32(fdt, "timebase-frequency", 1000000);
 
+       intc_phandles = malloc(sizeof(uint32_t) * ncpu);
        for (cpuid = 0; cpuid < ncpu; cpuid++)
-               add_cpu(fdt, cpuid, isa);
+               add_cpu(fdt, cpuid, isa, &intc_phandles[cpuid]);
 
        fdt_end_node(fdt);
 }
@@ -172,10 +175,11 @@ fdt_init(struct vmctx *ctx, int ncpu, vm_paddr_t fdtaddr, 
vm_size_t fdtsize,
 }
 
 void
-fdt_add_aplic(uint64_t mem_base, uint64_t mem_size)
+fdt_add_aplic(uint64_t mem_base, uint64_t mem_size, int ncpu)
 {
        char node_name[32];
        void *fdt, *prop;
+       int i;
 
        fdt = fdtroot;
 
@@ -196,10 +200,13 @@ fdt_add_aplic(uint64_t mem_base, uint64_t mem_size)
        SET_PROP_U64(prop, 0, mem_base);
        SET_PROP_U64(prop, 1, mem_size);
 
+       assert(intc_phandles != NULL);
        fdt_property_placeholder(fdt, "interrupts-extended",
-           2 * sizeof(uint32_t), &prop);
-       SET_PROP_U32(prop, 0, intc0_phandle);
-       SET_PROP_U32(prop, 1, 9);
+           2 * ncpu * sizeof(uint32_t), &prop);
+       for (i = 0; i < ncpu; i++) {
+               SET_PROP_U32(prop, i * 2 + 0, intc_phandles[i]);
+               SET_PROP_U32(prop, i * 2 + 1, IRQ_EXTERNAL_SUPERVISOR);
+       }
        fdt_property_u32(fdt, "riscv,num-sources", 63);
 
        fdt_end_node(fdt);
@@ -325,4 +332,6 @@ fdt_finalize(void)
        fdt_end_node(fdtroot);
 
        fdt_finish(fdtroot);
+
+       free(intc_phandles);
 }
diff --git a/usr.sbin/bhyve/riscv/fdt.h b/usr.sbin/bhyve/riscv/fdt.h
index 60140a82a211..21453e363c3c 100644
--- a/usr.sbin/bhyve/riscv/fdt.h
+++ b/usr.sbin/bhyve/riscv/fdt.h
@@ -37,7 +37,7 @@ struct vmctx;
 
 int    fdt_init(struct vmctx *ctx, int ncpu, vm_paddr_t addrp,
            vm_size_t size, const char *isa);
-void   fdt_add_aplic(uint64_t dist_base, uint64_t dist_size);
+void   fdt_add_aplic(uint64_t dist_base, uint64_t dist_size, int ncpu);
 void   fdt_add_pcie(int intrs[static 4]);
 void   fdt_add_uart(uint64_t uart_base, uint64_t uart_size, int intr);
 void   fdt_finalize(void);

Reply via email to