|
| 1 | +use bitflags::bitflags; |
| 2 | +use static_assertions::assert_eq_size; |
| 3 | + |
| 4 | +/// Descriptor for current PCI root bridge's configuration space. |
| 5 | +/// Specification: |
| 6 | +/// https://uefi.org/htmlspecs/ACPI_Spec_6_4_html/06_Device_Configuration/Device_Configuration.html#qword-address-space-descriptor |
| 7 | +#[repr(C, packed)] |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct QWordAddressSpaceDescriptor { |
| 10 | + tag: u8, |
| 11 | + descriptor_length: u16, |
| 12 | + pub resource_type: ResourceType, |
| 13 | + pub flags: GeneralFlags, |
| 14 | + pub type_flags: u8, |
| 15 | + pub address_granularity: u64, |
| 16 | + pub range_min: u64, |
| 17 | + pub range_max: u64, // inclusive |
| 18 | + pub translation_offset: u64, |
| 19 | + pub address_length: u64, |
| 20 | +} |
| 21 | +assert_eq_size!(QWordAddressSpaceDescriptor, [u8; 0x2E]); |
| 22 | + |
| 23 | +newtype_enum! { |
| 24 | + /// Indicates which type of resource this descriptor describes. |
| 25 | + pub enum ResourceType: u8 => { |
| 26 | + /// This resource describes range of memory. |
| 27 | + MEMORY = 0, |
| 28 | + |
| 29 | + /// This resource describes range of I/O ports. |
| 30 | + IO = 1, |
| 31 | + |
| 32 | + /// This resource describes range of Bus numbers. |
| 33 | + BUS = 2, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +bitflags! { |
| 38 | + #[repr(transparent)] |
| 39 | + #[derive(Debug, Copy, Clone)] |
| 40 | + pub struct GeneralFlags: u8 { |
| 41 | + /// Indicates maximum address is fixed. |
| 42 | + const MAX_ADDRESS_FIXED = 0b1000; |
| 43 | + |
| 44 | + /// Indicates minimum address is fixed. |
| 45 | + const MIN_ADDRESS_FIXED = 0b0100; |
| 46 | + |
| 47 | + /// Indicates if this bridge would subtract or positively decode address. |
| 48 | + /// 1 This bridge subtractively decodes this address (top level bridges only) |
| 49 | + /// 0 This bridge positively decodes this address |
| 50 | + const DECODE_TYPE = 0b0010; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl QWordAddressSpaceDescriptor { |
| 55 | + /// Verifies if given descriptor is valid according to specification. |
| 56 | + /// This also checks if all reserved bit fields which are supposed to be 0 are actually 0. |
| 57 | + pub fn verify(&self) { |
| 58 | + let tag = self.tag; |
| 59 | + if tag != 0x8A { |
| 60 | + panic!("Tag value for QWordAddressSpaceDescriptor should be 0x8A, not {}", tag); |
| 61 | + } |
| 62 | + |
| 63 | + let length = self.descriptor_length; |
| 64 | + if self.descriptor_length != 0x2B { |
| 65 | + panic!("Length value for QWordAddressSpaceDescriptor should be 0x2B, not {}", length); |
| 66 | + } |
| 67 | + |
| 68 | + if self.flags.bits() & 0b11110000 != 0 { |
| 69 | + panic!("Reserved bits for GeneralFlags are 1") |
| 70 | + } |
| 71 | + |
| 72 | + let type_flags = self.type_flags; |
| 73 | + match self.resource_type { |
| 74 | + ResourceType::MEMORY => { |
| 75 | + if type_flags & 0b11000000 != 0 { |
| 76 | + panic!("Reserved bits for Memory Type Flags are 1"); |
| 77 | + } |
| 78 | + } |
| 79 | + ResourceType::IO => { |
| 80 | + if type_flags & 0b11001100 != 0 { |
| 81 | + panic!("Reserved bits for IO Type Flags are 1"); |
| 82 | + } |
| 83 | + } |
| 84 | + ResourceType::BUS => { |
| 85 | + if type_flags != 0 { |
| 86 | + panic!("Bus type flags should be 0, not {}", type_flags); |
| 87 | + } |
| 88 | + } |
| 89 | + ResourceType(3..=191) => panic!("Invalid resource type: {}", self.resource_type.0), |
| 90 | + ResourceType(192..) => {} // Hardware defined range |
| 91 | + } |
| 92 | + |
| 93 | + let min = self.range_min; |
| 94 | + let max = self.range_max; |
| 95 | + if max < min { |
| 96 | + panic!("Address range is invalid. Max(0x{:X}) is smaller than Min(0x{:X}).", max, min); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments