blob: c0a2d2b4edef2f3ba348c816609c74b59a186672 [file] [log] [blame]
Andrew Walbran8217d062022-11-22 16:56:18 +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 device.
Andrew Walbran8217d062022-11-22 16:56:18 +000016
17use aarch64_paging::paging::MemoryRegion;
Andrew Walbran272bd7a2023-01-24 14:02:36 +000018use alloc::alloc::{alloc, dealloc, handle_alloc_error, Layout};
Andrew Walbran336d0cb2023-01-06 16:33:15 +000019use core::{mem::size_of, ptr::NonNull};
Andrew Walbran730375d2022-12-21 14:04:34 +000020use fdtpci::PciInfo;
Andrew Walbran8217d062022-11-22 16:56:18 +000021use log::{debug, info};
22use virtio_drivers::{
Andrew Walbran336d0cb2023-01-06 16:33:15 +000023 device::blk::VirtIOBlk,
24 transport::{
25 pci::{bus::PciRoot, virtio_device_type, PciTransport},
26 DeviceType, Transport,
27 },
Andrew Walbran272bd7a2023-01-24 14:02:36 +000028 BufferDirection, Hal, PhysAddr, PAGE_SIZE,
Andrew Walbran8217d062022-11-22 16:56:18 +000029};
30
31/// The standard sector size of a VirtIO block device, in bytes.
Andrew Walbranb713baa2022-12-07 14:34:49 +000032const SECTOR_SIZE_BYTES: usize = 512;
33
34/// The size in sectors of the test block device we expect.
35const EXPECTED_SECTOR_COUNT: usize = 4;
Andrew Walbran8217d062022-11-22 16:56:18 +000036
Andrew Walbran730375d2022-12-21 14:04:34 +000037pub fn check_pci(pci_root: &mut PciRoot) {
Andrew Walbranb713baa2022-12-07 14:34:49 +000038 let mut checked_virtio_device_count = 0;
Andrew Walbran8217d062022-11-22 16:56:18 +000039 for (device_function, info) in pci_root.enumerate_bus(0) {
40 let (status, command) = pci_root.get_status_command(device_function);
41 info!("Found {} at {}, status {:?} command {:?}", info, device_function, status, command);
42 if let Some(virtio_type) = virtio_device_type(&info) {
43 info!(" VirtIO {:?}", virtio_type);
Andrew Walbran730375d2022-12-21 14:04:34 +000044 let mut transport = PciTransport::new::<HalImpl>(pci_root, device_function).unwrap();
Andrew Walbran8217d062022-11-22 16:56:18 +000045 info!(
46 "Detected virtio PCI device with device type {:?}, features {:#018x}",
47 transport.device_type(),
48 transport.read_device_features(),
49 );
Andrew Walbranb713baa2022-12-07 14:34:49 +000050 if check_virtio_device(transport, virtio_type) {
51 checked_virtio_device_count += 1;
52 }
Andrew Walbran8217d062022-11-22 16:56:18 +000053 }
54 }
Andrew Walbranb713baa2022-12-07 14:34:49 +000055
56 assert_eq!(checked_virtio_device_count, 1);
Andrew Walbran8217d062022-11-22 16:56:18 +000057}
58
Andrew Walbranb713baa2022-12-07 14:34:49 +000059/// Checks the given VirtIO device, if we know how to.
60///
61/// Returns true if the device was checked, or false if it was ignored.
62fn check_virtio_device(transport: impl Transport, device_type: DeviceType) -> bool {
Andrew Walbran8217d062022-11-22 16:56:18 +000063 if device_type == DeviceType::Block {
Andrew Walbranb713baa2022-12-07 14:34:49 +000064 let mut blk = VirtIOBlk::<HalImpl, _>::new(transport).expect("failed to create blk driver");
65 info!("Found {} KiB block device.", blk.capacity() * SECTOR_SIZE_BYTES as u64 / 1024);
66 assert_eq!(blk.capacity(), EXPECTED_SECTOR_COUNT as u64);
67 let mut data = [0; SECTOR_SIZE_BYTES * EXPECTED_SECTOR_COUNT];
68 for i in 0..EXPECTED_SECTOR_COUNT {
69 blk.read_block(i, &mut data[i * SECTOR_SIZE_BYTES..(i + 1) * SECTOR_SIZE_BYTES])
70 .expect("Failed to read block device.");
71 }
72 for (i, chunk) in data.chunks(size_of::<u32>()).enumerate() {
73 assert_eq!(chunk, &(i as u32).to_le_bytes());
74 }
75 info!("Read expected data from block device.");
76 true
77 } else {
78 false
Andrew Walbran8217d062022-11-22 16:56:18 +000079 }
80}
81
Andrew Walbran730375d2022-12-21 14:04:34 +000082/// Gets the memory region in which BARs are allocated.
83pub fn get_bar_region(pci_info: &PciInfo) -> MemoryRegion {
84 MemoryRegion::new(pci_info.bar_range.start as usize, pci_info.bar_range.end as usize)
Andrew Walbran8217d062022-11-22 16:56:18 +000085}
86
Andrew Walbran8217d062022-11-22 16:56:18 +000087struct HalImpl;
88
89impl Hal for HalImpl {
Andrew Walbran272bd7a2023-01-24 14:02:36 +000090 fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) {
Andrew Walbran8217d062022-11-22 16:56:18 +000091 debug!("dma_alloc: pages={}", pages);
92 let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap();
93 // Safe because the layout has a non-zero size.
Andrew Walbran272bd7a2023-01-24 14:02:36 +000094 let vaddr = unsafe { alloc(layout) };
95 let vaddr =
96 if let Some(vaddr) = NonNull::new(vaddr) { vaddr } else { handle_alloc_error(layout) };
97 let paddr = virt_to_phys(vaddr);
98 (paddr, vaddr)
Andrew Walbran8217d062022-11-22 16:56:18 +000099 }
100
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000101 fn dma_dealloc(paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 {
Andrew Walbran8217d062022-11-22 16:56:18 +0000102 debug!("dma_dealloc: paddr={:#x}, pages={}", paddr, pages);
Andrew Walbran8217d062022-11-22 16:56:18 +0000103 let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap();
104 // Safe because the memory was allocated by `dma_alloc` above using the same allocator, and
105 // the layout is the same as was used then.
106 unsafe {
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000107 dealloc(vaddr.as_ptr(), layout);
Andrew Walbran8217d062022-11-22 16:56:18 +0000108 }
109 0
110 }
111
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000112 fn mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8> {
113 NonNull::new(paddr as _).unwrap()
Andrew Walbran8217d062022-11-22 16:56:18 +0000114 }
115
Andrew Walbran336d0cb2023-01-06 16:33:15 +0000116 fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr {
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000117 let vaddr = buffer.cast();
Andrew Walbran336d0cb2023-01-06 16:33:15 +0000118 // Nothing to do, as the host already has access to all memory.
119 virt_to_phys(vaddr)
Andrew Walbran8217d062022-11-22 16:56:18 +0000120 }
Andrew Walbran336d0cb2023-01-06 16:33:15 +0000121
122 fn unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection) {
123 // Nothing to do, as the host already has access to all memory and we didn't copy the buffer
124 // anywhere else.
125 }
126}
127
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000128fn virt_to_phys(vaddr: NonNull<u8>) -> PhysAddr {
129 vaddr.as_ptr() as _
Andrew Walbran8217d062022-11-22 16:56:18 +0000130}