drivers/gpu/nova-core/bitfield.rs

Source file repositories/reference/linux-study-clean/drivers/gpu/nova-core/bitfield.rs

File Facts

System
Linux kernel
Corpus path
drivers/gpu/nova-core/bitfield.rs
Extension
.rs
Size
11107 bytes
Lines
330
Domain
Driver Families
Bucket
drivers/gpu
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0

//! Bitfield library for Rust structures
//!
//! Support for defining bitfields in Rust structures. Also used by the [`register!`] macro.

/// Defines a struct with accessors to access bits within an inner unsigned integer.
///
/// # Syntax
///
/// ```rust
/// use nova_core::bitfield;
///
/// #[derive(Debug, Clone, Copy, Default)]
/// enum Mode {
///     #[default]
///     Low = 0,
///     High = 1,
///     Auto = 2,
/// }
///
/// impl TryFrom<u8> for Mode {
///     type Error = u8;
///     fn try_from(value: u8) -> Result<Self, Self::Error> {
///         match value {
///             0 => Ok(Mode::Low),
///             1 => Ok(Mode::High),
///             2 => Ok(Mode::Auto),
///             _ => Err(value),
///         }
///     }
/// }
///
/// impl From<Mode> for u8 {
///     fn from(mode: Mode) -> u8 {
///         mode as u8
///     }
/// }
///
/// #[derive(Debug, Clone, Copy, Default)]
/// enum State {
///     #[default]
///     Inactive = 0,
///     Active = 1,
/// }
///
/// impl From<bool> for State {
///     fn from(value: bool) -> Self {
///         if value { State::Active } else { State::Inactive }
///     }
/// }
///
/// impl From<State> for bool {
///     fn from(state: State) -> bool {
///         match state {
///             State::Inactive => false,
///             State::Active => true,
///         }
///     }
/// }
///
/// bitfield! {
///     pub struct ControlReg(u32) {
///         7:7 state as bool => State;
///         3:0 mode as u8 ?=> Mode;
///     }
/// }
/// ```
///
/// This generates a struct with:
/// - Field accessors: `mode()`, `state()`, etc.
/// - Field setters: `set_mode()`, `set_state()`, etc. (supports chaining with builder pattern).
///   Note that the compiler will error out if the size of the setter's arg exceeds the
///   struct's storage size.
/// - Debug and Default implementations.
///
/// Note: Field accessors and setters inherit the same visibility as the struct itself.
/// In the example above, both `mode()` and `set_mode()` methods will be `pub`.
///
/// Fields are defined as follows:
///
/// - `as <type>` simply returns the field value casted to <type>, typically `u32`, `u16`, `u8` or
///   `bool`. Note that `bool` fields must have a range of 1 bit.
/// - `as <type> => <into_type>` calls `<into_type>`'s `From::<<type>>` implementation and returns
///   the result.
/// - `as <type> ?=> <try_into_type>` calls `<try_into_type>`'s `TryFrom::<<type>>` implementation
///   and returns the result. This is useful with fields for which not all values are valid.
macro_rules! bitfield {
    // Main entry point - defines the bitfield struct with fields
    ($vis:vis struct $name:ident($storage:ty) $(, $comment:literal)? { $($fields:tt)* }) => {

Annotation

Implementation Notes