acpi-microvm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Support for generating ACPI tables and passing them to Guests
  2. *
  3. * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
  4. * Copyright (C) 2006 Fabrice Bellard
  5. * Copyright (C) 2013 Red Hat Inc
  6. *
  7. * Author: Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "exec/memory.h"
  23. #include "hw/acpi/acpi.h"
  24. #include "hw/acpi/aml-build.h"
  25. #include "hw/acpi/bios-linker-loader.h"
  26. #include "hw/acpi/generic_event_device.h"
  27. #include "hw/acpi/utils.h"
  28. #include "hw/boards.h"
  29. #include "hw/i386/fw_cfg.h"
  30. #include "hw/i386/microvm.h"
  31. #include "acpi-common.h"
  32. #include "acpi-microvm.h"
  33. static void
  34. build_dsdt_microvm(GArray *table_data, BIOSLinker *linker,
  35. MicrovmMachineState *mms)
  36. {
  37. X86MachineState *x86ms = X86_MACHINE(mms);
  38. Aml *dsdt, *sb_scope, *scope, *pkg;
  39. bool ambiguous;
  40. Object *isabus;
  41. isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
  42. assert(isabus);
  43. assert(!ambiguous);
  44. dsdt = init_aml_allocator();
  45. /* Reserve space for header */
  46. acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader));
  47. sb_scope = aml_scope("_SB");
  48. fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg);
  49. isa_build_aml(ISA_BUS(isabus), sb_scope);
  50. build_ged_aml(sb_scope, GED_DEVICE, HOTPLUG_HANDLER(mms->acpi_dev),
  51. GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE);
  52. acpi_dsdt_add_power_button(sb_scope);
  53. aml_append(dsdt, sb_scope);
  54. /* ACPI 5.0: Table 7-209 System State Package */
  55. scope = aml_scope("\\");
  56. pkg = aml_package(4);
  57. aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5));
  58. aml_append(pkg, aml_int(0)); /* ignored */
  59. aml_append(pkg, aml_int(0)); /* reserved */
  60. aml_append(pkg, aml_int(0)); /* reserved */
  61. aml_append(scope, aml_name_decl("_S5", pkg));
  62. aml_append(dsdt, scope);
  63. /* copy AML table into ACPI tables blob and patch header there */
  64. g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
  65. build_header(linker, table_data,
  66. (void *)(table_data->data + table_data->len - dsdt->buf->len),
  67. "DSDT", dsdt->buf->len, 2, NULL, NULL);
  68. free_aml_allocator();
  69. }
  70. static void acpi_build_microvm(AcpiBuildTables *tables,
  71. MicrovmMachineState *mms)
  72. {
  73. MachineState *machine = MACHINE(mms);
  74. GArray *table_offsets;
  75. GArray *tables_blob = tables->table_data;
  76. unsigned dsdt, xsdt;
  77. AcpiFadtData pmfadt = {
  78. /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */
  79. .rev = 5,
  80. .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) |
  81. (1 << ACPI_FADT_F_RESET_REG_SUP)),
  82. /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */
  83. .sleep_ctl = {
  84. .space_id = AML_AS_SYSTEM_MEMORY,
  85. .bit_width = 8,
  86. .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL,
  87. },
  88. .sleep_sts = {
  89. .space_id = AML_AS_SYSTEM_MEMORY,
  90. .bit_width = 8,
  91. .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS,
  92. },
  93. /* ACPI 5.0: 4.8.3.6 Reset Register */
  94. .reset_reg = {
  95. .space_id = AML_AS_SYSTEM_MEMORY,
  96. .bit_width = 8,
  97. .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET,
  98. },
  99. .reset_val = ACPI_GED_RESET_VALUE,
  100. };
  101. table_offsets = g_array_new(false, true /* clear */,
  102. sizeof(uint32_t));
  103. bios_linker_loader_alloc(tables->linker,
  104. ACPI_BUILD_TABLE_FILE, tables_blob,
  105. 64 /* Ensure FACS is aligned */,
  106. false /* high memory */);
  107. dsdt = tables_blob->len;
  108. build_dsdt_microvm(tables_blob, tables->linker, mms);
  109. pmfadt.dsdt_tbl_offset = &dsdt;
  110. pmfadt.xdsdt_tbl_offset = &dsdt;
  111. acpi_add_table(table_offsets, tables_blob);
  112. build_fadt(tables_blob, tables->linker, &pmfadt, NULL, NULL);
  113. acpi_add_table(table_offsets, tables_blob);
  114. acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine),
  115. mms->acpi_dev, false);
  116. xsdt = tables_blob->len;
  117. build_xsdt(tables_blob, tables->linker, table_offsets, NULL, NULL);
  118. /* RSDP is in FSEG memory, so allocate it separately */
  119. {
  120. AcpiRsdpData rsdp_data = {
  121. /* ACPI 2.0: 5.2.4.3 RSDP Structure */
  122. .revision = 2, /* xsdt needs v2 */
  123. .oem_id = ACPI_BUILD_APPNAME6,
  124. .xsdt_tbl_offset = &xsdt,
  125. .rsdt_tbl_offset = NULL,
  126. };
  127. build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
  128. }
  129. /* Cleanup memory that's no longer used. */
  130. g_array_free(table_offsets, true);
  131. }
  132. static void acpi_build_no_update(void *build_opaque)
  133. {
  134. /* nothing, microvm tables don't change at runtime */
  135. }
  136. void acpi_setup_microvm(MicrovmMachineState *mms)
  137. {
  138. X86MachineState *x86ms = X86_MACHINE(mms);
  139. AcpiBuildTables tables;
  140. assert(x86ms->fw_cfg);
  141. if (!x86_machine_is_acpi_enabled(x86ms)) {
  142. return;
  143. }
  144. acpi_build_tables_init(&tables);
  145. acpi_build_microvm(&tables, mms);
  146. /* Now expose it all to Guest */
  147. acpi_add_rom_blob(acpi_build_no_update, NULL,
  148. tables.table_data,
  149. ACPI_BUILD_TABLE_FILE,
  150. ACPI_BUILD_TABLE_MAX_SIZE);
  151. acpi_add_rom_blob(acpi_build_no_update, NULL,
  152. tables.linker->cmd_blob,
  153. "etc/table-loader", 0);
  154. acpi_add_rom_blob(acpi_build_no_update, NULL,
  155. tables.rsdp,
  156. ACPI_BUILD_RSDP_FILE, 0);
  157. acpi_build_tables_cleanup(&tables, false);
  158. }