1 // SPDX-License-Identifier: GPL-2.0 2 3 //! `register!` macro to define register layout and accessors. 4 //! 5 //! A single register typically includes several fields, which are accessed through a combination 6 //! of bit-shift and mask operations that introduce a class of potential mistakes, notably because 7 //! not all possible field values are necessarily valid. 8 //! 9 //! The `register!` macro in this module provides an intuitive and readable syntax for defining a 10 //! dedicated type for each register. Each such type comes with its own field accessors that can 11 //! return an error if a field's value is invalid. 12 13 /// Defines a dedicated type for a register with an absolute offset, including getter and setter 14 /// methods for its fields and methods to read and write it from an `Io` region. 15 /// 16 /// Example: 17 /// 18 /// ```no_run 19 /// register!(BOOT_0 @ 0x00000100, "Basic revision information about the GPU" { 20 /// 3:0 minor_revision as u8, "Minor revision of the chip"; 21 /// 7:4 major_revision as u8, "Major revision of the chip"; 22 /// 28:20 chipset as u32 ?=> Chipset, "Chipset model"; 23 /// }); 24 /// ``` 25 /// 26 /// This defines a `BOOT_0` type which can be read or written from offset `0x100` of an `Io` 27 /// region. It is composed of 3 fields, for instance `minor_revision` is made of the 4 least 28 /// significant bits of the register. Each field can be accessed and modified using accessor 29 /// methods: 30 /// 31 /// ```no_run 32 /// // Read from the register's defined offset (0x100). 33 /// let boot0 = BOOT_0::read(&bar); 34 /// pr_info!("chip revision: {}.{}", boot0.major_revision(), boot0.minor_revision()); 35 /// 36 /// // `Chipset::try_from` is called with the value of the `chipset` field and returns an 37 /// // error if it is invalid. 38 /// let chipset = boot0.chipset()?; 39 /// 40 /// // Update some fields and write the value back. 41 /// boot0.set_major_revision(3).set_minor_revision(10).write(&bar); 42 /// 43 /// // Or, just read and update the register in a single step: 44 /// BOOT_0::alter(&bar, |r| r.set_major_revision(3).set_minor_revision(10)); 45 /// ``` 46 /// 47 /// Fields are defined as follows: 48 /// 49 /// - `as <type>` simply returns the field value casted to <type>, typically `u32`, `u16`, `u8` or 50 /// `bool`. Note that `bool` fields must have a range of 1 bit. 51 /// - `as <type> => <into_type>` calls `<into_type>`'s `From::<<type>>` implementation and returns 52 /// the result. 53 /// - `as <type> ?=> <try_into_type>` calls `<try_into_type>`'s `TryFrom::<<type>>` implementation 54 /// and returns the result. This is useful with fields for which not all values are valid. 55 /// 56 /// The documentation strings are optional. If present, they will be added to the type's 57 /// definition, or the field getter and setter methods they are attached to. 58 /// 59 /// Putting a `+` before the address of the register makes it relative to a base: the `read` and 60 /// `write` methods take a `base` argument that is added to the specified address before access: 61 /// 62 /// ```no_run 63 /// register!(CPU_CTL @ +0x0000010, "CPU core control" { 64 /// 0:0 start as bool, "Start the CPU core"; 65 /// }); 66 /// 67 /// // Flip the `start` switch for the CPU core which base address is at `CPU_BASE`. 68 /// let cpuctl = CPU_CTL::read(&bar, CPU_BASE); 69 /// pr_info!("CPU CTL: {:#x}", cpuctl); 70 /// cpuctl.set_start(true).write(&bar, CPU_BASE); 71 /// ``` 72 /// 73 /// It is also possible to create a alias register by using the `=> ALIAS` syntax. This is useful 74 /// for cases where a register's interpretation depends on the context: 75 /// 76 /// ```no_run 77 /// register!(SCRATCH @ 0x00000200, "Scratch register" { 78 /// 31:0 value as u32, "Raw value"; 79 /// }); 80 /// 81 /// register!(SCRATCH_BOOT_STATUS => SCRATCH, "Boot status of the firmware" { 82 /// 0:0 completed as bool, "Whether the firmware has completed booting"; 83 /// }); 84 /// ``` 85 /// 86 /// In this example, `SCRATCH_0_BOOT_STATUS` uses the same I/O address as `SCRATCH`, while also 87 /// providing its own `completed` field. 88 macro_rules! register { 89 // Creates a register at a fixed offset of the MMIO space. 90 ($name:ident @ $offset:literal $(, $comment:literal)? { $($fields:tt)* } ) => { 91 register!(@common $name $(, $comment)?); 92 register!(@field_accessors $name { $($fields)* }); 93 register!(@io $name @ $offset); 94 }; 95 96 // Creates an alias register of fixed offset register `alias` with its own fields. 97 ($name:ident => $alias:ident $(, $comment:literal)? { $($fields:tt)* } ) => { 98 register!(@common $name $(, $comment)?); 99 register!(@field_accessors $name { $($fields)* }); 100 register!(@io $name @ $alias::OFFSET); 101 }; 102 103 // Creates a register at a relative offset from a base address. 104 ($name:ident @ + $offset:literal $(, $comment:literal)? { $($fields:tt)* } ) => { 105 register!(@common $name $(, $comment)?); 106 register!(@field_accessors $name { $($fields)* }); 107 register!(@io $name @ + $offset); 108 }; 109 110 // Creates an alias register of relative offset register `alias` with its own fields. 111 ($name:ident => + $alias:ident $(, $comment:literal)? { $($fields:tt)* } ) => { 112 register!(@common $name $(, $comment)?); 113 register!(@field_accessors $name { $($fields)* }); 114 register!(@io $name @ + $alias::OFFSET); 115 }; 116 117 // All rules below are helpers. 118 119 // Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`, `BitOr`, 120 // and conversion to regular `u32`). 121 (@common $name:ident $(, $comment:literal)?) => { 122 $( 123 #[doc=$comment] 124 )? 125 #[repr(transparent)] 126 #[derive(Clone, Copy, Default)] 127 pub(crate) struct $name(u32); 128 129 // TODO[REGA]: display the raw hex value, then the value of all the fields. This requires 130 // matching the fields, which will complexify the syntax considerably... 131 impl ::core::fmt::Debug for $name { 132 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { 133 f.debug_tuple(stringify!($name)) 134 .field(&format_args!("0x{0:x}", &self.0)) 135 .finish() 136 } 137 } 138 139 impl ::core::ops::BitOr for $name { 140 type Output = Self; 141 142 fn bitor(self, rhs: Self) -> Self::Output { 143 Self(self.0 | rhs.0) 144 } 145 } 146 147 impl ::core::convert::From<$name> for u32 { 148 fn from(reg: $name) -> u32 { 149 reg.0 150 } 151 } 152 }; 153 154 // Defines all the field getter/methods methods for `$name`. 155 ( 156 @field_accessors $name:ident { 157 $($hi:tt:$lo:tt $field:ident as $type:tt 158 $(?=> $try_into_type:ty)? 159 $(=> $into_type:ty)? 160 $(, $comment:literal)? 161 ; 162 )* 163 } 164 ) => { 165 $( 166 register!(@check_field_bounds $hi:$lo $field as $type); 167 )* 168 169 #[allow(dead_code)] 170 impl $name { 171 $( 172 register!(@field_accessor $name $hi:$lo $field as $type 173 $(?=> $try_into_type)? 174 $(=> $into_type)? 175 $(, $comment)? 176 ; 177 ); 178 )* 179 } 180 }; 181 182 // Boolean fields must have `$hi == $lo`. 183 (@check_field_bounds $hi:tt:$lo:tt $field:ident as bool) => { 184 #[allow(clippy::eq_op)] 185 const _: () = { 186 ::kernel::build_assert!( 187 $hi == $lo, 188 concat!("boolean field `", stringify!($field), "` covers more than one bit") 189 ); 190 }; 191 }; 192 193 // Non-boolean fields must have `$hi >= $lo`. 194 (@check_field_bounds $hi:tt:$lo:tt $field:ident as $type:tt) => { 195 #[allow(clippy::eq_op)] 196 const _: () = { 197 ::kernel::build_assert!( 198 $hi >= $lo, 199 concat!("field `", stringify!($field), "`'s MSB is smaller than its LSB") 200 ); 201 }; 202 }; 203 204 // Catches fields defined as `bool` and convert them into a boolean value. 205 ( 206 @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool => $into_type:ty 207 $(, $comment:literal)?; 208 ) => { 209 register!( 210 @leaf_accessor $name $hi:$lo $field 211 { |f| <$into_type>::from(if f != 0 { true } else { false }) } 212 $into_type => $into_type $(, $comment)?; 213 ); 214 }; 215 216 // Shortcut for fields defined as `bool` without the `=>` syntax. 217 ( 218 @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool $(, $comment:literal)?; 219 ) => { 220 register!(@field_accessor $name $hi:$lo $field as bool => bool $(, $comment)?;); 221 }; 222 223 // Catches the `?=>` syntax for non-boolean fields. 224 ( 225 @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt ?=> $try_into_type:ty 226 $(, $comment:literal)?; 227 ) => { 228 register!(@leaf_accessor $name $hi:$lo $field 229 { |f| <$try_into_type>::try_from(f as $type) } $try_into_type => 230 ::core::result::Result< 231 $try_into_type, 232 <$try_into_type as ::core::convert::TryFrom<$type>>::Error 233 > 234 $(, $comment)?;); 235 }; 236 237 // Catches the `=>` syntax for non-boolean fields. 238 ( 239 @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt => $into_type:ty 240 $(, $comment:literal)?; 241 ) => { 242 register!(@leaf_accessor $name $hi:$lo $field 243 { |f| <$into_type>::from(f as $type) } $into_type => $into_type $(, $comment)?;); 244 }; 245 246 // Shortcut for non-boolean fields defined without the `=>` or `?=>` syntax. 247 ( 248 @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt 249 $(, $comment:literal)?; 250 ) => { 251 register!(@field_accessor $name $hi:$lo $field as $type => $type $(, $comment)?;); 252 }; 253 254 // Generates the accessor methods for a single field. 255 ( 256 @leaf_accessor $name:ident $hi:tt:$lo:tt $field:ident 257 { $process:expr } $to_type:ty => $res_type:ty $(, $comment:literal)?; 258 ) => { 259 ::kernel::macros::paste!( 260 const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive<u8> = $lo..=$hi; 261 const [<$field:upper _MASK>]: u32 = ((((1 << $hi) - 1) << 1) + 1) - ((1 << $lo) - 1); 262 const [<$field:upper _SHIFT>]: u32 = Self::[<$field:upper _MASK>].trailing_zeros(); 263 ); 264 265 $( 266 #[doc="Returns the value of this field:"] 267 #[doc=$comment] 268 )? 269 #[inline] 270 pub(crate) fn $field(self) -> $res_type { 271 ::kernel::macros::paste!( 272 const MASK: u32 = $name::[<$field:upper _MASK>]; 273 const SHIFT: u32 = $name::[<$field:upper _SHIFT>]; 274 ); 275 let field = ((self.0 & MASK) >> SHIFT); 276 277 $process(field) 278 } 279 280 ::kernel::macros::paste!( 281 $( 282 #[doc="Sets the value of this field:"] 283 #[doc=$comment] 284 )? 285 #[inline] 286 pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self { 287 const MASK: u32 = $name::[<$field:upper _MASK>]; 288 const SHIFT: u32 = $name::[<$field:upper _SHIFT>]; 289 let value = (u32::from(value) << SHIFT) & MASK; 290 self.0 = (self.0 & !MASK) | value; 291 292 self 293 } 294 ); 295 }; 296 297 // Generates the IO accessors for a fixed offset register. 298 (@io $name:ident @ $offset:expr) => { 299 #[allow(dead_code)] 300 impl $name { 301 pub(crate) const OFFSET: usize = $offset; 302 303 /// Read the register from its address in `io`. 304 #[inline] 305 pub(crate) fn read<const SIZE: usize, T>(io: &T) -> Self where 306 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>, 307 { 308 Self(io.read32($offset)) 309 } 310 311 /// Write the value contained in `self` to the register address in `io`. 312 #[inline] 313 pub(crate) fn write<const SIZE: usize, T>(self, io: &T) where 314 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>, 315 { 316 io.write32(self.0, $offset) 317 } 318 319 /// Read the register from its address in `io` and run `f` on its value to obtain a new 320 /// value to write back. 321 #[inline] 322 pub(crate) fn alter<const SIZE: usize, T, F>( 323 io: &T, 324 f: F, 325 ) where 326 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>, 327 F: ::core::ops::FnOnce(Self) -> Self, 328 { 329 let reg = f(Self::read(io)); 330 reg.write(io); 331 } 332 } 333 }; 334 335 // Generates the IO accessors for a relative offset register. 336 (@io $name:ident @ + $offset:literal) => { 337 #[allow(dead_code)] 338 impl $name { 339 pub(crate) const OFFSET: usize = $offset; 340 341 #[inline] 342 pub(crate) fn read<const SIZE: usize, T>( 343 io: &T, 344 base: usize, 345 ) -> Self where 346 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>, 347 { 348 Self(io.read32(base + $offset)) 349 } 350 351 #[inline] 352 pub(crate) fn write<const SIZE: usize, T>( 353 self, 354 io: &T, 355 base: usize, 356 ) where 357 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>, 358 { 359 io.write32(self.0, base + $offset) 360 } 361 362 #[inline] 363 pub(crate) fn alter<const SIZE: usize, T, F>( 364 io: &T, 365 base: usize, 366 f: F, 367 ) where 368 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>, 369 F: ::core::ops::FnOnce(Self) -> Self, 370 { 371 let reg = f(Self::read(io, base)); 372 reg.write(io, base); 373 } 374 } 375 }; 376 } 377