Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +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 device. |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 16 | |
| 17 | use aarch64_paging::paging::MemoryRegion; |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 18 | use alloc::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout}; |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 19 | use core::{mem::size_of, ptr::NonNull}; |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 20 | use fdtpci::PciInfo; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 21 | use log::{debug, info}; |
| 22 | use virtio_drivers::{ |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 23 | device::console::VirtIOConsole, |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 24 | transport::{ |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 25 | pci::{bus::PciRoot, PciTransport}, |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 26 | DeviceType, Transport, |
| 27 | }, |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 28 | BufferDirection, Error, Hal, PhysAddr, PAGE_SIZE, |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 29 | }; |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 30 | use vmbase::virtio::pci::{self, PciTransportIterator}; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 31 | |
| 32 | /// The standard sector size of a VirtIO block device, in bytes. |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 33 | const SECTOR_SIZE_BYTES: usize = 512; |
| 34 | |
| 35 | /// The size in sectors of the test block device we expect. |
| 36 | const EXPECTED_SECTOR_COUNT: usize = 4; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 37 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 38 | pub fn check_pci(pci_root: &mut PciRoot) { |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 39 | let mut checked_virtio_device_count = 0; |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 40 | let mut block_device_count = 0; |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 41 | for mut transport in PciTransportIterator::<HalImpl>::new(pci_root) { |
| 42 | info!( |
| 43 | "Detected virtio PCI device with device type {:?}, features {:#018x}", |
| 44 | transport.device_type(), |
| 45 | transport.read_device_features(), |
| 46 | ); |
| 47 | match transport.device_type() { |
| 48 | DeviceType::Block => { |
| 49 | check_virtio_block_device(transport, block_device_count); |
| 50 | block_device_count += 1; |
| 51 | checked_virtio_device_count += 1; |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 52 | } |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 53 | DeviceType::Console => { |
| 54 | check_virtio_console_device(transport); |
| 55 | checked_virtio_device_count += 1; |
| 56 | } |
| 57 | _ => {} |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 60 | |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 61 | assert_eq!(checked_virtio_device_count, 5); |
| 62 | assert_eq!(block_device_count, 2); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 65 | /// Checks the given VirtIO block device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 66 | fn check_virtio_block_device(transport: PciTransport, index: usize) { |
| 67 | let mut blk = pci::VirtIOBlk::<HalImpl>::new(transport).expect("failed to create blk driver"); |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 68 | info!("Found {} KiB block device.", blk.capacity() * SECTOR_SIZE_BYTES as u64 / 1024); |
| 69 | match index { |
| 70 | 0 => { |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 71 | assert_eq!(blk.capacity(), EXPECTED_SECTOR_COUNT as u64); |
| 72 | let mut data = [0; SECTOR_SIZE_BYTES * EXPECTED_SECTOR_COUNT]; |
| 73 | for i in 0..EXPECTED_SECTOR_COUNT { |
| 74 | blk.read_block(i, &mut data[i * SECTOR_SIZE_BYTES..(i + 1) * SECTOR_SIZE_BYTES]) |
| 75 | .expect("Failed to read block device."); |
| 76 | } |
| 77 | for (i, chunk) in data.chunks(size_of::<u32>()).enumerate() { |
| 78 | assert_eq!(chunk, &(i as u32).to_le_bytes()); |
| 79 | } |
| 80 | info!("Read expected data from block device."); |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 81 | } |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 82 | 1 => { |
| 83 | assert_eq!(blk.capacity(), 0); |
| 84 | let mut data = [0; SECTOR_SIZE_BYTES]; |
| 85 | assert_eq!(blk.read_block(0, &mut data), Err(Error::IoError)); |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 86 | } |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 87 | _ => panic!("Unexpected VirtIO block device index {}.", index), |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 91 | /// Checks the given VirtIO console device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame^] | 92 | fn check_virtio_console_device(transport: PciTransport) { |
| 93 | let mut console = VirtIOConsole::<HalImpl, PciTransport>::new(transport) |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 94 | .expect("Failed to create VirtIO console driver"); |
| 95 | info!("Found console device: {:?}", console.info()); |
| 96 | for &c in b"Hello VirtIO console\n" { |
| 97 | console.send(c).expect("Failed to send character to VirtIO console device"); |
| 98 | } |
| 99 | info!("Wrote to VirtIO console."); |
| 100 | } |
| 101 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 102 | /// Gets the memory region in which BARs are allocated. |
| 103 | pub fn get_bar_region(pci_info: &PciInfo) -> MemoryRegion { |
| 104 | MemoryRegion::new(pci_info.bar_range.start as usize, pci_info.bar_range.end as usize) |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 107 | struct HalImpl; |
| 108 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 109 | unsafe impl Hal for HalImpl { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 110 | fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) { |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 111 | debug!("dma_alloc: pages={}", pages); |
| 112 | let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap(); |
| 113 | // Safe because the layout has a non-zero size. |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 114 | let vaddr = unsafe { alloc_zeroed(layout) }; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 115 | let vaddr = |
| 116 | if let Some(vaddr) = NonNull::new(vaddr) { vaddr } else { handle_alloc_error(layout) }; |
| 117 | let paddr = virt_to_phys(vaddr); |
| 118 | (paddr, vaddr) |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 121 | unsafe fn dma_dealloc(paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 { |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 122 | debug!("dma_dealloc: paddr={:#x}, pages={}", paddr, pages); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 123 | let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap(); |
| 124 | // Safe because the memory was allocated by `dma_alloc` above using the same allocator, and |
| 125 | // the layout is the same as was used then. |
| 126 | unsafe { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 127 | dealloc(vaddr.as_ptr(), layout); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 128 | } |
| 129 | 0 |
| 130 | } |
| 131 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 132 | unsafe fn mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8> { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 133 | NonNull::new(paddr as _).unwrap() |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 136 | unsafe fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 137 | let vaddr = buffer.cast(); |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 138 | // Nothing to do, as the host already has access to all memory. |
| 139 | virt_to_phys(vaddr) |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 140 | } |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 141 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 142 | unsafe fn unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection) { |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 143 | // Nothing to do, as the host already has access to all memory and we didn't copy the buffer |
| 144 | // anywhere else. |
| 145 | } |
| 146 | } |
| 147 | |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 148 | fn virt_to_phys(vaddr: NonNull<u8>) -> PhysAddr { |
| 149 | vaddr.as_ptr() as _ |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 150 | } |