acpi.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * 解析acpi信息的模块
  3. **/
  4. #pragma once
  5. #include "../../common/glib.h"
  6. #include "../../mm/mm.h"
  7. #define ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC 0
  8. #define ACPI_ICS_TYPE_IO_APIC 1
  9. #define ACPI_ICS_TYPE_INTERRUPT_SOURCE_OVERRIDE 2
  10. #define ACPI_ICS_TYPE_NMI_SOURCE 3
  11. #define ACPI_ICS_TYPE_LOCAL_APIC_NMI 4
  12. #define ACPI_ICS_TYPE_LOCAL_APIC_ADDRESS_OVERRIDE 5
  13. #define ACPI_ICS_TYPE_IO_SAPIC 6
  14. #define ACPI_ICS_TYPE_LOCAL_SAPIC 7
  15. #define ACPI_ICS_TYPE_PLATFORM_INTERRUPT_SOURCES 8
  16. #define ACPI_ICS_TYPE_PROCESSOR_LOCAL_x2APIC 9
  17. #define ACPI_ICS_TYPE_PROCESSOR_LOCAL_x2APIC_NMI 0xA
  18. #define ACPI_ICS_TYPE_PROCESSOR_GICC 0xB
  19. #define ACPI_ICS_TYPE_PROCESSOR_GICD 0xC
  20. #define ACPI_ICS_TYPE_PROCESSOR_GIC_MSI_Frame 0xD
  21. #define ACPI_ICS_TYPE_PROCESSOR_GICR 0xE
  22. #define ACPI_ICS_TYPE_PROCESSOR_GIC_ITS 0xF
  23. // 0x10-0x7f Reserved. OSPM skips structures of the reserved type.
  24. // 0x80-0xff Reserved for OEM use
  25. #define ACPI_RSDT_VIRT_ADDR_BASE SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + ACPI_RSDT_MAPPING_OFFSET
  26. #define ACPI_XSDT_VIRT_ADDR_BASE SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + ACPI_XSDT_MAPPING_OFFSET
  27. #define ACPI_DESCRIPTION_HEDERS_BASE ACPI_RSDT_VIRT_ADDR_BASE + (PAGE_2M_SIZE)
  28. #define ACPI_XSDT_DESCRIPTION_HEDERS_BASE ACPI_XSDT_VIRT_ADDR_BASE + (PAGE_2M_SIZE)
  29. bool acpi_use_xsdt = false;
  30. struct acpi_RSDP_t
  31. {
  32. unsigned char Signature[8];
  33. unsigned char Checksum;
  34. unsigned char OEMID[6];
  35. unsigned char Revision;
  36. // 32bit physical address of the RSDT
  37. uint RsdtAddress;
  38. } __attribute__((packed));
  39. struct acpi_RSDP_2_t
  40. {
  41. struct acpi_RSDP_t rsdp1;
  42. // fields below are only valid when the revision value is 2 or above
  43. // 表的长度(单位:字节)从offset=0开始算
  44. uint Length;
  45. // 64bit的XSDT的物理地址
  46. ul XsdtAddress;
  47. unsigned char ExtendedChecksum; // 整个表的checksum,包括了之前的checksum区域
  48. unsigned char Reserved[3];
  49. } __attribute__((packed));
  50. struct acpi_system_description_table_header_t
  51. {
  52. // The ascii string representation of the table header.
  53. unsigned char Signature[4];
  54. // 整个表的长度(单位:字节),包括了header,从偏移量0处开始
  55. uint Length;
  56. // The revision of the structure corresponding to the signature field for this table.
  57. unsigned char Revision;
  58. // The entire table, including the checksum field, must add to zero to be considered valid.
  59. char Checksum;
  60. unsigned char OEMID[6];
  61. unsigned char OEM_Table_ID[8];
  62. uint OEMRevision;
  63. uint CreatorID;
  64. uint CreatorRevision;
  65. } __attribute__((packed));
  66. // HPET描述符结构体,sign为HPET
  67. struct acpi_HPET_description_table_t
  68. {
  69. struct acpi_system_description_table_header_t header;
  70. uint8_t hardware_rev_id;
  71. uint8_t comparator_count : 5; // Number of Comparators in 1st Timer Block
  72. uint8_t counter_size : 1; // COUNT_SIZE_CAP counter size
  73. uint8_t reserved0 : 1;
  74. uint8_t legacy_replacement : 1; // LegacyReplacement IRQ Routing Capable
  75. uint16_t pci_vendor_id; // PCI Vendor ID of 1st Timer Block
  76. uint8_t address_space_id; // 0 - system memory, 1 - system I/O
  77. uint8_t register_bit_width;
  78. uint8_t register_bit_offset;
  79. uint8_t reserved1;
  80. uint64_t address;
  81. uint8_t hpet_number;
  82. uint16_t minimum_tick; // The minimum clock ticks can be set without lost interrupts while the counter is programmed to operate in periodic mode
  83. uint8_t page_protection;
  84. } __attribute__((packed));
  85. // =========== MADT结构,其中Signature为APIC ============
  86. struct acpi_Multiple_APIC_Description_Table_t
  87. {
  88. struct acpi_system_description_table_header_t header;
  89. // 32bit的,每个处理器可访问的local中断控制器的物理地址
  90. uint Local_Interrupt_Controller_Address;
  91. // Multiple APIC flags, 详见 ACPI Specification Version 6.3, Table 5-44
  92. uint flags;
  93. // 接下来的(length-44)字节是Interrupt Controller Structure
  94. };
  95. struct apic_Interrupt_Controller_Structure_header_t
  96. {
  97. unsigned char type;
  98. unsigned char length;
  99. };
  100. struct acpi_Processor_Local_APIC_Structure_t
  101. {
  102. // type=0
  103. struct apic_Interrupt_Controller_Structure_header_t header;
  104. unsigned char ACPI_Processor_UID;
  105. // 处理器的local apic id
  106. unsigned char ACPI_ID;
  107. //详见 ACPI Specification Version 6.3, Table 5-47
  108. uint flags;
  109. };
  110. struct acpi_IO_APIC_Structure_t
  111. {
  112. // type=1
  113. struct apic_Interrupt_Controller_Structure_header_t header;
  114. unsigned char IO_APIC_ID;
  115. unsigned char Reserved;
  116. // 32bit的IO APIC物理地址 (每个IO APIC都有一个独立的物理地址)
  117. uint IO_APIC_Address;
  118. // 当前IO APIC的全局系统中断向量号起始值
  119. // The number of intr inputs is determined by the IO APIC's Max Redir Entry register.
  120. uint Global_System_Interrupt_Base;
  121. };
  122. // =========== RSDT 结构 =============
  123. struct acpi_RSDT_Structure_t
  124. {
  125. // 通过RSDT的header->Length可以计算出entry的数量n
  126. // n = (length - 32)/4
  127. struct acpi_system_description_table_header_t header;
  128. // 一个包含了n个32bit物理地址的数组,指向了其他的description headers
  129. uint Entry;
  130. };
  131. // =========== XSDT 结构 =============
  132. struct acpi_XSDT_Structure_t
  133. {
  134. // 通过RSDT的header->Length可以计算出entry的数量n
  135. // n = (length - 36)/8
  136. struct acpi_system_description_table_header_t header;
  137. // 一个包含了n个64bit物理地址的数组,指向了其他的description headers
  138. ul Entry;
  139. };
  140. /**
  141. * @brief 迭代器,用于迭代描述符头(位于ACPI标准文件的Table 5-29)
  142. * @param _fun 迭代操作调用的函数
  143. * @param _data 数据
  144. */
  145. void acpi_iter_SDT(bool (*_fun)(const struct acpi_system_description_table_header_t *, void *),
  146. void *_data);
  147. /**
  148. * @brief 获取MADT信息 Multiple APIC Description Table
  149. *
  150. * @param _iter_data 要被迭代的信息的结构体
  151. * @param _data 返回的MADT的虚拟地址
  152. * @param count 返回数组的长度
  153. * @return true
  154. * @return false
  155. */
  156. bool acpi_get_MADT(const struct acpi_system_description_table_header_t *_iter_data, void *_data);
  157. /**
  158. * @brief 获取HPET HPET_description_table
  159. *
  160. * @param _iter_data 要被迭代的信息的结构体
  161. * @param _data 返回的HPET表的虚拟地址
  162. * @return true
  163. * @return false
  164. */
  165. bool acpi_get_HPET(const struct acpi_system_description_table_header_t *_iter_data, void *_data);
  166. // 初始化acpi模块
  167. void acpi_init();