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 | |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 17 | use super::hal::HalImpl; |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 18 | use crate::{entry::RebootReason, memory::MemoryTracker}; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 19 | use alloc::boxed::Box; |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 20 | use fdtpci::{PciError, PciInfo}; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 21 | use log::{debug, error, info}; |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 22 | use once_cell::race::OnceBox; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 23 | use virtio_drivers::{ |
| 24 | device::blk::VirtIOBlk, |
| 25 | transport::{ |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 26 | pci::{ |
| 27 | bus::{BusDeviceIterator, PciRoot}, |
| 28 | virtio_device_type, PciTransport, |
| 29 | }, |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 30 | DeviceType, Transport, |
| 31 | }, |
| 32 | }; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 33 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 34 | pub(super) static PCI_INFO: OnceBox<PciInfo> = OnceBox::new(); |
| 35 | |
| 36 | /// Prepares to use VirtIO PCI devices. |
| 37 | /// |
| 38 | /// In particular: |
| 39 | /// |
| 40 | /// 1. Maps the PCI CAM and BAR range in the page table and MMIO guard. |
| 41 | /// 2. Stores the `PciInfo` for the VirtIO HAL to use later. |
| 42 | /// 3. Creates and returns a `PciRoot`. |
| 43 | /// |
| 44 | /// This must only be called once; it will panic if it is called a second time. |
| 45 | pub fn initialise(pci_info: PciInfo, memory: &mut MemoryTracker) -> Result<PciRoot, RebootReason> { |
| 46 | map_mmio(&pci_info, memory)?; |
| 47 | |
| 48 | PCI_INFO.set(Box::new(pci_info.clone())).expect("Tried to set PCI_INFO a second time"); |
| 49 | |
| 50 | // Safety: This is the only place where we call make_pci_root, and `PCI_INFO.set` above will |
| 51 | // panic if it is called a second time. |
| 52 | Ok(unsafe { pci_info.make_pci_root() }) |
| 53 | } |
| 54 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 55 | /// Maps the CAM and BAR range in the page table and MMIO guard. |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 56 | fn map_mmio(pci_info: &PciInfo, memory: &mut MemoryTracker) -> Result<(), RebootReason> { |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 57 | memory.map_mmio_range(pci_info.cam_range.clone()).map_err(|e| { |
| 58 | error!("Failed to map PCI CAM: {}", e); |
| 59 | RebootReason::InternalError |
| 60 | })?; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 61 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 62 | memory |
| 63 | .map_mmio_range(pci_info.bar_range.start as usize..pci_info.bar_range.end as usize) |
| 64 | .map_err(|e| { |
| 65 | error!("Failed to map PCI MMIO range: {}", e); |
Andrew Walbran | 0d8b54d | 2022-12-08 16:32:33 +0000 | [diff] [blame] | 66 | RebootReason::InternalError |
| 67 | })?; |
| 68 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 69 | Ok(()) |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 70 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 71 | |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 72 | struct VirtIOBlkIterator<'a> { |
| 73 | pci_root: &'a mut PciRoot, |
| 74 | bus: BusDeviceIterator, |
| 75 | } |
| 76 | |
| 77 | impl<'a> VirtIOBlkIterator<'a> { |
| 78 | pub fn new(pci_root: &'a mut PciRoot) -> Self { |
| 79 | let bus = pci_root.enumerate_bus(0); |
| 80 | Self { pci_root, bus } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl<'a> Iterator for VirtIOBlkIterator<'a> { |
| 85 | type Item = VirtIOBlk<HalImpl, PciTransport>; |
| 86 | |
| 87 | fn next(&mut self) -> Option<Self::Item> { |
| 88 | loop { |
| 89 | let (device_function, info) = self.bus.next()?; |
| 90 | let (status, command) = self.pci_root.get_status_command(device_function); |
| 91 | debug!( |
| 92 | "Found PCI device {} at {}, status {:?} command {:?}", |
| 93 | info, device_function, status, command |
| 94 | ); |
| 95 | |
Pierre-Clément Tosi | ebb3760 | 2023-02-17 14:57:26 +0000 | [diff] [blame^] | 96 | let Some(virtio_type) = virtio_device_type(&info) else { |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 97 | continue; |
| 98 | }; |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 99 | debug!(" VirtIO {:?}", virtio_type); |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 100 | |
| 101 | let mut transport = |
| 102 | PciTransport::new::<HalImpl>(self.pci_root, device_function).unwrap(); |
| 103 | debug!( |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 104 | "Detected virtio PCI device with device type {:?}, features {:#018x}", |
| 105 | transport.device_type(), |
| 106 | transport.read_device_features(), |
| 107 | ); |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 108 | |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 109 | if virtio_type == DeviceType::Block { |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 110 | return Some(Self::Item::new(transport).expect("failed to create blk driver")); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 111 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Pierre-Clément Tosi | e8ec039 | 2023-01-16 15:38:31 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /// Finds VirtIO PCI devices. |
| 117 | pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> { |
| 118 | for mut blk in VirtIOBlkIterator::new(pci_root) { |
| 119 | info!("Found {} KiB block device.", blk.capacity() * 512 / 1024); |
| 120 | let mut data = [0; 512]; |
| 121 | blk.read_block(0, &mut data).expect("Failed to read block device"); |
| 122 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 123 | |
| 124 | Ok(()) |
| 125 | } |