Files
nvdriver/src/nvidia/kernel/rom_cache.rs
LIghtJUNction 32c8728037 Migrate the workspace and kernel crates to Rust edition 2024
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>
2026-12-02 01:52:38 +08:00

45 lines
1.6 KiB
Rust

//! CPU cache storage for the actual global image-cache algorithm.
//! No shared global instance, FFI pointer, GPU source or readiness is published.
use core::{ffi::c_void, ptr::NonNull};
use nv_rs_abi::NvStatus;
use nv_rs_core::gpu::rom_cache::Storage;
unsafe extern "C" {
fn nv_rs_rom_buffer_alloc(bytes: usize) -> *mut c_void;
fn nv_rs_rom_buffer_free(buffer: *mut c_void);
}
pub struct LinuxRomStorage;
/// Unique CPU allocation. As with other kernel owners, final Drop must execute
/// in active module context; no instance may escape through unpinned future FFI.
pub struct LinuxRomBuffer {
pointer: NonNull<u8>,
bytes: usize,
}
impl AsRef<[u8]> for LinuxRomBuffer {
fn as_ref(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts(self.pointer.as_ptr(), self.bytes) }
}
}
impl AsMut<[u8]> for LinuxRomBuffer {
fn as_mut(&mut self) -> &mut [u8] {
unsafe { core::slice::from_raw_parts_mut(self.pointer.as_ptr(), self.bytes) }
}
}
impl Drop for LinuxRomBuffer {
fn drop(&mut self) {
unsafe { nv_rs_rom_buffer_free(self.pointer.as_ptr().cast()) }
}
}
impl Storage for LinuxRomStorage {
type Buffer = LinuxRomBuffer;
fn allocate(&mut self, bytes: usize) -> Result<Self::Buffer, NvStatus> {
// CPU backing accepts original u32-sized sources independently of
// PROM/FW physical limits; actual allocator failure remains NO_MEMORY.
let pointer = NonNull::new(unsafe { nv_rs_rom_buffer_alloc(bytes).cast::<u8>() })
.ok_or(NvStatus(0x51))?;
Ok(LinuxRomBuffer { pointer, bytes })
}
fn free(&mut self, buffer: Self::Buffer) {
drop(buffer);
}
}