1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
// SPDX-License-Identifier: GPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//! Interrupt delivery self-test.
//!
//! The test triggers the CPU doorbell vector from software, twice, and checks that each trigger
//! reaches a registered handler. It runs during probe under `CONFIG_NOVA_CORE_SELFTESTS`.
use core::pin::Pin;
use kernel::{
device::Bound,
io::poll::read_poll_timeout,
irq,
pci,
prelude::*,
sync::atomic::{
Acquire,
Atomic,
Relaxed,
Release, //
},
time::Delta, //
};
use super::interrupt_tree::{
GinVector,
LeafEnableGuard,
LeafMask,
Subtree,
TopEnableGuard,
Tree, //
};
use crate::{
driver::Bar0,
gpu::Chipset,
selftest_assert,
selftest_assert_eq, //
};
/// The CPU doorbell vector. Every supported GPU uses this number, so the test needs nothing from
/// GSP-RM, which is not running yet.
const DOORBELL_VECTOR: GinVector = GinVector::new::<129>();
/// The only subtree that this test services.
const DOORBELL_SUBTREE: Subtree = DOORBELL_VECTOR.subtree();
/// Time allowed for each delivery to arrive.
const DELIVERY_TIMEOUT: Delta = Delta::from_millis(1000);
/// The self-test's interrupt handler.
///
/// It clears only the doorbell's bit, rearms delivery, and never walks the tree. A missing rearm
/// shows up as a timeout on the second delivery.
#[pin_data]
struct DoorbellTestHandler<'a> {
tree: &'a Tree<'a>,
/// Deliveries that found the doorbell bit set.
irq_count: Atomic<u32>,
/// The doorbell leaf's pending bits, as read by the first delivery.
first_pending: Atomic<u32>,
/// The doorbell leaf's pending bits, as read by the second delivery.
second_pending: Atomic<u32>,
}
impl irq::Handler for DoorbellTestHandler<'_> {
fn handle(&self) -> irq::IrqReturn {
let leaf = self.tree.read_pending(DOORBELL_VECTOR.leaf_index());
let pending = leaf.vectors();
if !pending.contains(DOORBELL_VECTOR.leaf_mask()) {
self.tree.rearm_pci_irq(DOORBELL_SUBTREE);
return irq::IrqReturn::None;
}
leaf.clear_vectors(DOORBELL_VECTOR.leaf_mask());
// Rearm before counting the delivery, since the waiting thread triggers the next doorbell
// as soon as it observes the count.
self.tree.rearm_pci_irq(DOORBELL_SUBTREE);
match self.irq_count.load(Relaxed) {
0 => self.first_pending.store(pending.into_raw(), Relaxed),
1 => self.second_pending.store(pending.into_raw(), Relaxed),
_ => (),
}
self.irq_count.fetch_add(1, Release);
irq::IrqReturn::Handled
}
}
/// The self-test's handler registration and the enables that deliver to it.
///
/// Drops in the order that "Enabling the GSP event" in
/// `Documentation/gpu/nova/core/interrupts.rst` requires: the vector is disabled, then the
/// handler is freed, then the subtree is disabled.
struct SelftestResources<'a, 'r> {
_leaf_guard: LeafEnableGuard<'a>,
reg: Pin<KBox<irq::Registration<'r, DoorbellTestHandler<'a>>>>,
_top_guard: TopEnableGuard<'a>,
}
impl<'a> SelftestResources<'a, '_> {
fn handler(&self) -> &DoorbellTestHandler<'a> {
self.reg.handler()
}
/// Disables the doorbell vector and waits for a handler in flight on another CPU to finish.
///
/// The handler's counters and the leaf's pending bits are final on return.
fn quiesce_source(&self) {
self.handler()
.tree
.disable_leaf(DOORBELL_VECTOR.leaf_index(), DOORBELL_VECTOR.leaf_mask());
self.reg.synchronize();
}
}
/// Waits until `handler` has serviced `deliveries` interrupts, or [`DELIVERY_TIMEOUT`] elapses.
///
/// Returns `false` on timeout.
fn wait_for_deliveries(handler: &DoorbellTestHandler<'_>, deliveries: u32) -> bool {
const DELIVERY_POLL_INTERVAL: Delta = Delta::from_micros(100);
read_poll_timeout(
|| Ok(handler.irq_count.load(Acquire)),
|count| *count >= deliveries,
DELIVERY_POLL_INTERVAL,
DELIVERY_TIMEOUT,
)
.is_ok()
}
/// Runs the interrupt delivery self-test.
///
/// Call this only during probe, before GSP boot: it disables every vector in the tree and clears
/// every pending bit. On return, the doorbell's subtree is disabled at `TOP`, and the test's PCI
/// vectors and handler are released.
///
/// # Errors
///
/// `EINVAL` if `chipset` does not implement the doorbell's subtree. `ETIMEDOUT` if a delivery
/// does not arrive within [`DELIVERY_TIMEOUT`]. `EIO` if a self-test assertion fails.
/// Otherwise the error from allocating the PCI vectors or registering the handler.
pub(crate) fn run_selftest(pdev: &pci::Device<Bound>, bar: Bar0<'_>, chipset: Chipset) -> Result {
let tree = Tree::new(pdev, bar, chipset, DOORBELL_SUBTREE.into())?;
let tree_ref = &tree;
let request = tree.request_for(DOORBELL_SUBTREE)?;
let doorbell = DOORBELL_VECTOR.leaf_index();
let doorbell_mask = DOORBELL_VECTOR.leaf_mask();
dev_info!(
pdev,
"interrupt self-test: starting on vector {}, subtree {}, with {:?}\n",
DOORBELL_VECTOR.into_raw(),
DOORBELL_SUBTREE.index(),
tree.msi_type,
);
// GFW boot can leave vectors enabled and pending. Registering a handler unmasks the PCI
// interrupt, and they would be delivered to a handler that services only the doorbell.
tree.reset();
// A delivery proves nothing unless the doorbell bit starts out clear.
let pre_pending = tree.read_pending(doorbell).vectors();
selftest_assert!(
pdev,
!pre_pending.contains(doorbell_mask),
"vector {} already pending, leaf[{}] is {:#x}",
DOORBELL_VECTOR.into_raw(),
doorbell.get(),
pre_pending.into_raw()
);
let handler_init = try_pin_init!(DoorbellTestHandler {
tree: tree_ref,
irq_count: Atomic::new(0),
first_pending: Atomic::new(0),
second_pending: Atomic::new(0),
}? Error);
// Registration must precede any enable, or a delivery reaches no handler.
let reg = KBox::pin_init(
// SAFETY: this registration is dropped before the enclosing function returns, so its
// `Drop`, which calls `free_irq()`, always runs.
unsafe {
irq::Registration::new(
request,
irq::Flags::TRIGGER_NONE,
c"nova-core-selftest",
handler_init,
)
},
GFP_KERNEL,
)?;
let resources = SelftestResources {
_leaf_guard: tree.enable_leaf_guarded(doorbell, doorbell_mask),
_top_guard: tree.enable_top_guarded(),
reg,
};
let handler = resources.handler();
tree.trigger(DOORBELL_VECTOR)?;
let mut completed = wait_for_deliveries(handler, 1);
// The second trigger waits for the first delivery, or the two could coalesce.
if completed {
tree.trigger(DOORBELL_VECTOR)?;
completed = wait_for_deliveries(handler, 2);
}
resources.quiesce_source();
let count = handler.irq_count.load(Relaxed);
let first_pending = LeafMask::from_raw(handler.first_pending.load(Relaxed));
let second_pending = LeafMask::from_raw(handler.second_pending.load(Relaxed));
let residual = tree.read_pending(doorbell).vectors();
if !completed {
dev_err!(
pdev,
"interrupt self-test: only {} of 2 deliveries arrived within {} ms\n",
count,
DELIVERY_TIMEOUT.as_millis(),
);
return Err(ETIMEDOUT);
}
selftest_assert_eq!(pdev, count, 2, "delivery count");
// Every other vector in the leaf is disabled and was drained, so require the exact mask.
selftest_assert_eq!(pdev, first_pending, doorbell_mask, "first delivery");
selftest_assert_eq!(pdev, second_pending, doorbell_mask, "second delivery");
selftest_assert!(
pdev,
!residual.contains(doorbell_mask),
"vector {} still pending, leaf[{}] is {:#x}",
DOORBELL_VECTOR.into_raw(),
doorbell.get(),
residual.into_raw()
);
dev_info!(
pdev,
"interrupt self-test: passed, subtree {}, {} deliveries\n",
DOORBELL_SUBTREE.index(),
count,
);
Ok(())
}
|