Skip to content

Commit 7357644

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

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-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;
+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
impl fmt::Debug for PciIoAddress {
75+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76+
f.debug_struct("PciIoAddress")
77+
.field("bus", &self.bus())
78+
.field("dev", &self.dev())
79+
.field("func", &self.fun())
80+
.field("reg", &self.reg())
81+
.field("ext_reg", &self.ext_reg())
82+
.finish()
83+
}
84+
}
85+
86+
#[derive(Debug)]
87+
#[repr(C)]
88+
pub struct PciRootBridgeIoAccess<TAddr> {
89+
pub read: unsafe extern "efiapi" fn(
90+
this: *const PciRootBridgeIoProtocol,
91+
width: PciRootBridgeIoProtocolWidth,
92+
address: TAddr,
93+
count: usize,
94+
buffer: *mut c_void,
95+
) -> Status,
96+
pub write: unsafe extern "efiapi" fn(
97+
this: *const Self,
98+
width: PciRootBridgeIoProtocolWidth,
99+
address: TAddr,
100+
count: usize,
101+
buffer: *const c_void,
102+
) -> Status,
103+
}
104+
105+
#[derive(Debug)]
106+
#[repr(C)]
107+
pub struct PciRootBridgeIoProtocol {
108+
pub parent_handle: Handle,
109+
pub poll_mem: unsafe extern "efiapi" fn(
110+
this: *const Self,
111+
width: PciRootBridgeIoProtocolWidth,
112+
address: u64,
113+
mask: u64,
114+
value: u64,
115+
delay: u64,
116+
result: *mut u64,
117+
) -> Status,
118+
pub poll_io: unsafe extern "efiapi" fn(
119+
this: *const Self,
120+
width: PciRootBridgeIoProtocolWidth,
121+
address: u64,
122+
mask: u64,
123+
value: u64,
124+
delay: u64,
125+
result: *mut u64,
126+
) -> Status,
127+
pub mem: PciRootBridgeIoAccess<u64>,
128+
pub io: PciRootBridgeIoAccess<u64>,
129+
pub pci: PciRootBridgeIoAccess<PciIoAddress>,
130+
pub copy_mem: unsafe extern "efiapi" fn(
131+
this: *const Self,
132+
width: PciRootBridgeIoProtocolWidth,
133+
dest_addr: u64,
134+
src_addr: u64,
135+
count: usize,
136+
) -> Status,
137+
pub map: unsafe extern "efiapi" fn(
138+
this: *const Self,
139+
operation: PciRootBridgeIoProtocolOperation,
140+
host_addr: *const c_void,
141+
num_bytes: *mut usize,
142+
device_addr: *mut PhysicalAddress,
143+
mapping: *mut *mut c_void,
144+
) -> Status,
145+
pub unmap: unsafe extern "efiapi" fn(this: *const Self, mapping: *const c_void) -> Status,
146+
pub allocate_buffer: unsafe extern "efiapi" fn(
147+
this: *const Self,
148+
alloc_ty: AllocateType,
149+
memory_ty: MemoryType,
150+
pages: usize,
151+
host_addr: *mut *const c_void,
152+
attributes: u64,
153+
) -> Status,
154+
pub free_buffer: unsafe extern "efiapi" fn(
155+
this: *const Self,
156+
pages: usize,
157+
host_addr: *const c_void,
158+
) -> Status,
159+
pub flush: unsafe extern "efiapi" fn(this: *const Self) -> Status,
160+
pub get_attributes: unsafe extern "efiapi" fn(
161+
this: *const Self,
162+
supports: *mut u64,
163+
attributes: *mut u64,
164+
) -> Status,
165+
pub set_attributes: unsafe extern "efiapi" fn(
166+
this: *const Self,
167+
attributes: u64,
168+
resource_base: *mut u64,
169+
resource_length: *mut u64,
170+
) -> Status,
171+
pub configuration:
172+
unsafe extern "efiapi" fn(this: *const Self, resources: *mut *const c_void) -> Status,
173+
pub segment_number: u32,
174+
}
175+
176+
impl PciRootBridgeIoProtocol {
177+
pub const GUID: Guid = guid!("2f707ebb-4a1a-11d4-9a38-0090273fc14d");
178+
}

0 commit comments

Comments
 (0)