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 | |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 17 | use alloc::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout}; |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 18 | use core::{mem::size_of, ptr::NonNull}; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 19 | use log::{debug, info}; |
| 20 | use virtio_drivers::{ |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 21 | device::console::VirtIOConsole, |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 22 | transport::{ |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 23 | pci::{ |
| 24 | bus::{ConfigurationAccess, PciRoot}, |
| 25 | PciTransport, |
| 26 | }, |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 27 | DeviceType, Transport, |
| 28 | }, |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 29 | BufferDirection, Error, Hal, PhysAddr, PAGE_SIZE, |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 30 | }; |
Pierre-Clément Tosi | d2c5472 | 2024-11-02 12:54:40 +0000 | [diff] [blame] | 31 | use vmbase::virtio::pci::{self, PciTransportIterator}; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 32 | |
| 33 | /// The standard sector size of a VirtIO block device, in bytes. |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 34 | const SECTOR_SIZE_BYTES: usize = 512; |
| 35 | |
| 36 | /// The size in sectors of the test block device we expect. |
| 37 | const EXPECTED_SECTOR_COUNT: usize = 4; |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 38 | |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 39 | pub fn check_pci(pci_root: &mut PciRoot<impl ConfigurationAccess>) { |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 40 | let mut checked_virtio_device_count = 0; |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 41 | let mut block_device_count = 0; |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 42 | let mut socket_device_count = 0; |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 43 | for mut transport in PciTransportIterator::<HalImpl, _>::new(pci_root) { |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 44 | info!( |
| 45 | "Detected virtio PCI device with device type {:?}, features {:#018x}", |
| 46 | transport.device_type(), |
| 47 | transport.read_device_features(), |
| 48 | ); |
| 49 | match transport.device_type() { |
| 50 | DeviceType::Block => { |
| 51 | check_virtio_block_device(transport, block_device_count); |
| 52 | block_device_count += 1; |
| 53 | checked_virtio_device_count += 1; |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 54 | } |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 55 | DeviceType::Console => { |
| 56 | check_virtio_console_device(transport); |
| 57 | checked_virtio_device_count += 1; |
| 58 | } |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 59 | DeviceType::Socket => { |
| 60 | check_virtio_socket_device(transport); |
| 61 | socket_device_count += 1; |
| 62 | checked_virtio_device_count += 1; |
| 63 | } |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 64 | _ => {} |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 67 | |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 68 | assert_eq!(checked_virtio_device_count, 6); |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 69 | assert_eq!(block_device_count, 2); |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 70 | assert_eq!(socket_device_count, 1); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 73 | /// Checks the given VirtIO block device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 74 | fn check_virtio_block_device(transport: PciTransport, index: usize) { |
| 75 | 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] | 76 | info!("Found {} KiB block device.", blk.capacity() * SECTOR_SIZE_BYTES as u64 / 1024); |
| 77 | match index { |
| 78 | 0 => { |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 79 | assert_eq!(blk.capacity(), EXPECTED_SECTOR_COUNT as u64); |
| 80 | let mut data = [0; SECTOR_SIZE_BYTES * EXPECTED_SECTOR_COUNT]; |
| 81 | for i in 0..EXPECTED_SECTOR_COUNT { |
Alice Wang | b27ec8f | 2023-07-27 13:19:26 +0000 | [diff] [blame] | 82 | blk.read_blocks(i, &mut data[i * SECTOR_SIZE_BYTES..(i + 1) * SECTOR_SIZE_BYTES]) |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 83 | .expect("Failed to read block device."); |
| 84 | } |
| 85 | for (i, chunk) in data.chunks(size_of::<u32>()).enumerate() { |
| 86 | assert_eq!(chunk, &(i as u32).to_le_bytes()); |
| 87 | } |
| 88 | info!("Read expected data from block device."); |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 89 | } |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 90 | 1 => { |
| 91 | assert_eq!(blk.capacity(), 0); |
| 92 | let mut data = [0; SECTOR_SIZE_BYTES]; |
Alice Wang | b27ec8f | 2023-07-27 13:19:26 +0000 | [diff] [blame] | 93 | assert_eq!(blk.read_blocks(0, &mut data), Err(Error::IoError)); |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 94 | } |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 95 | _ => panic!("Unexpected VirtIO block device index {}.", index), |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Alice Wang | 16e7c3f | 2023-07-06 08:06:00 +0000 | [diff] [blame] | 99 | /// Checks the given VirtIO socket device. |
| 100 | fn check_virtio_socket_device(transport: PciTransport) { |
| 101 | let socket = pci::VirtIOSocket::<HalImpl>::new(transport) |
| 102 | .expect("Failed to create VirtIO socket driver"); |
| 103 | info!("Found socket device: guest_cid={}", socket.guest_cid()); |
| 104 | } |
| 105 | |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 106 | /// Checks the given VirtIO console device. |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 107 | fn check_virtio_console_device(transport: PciTransport) { |
| 108 | let mut console = VirtIOConsole::<HalImpl, PciTransport>::new(transport) |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 109 | .expect("Failed to create VirtIO console driver"); |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 110 | info!( |
| 111 | "Found console device with size {:?}", |
| 112 | console.size().expect("Failed to get size of VirtIO console device") |
| 113 | ); |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 114 | for &c in b"Hello VirtIO console\n" { |
| 115 | console.send(c).expect("Failed to send character to VirtIO console device"); |
| 116 | } |
| 117 | info!("Wrote to VirtIO console."); |
| 118 | } |
| 119 | |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 120 | struct HalImpl; |
| 121 | |
Andrew Walbran | 4784280 | 2023-07-05 16:56:08 +0000 | [diff] [blame] | 122 | /// SAFETY: See the 'Implementation Safety' comments on methods below for how they fulfill the |
| 123 | /// safety requirements of the unsafe `Hal` trait. |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 124 | unsafe impl Hal for HalImpl { |
Andrew Walbran | 4784280 | 2023-07-05 16:56:08 +0000 | [diff] [blame] | 125 | /// # Implementation Safety |
| 126 | /// |
| 127 | /// `dma_alloc` ensures the returned DMA buffer is not aliased with any other allocation or |
| 128 | /// reference in the program until it is deallocated by `dma_dealloc` by allocating a unique |
| 129 | /// block of memory using `alloc_zeroed`, which is guaranteed to allocate valid, unique and |
| 130 | /// zeroed memory. We request an alignment of at least `PAGE_SIZE` from `alloc_zeroed`. |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 131 | fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) { |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 132 | debug!("dma_alloc: pages={}", pages); |
Andrew Walbran | 4784280 | 2023-07-05 16:56:08 +0000 | [diff] [blame] | 133 | let layout = |
| 134 | Layout::from_size_align(pages.checked_mul(PAGE_SIZE).unwrap(), PAGE_SIZE).unwrap(); |
| 135 | assert_ne!(layout.size(), 0); |
| 136 | // SAFETY: We just checked that the layout has a non-zero size. |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 137 | let vaddr = unsafe { alloc_zeroed(layout) }; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 138 | let vaddr = |
| 139 | if let Some(vaddr) = NonNull::new(vaddr) { vaddr } else { handle_alloc_error(layout) }; |
| 140 | let paddr = virt_to_phys(vaddr); |
| 141 | (paddr, vaddr) |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 144 | unsafe fn dma_dealloc(paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 { |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 145 | debug!("dma_dealloc: paddr={:#x}, pages={}", paddr, pages); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 146 | let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap(); |
Andrew Walbran | 4784280 | 2023-07-05 16:56:08 +0000 | [diff] [blame] | 147 | // SAFETY: The memory was allocated by `dma_alloc` above using the same allocator, and the |
| 148 | // layout is the same as was used then. |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 149 | unsafe { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 150 | dealloc(vaddr.as_ptr(), layout); |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 151 | } |
| 152 | 0 |
| 153 | } |
| 154 | |
Andrew Walbran | 4784280 | 2023-07-05 16:56:08 +0000 | [diff] [blame] | 155 | /// # Implementation Safety |
| 156 | /// |
| 157 | /// The returned pointer must be valid because the `paddr` describes a valid MMIO region, and we |
| 158 | /// previously mapped the entire PCI MMIO range. It can't alias any other allocations because |
| 159 | /// the PCI MMIO range doesn't overlap with any other memory ranges. |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 160 | unsafe fn mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8> { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 161 | NonNull::new(paddr as _).unwrap() |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 164 | unsafe fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr { |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 165 | let vaddr = buffer.cast(); |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 166 | // Nothing to do, as the host already has access to all memory. |
| 167 | virt_to_phys(vaddr) |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 168 | } |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 169 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 170 | unsafe fn unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection) { |
Andrew Walbran | 336d0cb | 2023-01-06 16:33:15 +0000 | [diff] [blame] | 171 | // Nothing to do, as the host already has access to all memory and we didn't copy the buffer |
| 172 | // anywhere else. |
| 173 | } |
| 174 | } |
| 175 | |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 176 | fn virt_to_phys(vaddr: NonNull<u8>) -> PhysAddr { |
| 177 | vaddr.as_ptr() as _ |
Andrew Walbran | 8217d06 | 2022-11-22 16:56:18 +0000 | [diff] [blame] | 178 | } |