mbox series

[v3,00/16] Device / Driver PCI / Platform Rust abstractions

Message ID 20241022213221.2383-1-dakr@kernel.org
Headers show
Series Device / Driver PCI / Platform Rust abstractions | expand

Message

Danilo Krummrich Oct. 22, 2024, 9:31 p.m. UTC
This patch series implements the necessary Rust abstractions to implement
device drivers in Rust.

This includes some basic generalizations for driver registration, handling of ID
tables, MMIO operations and device resource handling.

Those generalizations are used to implement device driver support for two
busses, the PCI and platfrom bus (with OF IDs) in order to provide some evidence
that the generalizations work as intended.

The patch series also includes two patches adding two driver samples, one PCI
driver and one platform driver.

The PCI bits are motivated by the Nova driver project [1], but are used by at
least one more OOT driver (rnvme [2]).

The platform bits, besides adding some more evidence to the base abstractions,
are required by a few more OOT drivers aiming at going upstream, i.e. rvkms [3],
cpufreq-dt [4], asahi [5] and the i2c work from Fabien [6].

The patches of this series can also be [7], [8] and [9].

Changes in v3:
==============
- add commits for `Opaque::try_ffi_init` and `InPlaceModule`
- rename `DriverOps` to `RegistrationOps`
- rework device ID abstractions to get rid of almost all macro magic (thanks to
  Gary Guo for working this out!)
  - this is possible due to recently stabilized language features
- add modpost alias generation for device ID tables
  - unfortunately, this is the part that still requires some macro magic in the
    device ID abstractions
- PCI
  - represent the driver private data with the driver specific `Driver` instance
    and bind it's lifetime to the time of the driver being bound to a device
    - this allows us to handle class / subsystem registrations in a cleaner way
  - get rid of `Driver::remove`
    - Rust drivers should bind cleanup code to the `Drop` implementation of the
      corresponding structure instead and put it into their driver structure for
      automatic cleanup
  - add a sample PCI driver
- add abstractions for `struct of_device_id`
- add abstractions for the platform bus, including a sample driver
- update the MAINTAINERS file accordingly
  - currently this turns out a bit messy, but it should become better once the
    build system supports a treewide distribution of the kernel crate
  - I didn't add myself as maintainer, but (if requested) I'm willing to do so
    and help with maintainance

Changes in v2:
==============
- statically initialize driver structures (Greg)
- move base device ID abstractions to a separate source file (Greg)
- remove `DeviceRemoval` trait in favor of using a `Devres` callback to
  unregister drivers
- remove `device::Data`, we don't need this abstraction anymore now that we
  `Devres` to revoke resources and registrations
- pass the module name to `Module::init` and `InPlaceModule::init` in a separate
  patch
- rework of `Io` including compile time boundary checks (Miguel, Wedson)
- adjust PCI abstractions accordingly and implement a `module_pci_driver!` macro
- rework `pci::Bar` to support a const SIZE
- increase the total amount of Documentation, rephrase some safety comments and
  commit messages for less ambiguity
- fix compilation issues with some documentation examples

[1] https://gitlab.freedesktop.org/drm/nova/-/tree/nova-next
[2] https://github.com/metaspace/linux/tree/rnvme
[3] https://lore.kernel.org/all/20240930233257.1189730-1-lyude@redhat.com/
[4] https://git.kernel.org/pub/scm/linux/kernel/git/vireshk/linux.git/log/?h=rust/cpufreq-dt
[5] https://github.com/AsahiLinux/linux
[6] https://github.com/Fabo/linux/tree/fparent/rust-i2c
[7] https://github.com/Rust-for-Linux/linux/tree/staging/rust-device
[8] https://github.com/Rust-for-Linux/linux/tree/staging/rust-pci
[9] https://github.com/Rust-for-Linux/linux/tree/staging/dev

Danilo Krummrich (11):
  rust: pass module name to `Module::init`
  rust: implement generic driver registration
  rust: implement `IdArray`, `IdTable` and `RawDeviceId`
  rust: add `io::Io` base type
  rust: add devres abstraction
  rust: pci: add basic PCI device / driver abstractions
  rust: pci: implement I/O mappable `pci::Bar`
  samples: rust: add Rust PCI sample driver
  rust: of: add `of::DeviceId` abstraction
  rust: platform: add basic platform device / driver abstractions
  samples: rust: add Rust platform sample driver

