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::{ |
| 26 | pci::{bus::PciRoot, virtio_device_type, PciTransport}, |
| 27 | DeviceType, Transport, |
| 28 | }, |
| 29 | }; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 30 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame^] | 31 | pub(super) static PCI_INFO: OnceBox<PciInfo> = OnceBox::new(); |
| 32 | |
| 33 | /// Prepares to use VirtIO PCI devices. |
| 34 | /// |
| 35 | /// In particular: |
| 36 | /// |
| 37 | /// 1. Maps the PCI CAM and BAR range in the page table and MMIO guard. |
| 38 | /// 2. Stores the `PciInfo` for the VirtIO HAL to use later. |
| 39 | /// 3. Creates and returns a `PciRoot`. |
| 40 | /// |
| 41 | /// This must only be called once; it will panic if it is called a second time. |
| 42 | pub fn initialise(pci_info: PciInfo, memory: &mut MemoryTracker) -> Result<PciRoot, RebootReason> { |
| 43 | map_mmio(&pci_info, memory)?; |
| 44 | |
| 45 | PCI_INFO.set(Box::new(pci_info.clone())).expect("Tried to set PCI_INFO a second time"); |
| 46 | |
| 47 | // Safety: This is the only place where we call make_pci_root, and `PCI_INFO.set` above will |
| 48 | // panic if it is called a second time. |
| 49 | Ok(unsafe { pci_info.make_pci_root() }) |
| 50 | } |
| 51 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 52 | /// 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^] | 53 | fn map_mmio(pci_info: &PciInfo, memory: &mut MemoryTracker) -> Result<(), RebootReason> { |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 54 | memory.map_mmio_range(pci_info.cam_range.clone()).map_err(|e| { |
| 55 | error!("Failed to map PCI CAM: {}", e); |
| 56 | RebootReason::InternalError |
| 57 | })?; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 58 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 59 | memory |
| 60 | .map_mmio_range(pci_info.bar_range.start as usize..pci_info.bar_range.end as usize) |
| 61 | .map_err(|e| { |
| 62 | error!("Failed to map PCI MMIO range: {}", e); |
Andrew Walbran | 0d8b54d | 2022-12-08 16:32:33 +0000 | [diff] [blame] | 63 | RebootReason::InternalError |
| 64 | })?; |
| 65 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 66 | Ok(()) |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 67 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 68 | |
Andrew Walbran | 0a8dac7 | 2022-12-21 13:49:06 +0000 | [diff] [blame] | 69 | /// Finds VirtIO PCI devices. |
| 70 | pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> { |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 71 | for (device_function, info) in pci_root.enumerate_bus(0) { |
| 72 | let (status, command) = pci_root.get_status_command(device_function); |
| 73 | debug!( |
| 74 | "Found PCI device {} at {}, status {:?} command {:?}", |
| 75 | info, device_function, status, command |
| 76 | ); |
| 77 | if let Some(virtio_type) = virtio_device_type(&info) { |
| 78 | debug!(" VirtIO {:?}", virtio_type); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 79 | let mut transport = PciTransport::new::<HalImpl>(pci_root, device_function).unwrap(); |
| 80 | info!( |
| 81 | "Detected virtio PCI device with device type {:?}, features {:#018x}", |
| 82 | transport.device_type(), |
| 83 | transport.read_device_features(), |
| 84 | ); |
| 85 | if virtio_type == DeviceType::Block { |
| 86 | let mut blk = |
| 87 | VirtIOBlk::<HalImpl, _>::new(transport).expect("failed to create blk driver"); |
| 88 | info!("Found {} KiB block device.", blk.capacity() * 512 / 1024); |
| 89 | let mut data = [0; 512]; |
| 90 | blk.read_block(0, &mut data).expect("Failed to read block device"); |
| 91 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | Ok(()) |
| 96 | } |