aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / pci / root_bridge.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! PCI Root Bridge protocol. |
| 4 | |
| 5 | use super::{PciIoAddress, PciIoUnit, encode_io_mode_and_unit}; |
| 6 | use crate::StatusExt; |
| 7 | #[cfg(feature = "alloc")] |
| 8 | use crate::proto::pci::configuration::QwordAddressSpaceDescriptor; |
| 9 | #[cfg(feature = "alloc")] |
| 10 | use alloc::vec::Vec; |
| 11 | #[cfg(feature = "alloc")] |
| 12 | use core::ffi::c_void; |
| 13 | use core::marker::PhantomData; |
| 14 | use core::ptr; |
| 15 | use uefi_macros::unsafe_protocol; |
| 16 | use uefi_raw::protocol::pci::root_bridge::{ |
| 17 | PciRootBridgeIoAccess, PciRootBridgeIoProtocol, PciRootBridgeIoProtocolAttributes, |
| 18 | }; |
| 19 | |
| 20 | #[cfg(doc)] |
| 21 | use crate::Status; |
| 22 | |
| 23 | /// Protocol that provides access to the PCI Root Bridge I/O protocol. |
| 24 | /// |
| 25 | /// # UEFI Specification |
| 26 | /// Provides the basic Memory, I/O, PCI configuration, and DMA interfaces that are |
| 27 | /// used to abstract accesses to PCI controllers behind a PCI Root Bridge Controller. |
| 28 | #[derive(Debug)] |
| 29 | #[repr(transparent)] |
| 30 | #[unsafe_protocol(PciRootBridgeIoProtocol::GUID)] |
| 31 | pub struct PciRootBridgeIo(PciRootBridgeIoProtocol); |
| 32 | |
| 33 | impl PciRootBridgeIo { |
| 34 | /// Get the segment number where this PCI root bridge resides. |
| 35 | #[must_use] |
| 36 | pub const fn segment_nr(&self) -> u32 { |
| 37 | self.0.segment_number |
| 38 | } |
| 39 | |
| 40 | /// Access PCI controller registers in the configuration space on this root bridge. |
| 41 | pub const fn pci(&mut self) -> PciIoAccess<'_, PciConfigurationSpace> { |
| 42 | let io_access = self.0.pci; |
| 43 | PciIoAccess::new(&mut self.0, io_access, PciConfigurationSpace) |
| 44 | } |
| 45 | |
| 46 | /// Access PCI controller registers in the memory space on this root bridge. |
| 47 | pub const fn memory(&mut self) -> PciIoAccess<'_, PciMemorySpace> { |
| 48 | let io_access = self.0.mem; |
| 49 | PciIoAccess::new(&mut self.0, io_access, PciMemorySpace) |
| 50 | } |
| 51 | |
| 52 | /// Access PCI controller registers in the I/O space on this root bridge. |
| 53 | pub const fn io(&mut self) -> PciIoAccess<'_, PciIoSpace> { |
| 54 | let io_access = self.0.io; |
| 55 | PciIoAccess::new(&mut self.0, io_access, PciIoSpace) |
| 56 | } |
| 57 | |
| 58 | /// Flush all PCI posted write transactions from a PCI host bridge to system memory. |
| 59 | /// |
| 60 | /// # Errors |
| 61 | /// - [`Status::DEVICE_ERROR`] The PCI posted write transactions were not flushed from the PCI host bridge |
| 62 | /// due to a hardware error. |
| 63 | pub fn flush(&mut self) -> crate::Result<()> { |
| 64 | // SAFETY: The memory is valid. |
| 65 | unsafe { (self.0.flush)(&mut self.0).to_result() } |
| 66 | } |
| 67 | |
| 68 | /// Returns the set of [`PciRootBridgeIoProtocolAttributes`] that this PCI root bridge |
| 69 | /// supports. |
| 70 | pub fn supported_attributes(&self) -> crate::Result<PciRootBridgeIoProtocolAttributes> { |
| 71 | let mut supported = 0; |
| 72 | |
| 73 | // SAFETY: The memory is valid. |
| 74 | unsafe { |
| 75 | (self.0.get_attributes)(&self.0, &mut supported, ptr::null_mut()).to_result_with_val( |
| 76 | || PciRootBridgeIoProtocolAttributes::from_bits_retain(supported), |
| 77 | ) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Returns the [`PciRootBridgeIoProtocolAttributes`] that this PCI root bridge is currently using. |
| 82 | pub fn attributes(&self) -> crate::Result<PciRootBridgeIoProtocolAttributes> { |
| 83 | let mut current = 0; |
| 84 | |
| 85 | // SAFETY: The memory is valid. |
| 86 | unsafe { |
| 87 | (self.0.get_attributes)(&self.0, ptr::null_mut(), &mut current) |
| 88 | .to_result_with_val(|| PciRootBridgeIoProtocolAttributes::from_bits_retain(current)) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Sets [`PciRootBridgeIoProtocolAttributes`] for this PCI root bridge. |
| 93 | /// |
| 94 | /// # Safety |
| 95 | /// |
| 96 | /// The new [`PciRootBridgeIoProtocolAttributes`] must be valid for the current system |
| 97 | /// configuration. |
| 98 | pub unsafe fn set_attributes( |
| 99 | &mut self, |
| 100 | attributes: PciRootBridgeIoProtocolAttributes, |
| 101 | ) -> crate::Result { |
| 102 | // SAFETY: The memory is valid. |
| 103 | unsafe { |
| 104 | (self.0.set_attributes)( |
| 105 | &mut self.0, |
| 106 | attributes.bits(), |
| 107 | ptr::null_mut(), |
| 108 | ptr::null_mut(), |
| 109 | ) |
| 110 | .to_result() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /// Sets [`PciRootBridgeIoProtocolAttributes`] for this PCI root bridge (supporting attributes |
| 115 | /// that require a resource range). For instance, modifying the cache settings of a PCI |
| 116 | /// memory range requires the use of this function. |
| 117 | /// |
| 118 | /// The provided base and length are set to the actual base and length of the region whose |
| 119 | /// attributes were changed (due to granularity or other requirements). |
| 120 | /// |
| 121 | /// # Safety |
| 122 | /// |
| 123 | /// The new [`PciRootBridgeIoProtocolAttributes`] must be valid for the current system |
| 124 | /// configuration. |
| 125 | pub unsafe fn set_attributes_with_range( |
| 126 | &mut self, |
| 127 | attributes: PciRootBridgeIoProtocolAttributes, |
| 128 | base: &mut u64, |
| 129 | length: &mut u64, |
| 130 | ) -> crate::Result { |
| 131 | // SAFETY: The memory is valid. |
| 132 | unsafe { (self.0.set_attributes)(&mut self.0, attributes.bits(), base, length).to_result() } |
| 133 | } |
| 134 | |
| 135 | // TODO: poll I/O |
| 136 | // TODO: map & unmap & copy memory |
| 137 | // TODO: buffer management |
| 138 | |
| 139 | /// Retrieves the current resource settings of this PCI root bridge in the form of a set of ACPI resource descriptors. |
| 140 | /// |
| 141 | /// The returned list of descriptors contains information about bus, memory and io ranges that were set up |
| 142 | /// by the firmware. |
| 143 | /// |
| 144 | /// # Errors |
| 145 | /// - [`Status::UNSUPPORTED`] The current configuration of this PCI root bridge could not be retrieved. |
| 146 | #[cfg(feature = "alloc")] |
| 147 | pub fn configuration(&self) -> crate::Result<Vec<QwordAddressSpaceDescriptor>> { |
| 148 | use crate::Status; |
| 149 | use crate::proto::pci::configuration; |
| 150 | // The storage for the resource descriptors is allocated by this function. The caller must treat |
| 151 | // the return buffer as read-only data, and the buffer must not be freed by the caller. |
| 152 | let mut resources: *const c_void = ptr::null(); |
| 153 | // SAFETY: The memory is valid. |
| 154 | unsafe { (self.0.configuration)(&self.0, &mut resources) }.to_result()?; |
| 155 | // The parser walks the list until its end tag, so a null pointer |
| 156 | // would be dereferenced. |
| 157 | if resources.is_null() { |
| 158 | return Err(Status::DEVICE_ERROR.into()); |
| 159 | } |
| 160 | // SAFETY: The firmware returned a descriptor list terminated by an |
| 161 | // end tag. |
| 162 | Ok(unsafe { configuration::parse(resources) }) |
| 163 | } |
| 164 | |
| 165 | // ################################################### |
| 166 | // # Convenience functionality |
| 167 | |
| 168 | /// Recursively enumerate all devices, device functions and pci(e)-to-pci(e) bridges, starting from this pci root. |
| 169 | /// |
| 170 | /// The returned addresses might overlap with the addresses returned by another [`PciRootBridgeIo`] instance. |
| 171 | /// Make sure to perform some form of cross-[`PciRootBridgeIo`] deduplication on the discovered addresses. |
| 172 | /// **WARNING:** Only use the returned addresses with the respective [`PciRootBridgeIo`] instance that returned them. |
| 173 | /// |
| 174 | /// # Returns |
| 175 | /// An ordered list of addresses containing all present devices below this RootBridge. |
| 176 | /// |
| 177 | /// # Errors |
| 178 | /// This can basically fail with all the IO errors found in [`PciIoAccess`] methods. |
| 179 | #[cfg(feature = "alloc")] |
| 180 | pub fn enumerate(&mut self) -> crate::Result<super::enumeration::PciTree> { |
| 181 | use super::enumeration::{self, PciTree}; |
| 182 | use crate::proto::pci::configuration::ResourceRangeType; |
| 183 | |
| 184 | let mut tree = PciTree::new(self.segment_nr()); |
| 185 | for descriptor in self.configuration()? { |
| 186 | // In the descriptors we can query for the current root bridge, Bus entries contain ranges of valid |
| 187 | // bus addresses. These are all bus addresses found below ourselves. These are not only the busses |
| 188 | // linked to **directly** from ourselves, but also recursively. Thus we use PciTree::push_bus() to |
| 189 | // determine whether we have already visited a given bus number. |
| 190 | if descriptor.resource_range_type == ResourceRangeType::Bus { |
| 191 | for bus in (descriptor.address_min as u8)..=(descriptor.address_max as u8) { |
| 192 | if tree.should_visit_bus(bus) { |
| 193 | let addr = PciIoAddress::new(bus, 0, 0); |
| 194 | enumeration::visit_bus(self, addr, &mut tree)?; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | Ok(tree) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// Struct for performing PCI I/O operations on a root bridge. |
| 205 | #[derive(Debug)] |
| 206 | pub struct PciIoAccess<'a, S: PciIoAddressSpace> { |
| 207 | proto: *mut PciRootBridgeIoProtocol, |
| 208 | // The firmware receives `proto` on every call and may access any part of |
| 209 | // the protocol instance through it. Hence, this must not hold a reference |
| 210 | // into the instance across the call. Instead, it keeps a copy of the |
| 211 | // function pointers it needs. |
| 212 | io_access: PciRootBridgeIoAccess, |
| 213 | _lifetime: PhantomData<&'a mut PciRootBridgeIoProtocol>, |
| 214 | _address_space: S, |
| 215 | } |
| 216 | |
| 217 | impl<'a, S: PciIoAddressSpace> PciIoAccess<'a, S> { |
| 218 | /// Takes `proto` as a reference rather than as a raw pointer to tie `'a` |
| 219 | /// to the exclusive borrow of the protocol instance. |
| 220 | const fn new( |
| 221 | proto: &'a mut PciRootBridgeIoProtocol, |
| 222 | io_access: PciRootBridgeIoAccess, |
| 223 | address_space: S, |
| 224 | ) -> Self { |
| 225 | Self { |
| 226 | proto: ptr::from_mut(proto), |
| 227 | io_access, |
| 228 | _lifetime: PhantomData, |
| 229 | _address_space: address_space, |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /// Reads a single value of type `U` from the specified PCI address. |
| 234 | /// |
| 235 | /// # Arguments |
| 236 | /// - `addr` - The PCI address to read from. |
| 237 | /// |
| 238 | /// # Returns |
| 239 | /// - The read value of type `U`. |
| 240 | /// |
| 241 | /// # Errors |
| 242 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 243 | /// - [`Status::OUT_OF_RESOURCES`] The read request could not be completed due to a lack of resources. |
| 244 | pub fn read_one<U: PciIoUnit>(&self, addr: S::Address) -> crate::Result<U> { |
| 245 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 246 | let mut result = U::default(); |
| 247 | // SAFETY: The memory is valid. |
| 248 | unsafe { |
| 249 | (self.io_access.read)( |
| 250 | self.proto, |
| 251 | width_mode, |
| 252 | addr.into(), |
| 253 | 1, |
| 254 | ptr::from_mut(&mut result).cast(), |
| 255 | ) |
| 256 | .to_result_with_val(|| result) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /// Writes a single value of type `U` to the specified PCI address. |
| 261 | /// |
| 262 | /// # Arguments |
| 263 | /// - `addr` - The PCI address to write to. |
| 264 | /// - `data` - The value to write. |
| 265 | /// |
| 266 | /// # Errors |
| 267 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 268 | /// - [`Status::OUT_OF_RESOURCES`] The write request could not be completed due to a lack of resources. |
| 269 | pub fn write_one<U: PciIoUnit>(&self, addr: S::Address, data: U) -> crate::Result<()> { |
| 270 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 271 | // SAFETY: The memory is valid. |
| 272 | unsafe { |
| 273 | (self.io_access.write)( |
| 274 | self.proto, |
| 275 | width_mode, |
| 276 | addr.into(), |
| 277 | 1, |
| 278 | ptr::from_ref(&data).cast(), |
| 279 | ) |
| 280 | .to_result() |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /// Reads multiple values from the specified PCI address range. |
| 285 | /// |
| 286 | /// # Arguments |
| 287 | /// - `addr` - The starting PCI address to read from. |
| 288 | /// - `data` - A mutable slice to store the read values. |
| 289 | /// |
| 290 | /// # Errors |
| 291 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 292 | /// - [`Status::OUT_OF_RESOURCES`] The read operation could not be completed due to a lack of resources. |
| 293 | pub fn read<U: PciIoUnit>(&self, addr: S::Address, data: &mut [U]) -> crate::Result<()> { |
| 294 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 295 | // SAFETY: The memory is valid. |
| 296 | unsafe { |
| 297 | (self.io_access.read)( |
| 298 | self.proto, |
| 299 | width_mode, |
| 300 | addr.into(), |
| 301 | data.len(), |
| 302 | data.as_mut_ptr().cast(), |
| 303 | ) |
| 304 | .to_result() |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /// Writes multiple values to the specified PCI address range. |
| 309 | /// |
| 310 | /// # Arguments |
| 311 | /// - `addr` - The starting PCI address to write to. |
| 312 | /// - `data` - A slice containing the values to write. |
| 313 | /// |
| 314 | /// # Errors |
| 315 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 316 | /// - [`Status::OUT_OF_RESOURCES`] The write operation could not be completed due to a lack of resources. |
| 317 | pub fn write<U: PciIoUnit>(&self, addr: S::Address, data: &[U]) -> crate::Result<()> { |
| 318 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 319 | // SAFETY: The memory is valid. |
| 320 | unsafe { |
| 321 | (self.io_access.write)( |
| 322 | self.proto, |
| 323 | width_mode, |
| 324 | addr.into(), |
| 325 | data.len(), |
| 326 | data.as_ptr().cast(), |
| 327 | ) |
| 328 | .to_result() |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /// Fills a PCI address range with the specified value. |
| 333 | /// |
| 334 | /// # Arguments |
| 335 | /// - `addr` - The starting PCI address to fill. |
| 336 | /// - `count` - The number of units to write. |
| 337 | /// - `data` - The value to fill the address range with. |
| 338 | /// |
| 339 | /// # Errors |
| 340 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 341 | /// - [`Status::OUT_OF_RESOURCES`] The operation could not be completed due to a lack of resources. |
| 342 | pub fn fill_write<U: PciIoUnit>( |
| 343 | &self, |
| 344 | addr: S::Address, |
| 345 | count: usize, |
| 346 | data: U, |
| 347 | ) -> crate::Result<()> { |
| 348 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Fill); |
| 349 | // SAFETY: The memory is valid. |
| 350 | unsafe { |
| 351 | (self.io_access.write)( |
| 352 | self.proto, |
| 353 | width_mode, |
| 354 | addr.into(), |
| 355 | count, |
| 356 | ptr::from_ref(&data).cast(), |
| 357 | ) |
| 358 | .to_result() |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /// Reads a sequence of values of type `U` from the specified PCI address by repeatedly accessing it. |
| 363 | /// |
| 364 | /// # Arguments |
| 365 | /// - `addr` - The PCI address to read from. |
| 366 | /// - `data` - A mutable slice to store the read values. |
| 367 | /// |
| 368 | /// # Behavior |
| 369 | /// This reads from the same memory region (starting at `addr` and ending at `addr + size_of::<U>()`) repeatedly. |
| 370 | /// The resulting `data` buffer will contain the elements returned by reading the same address multiple times sequentially. |
| 371 | /// |
| 372 | /// # Errors |
| 373 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 374 | /// - [`Status::OUT_OF_RESOURCES`] The read operation could not be completed due to a lack of resources. |
| 375 | pub fn fifo_read<U: PciIoUnit>(&self, addr: S::Address, data: &mut [U]) -> crate::Result<()> { |
| 376 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Fifo); |
| 377 | // SAFETY: The memory is valid. |
| 378 | unsafe { |
| 379 | (self.io_access.read)( |
| 380 | self.proto, |
| 381 | width_mode, |
| 382 | addr.into(), |
| 383 | data.len(), |
| 384 | data.as_mut_ptr().cast(), |
| 385 | ) |
| 386 | .to_result() |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /// Writes a sequence of values of type `U` to the specified PCI address repeatedly. |
| 391 | /// |
| 392 | /// # Arguments |
| 393 | /// - `addr` - The PCI address to write to. |
| 394 | /// - `data` - A slice containing the values to write. |
| 395 | /// |
| 396 | /// # Behavior |
| 397 | /// This sequentially writes all elements within the given `data` buffer to the same memory region |
| 398 | /// (starting at `addr` and ending at `addr + size_of::<U>()`) sequentially. |
| 399 | /// |
| 400 | /// # Errors |
| 401 | /// - [`Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 402 | /// - [`Status::OUT_OF_RESOURCES`] The write operation could not be completed due to a lack of resources. |
| 403 | pub fn fifo_write<U: PciIoUnit>(&self, addr: S::Address, data: &[U]) -> crate::Result<()> { |
| 404 | let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Fifo); |
| 405 | // SAFETY: The memory is valid. |
| 406 | unsafe { |
| 407 | (self.io_access.write)( |
| 408 | self.proto, |
| 409 | width_mode, |
| 410 | addr.into(), |
| 411 | data.len(), |
| 412 | data.as_ptr().cast(), |
| 413 | ) |
| 414 | .to_result() |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /// Marker struct for the PCI memory space. |
| 420 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 421 | pub struct PciMemorySpace; |
| 422 | |
| 423 | impl private::Sealed for PciMemorySpace {} |
| 424 | impl PciIoAddressSpace for PciMemorySpace { |
| 425 | type Address = u64; |
| 426 | } |
| 427 | |
| 428 | /// Marker struct for the PCI I/O space. |
| 429 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 430 | pub struct PciIoSpace; |
| 431 | |
| 432 | impl private::Sealed for PciIoSpace {} |
| 433 | impl PciIoAddressSpace for PciIoSpace { |
| 434 | type Address = u32; |
| 435 | } |
| 436 | |
| 437 | /// Marker struct for the PCI configuration space. |
| 438 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 439 | pub struct PciConfigurationSpace; |
| 440 | |
| 441 | impl private::Sealed for PciConfigurationSpace {} |
| 442 | impl PciIoAddressSpace for PciConfigurationSpace { |
| 443 | type Address = PciIoAddress; |
| 444 | } |
| 445 | |
| 446 | /// Trait representing how to convert from the address type expected for the address space and the |
| 447 | /// raw address space. |
| 448 | pub trait PciIoAddressSpace: private::Sealed { |
| 449 | /// Specifies the type of the address space addresses. |
| 450 | type Address: Into<u64>; |
| 451 | } |
| 452 | |
| 453 | mod private { |
| 454 | pub trait Sealed {} |
| 455 | } |
| 456 | |
| 457 | #[cfg(test)] |
| 458 | mod tests { |
| 459 | use super::*; |
| 460 | use crate::Status; |
| 461 | use core::ffi::c_void; |
| 462 | use uefi_raw::PhysicalAddress; |
| 463 | use uefi_raw::protocol::pci::root_bridge::{ |
| 464 | PciRootBridgeIoProtocolOperation, PciRootBridgeIoProtocolWidth, |
| 465 | }; |
| 466 | use uefi_raw::table::boot::{AllocateType, MemoryType}; |
| 467 | |
| 468 | /// Segment number of the mocked root bridge. |
| 469 | const SEGMENT: u32 = 0x1234_5678; |
| 470 | |
| 471 | /// Mock of `PciRootBridgeIoAccess::read`. |
| 472 | /// |
| 473 | /// Firmware owns the protocol instance and is free to access any of its |
| 474 | /// fields through `this`. The mock does the same. |
| 475 | /// |
| 476 | /// # Safety |
| 477 | /// `this` must point to a valid protocol instance and `buffer` must be |
| 478 | /// valid for writing one `u32`. |
| 479 | unsafe extern "efiapi" fn mock_read( |
| 480 | this: *mut PciRootBridgeIoProtocol, |
| 481 | width: PciRootBridgeIoProtocolWidth, |
| 482 | _address: u64, |
| 483 | count: usize, |
| 484 | buffer: *mut c_void, |
| 485 | ) -> Status { |
| 486 | if width != PciRootBridgeIoProtocolWidth::UINT32 || count != 1 { |
| 487 | return Status::INVALID_PARAMETER; |
| 488 | } |
| 489 | // SAFETY: Guaranteed by the caller. |
| 490 | let this = unsafe { &*this }; |
| 491 | // SAFETY: Guaranteed by the caller. |
| 492 | unsafe { buffer.cast::<u32>().write(this.segment_number) }; |
| 493 | Status::SUCCESS |
| 494 | } |
| 495 | |
| 496 | /// Mock of `PciRootBridgeIoAccess::write`. See [`mock_read`]. |
| 497 | /// |
| 498 | /// # Safety |
| 499 | /// `this` must point to a valid protocol instance and `buffer` must be |
| 500 | /// valid for reading one `u32`. |
| 501 | unsafe extern "efiapi" fn mock_write( |
| 502 | this: *mut PciRootBridgeIoProtocol, |
| 503 | width: PciRootBridgeIoProtocolWidth, |
| 504 | _address: u64, |
| 505 | count: usize, |
| 506 | buffer: *const c_void, |
| 507 | ) -> Status { |
| 508 | if width != PciRootBridgeIoProtocolWidth::UINT32 || count != 1 { |
| 509 | return Status::INVALID_PARAMETER; |
| 510 | } |
| 511 | // SAFETY: Guaranteed by the caller. |
| 512 | let this = unsafe { &*this }; |
| 513 | // SAFETY: Guaranteed by the caller. |
| 514 | let value = unsafe { buffer.cast::<u32>().read() }; |
| 515 | if value == this.segment_number { |
| 516 | Status::SUCCESS |
| 517 | } else { |
| 518 | Status::DEVICE_ERROR |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | const MOCK_ACCESS: PciRootBridgeIoAccess = PciRootBridgeIoAccess { |
| 523 | read: mock_read, |
| 524 | write: mock_write, |
| 525 | }; |
| 526 | |
| 527 | // Stubs for the operations the test does not exercise. |
| 528 | |
| 529 | extern "efiapi" fn stub_poll( |
| 530 | _: *mut PciRootBridgeIoProtocol, |
| 531 | _: PciRootBridgeIoProtocolWidth, |
| 532 | _: u64, |
| 533 | _: u64, |
| 534 | _: u64, |
| 535 | _: u64, |
| 536 | _: *mut u64, |
| 537 | ) -> Status { |
| 538 | unimplemented!() |
| 539 | } |
| 540 | |
| 541 | extern "efiapi" fn stub_copy_mem( |
| 542 | _: *mut PciRootBridgeIoProtocol, |
| 543 | _: PciRootBridgeIoProtocolWidth, |
| 544 | _: u64, |
| 545 | _: u64, |
| 546 | _: usize, |
| 547 | ) -> Status { |
| 548 | unimplemented!() |
| 549 | } |
| 550 | |
| 551 | extern "efiapi" fn stub_map( |
| 552 | _: *const PciRootBridgeIoProtocol, |
| 553 | _: PciRootBridgeIoProtocolOperation, |
| 554 | _: *const c_void, |
| 555 | _: *mut usize, |
| 556 | _: *mut PhysicalAddress, |
| 557 | _: *mut *mut c_void, |
| 558 | ) -> Status { |
| 559 | unimplemented!() |
| 560 | } |
| 561 | |
| 562 | extern "efiapi" fn stub_unmap(_: *const PciRootBridgeIoProtocol, _: *const c_void) -> Status { |
| 563 | unimplemented!() |
| 564 | } |
| 565 | |
| 566 | extern "efiapi" fn stub_allocate_buffer( |
| 567 | _: *const PciRootBridgeIoProtocol, |
| 568 | _: AllocateType, |
| 569 | _: MemoryType, |
| 570 | _: usize, |
| 571 | _: *mut *mut c_void, |
| 572 | _: u64, |
| 573 | ) -> Status { |
| 574 | unimplemented!() |
| 575 | } |
| 576 | |
| 577 | extern "efiapi" fn stub_free_buffer( |
| 578 | _: *const PciRootBridgeIoProtocol, |
| 579 | _: usize, |
| 580 | _: *const c_void, |
| 581 | ) -> Status { |
| 582 | unimplemented!() |
| 583 | } |
| 584 | |
| 585 | extern "efiapi" fn stub_flush(_: *mut PciRootBridgeIoProtocol) -> Status { |
| 586 | unimplemented!() |
| 587 | } |
| 588 | |
| 589 | extern "efiapi" fn stub_get_attributes( |
| 590 | _: *const PciRootBridgeIoProtocol, |
| 591 | _: *mut u64, |
| 592 | _: *mut u64, |
| 593 | ) -> Status { |
| 594 | unimplemented!() |
| 595 | } |
| 596 | |
| 597 | extern "efiapi" fn stub_set_attributes( |
| 598 | _: *mut PciRootBridgeIoProtocol, |
| 599 | _: u64, |
| 600 | _: *mut u64, |
| 601 | _: *mut u64, |
| 602 | ) -> Status { |
| 603 | unimplemented!() |
| 604 | } |
| 605 | |
| 606 | extern "efiapi" fn stub_configuration( |
| 607 | _: *const PciRootBridgeIoProtocol, |
| 608 | _: *mut *const c_void, |
| 609 | ) -> Status { |
| 610 | unimplemented!() |
| 611 | } |
| 612 | |
| 613 | const fn mock_protocol() -> PciRootBridgeIoProtocol { |
| 614 | PciRootBridgeIoProtocol { |
| 615 | parent_handle: ptr::null_mut(), |
| 616 | poll_mem: stub_poll, |
| 617 | poll_io: stub_poll, |
| 618 | mem: MOCK_ACCESS, |
| 619 | io: MOCK_ACCESS, |
| 620 | pci: MOCK_ACCESS, |
| 621 | copy_mem: stub_copy_mem, |
| 622 | map: stub_map, |
| 623 | unmap: stub_unmap, |
| 624 | allocate_buffer: stub_allocate_buffer, |
| 625 | free_buffer: stub_free_buffer, |
| 626 | flush: stub_flush, |
| 627 | get_attributes: stub_get_attributes, |
| 628 | set_attributes: stub_set_attributes, |
| 629 | configuration: stub_configuration, |
| 630 | segment_number: SEGMENT, |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /// Each accessor hands a pointer to the whole protocol instance to the |
| 635 | /// firmware callee, which may access any part of it. This test is mainly |
| 636 | /// useful under Miri, which checks that this does not violate the |
| 637 | /// aliasing rules. |
| 638 | #[test] |
| 639 | fn test_pci_io_access_aliasing() { |
| 640 | let mut root = PciRootBridgeIo(mock_protocol()); |
| 641 | let addr = PciIoAddress::new(0, 0, 0); |
| 642 | |
| 643 | assert_eq!(root.pci().read_one::<u32>(addr).unwrap(), SEGMENT); |
| 644 | root.pci().write_one(addr, SEGMENT).unwrap(); |
| 645 | |
| 646 | assert_eq!(root.memory().read_one::<u32>(0).unwrap(), SEGMENT); |
| 647 | root.memory().write_one(0, SEGMENT).unwrap(); |
| 648 | |
| 649 | assert_eq!(root.io().read_one::<u32>(0).unwrap(), SEGMENT); |
| 650 | root.io().write_one(0, SEGMENT).unwrap(); |
| 651 | } |
| 652 | } |