2
0

acpi-microvm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "qemu/cutils.h"
  22. #include "qapi/error.h"
  23. #include "exec/memory.h"
  24. #include "hw/acpi/acpi.h"
  25. #include "hw/acpi/acpi_aml_interface.h"
  26. #include "hw/acpi/aml-build.h"
  27. #include "hw/acpi/bios-linker-loader.h"
  28. #include "hw/acpi/generic_event_device.h"
  29. #include "hw/acpi/utils.h"
  30. #include "hw/acpi/erst.h"
  31. #include "hw/i386/fw_cfg.h"
  32. #include "hw/i386/microvm.h"
  33. #include "hw/pci/pci.h"
  34. #include "hw/pci/pcie_host.h"
  35. #include "hw/usb/xhci.h"
  36. #include "hw/virtio/virtio-acpi.h"
  37. #include "hw/virtio/virtio-mmio.h"
  38. #include "hw/input/i8042.h"
  39. #include "acpi-common.h"
  40. #include "acpi-microvm.h"
  41. #include CONFIG_DEVICES
  42. static void acpi_dsdt_add_virtio(Aml *scope,
  43. MicrovmMachineState *mms)
  44. {
  45. gchar *separator;
  46. long int index;
  47. BusState *bus;
  48. BusChild *kid;
  49. bus = sysbus_get_default();
  50. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  51. Object *obj = object_dynamic_cast(OBJECT(kid->child),
  52. TYPE_VIRTIO_MMIO);
  53. if (obj) {
  54. VirtIOMMIOProxy *mmio = VIRTIO_MMIO(obj);
  55. VirtioBusState *mmio_virtio_bus = &mmio->bus;
  56. BusState *mmio_bus = &mmio_virtio_bus->parent_obj;
  57. if (QTAILQ_EMPTY(&mmio_bus->children)) {
  58. continue;
  59. }
  60. separator = g_strrstr(mmio_bus->name, ".");
  61. if (!separator) {
  62. continue;
  63. }
  64. if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) {
  65. continue;
  66. }
  67. uint32_t irq = mms->virtio_irq_base + index;
  68. hwaddr base = VIRTIO_MMIO_BASE + index * 512;
  69. hwaddr size = 512;
  70. virtio_acpi_dsdt_add(scope, base, size, irq, index, 1);
  71. }
  72. }
  73. }
  74. static void acpi_dsdt_add_xhci(Aml *scope, MicrovmMachineState *mms)
  75. {
  76. if (machine_usb(MACHINE(mms))) {
  77. xhci_sysbus_build_aml(scope, MICROVM_XHCI_BASE, MICROVM_XHCI_IRQ);
  78. }
  79. }
  80. static void acpi_dsdt_add_pci(Aml *scope, MicrovmMachineState *mms)
  81. {
  82. if (mms->pcie != ON_OFF_AUTO_ON) {
  83. return;
  84. }
  85. acpi_dsdt_add_gpex(scope, &mms->gpex);
  86. }
  87. static void
  88. build_dsdt_microvm(GArray *table_data, BIOSLinker *linker,
  89. MicrovmMachineState *mms)
  90. {
  91. X86MachineState *x86ms = X86_MACHINE(mms);
  92. Aml *dsdt, *sb_scope, *scope, *pkg;
  93. bool ambiguous;
  94. Object *isabus;
  95. AcpiTable table = { .sig = "DSDT", .rev = 2, .oem_id = x86ms->oem_id,
  96. .oem_table_id = x86ms->oem_table_id };
  97. isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous);
  98. assert(isabus);
  99. assert(!ambiguous);
  100. acpi_table_begin(&table, table_data);
  101. dsdt = init_aml_allocator();
  102. sb_scope = aml_scope("_SB");
  103. fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg);
  104. qbus_build_aml(BUS(isabus), sb_scope);
  105. build_ged_aml(sb_scope, GED_DEVICE, x86ms->acpi_dev,
  106. GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE);
  107. acpi_dsdt_add_power_button(sb_scope);
  108. acpi_dsdt_add_virtio(sb_scope, mms);
  109. acpi_dsdt_add_xhci(sb_scope, mms);
  110. acpi_dsdt_add_pci(sb_scope, mms);
  111. aml_append(dsdt, sb_scope);
  112. /* ACPI 5.0: Table 7-209 System State Package */
  113. scope = aml_scope("\\");
  114. pkg = aml_package(4);
  115. aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5));
  116. aml_append(pkg, aml_int(0)); /* ignored */
  117. aml_append(pkg, aml_int(0)); /* reserved */
  118. aml_append(pkg, aml_int(0)); /* reserved */
  119. aml_append(scope, aml_name_decl("_S5", pkg));
  120. aml_append(dsdt, scope);
  121. /* copy AML bytecode into ACPI tables blob */
  122. g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
  123. acpi_table_end(linker, &table);
  124. free_aml_allocator();
  125. }
  126. static void acpi_build_microvm(AcpiBuildTables *tables,
  127. MicrovmMachineState *mms)
  128. {
  129. MachineState *machine = MACHINE(mms);
  130. X86MachineState *x86ms = X86_MACHINE(mms);
  131. GArray *table_offsets;
  132. GArray *tables_blob = tables->table_data;
  133. unsigned dsdt, xsdt;
  134. AcpiFadtData pmfadt = {
  135. /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */
  136. .rev = 5,
  137. .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) |
  138. (1 << ACPI_FADT_F_RESET_REG_SUP)),
  139. /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */
  140. .sleep_ctl = {
  141. .space_id = AML_AS_SYSTEM_MEMORY,
  142. .bit_width = 8,
  143. .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL,
  144. },
  145. .sleep_sts = {
  146. .space_id = AML_AS_SYSTEM_MEMORY,
  147. .bit_width = 8,
  148. .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS,
  149. },
  150. /* ACPI 5.0: 4.8.3.6 Reset Register */
  151. .reset_reg = {
  152. .space_id = AML_AS_SYSTEM_MEMORY,
  153. .bit_width = 8,
  154. .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET,
  155. },
  156. .reset_val = ACPI_GED_RESET_VALUE,
  157. /*
  158. * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
  159. * Flags, bit offset 1 - 8042.
  160. */
  161. .iapc_boot_arch = iapc_boot_arch_8042(),
  162. };
  163. table_offsets = g_array_new(false, true /* clear */,
  164. sizeof(uint32_t));
  165. bios_linker_loader_alloc(tables->linker,
  166. ACPI_BUILD_TABLE_FILE, tables_blob,
  167. 64 /* Ensure FACS is aligned */,
  168. false /* high memory */);
  169. dsdt = tables_blob->len;
  170. build_dsdt_microvm(tables_blob, tables->linker, mms);
  171. pmfadt.dsdt_tbl_offset = &dsdt;
  172. pmfadt.xdsdt_tbl_offset = &dsdt;
  173. acpi_add_table(table_offsets, tables_blob);
  174. build_fadt(tables_blob, tables->linker, &pmfadt, x86ms->oem_id,
  175. x86ms->oem_table_id);
  176. acpi_add_table(table_offsets, tables_blob);
  177. acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine),
  178. x86ms->oem_id, x86ms->oem_table_id);
  179. #ifdef CONFIG_ACPI_ERST
  180. {
  181. Object *erst_dev;
  182. erst_dev = find_erst_dev();
  183. if (erst_dev) {
  184. acpi_add_table(table_offsets, tables_blob);
  185. build_erst(tables_blob, tables->linker, erst_dev,
  186. x86ms->oem_id, x86ms->oem_table_id);
  187. }
  188. }
  189. #endif
  190. xsdt = tables_blob->len;
  191. build_xsdt(tables_blob, tables->linker, table_offsets, x86ms->oem_id,
  192. x86ms->oem_table_id);
  193. /* RSDP is in FSEG memory, so allocate it separately */
  194. {
  195. AcpiRsdpData rsdp_data = {
  196. /* ACPI 2.0: 5.2.4.3 RSDP Structure */
  197. .revision = 2, /* xsdt needs v2 */
  198. .oem_id = x86ms->oem_id,
  199. .xsdt_tbl_offset = &xsdt,
  200. .rsdt_tbl_offset = NULL,
  201. };
  202. build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
  203. }
  204. /* Cleanup memory that's no longer used. */
  205. g_array_free(table_offsets, true);
  206. }
  207. static void acpi_build_no_update(void *build_opaque)
  208. {
  209. /* nothing, microvm tables don't change at runtime */
  210. }
  211. void acpi_setup_microvm(MicrovmMachineState *mms)
  212. {
  213. X86MachineState *x86ms = X86_MACHINE(mms);
  214. AcpiBuildTables tables;
  215. assert(x86ms->fw_cfg);
  216. if (!x86_machine_is_acpi_enabled(x86ms)) {
  217. return;
  218. }
  219. acpi_build_tables_init(&tables);
  220. acpi_build_microvm(&tables, mms);
  221. /* Now expose it all to Guest */
  222. acpi_add_rom_blob(acpi_build_no_update, NULL, tables.table_data,
  223. ACPI_BUILD_TABLE_FILE);
  224. acpi_add_rom_blob(acpi_build_no_update, NULL, tables.linker->cmd_blob,
  225. ACPI_BUILD_LOADER_FILE);
  226. acpi_add_rom_blob(acpi_build_no_update, NULL, tables.rsdp,
  227. ACPI_BUILD_RSDP_FILE);
  228. acpi_build_tables_cleanup(&tables, false);
  229. }