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 | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 17 | use crate::{entry::RebootReason, memory::MemoryTracker}; |
| 18 | use fdtpci::{PciError, PciInfo}; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 19 | use log::{debug, error}; |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 20 | use virtio_drivers::transport::pci::{bus::PciRoot, virtio_device_type}; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 21 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 22 | /// Maps the CAM and BAR range in the page table and MMIO guard. |
| 23 | pub fn map_mmio(pci_info: &PciInfo, memory: &mut MemoryTracker) -> Result<(), RebootReason> { |
| 24 | memory.map_mmio_range(pci_info.cam_range.clone()).map_err(|e| { |
| 25 | error!("Failed to map PCI CAM: {}", e); |
| 26 | RebootReason::InternalError |
| 27 | })?; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 28 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 29 | memory |
| 30 | .map_mmio_range(pci_info.bar_range.start as usize..pci_info.bar_range.end as usize) |
| 31 | .map_err(|e| { |
| 32 | error!("Failed to map PCI MMIO range: {}", e); |
Andrew Walbran | 0d8b54d | 2022-12-08 16:32:33 +0000 | [diff] [blame] | 33 | RebootReason::InternalError |
| 34 | })?; |
| 35 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 36 | Ok(()) |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 37 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 38 | |
Andrew Walbran | 0a8dac7 | 2022-12-21 13:49:06 +0000 | [diff] [blame] | 39 | /// Finds VirtIO PCI devices. |
| 40 | pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> { |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 41 | for (device_function, info) in pci_root.enumerate_bus(0) { |
| 42 | let (status, command) = pci_root.get_status_command(device_function); |
| 43 | debug!( |
| 44 | "Found PCI device {} at {}, status {:?} command {:?}", |
| 45 | info, device_function, status, command |
| 46 | ); |
| 47 | if let Some(virtio_type) = virtio_device_type(&info) { |
| 48 | debug!(" VirtIO {:?}", virtio_type); |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | Ok(()) |
| 53 | } |