Wedson Almeida Filho (5):
  rust: init: introduce `Opaque::try_ffi_init`
  rust: introduce `InPlaceModule`
  rust: add rcu abstraction
  rust: add `Revocable` type
  rust: add `dev_*` print macros.

 MAINTAINERS                          |   8 +
 drivers/block/rnull.rs               |   2 +-
 rust/bindings/bindings_helper.h      |   3 +
 rust/helpers/device.c                |  10 +
 rust/helpers/helpers.c               |   5 +
 rust/helpers/io.c                    |  91 ++++++
 rust/helpers/pci.c                   |  18 ++
 rust/helpers/platform.c              |  13 +
 rust/helpers/rcu.c                   |  13 +
 rust/kernel/device.rs                | 319 +++++++++++++++++++-
 rust/kernel/device_id.rs             | 162 ++++++++++
 rust/kernel/devres.rs                | 180 +++++++++++
 rust/kernel/driver.rs                | 120 ++++++++
 rust/kernel/io.rs                    | 234 +++++++++++++++
 rust/kernel/lib.rs                   |  46 ++-
 rust/kernel/net/phy.rs               |   2 +-
 rust/kernel/of.rs                    |  63 ++++
 rust/kernel/pci.rs                   | 429 +++++++++++++++++++++++++++
 rust/kernel/platform.rs              | 217 ++++++++++++++
 rust/kernel/prelude.rs               |   2 +
 rust/kernel/revocable.rs             | 211 +++++++++++++
 rust/kernel/sync.rs                  |   1 +
 rust/kernel/sync/rcu.rs              |  52 ++++
 rust/kernel/types.rs                 |  20 +-
 rust/macros/module.rs                |  30 +-
 samples/rust/Kconfig                 |  21 ++
 samples/rust/Makefile                |   2 +
 samples/rust/rust_driver_pci.rs      | 109 +++++++
 samples/rust/rust_driver_platform.rs |  62 ++++
 samples/rust/rust_minimal.rs         |   2 +-
 samples/rust/rust_print.rs           |   2 +-
 31 files changed, 2421 insertions(+), 28 deletions(-)
 create mode 100644 rust/helpers/device.c
 create mode 100644 rust/helpers/io.c
 create mode 100644 rust/helpers/pci.c
 create mode 100644 rust/helpers/platform.c
 create mode 100644 rust/helpers/rcu.c
 create mode 100644 rust/kernel/device_id.rs
 create mode 100644 rust/kernel/devres.rs
 create mode 100644 rust/kernel/driver.rs
 create mode 100644 rust/kernel/io.rs
 create mode 100644 rust/kernel/of.rs
 create mode 100644 rust/kernel/pci.rs
 create mode 100644 rust/kernel/platform.rs
 create mode 100644 rust/kernel/revocable.rs
 create mode 100644 rust/kernel/sync/rcu.rs
 create mode 100644 samples/rust/rust_driver_pci.rs
 create mode 100644 samples/rust/rust_driver_platform.rs


base-commit: 15541c9263ce34ff95a06bc68f45d9bc5c990bcd

Comments

Greg KH Oct. 23, 2024, 5:13 a.m. UTC | #1
On Tue, Oct 22, 2024 at 11:31:37PM +0200, Danilo Krummrich wrote:
> This patch series implements the necessary Rust abstractions to implement
> device drivers in Rust.
> 
> This includes some basic generalizations for driver registration, handling of ID
> tables, MMIO operations and device resource handling.
> 
> Those generalizations are used to implement device driver support for two
> busses, the PCI and platfrom bus (with OF IDs) in order to provide some evidence
> that the generalizations work as intended.
> 
> The patch series also includes two patches adding two driver samples, one PCI
> driver and one platform driver.
> 
> The PCI bits are motivated by the Nova driver project [1], but are used by at
> least one more OOT driver (rnvme [2]).
> 
> The platform bits, besides adding some more evidence to the base abstractions,
> are required by a few more OOT drivers aiming at going upstream, i.e. rvkms [3],
> cpufreq-dt [4], asahi [5] and the i2c work from Fabien [6].
> 
> The patches of this series can also be [7], [8] and [9].

