| 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 register;
|
| 15 | pub mod resource;
|
| 16 |
|
| 17 | pub use crate::register;
|
| 18 | pub use resource::Resource;
|
| 19 |
|
| 20 | use register::LocatedRegister;
|
| 21 |
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | pub type PhysAddr = bindings::phys_addr_t;
|
| 27 |
|
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | pub type ResourceSize = bindings::resource_size_t;
|
| 33 |
|
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | pub struct MmioRaw<const SIZE: usize = 0> {
|
| 43 | addr: usize,
|
| 44 | maxsize: usize,
|
| 45 | }
|
| 46 |
|
| 47 | impl<const SIZE: usize> MmioRaw<SIZE> {
|
| 48 | |
| 49 | pub fn new(addr: usize, maxsize: usize) -> Result<Self> {
|
| 50 | if maxsize < SIZE {
|
| 51 | return Err(EINVAL);
|
| 52 | }
|
| 53 |
|
| 54 | Ok(Self { addr, maxsize })
|
| 55 | }
|
| 56 |
|
| 57 | |
| 58 | #[inline]
|
| 59 | pub fn addr(&self) -> usize {
|
| 60 | self.addr
|
| 61 | }
|
| 62 |
|
| 63 | |
| 64 | #[inline]
|
| 65 | pub fn maxsize(&self) -> usize {
|
| 66 | self.maxsize
|
| 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 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[repr(transparent)]
|
| 142 | pub struct Mmio<const SIZE: usize = 0>(MmioRaw<SIZE>);
|
| 143 |
|
| 144 | |
| 145 | |
| 146 | #[inline]
|
| 147 | const fn offset_valid<U>(offset: usize, size: usize) -> bool {
|
| 148 | let type_size = core::mem::size_of::<U>();
|
| 149 | if let Some(end) = offset.checked_add(type_size) {
|
| 150 | end <= size && offset % type_size == 0
|
| 151 | } else {
|
| 152 | false
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | pub trait IoCapable<T> {
|
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | unsafe fn io_read(&self, address: usize) -> T;
|
| 171 |
|
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | unsafe fn io_write(&self, value: T, address: usize);
|
| 178 | }
|
| 179 |
|
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | pub trait IoLoc<T> {
|
| 198 | |
| 199 | type IoType: Into<T> + From<T>;
|
| 200 |
|
| 201 | |
| 202 | fn offset(self) -> usize;
|
| 203 | }
|
| 204 |
|
| 205 | |
| 206 | |
| 207 | macro_rules! impl_usize_ioloc {
|
| 208 | ($($ty:ty),*) => {
|
| 209 | $(
|
| 210 | impl IoLoc<$ty> for usize {
|
| 211 | type IoType = $ty;
|
| 212 |
|
| 213 | #[inline(always)]
|
| 214 | fn offset(self) -> usize {
|
| 215 | self
|
| 216 | }
|
| 217 | }
|
| 218 | )*
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | |
| 223 | impl_usize_ioloc!(u8, u16, u32, u64);
|
| 224 |
|
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | pub trait Io {
|
| 244 | |
| 245 | fn addr(&self) -> usize;
|
| 246 |
|
| 247 | |
| 248 | fn maxsize(&self) -> usize;
|
| 249 |
|
| 250 | |
| 251 | |
| 252 | #[inline]
|
| 253 | fn io_addr<U>(&self, offset: usize) -> Result<usize> {
|
| 254 | if !offset_valid::<U>(offset, self.maxsize()) {
|
| 255 | return Err(EINVAL);
|
| 256 | }
|
| 257 |
|
| 258 | |
| 259 | |
| 260 | self.addr().checked_add(offset).ok_or(EINVAL)
|
| 261 | }
|
| 262 |
|
| 263 | |
| 264 | #[inline(always)]
|
| 265 | fn try_read8(&self, offset: usize) -> Result<u8>
|
| 266 | where
|
| 267 | Self: IoCapable<u8>,
|
| 268 | {
|
| 269 | self.try_read(offset)
|
| 270 | }
|
| 271 |
|
| 272 | |
| 273 | #[inline(always)]
|
| 274 | fn try_read16(&self, offset: usize) -> Result<u16>
|
| 275 | where
|
| 276 | Self: IoCapable<u16>,
|
| 277 | {
|
| 278 | self.try_read(offset)
|
| 279 | }
|
| 280 |
|
| 281 | |
| 282 | #[inline(always)]
|
| 283 | fn try_read32(&self, offset: usize) -> Result<u32>
|
| 284 | where
|
| 285 | Self: IoCapable<u32>,
|
| 286 | {
|
| 287 | self.try_read(offset)
|
| 288 | }
|
| 289 |
|
| 290 | |
| 291 | #[inline(always)]
|
| 292 | fn try_read64(&self, offset: usize) -> Result<u64>
|
| 293 | where
|
| 294 | Self: IoCapable<u64>,
|
| 295 | {
|
| 296 | self.try_read(offset)
|
| 297 | }
|
| 298 |
|
| 299 | |
| 300 | #[inline(always)]
|
| 301 | fn try_write8(&self, value: u8, offset: usize) -> Result
|
| 302 | where
|
| 303 | Self: IoCapable<u8>,
|
| 304 | {
|
| 305 | self.try_write(offset, value)
|
| 306 | }
|
| 307 |
|
| 308 | |
| 309 | #[inline(always)]
|
| 310 | fn try_write16(&self, value: u16, offset: usize) -> Result
|
| 311 | where
|
| 312 | Self: IoCapable<u16>,
|
| 313 | {
|
| 314 | self.try_write(offset, value)
|
| 315 | }
|
| 316 |
|
| 317 | |
| 318 | #[inline(always)]
|
| 319 | fn try_write32(&self, value: u32, offset: usize) -> Result
|
| 320 | where
|
| 321 | Self: IoCapable<u32>,
|
| 322 | {
|
| 323 | self.try_write(offset, value)
|
| 324 | }
|
| 325 |
|
| 326 | |
| 327 | #[inline(always)]
|
| 328 | fn try_write64(&self, value: u64, offset: usize) -> Result
|
| 329 | where
|
| 330 | Self: IoCapable<u64>,
|
| 331 | {
|
| 332 | self.try_write(offset, value)
|
| 333 | }
|
| 334 |
|
| 335 | |
| 336 | #[inline(always)]
|
| 337 | fn read8(&self, offset: usize) -> u8
|
| 338 | where
|
| 339 | Self: IoKnownSize + IoCapable<u8>,
|
| 340 | {
|
| 341 | self.read(offset)
|
| 342 | }
|
| 343 |
|
| 344 | |
| 345 | #[inline(always)]
|
| 346 | fn read16(&self, offset: usize) -> u16
|
| 347 | where
|
| 348 | Self: IoKnownSize + IoCapable<u16>,
|
| 349 | {
|
| 350 | self.read(offset)
|
| 351 | }
|
| 352 |
|
| 353 | |
| 354 | #[inline(always)]
|
| 355 | fn read32(&self, offset: usize) -> u32
|
| 356 | where
|
| 357 | Self: IoKnownSize + IoCapable<u32>,
|
| 358 | {
|
| 359 | self.read(offset)
|
| 360 | }
|
| 361 |
|
| 362 | |
| 363 | #[inline(always)]
|
| 364 | fn read64(&self, offset: usize) -> u64
|
| 365 | where
|
| 366 | Self: IoKnownSize + IoCapable<u64>,
|
| 367 | {
|
| 368 | self.read(offset)
|
| 369 | }
|
| 370 |
|
| 371 | |
| 372 | #[inline(always)]
|
| 373 | fn write8(&self, value: u8, offset: usize)
|
| 374 | where
|
| 375 | Self: IoKnownSize + IoCapable<u8>,
|
| 376 | {
|
| 377 | self.write(offset, value)
|
| 378 | }
|
| 379 |
|
| 380 | |
| 381 | #[inline(always)]
|
| 382 | fn write16(&self, value: u16, offset: usize)
|
| 383 | where
|
| 384 | Self: IoKnownSize + IoCapable<u16>,
|
| 385 | {
|
| 386 | self.write(offset, value)
|
| 387 | }
|
| 388 |
|
| 389 | |
| 390 | #[inline(always)]
|
| 391 | fn write32(&self, value: u32, offset: usize)
|
| 392 | where
|
| 393 | Self: IoKnownSize + IoCapable<u32>,
|
| 394 | {
|
| 395 | self.write(offset, value)
|
| 396 | }
|
| 397 |
|
| 398 | |
| 399 | #[inline(always)]
|
| 400 | fn write64(&self, value: u64, offset: usize)
|
| 401 | where
|
| 402 | Self: IoKnownSize + IoCapable<u64>,
|
| 403 | {
|
| 404 | self.write(offset, value)
|
| 405 | }
|
| 406 |
|
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | #[inline(always)]
|
| 430 | fn try_read<T, L>(&self, location: L) -> Result<T>
|
| 431 | where
|
| 432 | L: IoLoc<T>,
|
| 433 | Self: IoCapable<L::IoType>,
|
| 434 | {
|
| 435 | let address = self.io_addr::<L::IoType>(location.offset())?;
|
| 436 |
|
| 437 | |
| 438 | Ok(unsafe { self.io_read(address) }.into())
|
| 439 | }
|
| 440 |
|
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | #[inline(always)]
|
| 464 | fn try_write<T, L>(&self, location: L, value: T) -> Result
|
| 465 | where
|
| 466 | L: IoLoc<T>,
|
| 467 | Self: IoCapable<L::IoType>,
|
| 468 | {
|
| 469 | let address = self.io_addr::<L::IoType>(location.offset())?;
|
| 470 | let io_value = value.into();
|
| 471 |
|
| 472 | |
| 473 | unsafe { self.io_write(io_value, address) }
|
| 474 |
|
| 475 | Ok(())
|
| 476 | }
|
| 477 |
|
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | #[inline(always)]
|
| 510 | fn try_write_reg<T, L, V>(&self, value: V) -> Result
|
| 511 | where
|
| 512 | L: IoLoc<T>,
|
| 513 | V: LocatedRegister<Location = L, Value = T>,
|
| 514 | Self: IoCapable<L::IoType>,
|
| 515 | {
|
| 516 | let (location, value) = value.into_io_op();
|
| 517 |
|
| 518 | self.try_write(location, value)
|
| 519 | }
|
| 520 |
|
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | #[inline(always)]
|
| 543 | fn try_update<T, L, F>(&self, location: L, f: F) -> Result
|
| 544 | where
|
| 545 | L: IoLoc<T>,
|
| 546 | Self: IoCapable<L::IoType>,
|
| 547 | F: FnOnce(T) -> T,
|
| 548 | {
|
| 549 | let address = self.io_addr::<L::IoType>(location.offset())?;
|
| 550 |
|
| 551 | |
| 552 | let value: T = unsafe { self.io_read(address) }.into();
|
| 553 | let io_value = f(value).into();
|
| 554 |
|
| 555 | |
| 556 | unsafe { self.io_write(io_value, address) }
|
| 557 |
|
| 558 | Ok(())
|
| 559 | }
|
| 560 |
|
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | #[inline(always)]
|
| 582 | fn read<T, L>(&self, location: L) -> T
|
| 583 | where
|
| 584 | L: IoLoc<T>,
|
| 585 | Self: IoKnownSize + IoCapable<L::IoType>,
|
| 586 | {
|
| 587 | let address = self.io_addr_assert::<L::IoType>(location.offset());
|
| 588 |
|
| 589 | |
| 590 | unsafe { self.io_read(address) }.into()
|
| 591 | }
|
| 592 |
|
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | #[inline(always)]
|
| 614 | fn write<T, L>(&self, location: L, value: T)
|
| 615 | where
|
| 616 | L: IoLoc<T>,
|
| 617 | Self: IoKnownSize + IoCapable<L::IoType>,
|
| 618 | {
|
| 619 | let address = self.io_addr_assert::<L::IoType>(location.offset());
|
| 620 | let io_value = value.into();
|
| 621 |
|
| 622 | |
| 623 | unsafe { self.io_write(io_value, address) }
|
| 624 | }
|
| 625 |
|
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | #[inline(always)]
|
| 657 | fn write_reg<T, L, V>(&self, value: V)
|
| 658 | where
|
| 659 | L: IoLoc<T>,
|
| 660 | V: LocatedRegister<Location = L, Value = T>,
|
| 661 | Self: IoKnownSize + IoCapable<L::IoType>,
|
| 662 | {
|
| 663 | let (location, value) = value.into_io_op();
|
| 664 |
|
| 665 | self.write(location, value)
|
| 666 | }
|
| 667 |
|
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | #[inline(always)]
|
| 690 | fn update<T, L, F>(&self, location: L, f: F)
|
| 691 | where
|
| 692 | L: IoLoc<T>,
|
| 693 | Self: IoKnownSize + IoCapable<L::IoType> + Sized,
|
| 694 | F: FnOnce(T) -> T,
|
| 695 | {
|
| 696 | let address = self.io_addr_assert::<L::IoType>(location.offset());
|
| 697 |
|
| 698 | |
| 699 | let value: T = unsafe { self.io_read(address) }.into();
|
| 700 | let io_value = f(value).into();
|
| 701 |
|
| 702 | |
| 703 | unsafe { self.io_write(io_value, address) }
|
| 704 | }
|
| 705 | }
|
| 706 |
|
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | pub trait IoKnownSize: Io {
|
| 715 | |
| 716 | const MIN_SIZE: usize;
|
| 717 |
|
| 718 | |
| 719 | |
| 720 | |
| 721 | #[inline(always)]
|
| 722 | fn io_addr_assert<U>(&self, offset: usize) -> usize {
|
| 723 | build_assert!(offset_valid::<U>(offset, Self::MIN_SIZE));
|
| 724 |
|
| 725 | self.addr() + offset
|
| 726 | }
|
| 727 | }
|
| 728 |
|
| 729 | |
| 730 | macro_rules! impl_mmio_io_capable {
|
| 731 | ($mmio:ident, $(#[$attr:meta])* $ty:ty, $read_fn:ident, $write_fn:ident) => {
|
| 732 | $(#[$attr])*
|
| 733 | impl<const SIZE: usize> IoCapable<$ty> for $mmio<SIZE> {
|
| 734 | unsafe fn io_read(&self, address: usize) -> $ty {
|
| 735 | |
| 736 | unsafe { bindings::$read_fn(address as *const c_void) }
|
| 737 | }
|
| 738 |
|
| 739 | unsafe fn io_write(&self, value: $ty, address: usize) {
|
| 740 | |
| 741 | unsafe { bindings::$write_fn(value, address as *mut c_void) }
|
| 742 | }
|
| 743 | }
|
| 744 | };
|
| 745 | }
|
| 746 |
|
| 747 | |
| 748 | impl_mmio_io_capable!(Mmio, u8, readb, writeb);
|
| 749 | impl_mmio_io_capable!(Mmio, u16, readw, writew);
|
| 750 | impl_mmio_io_capable!(Mmio, u32, readl, writel);
|
| 751 | |
| 752 | impl_mmio_io_capable!(
|
| 753 | Mmio,
|
| 754 | #[cfg(CONFIG_64BIT)]
|
| 755 | u64,
|
| 756 | readq,
|
| 757 | writeq
|
| 758 | );
|
| 759 |
|
| 760 | impl<const SIZE: usize> Io for Mmio<SIZE> {
|
| 761 | |
| 762 | #[inline]
|
| 763 | fn addr(&self) -> usize {
|
| 764 | self.0.addr()
|
| 765 | }
|
| 766 |
|
| 767 | |
| 768 | #[inline]
|
| 769 | fn maxsize(&self) -> usize {
|
| 770 | self.0.maxsize()
|
| 771 | }
|
| 772 | }
|
| 773 |
|
| 774 | impl<const SIZE: usize> IoKnownSize for Mmio<SIZE> {
|
| 775 | const MIN_SIZE: usize = SIZE;
|
| 776 | }
|
| 777 |
|
| 778 | impl<const SIZE: usize> Mmio<SIZE> {
|
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | pub unsafe fn from_raw(raw: &MmioRaw<SIZE>) -> &Self {
|
| 786 | |
| 787 | unsafe { &*core::ptr::from_ref(raw).cast() }
|
| 788 | }
|
| 789 | }
|
| 790 |
|
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | #[repr(transparent)]
|
| 798 | pub struct RelaxedMmio<const SIZE: usize = 0>(Mmio<SIZE>);
|
| 799 |
|
| 800 | impl<const SIZE: usize> Io for RelaxedMmio<SIZE> {
|
| 801 | #[inline]
|
| 802 | fn addr(&self) -> usize {
|
| 803 | self.0.addr()
|
| 804 | }
|
| 805 |
|
| 806 | #[inline]
|
| 807 | fn maxsize(&self) -> usize {
|
| 808 | self.0.maxsize()
|
| 809 | }
|
| 810 | }
|
| 811 |
|
| 812 | impl<const SIZE: usize> IoKnownSize for RelaxedMmio<SIZE> {
|
| 813 | const MIN_SIZE: usize = SIZE;
|
| 814 | }
|
| 815 |
|
| 816 | impl<const SIZE: usize> Mmio<SIZE> {
|
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | pub fn relaxed(&self) -> &RelaxedMmio<SIZE> {
|
| 838 | |
| 839 | |
| 840 | unsafe { core::mem::transmute(self) }
|
| 841 | }
|
| 842 | }
|
| 843 |
|
| 844 | |
| 845 | impl_mmio_io_capable!(RelaxedMmio, u8, readb_relaxed, writeb_relaxed);
|
| 846 | impl_mmio_io_capable!(RelaxedMmio, u16, readw_relaxed, writew_relaxed);
|
| 847 | impl_mmio_io_capable!(RelaxedMmio, u32, readl_relaxed, writel_relaxed);
|
| 848 | |
| 849 | impl_mmio_io_capable!(
|
| 850 | RelaxedMmio,
|
| 851 | #[cfg(CONFIG_64BIT)]
|
| 852 | u64,
|
| 853 | readq_relaxed,
|
| 854 | writeq_relaxed
|
| 855 | );
|