blob: e9ac45b0d7d2d64ba8069c706edcc34f537c3e65 [file] [log] [blame]
Andrew Walbran19690632022-12-07 16:41:30 +00001// 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 Walbran0a8dac72022-12-21 13:49:06 +000015//! Functions to scan the PCI bus for VirtIO devices.
Andrew Walbran19690632022-12-07 16:41:30 +000016
Andrew Walbran730375d2022-12-21 14:04:34 +000017use crate::{entry::RebootReason, memory::MemoryTracker};
18use fdtpci::{PciError, PciInfo};
Andrew Walbran19690632022-12-07 16:41:30 +000019use log::{debug, error};
Andrew Walbran730375d2022-12-21 14:04:34 +000020use virtio_drivers::pci::{bus::PciRoot, virtio_device_type};
Andrew Walbran19690632022-12-07 16:41:30 +000021
Andrew Walbran730375d2022-12-21 14:04:34 +000022/// Maps the CAM and BAR range in the page table and MMIO guard.
23pub 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 Walbran19690632022-12-07 16:41:30 +000028
Andrew Walbran730375d2022-12-21 14:04:34 +000029 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 Walbran0d8b54d2022-12-08 16:32:33 +000033 RebootReason::InternalError
34 })?;
35
Andrew Walbran730375d2022-12-21 14:04:34 +000036 Ok(())
Andrew Walbran19690632022-12-07 16:41:30 +000037}
Andrew Walbrand1d03182022-12-09 18:20:01 +000038
Andrew Walbran0a8dac72022-12-21 13:49:06 +000039/// Finds VirtIO PCI devices.
40pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> {
Andrew Walbrand1d03182022-12-09 18:20:01 +000041 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 Walbrand1d03182022-12-09 18:20:01 +000049 }
50 }
51
52 Ok(())
53}