Nice!

Thanks for redoing this, at first glance it's much better.  It will be a
few days before I can dive into this, It's conference season and the
travel is rough, so be patient but I will get to this...

thanks,

greg k-h
Danilo Krummrich Oct. 23, 2024, 7:28 a.m. UTC | #2
On Wed, Oct 23, 2024 at 07:13:37AM +0200, Greg KH wrote:
> On Tue, Oct 22, 2024 at 11:31:37PM +0200, Danilo Krummrich wrote:
> > This patch series implements the necessary Rust abstractions to implement
> > device drivers in Rust.
> > 
> > This includes some basic generalizations for driver registration, handling of ID
> > tables, MMIO operations and device resource handling.
> > 
> > Those generalizations are used to implement device driver support for two
> > busses, the PCI and platfrom bus (with OF IDs) in order to provide some evidence
> > that the generalizations work as intended.
> > 
> > The patch series also includes two patches adding two driver samples, one PCI
> > driver and one platform driver.
> > 
> > The PCI bits are motivated by the Nova driver project [1], but are used by at
> > least one more OOT driver (rnvme [2]).
> > 
> > The platform bits, besides adding some more evidence to the base abstractions,
> > are required by a few more OOT drivers aiming at going upstream, i.e. rvkms [3],
> > cpufreq-dt [4], asahi [5] and the i2c work from Fabien [6].
> > 
> > The patches of this series can also be [7], [8] and [9].
> 
> Nice!
> 
> Thanks for redoing this, at first glance it's much better.  It will be a
> few days before I can dive into this, It's conference season and the
> travel is rough, so be patient but I will get to this...

No worries, I'll be also a bit less responsive than usual in the next weeks.

> 
> thanks,
> 
> greg k-h
>
Dirk Behme Oct. 25, 2024, 5:15 a.m. UTC | #3
Hi Danilo,

On 22.10.2024 23:31, Danilo Krummrich wrote:
> This patch series implements the necessary Rust abstractions to implement
> device drivers in Rust.
> 
> This includes some basic generalizations for driver registration, handling of ID
> tables, MMIO operations and device resource handling.
> 
> Those generalizations are used to implement device driver support for two
> busses, the PCI and platfrom bus (with OF IDs) in order to provide some evidence
> that the generalizations work as intended.
> 
> The patch series also includes two patches adding two driver samples, one PCI
> driver and one platform driver.
> 
> The PCI bits are motivated by the Nova driver project [1], but are used by at
> least one more OOT driver (rnvme [2]).
> 
> The platform bits, besides adding some more evidence to the base abstractions,
> are required by a few more OOT drivers aiming at going upstream, i.e. rvkms [3],
> cpufreq-dt [4], asahi [5] and the i2c work from Fabien [6].
> 
> The patches of this series can also be [7], [8] and [9].

Just some minor typos:

-----------------------------------------------------
0004_rust_implement_generic_driver_registration.patch
-----------------------------------------------------
WARNING: 'privide' may be misspelled - perhaps 'provide'?
#63: FILE: rust/kernel/driver.rs:14:
+/// Amba, etc.) to privide the corresponding subsystem specific 
implementation to register /
                     ^^^^^^^
---------------------------------------------------------
0005_rust_implement_idarray_idtable_and_rawdeviceid.patch
---------------------------------------------------------
WARNING: 'offest' may be misspelled - perhaps 'offset'?
#84: FILE: rust/kernel/device_id.rs:39:
+    /// The offest to the context/data field.
              ^^^^^^
-----------------------------------
0009_rust_add_io_io_base_type.patch
-----------------------------------
WARNING: 'embedd' may be misspelled - perhaps 'embed'?
#27:
bound to, subsystems should embedd the corresponding I/O memory type
                             ^^^^^^

Best regards

Dirk