[PATCH v6 11/34] gpu: nova-core: mm: add VramBlock
From: Alok Kumar
Date: Wed Dec 02 2026 - 03:35:26 EST
From: Zhi Wang <zhiw@xxxxxxxxxx>
vGPU memory slots need allocations at fixed offsets within usable VRAM.
Add GpuMm::alloc_vram_range and VramBlock to own these exact allocations
and return their storage to the buddy allocator on drop.
Embed AllocatedBlocks directly as a pinned field of VramBlock and return
a pinned initializer from alloc_vram_range. This lets a caller embed the
reservation in its owning object without a separate allocation for the
AllocatedBlocks wrapper.
Use the buddy allocator's range validation and retain the checks for
absolute physical address overflow and alignment.
Cc: Alistair Popple <apopple@xxxxxxxxxx>
Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
Co-developed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
Signed-off-by: Alok Kumar <alkumar@xxxxxxxxxx>
---
drivers/gpu/nova-core/mm.rs | 2 +
drivers/gpu/nova-core/mm/vram.rs | 79 ++++++++++++++++++++++++++++++++
2 files changed, 81 insertions(+)
create mode 100644 drivers/gpu/nova-core/mm/vram.rs
diff --git a/drivers/gpu/nova-core/mm.rs b/drivers/gpu/nova-core/mm.rs
index f53886f9ed91..a0aeb96bfa36 100644
--- a/drivers/gpu/nova-core/mm.rs
+++ b/drivers/gpu/nova-core/mm.rs
@@ -67,6 +67,8 @@ macro_rules! impl_pfn_bounded {
mod regs;
pub(super) mod tlb;
pub(super) mod vmm;
+#[expect(dead_code)]
+pub(crate) mod vram;
/// GPU Memory Manager - owns all core MM components.
///
diff --git a/drivers/gpu/nova-core/mm/vram.rs b/drivers/gpu/nova-core/mm/vram.rs
new file mode 100644
index 000000000000..63bc0ce93663
--- /dev/null
+++ b/drivers/gpu/nova-core/mm/vram.rs
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+
+//! VRAM allocation.
+
+use core::ops::Range;
+
+use kernel::{
+ gpu::buddy::{
+ AllocatedBlocks,
+ GpuBuddyAllocFlags,
+ GpuBuddyAllocMode, //
+ },
+ num::casts::FromSafeCast,
+ prelude::*,
+ ptr::Alignment, //
+};
+
+use super::{
+ GpuMm,
+ PAGE_SIZE, //
+};
+
+/// A physically contiguous VRAM allocation.
+///
+/// # Invariants
+///
+/// `range.start <= range.end`.
+#[pin_data]
+pub(crate) struct VramBlock {
+ #[pin]
+ _blocks: AllocatedBlocks,
+ range: Range<u64>,
+}
+
+impl VramBlock {
+ /// Returns the range covered by this VRAM block.
+ pub(crate) const fn range(&self) -> &Range<u64> {
+ &self.range
+ }
+}
+
+impl GpuMm<'_> {
+ /// Allocates an exact byte range relative to the buddy allocator's base.
+ pub(crate) fn alloc_vram_range(
+ &self,
+ range: Range<u64>,
+ align: Alignment,
+ ) -> Result<impl PinInit<VramBlock, Error>> {
+ const PAGE_SIZE_ALIGN: Alignment = Alignment::new::<PAGE_SIZE>();
+
+ let align = if align > PAGE_SIZE_ALIGN {
+ align
+ } else {
+ PAGE_SIZE_ALIGN
+ };
+ let size = range.end.checked_sub(range.start).ok_or(EINVAL)?;
+ let buddy = self.buddy();
+ let base = buddy.base_offset();
+ let start = base.checked_add(range.start).ok_or(EOVERFLOW)?;
+ let end = base.checked_add(range.end).ok_or(EOVERFLOW)?;
+ if !start.is_multiple_of(u64::from_safe_cast(align.as_usize())) {
+ return Err(EINVAL);
+ }
+
+ let blocks = buddy.alloc_blocks(
+ GpuBuddyAllocMode::Range(range),
+ size,
+ align,
+ GpuBuddyAllocFlags::default(),
+ );
+
+ Ok(try_pin_init!(VramBlock {
+ _blocks <- blocks,
+ // INVARIANT: `range.start <= range.end` was checked above.
+ range: start..end,
+ }))
+ }
+}