ACPIStaticParser.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/ACPI/ACPIStaticParser.h>
  27. #include <Kernel/IO.h>
  28. #include <Kernel/StdLib.h>
  29. #include <Kernel/VM/MemoryManager.h>
  30. //#define ACPI_DEBUG
  31. void ACPIStaticParser::initialize(ACPI_RAW::RSDPDescriptor20& rsdp)
  32. {
  33. if (!ACPIParser::is_initialized()) {
  34. new ACPIStaticParser(rsdp);
  35. }
  36. }
  37. void ACPIStaticParser::initialize_without_rsdp()
  38. {
  39. if (!ACPIParser::is_initialized()) {
  40. new ACPIStaticParser();
  41. }
  42. }
  43. bool ACPIStaticParser::is_initialized()
  44. {
  45. return ACPIParser::is_initialized();
  46. }
  47. void ACPIStaticParser::locate_static_data()
  48. {
  49. locate_main_system_description_table();
  50. initialize_main_system_description_table();
  51. init_fadt();
  52. locate_all_aml_tables();
  53. }
  54. ACPI_RAW::SDTHeader* ACPIStaticParser::find_table(const char* sig)
  55. {
  56. #ifdef ACPI_DEBUG
  57. dbgprintf("ACPI: Calling Find Table method!\n");
  58. #endif
  59. for (auto* physical_sdt_ptr : m_main_sdt->get_sdt_pointers()) {
  60. auto region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)physical_sdt_ptr)), (PAGE_SIZE * 2), "ACPI Static Parser Tables Finding", Region::Access::Read);
  61. ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)physical_sdt_ptr)).as_ptr();
  62. #ifdef ACPI_DEBUG
  63. dbgprintf("ACPI: Examining Table @ P 0x%x\n", physical_sdt_ptr);
  64. #endif
  65. if (!strncmp(sdt->sig, sig, 4)) {
  66. #ifdef ACPI_DEBUG
  67. dbgprintf("ACPI: Found Table @ P 0x%x\n", physical_sdt_ptr);
  68. #endif
  69. return physical_sdt_ptr;
  70. }
  71. }
  72. return nullptr;
  73. }
  74. void ACPIStaticParser::init_fadt()
  75. {
  76. kprintf("ACPI: Initializing Fixed ACPI data\n");
  77. kprintf("ACPI: Searching for the Fixed ACPI Data Table\n");
  78. ASSERT(find_table("FACP") != nullptr);
  79. auto* fadt_ptr = find_table("FACP");
  80. auto checkup_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)(fadt_ptr))), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read);
  81. #ifdef ACPI_DEBUG
  82. dbgprintf("ACPI: Checking FADT Length to choose the correct mapping size\n");
  83. #endif
  84. ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)checkup_region->vaddr().offset(offset_in_page((uintptr_t)(fadt_ptr))).as_ptr();
  85. #ifdef ACPI_DEBUG
  86. dbgprintf("ACPI: FADT @ V 0x%x, P 0x%x\n", sdt, fadt_ptr);
  87. #endif
  88. u32 length = sdt->length;
  89. kprintf("ACPI: Fixed ACPI data, Revision %u\n", sdt->revision);
  90. auto fadt_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)(fadt_ptr))), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser", Region::Access::Read);
  91. m_fadt = make<ACPI::FixedACPIData>(*(ACPI_RAW::FADT*)fadt_region->vaddr().offset(offset_in_page((uintptr_t)(fadt_ptr))).as_ptr());
  92. #ifdef ACPI_DEBUG
  93. dbgprintf("ACPI: Finished to initialize Fixed ACPI data\n");
  94. #endif
  95. }
  96. void ACPIStaticParser::do_acpi_reboot()
  97. {
  98. // FIXME: Determine if we need to do MMIO/PCI/IO access to reboot, according to ACPI spec 6.2, Section 4.8.3.6
  99. #ifdef ACPI_DEBUG
  100. dbgprintf("ACPI: Rebooting, Probing FADT (P @ 0x%x)\n", m_fadt.ptr());
  101. #endif
  102. if (m_fadt->m_revision >= 2) {
  103. kprintf("ACPI: Reboot, Sending value 0%x to Port 0x%x\n", m_fadt->m_reset_value, m_fadt->m_reset_reg.address);
  104. IO::out8(m_fadt->m_reset_reg.address, m_fadt->m_reset_value);
  105. } else {
  106. kprintf("ACPI: Reboot, Not supported!\n");
  107. }
  108. ASSERT_NOT_REACHED(); /// If rebooting didn't work, halt.
  109. }
  110. void ACPIStaticParser::do_acpi_shutdown()
  111. {
  112. kprintf("ACPI: Shutdown is not supported with the current configuration, Abort!\n");
  113. ASSERT_NOT_REACHED();
  114. }
  115. inline bool validate_acpi_table(ACPI_RAW::SDTHeader& v_header, size_t length)
  116. {
  117. u8 checksum = 0;
  118. auto* sdt = (u8*)&v_header;
  119. for (size_t i = 0; i < length; i++)
  120. checksum += sdt[i];
  121. if (checksum == 0)
  122. return true;
  123. return false;
  124. }
  125. size_t ACPIStaticParser::get_table_size(ACPI_RAW::SDTHeader& p_header)
  126. {
  127. InterruptDisabler disabler;
  128. #ifdef ACPI_DEBUG
  129. dbgprintf("ACPI: Checking SDT Length\n");
  130. #endif
  131. auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)&p_header & PAGE_MASK), (PAGE_SIZE * 2), "ACPI get_table_size()", Region::Access::Read);
  132. volatile auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)&p_header)).as_ptr();
  133. return sdt->length;
  134. }
  135. u8 ACPIStaticParser::get_table_revision(ACPI_RAW::SDTHeader& p_header)
  136. {
  137. InterruptDisabler disabler;
  138. #ifdef ACPI_DEBUG
  139. dbgprintf("ACPI: Checking SDT Revision\n");
  140. #endif
  141. auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)&p_header & PAGE_MASK), (PAGE_SIZE * 2), "ACPI get_table_revision()", Region::Access::Read);
  142. volatile auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)&p_header)).as_ptr();
  143. return sdt->revision;
  144. }
  145. void ACPIStaticParser::initialize_main_system_description_table()
  146. {
  147. #ifdef ACPI_DEBUG
  148. dbgprintf("ACPI: Checking Main SDT Length to choose the correct mapping size\n");
  149. #endif
  150. ASSERT(m_main_system_description_table != nullptr);
  151. u32 length;
  152. u8 revision;
  153. if (m_xsdt_supported) {
  154. length = get_table_size(*m_main_system_description_table);
  155. revision = get_table_revision(*m_main_system_description_table);
  156. } else {
  157. length = get_table_size(*m_main_system_description_table);
  158. revision = get_table_revision(*m_main_system_description_table);
  159. }
  160. auto main_sdt_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)m_main_system_description_table)), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser Initialization", Region::Access::Read, false, true);
  161. volatile auto* sdt = (ACPI_RAW::SDTHeader*)main_sdt_region->vaddr().offset(offset_in_page((uintptr_t)m_main_system_description_table)).as_ptr();
  162. kprintf("ACPI: Main Description Table valid? 0x%x\n", validate_acpi_table(const_cast<ACPI_RAW::SDTHeader&>(*sdt), length));
  163. Vector<ACPI_RAW::SDTHeader*> sdt_pointers;
  164. if (m_xsdt_supported) {
  165. volatile auto* xsdt = (volatile ACPI_RAW::XSDT*)sdt;
  166. kprintf("ACPI: Using XSDT, Enumerating tables @ P 0x%x\n", m_main_system_description_table);
  167. kprintf("ACPI: XSDT Revision %d, Total length - %u\n", revision, length);
  168. #ifdef ACPI_DEBUG
  169. dbgprintf("ACPI: XSDT pointer @ V 0x%x\n", xsdt);
  170. #endif
  171. for (u32 i = 0; i < ((length - sizeof(ACPI_RAW::SDTHeader)) / sizeof(u64)); i++) {
  172. #ifdef ACPI_DEBUG
  173. dbgprintf("ACPI: Found new table [%u], @ V0x%x - P0x%x\n", i, &xsdt->table_ptrs[i], xsdt->table_ptrs[i]);
  174. #endif
  175. sdt_pointers.append((ACPI_RAW::SDTHeader*)xsdt->table_ptrs[i]);
  176. }
  177. } else {
  178. volatile auto* rsdt = (volatile ACPI_RAW::RSDT*)sdt;
  179. kprintf("ACPI: Using RSDT, Enumerating tables @ P 0x%x\n", m_main_system_description_table);
  180. kprintf("ACPI: RSDT Revision %d, Total length - %u\n", revision, length);
  181. #ifdef ACPI_DEBUG
  182. dbgprintf("ACPI: RSDT pointer @ V 0x%x\n", rsdt);
  183. #endif
  184. for (u32 i = 0; i < ((length - sizeof(ACPI_RAW::SDTHeader)) / sizeof(u32)); i++) {
  185. #ifdef ACPI_DEBUG
  186. dbgprintf("ACPI: Found new table [%u], @ V0x%x - P0x%x\n", i, &rsdt->table_ptrs[i], rsdt->table_ptrs[i]);
  187. #endif
  188. sdt_pointers.append((ACPI_RAW::SDTHeader*)rsdt->table_ptrs[i]);
  189. }
  190. }
  191. m_main_sdt = OwnPtr<ACPI::MainSystemDescriptionTable>(new ACPI::MainSystemDescriptionTable(move(sdt_pointers)));
  192. }
  193. void ACPIStaticParser::locate_main_system_description_table()
  194. {
  195. if (m_rsdp->base.revision == 0) {
  196. m_xsdt_supported = false;
  197. } else if (m_rsdp->base.revision >= 2) {
  198. if (m_rsdp->xsdt_ptr != (u64) nullptr) {
  199. m_xsdt_supported = true;
  200. } else {
  201. m_xsdt_supported = false;
  202. }
  203. }
  204. if (!m_xsdt_supported) {
  205. m_main_system_description_table = (ACPI_RAW::SDTHeader*)m_rsdp->base.rsdt_ptr;
  206. } else {
  207. m_main_system_description_table = (ACPI_RAW::SDTHeader*)m_rsdp->xsdt_ptr;
  208. }
  209. }
  210. void ACPIStaticParser::locate_all_aml_tables()
  211. {
  212. // Note: According to the ACPI spec, DSDT pointer may be found in the FADT table.
  213. // All other continuation of the DSDT can be found as pointers in the RSDT/XSDT.
  214. kprintf("ACPI: Searching for AML Tables\n");
  215. m_aml_tables_ptrs.append(m_fadt->get_dsdt());
  216. for (auto* sdt_ptr : m_main_sdt->get_sdt_pointers()) {
  217. auto region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)sdt_ptr)), (PAGE_SIZE * 2), "ACPI Static Parser AML Tables Finding", Region::Access::Read);
  218. auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)sdt_ptr)).as_ptr();
  219. #ifdef ACPI_DEBUG
  220. dbgprintf("ACPI: Examining Table @ P 0x%x\n", sdt_ptr);
  221. #endif
  222. if (!strncmp(sdt->sig, "SSDT", 4)) {
  223. kprintf("ACPI: Found AML Table @ P 0x%x, registering\n", sdt_ptr);
  224. m_aml_tables_ptrs.append(sdt);
  225. }
  226. }
  227. }
  228. ACPIStaticParser::ACPIStaticParser()
  229. : ACPIParser(true)
  230. , m_rsdp(nullptr)
  231. , m_main_sdt(nullptr)
  232. , m_fadt(nullptr)
  233. {
  234. m_rsdp = search_rsdp();
  235. if (m_rsdp != nullptr) {
  236. kprintf("ACPI: Using RSDP @ P 0x%x\n", m_rsdp);
  237. m_operable = true;
  238. locate_static_data();
  239. } else {
  240. m_operable = false;
  241. kprintf("ACPI: Disabled, due to RSDP being absent\n");
  242. }
  243. }
  244. ACPI_RAW::RSDPDescriptor20* ACPIStaticParser::search_rsdp()
  245. {
  246. auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "ACPI Static Parser RSDP Finding", Region::Access::Read);
  247. u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
  248. kprintf("ACPI: Probing EBDA, Segment 0x%x\n", ebda_seg);
  249. // FIXME: Ensure that we always have identity mapping (identity paging) here! Don't rely on existing mapping...
  250. for (char* rsdp_str = (char*)(PhysicalAddress(ebda_seg << 4).as_ptr()); rsdp_str < (char*)((ebda_seg << 4) + 1024); rsdp_str += 16) {
  251. #ifdef ACPI_DEBUG
  252. dbgprintf("ACPI: Looking for RSDP in EBDA @ Px%x\n", rsdp_str);
  253. #endif
  254. if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
  255. return (ACPI_RAW::RSDPDescriptor20*)rsdp_str;
  256. }
  257. // FIXME: Ensure that we always have identity mapping (identity paging) here! Don't rely on existing mapping...
  258. for (char* rsdp_str = (char*)(PhysicalAddress(0xE0000).as_ptr()); rsdp_str < (char*)0xFFFFF; rsdp_str += 16) {
  259. #ifdef ACPI_DEBUG
  260. dbgprintf("ACPI: Looking for RSDP in EBDA @ Px%x\n", rsdp_str);
  261. #endif
  262. if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
  263. return (ACPI_RAW::RSDPDescriptor20*)rsdp_str;
  264. }
  265. return nullptr;
  266. }
  267. ACPIStaticParser::ACPIStaticParser(ACPI_RAW::RSDPDescriptor20& rsdp)
  268. : ACPIParser(true)
  269. , m_rsdp(&rsdp)
  270. , m_main_sdt(nullptr)
  271. , m_fadt(nullptr)
  272. {
  273. kprintf("ACPI: Using RSDP @ Px%x\n", &rsdp);
  274. m_operable = true;
  275. locate_static_data();
  276. }
  277. ACPI::MainSystemDescriptionTable::MainSystemDescriptionTable(Vector<ACPI_RAW::SDTHeader*>&& sdt_pointers)
  278. {
  279. for (auto* sdt_ptr : sdt_pointers) {
  280. #ifdef ACPI_DEBUG
  281. dbgprintf("ACPI: Register new table in Main SDT, @ P 0x%x\n", sdt_ptr);
  282. #endif
  283. m_sdt_pointers.append(sdt_ptr);
  284. }
  285. }
  286. Vector<ACPI_RAW::SDTHeader*>& ACPI::MainSystemDescriptionTable::get_sdt_pointers()
  287. {
  288. return m_sdt_pointers;
  289. }
  290. ACPI::FixedACPIData::FixedACPIData(ACPI_RAW::FADT& fadt)
  291. {
  292. m_dsdt_ptr = fadt.dsdt_ptr;
  293. #ifdef ACPI_DEBUG
  294. dbgprintf("ACPI: DSDT pointer @ P 0x%x\n", m_dsdt_ptr);
  295. #endif
  296. m_revision = fadt.h.revision;
  297. m_x_dsdt_ptr = fadt.x_dsdt;
  298. m_preferred_pm_profile = fadt.preferred_pm_profile;
  299. m_sci_int = fadt.sci_int;
  300. m_smi_cmd = fadt.smi_cmd;
  301. m_acpi_enable_value = fadt.acpi_enable_value;
  302. m_acpi_disable_value = fadt.acpi_disable_value;
  303. m_s4bios_req = fadt.s4bios_req;
  304. m_pstate_cnt = fadt.pstate_cnt;
  305. m_PM1a_EVT_BLK = fadt.PM1a_EVT_BLK;
  306. m_PM1b_EVT_BLK = fadt.PM1b_EVT_BLK;
  307. m_PM1a_CNT_BLK = fadt.PM1a_CNT_BLK;
  308. m_PM1b_CNT_BLK = fadt.PM1b_CNT_BLK;
  309. m_PM2_CNT_BLK = fadt.PM2_CNT_BLK;
  310. m_PM_TMR_BLK = fadt.PM_TMR_BLK;
  311. m_GPE0_BLK = fadt.GPE0_BLK;
  312. m_GPE1_BLK = fadt.GPE1_BLK;
  313. m_PM1_EVT_LEN = fadt.PM1_EVT_LEN;
  314. m_PM1_CNT_LEN = fadt.PM1_CNT_LEN;
  315. m_PM2_CNT_LEN = fadt.PM2_CNT_LEN;
  316. m_PM_TMR_LEN = fadt.PM_TMR_LEN;
  317. m_GPE0_BLK_LEN = fadt.GPE0_BLK_LEN;
  318. m_GPE1_BLK_LEN = fadt.GPE1_BLK_LEN;
  319. m_GPE1_BASE = fadt.GPE1_BASE;
  320. m_cst_cnt = fadt.cst_cnt;
  321. m_P_LVL2_LAT = fadt.P_LVL2_LAT;
  322. m_P_LVL3_LAT = fadt.P_LVL3_LAT;
  323. m_flush_size = fadt.flush_size;
  324. m_flush_stride = fadt.flush_stride;
  325. m_duty_offset = fadt.duty_offset;
  326. m_duty_width = fadt.duty_width;
  327. m_day_alrm = fadt.day_alrm;
  328. m_mon_alrm = fadt.mon_alrm;
  329. m_century = fadt.century;
  330. m_ia_pc_boot_arch_flags = fadt.ia_pc_boot_arch_flags;
  331. m_flags = fadt.flags;
  332. m_reset_reg = fadt.reset_reg;
  333. #ifdef ACPI_DEBUG
  334. dbgprintf("ACPI: Reset Register @ IO 0x%x\n", m_reset_reg.address);
  335. dbgprintf("ACPI: Reset Register Address space %x\n", fadt.reset_reg.address_space);
  336. #endif
  337. m_reset_value = fadt.reset_value;
  338. #ifdef ACPI_DEBUG
  339. dbgprintf("ACPI: Reset Register value @ P 0x%x\n", m_reset_value);
  340. #endif
  341. m_x_pm1a_evt_blk = fadt.x_pm1a_evt_blk;
  342. m_x_pm1b_evt_blk = fadt.x_pm1b_evt_blk;
  343. m_x_pm1a_cnt_blk = fadt.x_pm1a_cnt_blk;
  344. m_x_pm1b_cnt_blk = fadt.x_pm1b_cnt_blk;
  345. m_x_pm2_cnt_blk = fadt.x_pm2_cnt_blk;
  346. m_x_pm_tmr_blk = fadt.x_pm_tmr_blk;
  347. m_x_gpe0_blk = fadt.x_gpe0_blk;
  348. m_x_gpe1_blk = fadt.x_gpe1_blk;
  349. m_sleep_control = fadt.sleep_control;
  350. m_sleep_status = fadt.sleep_status;
  351. m_hypervisor_vendor_identity = fadt.hypervisor_vendor_identity;
  352. }
  353. ACPI_RAW::SDTHeader* ACPI::FixedACPIData::get_dsdt()
  354. {
  355. if (m_x_dsdt_ptr != (uintptr_t) nullptr)
  356. return (ACPI_RAW::SDTHeader*)m_x_dsdt_ptr;
  357. else {
  358. ASSERT((ACPI_RAW::SDTHeader*)m_dsdt_ptr != nullptr);
  359. return (ACPI_RAW::SDTHeader*)m_dsdt_ptr;
  360. }
  361. }