The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "John Hubbard" <jhubbard@nvidia.com>
Cc: "Joel Fernandes" <joel@joelfernandes.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Shashank Sharma" <shashanks@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>, "David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	nova-gpu@lists.linux.dev, LKML <linux-kernel@vger.kernel.org>,
	"Joel Fernandes" <joelagnelf@nvidia.com>,
	"Will Pierce" <wpierce@nvidia.com>
Subject: Re: [PATCH 06/17] gpu: nova-core: add the GIN interrupt tree API
Date: Fri, 14 Aug 2026 14:13:46 +0200	[thread overview]
Message-ID: <DKONPB717CY7.4UDYVW6C29FY@kernel.org> (raw)
In-Reply-To: <20260808031120.363869-7-jhubbard@nvidia.com>

On Sat Aug 8, 2026 at 5:11 AM CEST, John Hubbard wrote:
> From: Joel Fernandes <joelagnelf@nvidia.com>
>
> Servicing a GIN leaf has a required order: read its pending bits, then
> clear them. Clearing a leaf before reading it discards every vector
> latched in it, and nothing reports the loss.
>
> Add an API for one PCIe function's CPU interrupt tree. The leaf handle
> carries that order as a type state, so the wrong order does not compile.
>
> The CPU doorbell self-test added later in this series is the first user.
>
> Reviewed-by: Will Pierce <wpierce@nvidia.com>
> Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
> [jhubbard: use the canonical NV_VIRTUAL_FUNCTION_PRIV_CPU_INTR_*
>  register names, name the module interrupt_tree with a Tree type, drop
>  the type state from the Top handle, take the leaf count from the
>  chipset, define the vector encoding here, reject a trigger for a vector
>  outside the tree, and read every implemented leaf in drain() rather
>  than descending from the TOP registers, which cannot see a vector that
>  latched while disabled]
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>

It would make more sense if this patch is merged into patch 8 after the HAL is
introduced in patch 7.

(The newtypes mentioned below need their own commit first though, as they'd also
be used by the HAL.)

> +/// Index of a leaf register, bounded to the `0..16` range covered by the leaf register arrays.
> +pub(super) type LeafIndex = Bounded<usize, 4>;
> +
> +/// Maps an interrupt `vector` to its position in the tree: the leaf that carries it
> +/// (`vector / 32`) and the bit index within that leaf (`vector % 32`).
> +///
> +/// The returned leaf is a raw index. [`LeafIndex::try_new`] bounds it to the leaf register
> +/// arrays, and the architecture's leaf count is a separate, narrower bound.
> +pub(super) const fn vector_leaf_bit(vector: u32) -> (usize, u32) {
> +    (crate::num::u32_as_usize(vector / 32), vector % 32)
> +}
> +
> +/// Maps an interrupt `vector` to the `TOP` enable mask of the subtree that carries it.
> +///
> +/// A subtree covers two adjacent leaves, so the vector's leaf is in subtree `vector / 64`. The
> +/// result has that subtree's bit set, in the form `TOP_EN_SET` and `TOP_EN_CLEAR` take as a
> +/// value.
> +///
> +/// The result is not validated against the subtrees that the architecture supports.
> +pub(super) const fn vector_subtree_mask(vector: u32) -> u32 {
> +    1 << (vector / 64)
> +}

I think we use new types for those. I'm thinking of:

	struct GinVector(u32);

	impl GinVector {
	    const fn leaf_index(&self) -> LeafIndex { ... }
	    const fn leaf_mask(&self) -> LeafMask { ... }
	    const fn subtree(&self) -> Subtree { ... }
	}

With additional new types LeafMask and Subtree.

This removes the need for

	const GSP_LEAF: usize = GSP_LOC.0;
	const GSP_BIT: u32 = 1 << GSP_LOC.1;

and subsequent

	LeafIndex::new::<GSP_LEAF>()

Subtree should represent a single TOP bit produced by GinVector::subtree().

This way Subtree already carries the invariant we need and we get rid of the
runtime count_ones() check in SubtreeVectors::request_for().

The semantics of serviced is different and can be represented by a SubtreeSet
type, so the check becomes serviced.contains(subtree).

