[PATCH v4 03/17] gpu: nova-core: add the GIN vector, leaf and subtree types

From: John Hubbard

Date: Sat Sep 12 2026 - 00:45:35 EST


GIN, the GPU Interrupt and Notification unit, is the GPU's interrupt
controller. Each interrupt source has a GIN vector number, and the
controller latches a pending vector in a two-level tree: one bit of a
LEAF register, summarized two leaves at a time by one bit of the TOP
register. A vector's number fixes its position in that tree:

leaf = vector / 32
bit = vector % 32
subtree = leaf / 2

A tree implements either 8 or 16 leaves, depending on the GPU family.
The leaf count sets both the number of subtrees and the highest vector
the tree carries.

Without distinct types, a vector, a leaf index, a set of vectors within
one leaf, a subtree and a set of subtrees are all plain integers.
Nothing stops a caller from passing one where another belongs, or a
register field from accepting the wrong one.

Add a type for each of those, and for the leaf count. A vector converts
to its own leaf, bit and subtree. Its constructor rejects, at build
time, a number beyond the widest supported tree, and a validation
method rejects, at run time, a number beyond the leaves that the
current tree implements. A leaf count yields the set of subtrees it
implements.

Nothing uses the module yet. The following patches declare the tree
registers and the tree itself in terms of these types.

Suggested-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>
---
drivers/gpu/nova-core/irq.rs | 12 +
drivers/gpu/nova-core/irq/interrupt_tree.rs | 238 ++++++++++++++++++++
drivers/gpu/nova-core/nova_core.rs | 2 +
3 files changed, 252 insertions(+)
create mode 100644 drivers/gpu/nova-core/irq.rs
create mode 100644 drivers/gpu/nova-core/irq/interrupt_tree.rs

