-
-
Notifications
You must be signed in to change notification settings - Fork 184
uefi: Add device path generation for discovered devices in a PciTree #1831
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
seijikun
wants to merge
3
commits into
rust-osdev:main
Choose a base branch
from
seijikun:mr-pci-device_paths
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.
+139
−15
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
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
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 |
|---|---|---|
|
|
@@ -2,11 +2,14 @@ | |
|
|
||
| //! PCI Bus device function and bridge enumeration. | ||
|
|
||
| use core::mem; | ||
| use core::mem::{self, MaybeUninit}; | ||
|
|
||
| use alloc::collections::btree_map::BTreeMap; | ||
| use alloc::collections::btree_set::{self, BTreeSet}; | ||
|
|
||
| use crate::proto::device_path::build::{BuildError, DevicePathBuilder}; | ||
| use crate::proto::device_path::{self, DevicePath, DevicePathUtilitiesError, PoolDevicePath}; | ||
|
|
||
| use super::PciIoAddress; | ||
| use super::root_bridge::PciRootBridgeIo; | ||
|
|
||
|
|
@@ -58,6 +61,29 @@ fn read_device_register_u32<T: Sized + Copy>( | |
|
|
||
| // ########################################################################################## | ||
|
|
||
| /// Error type used by the device path construction of [`PciTree`]. | ||
| #[derive(Debug)] | ||
| pub enum PciDevicePathBuildError { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should implement |
||
| /// The given [`PciIoAddress`] was invalid or not path of the enumeration. | ||
| InvalidAddress, | ||
| /// Error while constructing the pci device DevicePath. | ||
| PathBuildError(BuildError), | ||
| /// Error while | ||
| DevicePathUtilitiesError(DevicePathUtilitiesError), | ||
| } | ||
| impl From<BuildError> for PciDevicePathBuildError { | ||
| fn from(value: BuildError) -> Self { | ||
| Self::PathBuildError(value) | ||
| } | ||
| } | ||
| impl From<DevicePathUtilitiesError> for PciDevicePathBuildError { | ||
| fn from(value: DevicePathUtilitiesError) -> Self { | ||
| Self::DevicePathUtilitiesError(value) | ||
| } | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------------------------ | ||
|
|
||
| /// Struct representing the tree structure of PCI devices. | ||
| /// | ||
| /// This allows iterating over all valid PCI device addresses in a tree, as well as querying | ||
|
|
@@ -125,6 +151,54 @@ impl PciTree { | |
| .map(|(bus, _)| bus) | ||
| .cloned() | ||
| } | ||
|
|
||
| /// Construct a device path for the given PCI `addr` and append it to the given `root_path`. | ||
| /// | ||
| /// # Arguments | ||
| /// - `root_path`: The [`DevicePath`] instance corresponding to the [`PciRootBridgeIo`] instance that | ||
| /// produced this [`PciTree`]. This path is prepended to the generated device paths. | ||
| /// - `addr`: [`PciIoAddress`] of the device | ||
| pub fn device_path( | ||
| &self, | ||
| root_path: &DevicePath, | ||
| addr: PciIoAddress, | ||
| ) -> Result<PoolDevicePath, PciDevicePathBuildError> { | ||
| use device_path::build; | ||
|
|
||
| if !self.devices.contains(&addr) { | ||
| return Err(PciDevicePathBuildError::InvalidAddress); | ||
| } | ||
|
|
||
| // A PCI [`DevicePath`] can have max. 255 PCI segments, each of which is 6 bytes in size. | ||
| // These are prepended by the given `root_path`. A construction buffer of 2048 bytes | ||
| // should thus suffice for all realistic scenarios. | ||
| let mut bfr = [MaybeUninit::uninit(); 2048]; | ||
| let mut builder = DevicePathBuilder::with_buf(&mut bfr); | ||
| for node in root_path.node_iter() { | ||
| builder = builder.push(&node)?; | ||
| } | ||
|
|
||
| // A pci device path is built by appending segments of `dev` and `fun` address byte pairs | ||
| // starting from a pci root bus to the specified address. Since the child <-> parent | ||
| // relationship is stored from child to parent, we start at the address and recurse back | ||
| // to the parent for path generation. | ||
| fn inner<'a>( | ||
| root: &PciTree, | ||
| mut builder: DevicePathBuilder<'a>, | ||
| addr: PciIoAddress, | ||
| ) -> Result<DevicePathBuilder<'a>, BuildError> { | ||
| if let Some(parent) = root.parent_for(addr) { | ||
| builder = inner(root, builder, parent)?; | ||
| } | ||
| builder.push(&build::hardware::Pci { | ||
| function: addr.fun, | ||
| device: addr.dev, | ||
| }) | ||
| } | ||
|
|
||
| builder = inner(self, builder, addr)?; | ||
| Ok(builder.finalize()?.to_pool()?) | ||
| } | ||
| } | ||
| impl IntoIterator for PciTree { | ||
| type Item = PciIoAddress; | ||
|
|
||
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.
Correct me if I'm wrong but I think
#[cfg(feature = "alloc")]is not needed here.PoolDevicePathworks with the underlying UEFI allocator and does not interfer with the global Rust allocator at all?The feature
allocis only for the global Rust allocator and not all allocations in general.