xref: /linux/drivers/gpu/nova-core/mm.rs (revision 1473ea0e38b3e3bc76d5e9f89ea4378550ed6328)
1 // SPDX-License-Identifier: GPL-2.0
2 // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 
4 //! Memory management subsystems.
5 
6 #![expect(dead_code)]
7 
8 use core::{
9     fmt::LowerHex,
10     ops, //
11 };
12 
13 use kernel::fmt;
14 
15 /// Physical VRAM address in GPU video memory.
16 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
17 #[repr(transparent)]
18 pub(crate) struct VramAddress(u64);
19 
20 impl VramAddress {
21     /// Creates an address from a raw value.
22     pub(crate) const fn from_raw(addr: u64) -> Self {
23         Self(addr)
24     }
25 
26     /// Returns the address as a raw value.
27     pub(crate) const fn into_raw(self) -> u64 {
28         self.0
29     }
30 
31     /// Adds `rhs` to this address, returning [`None`] on overflow.
32     pub(crate) const fn checked_add(self, rhs: u64) -> Option<Self> {
33         match self.into_raw().checked_add(rhs) {
34             Some(addr) => Some(Self::from_raw(addr)),
35             None => None,
36         }
37     }
38 }
39 
40 impl LowerHex for VramAddress {
41     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42         LowerHex::fmt(&self.into_raw(), f)
43     }
44 }
45 
46 impl ops::Add<u64> for VramAddress {
47     type Output = Self;
48 
49     fn add(self, rhs: u64) -> Self::Output {
50         Self::from_raw(self.into_raw() + rhs)
51     }
52 }
53 
54 impl ops::Sub for VramAddress {
55     type Output = u64;
56 
57     fn sub(self, rhs: Self) -> Self::Output {
58         self.into_raw() - rhs.into_raw()
59     }
60 }
61