lib.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //! A library for parsing ACPI tables. This crate can be used by bootloaders and kernels for
  2. //! architectures that support ACPI. The crate is far from feature-complete, but can still be used
  3. //! for finding and parsing the static tables, which is enough to set up hardware such as the APIC
  4. //! and HPET on x86_64.
  5. //!
  6. //! The crate is designed for use in conjunction with the `aml` crate, which is the (much
  7. //! less complete) AML parser used to parse the DSDT and SSDTs. These crates are separate because
  8. //! some kernels may want to detect the static tables, but delay AML parsing to a later stage.
  9. //!
  10. //! ### Usage
  11. //! To use the library, you will need to provide an implementation of the `AcpiHandler` trait,
  12. //! which allows the library to make requests such as mapping a particular region of physical
  13. //! memory into the virtual address space.
  14. //!
  15. //! You should then call one of the entry points, based on how much information you have:
  16. //! * Call `parse_rsdp` if you have the physical address of the RSDP
  17. //! * Call `parse_rsdt` if you have the physical address of the RSDT / XSDT
  18. //! * Call `search_for_rsdp_bios` if you don't have the address of either structure, but **you know
  19. //! you're running on BIOS, not UEFI**
  20. //!
  21. //! All of these methods return an instance of `Acpi`. This struct contains all the information
  22. //! gathered from the static tables, and can be queried to set up hardware etc.
  23. #![no_std]
  24. #![feature(nll, exclusive_range_pattern, const_generics)]
  25. extern crate alloc;
  26. #[cfg_attr(test, macro_use)]
  27. #[cfg(test)]
  28. extern crate std;
  29. mod fadt;
  30. pub mod handler;
  31. mod hpet;
  32. pub mod interrupt;
  33. mod madt;
  34. mod mcfg;
  35. mod rsdp;
  36. mod rsdp_search;
  37. mod sdt;
  38. pub use crate::{
  39. handler::{AcpiHandler, PhysicalMapping},
  40. hpet::HpetInfo,
  41. interrupt::InterruptModel,
  42. madt::MadtError,
  43. mcfg::PciConfigRegions,
  44. rsdp_search::search_for_rsdp_bios,
  45. };
  46. use crate::{
  47. rsdp::Rsdp,
  48. sdt::{SdtHeader, Signature},
  49. };
  50. use alloc::vec::Vec;
  51. use core::mem;
  52. #[derive(Debug)]
  53. pub enum AcpiError {
  54. RsdpIncorrectSignature,
  55. RsdpInvalidOemId,
  56. RsdpInvalidChecksum,
  57. NoValidRsdp,
  58. SdtInvalidSignature(Signature),
  59. SdtInvalidOemId(Signature),
  60. SdtInvalidTableId(Signature),
  61. SdtInvalidChecksum(Signature),
  62. InvalidMadt(MadtError),
  63. }
  64. #[derive(Clone, Copy, Debug)]
  65. #[repr(C, packed)]
  66. pub(crate) struct GenericAddress {
  67. address_space: u8,
  68. bit_width: u8,
  69. bit_offset: u8,
  70. access_size: u8,
  71. address: u64,
  72. }
  73. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  74. pub enum ProcessorState {
  75. /// A processor in this state is unusable, and you must not attempt to bring it up.
  76. Disabled,
  77. /// A processor waiting for a SIPI (Startup Inter-processor Interrupt) is currently not active,
  78. /// but may be brought up.
  79. WaitingForSipi,
  80. /// A Running processor is currently brought up and running code.
  81. Running,
  82. }
  83. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  84. pub struct Processor {
  85. pub processor_uid: u8,
  86. pub local_apic_id: u8,
  87. /// The state of this processor. Always check that the processor is not `Disabled` before
  88. /// attempting to bring it up!
  89. pub state: ProcessorState,
  90. /// Whether this processor is the Bootstrap Processor (BSP), or an Application Processor (AP).
  91. /// When the bootloader is entered, the BSP is the only processor running code. To run code on
  92. /// more than one processor, you need to "bring up" the APs.
  93. pub is_ap: bool,
  94. }
  95. #[derive(Debug)]
  96. pub struct AmlTable {
  97. /// Physical address of the start of the AML stream (excluding the table header).
  98. pub address: usize,
  99. /// Length (in bytes) of the AML stream.
  100. pub length: u32,
  101. }
  102. impl AmlTable {
  103. /// Create an `AmlTable` from the address and length of the table **including the SDT header**.
  104. pub(crate) fn new(address: usize, length: u32) -> AmlTable {
  105. AmlTable {
  106. address: address + mem::size_of::<SdtHeader>(),
  107. length: length - mem::size_of::<SdtHeader>() as u32,
  108. }
  109. }
  110. }
  111. #[derive(Debug)]
  112. pub struct Acpi {
  113. pub acpi_revision: u8,
  114. /// The boot processor. Until you bring up any APs, this is the only processor running code.
  115. pub boot_processor: Option<Processor>,
  116. /// Application processes. These are not brought up until you do so, and must be brought up in
  117. /// the order they appear in this list.
  118. pub application_processors: Vec<Processor>,
  119. /// ACPI theoretically allows for more than one interrupt model to be supported by the same
  120. /// hardware. For simplicity and because hardware practically will only support one model, we
  121. /// just error in cases that the tables detail more than one.
  122. pub interrupt_model: Option<InterruptModel>,
  123. pub hpet: Option<HpetInfo>,
  124. /// Info about the DSDT, if we find it.
  125. pub dsdt: Option<AmlTable>,
  126. /// Info about any SSDTs, if there are any.
  127. pub ssdts: Vec<AmlTable>,
  128. /// Info about the PCI-E configuration memory regions, collected from the MCFG.
  129. pub pci_config_regions: Option<PciConfigRegions>,
  130. }
  131. /// This is the entry point of `acpi` if you have the **physical** address of the RSDP. It maps
  132. /// the RSDP, works out what version of ACPI the hardware supports, and passes the physical
  133. /// address of the RSDT/XSDT to `parse_rsdt`.
  134. pub fn parse_rsdp<H>(handler: &mut H, rsdp_address: usize) -> Result<Acpi, AcpiError>
  135. where
  136. H: AcpiHandler,
  137. {
  138. let rsdp_mapping = handler.map_physical_region::<Rsdp>(rsdp_address, mem::size_of::<Rsdp>());
  139. (*rsdp_mapping).validate()?;
  140. parse_validated_rsdp(handler, rsdp_mapping)
  141. }
  142. fn parse_validated_rsdp<H>(handler: &mut H, rsdp_mapping: PhysicalMapping<Rsdp>) -> Result<Acpi, AcpiError>
  143. where
  144. H: AcpiHandler,
  145. {
  146. let revision = (*rsdp_mapping).revision();
  147. if revision == 0 {
  148. /*
  149. * We're running on ACPI Version 1.0. We should use the 32-bit RSDT address.
  150. */
  151. let rsdt_address = (*rsdp_mapping).rsdt_address();
  152. handler.unmap_physical_region(rsdp_mapping);
  153. parse_rsdt(handler, revision, rsdt_address as usize)
  154. } else {
  155. /*
  156. * We're running on ACPI Version 2.0+. We should use the 64-bit XSDT address, truncated
  157. * to 32 bits on x86.
  158. */
  159. let xsdt_address = (*rsdp_mapping).xsdt_address();
  160. handler.unmap_physical_region(rsdp_mapping);
  161. parse_rsdt(handler, revision, xsdt_address as usize)
  162. }
  163. }
  164. /// This is the entry point of `acpi` if you already have the **physical** address of the
  165. /// RSDT/XSDT; it parses all the SDTs in the RSDT/XSDT, calling the relevant handlers in the
  166. /// implementation's `AcpiHandler`.
  167. ///
  168. /// If the given revision is 0, an address to the RSDT is expected. Otherwise, an address to
  169. /// the XSDT is expected.
  170. pub fn parse_rsdt<H>(handler: &mut H, revision: u8, physical_address: usize) -> Result<Acpi, AcpiError>
  171. where
  172. H: AcpiHandler,
  173. {
  174. let mut acpi = Acpi {
  175. acpi_revision: revision,
  176. boot_processor: None,
  177. application_processors: Vec::new(),
  178. interrupt_model: None,
  179. hpet: None,
  180. dsdt: None,
  181. ssdts: Vec::new(),
  182. pci_config_regions: None,
  183. };
  184. let header = sdt::peek_at_sdt_header(handler, physical_address);
  185. let mapping = handler.map_physical_region::<SdtHeader>(physical_address, header.length as usize);
  186. if revision == 0 {
  187. /*
  188. * ACPI Version 1.0. It's a RSDT!
  189. */
  190. (*mapping).validate(sdt::Signature::RSDT)?;
  191. let num_tables = ((*mapping).length as usize - mem::size_of::<SdtHeader>()) / mem::size_of::<u32>();
  192. let tables_base = ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u32;
  193. for i in 0..num_tables {
  194. sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) } as usize)?;
  195. }
  196. } else {
  197. /*
  198. * ACPI Version 2.0+. It's a XSDT!
  199. */
  200. (*mapping).validate(sdt::Signature::XSDT)?;
  201. let num_tables = ((*mapping).length as usize - mem::size_of::<SdtHeader>()) / mem::size_of::<u64>();
  202. let tables_base = ((mapping.virtual_start.as_ptr() as usize) + mem::size_of::<SdtHeader>()) as *const u64;
  203. for i in 0..num_tables {
  204. sdt::dispatch_sdt(&mut acpi, handler, unsafe { *tables_base.offset(i as isize) } as usize)?;
  205. }
  206. }
  207. handler.unmap_physical_region(mapping);
  208. Ok(acpi)
  209. }