drivers/fwctl/Kconfig | 12 + rust/bindings/bindings_helper.h | 1 + rust/helpers/fwctl.c | 17 + rust/helpers/helpers.c | 3 +- rust/kernel/fwctl.rs | 546 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 6 files changed, 580 insertions(+), 1 deletion(-) create mode 100644 rust/helpers/fwctl.c create mode 100644 rust/kernel/fwctl.rs
In the NVIDIA vGPU RFC [1], the vGPU type blobs must be provided to the GSP
before userspace can enumerate available vGPU types and create vGPU
instances. The original design relied on the firmware loading interface,
but fwctl is a more natural fit for this use case, as it is designed for
uploading configuration or firmware data required before the device becomes
operational.
This series introduces a Rust abstraction over the fwctl subsystem,
providing safe and idiomatic bindings.
The series is now a single Rust patch. The fwctl core release hook from v4 is
no longer needed. Instead, `Registration` owns the callback-visible driver
data and ties it to the parent device binding lifetime using a GAT on
`Operations`. `Device<T>` remains the refcounted fwctl object, while
callbacks borrow registration data for the callback lifetime.
The Rust fwctl module allows Rust drivers to integrate with the existing
C-side fwctl core through a typed trait interface. It provides:
- `Operations` trait: defines driver-specific callbacks: `open()`,
`close()`, `info()`, and `fw_rpc()`. The implementing type itself
serves as the per-FD user context, one instance per open().
- `RegistrationData<'a>`: a GAT associated type owned by `Registration`
and borrowed by callbacks under the fwctl registration lock.
- `Device<T>`: wraps `struct fwctl_device` and stores the pointer to
registration-owned driver data used during callbacks.
- `Registration<T>`: registration and automatic unregistration of
`struct fwctl_device` objects.
- `RpcScope` / `FwRpcResponse`: type-safe enums for RPC scope and response
handling, keeping unsafe pointer manipulation confined to the abstraction
layer.
`rust/kernel/lib.rs` is updated to conditionally include this module under
`CONFIG_RUST_FWCTL_ABSTRACTIONS`.
v6:
- Use a GAT lifetime for `Operations::RegistrationData<'a>` instead of
requiring `ForLt`. (Danilo)
- Require `FWCTL=y` for `RUST_FWCTL_ABSTRACTIONS` to avoid a built-in Rust
abstraction depending on a modular fwctl core.
- Add `#[inline]` to small forwarding helpers.
- Return `EINVAL` for an unrecognized RPC scope instead of the internal
`ENOTSUPP` errno.
Link to Danilo's GAT diff: [2]
Link to v4: [3]
v5:
- Rebase on top of the current drm-rust-next.
- Adopt Danilo's higher-ranked lifetime model for callback-visible
registration data.
- Drop the fwctl core release hook patch.
- Reject `FwRpcResponse::InPlace(len)` when `len` is larger than the input
buffer length.
- Add the missing BNXT fwctl device type to the Rust `DeviceType` enum.
- Document that `Registration::new()` requires an unregistered `Device` with
no live `Registration`, and no concurrent registration attempt for that
device.
v4:
- Rebase on top of the current drm-rust-next.
- Split out the fwctl core release hook before the Rust abstraction.
- Drop the fwctl init-ordering change from this series; it is already in
drm-rust-next as commit a55f80233f38 ("fwctl: Fix class init ordering to
avoid NULL pointer dereference on device removal").
- Add compile-time layout checks for the embedded `struct fwctl_device` and
`struct fwctl_uctx` offset assumptions. (Jason)
- Use `const_assert!()` for generic layout assertions. (Zhi)
- Require `Operations` and its `DeviceData` to be `Send + Sync`. (Danilo)
- Pass pinned shared references to `info()` and `fw_rpc()`. (Danilo)
- Make `Operations::open()` return an initializer directly and report open
failures through the initializer error path. (Danilo)
- Drop `DeviceData` from the fwctl device release hook.
- Fix clippy warnings for raw pointer casts and unsafe blocks. (Danilo)
- Fix the rustdoc broken link warning. (Danilo)
Link to v3: [4]
v3:
Quite some updates in this version. Here you can find the example
nova-core fwctl driver [5]. The interface is still WIP so it is just to
demonstrate the use of the rust fwctl abstractions.
Comments from folks:
- Use an enum for the return of fw_rpc. (Joel)
- Remove FWCTL_DEVICE_TYPE_RUST_FWCTL_TEST together with the sample
driver. (Jason)
- Remove DeviceType:Error. (Gary)
- Add __rust_helper for fwctl_get/fwctl_put. (Gary)
- Refine the design of the device private data. Now it has a similar
device private data structure as DRM. (Danilo)
- Separate fwctl alloc and register in the abstractions. (Jason)
- Registration::new() now takes &fwctl::Device<T> and the parent
&Device<Bound> to align with other class device abstractions. (Danilo)
- Update the Registration SAFETY comments. (Danilo & Jason)
- Take self as per-FD user context in callbacks. (Danilo)
- {open, close}_uctx -> {open, close}(). open() now takes &Device<Self>.
(Danilo)
Updates from me:
- Introduce enums for fwctl RPC scope.
- Introduce AlwaysRefCounted to avoid hacks after introducing the
refined flow of device private data.
- Introduce default implementation of close()/info().
- Fix a leak: Drop T::UserCtx in the close_uctx_callback explicitly.
v2:
- Don't open fwctl_put(). Add a rust helper. (Jason/Danilo)
- Wrap Registration with Devres to guarantee proper lifetime management.
(Jason/Danilo)
- Rename FwctlOps to Operations, FwctlUserCtx to UserCtx, FwctlDevice to
Device. (Danilo)
- Use fwctl::DeviceType enum instead of raw u32 for DEVICE_TYPE. (Danilo)
- Change fwctl_uctx field in UserCtx to Opaque<bindings::fwctl_uctx> and
make it private. (Danilo)
- Provide Deref and DerefMut implementations for UserCtx::uctx. (Danilo)
- Add UserCtx::parent_device_from_raw() to simplify parent device access.
- Use cast() and cast_mut() instead of manual pointer casts. (Danilo)
- Implement AlwaysRefCounted for Device and use ARef<Device> in
Registration. (Danilo)
- Add rust_helper_fwctl_get() for reference counting.
- Improve safety comments for slice::from_raw_parts_mut() in
fw_rpc_callback. (Danilo)
- Convert imports to vertical style.
- Fix all clippy warnings.
v1:
- Initial submission introducing fwctl Rust abstractions.
[1] https://lore.kernel.org/all/20250903221111.3866249-1-zhiw@nvidia.com/
[2] https://lore.kernel.org/all/DJJW7X4ESDSM.QCVYK2FC7ZR3@kernel.org/
[3] https://lore.kernel.org/all/20260624091758.1678092-1-zhiw@nvidia.com/
[4] https://lore.kernel.org/all/20260217204909.211793-1-zhiw@nvidia.com/
[5] https://github.com/zhiwang-nvidia/nova-core/commit/2068da7e8caf58da9584b0aa6c81fed8f547d59f
Zhi Wang (1):
rust: introduce abstractions for fwctl
drivers/fwctl/Kconfig | 12 +
rust/bindings/bindings_helper.h | 1 +
rust/helpers/fwctl.c | 17 +
rust/helpers/helpers.c | 3 +-
rust/kernel/fwctl.rs | 546 ++++++++++++++++++++++++++++++++
rust/kernel/lib.rs | 2 +
6 files changed, 580 insertions(+), 1 deletion(-)
create mode 100644 rust/helpers/fwctl.c
create mode 100644 rust/kernel/fwctl.rs
--
2.51.0
© 2016 - 2026 Red Hat, Inc.