mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
Own the P100 system-memory flush page during runtime
(cherry picked from commit 5bc9a73db7f09578d065699610965a94ae8143c1)
This commit is contained in:
@ -1904,6 +1904,104 @@ fn boot_maxwell_pascal_pre_gsp(
|
||||
profile: &'static nv_rs_core::gpu::legacy_hal::chip::ChipProfile,
|
||||
level: u32,
|
||||
runtime: bool,
|
||||
) -> Result<(), i32> {
|
||||
use nv_rs_core::gpu::legacy_hal::chip::ChipId;
|
||||
if !runtime || level == 0 || profile.id != ChipId::Gp100 {
|
||||
return boot_maxwell_pascal_pre_gsp_inner(lease, profile, level, runtime);
|
||||
}
|
||||
let mut guard = unsafe { lease.lock_register_io()? };
|
||||
let mut page = PascalFlushPage::allocate(&mut guard)?;
|
||||
let configured = page.program(&mut guard);
|
||||
if let Err(error) = configured {
|
||||
return page.retire(&mut guard).and(Err(error));
|
||||
}
|
||||
drop(guard);
|
||||
|
||||
let result = boot_maxwell_pascal_pre_gsp_inner(lease, profile, level, runtime);
|
||||
let mut guard = unsafe { lease.lock_register_io()? };
|
||||
let retired = page.retire(&mut guard);
|
||||
result.and(retired)
|
||||
}
|
||||
|
||||
struct PascalFlushPage {
|
||||
dma: crate::dma::LinuxDma,
|
||||
backing: nv_rs_core::dma::Backing,
|
||||
mapping: nv_rs_core::dma::DeviceMapping,
|
||||
}
|
||||
|
||||
impl PascalFlushPage {
|
||||
fn allocate(guard: &mut LockedGpuRegisters) -> Result<Self, i32> {
|
||||
use nv_rs_core::dma::Backend;
|
||||
use nv_rs_core::gpu::acr_inputs::FLUSH_BYTES;
|
||||
let mut dma = unsafe { crate::dma::LinuxDma::from_owned_pci(guard.pci_token(), 32) }
|
||||
.map_err(|error| error.0)?;
|
||||
let backing = match dma.allocate(FLUSH_BYTES, FLUSH_BYTES) {
|
||||
Ok(backing) => backing,
|
||||
Err(error) => return dma.close_in_place().map_err(|e| e.0).and(Err(error.0)),
|
||||
};
|
||||
let mapping = match dma.map(&backing) {
|
||||
Ok(mapping) => mapping,
|
||||
Err(error) => {
|
||||
dma.release(&backing).map_err(|e| e.0)?;
|
||||
dma.close_in_place().map_err(|e| e.0)?;
|
||||
return Err(error.0);
|
||||
}
|
||||
};
|
||||
if mapping.address == 0 || mapping.address % FLUSH_BYTES != 0
|
||||
|| mapping.address.checked_add(FLUSH_BYTES).is_none_or(|end| end > 1u64 << 32)
|
||||
{
|
||||
dma.unmap(&backing, &mapping).map_err(|e| e.0)?;
|
||||
dma.release(&backing).map_err(|e| e.0)?;
|
||||
dma.close_in_place().map_err(|e| e.0)?;
|
||||
return Err(-22);
|
||||
}
|
||||
Ok(Self { dma, backing, mapping })
|
||||
}
|
||||
|
||||
fn program(&self, guard: &mut LockedGpuRegisters) -> Result<(), i32> {
|
||||
use nv_rs_core::gpu::acr_inputs::{FLUSH_ADDRESS_REGISTER, FLUSH_ADDRESS_SHIFT};
|
||||
let previous = guard.read(FLUSH_ADDRESS_REGISTER)?;
|
||||
let address = (self.mapping.address >> FLUSH_ADDRESS_SHIFT) as u32;
|
||||
unsafe { nv_rs_bringup_note(guard.pci_token(), 0x4c47_00e0, previous) };
|
||||
guard.write(FLUSH_ADDRESS_REGISTER, address)?;
|
||||
if guard.read(FLUSH_ADDRESS_REGISTER)? != address {
|
||||
return Err(-5);
|
||||
}
|
||||
unsafe { nv_rs_bringup_note(guard.pci_token(), 0x4c47_00e1, address) };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn retire(&mut self, guard: &mut LockedGpuRegisters) -> Result<(), i32> {
|
||||
use nv_rs_core::dma::Backend;
|
||||
use nv_rs_core::gpu::acr_inputs::FLUSH_ADDRESS_REGISTER;
|
||||
unsafe extern "C" {
|
||||
fn nv_rs_pci_bus_master(pci: *mut core::ffi::c_void, enable: bool) -> i32;
|
||||
}
|
||||
let status = unsafe { nv_rs_pci_bus_master(guard.pci_token(), false) };
|
||||
if status != 0 {
|
||||
return Err(status);
|
||||
}
|
||||
// Do not republish the previous driver's retired IOVA. Keep the
|
||||
// new mapping alive if clearing the register cannot be verified.
|
||||
if guard.read(0x88004)? & 4 != 0 {
|
||||
return Err(-5);
|
||||
}
|
||||
guard.write(FLUSH_ADDRESS_REGISTER, 0)?;
|
||||
if guard.read(FLUSH_ADDRESS_REGISTER)? != 0 {
|
||||
return Err(-5);
|
||||
}
|
||||
self.dma.unmap(&self.backing, &self.mapping).map_err(|e| e.0)?;
|
||||
self.dma.release(&self.backing).map_err(|e| e.0)?;
|
||||
self.dma.close_in_place().map_err(|e| e.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn boot_maxwell_pascal_pre_gsp_inner(
|
||||
lease: &GpuLease,
|
||||
profile: &'static nv_rs_core::gpu::legacy_hal::chip::ChipProfile,
|
||||
level: u32,
|
||||
runtime: bool,
|
||||
) -> Result<(), i32> {
|
||||
use nv_rs_core::gpu::legacy_hal::{mmu_v2_pascal, pre_gsp_boot, MmioOp};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user