Skip to content

Commit 3fe9c6d

Browse files
committed
uefi: Refactor Buffer and Region structs
1 parent 7bdd369 commit 3fe9c6d

4 files changed

Lines changed: 35 additions & 29 deletions

File tree

uefi/src/proto/pci/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocolWidth;
99
pub mod configuration;
1010
#[cfg(feature = "alloc")]
1111
pub mod enumeration;
12-
pub mod page;
13-
pub mod region;
1412
pub mod root_bridge;
1513

1614
/// IO Address for PCI/register IO operations
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! PCI Root Bridge protocol.
44
55
use super::{PciIoAddress, PciIoUnit, encode_io_mode_and_unit};
6+
#[cfg(doc)]
7+
use crate::Status;
68
use crate::StatusExt;
79
#[cfg(feature = "alloc")]
810
use crate::proto::pci::configuration::QwordAddressSpaceDescriptor;
@@ -14,8 +16,8 @@ use core::ptr;
1416
use uefi_macros::unsafe_protocol;
1517
use uefi_raw::protocol::pci::root_bridge::{PciRootBridgeIoAccess, PciRootBridgeIoProtocol};
1618

17-
#[cfg(doc)]
18-
use crate::Status;
19+
pub mod page;
20+
pub mod region;
1921

2022
/// Protocol that provides access to the PCI Root Bridge I/O protocol.
2123
///
Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::mem::{ManuallyDrop, MaybeUninit};
66
use core::num::NonZeroUsize;
77
use core::ops::{Deref, DerefMut};
88
use core::ptr::NonNull;
9-
use log::trace;
9+
use log::{error, trace};
1010
use uefi_raw::Status;
1111
use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocol;
1212

@@ -15,30 +15,20 @@ use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocol;
1515
/// # Lifetime
1616
/// `'p` is the lifetime for Protocol.
1717
#[derive(Debug)]
18-
pub struct PciPage<'p, T> {
19-
base: NonNull<T>,
20-
pages: NonZeroUsize,
21-
proto: &'p PciRootBridgeIoProtocol,
18+
pub struct PciBuffer<'p, T> {
19+
pub(super) base: NonNull<T>,
20+
pub(super) pages: NonZeroUsize,
21+
pub(super) proto: &'p PciRootBridgeIoProtocol,
2222
}
2323

24-
impl<'p, T> PciPage<'p, MaybeUninit<T>> {
25-
/// Creates wrapper for pages allocated by PCI Root Bridge protocol.
26-
#[must_use]
27-
pub const fn new(
28-
base: NonNull<MaybeUninit<T>>,
29-
pages: NonZeroUsize,
30-
proto: &'p PciRootBridgeIoProtocol,
31-
) -> Self {
32-
Self { base, pages, proto }
33-
}
34-
24+
impl<'p, T> PciBuffer<'p, MaybeUninit<T>> {
3525
/// Assumes the contents of this buffer have been initialized.
3626
///
3727
/// # Safety
3828
/// Callers of this function must guarantee that the value stored is valid.
3929
#[must_use]
40-
pub const unsafe fn assume_init(self) -> PciPage<'p, T> {
41-
let initialized = PciPage {
30+
pub const unsafe fn assume_init(self) -> PciBuffer<'p, T> {
31+
let initialized = PciBuffer {
4232
base: self.base.cast(),
4333
pages: self.pages,
4434
proto: self.proto,
@@ -48,33 +38,47 @@ impl<'p, T> PciPage<'p, MaybeUninit<T>> {
4838
}
4939
}
5040

51-
impl<T> AsRef<T> for PciPage<'_, T> {
41+
impl<'p, T> PciBuffer<'p, T> {
42+
/// Returns the base address of this buffer
43+
#[must_use]
44+
pub fn base(&self) -> NonZeroUsize {
45+
self.base.addr()
46+
}
47+
48+
/// Returns the number of pages this buffer uses
49+
#[must_use]
50+
pub const fn pages(&self) -> NonZeroUsize {
51+
self.pages
52+
}
53+
}
54+
55+
impl<T> AsRef<T> for PciBuffer<'_, T> {
5256
fn as_ref(&self) -> &T {
5357
unsafe { self.base.as_ref() }
5458
}
5559
}
5660

57-
impl<T> AsMut<T> for PciPage<'_, T> {
61+
impl<T> AsMut<T> for PciBuffer<'_, T> {
5862
fn as_mut(&mut self) -> &mut T {
5963
unsafe { self.base.as_mut() }
6064
}
6165
}
6266

63-
impl<T> Deref for PciPage<'_, T> {
67+
impl<T> Deref for PciBuffer<'_, T> {
6468
type Target = T;
6569

6670
fn deref(&self) -> &Self::Target {
6771
self.as_ref()
6872
}
6973
}
7074

71-
impl<T> DerefMut for PciPage<'_, T> {
75+
impl<T> DerefMut for PciBuffer<'_, T> {
7276
fn deref_mut(&mut self) -> &mut Self::Target {
7377
self.as_mut()
7478
}
7579
}
7680

77-
impl<T> Drop for PciPage<'_, T> {
81+
impl<T> Drop for PciBuffer<'_, T> {
7882
fn drop(&mut self) {
7983
let status = unsafe {
8084
(self.proto.free_buffer)(self.proto, self.pages.get(), self.base.as_ptr().cast())
@@ -88,9 +92,11 @@ impl<T> Drop for PciPage<'_, T> {
8892
);
8993
}
9094
Status::INVALID_PARAMETER => {
91-
panic!("PciBuffer was not created through valid protocol usage!")
95+
error!("PciBuffer was not created through valid protocol usage!")
96+
}
97+
etc => {
98+
error!("Failed to free PciBuffer: {:?}", etc);
9299
}
93-
_ => unreachable!(),
94100
}
95101
}
96102
}

0 commit comments

Comments
 (0)