Alexandre Courbot
2025-Sep-03 07:08 UTC
[PATCH v3 02/11] gpu: nova-core: move GSP boot code out of `Gpu` constructor
On Wed Sep 3, 2025 at 4:53 AM JST, Danilo Krummrich wrote:> On Tue Sep 2, 2025 at 4:31 PM CEST, Alexandre Courbot wrote: >> diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs >> index 274989ea1fb4a5e3e6678a08920ddc76d2809ab2..1062014c0a488e959379f009c2e8029ffaa1e2f8 100644 >> --- a/drivers/gpu/nova-core/driver.rs >> +++ b/drivers/gpu/nova-core/driver.rs >> @@ -6,6 +6,8 @@ >> >> #[pin_data] >> pub(crate) struct NovaCore { >> + // Placeholder for the real `Gsp` object once it is built. >> + pub(crate) gsp: (), >> #[pin] >> pub(crate) gpu: Gpu, >> _reg: auxiliary::Registration, >> @@ -40,8 +42,14 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self >> )?; >> >> let this = KBox::pin_init( >> - try_pin_init!(Self { >> + try_pin_init!(&this in Self { >> gpu <- Gpu::new(pdev, bar)?, >> + gsp <- { >> + // SAFETY: `this.gpu` is initialized to a valid value. >> + let gpu = unsafe { &(*this.as_ptr()).gpu }; >> + >> + gpu.start_gsp(pdev)? >> + }, > > Please use pin_chain() [1] for this.Sorry, but I couldn't figure out how I can use pin_chain here (and couldn't find any relevant example in the kernel code either). Can you elaborate a bit?> > More in general, unsafe code should be the absolute last resort. If we add new > unsafe code I'd love to see a comment justifying why there's no other way than > using unsafe code for this, as we agreed in [2]. > > I did a quick grep on this series and I see 21 occurrences of "unsafe", if I > substract the ones for annotations and for FromBytes impls, it's still 9 new > ones. :( > > Do we really need all of them?I've counted 16 uses of `unsafe`. :) - 3 in the bindgen-generated code (these can't be avoided), - 7 to implement `FromBytes`, - 1 to work around the fact that `FromBytes` doesn't work on slices yet (maybe that one can be removed) - 5 as a result of intra-dependencies in PinInit initializers (which we might be able to remove if I figure out how to use `pin_chain`). So best-case scenario would be that we will be down to 10 that are truly unavoidable.
Alexandre Courbot
2025-Sep-03 07:21 UTC
[PATCH v3 02/11] gpu: nova-core: move GSP boot code out of `Gpu` constructor
On Wed Sep 3, 2025 at 4:08 PM JST, Alexandre Courbot wrote:> On Wed Sep 3, 2025 at 4:53 AM JST, Danilo Krummrich wrote: >> On Tue Sep 2, 2025 at 4:31 PM CEST, Alexandre Courbot wrote: >>> diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs >>> index 274989ea1fb4a5e3e6678a08920ddc76d2809ab2..1062014c0a488e959379f009c2e8029ffaa1e2f8 100644 >>> --- a/drivers/gpu/nova-core/driver.rs >>> +++ b/drivers/gpu/nova-core/driver.rs >>> @@ -6,6 +6,8 @@ >>> >>> #[pin_data] >>> pub(crate) struct NovaCore { >>> + // Placeholder for the real `Gsp` object once it is built. >>> + pub(crate) gsp: (), >>> #[pin] >>> pub(crate) gpu: Gpu, >>> _reg: auxiliary::Registration, >>> @@ -40,8 +42,14 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self >>> )?; >>> >>> let this = KBox::pin_init( >>> - try_pin_init!(Self { >>> + try_pin_init!(&this in Self { >>> gpu <- Gpu::new(pdev, bar)?, >>> + gsp <- { >>> + // SAFETY: `this.gpu` is initialized to a valid value. >>> + let gpu = unsafe { &(*this.as_ptr()).gpu }; >>> + >>> + gpu.start_gsp(pdev)? >>> + }, >> >> Please use pin_chain() [1] for this. > > Sorry, but I couldn't figure out how I can use pin_chain here (and > couldn't find any relevant example in the kernel code either). Can you > elaborate a bit?To be more specific on what I don't get: I see how pin_chain could be used to initialize a structure which dependent member can take a temporary value (like a pointer set to `null`), but in this case `gsp` must be initialized with the result of `start_gsp`, and there is no "default" valid value for it meanwhile (I use `()` as its type, but it is a temporary placeholder). But maybe I am just misunderstanding something about how `pin_chain` can be used.
Danilo Krummrich
2025-Sep-03 08:26 UTC
[PATCH v3 02/11] gpu: nova-core: move GSP boot code out of `Gpu` constructor
On Wed Sep 3, 2025 at 9:08 AM CEST, Alexandre Courbot wrote:> On Wed Sep 3, 2025 at 4:53 AM JST, Danilo Krummrich wrote: >> On Tue Sep 2, 2025 at 4:31 PM CEST, Alexandre Courbot wrote: >>> diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs >>> index 274989ea1fb4a5e3e6678a08920ddc76d2809ab2..1062014c0a488e959379f009c2e8029ffaa1e2f8 100644 >>> --- a/drivers/gpu/nova-core/driver.rs >>> +++ b/drivers/gpu/nova-core/driver.rs >>> @@ -6,6 +6,8 @@ >>> >>> #[pin_data] >>> pub(crate) struct NovaCore { >>> + // Placeholder for the real `Gsp` object once it is built. >>> + pub(crate) gsp: (), >>> #[pin] >>> pub(crate) gpu: Gpu, >>> _reg: auxiliary::Registration, >>> @@ -40,8 +42,14 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self >>> )?; >>> >>> let this = KBox::pin_init( >>> - try_pin_init!(Self { >>> + try_pin_init!(&this in Self { >>> gpu <- Gpu::new(pdev, bar)?, >>> + gsp <- { >>> + // SAFETY: `this.gpu` is initialized to a valid value. >>> + let gpu = unsafe { &(*this.as_ptr()).gpu }; >>> + >>> + gpu.start_gsp(pdev)? >>> + }, >> >> Please use pin_chain() [1] for this. > > Sorry, but I couldn't figure out how I can use pin_chain here (and > couldn't find any relevant example in the kernel code either). Can you > elaborate a bit?I thought of just doing the following, which I think should be equivalent (diff against current nova-next). diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs index 274989ea1fb4..6d62867f7503 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -41,7 +41,9 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self let this = KBox::pin_init( try_pin_init!(Self { - gpu <- Gpu::new(pdev, bar)?, + gpu <- Gpu::new(pdev, bar)?.pin_chain(|gpu| { + gpu.start_gsp(pdev) + }), _reg: auxiliary::Registration::new( pdev.as_ref(), c_str!("nova-drm"), diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs index 8caecaf7dfb4..211bc1a5a5b3 100644 --- a/drivers/gpu/nova-core/gpu.rs +++ b/drivers/gpu/nova-core/gpu.rs @@ -266,7 +266,7 @@ fn run_fwsec_frts( pub(crate) fn new( pdev: &pci::Device<device::Bound>, devres_bar: Arc<Devres<Bar0>>, - ) -> Result<impl PinInit<Self>> { + ) -> Result<impl PinInit<Self, Error>> { let bar = devres_bar.access(pdev.as_ref())?; let spec = Spec::new(bar)?; let fw = Firmware::new(pdev.as_ref(), spec.chipset, FIRMWARE_VERSION)?; @@ -302,11 +302,16 @@ pub(crate) fn new( Self::run_fwsec_frts(pdev.as_ref(), &gsp_falcon, bar, &bios, &fb_layout)?; - Ok(pin_init!(Self { + Ok(try_pin_init!(Self { spec, bar: devres_bar, fw, sysmem_flush, })) } + + pub(crate) fn start_gsp(&self, _pdev: &pci::Device<device::Core>) -> Result { + // noop + Ok(()) + } } But maybe it doesn't capture your intend?>> >> More in general, unsafe code should be the absolute last resort. If we add new >> unsafe code I'd love to see a comment justifying why there's no other way than >> using unsafe code for this, as we agreed in [2]. >> >> I did a quick grep on this series and I see 21 occurrences of "unsafe", if I >> substract the ones for annotations and for FromBytes impls, it's still 9 new >> ones. :( >> >> Do we really need all of them? > > I've counted 16 uses of `unsafe`. :)I did a grep | wc on the mbox file, so it includes the 5 additional occurrences from the annotations. :) Otherwise the 9 "real" ones I counted seem to match the 3 bindgen ones (fine of course) plus the 5 ones from the pin initializers (we should avoid them).> > - 3 in the bindgen-generated code (these can't be avoided), > - 7 to implement `FromBytes`, > - 1 to work around the fact that `FromBytes` doesn't work on slices yet > (maybe that one can be removed) > - 5 as a result of intra-dependencies in PinInit initializers (which we > might be able to remove if I figure out how to use `pin_chain`). > > So best-case scenario would be that we will be down to 10 that are truly > unavoidable.