Now, there's already an existing Subtree type, which represents an index. But I
think we should just get rid of it, as it doesn't really add any value. The two
methods it implements, iter_leaves() and iter_pending_leaves(), are rather Tree
methods.

If we add Subtree::index() they can still take a Subtree argument, but I'm not
sure it's worth. They are only called by drain(), which creates this new type
from a raw value, just to immediately convert it back to a raw value. So, here
I'd just work with the raw value.

> +
> +/// Type state of a [`Leaf`] handle: `Idle` before its pending bits are read, `Pending` after.
> +pub(super) trait State: private::Sealed {}
> +
> +/// State in which the handle holds no pending bits.
> +pub(super) struct Idle;
> +impl State for Idle {}
> +
> +/// State holding the pending bits read from hardware.
> +pub(super) struct Pending {
> +    pending_bits: u32,
> +}
> +impl State for Pending {}
> +
> +mod private {
> +    pub(in crate::irq) trait Sealed {}

This can just be pub.

> +    impl Sealed for super::Idle {}
> +    impl Sealed for super::Pending {}
> +}
> +
> +/// The GIN CPU interrupt tree for a single PCIe function.
> +#[derive(Clone)]

I think we don't need Clone on this, I know it is used in the doorbell test, but
we can simplify the doorbell test significantly by getting rid of the custom
SelftestGuard and replace it with

	struct SelftestResources<'a, 'r> {
	    _leaf_guard: LeafEnableGuard<'a>,
	    reg: Pin<KBox<irq::Registration<'r, DoorbellTestHandler<'a>>>>,
	    _top_guard: TopEnableGuard<'a>,
	}

Note that a LeafEnableGuard will also be very useful for the GspIrq introduced
later.

	pub(crate) struct GspIrq<'a> {
	    #[pin]
	    reg: irq::ThreadedRegistration<'a, GspInterrupt<'a>>,
	    bar: Bar0<'a>,
	    tree: Tree,
	}

just becomes

	pub(crate) struct GspIrq<'a> {
	    _leaf_guard: LeafEnableGuard<'a>,
	    #[pin]
	    reg: irq::ThreadedRegistration<'a, GspInterrupt<'a>>,
	}

And the open-coded destructor of GspIrq goes away.

