|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | + |
| 3 | +//! PCI Root Bridge protocol. |
| 4 | +
|
| 5 | +use core::ptr; |
| 6 | + |
| 7 | +use super::{PciIoAddress, PciIoUnit, encode_io_mode_and_unit}; |
| 8 | +use crate::StatusExt; |
| 9 | +use uefi_macros::unsafe_protocol; |
| 10 | +use uefi_raw::protocol::pci::root_bridge::{PciRootBridgeIoAccess, PciRootBridgeIoProtocol}; |
| 11 | + |
| 12 | +/// Protocol that provides access to the PCI Root Bridge I/O protocol. |
| 13 | +/// |
| 14 | +/// # UEFI Spec Description |
| 15 | +/// Provides the basic Memory, I/O, PCI configuration, and DMA interfaces that are |
| 16 | +/// used to abstract accesses to PCI controllers behind a PCI Root Bridge Controller. |
| 17 | +#[derive(Debug)] |
| 18 | +#[repr(transparent)] |
| 19 | +#[unsafe_protocol(PciRootBridgeIoProtocol::GUID)] |
| 20 | +pub struct PciRootBridgeIo(PciRootBridgeIoProtocol); |
| 21 | + |
| 22 | +impl PciRootBridgeIo { |
| 23 | + /// Get the segment number where this PCI root bridge resides. |
| 24 | + #[must_use] |
| 25 | + pub const fn segment_nr(&self) -> u32 { |
| 26 | + self.0.segment_number |
| 27 | + } |
| 28 | + |
| 29 | + /// Access PCI I/O operations on this root bridge. |
| 30 | + pub const fn pci(&mut self) -> PciIoAccessPci<'_> { |
| 31 | + PciIoAccessPci { |
| 32 | + proto: &mut self.0, |
| 33 | + io_access: &mut self.0.pci, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Flush all PCI posted write transactions from a PCI host bridge to system memory. |
| 38 | + /// |
| 39 | + /// # Errors |
| 40 | + /// - [`crate::Status::DEVICE_ERROR`] The PCI posted write transactions were not flushed from the PCI host bridge |
| 41 | + /// due to a hardware error. |
| 42 | + pub fn flush(&mut self) -> crate::Result<()> { |
| 43 | + unsafe { (self.0.flush)(&mut self.0).to_result() } |
| 44 | + } |
| 45 | + |
| 46 | + // TODO: poll I/O |
| 47 | + // TODO: mem I/O access |
| 48 | + // TODO: io I/O access |
| 49 | + // TODO: map & unmap & copy memory |
| 50 | + // TODO: buffer management |
| 51 | + // TODO: get/set attributes |
| 52 | + // TODO: configuration / resource settings |
| 53 | +} |
| 54 | + |
| 55 | +/// Struct for performing PCI I/O operations on a root bridge. |
| 56 | +#[derive(Debug)] |
| 57 | +pub struct PciIoAccessPci<'a> { |
| 58 | + proto: *mut PciRootBridgeIoProtocol, |
| 59 | + io_access: &'a mut PciRootBridgeIoAccess, |
| 60 | +} |
| 61 | + |
| 62 | +impl PciIoAccessPci<'_> { |
| 63 | + /// Reads a single value of type `U` from the specified PCI address. |
| 64 | + /// |
| 65 | + /// # Arguments |
| 66 | + /// - `addr` - The PCI address to read from. |
| 67 | + /// |
| 68 | + /// # Returns |
| 69 | + /// - The read value of type `U`. |
| 70 | + /// |
| 71 | + /// # Errors |
| 72 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 73 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The read request could not be completed due to a lack of resources. |
| 74 | + pub fn read_one<U: PciIoUnit>(&self, addr: PciIoAddress) -> crate::Result<U> { |
| 75 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 76 | + let mut result = U::default(); |
| 77 | + unsafe { |
| 78 | + (self.io_access.read)( |
| 79 | + self.proto, |
| 80 | + width_mode, |
| 81 | + addr.into(), |
| 82 | + 1, |
| 83 | + ptr::from_mut(&mut result).cast(), |
| 84 | + ) |
| 85 | + .to_result_with_val(|| result) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Writes a single value of type `U` to the specified PCI address. |
| 90 | + /// |
| 91 | + /// # Arguments |
| 92 | + /// - `addr` - The PCI address to write to. |
| 93 | + /// - `data` - The value to write. |
| 94 | + /// |
| 95 | + /// # Errors |
| 96 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 97 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The write request could not be completed due to a lack of resources. |
| 98 | + pub fn write_one<U: PciIoUnit>(&self, addr: PciIoAddress, data: U) -> crate::Result<()> { |
| 99 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 100 | + unsafe { |
| 101 | + (self.io_access.write)( |
| 102 | + self.proto, |
| 103 | + width_mode, |
| 104 | + addr.into(), |
| 105 | + 1, |
| 106 | + ptr::from_ref(&data).cast(), |
| 107 | + ) |
| 108 | + .to_result() |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// Reads multiple values from the specified PCI address range. |
| 113 | + /// |
| 114 | + /// # Arguments |
| 115 | + /// - `addr` - The starting PCI address to read from. |
| 116 | + /// - `data` - A mutable slice to store the read values. |
| 117 | + /// |
| 118 | + /// # Errors |
| 119 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 120 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The read operation could not be completed due to a lack of resources. |
| 121 | + pub fn read<U: PciIoUnit>(&self, addr: PciIoAddress, data: &mut [U]) -> crate::Result<()> { |
| 122 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 123 | + unsafe { |
| 124 | + (self.io_access.read)( |
| 125 | + self.proto, |
| 126 | + width_mode, |
| 127 | + addr.into(), |
| 128 | + data.len(), |
| 129 | + data.as_mut_ptr().cast(), |
| 130 | + ) |
| 131 | + .to_result() |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// Writes multiple values to the specified PCI address range. |
| 136 | + /// |
| 137 | + /// # Arguments |
| 138 | + /// - `addr` - The starting PCI address to write to. |
| 139 | + /// - `data` - A slice containing the values to write. |
| 140 | + /// |
| 141 | + /// # Errors |
| 142 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 143 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The write operation could not be completed due to a lack of resources. |
| 144 | + pub fn write<U: PciIoUnit>(&self, addr: PciIoAddress, data: &[U]) -> crate::Result<()> { |
| 145 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Normal); |
| 146 | + unsafe { |
| 147 | + (self.io_access.write)( |
| 148 | + self.proto, |
| 149 | + width_mode, |
| 150 | + addr.into(), |
| 151 | + data.len(), |
| 152 | + data.as_ptr().cast(), |
| 153 | + ) |
| 154 | + .to_result() |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /// Fills a PCI address range with the specified value. |
| 159 | + /// |
| 160 | + /// # Arguments |
| 161 | + /// - `addr` - The starting PCI address to fill. |
| 162 | + /// - `count` - The number of units to write. |
| 163 | + /// - `data` - The value to fill the address range with. |
| 164 | + /// |
| 165 | + /// # Errors |
| 166 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 167 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The operation could not be completed due to a lack of resources. |
| 168 | + pub fn fill_write<U: PciIoUnit>( |
| 169 | + &self, |
| 170 | + addr: PciIoAddress, |
| 171 | + count: usize, |
| 172 | + data: U, |
| 173 | + ) -> crate::Result<()> { |
| 174 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Fill); |
| 175 | + unsafe { |
| 176 | + (self.io_access.write)( |
| 177 | + self.proto, |
| 178 | + width_mode, |
| 179 | + addr.into(), |
| 180 | + count, |
| 181 | + ptr::from_ref(&data).cast(), |
| 182 | + ) |
| 183 | + .to_result() |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /// Reads a sequence of values of type `U` from the specified PCI address by repeatedly accessing it. |
| 188 | + /// |
| 189 | + /// # Arguments |
| 190 | + /// - `addr` - The PCI address to read from. |
| 191 | + /// - `data` - A mutable slice to store the read values. |
| 192 | + /// |
| 193 | + /// # Behavior |
| 194 | + /// This reads from the same memory region (starting at `addr` and ending at `addr + size_of::<U>()`) repeatedly. |
| 195 | + /// The resulting `data` buffer will contain the elements returned by reading the same address multiple times sequentially. |
| 196 | + /// |
| 197 | + /// # Errors |
| 198 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 199 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The read operation could not be completed due to a lack of resources. |
| 200 | + pub fn fifo_read<U: PciIoUnit>(&self, addr: PciIoAddress, data: &mut [U]) -> crate::Result<()> { |
| 201 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Fifo); |
| 202 | + unsafe { |
| 203 | + (self.io_access.read)( |
| 204 | + self.proto, |
| 205 | + width_mode, |
| 206 | + addr.into(), |
| 207 | + data.len(), |
| 208 | + data.as_mut_ptr().cast(), |
| 209 | + ) |
| 210 | + .to_result() |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + /// Writes a sequence of values of type `U` to the specified PCI address repeatedly. |
| 215 | + /// |
| 216 | + /// # Arguments |
| 217 | + /// - `addr` - The PCI address to write to. |
| 218 | + /// - `data` - A slice containing the values to write. |
| 219 | + /// |
| 220 | + /// # Behavior |
| 221 | + /// This sequentially writes all elements within the given `data` buffer to the same memory region |
| 222 | + /// (starting at `addr` and ending at `addr + size_of::<U>()`) sequentially. |
| 223 | + /// |
| 224 | + /// # Errors |
| 225 | + /// - [`crate::Status::INVALID_PARAMETER`] The requested width is invalid for this PCI root bridge. |
| 226 | + /// - [`crate::Status::OUT_OF_RESOURCES`] The write operation could not be completed due to a lack of resources. |
| 227 | + pub fn fifo_write<U: PciIoUnit>(&self, addr: PciIoAddress, data: &[U]) -> crate::Result<()> { |
| 228 | + let width_mode = encode_io_mode_and_unit::<U>(super::PciIoMode::Fifo); |
| 229 | + unsafe { |
| 230 | + (self.io_access.write)( |
| 231 | + self.proto, |
| 232 | + width_mode, |
| 233 | + addr.into(), |
| 234 | + data.len(), |
| 235 | + data.as_ptr().cast(), |
| 236 | + ) |
| 237 | + .to_result() |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments