|
| 1 | +use super::rsdt::SDT; |
| 2 | +use super::GenericAddressStructure; |
| 3 | +use crate::error::{Error, Result}; |
| 4 | +use byteorder::{ByteOrder, NativeEndian}; |
| 5 | +use core::fmt; |
| 6 | +use core::ops::Range; |
| 7 | +use derive_try_from_primitive::TryFromPrimitive; |
| 8 | + |
| 9 | +mod offsets { |
| 10 | + use super::*; |
| 11 | + pub const EVENT_TIMER_BLOCK_ID: Range<usize> = 0..4; |
| 12 | + pub const BASE_ADDRESS: Range<usize> = 4..16; |
| 13 | + pub const HPET_NUMBER: usize = 16; |
| 14 | + pub const MIN_CLOCK_TICK: Range<usize> = 17..19; |
| 15 | + pub const PAGE_PROTECTION: usize = 19; |
| 16 | +} |
| 17 | + |
| 18 | +/// Page Protection for HPET register access. |
| 19 | +/// |
| 20 | +/// See Table 3 in the IA-PC HPET specification. |
| 21 | +#[repr(u8)] |
| 22 | +#[derive(Clone, Copy, Debug, PartialEq, TryFromPrimitive)] |
| 23 | +pub enum PageProtection { |
| 24 | + /// No page protection |
| 25 | + NoProtection = 0x00, |
| 26 | + /// Register access is protected by a 4KB page |
| 27 | + Protected4KB = 0x01, |
| 28 | + /// Register access is protected by a 64KB page |
| 29 | + Protected64KB = 0x02, |
| 30 | +} |
| 31 | + |
| 32 | +/// High Precision Event Timer ACPI entry. |
| 33 | +/// |
| 34 | +/// See `IA-PC HPET § 1.0a`. |
| 35 | +pub struct HPET<'a> { |
| 36 | + /// System Descriptor Table Header for this structure. |
| 37 | + sdt: &'a SDT<'a>, |
| 38 | + /// The hardware revision ID. |
| 39 | + pub hardware_rev_id: u8, |
| 40 | + /// The number of comparators in the first timer block. |
| 41 | + pub comparator_count: u8, |
| 42 | + /// The size cap of the counter. If false, then only 32 bit mode is allowed. |
| 43 | + /// If true, then both 32 bit and 64 bit modes are supported. |
| 44 | + pub counter_cap: bool, |
| 45 | + /// If true, then the HPET is LegacyReplacement IRQ routing capable. |
| 46 | + pub legacy_replacement: bool, |
| 47 | + /// The PCI vendor ID of the first timer block. |
| 48 | + pub pci_vendor_id: u16, |
| 49 | + /// The address of the HPET register stored as an ACPI GAS. |
| 50 | + pub address: GenericAddressStructure, |
| 51 | + /// The HPET sequence number. |
| 52 | + pub hpet_number: u8, |
| 53 | + /// The minimum number of ticks that must be used by any counter programmed |
| 54 | + /// in periodic mode to avoid lost interrupts. |
| 55 | + pub minimum_tick: u16, |
| 56 | + /// The type of page protection for HPET register access. |
| 57 | + pub page_protection: PageProtection, |
| 58 | +} |
| 59 | + |
| 60 | +impl<'a> HPET<'a> { |
| 61 | + /// Create a new HPET given a SDT. |
| 62 | + pub fn new(sdt: &'a SDT<'a>) -> Result<HPET<'a>> { |
| 63 | + let event_timer_block_id = |
| 64 | + NativeEndian::read_u32(&sdt.table[offsets::EVENT_TIMER_BLOCK_ID]); |
| 65 | + let address = |
| 66 | + GenericAddressStructure::new(&sdt.table[offsets::BASE_ADDRESS])?; |
| 67 | + let hpet_number = sdt.table[offsets::HPET_NUMBER]; |
| 68 | + let minimum_tick = |
| 69 | + NativeEndian::read_u16(&sdt.table[offsets::MIN_CLOCK_TICK]); |
| 70 | + |
| 71 | + let page_protection = |
| 72 | + PageProtection::try_from(sdt.table[offsets::PAGE_PROTECTION] & 0xF) |
| 73 | + .ok_or_else(|| { |
| 74 | + Error::InvalidValue(format!( |
| 75 | + "Invalid HPET Page Protection type: {}", |
| 76 | + sdt.table[offsets::PAGE_PROTECTION] & 0xF |
| 77 | + )) |
| 78 | + })?; |
| 79 | + |
| 80 | + let hardware_rev_id = (event_timer_block_id & 0xFF) as u8; |
| 81 | + let comparator_count = ((event_timer_block_id >> 8) & 0x1F) as u8; |
| 82 | + let counter_cap = ((event_timer_block_id >> 13) & 0x1) as u8; |
| 83 | + let legacy_replacement = ((event_timer_block_id >> 15) & 0x1) as u8; |
| 84 | + let pci_vendor_id = ((event_timer_block_id >> 16) & 0xFFFF) as u16; |
| 85 | + |
| 86 | + let counter_cap = counter_cap != 0; |
| 87 | + let legacy_replacement = legacy_replacement != 0; |
| 88 | + |
| 89 | + Ok(Self { |
| 90 | + sdt, |
| 91 | + hardware_rev_id, |
| 92 | + comparator_count, |
| 93 | + counter_cap, |
| 94 | + legacy_replacement, |
| 95 | + pci_vendor_id, |
| 96 | + address, |
| 97 | + hpet_number, |
| 98 | + minimum_tick, |
| 99 | + page_protection, |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl<'a> fmt::Debug for HPET<'a> { |
| 105 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 106 | + write!(f, "{:?}", self.sdt)?; |
| 107 | + write!(f, " HPET address=0x{:x}", self.address.address) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod test { |
| 113 | + use super::*; |
| 114 | + use crate::acpi::{AccessSize, AddressSpaceID}; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_hpet_parse() { |
| 118 | + // sample HPET ACPI entry |
| 119 | + let buf = [ |
| 120 | + 0x48, 0x50, 0x45, 0x54, 0x38, 0x00, 0x00, 0x00, 0x01, 0xb6, 0x41, |
| 121 | + 0x4c, 0x41, 0x53, 0x4b, 0x41, 0x41, 0x20, 0x4d, 0x20, 0x49, 0x00, |
| 122 | + 0x00, 0x00, 0x09, 0x20, 0x07, 0x01, 0x41, 0x4d, 0x49, 0x2e, 0x05, |
| 123 | + 0x00, 0x00, 0x00, 0x01, 0xa7, 0x86, 0x80, 0x00, 0x40, 0x00, 0x00, |
| 124 | + 0x00, 0x00, 0xd0, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0xee, 0x37, |
| 125 | + 0x00, |
| 126 | + ]; |
| 127 | + |
| 128 | + let hpet_sdt = unsafe { SDT::new(buf.as_ptr()).unwrap() }; |
| 129 | + let hpet = HPET::new(&hpet_sdt).unwrap(); |
| 130 | + |
| 131 | + assert_eq!(hpet.hardware_rev_id, 1); |
| 132 | + assert_eq!(hpet.comparator_count, 7); |
| 133 | + assert_eq!(hpet.counter_cap, true); |
| 134 | + assert_eq!(hpet.legacy_replacement, true); |
| 135 | + assert_eq!(hpet.pci_vendor_id, 0x8086); |
| 136 | + assert_eq!(hpet.address.address_space, AddressSpaceID::SystemMemory); |
| 137 | + assert_eq!(hpet.address.bit_width, 64); |
| 138 | + assert_eq!(hpet.address.bit_offset, 0); |
| 139 | + assert_eq!(hpet.address.access_size, AccessSize::Undefined); |
| 140 | + assert_eq!(hpet.address.address, 0xfed00000); |
| 141 | + assert_eq!(hpet.hpet_number, 0); |
| 142 | + assert_eq!(hpet.minimum_tick, 0x37ee); |
| 143 | + assert_eq!(hpet.page_protection, PageProtection::NoProtection); |
| 144 | + } |
| 145 | +} |
0 commit comments