[PATCH v12 5/5] sample: rust: pci: add tests for config space routines

Zhi Wang posted 5 patches 2 weeks, 4 days ago
[PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Zhi Wang 2 weeks, 4 days ago
Add tests exercising the PCI configuration space helpers.

Suggested-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Zhi Wang <zhiw@nvidia.com>
---
 samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
index 38c949efce38..1bc5bd1a8df5 100644
--- a/samples/rust/rust_driver_pci.rs
+++ b/samples/rust/rust_driver_pci.rs
@@ -5,6 +5,7 @@
 //! To make this driver probe, QEMU must be run with `-device pci-testdev`.
 
 use kernel::{
+    device::Bound,
     device::Core,
     devres::Devres,
     io::Io,
@@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
 
         Ok(bar.read32(Regs::COUNT))
     }
+
+    fn config_space(pdev: &pci::Device<Bound>) -> Result {
+        let config = pdev.config_space()?;
+
+        // TODO: use the register!() macro for defining PCI configuration space registers once it
+        // has been move out of nova-core.
+        dev_info!(
+            pdev.as_ref(),
+            "pci-testdev config space read8 rev ID: {:x}\n",
+            config.read8(0x8)
+        );
+
+        dev_info!(
+            pdev.as_ref(),
+            "pci-testdev config space read16 vendor ID: {:x}\n",
+            config.read16(0)
+        );
+
+        dev_info!(
+            pdev.as_ref(),
+            "pci-testdev config space read32 BAR 0: {:x}\n",
+            config.read32(0x10)
+        );
+
+        Ok(())
+    }
 }
 
 impl pci::Driver for SampleDriver {
@@ -96,6 +123,7 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> impl PinInit<Self, Er
                         "pci-testdev data-match count: {}\n",
                         Self::testdev(info, bar)?
                     );
+                    Self::config_space(pdev)?;
                 },
                 pdev: pdev.into(),
             }))
-- 
2.51.0
Re: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Gary Guo 2 weeks, 4 days ago
On Wed Jan 21, 2026 at 8:22 PM GMT, Zhi Wang wrote:
> Add tests exercising the PCI configuration space helpers.
> 
> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> Signed-off-by: Zhi Wang <zhiw@nvidia.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
Re: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Alexandre Courbot 2 weeks, 4 days ago
On Thu Jan 22, 2026 at 5:22 AM JST, Zhi Wang wrote:
> Add tests exercising the PCI configuration space helpers.
>
> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
> ---
>  samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
>
> diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
> index 38c949efce38..1bc5bd1a8df5 100644
> --- a/samples/rust/rust_driver_pci.rs
> +++ b/samples/rust/rust_driver_pci.rs
> @@ -5,6 +5,7 @@
>  //! To make this driver probe, QEMU must be run with `-device pci-testdev`.
>  
>  use kernel::{
> +    device::Bound,
>      device::Core,
>      devres::Devres,
>      io::Io,
> @@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
>  
>          Ok(bar.read32(Regs::COUNT))
>      }
> +
> +    fn config_space(pdev: &pci::Device<Bound>) -> Result {
> +        let config = pdev.config_space()?;
> +
> +        // TODO: use the register!() macro for defining PCI configuration space registers once it

I'll rebase the `register!` series on top of this and try to address
this item.

For now,

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Alexandre Courbot 2 weeks ago
On Thu Jan 22, 2026 at 1:31 PM JST, Alexandre Courbot wrote:
> On Thu Jan 22, 2026 at 5:22 AM JST, Zhi Wang wrote:
>> Add tests exercising the PCI configuration space helpers.
>>
>> Suggested-by: Danilo Krummrich <dakr@kernel.org>
>> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
>> ---
>>  samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
>>  1 file changed, 28 insertions(+)
>>
>> diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
>> index 38c949efce38..1bc5bd1a8df5 100644
>> --- a/samples/rust/rust_driver_pci.rs
>> +++ b/samples/rust/rust_driver_pci.rs
>> @@ -5,6 +5,7 @@
>>  //! To make this driver probe, QEMU must be run with `-device pci-testdev`.
>>  
>>  use kernel::{
>> +    device::Bound,
>>      device::Core,
>>      devres::Devres,
>>      io::Io,
>> @@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
>>  
>>          Ok(bar.read32(Regs::COUNT))
>>      }
>> +
>> +    fn config_space(pdev: &pci::Device<Bound>) -> Result {
>> +        let config = pdev.config_space()?;
>> +
>> +        // TODO: use the register!() macro for defining PCI configuration space registers once it
>
> I'll rebase the `register!` series on top of this and try to address
> this item.

Actually... is this expected to work?

> +        dev_info!(
> +            pdev.as_ref(),
> +            "pci-testdev config space read8 rev ID: {:x}\n",
> +            config.read8(0x8)
> +        );

We are performing I/O on `config`, but since this is a sample device
with no hardware backing, what is providing the data for the registers?
Re: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Zhi Wang 2 weeks ago
On Mon, 26 Jan 2026 14:08:30 +0900
"Alexandre Courbot" <acourbot@nvidia.com> wrote:

> On Thu Jan 22, 2026 at 1:31 PM JST, Alexandre Courbot wrote:
> > On Thu Jan 22, 2026 at 5:22 AM JST, Zhi Wang wrote:
> >> Add tests exercising the PCI configuration space helpers.
> >>
> >> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> >> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
> >> ---
> >>  samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
> >>  1 file changed, 28 insertions(+)
> >>
> >> diff --git a/samples/rust/rust_driver_pci.rs
> >> b/samples/rust/rust_driver_pci.rs index 38c949efce38..1bc5bd1a8df5
> >> 100644 --- a/samples/rust/rust_driver_pci.rs
> >> +++ b/samples/rust/rust_driver_pci.rs
> >> @@ -5,6 +5,7 @@
> >>  //! To make this driver probe, QEMU must be run with `-device
> >> pci-testdev`. 
> >>  use kernel::{
> >> +    device::Bound,
> >>      device::Core,
> >>      devres::Devres,
> >>      io::Io,
> >> @@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) ->
> >> Result<u32> { 
> >>          Ok(bar.read32(Regs::COUNT))
> >>      }
> >> +
> >> +    fn config_space(pdev: &pci::Device<Bound>) -> Result {
> >> +        let config = pdev.config_space()?;
> >> +
> >> +        // TODO: use the register!() macro for defining PCI
> >> configuration space registers once it
> >
> > I'll rebase the `register!` series on top of this and try to address
> > this item.
> 
> Actually... is this expected to work?
> 
> > +        dev_info!(
> > +            pdev.as_ref(),
> > +            "pci-testdev config space read8 rev ID: {:x}\n",
> > +            config.read8(0x8)
> > +        );
> 
> We are performing I/O on `config`, but since this is a sample device
> with no hardware backing, what is providing the data for the
> registers?
> 

We are running the sample driver in the virtual machine. QEMU provides
a emulated PCI test device (-device pci-testdev) for this purpose. E.g.
with the following command line, I am able to run and test this in the
virtual machine.

qemu-system-x86_64 \
        -enable-kvm \
        -cpu host \
        -m 8192 \
        -smp 24 \
        -drive file=/home/inno/vm/xubuntu.img,if=virtio \
        -netdev user,id=net0 \
        -device virtio-net-pci,netdev=net0 \
        -device usb-tablet \
        -device pci-testdev \
        -usb
Re: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Alexandre Courbot 2 weeks ago
On Mon Jan 26, 2026 at 6:05 PM JST, Zhi Wang wrote:
> On Mon, 26 Jan 2026 14:08:30 +0900
> "Alexandre Courbot" <acourbot@nvidia.com> wrote:
>
>> On Thu Jan 22, 2026 at 1:31 PM JST, Alexandre Courbot wrote:
>> > On Thu Jan 22, 2026 at 5:22 AM JST, Zhi Wang wrote:
>> >> Add tests exercising the PCI configuration space helpers.
>> >>
>> >> Suggested-by: Danilo Krummrich <dakr@kernel.org>
>> >> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
>> >> ---
>> >>  samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
>> >>  1 file changed, 28 insertions(+)
>> >>
>> >> diff --git a/samples/rust/rust_driver_pci.rs
>> >> b/samples/rust/rust_driver_pci.rs index 38c949efce38..1bc5bd1a8df5
>> >> 100644 --- a/samples/rust/rust_driver_pci.rs
>> >> +++ b/samples/rust/rust_driver_pci.rs
>> >> @@ -5,6 +5,7 @@
>> >>  //! To make this driver probe, QEMU must be run with `-device
>> >> pci-testdev`. 
>> >>  use kernel::{
>> >> +    device::Bound,
>> >>      device::Core,
>> >>      devres::Devres,
>> >>      io::Io,
>> >> @@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) ->
>> >> Result<u32> { 
>> >>          Ok(bar.read32(Regs::COUNT))
>> >>      }
>> >> +
>> >> +    fn config_space(pdev: &pci::Device<Bound>) -> Result {
>> >> +        let config = pdev.config_space()?;
>> >> +
>> >> +        // TODO: use the register!() macro for defining PCI
>> >> configuration space registers once it
>> >
>> > I'll rebase the `register!` series on top of this and try to address
>> > this item.
>> 
>> Actually... is this expected to work?
>> 
>> > +        dev_info!(
>> > +            pdev.as_ref(),
>> > +            "pci-testdev config space read8 rev ID: {:x}\n",
>> > +            config.read8(0x8)
>> > +        );
>> 
>> We are performing I/O on `config`, but since this is a sample device
>> with no hardware backing, what is providing the data for the
>> registers?
>> 
>
> We are running the sample driver in the virtual machine. QEMU provides
> a emulated PCI test device (-device pci-testdev) for this purpose. E.g.
> with the following command line, I am able to run and test this in the
> virtual machine.
>
> qemu-system-x86_64 \
>         -enable-kvm \
>         -cpu host \
>         -m 8192 \
>         -smp 24 \
>         -drive file=/home/inno/vm/xubuntu.img,if=virtio \
>         -netdev user,id=net0 \
>         -device virtio-net-pci,netdev=net0 \
>         -device usb-tablet \
>         -device pci-testdev \
>         -usb

Ah, that makes sense! Thanks for the explanation.
Re: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines
Posted by Zhi Wang 2 weeks ago
On Mon, 26 Jan 2026 09:05:31 +0000
Zhi Wang <zhiw@nvidia.com> wrote:

> On Mon, 26 Jan 2026 14:08:30 +0900
> "Alexandre Courbot" <acourbot@nvidia.com> wrote:
> 
> > On Thu Jan 22, 2026 at 1:31 PM JST, Alexandre Courbot wrote:
> > > On Thu Jan 22, 2026 at 5:22 AM JST, Zhi Wang wrote:
> > >> Add tests exercising the PCI configuration space helpers.
> > >>
> > >> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> > >> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
> > >> ---
> > >>  samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++
> > >>  1 file changed, 28 insertions(+)
> > >>
> > >> diff --git a/samples/rust/rust_driver_pci.rs
> > >> b/samples/rust/rust_driver_pci.rs index 38c949efce38..1bc5bd1a8df5
> > >> 100644 --- a/samples/rust/rust_driver_pci.rs
> > >> +++ b/samples/rust/rust_driver_pci.rs
> > >> @@ -5,6 +5,7 @@
> > >>  //! To make this driver probe, QEMU must be run with `-device
> > >> pci-testdev`. 
> > >>  use kernel::{
> > >> +    device::Bound,
> > >>      device::Core,
> > >>      devres::Devres,
> > >>      io::Io,
> > >> @@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) ->
> > >> Result<u32> { 
> > >>          Ok(bar.read32(Regs::COUNT))
> > >>      }
> > >> +
> > >> +    fn config_space(pdev: &pci::Device<Bound>) -> Result {
> > >> +        let config = pdev.config_space()?;
> > >> +
> > >> +        // TODO: use the register!() macro for defining PCI
> > >> configuration space registers once it
> > >
> > > I'll rebase the `register!` series on top of this and try to address
> > > this item.
> > 
> > Actually... is this expected to work?
> > 
> > > +        dev_info!(
> > > +            pdev.as_ref(),
> > > +            "pci-testdev config space read8 rev ID: {:x}\n",
> > > +            config.read8(0x8)
> > > +        );
> > 
> > We are performing I/O on `config`, but since this is a sample device
> > with no hardware backing, what is providing the data for the
> > registers?
> > 
> 
> We are running the sample driver in the virtual machine. QEMU provides
> a emulated PCI test device (-device pci-testdev) for this purpose. E.g.
> with the following command line, I am able to run and test this in the
> virtual machine.
> 
> qemu-system-x86_64 \
>         -enable-kvm \
>         -cpu host \
>         -m 8192 \
>         -smp 24 \
>         -drive file=/home/inno/vm/xubuntu.img,if=virtio \
>         -netdev user,id=net0 \
>         -device virtio-net-pci,netdev=net0 \
>         -device usb-tablet \
>         -device pci-testdev \
>         -usb

The dmesg in the VM:

[    2.762517] rust_driver_pci 0000:00:04.0: pci-testdev data-match count:1 
[    2.764368] rust_driver_pci 0000:00:04.0: pci-testdev config space
read8 rev ID: 0 
[    2.766478] rust_driver_pci 0000:00:04.0: pci-testdev
config space read16 vendor ID: 1b36
[    2.770523] rust_driver_pci
0000:00:04.0: pci-testdev config space read32 BAR 0: febd2000

Z.