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 | |
Pierre-Clément Tosi | f2c19d4 | 2024-10-01 17:42:04 +0100 | [diff] [blame] | 17 | use crate::{ |
| 18 | fdt::pci::PciInfo, |
Pierre-Clément Tosi | c26e220 | 2024-11-01 23:12:23 +0000 | [diff] [blame] | 19 | memory::{map_device, MemoryTrackerError}, |
Pierre-Clément Tosi | f2c19d4 | 2024-10-01 17:42:04 +0100 | [diff] [blame] | 20 | }; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 21 | use alloc::boxed::Box; |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 22 | use core::fmt; |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 23 | use core::marker::PhantomData; |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 24 | use log::debug; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 25 | use once_cell::race::OnceBox; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 26 | use virtio_drivers::{ |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 27 | device::{blk, socket}, |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 28 | transport::pci::{ |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 29 | bus::{BusDeviceIterator, ConfigurationAccess, PciRoot}, |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 30 | virtio_device_type, PciTransport, |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 31 | }, |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 32 | Hal, |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 33 | }; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 34 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 35 | pub(super) static PCI_INFO: OnceBox<PciInfo> = OnceBox::new(); |
| 36 | |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 37 | /// PCI errors. |
| 38 | #[derive(Debug, Clone)] |
| 39 | pub enum PciError { |
| 40 | /// Attempted to initialize the PCI more than once. |
| 41 | DuplicateInitialization, |
| 42 | /// Failed to map PCI CAM. |
| 43 | CamMapFailed(MemoryTrackerError), |
| 44 | /// Failed to map PCI BAR. |
| 45 | BarMapFailed(MemoryTrackerError), |
| 46 | } |
| 47 | |
| 48 | impl fmt::Display for PciError { |
| 49 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 50 | match self { |
| 51 | Self::DuplicateInitialization => { |
| 52 | write!(f, "Attempted to initialize the PCI more than once.") |
| 53 | } |
| 54 | Self::CamMapFailed(e) => write!(f, "Failed to map PCI CAM: {e}"), |
| 55 | Self::BarMapFailed(e) => write!(f, "Failed to map PCI BAR: {e}"), |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 60 | /// Prepares to use VirtIO PCI devices. |
| 61 | /// |
| 62 | /// In particular: |
| 63 | /// |
| 64 | /// 1. Maps the PCI CAM and BAR range in the page table and MMIO guard. |
| 65 | /// 2. Stores the `PciInfo` for the VirtIO HAL to use later. |
| 66 | /// 3. Creates and returns a `PciRoot`. |
| 67 | /// |
Pierre-Clément Tosi | c26e220 | 2024-11-01 23:12:23 +0000 | [diff] [blame] | 68 | /// This must only be called once and after having switched to the dynamic page tables. |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 69 | pub fn initialize(pci_info: PciInfo) -> Result<PciRoot<impl ConfigurationAccess>, PciError> { |
Alice Wang | 287de62 | 2023-06-08 13:17:03 +0000 | [diff] [blame] | 70 | PCI_INFO.set(Box::new(pci_info.clone())).map_err(|_| PciError::DuplicateInitialization)?; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 71 | |
Pierre-Clément Tosi | c26e220 | 2024-11-01 23:12:23 +0000 | [diff] [blame] | 72 | let cam_start = pci_info.cam_range.start; |
| 73 | let cam_size = pci_info.cam_range.len().try_into().unwrap(); |
| 74 | map_device(cam_start, cam_size).map_err(PciError::CamMapFailed)?; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 75 | |
Pierre-Clément Tosi | c26e220 | 2024-11-01 23:12:23 +0000 | [diff] [blame] | 76 | let bar_start = pci_info.bar_range.start.try_into().unwrap(); |
| 77 | let bar_size = pci_info.bar_range.len().try_into().unwrap(); |
| 78 | map_device(bar_start, bar_size).map_err(PciError::BarMapFailed)?; |
| 79 | |
| 80 | // SAFETY: This is the only place where we call make_pci_root, validated by `PCI_INFO.set`. |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 81 | Ok(unsafe { pci_info.make_pci_root() }) |
| 82 | } |
| 83 | |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 84 | /// Virtio Block device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 85 | pub type VirtIOBlk<T> = blk::VirtIOBlk<T, PciTransport>; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 86 | |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 87 | /// Virtio Socket device. |
| 88 | /// |
| 89 | /// Spec: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html 5.10 |
| 90 | pub type VirtIOSocket<T> = socket::VirtIOSocket<T, PciTransport>; |
| 91 | |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 92 | /// An iterator that iterates over the PCI transport for each device. |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 93 | pub struct PciTransportIterator<'a, T: Hal, C: ConfigurationAccess> { |
| 94 | pci_root: &'a mut PciRoot<C>, |
| 95 | bus: BusDeviceIterator<C>, |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 96 | _hal: PhantomData<T>, |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 99 | impl<'a, T: Hal, C: ConfigurationAccess> PciTransportIterator<'a, T, C> { |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 100 | /// Creates a new iterator. |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 101 | pub fn new(pci_root: &'a mut PciRoot<C>) -> Self { |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 102 | let bus = pci_root.enumerate_bus(0); |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 103 | Self { pci_root, bus, _hal: PhantomData } |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Chris Wailes | 52358e9 | 2025-01-27 17:04:40 -0800 | [diff] [blame] | 107 | impl<T: Hal, C: ConfigurationAccess> Iterator for PciTransportIterator<'_, T, C> { |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 108 | type Item = PciTransport; |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 109 | |
| 110 | fn next(&mut self) -> Option<Self::Item> { |
| 111 | loop { |
| 112 | let (device_function, info) = self.bus.next()?; |
| 113 | let (status, command) = self.pci_root.get_status_command(device_function); |
| 114 | debug!( |
| 115 | "Found PCI device {} at {}, status {:?} command {:?}", |
| 116 | info, device_function, status, command |
| 117 | ); |
| 118 | |
Pierre-Clément Tosi | ebb3760 | 2023-02-17 14:57:26 +0000 | [diff] [blame] | 119 | let Some(virtio_type) = virtio_device_type(&info) else { |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 120 | continue; |
| 121 | }; |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 122 | debug!(" VirtIO {:?}", virtio_type); |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 123 | |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 124 | return PciTransport::new::<T, C>(self.pci_root, device_function).ok(); |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 127 | } |