gpu: nova-core: register definitions and basic timer and falcon devices
Subsystems (5)
Alexandre Courbot
maintainerv2 cover gpu: nova-core: register definitions and basic timer and falcon devices
Hi everyone,
This RFC is based on top of Danilo's initial driver stub series v4 [1] and adds very basic support for the Timer and Falcon devices, in order to see and "feel" the proposed register access abstractions and discuss them before moving forward with GSP initialization.
It is kept simple and short on purpose, to avoid bumping into a wall with much more device code because my assumptions were incorrect.
The main addition is the nv_reg!() register definition macro, which aims at providing safe and convenient access to all useful registers and their fields. I elaborate on its definition in the patch that introduces it ; it is also probably better to look at all the register definitions to understand how it can be used, and the services it provides. Right now it provides accessors and builders for all the fields of a register. It will probably need to be extended with more operations as we deem them useful.
The timer device has not changed much from v1, with the exception of having its own Timestamp type to easily obtain Durations between two samples.
The falcon implementation is still super incomplete, and just designed to illustrate how the register macros can be used. I have more progress in a private branch, but want to keep the focus on the nv_reg!() macro for this review since the rest will ultimately depend on it.
It would be charitable to say that my Rust macro skills are lacking ; so please point out any deficiency in its definition. I am also not entirely sure about the syntax for register definition - I would like to keep things simple and close to OpenRM (notably for the mask definitions) to make it easier to port definition from it into Nova.
[1] https://lore.kernel.org/nouveau/[email protected]/T/
---
Changes in v2:
- Don't hold the Bar guard in methods that can sleep.
- Added a Timestamp type for Timer to safely and easily get durations between two measurements.
- Added a macro to make register definitions easier.
- Added a very basic falcon implementation to define more registers and exercise the register definition macro.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alexandre Courbot (5):
rust: add useful ops for u64
rust: make ETIMEDOUT error available
gpu: nova-core: add register definition macro
gpu: nova-core: add basic timer device
gpu: nova-core: add falcon register definitions and probe code
drivers/gpu/nova-core/driver.rs | 4 +- drivers/gpu/nova-core/falcon.rs | 124 +++++++++++++++ drivers/gpu/nova-core/gpu.rs | 70 ++++++++- drivers/gpu/nova-core/nova_core.rs | 2 + drivers/gpu/nova-core/regs.rs | 311 ++++++++++++++++++++++++++++++++----- drivers/gpu/nova-core/timer.rs | 124 +++++++++++++++ rust/kernel/error.rs | 1 + rust/kernel/lib.rs | 1 + rust/kernel/num.rs | 43 +++++ 9 files changed, 639 insertions(+), 41 deletions(-)
---
base-commit: 3ac10b625b709d59556cd2c1bf8a009c2bfdbefc
change-id: 20250216-nova_timer-c69430184f54
Best regards,
Signature · 1 line
Alexandre Courbot <[email protected]>
Alexandre Courbot
maintainerRFC PATCH v2 1/5 rust: add useful ops for u64
It is common to build a u64 from its high and low parts obtained from two 32-bit registers. Conversely, it is also common to split a u64 into two u32s to write them into registers. Add an extension trait for u64 that implement these methods in a new num module.
It is expected that this trait will be extended with other useful operations, and similar extension traits implemented for other types.
--- rust/kernel/lib.rs | 1 + rust/kernel/num.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)
rust/kernel/lib.rs +1 -0
@@ -61,6 +61,7 @@pub mod miscdevice;#[cfg( CONFIG_NET )] pub mod net; +pub mod num;pub mod of;pub mod page;#[cfg( CONFIG_PCI )]
rust/kernel/num.rs new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH RFC v2 1/5] rust: add useful ops for u64
Quoted 10 lines · Alexandre Courbot
rust/kernel/num.rs new file +7
-0+/// Useful operations for `u64`. +pub trait U64Ext { + /// Build a `u64` by combining its `high` and `low` parts. + /// + /// ``` + /// use kernel::num::U64Ext; + /// assert_eq!(u64::from_u32s(0x01234567, 0x89abcdef),0x01234567_89abcdef);
+ /// ```
+ fnfrom_u32s(high: u32, low: u32)-> Self;
rust/kernel/num.rs line 17
Quoted 4 lines
rust/kernel/num.rs new file +4
-0+ + fn upper_32_bits(self) -> u32; + fn lower_32_bits(self) -> u32; +}
This generates a few warnings when I build it:
RUSTC L rust/kernel.o
warning: missing documentation for a method
--> rust/kernel/num.rs:15:5
| 15 | fn upper_32_bits(self) -> u32; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
= note: requested on the command line with -W missing-docs
warning: missing documentation for a method
--> rust/kernel/num.rs:16:5
| 16 | fn lower_32_bits(self) -> u32; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: missing documentation for a function
--> rust/kernel/num.rs:33:1
|
33 | pub const fn upper_32_bits(v: u64) -> u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^warning: missing documentation for a function
--> rust/kernel/num.rs:37:1
|
37 | pub const fn lower_32_bits(v: u64) -> u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^warning: missing documentation for a function
--> rust/kernel/num.rs:41:1
|
41 | pub const fn u32s_to_u64(high: u32, low: u32) -> u64 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^warning: 5 warnings emitted
Alexandre Courbot
maintainerRFC PATCH v2 2/5 rust: make ETIMEDOUT error available
In reply to Alexandre Courbot--- rust/kernel/error.rs | 1 + 1 file changed, 1 insertion(+)
rust/kernel/error.rs +1 -0
@@ -65,6 +65,7 @@ macro_rules! declare_err {declare_err! (EDOM ,"Math argument out of domain of func." );declare_err! (ERANGE ,"Math result not representable." );declare_err! (EOVERFLOW ,"Value too large for defined data type." ); +declare_err! (ETIMEDOUT ,"Connection timed out." );declare_err! (ERESTARTSYS ,"Restart the system call." );declare_err! (ERESTARTNOINTR ,"System call was interrupted by a signal and will be restarted." );declare_err! (ERESTARTNOHAND ,"Restart if no handler." );
Signature · 1 line
2.48.1
Alexandre Courbot
maintainerRFC PATCH v2 3/5 gpu: nova-core: add register definition macro
In reply to Alexandre CourbotRegister data manipulation is one of the error-prone areas of a kernel driver. It is particularly easy to mix addresses of registers, masks and shifts of fields, and to proceed with invalid values.
This patch introduces the nv_reg!() macro, which creates a safe type definition for a given register, along with field accessors and value builder. The macro is designed to type the same field ranges as the NVIDIA OpenRM project, to facilitate porting its register definitions to Nova.
Here is for instance the definition of the Boot0 register:
nv_reg!(Boot0@0x00000000, "Basic revision information about the GPU";
3:0 minor_rev as (u8), "minor revision of the chip";
7:4 major_rev as (u8), "major revision of the chip";
25:20 chipset try_into (Chipset), "chipset model");
This definition creates a Boot0 type that includes read() and write() methods that will automatically use the correct register offset (0x0 in this case).
Creating a type for each register lets us leverage the type system to make sure register values don't get mix up.
It also allows us to create register-specific field extractor methods (here minor_rev(), major_rev(), and chipset()) that present each field in a convenient way and validate its data if relevant. The chipset() accessor, in particular, uses the TryFrom<u32> implementation of Chipset to build a Chipset instance and returns its associated error type if the conversion has failed because of an invalid value.
The ending string at the end of each line is optional, and expands to doc comments for the type itself, or each of the field accessors.
--- drivers/gpu/nova-core/gpu.rs | 2 +- drivers/gpu/nova-core/regs.rs | 195 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 158 insertions(+), 39 deletions(-)
drivers/gpu/nova-core/gpu.rs +1 -1
@@ -164,7 +164,7 @@ fn new(bar: &Devres<Bar0>) -> Result<Spec> {let boot0 = regs::Boot0 ::read (&bar);Ok (Self { - chipset: boot0.chipset ().try_into ()?, + chipset: boot0.chipset ()?, revision:Revision ::from_boot0 (boot0), }) }
drivers/gpu/nova-core/regs.rs +157 -38
212 lines not on this page · raw message
Signature · 1 line
2.48.1
Alexandre Courbot
maintainerRFC PATCH v2 4/5 gpu: nova-core: add basic timer device
In reply to Alexandre CourbotAdd a basic timer device and exercise it during device probing. This first draft is probably very questionable.
One point in particular which should IMHO receive attention: the generic wait_on() method aims at providing similar functionality to Nouveau's nvkm_[num]sec() macros. Since this method will be heavily used with different conditions to test, I'd like to avoid monomorphizing it entirely with each instance ; that's something that is achieved in nvkm_xsec() using functions that the macros invoke.
I have tried achieving the same result in Rust using closures (kept as-is in the current code), but they seem to be monomorphized as well. Calling extra functions could work better, but looks also less elegant to me, so I am really open to suggestions here.
--- drivers/gpu/nova-core/driver.rs | 4 +- drivers/gpu/nova-core/gpu.rs | 58 ++++++++++++++++- drivers/gpu/nova-core/nova_core.rs | 1 + drivers/gpu/nova-core/regs.rs | 8 +++ drivers/gpu/nova-core/timer.rs | 124 +++++++++++++++++++++++++++++++++++++ 5 files changed, 193 insertions(+), 2 deletions(-)
drivers/gpu/nova-core/driver.rs +3 -1
@@ -10,7 +10,7 @@ pub(crate) struct NovaCore { pub(crate) gpu: Gpu, } -const BAR0_SIZE :usize =8 ; +const BAR0_SIZE :usize =0x9500 ;pub (crate )type Bar0 = pci::Bar <BAR0_SIZE >; kernel::pci_device_table!(
@@ -42,6 +42,8 @@ fn probe(pdev: &mut pci::Device, _info: &Self::IdInfo) -> Result<Pin<KBox<Self>> GFP_KERNEL, )?; +let _ = this.gpu.test_timer (); +Ok (this) } }
drivers/gpu/nova-core/gpu.rs +57 -1
85 lines not on this page · raw message
drivers/gpu/nova-core/nova_core.rs +1 -0
@@ -6,6 +6,7 @@mod firmware;mod gpu;mod regs; +mod timer; kernel::module_pci_driver! {type :driver ::NovaCore ,
drivers/gpu/nova-core/regs.rs +8 -0
@@ -172,3 +172,11 @@ impl Builder<$name> { 7:4 major_rev as (u8), "major revision of the chip"; 25:20 chipset try_into (Chipset), "chipset model" ); + +nv_reg! (PtimerTime0 @0x00009400 ; +31 :0 loas (u32 ),"low 32-bits of the timer" +); + +nv_reg! (PtimerTime1 @0x00009410 ; +31 :0 hias (u32 ),"high 32 bits of the timer" +);
drivers/gpu/nova-core/timer.rs new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
2.48.1
Alexandre Courbot
maintainerRFC PATCH v2 5/5 gpu: nova-core: add falcon register definitions and probe code
In reply to Alexandre CourbotThis is still very preliminary work, and is mostly designed to show how register fields can be turned into safe types that force us to handle invalid values.
--- drivers/gpu/nova-core/driver.rs | 2 +- drivers/gpu/nova-core/falcon.rs | 124 +++++++++++++++++++++++++++++++++++++ drivers/gpu/nova-core/gpu.rs | 10 +++ drivers/gpu/nova-core/nova_core.rs | 1 + drivers/gpu/nova-core/regs.rs | 108 ++++++++++++++++++++++++++++++++ 5 files changed, 244 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/driver.rs +1 -1
@@ -10,7 +10,7 @@ pub(crate) struct NovaCore { pub(crate) gpu: Gpu, } -const BAR0_SIZE :usize =0x9500 ; +const BAR0_SIZE :usize =0x1000000 ;pub (crate )type Bar0 = pci::Bar <BAR0_SIZE >; kernel::pci_device_table!(
drivers/gpu/nova-core/falcon.rs new file +124 -0
124 lines not on this page · raw message
drivers/gpu/nova-core/gpu.rs +10 -0
@@ -7,6 +7,7 @@ };use crate ::driver::Bar0 ; +use crate ::falcon::Falcon ;use crate ::regs;use crate ::timer::Timer ;use core::fmt;
@@ -228,6 +229,15 @@ pub(crate) fn new(pdev: &pci::Device, bar: Devres<Bar0>) -> Result<impl PinInit<let dev = pdev.as_ref ().into ();let timer =Timer ::new (); +let gsp_falcon =Falcon ::new (pdev, &bar, regs::FALCON_GSP_BASE )?; +let sec2 =Falcon ::new (pdev, &bar, regs::FALCON_SEC2_BASE )?; +let b = bar.try_access ().ok_or (ENXIO )?; +dev_info! ( + pdev.as_ref (), +"GSP Falcon CpuCtl: {:?}" , + gsp_falcon.cpu_ctl (&b) + ); +dev_info! (pdev.as_ref (),"SEC2 Falcon CpuCtl: {:?}" , sec2.cpu_ctl (&b)); Ok(pin_init!(Self { dev,
drivers/gpu/nova-core/nova_core.rs +1 -0
@@ -3,6 +3,7 @@//! Nova Core GPU Driver mod driver; +mod falcon;mod firmware;mod gpu;mod regs;
drivers/gpu/nova-core/regs.rs +108 -0
117 lines not on this page · raw message
Signature · 1 line
2.48.1