diff --git a/drivers/gpu/nova-core/irq.rs b/drivers/gpu/nova-core/irq.rs
new file mode 100644
index 000000000000..f1323f633a03
--- /dev/null
+++ b/drivers/gpu/nova-core/irq.rs
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+
+//! GPU interrupt support.
+//!
+//! GIN, the GPU Interrupt and Notification unit, is the GPU's interrupt controller. It latches
+//! every interrupt source in a two-level register tree and delivers the tree to the CPU as a
+//! message-signaled PCI interrupt.
+//!
+//! See `Documentation/gpu/nova/core/interrupts.rst`.
+
+mod interrupt_tree;
diff --git a/drivers/gpu/nova-core/irq/interrupt_tree.rs b/drivers/gpu/nova-core/irq/interrupt_tree.rs
new file mode 100644
index 000000000000..24976a3146be
--- /dev/null
+++ b/drivers/gpu/nova-core/irq/interrupt_tree.rs
@@ -0,0 +1,238 @@
+// SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+
+//! Vector addressing in the GIN CPU interrupt tree.
+//!
+//! A [`GinVector`] names an interrupt source, a [`LeafIndex`] the leaf register that latches it,
+//! a [`LeafMask`] a set of vectors within one leaf, and a [`Subtree`] one `TOP` bit. The types
+//! keep the four from being confused with one another.
+//!
+//! See `Documentation/gpu/nova/core/interrupts.rst`.
+
+use kernel::{
+ num::Bounded,
+ prelude::*, //
+};
+
+use crate::num;
+
+/// Number of vectors one leaf register carries, one per bit.
+const VECTORS_PER_LEAF: u32 = u32::BITS;
+
+/// Number of leaves one subtree covers.
+const LEAVES_PER_SUBTREE: u32 = 2;
+
+/// Number of subtrees the widest supported tree implements.
+const MAX_NUM_SUBTREES: u32 = 8;
+
+/// Number of leaves the widest supported tree implements.
+const MAX_NUM_LEAVES: u32 = MAX_NUM_SUBTREES * LEAVES_PER_SUBTREE;
+
+/// Number of bits needed to address every vector in the widest supported tree.
+const VECTOR_BITS: u32 = (MAX_NUM_LEAVES * VECTORS_PER_LEAF).ilog2();
+
+/// Index of a leaf register within the widest supported tree. An 8-leaf tree implements only the
+/// lower half of the range.
+pub(super) type LeafIndex = Bounded<usize, { MAX_NUM_LEAVES.ilog2() }>;
+
+/// Number of leaves a tree implements.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+#[repr(usize)]
+pub(super) enum LeafCount {
+ /// Turing through Ada.
+ Eight = 8,
+
+ /// Hopper and later.
+ Sixteen = 16,
+}
+
+impl LeafCount {
+ pub(super) const fn into_u32(self) -> u32 {
+ // CAST: both discriminants are 16 or below.
+ self as u32
+ }
+
+ pub(super) const fn into_raw(self) -> usize {
+ num::u32_as_usize(self.into_u32())
+ }
+
+ /// Returns the number of subtrees a tree of this size implements.
+ pub(super) const fn subtree_count(self) -> u32 {
+ self.into_u32() / LEAVES_PER_SUBTREE
+ }
+
+ /// Returns the set of every subtree a tree of this size implements.
+ pub(super) const fn subtree_set(self) -> SubtreeSet {
+ SubtreeSet((1u32 << self.subtree_count()) - 1)
+ }
+
+ /// Returns the number of vectors a tree of this size carries.
+ pub(super) const fn vector_count(self) -> u32 {
+ self.into_u32() * VECTORS_PER_LEAF
+ }
+}
+
+// `VECTOR_BITS` and `LeafCount::Sixteen` are written separately. This assert keeps them in
+// agreement about the widest supported tree.
+static_assert!(1 << VECTOR_BITS == LeafCount::Sixteen.vector_count());
+
+/// Set of vectors within one leaf, one bit per vector.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(super) struct LeafMask(u32);
+
+impl LeafMask {
+ /// Returns the mask with every vector set.
+ pub(super) const fn all() -> Self {
+ Self(u32::MAX)
+ }
+
+ pub(super) const fn from_raw(raw: u32) -> Self {
+ Self(raw)
+ }
+
+ pub(super) const fn into_raw(self) -> u32 {
+ self.0
+ }
+
+ pub(super) const fn is_empty(self) -> bool {
+ self.0 == 0
+ }
+
+ /// Returns whether every vector in `other` is also in this mask.
+ pub(super) const fn contains(self, other: Self) -> bool {
+ self.0 & other.0 == other.0
+ }
+}
+
+impl From<Bounded<u32, 32>> for LeafMask {
+ fn from(vectors: Bounded<u32, 32>) -> Self {
+ Self(vectors.get())
+ }
+}
+
+impl From<LeafMask> for Bounded<u32, 32> {
+ fn from(vectors: LeafMask) -> Self {
+ vectors.0.into()
+ }
+}
+
+/// One subtree, held as the `TOP` bit that covers it.
+///
+/// # Invariants
+///
+/// Exactly one bit is set.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(super) struct Subtree(u32);
+
+impl Subtree {
+ /// Returns the subtree at index `idx`.
+ const fn new(idx: u32) -> Self {
+ // INVARIANT: shifting `1` left leaves exactly one bit set.
+ Self(1 << idx)
+ }
+
+ /// Returns this subtree's index within the tree.
+ pub(super) const fn index(self) -> u32 {
+ self.0.trailing_zeros()
+ }
+
+ pub(super) const fn into_raw(self) -> u32 {
+ self.0
+ }
+}
+
+/// Set of subtrees, one bit per subtree, in the layout of the `TOP` registers.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(super) struct SubtreeSet(u32);
+
+impl SubtreeSet {
+ pub(super) const fn contains(self, subtree: Subtree) -> bool {
+ self.0 & subtree.into_raw() != 0
+ }
+
+ pub(super) const fn is_empty(self) -> bool {
+ self.0 == 0
+ }
+
+ pub(super) const fn intersection(self, other: Self) -> Self {
+ Self(self.0 & other.0)
+ }
+
+ /// Returns one more than the highest index in this set, or `0` for an empty set. An MSI-X
+ /// allocation that covers the set needs this many entries.
+ pub(super) const fn span(self) -> u32 {
+ u32::BITS - self.0.leading_zeros()
+ }
+
+ /// Returns the subtrees of this set, lowest index first.
+ #[expect(dead_code)]
+ pub(super) fn iter(self) -> impl Iterator<Item = Subtree> {
+ (0..u32::BITS)
+ .map(Subtree::new)
+ .filter(move |subtree| self.contains(*subtree))
+ }
+}
+
+impl From<Subtree> for SubtreeSet {
+ fn from(subtree: Subtree) -> Self {
+ Self(subtree.into_raw())
+ }
+}
+
+impl From<Bounded<u32, 32>> for SubtreeSet {
+ fn from(subtrees: Bounded<u32, 32>) -> Self {
+ Self(subtrees.get())
+ }
+}
+
+impl From<SubtreeSet> for Bounded<u32, 32> {
+ fn from(subtrees: SubtreeSet) -> Self {
+ subtrees.0.into()
+ }
+}
+
+/// A GIN interrupt vector, bounded to the widest tree any supported part implements.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(super) struct GinVector(Bounded<u32, VECTOR_BITS>);
+
+impl GinVector {
+ /// Returns vector number `VECTOR`.
+ ///
+ /// Fails to compile if `VECTOR` is beyond the widest supported tree.
+ pub(super) const fn new<const VECTOR: u32>() -> Self {
+ Self(Bounded::<u32, VECTOR_BITS>::new::<VECTOR>())
+ }
+
+ pub(super) const fn into_raw(self) -> u32 {
+ self.0.get()
+ }
+
+ /// Returns this vector's leaf.
+ pub(super) fn leaf_index(self) -> LeafIndex {
+ // CALC: `self.0 / VECTORS_PER_LEAF`.
+ self.0.shr::<{ VECTORS_PER_LEAF.ilog2() }, _>().cast()
+ }
+
+ /// Returns this vector's bit within its leaf.
+ pub(super) const fn leaf_mask(self) -> LeafMask {
+ LeafMask(1 << (self.0.get() % VECTORS_PER_LEAF))
+ }
+
+ /// Returns this vector's subtree.
+ pub(super) const fn subtree(self) -> Subtree {
+ Subtree::new(self.0.get() / (VECTORS_PER_LEAF * LEAVES_PER_SUBTREE))
+ }
+
+ /// Checks that a tree with `leaves` leaves implements this vector.
+ ///
+ /// # Errors
+ ///
+ /// `EINVAL` if it does not.
+ pub(super) const fn validate(self, leaves: LeafCount) -> Result {
+ if self.0.get() >= leaves.vector_count() {
+ return Err(EINVAL);
+ }
+
+ Ok(())
+ }
+}
diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nova_core.rs
index 1133c6ce5c55..5176a5fe2da2 100644
--- a/drivers/gpu/nova-core/nova_core.rs
+++ b/drivers/gpu/nova-core/nova_core.rs
@@ -17,6 +17,8 @@
mod fsp;
mod gpu;
mod gsp;
+#[expect(dead_code)]
+mod irq;
mod mctp;
mod mm;
#[macro_use]
--
2.55.0