Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Andrew Walbran | 0a8dac7 | 2022-12-21 13:49:06 +0000 | [diff] [blame] | 15 | //! Functions to scan the PCI bus for VirtIO devices. |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 16 | |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 17 | use crate::memory::{MemoryTracker, MemoryTrackerError}; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 18 | use alloc::boxed::Box; |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 19 | use core::fmt; |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 20 | use core::marker::PhantomData; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 21 | use fdtpci::PciInfo; |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 22 | use log::debug; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 23 | use once_cell::race::OnceBox; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 24 | use virtio_drivers::{ |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 25 | device::blk, |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 26 | transport::pci::{ |
| 27 | bus::{BusDeviceIterator, PciRoot}, |
| 28 | virtio_device_type, PciTransport, |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 29 | }, |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 30 | Hal, |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 31 | }; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 32 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 33 | pub(super) static PCI_INFO: OnceBox<PciInfo> = OnceBox::new(); |
| 34 | |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 35 | /// PCI errors. |
| 36 | #[derive(Debug, Clone)] |
| 37 | pub enum PciError { |
| 38 | /// Attempted to initialize the PCI more than once. |
| 39 | DuplicateInitialization, |
| 40 | /// Failed to map PCI CAM. |
| 41 | CamMapFailed(MemoryTrackerError), |
| 42 | /// Failed to map PCI BAR. |
| 43 | BarMapFailed(MemoryTrackerError), |
| 44 | } |
| 45 | |
| 46 | impl fmt::Display for PciError { |
| 47 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 48 | match self { |
| 49 | Self::DuplicateInitialization => { |
| 50 | write!(f, "Attempted to initialize the PCI more than once.") |
| 51 | } |
| 52 | Self::CamMapFailed(e) => write!(f, "Failed to map PCI CAM: {e}"), |
| 53 | Self::BarMapFailed(e) => write!(f, "Failed to map PCI BAR: {e}"), |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 58 | /// Prepares to use VirtIO PCI devices. |
| 59 | /// |
| 60 | /// In particular: |
| 61 | /// |
| 62 | /// 1. Maps the PCI CAM and BAR range in the page table and MMIO guard. |
| 63 | /// 2. Stores the `PciInfo` for the VirtIO HAL to use later. |
| 64 | /// 3. Creates and returns a `PciRoot`. |
| 65 | /// |
| 66 | /// This must only be called once; it will panic if it is called a second time. |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 67 | pub fn initialize(pci_info: PciInfo, memory: &mut MemoryTracker) -> Result<PciRoot, PciError> { |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 68 | PCI_INFO.set(Box::new(pci_info.clone())).map_err(|_| PciError::DuplicateInitialization)?; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 69 | |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 70 | memory.map_mmio_range(pci_info.cam_range.clone()).map_err(PciError::CamMapFailed)?; |
| 71 | let bar_range = pci_info.bar_range.start as usize..pci_info.bar_range.end as usize; |
| 72 | memory.map_mmio_range(bar_range).map_err(PciError::BarMapFailed)?; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 73 | |
| 74 | // Safety: This is the only place where we call make_pci_root, and `PCI_INFO.set` above will |
| 75 | // panic if it is called a second time. |
| 76 | Ok(unsafe { pci_info.make_pci_root() }) |
| 77 | } |
| 78 | |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 79 | /// Virtio Block device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 80 | pub type VirtIOBlk<T> = blk::VirtIOBlk<T, PciTransport>; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 81 | |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 82 | /// An iterator that iterates over the PCI transport for each device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 83 | pub struct PciTransportIterator<'a, T: Hal> { |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 84 | pci_root: &'a mut PciRoot, |
| 85 | bus: BusDeviceIterator, |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 86 | _hal: PhantomData<T>, |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 89 | impl<'a, T: Hal> PciTransportIterator<'a, T> { |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 90 | /// Creates a new iterator. |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 91 | pub fn new(pci_root: &'a mut PciRoot) -> Self { |
| 92 | let bus = pci_root.enumerate_bus(0); |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 93 | Self { pci_root, bus, _hal: PhantomData } |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 97 | impl<'a, T: Hal> Iterator for PciTransportIterator<'a, T> { |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 98 | type Item = PciTransport; |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 99 | |
| 100 | fn next(&mut self) -> Option<Self::Item> { |
| 101 | loop { |
| 102 | let (device_function, info) = self.bus.next()?; |
| 103 | let (status, command) = self.pci_root.get_status_command(device_function); |
| 104 | debug!( |
| 105 | "Found PCI device {} at {}, status {:?} command {:?}", |
| 106 | info, device_function, status, command |
| 107 | ); |
| 108 | |
Pierre-Clément Tosi | ebb3760 | 2023-02-17 14:57:26 +0000 | [diff] [blame] | 109 | let Some(virtio_type) = virtio_device_type(&info) else { |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 110 | continue; |
| 111 | }; |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 112 | debug!(" VirtIO {:?}", virtio_type); |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 113 | |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 114 | return PciTransport::new::<T>(self.pci_root, device_function).ok(); |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 117 | } |