> +pub(super) struct Tree {
> +    /// Number of implemented leaves in this tree, either 8 or 16.
> +    num_leaves: usize,

If it is really just one or the other, maybe worth to consider something like:

	#[repr(usize)]
	enum LeafCount {
	    Eight = 8,
	    Sixteen = 16,
	}
	
	impl LeafCount {
	    const fn into_raw(self) -> usize {
	        self as usize
	    }
	
	    const fn subtree_count(self) -> usize {
	        self.into_raw() / 2
	    }

	    const fn subtree_set(self) -> SubtreeSet {
	        SubtreeSet((1u32 << self.subtree_count()) - 1)
	    }
	}

A HAL can then only ever pick an expected leaf count.

> +    /// Mask of subtree bits the architecture implements.
> +    subtree_mask: u32,

This should be SubtreeSet.

> +}
> +
> +impl Tree {
> +    /// Creates a `Tree` sized for `chipset`.
> +    pub(super) fn new(chipset: Chipset) -> Self {
> +        let num_leaves = match chipset.arch() {
> +            Architecture::Turing | Architecture::Ampere | Architecture::Ada => 8,
> +            Architecture::Hopper | Architecture::BlackwellGB10x | Architecture::BlackwellGB20x => {
> +                16
> +            }
> +        };
> +
> +        Self {
> +            num_leaves,
> +            // Each subtree covers two leaves, so one bit per pair of leaves.
> +            subtree_mask: (1u32 << (num_leaves / 2)) - 1,
> +        }
> +    }
> +
> +    /// Returns a [`Top`] handle for this tree.
> +    pub(super) fn top(&self) -> Top {
> +        Top {
> +            subtree_mask: self.subtree_mask,
> +        }
> +    }
> +
> +    /// Returns a [`Leaf`] handle in the [`Idle`] state for `index`.
> +    pub(super) fn leaf(&self, index: LeafIndex) -> Leaf<Idle> {
> +        Leaf::from_index(index)
> +    }
> +
> +    /// Injects a software interrupt for `vector` via the trigger register.
> +    ///
> +    /// # Errors
> +    ///
> +    /// `EINVAL` if `vector` lies outside this tree (`vector >= num_leaves * 32`). `EOVERFLOW` if
> +    /// `vector` does not fit in the trigger register's vector field.
> +    pub(super) fn trigger(&self, bar: Bar0<'_>, vector: u32) -> Result {

Most methods take a Bar0 argument. I think it would be cleaner if the Tree
constructor would just take a Bar0 and store it within the tree.

In case this gets into the way of some tests, we can also have a TreeInner type.

> +        if crate::num::u32_as_usize(vector) >= self.num_leaves * 32 {

This check could be replaced by:

	impl GinVector {
	    const fn validate(self, leaves: LeafCount) -> Result {
	        if self.as_raw() >= leaves.vector_count() { return Err(EINVAL) }
	        Ok(())
	    }
	}

so this becomes

	vector.validate(self.leaf_count)?;

> +/// View of a single interrupt leaf.
> +pub(super) struct Leaf<S: State = Idle> {
> +    index: LeafIndex,
> +    state: S,
> +}
> +
> +// The `try_at(...)` calls below cannot fail: `LeafIndex` is `Bounded<usize, 4>`, so its value is
> +// in 0..16, and every leaf register array has 16 elements.
> +impl Leaf<Idle> {
> +    /// Creates a [`Leaf`] handle for `index`.
> +    pub(super) fn from_index(index: LeafIndex) -> Self {
> +        Leaf { index, state: Idle }
> +    }
> +
> +    /// Enables the vectors set in `vectors` for this leaf (`LEAF_EN_SET`).
> +    ///
> +    /// This is the per-vector counterpart of [`Top::enable`], which enables a whole subtree.
> +    pub(super) fn enable(&self, bar: Bar0<'_>, vectors: u32) {
> +        if let Some(loc) = CPU_INTR_LEAF_EN_SET::try_at(self.index.get()) {
> +            bar.write(loc, vectors.into());
> +        }
> +    }
> +
> +    /// Disables the vectors set in `vectors` for this leaf (`LEAF_EN_CLEAR`).
> +    pub(super) fn disable(&self, bar: Bar0<'_>, vectors: u32) {
> +        if let Some(loc) = CPU_INTR_LEAF_EN_CLEAR::try_at(self.index.get()) {
> +            bar.write(loc, vectors.into());
> +        }
> +    }

IIUC, the type state exists only to guard clear_pending() from being called
before read_pending() has been called?

In this case, enable() and disable() are orthogonal and do not participate in
the state machine.

If that holds, I'd probably move enable(), disable() and enable_guarded() to
Tree or just remove the type state and make Leaf<Pending> a new type. (One can
obtain a new  Leaf<Idle> while still having a Leaf<Pending> anyways.)

Also, does the type state really properly fulfill its purpose? Once I called
read_pending() once I can hold on to Leaf<Pending> for as long as I want and
call clear_pending() for as often as I want, right?

I think the idea is that the read_pending() then clear_pending() sequence must
run within the interrupt handler?

So, I assume that we rather want read_pending() to take a token that only lives
for irq::ThreadedHandler::handle() and have a LeafPending that borrows from
this token?

I wonder if the core IRQ code should provide such a token in handle() and
handle_threaded().

This way we can enforce that certain things can only be executed in an IRQ
handler and that certain sequences like read and clear pending must complete
within a single execution of an IRQ.

> +
> +    /// Reads this leaf's pending bits and transitions to [`Pending`].
> +    pub(super) fn read_pending(self, bar: Bar0<'_>) -> Leaf<Pending> {
> +        let pending_bits = CPU_INTR_LEAF::try_at(self.index.get())
> +            .map(|loc| bar.read(loc).into_raw())
> +            .unwrap_or(0);
> +        Leaf {
> +            index: self.index,
> +            state: Pending { pending_bits },
> +        }
> +    }
> +}
> +
> +impl Leaf<Pending> {
> +    /// Returns the pending bits read from hardware.
> +    pub(super) fn pending_bits(&self) -> u32 {
> +        self.state.pending_bits
> +    }
> +
> +    /// Clears every pending vector by writing its bits back (write-1-to-clear).
> +    pub(super) fn clear_pending(&self, bar: Bar0<'_>) {
> +        if self.state.pending_bits != 0 {
> +            if let Some(loc) = CPU_INTR_LEAF::try_at(self.index.get()) {
> +                bar.write(loc, self.state.pending_bits.into());
> +            }
> +        }
> +    }
> +}

  reply	other threads:[~2026-08-14 12:13 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08  3:11 [PATCH 00/17] nova-core: GPU interrupt support and GSP event delivery John Hubbard
2026-08-08  3:11 ` [PATCH 01/17] rust: sync: completion: add wait_for_completion_timeout() John Hubbard
     [not found]   ` <DKK2DM3VK6TF.3KBBWP7S4A8T1@nvidia.com>
2026-08-09 21:43     ` John Hubbard
2026-08-08  3:11 ` [PATCH 02/17] rust: pci: expose the whole interrupt vector allocation John Hubbard
2026-08-09 13:27   ` Danilo Krummrich
2026-08-08  3:11 ` [PATCH 03/17] rust: pci: expose the allocated interrupt type John Hubbard
2026-08-09 13:24   ` Danilo Krummrich
2026-08-09 21:42     ` John Hubbard
2026-08-10 22:53       ` Danilo Krummrich
2026-08-10 22:55         ` John Hubbard
2026-08-11  3:09         ` John Hubbard
2026-08-11 23:56           ` Danilo Krummrich
2026-08-08  3:11 ` [PATCH 04/17] gpu: nova-core: allocate PCI MSI vector during probe John Hubbard
2026-08-13 21:31   ` Danilo Krummrich
2026-08-13 21:37     ` Gary Guo
2026-08-13 21:43     ` John Hubbard
2026-08-13 22:26       ` Danilo Krummrich
2026-08-13 22:29         ` John Hubbard
2026-08-13 22:37           ` Danilo Krummrich
2026-08-13 22:45             ` John Hubbard
2026-08-14 12:12   ` Danilo Krummrich
2026-08-08  3:11 ` [PATCH 05/17] gpu: nova-core: add the GIN CPU interrupt tree and MSI EOI registers John Hubbard
2026-08-13 21:42   ` Danilo Krummrich
2026-08-13 21:43     ` John Hubbard
2026-08-08  3:11 ` [PATCH 06/17] gpu: nova-core: add the GIN interrupt tree API John Hubbard
2026-08-14 12:13   ` Danilo Krummrich [this message]
2026-08-08  3:11 ` [PATCH 07/17] gpu: nova-core: add the per-architecture GIN CPU interrupt HAL John Hubbard
2026-08-08  3:11 ` [PATCH 08/17] gpu: nova-core: allocate interrupt vectors for the serviced subtrees John Hubbard
2026-08-08  3:11 ` [PATCH 09/17] gpu: nova-core: add an interrupt delivery self-test John Hubbard
2026-08-08  3:11 ` [PATCH 10/17] gpu: nova-core: dispatch GSP events instead of discarding them John Hubbard
2026-08-08  3:11 ` [PATCH 11/17] gpu: nova-core: match GSP RPC replies by sequence, not just function John Hubbard
2026-08-08  3:11 ` [PATCH 12/17] gpu: nova-core: recover the GSP receive path from corrupt framing John Hubbard
2026-08-08  3:11 ` [PATCH 13/17] gpu: nova-core: bound a GSP wait by a single deadline John Hubbard
2026-08-08  3:11 ` [PATCH 14/17] gpu: nova-core: drive GSP events with the SWGEN0 interrupt John Hubbard
2026-08-08  3:11 ` [PATCH 15/17] gpu: nova-core: retrigger the GSP falcon and clear every latched cause John Hubbard
2026-08-08  3:11 ` [PATCH 16/17] gpu: nova-core: add KUnit tests for the interrupt tree and HALs John Hubbard
2026-08-08  3:11 ` [PATCH 17/17] gpu: nova-core: document the GIN interrupt controller and GSP events John Hubbard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DKONPB717CY7.4UDYVW6C29FY@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=ecourtney@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=joel@joelfernandes.org \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=shashanks@nvidia.com \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=wpierce@nvidia.com \
    --cc=zhiw@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox