-
Notifications
You must be signed in to change notification settings - Fork 161
acpi_spec: add MCFG definitions and parsing #1985
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
Merged
jackschefer-msft
merged 3 commits into
microsoft:main
from
jackschefer-msft:acpi-pcie-mcfg
Sep 19, 2025
Merged
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,154 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #[cfg(feature = "alloc")] | ||
| pub use self::alloc_parse::*; | ||
|
|
||
| use super::Table; | ||
| use crate::packed_nums::*; | ||
| use core::mem::size_of; | ||
| use static_assertions::const_assert_eq; | ||
| use thiserror::Error; | ||
| use zerocopy::FromBytes; | ||
| use zerocopy::Immutable; | ||
| use zerocopy::IntoBytes; | ||
| use zerocopy::KnownLayout; | ||
| use zerocopy::Ref; | ||
| use zerocopy::Unaligned; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Copy, Clone, Debug, IntoBytes, Immutable, KnownLayout, FromBytes, Unaligned)] | ||
| pub struct McfgHeader { | ||
| pub rsvd: u64_ne, | ||
| } | ||
|
|
||
| impl McfgHeader { | ||
| pub fn new() -> Self { | ||
| McfgHeader { rsvd: 0.into() } | ||
| } | ||
| } | ||
|
|
||
| impl Table for McfgHeader { | ||
| const SIGNATURE: [u8; 4] = *b"MCFG"; | ||
| } | ||
|
|
||
| pub const MCFG_REVISION: u8 = 1; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Copy, Clone, Debug, IntoBytes, Immutable, KnownLayout, FromBytes, Unaligned)] | ||
| pub struct McfgSegmentBusRange { | ||
| pub ecam_base: u64_ne, | ||
| pub segment: u16_ne, | ||
| pub start_bus: u8, | ||
| pub end_bus: u8, | ||
| pub rsvd: u32_ne, | ||
| } | ||
|
|
||
| const_assert_eq!(size_of::<McfgSegmentBusRange>(), 16); | ||
|
|
||
| impl McfgSegmentBusRange { | ||
| pub fn new(ecam_base: u64, segment: u16, start_bus: u8, end_bus: u8) -> Self { | ||
| Self { | ||
| ecam_base: ecam_base.into(), | ||
| segment: segment.into(), | ||
| start_bus, | ||
| end_bus, | ||
| rsvd: 0.into(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum ParseMcfgError { | ||
| #[error("could not read standard ACPI header")] | ||
| MissingAcpiHeader, | ||
jackschefer-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[error("invalid signature. expected b\"MCFG\", found {0:?}")] | ||
| InvalidSignature([u8; 4]), | ||
| #[error("mismatched lengh, header: {0}, actual: {1}")] | ||
| MismatchedLength(usize, usize), | ||
| #[error("could not read fixed MCFG header")] | ||
| MissingFixedHeader, | ||
| #[error("could not read segment bus range structure")] | ||
| BadSegmentBusRange, | ||
| } | ||
|
|
||
| pub fn parse_mcfg<'a>( | ||
| raw_mcfg: &'a [u8], | ||
| mut on_segment_bus_range: impl FnMut(&'a McfgSegmentBusRange), | ||
| ) -> Result<(&'a crate::Header, &'a McfgHeader), ParseMcfgError> { | ||
| let raw_mcfg_len = raw_mcfg.len(); | ||
| let (acpi_header, buf) = Ref::<_, crate::Header>::from_prefix(raw_mcfg) | ||
| .map_err(|_| ParseMcfgError::MissingAcpiHeader)?; | ||
|
|
||
| if acpi_header.signature != *b"MCFG" { | ||
| return Err(ParseMcfgError::InvalidSignature(acpi_header.signature)); | ||
| } | ||
|
|
||
| if acpi_header.length.get() as usize != raw_mcfg_len { | ||
| return Err(ParseMcfgError::MismatchedLength( | ||
| acpi_header.length.get() as usize, | ||
| raw_mcfg_len, | ||
| )); | ||
| } | ||
|
|
||
| let (mcfg_header, mut buf) = | ||
| Ref::<_, McfgHeader>::from_prefix(buf).map_err(|_| ParseMcfgError::MissingFixedHeader)?; | ||
|
|
||
| while !buf.is_empty() { | ||
| let (sbr, rest) = Ref::<_, McfgSegmentBusRange>::from_prefix(buf) | ||
| .map_err(|_| ParseMcfgError::BadSegmentBusRange)?; | ||
| on_segment_bus_range(Ref::into_ref(sbr)); | ||
| buf = rest | ||
| } | ||
|
|
||
| Ok((Ref::into_ref(acpi_header), Ref::into_ref(mcfg_header))) | ||
| } | ||
|
|
||
| #[cfg(feature = "alloc")] | ||
| pub mod alloc_parse { | ||
chris-oo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| use super::*; | ||
| use alloc::vec::Vec; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct BorrowedMcfg<'a> { | ||
| pub acpi_header: &'a crate::Header, | ||
| pub mcfg_header: &'a McfgHeader, | ||
| pub segment_bus_ranges: Vec<&'a McfgSegmentBusRange>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct OwnedMcfg { | ||
| pub acpi_header: crate::Header, | ||
| pub mcfg_header: McfgHeader, | ||
| pub segment_bus_ranges: Vec<McfgSegmentBusRange>, | ||
| } | ||
|
|
||
| impl From<BorrowedMcfg<'_>> for OwnedMcfg { | ||
| fn from(b: BorrowedMcfg<'_>) -> Self { | ||
| OwnedMcfg { | ||
| acpi_header: *b.acpi_header, | ||
| mcfg_header: *b.mcfg_header, | ||
| segment_bus_ranges: b.segment_bus_ranges.into_iter().copied().collect(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl BorrowedMcfg<'_> { | ||
| pub fn new(raw_mcfg: &[u8]) -> Result<BorrowedMcfg<'_>, ParseMcfgError> { | ||
| let mut segment_bus_ranges = Vec::new(); | ||
| let (acpi_header, mcfg_header) = parse_mcfg(raw_mcfg, |x| segment_bus_ranges.push(x))?; | ||
|
|
||
| Ok(BorrowedMcfg { | ||
| acpi_header, | ||
| mcfg_header, | ||
| segment_bus_ranges, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl OwnedMcfg { | ||
| pub fn new(raw_mcfg: &[u8]) -> Result<OwnedMcfg, ParseMcfgError> { | ||
| Ok(BorrowedMcfg::new(raw_mcfg)?.into()) | ||
| } | ||
| } | ||
| } | ||
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.
This can be a
bitfield(u128), and then you're guaranteed that the size will be correct.In general, I prefer
bitfields for structures that are up to 128 bits in length that need to match some hardware spec. That is perhaps stylistic without any real basis. Interested what you / others think.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.
Personally, I think using a
bitfieldto represent a structure where all members are on byte boundaries is confusing, so I would lean towards keeping it as-is with the const assert. However, I am still in the process of refining my personal stylistic preferences in RustThere 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.
my rule of thumb is generally to use bitfields if there are actual bit fields, but if all members are standard integers then it's not usually something I use. I don't think we codify that anywhere, but regardless as long as you assert the size correctly I don't think either approach is wrong.