| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | bindings,
|
| 9 | prelude::*, |
| 10 | };
|
| 11 |
|
| 12 | pub mod mem;
|
| 13 | pub mod poll;
|
| 14 | pub mod resource;
|
| 15 |
|
| 16 | pub use resource::Resource;
|
| 17 |
|
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub type PhysAddr = bindings::phys_addr_t;
|
| 23 |
|
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | pub type ResourceSize = bindings::resource_size_t;
|
| 29 |
|
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | pub struct MmioRaw<const SIZE: usize = 0> {
|
| 39 | addr: usize,
|
| 40 | maxsize: usize,
|
| 41 | }
|
| 42 |
|
| 43 | impl<const SIZE: usize> MmioRaw<SIZE> {
|
| 44 | |
| 45 | pub fn new(addr: usize, maxsize: usize) -> Result<Self> {
|
| 46 | if maxsize < SIZE {
|
| 47 | return Err(EINVAL);
|
| 48 | }
|
| 49 |
|
| 50 | Ok(Self { addr, maxsize })
|
| 51 | }
|
| 52 |
|
| 53 | |
| 54 | #[inline]
|
| 55 | pub fn addr(&self) -> usize {
|
| 56 | self.addr
|
| 57 | }
|
| 58 |
|
| 59 | |
| 60 | #[inline]
|
| 61 | pub fn maxsize(&self) -> usize {
|
| 62 | self.maxsize
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | #[repr(transparent)]
|
| 138 | pub struct Mmio<const SIZE: usize = 0>(MmioRaw<SIZE>);
|
| 139 |
|
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | macro_rules! call_mmio_read {
|
| 156 | (infallible, $c_fn:ident, $self:ident, $type:ty, $addr:expr) => {
|
| 157 | |
| 158 | unsafe { bindings::$c_fn($addr as *const c_void) as $type }
|
| 159 | };
|
| 160 |
|
| 161 | (fallible, $c_fn:ident, $self:ident, $type:ty, $addr:expr) => {{
|
| 162 | |
| 163 | Ok(unsafe { bindings::$c_fn($addr as *const c_void) as $type })
|
| 164 | }};
|
| 165 | }
|
| 166 |
|
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | macro_rules! call_mmio_write {
|
| 184 | (infallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr) => {
|
| 185 | |
| 186 | unsafe { bindings::$c_fn($value, $addr as *mut c_void) }
|
| 187 | };
|
| 188 |
|
| 189 | (fallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr) => {{
|
| 190 | |
| 191 | unsafe { bindings::$c_fn($value, $addr as *mut c_void) };
|
| 192 | Ok(())
|
| 193 | }};
|
| 194 | }
|
| 195 |
|
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | #[macro_export]
|
| 219 | macro_rules! io_define_read {
|
| 220 | (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $call_macro:ident($c_fn:ident) ->
|
| 221 | $type_name:ty) => {
|
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | $(#[$attr])*
|
| 227 | |
| 228 | #[inline(always)]
|
| 229 | $vis fn $name(&self, offset: usize) -> $type_name {
|
| 230 | let addr = self.io_addr_assert::<$type_name>(offset);
|
| 231 |
|
| 232 | |
| 233 | $call_macro!(infallible, $c_fn, self, $type_name, addr)
|
| 234 | }
|
| 235 | };
|
| 236 |
|
| 237 | (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $call_macro:ident($c_fn:ident) ->
|
| 238 | $type_name:ty) => {
|
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | $(#[$attr])*
|
| 244 | $vis fn $try_name(&self, offset: usize) -> Result<$type_name> {
|
| 245 | let addr = self.io_addr::<$type_name>(offset)?;
|
| 246 |
|
| 247 | |
| 248 | $call_macro!(fallible, $c_fn, self, $type_name, addr)
|
| 249 | }
|
| 250 | };
|
| 251 | }
|
| 252 | pub use io_define_read;
|
| 253 |
|
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | #[macro_export]
|
| 278 | macro_rules! io_define_write {
|
| 279 | (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $call_macro:ident($c_fn:ident) <-
|
| 280 | $type_name:ty) => {
|
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | $(#[$attr])*
|
| 286 | |
| 287 | #[inline(always)]
|
| 288 | $vis fn $name(&self, value: $type_name, offset: usize) {
|
| 289 | let addr = self.io_addr_assert::<$type_name>(offset);
|
| 290 |
|
| 291 | $call_macro!(infallible, $c_fn, self, $type_name, addr, value);
|
| 292 | }
|
| 293 | };
|
| 294 |
|
| 295 | (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $call_macro:ident($c_fn:ident) <-
|
| 296 | $type_name:ty) => {
|
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | $(#[$attr])*
|
| 302 | $vis fn $try_name(&self, value: $type_name, offset: usize) -> Result {
|
| 303 | let addr = self.io_addr::<$type_name>(offset)?;
|
| 304 |
|
| 305 | $call_macro!(fallible, $c_fn, self, $type_name, addr, value)
|
| 306 | }
|
| 307 | };
|
| 308 | }
|
| 309 | pub use io_define_write;
|
| 310 |
|
| 311 | |
| 312 | |
| 313 | #[inline]
|
| 314 | const fn offset_valid<U>(offset: usize, size: usize) -> bool {
|
| 315 | let type_size = core::mem::size_of::<U>();
|
| 316 | if let Some(end) = offset.checked_add(type_size) {
|
| 317 | end <= size && offset % type_size == 0
|
| 318 | } else {
|
| 319 | false
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | pub trait IoCapable<T> {}
|
| 331 |
|
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | pub trait Io {
|
| 351 | |
| 352 | fn addr(&self) -> usize;
|
| 353 |
|
| 354 | |
| 355 | fn maxsize(&self) -> usize;
|
| 356 |
|
| 357 | |
| 358 | |
| 359 | #[inline]
|
| 360 | fn io_addr<U>(&self, offset: usize) -> Result<usize> {
|
| 361 | if !offset_valid::<U>(offset, self.maxsize()) {
|
| 362 | return Err(EINVAL);
|
| 363 | }
|
| 364 |
|
| 365 | |
| 366 | |
| 367 | self.addr().checked_add(offset).ok_or(EINVAL)
|
| 368 | }
|
| 369 |
|
| 370 | |
| 371 | #[inline(always)]
|
| 372 | fn try_read8(&self, _offset: usize) -> Result<u8>
|
| 373 | where
|
| 374 | Self: IoCapable<u8>,
|
| 375 | {
|
| 376 | build_error!("Backend does not support fallible 8-bit read")
|
| 377 | }
|
| 378 |
|
| 379 | |
| 380 | #[inline(always)]
|
| 381 | fn try_read16(&self, _offset: usize) -> Result<u16>
|
| 382 | where
|
| 383 | Self: IoCapable<u16>,
|
| 384 | {
|
| 385 | build_error!("Backend does not support fallible 16-bit read")
|
| 386 | }
|
| 387 |
|
| 388 | |
| 389 | #[inline(always)]
|
| 390 | fn try_read32(&self, _offset: usize) -> Result<u32>
|
| 391 | where
|
| 392 | Self: IoCapable<u32>,
|
| 393 | {
|
| 394 | build_error!("Backend does not support fallible 32-bit read")
|
| 395 | }
|
| 396 |
|
| 397 | |
| 398 | #[inline(always)]
|
| 399 | fn try_read64(&self, _offset: usize) -> Result<u64>
|
| 400 | where
|
| 401 | Self: IoCapable<u64>,
|
| 402 | {
|
| 403 | build_error!("Backend does not support fallible 64-bit read")
|
| 404 | }
|
| 405 |
|
| 406 | |
| 407 | #[inline(always)]
|
| 408 | fn try_write8(&self, _value: u8, _offset: usize) -> Result
|
| 409 | where
|
| 410 | Self: IoCapable<u8>,
|
| 411 | {
|
| 412 | build_error!("Backend does not support fallible 8-bit write")
|
| 413 | }
|
| 414 |
|
| 415 | |
| 416 | #[inline(always)]
|
| 417 | fn try_write16(&self, _value: u16, _offset: usize) -> Result
|
| 418 | where
|
| 419 | Self: IoCapable<u16>,
|
| 420 | {
|
| 421 | build_error!("Backend does not support fallible 16-bit write")
|
| 422 | }
|
| 423 |
|
| 424 | |
| 425 | #[inline(always)]
|
| 426 | fn try_write32(&self, _value: u32, _offset: usize) -> Result
|
| 427 | where
|
| 428 | Self: IoCapable<u32>,
|
| 429 | {
|
| 430 | build_error!("Backend does not support fallible 32-bit write")
|
| 431 | }
|
| 432 |
|
| 433 | |
| 434 | #[inline(always)]
|
| 435 | fn try_write64(&self, _value: u64, _offset: usize) -> Result
|
| 436 | where
|
| 437 | Self: IoCapable<u64>,
|
| 438 | {
|
| 439 | build_error!("Backend does not support fallible 64-bit write")
|
| 440 | }
|
| 441 |
|
| 442 | |
| 443 | #[inline(always)]
|
| 444 | fn read8(&self, _offset: usize) -> u8
|
| 445 | where
|
| 446 | Self: IoKnownSize + IoCapable<u8>,
|
| 447 | {
|
| 448 | build_error!("Backend does not support infallible 8-bit read")
|
| 449 | }
|
| 450 |
|
| 451 | |
| 452 | #[inline(always)]
|
| 453 | fn read16(&self, _offset: usize) -> u16
|
| 454 | where
|
| 455 | Self: IoKnownSize + IoCapable<u16>,
|
| 456 | {
|
| 457 | build_error!("Backend does not support infallible 16-bit read")
|
| 458 | }
|
| 459 |
|
| 460 | |
| 461 | #[inline(always)]
|
| 462 | fn read32(&self, _offset: usize) -> u32
|
| 463 | where
|
| 464 | Self: IoKnownSize + IoCapable<u32>,
|
| 465 | {
|
| 466 | build_error!("Backend does not support infallible 32-bit read")
|
| 467 | }
|
| 468 |
|
| 469 | |
| 470 | #[inline(always)]
|
| 471 | fn read64(&self, _offset: usize) -> u64
|
| 472 | where
|
| 473 | Self: IoKnownSize + IoCapable<u64>,
|
| 474 | {
|
| 475 | build_error!("Backend does not support infallible 64-bit read")
|
| 476 | }
|
| 477 |
|
| 478 | |
| 479 | #[inline(always)]
|
| 480 | fn write8(&self, _value: u8, _offset: usize)
|
| 481 | where
|
| 482 | Self: IoKnownSize + IoCapable<u8>,
|
| 483 | {
|
| 484 | build_error!("Backend does not support infallible 8-bit write")
|
| 485 | }
|
| 486 |
|
| 487 | |
| 488 | #[inline(always)]
|
| 489 | fn write16(&self, _value: u16, _offset: usize)
|
| 490 | where
|
| 491 | Self: IoKnownSize + IoCapable<u16>,
|
| 492 | {
|
| 493 | build_error!("Backend does not support infallible 16-bit write")
|
| 494 | }
|
| 495 |
|
| 496 | |
| 497 | #[inline(always)]
|
| 498 | fn write32(&self, _value: u32, _offset: usize)
|
| 499 | where
|
| 500 | Self: IoKnownSize + IoCapable<u32>,
|
| 501 | {
|
| 502 | build_error!("Backend does not support infallible 32-bit write")
|
| 503 | }
|
| 504 |
|
| 505 | |
| 506 | #[inline(always)]
|
| 507 | fn write64(&self, _value: u64, _offset: usize)
|
| 508 | where
|
| 509 | Self: IoKnownSize + IoCapable<u64>,
|
| 510 | {
|
| 511 | build_error!("Backend does not support infallible 64-bit write")
|
| 512 | }
|
| 513 | }
|
| 514 |
|
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | pub trait IoKnownSize: Io {
|
| 523 | |
| 524 | const MIN_SIZE: usize;
|
| 525 |
|
| 526 | |
| 527 | |
| 528 | |
| 529 | #[inline(always)]
|
| 530 | fn io_addr_assert<U>(&self, offset: usize) -> usize {
|
| 531 | build_assert!(offset_valid::<U>(offset, Self::MIN_SIZE));
|
| 532 |
|
| 533 | self.addr() + offset
|
| 534 | }
|
| 535 | }
|
| 536 |
|
| 537 | |
| 538 | impl<const SIZE: usize> IoCapable<u8> for Mmio<SIZE> {}
|
| 539 | impl<const SIZE: usize> IoCapable<u16> for Mmio<SIZE> {}
|
| 540 | impl<const SIZE: usize> IoCapable<u32> for Mmio<SIZE> {}
|
| 541 |
|
| 542 | |
| 543 | #[cfg(CONFIG_64BIT)]
|
| 544 | impl<const SIZE: usize> IoCapable<u64> for Mmio<SIZE> {}
|
| 545 |
|
| 546 | impl<const SIZE: usize> Io for Mmio<SIZE> {
|
| 547 | |
| 548 | #[inline]
|
| 549 | fn addr(&self) -> usize {
|
| 550 | self.0.addr()
|
| 551 | }
|
| 552 |
|
| 553 | |
| 554 | #[inline]
|
| 555 | fn maxsize(&self) -> usize {
|
| 556 | self.0.maxsize()
|
| 557 | }
|
| 558 |
|
| 559 | io_define_read!(fallible, try_read8, call_mmio_read(readb) -> u8);
|
| 560 | io_define_read!(fallible, try_read16, call_mmio_read(readw) -> u16);
|
| 561 | io_define_read!(fallible, try_read32, call_mmio_read(readl) -> u32);
|
| 562 | io_define_read!(
|
| 563 | fallible,
|
| 564 | #[cfg(CONFIG_64BIT)]
|
| 565 | try_read64,
|
| 566 | call_mmio_read(readq) -> u64
|
| 567 | );
|
| 568 |
|
| 569 | io_define_write!(fallible, try_write8, call_mmio_write(writeb) <- u8);
|
| 570 | io_define_write!(fallible, try_write16, call_mmio_write(writew) <- u16);
|
| 571 | io_define_write!(fallible, try_write32, call_mmio_write(writel) <- u32);
|
| 572 | io_define_write!(
|
| 573 | fallible,
|
| 574 | #[cfg(CONFIG_64BIT)]
|
| 575 | try_write64,
|
| 576 | call_mmio_write(writeq) <- u64
|
| 577 | );
|
| 578 |
|
| 579 | io_define_read!(infallible, read8, call_mmio_read(readb) -> u8);
|
| 580 | io_define_read!(infallible, read16, call_mmio_read(readw) -> u16);
|
| 581 | io_define_read!(infallible, read32, call_mmio_read(readl) -> u32);
|
| 582 | io_define_read!(
|
| 583 | infallible,
|
| 584 | #[cfg(CONFIG_64BIT)]
|
| 585 | read64,
|
| 586 | call_mmio_read(readq) -> u64
|
| 587 | );
|
| 588 |
|
| 589 | io_define_write!(infallible, write8, call_mmio_write(writeb) <- u8);
|
| 590 | io_define_write!(infallible, write16, call_mmio_write(writew) <- u16);
|
| 591 | io_define_write!(infallible, write32, call_mmio_write(writel) <- u32);
|
| 592 | io_define_write!(
|
| 593 | infallible,
|
| 594 | #[cfg(CONFIG_64BIT)]
|
| 595 | write64,
|
| 596 | call_mmio_write(writeq) <- u64
|
| 597 | );
|
| 598 | }
|
| 599 |
|
| 600 | impl<const SIZE: usize> IoKnownSize for Mmio<SIZE> {
|
| 601 | const MIN_SIZE: usize = SIZE;
|
| 602 | }
|
| 603 |
|
| 604 | impl<const SIZE: usize> Mmio<SIZE> {
|
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | pub unsafe fn from_raw(raw: &MmioRaw<SIZE>) -> &Self {
|
| 612 | |
| 613 | unsafe { &*core::ptr::from_ref(raw).cast() }
|
| 614 | }
|
| 615 |
|
| 616 | io_define_read!(infallible, pub read8_relaxed, call_mmio_read(readb_relaxed) -> u8);
|
| 617 | io_define_read!(infallible, pub read16_relaxed, call_mmio_read(readw_relaxed) -> u16);
|
| 618 | io_define_read!(infallible, pub read32_relaxed, call_mmio_read(readl_relaxed) -> u32);
|
| 619 | io_define_read!(
|
| 620 | infallible,
|
| 621 | #[cfg(CONFIG_64BIT)]
|
| 622 | pub read64_relaxed,
|
| 623 | call_mmio_read(readq_relaxed) -> u64
|
| 624 | );
|
| 625 |
|
| 626 | io_define_read!(fallible, pub try_read8_relaxed, call_mmio_read(readb_relaxed) -> u8);
|
| 627 | io_define_read!(fallible, pub try_read16_relaxed, call_mmio_read(readw_relaxed) -> u16);
|
| 628 | io_define_read!(fallible, pub try_read32_relaxed, call_mmio_read(readl_relaxed) -> u32);
|
| 629 | io_define_read!(
|
| 630 | fallible,
|
| 631 | #[cfg(CONFIG_64BIT)]
|
| 632 | pub try_read64_relaxed,
|
| 633 | call_mmio_read(readq_relaxed) -> u64
|
| 634 | );
|
| 635 |
|
| 636 | io_define_write!(infallible, pub write8_relaxed, call_mmio_write(writeb_relaxed) <- u8);
|
| 637 | io_define_write!(infallible, pub write16_relaxed, call_mmio_write(writew_relaxed) <- u16);
|
| 638 | io_define_write!(infallible, pub write32_relaxed, call_mmio_write(writel_relaxed) <- u32);
|
| 639 | io_define_write!(
|
| 640 | infallible,
|
| 641 | #[cfg(CONFIG_64BIT)]
|
| 642 | pub write64_relaxed,
|
| 643 | call_mmio_write(writeq_relaxed) <- u64
|
| 644 | );
|
| 645 |
|
| 646 | io_define_write!(fallible, pub try_write8_relaxed, call_mmio_write(writeb_relaxed) <- u8);
|
| 647 | io_define_write!(fallible, pub try_write16_relaxed, call_mmio_write(writew_relaxed) <- u16);
|
| 648 | io_define_write!(fallible, pub try_write32_relaxed, call_mmio_write(writel_relaxed) <- u32);
|
| 649 | io_define_write!(
|
| 650 | fallible,
|
| 651 | #[cfg(CONFIG_64BIT)]
|
| 652 | pub try_write64_relaxed,
|
| 653 | call_mmio_write(writeq_relaxed) <- u64
|
| 654 | );
|
| 655 | }
|