Skip to content

Commit 4adcc5c

Browse files
committed
uefi-raw: Add binding for EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
1 parent fc70683 commit 4adcc5c

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

uefi-raw/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Added `Usb2HostControllerProtocol`.
2020
- Added `DevicePathProtocol::length()` properly constructing the `u16` value
2121
- Added `AllocateType`.
22+
- Added `PciRootBridgeIoProtocol`.
2223

2324
## Changed
2425
- `DevicePathProtocol` now derives

uefi-raw/src/protocol/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod memory_protection;
2121
pub mod misc;
2222
pub mod network;
2323
pub mod nvme;
24+
pub mod pci;
2425
pub mod rng;
2526
pub mod scsi;
2627
pub mod shell_params;

uefi-raw/src/protocol/pci/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
pub mod root_bridge;
+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
3+
use crate::table::boot::{AllocateType, MemoryType};
4+
use crate::{Handle, PhysicalAddress, Status};
5+
use core::ffi::c_void;
6+
use core::fmt;
7+
use uguid::{guid, Guid};
8+
9+
/// Corresponds to the `EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH` enum.
10+
#[repr(u32)]
11+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12+
pub enum PciRootBridgeIoProtocolWidth {
13+
Uint8 = 0,
14+
Uint16 = 1,
15+
Uint32 = 2,
16+
Uint64 = 3,
17+
FifoUint8 = 4,
18+
FifoUint16 = 5,
19+
FifoUint32 = 6,
20+
FifoUint64 = 7,
21+
FillUint8 = 8,
22+
FillUint16 = 9,
23+
FillUint32 = 10,
24+
FillUint64 = 11,
25+
Maximum = 12,
26+
}
27+
28+
/// Corresponds to the `EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION` enum.
29+
#[repr(u32)]
30+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31+
pub enum PciRootBridgeIoProtocolOperation {
32+
BusMasterRead = 0,
33+
BusMasterWrite = 1,
34+
BusMasterCommonBuffer = 2,
35+
BusMasterRead64 = 3,
36+
BusMasterWrite64 = 4,
37+
BusMasterCommonBuffer64 = 5,
38+
Maximum = 6,
39+
}
40+
41+
#[repr(transparent)]
42+
#[derive(Clone, Copy, PartialEq, Eq)]
43+
pub struct PciIoAddress(u64);
44+
45+
impl PciIoAddress {
46+
pub fn with_register(bus: u8, dev: u8, func: u8, reg: u8) -> Self {
47+
let address =
48+
((bus as u64) << 24) | ((dev as u64) << 16) | ((func as u64) << 8) | (reg as u64);
49+
Self(address)
50+
}
51+
pub fn with_extended_register(bus: u8, dev: u8, func: u8, ext_reg: u32) -> Self {
52+
let address = ((ext_reg as u64) << 32)
53+
| ((bus as u64) << 24)
54+
| ((dev as u64) << 16)
55+
| ((func as u64) << 8);
56+
Self(address)
57+
}
58+
pub fn ext_reg(&self) -> u32 {
59+
(self.0 >> 32) as u32
60+
}
61+
pub fn bus(&self) -> u8 {
62+
(self.0 >> 24) as u8
63+
}
64+
pub fn dev(&self) -> u8 {
65+
(self.0 >> 16) as u8
66+
}
67+
pub fn fun(&self) -> u8 {
68+
(self.0 >> 8) as u8
69+
}
70+
pub fn reg(&self) -> u8 {
71+
self.0 as u8
72+
}
73+
}
74+
75+
impl fmt::Debug for PciIoAddress {
76+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77+
f.debug_struct("PciIoAddress")
78+
.field("bus", &self.bus())
79+
.field("dev", &self.dev())
80+
.field("func", &self.fun())
81+
.field("reg", &self.reg())
82+
.field("ext_reg", &self.ext_reg())
83+
.finish()
84+
}
85+
}
86+
87+
#[derive(Debug)]
88+
#[repr(C)]
89+
pub struct PciRootBridgeIoAccess<TAddr> {
90+
pub read: unsafe extern "efiapi" fn(
91+
this: *const PciRootBridgeIoProtocol,
92+
width: PciRootBridgeIoProtocolWidth,
93+
address: TAddr,
94+
count: usize,
95+
buffer: *mut c_void,
96+
) -> Status,
97+
pub write: unsafe extern "efiapi" fn(
98+
this: *const Self,
99+
width: PciRootBridgeIoProtocolWidth,
100+
address: TAddr,
101+
count: usize,
102+
buffer: *const c_void,
103+
) -> Status,
104+
}
105+
106+
#[derive(Debug)]
107+
#[repr(C)]
108+
pub struct PciRootBridgeIoProtocol {
109+
pub parent_handle: Handle,
110+
pub poll_mem: unsafe extern "efiapi" fn(
111+
this: *const Self,
112+
width: PciRootBridgeIoProtocolWidth,
113+
address: u64,
114+
mask: u64,
115+
value: u64,
116+
delay: u64,
117+
result: *mut u64,
118+
) -> Status,
119+
pub poll_io: unsafe extern "efiapi" fn(
120+
this: *const Self,
121+
width: PciRootBridgeIoProtocolWidth,
122+
address: u64,
123+
mask: u64,
124+
value: u64,
125+
delay: u64,
126+
result: *mut u64,
127+
) -> Status,
128+
pub mem: PciRootBridgeIoAccess<u64>,
129+
pub io: PciRootBridgeIoAccess<u64>,
130+
pub pci: PciRootBridgeIoAccess<PciIoAddress>,
131+
pub copy_mem: unsafe extern "efiapi" fn(
132+
this: *const Self,
133+
width: PciRootBridgeIoProtocolWidth,
134+
dest_addr: u64,
135+
src_addr: u64,
136+
count: usize,
137+
) -> Status,
138+
pub map: unsafe extern "efiapi" fn(
139+
this: *const Self,
140+
operation: PciRootBridgeIoProtocolOperation,
141+
host_addr: *const c_void,
142+
num_bytes: *mut usize,
143+
device_addr: *mut PhysicalAddress,
144+
mapping: *mut *mut c_void,
145+
) -> Status,
146+
pub unmap: unsafe extern "efiapi" fn(this: *const Self, mapping: *const c_void) -> Status,
147+
pub allocate_buffer: unsafe extern "efiapi" fn(
148+
this: *const Self,
149+
alloc_ty: AllocateType,
150+
memory_ty: MemoryType,
151+
pages: usize,
152+
host_addr: *mut *const c_void,
153+
attributes: u64,
154+
) -> Status,
155+
pub free_buffer: unsafe extern "efiapi" fn(
156+
this: *const Self,
157+
pages: usize,
158+
host_addr: *const c_void,
159+
) -> Status,
160+
pub flush: unsafe extern "efiapi" fn(this: *const Self) -> Status,
161+
pub get_attributes: unsafe extern "efiapi" fn(
162+
this: *const Self,
163+
supports: *mut u64,
164+
attributes: *mut u64,
165+
) -> Status,
166+
pub set_attributes: unsafe extern "efiapi" fn(
167+
this: *const Self,
168+
attributes: u64,
169+
resource_base: *mut u64,
170+
resource_length: *mut u64,
171+
) -> Status,
172+
pub configuration:
173+
unsafe extern "efiapi" fn(this: *const Self, resources: *mut *const c_void) -> Status,
174+
pub segment_number: u32,
175+
}
176+
177+
impl PciRootBridgeIoProtocol {
178+
pub const GUID: Guid = guid!("2f707ebb-4a1a-11d4-9a38-0090273fc14d");
179+
}

0 commit comments

Comments
 (0)