blob: d3b3124880d22057cd3b976167f4cfa8f17c698d [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 Walbran848decf2022-12-15 14:39:38 +000017use super::hal::HalImpl;
Andrew Walbran730375d2022-12-21 14:04:34 +000018use crate::{entry::RebootReason, memory::MemoryTracker};
Andrew Walbranb398fc82023-01-24 14:45:46 +000019use alloc::boxed::Box;
Andrew Walbran730375d2022-12-21 14:04:34 +000020use fdtpci::{PciError, PciInfo};
Andrew Walbran848decf2022-12-15 14:39:38 +000021use log::{debug, error, info};
Andrew Walbranb398fc82023-01-24 14:45:46 +000022use once_cell::race::OnceBox;
Andrew Walbran848decf2022-12-15 14:39:38 +000023use virtio_drivers::{
24 device::blk::VirtIOBlk,
25 transport::{
26 pci::{bus::PciRoot, virtio_device_type, PciTransport},
27 DeviceType, Transport,
28 },
29};
Andrew Walbran19690632022-12-07 16:41:30 +000030
Andrew Walbranb398fc82023-01-24 14:45:46 +000031pub(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.
42pub 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 Walbran730375d2022-12-21 14:04:34 +000052/// Maps the CAM and BAR range in the page table and MMIO guard.
Andrew Walbranb398fc82023-01-24 14:45:46 +000053fn map_mmio(pci_info: &PciInfo, memory: &mut MemoryTracker) -> Result<(), RebootReason> {
Andrew Walbran730375d2022-12-21 14:04:34 +000054 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 Walbran19690632022-12-07 16:41:30 +000058
Andrew Walbran730375d2022-12-21 14:04:34 +000059 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 Walbran0d8b54d2022-12-08 16:32:33 +000063 RebootReason::InternalError
64 })?;
65
Andrew Walbran730375d2022-12-21 14:04:34 +000066 Ok(())
Andrew Walbran19690632022-12-07 16:41:30 +000067}
Andrew Walbrand1d03182022-12-09 18:20:01 +000068
Andrew Walbran0a8dac72022-12-21 13:49:06 +000069/// Finds VirtIO PCI devices.
70pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> {
Andrew Walbrand1d03182022-12-09 18:20:01 +000071 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 Walbran848decf2022-12-15 14:39:38 +000079 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 Walbrand1d03182022-12-09 18:20:01 +000092 }
93 }
94
95 Ok(())
96}