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 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 #include "qemu/osdep.h" 24 #include "qemu/cutils.h" 25 #include "qapi/error.h" 26 27 #include "exec/memory.h" 28 #include "hw/acpi/acpi.h" 29 #include "hw/acpi/aml-build.h" 30 #include "hw/acpi/bios-linker-loader.h" 31 #include "hw/acpi/generic_event_device.h" 32 #include "hw/acpi/utils.h" 33 #include "hw/acpi/erst.h" 34 #include "hw/i386/fw_cfg.h" 35 #include "hw/i386/microvm.h" 36 #include "hw/pci/pci.h" 37 #include "hw/pci/pcie_host.h" 38 #include "hw/usb/xhci.h" 39 #include "hw/virtio/virtio-mmio.h" 40 41 #include "acpi-common.h" 42 #include "acpi-microvm.h" 43 44 #include CONFIG_DEVICES 45 46 static void acpi_dsdt_add_virtio(Aml *scope, 47 MicrovmMachineState *mms) 48 { 49 gchar *separator; 50 long int index; 51 BusState *bus; 52 BusChild *kid; 53 54 bus = sysbus_get_default(); 55 QTAILQ_FOREACH(kid, &bus->children, sibling) { 56 DeviceState *dev = kid->child; 57 Object *obj = object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MMIO); 58 59 if (obj) { 60 VirtIOMMIOProxy *mmio = VIRTIO_MMIO(obj); 61 VirtioBusState *mmio_virtio_bus = &mmio->bus; 62 BusState *mmio_bus = &mmio_virtio_bus->parent_obj; 63 64 if (QTAILQ_EMPTY(&mmio_bus->children)) { 65 continue; 66 } 67 separator = g_strrstr(mmio_bus->name, "."); 68 if (!separator) { 69 continue; 70 } 71 if (qemu_strtol(separator + 1, NULL, 10, &index) != 0) { 72 continue; 73 } 74 75 uint32_t irq = mms->virtio_irq_base + index; 76 hwaddr base = VIRTIO_MMIO_BASE + index * 512; 77 hwaddr size = 512; 78 79 Aml *dev = aml_device("VR%02u", (unsigned)index); 80 aml_append(dev, aml_name_decl("_HID", aml_string("LNRO0005"))); 81 aml_append(dev, aml_name_decl("_UID", aml_int(index))); 82 aml_append(dev, aml_name_decl("_CCA", aml_int(1))); 83 84 Aml *crs = aml_resource_template(); 85 aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE)); 86 aml_append(crs, 87 aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, 88 AML_EXCLUSIVE, &irq, 1)); 89 aml_append(dev, aml_name_decl("_CRS", crs)); 90 aml_append(scope, dev); 91 } 92 } 93 } 94 95 static void acpi_dsdt_add_xhci(Aml *scope, MicrovmMachineState *mms) 96 { 97 if (machine_usb(MACHINE(mms))) { 98 xhci_sysbus_build_aml(scope, MICROVM_XHCI_BASE, MICROVM_XHCI_IRQ); 99 } 100 } 101 102 static void acpi_dsdt_add_pci(Aml *scope, MicrovmMachineState *mms) 103 { 104 if (mms->pcie != ON_OFF_AUTO_ON) { 105 return; 106 } 107 108 acpi_dsdt_add_gpex(scope, &mms->gpex); 109 } 110 111 static void 112 build_dsdt_microvm(GArray *table_data, BIOSLinker *linker, 113 MicrovmMachineState *mms) 114 { 115 X86MachineState *x86ms = X86_MACHINE(mms); 116 Aml *dsdt, *sb_scope, *scope, *pkg; 117 bool ambiguous; 118 Object *isabus; 119 AcpiTable table = { .sig = "DSDT", .rev = 2, .oem_id = x86ms->oem_id, 120 .oem_table_id = x86ms->oem_table_id }; 121 122 isabus = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous); 123 assert(isabus); 124 assert(!ambiguous); 125 126 acpi_table_begin(&table, table_data); 127 dsdt = init_aml_allocator(); 128 129 sb_scope = aml_scope("_SB"); 130 fw_cfg_add_acpi_dsdt(sb_scope, x86ms->fw_cfg); 131 isa_build_aml(ISA_BUS(isabus), sb_scope); 132 build_ged_aml(sb_scope, GED_DEVICE, x86ms->acpi_dev, 133 GED_MMIO_IRQ, AML_SYSTEM_MEMORY, GED_MMIO_BASE); 134 acpi_dsdt_add_power_button(sb_scope); 135 acpi_dsdt_add_virtio(sb_scope, mms); 136 acpi_dsdt_add_xhci(sb_scope, mms); 137 acpi_dsdt_add_pci(sb_scope, mms); 138 aml_append(dsdt, sb_scope); 139 140 /* ACPI 5.0: Table 7-209 System State Package */ 141 scope = aml_scope("\\"); 142 pkg = aml_package(4); 143 aml_append(pkg, aml_int(ACPI_GED_SLP_TYP_S5)); 144 aml_append(pkg, aml_int(0)); /* ignored */ 145 aml_append(pkg, aml_int(0)); /* reserved */ 146 aml_append(pkg, aml_int(0)); /* reserved */ 147 aml_append(scope, aml_name_decl("_S5", pkg)); 148 aml_append(dsdt, scope); 149 150 /* copy AML bytecode into ACPI tables blob */ 151 g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len); 152 153 acpi_table_end(linker, &table); 154 free_aml_allocator(); 155 } 156 157 static void acpi_build_microvm(AcpiBuildTables *tables, 158 MicrovmMachineState *mms) 159 { 160 MachineState *machine = MACHINE(mms); 161 X86MachineState *x86ms = X86_MACHINE(mms); 162 GArray *table_offsets; 163 GArray *tables_blob = tables->table_data; 164 unsigned dsdt, xsdt; 165 AcpiFadtData pmfadt = { 166 /* ACPI 5.0: 4.1 Hardware-Reduced ACPI */ 167 .rev = 5, 168 .flags = ((1 << ACPI_FADT_F_HW_REDUCED_ACPI) | 169 (1 << ACPI_FADT_F_RESET_REG_SUP)), 170 171 /* ACPI 5.0: 4.8.3.7 Sleep Control and Status Registers */ 172 .sleep_ctl = { 173 .space_id = AML_AS_SYSTEM_MEMORY, 174 .bit_width = 8, 175 .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_CTL, 176 }, 177 .sleep_sts = { 178 .space_id = AML_AS_SYSTEM_MEMORY, 179 .bit_width = 8, 180 .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_SLEEP_STS, 181 }, 182 183 /* ACPI 5.0: 4.8.3.6 Reset Register */ 184 .reset_reg = { 185 .space_id = AML_AS_SYSTEM_MEMORY, 186 .bit_width = 8, 187 .address = GED_MMIO_BASE_REGS + ACPI_GED_REG_RESET, 188 }, 189 .reset_val = ACPI_GED_RESET_VALUE, 190 }; 191 192 table_offsets = g_array_new(false, true /* clear */, 193 sizeof(uint32_t)); 194 bios_linker_loader_alloc(tables->linker, 195 ACPI_BUILD_TABLE_FILE, tables_blob, 196 64 /* Ensure FACS is aligned */, 197 false /* high memory */); 198 199 dsdt = tables_blob->len; 200 build_dsdt_microvm(tables_blob, tables->linker, mms); 201 202 pmfadt.dsdt_tbl_offset = &dsdt; 203 pmfadt.xdsdt_tbl_offset = &dsdt; 204 acpi_add_table(table_offsets, tables_blob); 205 build_fadt(tables_blob, tables->linker, &pmfadt, x86ms->oem_id, 206 x86ms->oem_table_id); 207 208 acpi_add_table(table_offsets, tables_blob); 209 acpi_build_madt(tables_blob, tables->linker, X86_MACHINE(machine), 210 ACPI_DEVICE_IF(x86ms->acpi_dev), x86ms->oem_id, 211 x86ms->oem_table_id); 212 213 #ifdef CONFIG_ACPI_ERST 214 { 215 Object *erst_dev; 216 erst_dev = find_erst_dev(); 217 if (erst_dev) { 218 acpi_add_table(table_offsets, tables_blob); 219 build_erst(tables_blob, tables->linker, erst_dev, 220 x86ms->oem_id, x86ms->oem_table_id); 221 } 222 } 223 #endif 224 225 xsdt = tables_blob->len; 226 build_xsdt(tables_blob, tables->linker, table_offsets, x86ms->oem_id, 227 x86ms->oem_table_id); 228 229 /* RSDP is in FSEG memory, so allocate it separately */ 230 { 231 AcpiRsdpData rsdp_data = { 232 /* ACPI 2.0: 5.2.4.3 RSDP Structure */ 233 .revision = 2, /* xsdt needs v2 */ 234 .oem_id = x86ms->oem_id, 235 .xsdt_tbl_offset = &xsdt, 236 .rsdt_tbl_offset = NULL, 237 }; 238 build_rsdp(tables->rsdp, tables->linker, &rsdp_data); 239 } 240 241 /* Cleanup memory that's no longer used. */ 242 g_array_free(table_offsets, true); 243 } 244 245 static void acpi_build_no_update(void *build_opaque) 246 { 247 /* nothing, microvm tables don't change at runtime */ 248 } 249 250 void acpi_setup_microvm(MicrovmMachineState *mms) 251 { 252 X86MachineState *x86ms = X86_MACHINE(mms); 253 AcpiBuildTables tables; 254 255 assert(x86ms->fw_cfg); 256 257 if (!x86_machine_is_acpi_enabled(x86ms)) { 258 return; 259 } 260 261 acpi_build_tables_init(&tables); 262 acpi_build_microvm(&tables, mms); 263 264 /* Now expose it all to Guest */ 265 acpi_add_rom_blob(acpi_build_no_update, NULL, tables.table_data, 266 ACPI_BUILD_TABLE_FILE); 267 acpi_add_rom_blob(acpi_build_no_update, NULL, tables.linker->cmd_blob, 268 ACPI_BUILD_LOADER_FILE); 269 acpi_add_rom_blob(acpi_build_no_update, NULL, tables.rsdp, 270 ACPI_BUILD_RSDP_FILE); 271 272 acpi_build_tables_cleanup(&tables, false); 273 } 274