Nova core will be used to export core functionality to other drivers which will bind to it via auxiliary bus devices. To access methods on the nova-core GPU add an AuxData module and type which will implement the required methods and hold a reference to the associated GPU.
Create an instance of this type when registering the GPU on the auxbus for use as the auxbus registration data. Signed-off-by: Alistair Popple <[email protected]> --- drivers/gpu/nova-core/auxdata.rs | 12 ++++++++ drivers/gpu/nova-core/driver.rs | 45 ++++++++++++++++++++++-------- drivers/gpu/nova-core/gsp/hal.rs | 2 +- drivers/gpu/nova-core/nova_core.rs | 1 + 4 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 drivers/gpu/nova-core/auxdata.rs diff --git a/drivers/gpu/nova-core/auxdata.rs b/drivers/gpu/nova-core/auxdata.rs new file mode 100644 index 000000000000..266b5ce4ee89 --- /dev/null +++ b/drivers/gpu/nova-core/auxdata.rs @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Nova-core auxbus data. Contains all the methods used by the auxbus drivers +//! to interact with nova-core. + +use crate::gpu::Gpu; + +/// Auxiliary bus registration data. Used by the auxbus drivers to call methods on +/// the GPU. +pub struct AuxData<'bound> { + pub(crate) _gpu: &'bound Gpu<'bound>, +} diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs index 5738d4ac521b..ad232ca11833 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -18,6 +18,7 @@ types::ForLt, }; +use crate::auxdata::AuxData; use crate::gpu::Gpu; /// Counter for generating unique auxiliary device IDs. @@ -25,11 +26,14 @@ #[pin_data] pub(crate) struct NovaCore<'bound> { + // Fields are dropped in declaration order: unregister the auxiliary + // device before dropping `gpu`, and drop `gpu` before `bar` because `Gpu` + // borrows `bar`. + #[allow(clippy::type_complexity)] + _reg: auxiliary::Registration<'bound, ForLt!(AuxData<'_>)>, #[pin] pub(crate) gpu: Gpu<'bound>, bar: pci::Bar<'bound, BAR0_SIZE>, - #[allow(clippy::type_complexity)] - _reg: auxiliary::Registration<'bound, ForLt!(())>, } pub(crate) struct NovaCoreDriver; @@ -78,7 +82,7 @@ fn probe<'bound>( pdev.enable_device_mem()?; pdev.set_master(); - Ok(try_pin_init!(NovaCore { + Ok(try_pin_init!(&this in NovaCore { bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?, // TODO: Use `&bar` self-referential pin-init syntax once available. // @@ -86,15 +90,32 @@ fn probe<'bound>( // (`try_pin_init!()` initializes fields in declaration order), lives at a pinned // stable address, and is dropped after `gpu` (struct field drop order). gpu <- Gpu::new(pdev, unsafe { &*core::ptr::from_ref(bar) }), - _reg: auxiliary::Registration::new( - pdev.as_ref(), - c"nova-drm", - // TODO[XARR]: Use XArray or perhaps IDA for proper ID allocation/recycling. For - // now, use a simple atomic counter that never recycles IDs. - AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed), - crate::MODULE_NAME, - (), - )?, + + // SAFETY: + // - `NovaCore` is dropped when the device is unbound; i.e. + // `mem::forget()` is never called on it. + // - `gpu` is initialized above, lives at a pinned stable + // address, and is dropped after `_reg` (struct field drop + // order). + _reg: unsafe { + auxiliary::Registration::new_with_lt( + pdev.as_ref(), + c"nova-drm", + // TODO[XARR]: Use XArray or perhaps IDA for proper ID allocation/recycling. + // For now, use a simple atomic counter that never recycles IDs. + AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed), + crate::MODULE_NAME, + AuxData { + // TODO: Use `&gpu` self-referential pin-init syntax once available. + // + // SAFETY: `this.gpu` is initialized before this expression is evaluated + // (`try_pin_init!()` initializes fields in declaration order), lives at + // a pinned stable address, and is dropped after `_reg` (struct field + // drop order). + _gpu: &*core::ptr::from_ref(&this.as_ref().gpu), + }, + )? + }, })) }) } diff --git a/drivers/gpu/nova-core/gsp/hal.rs b/drivers/gpu/nova-core/gsp/hal.rs index d3e47ef206de..06647b2b7894 100644 --- a/drivers/gpu/nova-core/gsp/hal.rs +++ b/drivers/gpu/nova-core/gsp/hal.rs @@ -36,7 +36,7 @@ /// The GSP unload code might run in a situation where we cannot load firmware dynamically (e.g. /// because we are in shutdown and the file system is not accessible anymore). Thus, the firmware /// required for unloading is prepared at load time, and stored here until it needs to be run. -pub(super) trait UnloadBundle: Send { +pub(super) trait UnloadBundle: Send + Sync { /// Performs the steps required to properly reset the GSP after it has been stopped. fn run( &self, diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nova_core.rs index 735b8e17c6b6..e463f315635f 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -13,6 +13,7 @@ #[macro_use] mod bitfield; +pub mod auxdata; mod driver; mod falcon; mod fb; -- 2.54.0
