mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
Mechanical, behavior-neutral: edition/resolver keys, unsafe extern blocks, unsafe(no_mangle)/unsafe(export_name), unsafe blocks inside unsafe fn, r#gen renames, --edition flags in Makefiles/tools, rustfmt style_edition pinned to 2021 so formatting stays edition-independent, and clippy collapsible_if allowed workspace-wide (let-chain churn). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
351 lines
12 KiB
Rust
351 lines
12 KiB
Rust
//! Real Linux closed580 jiffy IRQ/deferred OS-event registration and scheduling; one owned test
|
|
//! event/list entry, not an RM active-list implementation or GPU readiness.
|
|
use crate::{
|
|
clock::LinuxClock,
|
|
gpu::{GpuLease, LockedGpuRegisters},
|
|
gpu_registry::{self, Key},
|
|
scheduling::LinuxScheduling,
|
|
thread,
|
|
timer_event::{self, Diagnostics, Dispatch, Fields, Relative, Source},
|
|
};
|
|
use core::{
|
|
ffi::c_void,
|
|
sync::atomic::{AtomicBool, AtomicU32, Ordering},
|
|
};
|
|
use nv_rs_abi::NvStatus;
|
|
use nv_rs_core::gpu::timer::load;
|
|
static DROPS: AtomicU32 = AtomicU32::new(0);
|
|
struct Counter {
|
|
key: Key,
|
|
linked: AtomicBool,
|
|
calls: AtomicU32,
|
|
queued_seen: AtomicBool,
|
|
irq: AtomicU32,
|
|
held: AtomicBool,
|
|
entries: AtomicU32,
|
|
exits: AtomicU32,
|
|
fail_detach: AtomicBool,
|
|
detaches: AtomicU32,
|
|
}
|
|
impl Counter {
|
|
fn new(key: Key) -> Self {
|
|
Self {
|
|
key,
|
|
linked: AtomicBool::new(true),
|
|
calls: AtomicU32::new(0),
|
|
queued_seen: AtomicBool::new(false),
|
|
irq: AtomicU32::new(u32::MAX),
|
|
held: AtomicBool::new(false),
|
|
entries: AtomicU32::new(0),
|
|
exits: AtomicU32::new(0),
|
|
fail_detach: AtomicBool::new(false),
|
|
detaches: AtomicU32::new(0),
|
|
}
|
|
}
|
|
}
|
|
struct WorkerGuard<'a> {
|
|
counter: &'a Counter,
|
|
operation: Option<LockedGpuRegisters>,
|
|
}
|
|
impl Drop for WorkerGuard<'_> {
|
|
fn drop(&mut self) {
|
|
self.counter.held.store(false, Ordering::Release);
|
|
drop(self.operation.take());
|
|
self.counter.exits.fetch_add(1, Ordering::AcqRel);
|
|
}
|
|
}
|
|
unsafe impl Dispatch for Counter {
|
|
type Guard<'a> = WorkerGuard<'a>;
|
|
fn enter_work(&self) -> Result<timer_event::WorkEnter<WorkerGuard<'_>>, NvStatus> {
|
|
let context = thread::current().map_err(|_| NvStatus(0x40))?;
|
|
if context.in_interrupt != 0 {
|
|
return Err(NvStatus(0x40));
|
|
}
|
|
let gpu = unsafe { gpu_registry::acquire(self.key) }.map_err(|_| NvStatus(0x40))?;
|
|
// Use the actual C operation mutex shared with the scheduling caller.
|
|
// This is a test list owner, not the original RM GPU-group lock.
|
|
let operation = unsafe { gpu.lock_register_io() }.map_err(|_| NvStatus(0x40))?;
|
|
if self.held.swap(true, Ordering::AcqRel) {
|
|
return Err(NvStatus(0x40));
|
|
}
|
|
self.irq.store(context.in_interrupt, Ordering::Release);
|
|
self.entries.fetch_add(1, Ordering::AcqRel);
|
|
Ok(timer_event::WorkEnter::Acquired(WorkerGuard {
|
|
counter: self,
|
|
operation: Some(operation),
|
|
}))
|
|
}
|
|
fn remove_from_active_list(
|
|
&self,
|
|
_guard: &mut WorkerGuard<'_>,
|
|
_event: &Fields,
|
|
) -> Result<nv_rs_core::gpu::bringup::ExecutedStatus, NvStatus> {
|
|
Ok(nv_rs_core::gpu::bringup::ExecutedStatus::completed(
|
|
if self.linked.swap(false, Ordering::AcqRel) {
|
|
NvStatus::OK
|
|
} else {
|
|
NvStatus(0x3f)
|
|
},
|
|
))
|
|
}
|
|
fn invoke(&self, _guard: &mut WorkerGuard<'_>, event: &Fields) {
|
|
self.queued_seen.store(
|
|
event.flags() & 4 != 0 && self.held.load(Ordering::Acquire),
|
|
Ordering::Release,
|
|
);
|
|
event.set_flags(event.flags() | 0x84);
|
|
event.set_period_ns(999);
|
|
self.calls.fetch_add(1, Ordering::AcqRel);
|
|
}
|
|
fn detach(&self) -> Result<(), NvStatus> {
|
|
let gpu = unsafe { gpu_registry::acquire(self.key) }.map_err(|_| NvStatus(0x40))?;
|
|
let _operation = unsafe { gpu.lock_register_io() }.map_err(|_| NvStatus(0x40))?;
|
|
self.detaches.fetch_add(1, Ordering::AcqRel);
|
|
if self.fail_detach.load(Ordering::Acquire) {
|
|
return Err(NvStatus(0x56));
|
|
}
|
|
self.linked.store(false, Ordering::Release);
|
|
Ok(())
|
|
}
|
|
fn diagnostic(&self, _stage: timer_event::Stage, _status: NvStatus) {}
|
|
}
|
|
impl Drop for Counter {
|
|
fn drop(&mut self) {
|
|
DROPS.fetch_add(1, Ordering::AcqRel);
|
|
}
|
|
}
|
|
struct Diag {
|
|
missing: u32,
|
|
rejected: u32,
|
|
}
|
|
impl Diagnostics for Diag {
|
|
fn event_diagnostic(&mut self, event: load::Diagnostic) -> Result<(), NvStatus> {
|
|
match event {
|
|
load::Diagnostic::MissingOsHandle => self.missing += 1,
|
|
load::Diagnostic::StartRejected(_) => self.rejected += 1,
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
struct Schedule<'a> {
|
|
source: &'a Source,
|
|
event: &'a timer_event::Event<Counter>,
|
|
diagnostics: &'a mut Diag,
|
|
ns: u64,
|
|
}
|
|
unsafe extern "C" fn schedule_irqoff(raw: *mut c_void) -> i32 {
|
|
unsafe {
|
|
let context = &mut *raw.cast::<Schedule<'_>>();
|
|
let mut backend = Relative::<Counter, _>::new(context.source, context.diagnostics);
|
|
match load::schedule_os_relative(&mut backend, &context.event.reference(), context.ns) {
|
|
Ok(receipt) => receipt.status().0 as i32,
|
|
Err(_) => -5,
|
|
}
|
|
}
|
|
}
|
|
unsafe extern "C" fn close_irqoff(raw: *mut c_void) -> i32 {
|
|
unsafe {
|
|
match (&mut *raw.cast::<timer_event::Event<Counter>>()).close() {
|
|
Err(timer_event::Error::Context) => 0,
|
|
_ => -5,
|
|
}
|
|
}
|
|
}
|
|
unsafe extern "C" {
|
|
fn vm_nano_call_irqoff(
|
|
timer: *mut c_void,
|
|
call: unsafe extern "C" fn(*mut c_void) -> i32,
|
|
) -> i32;
|
|
}
|
|
pub unsafe fn run(gpu: &GpuLease) -> i32 {
|
|
unsafe {
|
|
let mut checks = 0;
|
|
macro_rules! check {
|
|
($value:expr) => {
|
|
if !$value {
|
|
return -5;
|
|
}
|
|
checks += 1;
|
|
};
|
|
}
|
|
let source = match gpu
|
|
.try_clone()
|
|
.and_then(|g| Source::new(g).map_err(|v| v.0))
|
|
{
|
|
Ok(s) => {
|
|
checks += 1;
|
|
s
|
|
}
|
|
Err(_) => return -5,
|
|
}; //1
|
|
let key = match gpu_registry::key_for(gpu) {
|
|
Ok(key) => key,
|
|
Err(_) => return -5,
|
|
};
|
|
let counter = Counter::new(key);
|
|
let mut event = match source.create(0x10, 55, counter) {
|
|
Ok(e) => {
|
|
checks += 1;
|
|
e
|
|
}
|
|
Err(_) => return -5,
|
|
}; //2
|
|
check!(event.flags() == Ok(0x10)); //3
|
|
check!(event.fields().is_ok_and(|e| e.period_ns() == 55)); //4
|
|
check!(event
|
|
.dispatch()
|
|
.is_ok_and(|e| e.calls.load(Ordering::Acquire) == 0)); //5
|
|
let mut diag = Diag {
|
|
missing: 0,
|
|
rejected: 0,
|
|
};
|
|
let mut context = Schedule {
|
|
source: &source,
|
|
event: &event,
|
|
diagnostics: &mut diag,
|
|
ns: 60_000_000_000,
|
|
};
|
|
let operation = match gpu.lock_register_io() {
|
|
Ok(lock) => lock,
|
|
Err(_) => return -5,
|
|
};
|
|
check!(
|
|
vm_nano_call_irqoff((&mut context as *mut Schedule<'_>).cast(), schedule_irqoff) == 0
|
|
); //6
|
|
check!(event.flags() == Ok(0x14)); //7
|
|
context.ns = 20_000_000;
|
|
check!(
|
|
vm_nano_call_irqoff((&mut context as *mut Schedule<'_>).cast(), schedule_irqoff) == 0
|
|
); //8
|
|
drop(operation);
|
|
let start = LinuxClock.monotonic_ns();
|
|
loop {
|
|
if event.dispatch().is_ok_and(|e| {
|
|
e.calls.load(Ordering::Acquire) == 1 && e.exits.load(Ordering::Acquire) == 1
|
|
}) && event.flags().is_ok_and(|f| f & 4 == 0)
|
|
{
|
|
break;
|
|
}
|
|
if LinuxClock.monotonic_ns().wrapping_sub(start) > 1_000_000_000 {
|
|
return -5;
|
|
}
|
|
if LinuxScheduling.delay_us(1000).is_err() {
|
|
return -5;
|
|
}
|
|
}
|
|
check!(event
|
|
.dispatch()
|
|
.is_ok_and(|e| e.calls.load(Ordering::Acquire) == 1)); //9
|
|
check!(event.fields().is_ok_and(|e| e.period_ns() == 999)); //10
|
|
check!(event.flags() == Ok(0x90)); //11
|
|
check!(event
|
|
.dispatch()
|
|
.is_ok_and(|e| e.queued_seen.load(Ordering::Acquire))); //12
|
|
check!(event
|
|
.dispatch()
|
|
.is_ok_and(|e| e.irq.load(Ordering::Acquire) == 0
|
|
&& e.entries.load(Ordering::Acquire) == 1
|
|
&& e.exits.load(Ordering::Acquire) == 1
|
|
&& !e.held.load(Ordering::Acquire))); //13
|
|
check!(event.callback_failure() == Ok(None)); //14
|
|
event
|
|
.dispatch()
|
|
.unwrap()
|
|
.fail_detach
|
|
.store(true, Ordering::Release);
|
|
check!(event.close() == Err(timer_event::Error::Service(NvStatus(0x56)))); //15
|
|
check!(event.flags() == Ok(0x90)); //16
|
|
check!(DROPS.load(Ordering::Acquire) == 0); //17
|
|
check!(
|
|
vm_nano_call_irqoff(
|
|
(&mut event as *mut timer_event::Event<Counter>).cast(),
|
|
close_irqoff
|
|
) == 0
|
|
); //18
|
|
check!(event
|
|
.dispatch()
|
|
.is_ok_and(|e| e.detaches.load(Ordering::Acquire) == 1)); //19
|
|
event
|
|
.dispatch()
|
|
.unwrap()
|
|
.fail_detach
|
|
.store(false, Ordering::Release);
|
|
check!(event.close() == Ok(())); //20
|
|
check!(DROPS.load(Ordering::Acquire) == 1); //21
|
|
let mut backend = Relative::<Counter, _>::new(&source, &mut diag);
|
|
check!(
|
|
load::schedule_os_relative(&mut backend, &event.reference(), 1)
|
|
.is_ok_and(|r| r.status() == NvStatus(0x3f))
|
|
); //22
|
|
check!(diag.missing == 1 && diag.rejected == 0); //23
|
|
check!(event.close() == Ok(())); //24
|
|
let mut cancelled = match source.create(0x10, 55, Counter::new(key)) {
|
|
Ok(event) => {
|
|
checks += 1;
|
|
event
|
|
}
|
|
Err(_) => return -5,
|
|
}; //25
|
|
let operation = match gpu.lock_register_io() {
|
|
Ok(lock) => lock,
|
|
Err(_) => return -5,
|
|
};
|
|
let mut schedule = Schedule {
|
|
source: &source,
|
|
event: &cancelled,
|
|
diagnostics: &mut diag,
|
|
ns: 1,
|
|
};
|
|
if vm_nano_call_irqoff((&mut schedule as *mut Schedule<'_>).cast(), schedule_irqoff) != 0 {
|
|
return -5;
|
|
}
|
|
let start = LinuxClock.monotonic_ns();
|
|
while cancelled.queue_status() != Ok(Some(NvStatus::OK)) {
|
|
if LinuxClock.monotonic_ns().wrapping_sub(start) > 1_000_000_000
|
|
|| LinuxScheduling.delay_us(1000).is_err()
|
|
{
|
|
return -5;
|
|
}
|
|
}
|
|
check!(cancelled.queue_status() == Ok(Some(NvStatus::OK))); //26
|
|
check!(
|
|
cancelled.flags() == Ok(0x14)
|
|
&& cancelled
|
|
.dispatch()
|
|
.is_ok_and(|e| e.calls.load(Ordering::Acquire) == 0
|
|
&& e.entries.load(Ordering::Acquire) == 0)
|
|
); //27
|
|
// Cancel membership while the real worker is blocked on our operation mutex.
|
|
cancelled
|
|
.dispatch()
|
|
.unwrap()
|
|
.linked
|
|
.store(false, Ordering::Release);
|
|
drop(operation);
|
|
let start = LinuxClock.monotonic_ns();
|
|
while cancelled
|
|
.dispatch()
|
|
.is_ok_and(|e| e.exits.load(Ordering::Acquire) == 0)
|
|
{
|
|
if LinuxClock.monotonic_ns().wrapping_sub(start) > 1_000_000_000
|
|
|| LinuxScheduling.delay_us(1000).is_err()
|
|
{
|
|
return -5;
|
|
}
|
|
}
|
|
check!(cancelled.service_status() == Ok(Some(NvStatus(0x3f)))); //28
|
|
check!(
|
|
cancelled.flags() == Ok(0x14)
|
|
&& cancelled
|
|
.dispatch()
|
|
.is_ok_and(|e| e.calls.load(Ordering::Acquire) == 0)
|
|
); //29
|
|
check!(cancelled.dispatch().is_ok_and(
|
|
|e| e.entries.load(Ordering::Acquire) == 1 && e.exits.load(Ordering::Acquire) == 1
|
|
)); //30
|
|
check!(cancelled.close() == Ok(())); //31
|
|
check!(DROPS.load(Ordering::Acquire) == 2); //32
|
|
checks
|
|
}
|
|
}
|