All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
@ 2026-11-16 15:29 Baptiste Le Duc
  2026-11-16 15:36 ` Jan Beulich
  2026-11-16 16:32 ` Oleksii Kurochko
  0 siblings, 2 replies; 7+ messages in thread
From: Baptiste Le Duc @ 2026-11-16 15:29 UTC (permalink / raw)
  To: xen-devel
  Cc: Alistair Francis, Connor Davis, Oleksii Kurochko, Andrew Cooper,
	Anthony PERARD, Michal Orzel, Jan Beulich, Julien Grall,
	Roger Pau Monné, Stefano Stabellini, Baptiste Le Duc

imsic_init() requires every MMIO register set to be exactly
IMSIC_HART_SIZE(guest_index_bits) * nr_parent_irqs bytes, i.e. to cover
the interrupt files of all harts listed in interrupts-extended. This only
holds when the DT describes a single register set.

When harts are split into groups (riscv,group-index-bits > 0), each group
has its own register set, which only covers the harts of that group. For
example, the SpacemiT V100 IMSIC node describes two 2 MiB register sets,
each holding 64 hart slots of 32 KiB, with 128 parent interrupts in total.
Xen expects 4 MiB per register set and panics with:

  IMSIC MMIO size is incorrect 2097152, expected MMIO size: 4194304

Check instead that a register set does not run past the end of the group it
starts in, i.e. BIT(hart_index_bits) hart slots. A register set may be
smaller than that, e.g. when a group holds a non-power-of-two number of
harts.

Parent interrupt i gets the i-th hart slot of the register sets, taken in
order. A slot with no hart behind it can be:
- at the very end of the last register set, e.g. when a DT describes a
  group by its full capacity: harmless, no interrupt is left to take it.
- anywhere else: every later interrupt is shifted one slot early, onto the
  previous hart's interrupt file.

Both show up as more slots than parent interrupts, so counting can't tell
them apart. Refusing to boot would reject the valid DTs of the first case,
so only warn: they keep booting, and broken ones get a hint as to why their
interrupts are misrouted.

While at it, print the MMIO sizes in hex to match the DT reg property.

Fixes: c9bd8b322ecb ("xen/riscv: imsic_init() implementation")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Baptiste Le Duc <baptiste.le-duc@vates.tech>
---
Changes in v2:
- Bound a register set by the end of the group it starts in (offset +
  size), not only by the group size.
- Warn when the register sets hold more hart slots than parent
  interrupts, as an unused slot before the end of the last register set
  misroutes every following hart.
- Reuse the group size for the base address mask.
- Link to v1: https://patch.msgid.link/20261113-fix-imsic-mmio-dt-v1-1-10efb6c28de4@vates.tech
---
 xen/arch/riscv/imsic.c | 39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/xen/arch/riscv/imsic.c b/xen/arch/riscv/imsic.c
index 44b8640ea3..48acc02440 100644
--- a/xen/arch/riscv/imsic.c
+++ b/xen/arch/riscv/imsic.c
@@ -381,7 +381,7 @@ void cf_check vcpu_imsic_deinit(struct vcpu *v)
 int __init imsic_init(const struct dt_device_node *node)
 {
     int rc;
-    unsigned long reloff, hartid;
+    unsigned long reloff, hartid, nr_slots = 0;
     unsigned int nr_parent_irqs, index, nr_handlers = 0;
     paddr_t base_addr;
     unsigned int nr_mmios;
@@ -415,9 +415,11 @@ int __init imsic_init(const struct dt_device_node *node)
     /* Check MMIO register sets */
     for ( unsigned int i = 0; i < nr_mmios; i++ )
     {
-        unsigned int guest_bits = imsic_cfg.guest_index_bits;
-        unsigned long expected_mmio_size =
-            IMSIC_HART_SIZE(guest_bits) * nr_parent_irqs;
+        /* Address space of one group: BIT(hart_index_bits) hart slots. */
+        unsigned long group_size = BIT(imsic_cfg.guest_index_bits +
+                                       imsic_cfg.hart_index_bits +
+                                       IMSIC_MMIO_PAGE_SHIFT, UL);
+        unsigned long offset;
 
         rc = dt_device_get_address(node, i, &mmios[i].base_addr,
                                    &mmios[i].size);
@@ -429,9 +431,7 @@ int __init imsic_init(const struct dt_device_node *node)
         }
 
         base_addr = mmios[i].base_addr;
-        base_addr &= ~(BIT(guest_bits +
-                           imsic_cfg.hart_index_bits +
-                           IMSIC_MMIO_PAGE_SHIFT, UL) - 1);
+        base_addr &= ~(group_size - 1);
         base_addr &= ~((BIT(imsic_cfg.group_index_bits, UL) - 1) <<
                        imsic_cfg.group_index_shift);
         if ( base_addr != imsic_cfg.base_addr )
@@ -442,15 +442,34 @@ int __init imsic_init(const struct dt_device_node *node)
             goto imsic_init_err;
         }
 
-        if ( mmios[i].size != expected_mmio_size )
+        /* A register set must not run past the end of its group. */
+        offset = mmios[i].base_addr & (group_size - 1);
+        if ( mmios[i].size + offset > group_size )
         {
             rc = -EINVAL;
-            printk(XENLOG_ERR "%s: IMSIC MMIO size is incorrect %ld, expected MMIO size: %ld\n",
-                   node->name, mmios[i].size, expected_mmio_size);
+            printk(XENLOG_ERR
+                   "%s: IMSIC MMIO regset %u (offset %#lx + size %#lx) exceeds group size %#lx\n",
+                   node->name, i, offset, mmios[i].size, group_size);
             goto imsic_init_err;
         }
+
+        /* The last slot may be cut short, see the lookup below. */
+        nr_slots += DIV_ROUND_UP(mmios[i].size,
+                                 IMSIC_HART_SIZE(imsic_cfg.guest_index_bits));
     }
 
+    /*
+     * Parent irq i is given hart slot i, counting slots across the register
+     * sets in order. A slot with no hart behind it therefore shifts every
+     * later irq one slot early, onto the previous hart's interrupt file.
+     * Counting can't tell that from harmless unused slots at the end, hence
+     * only a warning.
+     */
+    if ( nr_slots > nr_parent_irqs )
+        printk(XENLOG_WARNING
+               "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
+               node->name, nr_slots, nr_parent_irqs);
+
     /* Configure handlers for target CPUs */
     for ( unsigned int i = 0; i < nr_parent_irqs; i++ )
     {

---
base-commit: 97bf370375113caf92fccf04e2877fc6b5ff887f
change-id: 20261113-fix-imsic-mmio-dt-c3317ac2efec

Best regards,
--  
Baptiste Le Duc <baptiste.le-duc@vates.tech>



^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-16 16:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 15:29 [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots Baptiste Le Duc
2026-11-16 15:36 ` Jan Beulich
2026-11-16 15:45   ` Baptiste Le Duc
2026-11-16 15:47     ` Jan Beulich
2026-11-16 16:32 ` Oleksii Kurochko
2026-11-16 16:42   ` Baptiste Le Duc
2026-11-16 16:50     ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.