John Hubbard
2025-Nov-27 00:53 UTC
[PATCH v2 2/5] gpu: nova-core: add FbRange.len() and use it in boot.rs
On 11/26/25 3:43 PM, Lyude Paul wrote:> I'm not sure this is necessary - for one, we could just use the .len() method > on the Range. As well - if we just implement Deref for FbRange (which I think > would be fine here) we could just use .len() through that.Hi Lyude! Good idea about the deref coercion. It has a minor type mismatch as-is, though: Range<u64>::len() returns usize, but FbRange::len() returns u64, which matches the callers that we have so far. Thoughts? thanks, -- John Hubbard> > On Tue, 2025-11-25 at 17:39 -0800, John Hubbard wrote: >> A tiny simplification: now that FbLayout uses its own specific FbRange >> type, add an FbRange.len() method, and use that to (very slightly) >> simplify the calculation of Frts::frts_size initialization. >> >> Suggested-by: Alexandre Courbot <acourbot at nvidia.com> >> Signed-off-by: John Hubbard <jhubbard at nvidia.com> >> --- >> drivers/gpu/nova-core/fb.rs | 6 ++++++ >> drivers/gpu/nova-core/gsp/boot.rs | 2 +- >> 2 files changed, 7 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/gpu/nova-core/fb.rs b/drivers/gpu/nova-core/fb.rs >> index 333e952400e6..9fcd915e12e1 100644 >> --- a/drivers/gpu/nova-core/fb.rs >> +++ b/drivers/gpu/nova-core/fb.rs >> @@ -100,6 +100,12 @@ pub(crate) fn unregister(&self, bar: &Bar0) { >> >> pub(crate) struct FbRange(Range<u64>); >> >> +impl FbRange { >> + pub(crate) fn len(&self) -> u64 { >> + self.0.end - self.0.start >> + } >> +} >> + >> impl From<Range<u64>> for FbRange { >> fn from(range: Range<u64>) -> Self { >> Self(range) >> diff --git a/drivers/gpu/nova-core/gsp/boot.rs b/drivers/gpu/nova-core/gsp/boot.rs >> index 54937606b5b0..846064221931 100644 >> --- a/drivers/gpu/nova-core/gsp/boot.rs >> +++ b/drivers/gpu/nova-core/gsp/boot.rs >> @@ -70,7 +70,7 @@ fn run_fwsec_frts( >> bios, >> FwsecCommand::Frts { >> frts_addr: fb_layout.frts.start, >> - frts_size: fb_layout.frts.end - fb_layout.frts.start, >> + frts_size: fb_layout.frts.len(), >> }, >> )?; >> >
Alexandre Courbot
2025-Nov-28 05:27 UTC
[PATCH v2 2/5] gpu: nova-core: add FbRange.len() and use it in boot.rs
On Thu Nov 27, 2025 at 9:53 AM JST, John Hubbard wrote:> On 11/26/25 3:43 PM, Lyude Paul wrote: >> I'm not sure this is necessary - for one, we could just use the .len() method >> on the Range. As well - if we just implement Deref for FbRange (which I think >> would be fine here) we could just use .len() through that. > > Hi Lyude! > > Good idea about the deref coercion. It has a minor type mismatch as-is, > though: Range<u64>::len() returns usize, but FbRange::len() returns u64, > which matches the callers that we have so far.It's even worse than that, `Range<u64>::len()` simply doesn't exist. :) `len()` is implemented through `ExactSizeIterator`, which specifies the return type as `usize`. This obviously cannot provide a reliable result when the range is u64, so the implementation was simply not done. See [1] for evidence. But having our own range type lets us slip our own `fn len(&self) -> u64` implementation. [1] https://doc.rust-lang.org/std/ops/struct.Range.html