-
-
Notifications
You must be signed in to change notification settings - Fork 177
Implement base for PCI Root bridge io protocols #1724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tmvkrpxl0
wants to merge
3
commits into
rust-osdev:main
Choose a base branch
from
tmvkrpxl0:pci_protocol_base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
//! Defines wrapper for pages allocated by PCI Root Bridge protocol. | ||
use core::fmt::Debug; | ||
use core::mem::{ManuallyDrop, MaybeUninit}; | ||
use core::num::NonZeroUsize; | ||
use core::ops::{Deref, DerefMut}; | ||
use core::ptr::NonNull; | ||
use log::trace; | ||
use uefi_raw::Status; | ||
use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocol; | ||
|
||
/// Smart pointer for wrapping owned pages allocated by PCI Root Bridge protocol. | ||
/// | ||
/// # Lifetime | ||
/// `'p` is the lifetime for Protocol. | ||
#[derive(Debug)] | ||
pub struct PciPage<'p, T> { | ||
base: NonNull<T>, | ||
pages: NonZeroUsize, | ||
proto: &'p PciRootBridgeIoProtocol, | ||
} | ||
|
||
impl<'p, T> PciPage<'p, MaybeUninit<T>> { | ||
/// Creates wrapper for pages allocated by PCI Root Bridge protocol. | ||
#[must_use] | ||
pub const fn new( | ||
base: NonNull<MaybeUninit<T>>, | ||
pages: NonZeroUsize, | ||
proto: &'p PciRootBridgeIoProtocol, | ||
) -> Self { | ||
Self { base, pages, proto } | ||
} | ||
|
||
/// Assumes the contents of this buffer have been initialized. | ||
/// | ||
/// # Safety | ||
/// Callers of this function must guarantee that the value stored is valid. | ||
#[must_use] | ||
pub const unsafe fn assume_init(self) -> PciPage<'p, T> { | ||
let initialized = PciPage { | ||
base: self.base.cast(), | ||
pages: self.pages, | ||
proto: self.proto, | ||
}; | ||
let _ = ManuallyDrop::new(self); | ||
initialized | ||
} | ||
} | ||
|
||
impl<T> AsRef<T> for PciPage<'_, T> { | ||
fn as_ref(&self) -> &T { | ||
unsafe { self.base.as_ref() } | ||
} | ||
} | ||
|
||
impl<T> AsMut<T> for PciPage<'_, T> { | ||
fn as_mut(&mut self) -> &mut T { | ||
unsafe { self.base.as_mut() } | ||
} | ||
} | ||
|
||
impl<T> Deref for PciPage<'_, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<T> DerefMut for PciPage<'_, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.as_mut() | ||
} | ||
} | ||
|
||
impl<T> Drop for PciPage<'_, T> { | ||
fn drop(&mut self) { | ||
let status = unsafe { | ||
(self.proto.free_buffer)(self.proto, self.pages.get(), self.base.as_ptr().cast()) | ||
}; | ||
match status { | ||
Status::SUCCESS => { | ||
trace!( | ||
"Freed {} pages at 0x{:X}", | ||
self.pages.get(), | ||
self.base.as_ptr().addr() | ||
); | ||
} | ||
Status::INVALID_PARAMETER => { | ||
panic!("PciBuffer was not created through valid protocol usage!") | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
//! Defines wrapper for a region mapped by PCI Root Bridge I/O protocol. | ||
use core::ffi::c_void; | ||
use core::fmt::Debug; | ||
use core::marker::PhantomData; | ||
use log::trace; | ||
use uefi_raw::Status; | ||
use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocol; | ||
|
||
/// Represents a region of memory mapped by PCI Root Bridge I/O protocol. | ||
/// The region will be unmapped automatically when it is dropped. | ||
/// | ||
/// # Lifetime | ||
/// `'p` is the lifetime for Protocol. | ||
/// `'r` is the lifetime for Mapped Region. | ||
/// Protocol must outlive the mapped region | ||
/// as unmap function can only be accessed through the protocol. | ||
#[derive(Debug)] | ||
pub struct PciMappedRegion<'p, 'r> | ||
where | ||
'p: 'r, | ||
{ | ||
region: PciRegion, | ||
_lifetime_holder: PhantomData<&'r ()>, | ||
key: *const c_void, | ||
proto: &'p PciRootBridgeIoProtocol, | ||
} | ||
|
||
/// Represents a region of memory in PCI root bridge memory space. | ||
/// CPU cannot use address in this struct to deference memory. | ||
/// This is effectively the same as rust's slice type. | ||
/// This type only exists to prevent users from accidentally dereferencing it. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct PciRegion { | ||
/// Starting address of the memory region | ||
pub device_address: u64, | ||
|
||
/// Byte length of the memory region. | ||
pub length: usize, | ||
} | ||
|
||
impl<'p, 'r> PciMappedRegion<'p, 'r> | ||
where | ||
'p: 'r, | ||
{ | ||
#[allow(dead_code)] // TODO Implement Map function | ||
pub(crate) fn new<T: ?Sized>( | ||
device_address: u64, | ||
length: usize, | ||
key: *const c_void, | ||
_to_map: &'r T, | ||
proto: &'p PciRootBridgeIoProtocol, | ||
) -> Self { | ||
let end = device_address + length as u64; | ||
trace!("Mapped new region [0x{device_address:X}..0x{end:X}]"); | ||
Self { | ||
region: PciRegion { | ||
device_address, | ||
length, | ||
}, | ||
_lifetime_holder: PhantomData, | ||
key, | ||
proto, | ||
} | ||
} | ||
|
||
/// Returns mapped address and length of a region. | ||
/// | ||
/// # Warning | ||
/// **Returned address cannot be used to reference memory from CPU!** | ||
/// **Do not cast it back to pointer or reference** | ||
#[must_use] | ||
pub const fn region(&self) -> PciRegion { | ||
self.region | ||
} | ||
} | ||
|
||
impl Drop for PciMappedRegion<'_, '_> { | ||
fn drop(&mut self) { | ||
let status = unsafe { (self.proto.unmap)(self.proto, self.key) }; | ||
match status { | ||
Status::SUCCESS => { | ||
let end = self.region.device_address + self.region.length as u64; | ||
trace!( | ||
"Region [0x{:X}..0x{:X}] was unmapped", | ||
self.region.device_address, end | ||
); | ||
} | ||
Status::INVALID_PARAMETER => { | ||
panic!("This region was not mapped using PciRootBridgeIo::map"); | ||
} | ||
Status::DEVICE_ERROR => { | ||
panic!("The data was not committed to the target system memory."); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
impl PciRegion { | ||
/// Creates a new region of memory with different length. | ||
/// The new region must have shorter length to ensure | ||
/// it won't contain invalid memory address. | ||
#[must_use] | ||
pub fn with_length(self, new_length: usize) -> Self { | ||
assert!(new_length <= self.length); | ||
Self { | ||
device_address: self.device_address, | ||
length: new_length, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to add a
fn unmap(self) -> Result
method, so that callers can choose to handle errors from unmapping? TheDrop
trait doesn't really allow that. I think the drop implementation should probably not panic on errors, and that we should remove theunreachable
; UEFI implementations sometimes return additional errors beyond what the spec includes.Ditto for the
PciPage
implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I initially put unreachable macro there to indicate that "errors that may happen according to spec are all handled and no other error can occur"
But yeah second thought we can never be sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tho, what else should it do when an error occurs? Should I make it undroppable(assuming it's possible) so that users must use functions like unmap?