Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 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 "acpi-build.h" |
| 24 | #include <stddef.h> |
| 25 | #include <glib.h> |
| 26 | #include "qemu-common.h" |
| 27 | #include "qemu/bitmap.h" |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 28 | #include "qemu/osdep.h" |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 29 | #include "qemu/error-report.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 30 | #include "hw/pci/pci.h" |
| 31 | #include "qom/cpu.h" |
| 32 | #include "hw/i386/pc.h" |
| 33 | #include "target-i386/cpu.h" |
| 34 | #include "hw/timer/hpet.h" |
Shannon Zhao | 395e5fb | 2015-04-03 18:03:33 +0800 | [diff] [blame] | 35 | #include "hw/acpi/acpi-defs.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 36 | #include "hw/acpi/acpi.h" |
| 37 | #include "hw/nvram/fw_cfg.h" |
Michael S. Tsirkin | 0058ae1 | 2015-01-19 23:58:55 +0200 | [diff] [blame] | 38 | #include "hw/acpi/bios-linker-loader.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 39 | #include "hw/loader.h" |
Gabriel L. Somlo | 15bce1b | 2013-12-22 10:34:56 -0500 | [diff] [blame] | 40 | #include "hw/isa/isa.h" |
Igor Mammedov | bef3492 | 2014-06-02 15:25:26 +0200 | [diff] [blame] | 41 | #include "hw/acpi/memory_hotplug.h" |
Xiao Guangrong | 87252e1 | 2015-12-02 15:20:58 +0800 | [diff] [blame] | 42 | #include "hw/mem/nvdimm.h" |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 43 | #include "sysemu/tpm.h" |
| 44 | #include "hw/acpi/tpm.h" |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 45 | #include "sysemu/tpm_backend.h" |
Laszlo Ersek | f070efa | 2015-12-10 18:25:34 +0100 | [diff] [blame] | 46 | #include "hw/timer/mc146818rtc_regs.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 47 | |
| 48 | /* Supported chipsets: */ |
| 49 | #include "hw/acpi/piix4.h" |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 50 | #include "hw/acpi/pcihp.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 51 | #include "hw/i386/ich9.h" |
| 52 | #include "hw/pci/pci_bus.h" |
| 53 | #include "hw/pci-host/q35.h" |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 54 | #include "hw/i386/intel_iommu.h" |
Igor Mammedov | a57d708 | 2015-12-28 18:02:29 +0100 | [diff] [blame] | 55 | #include "hw/timer/hpet.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 56 | |
| 57 | #include "hw/i386/q35-acpi-dsdt.hex" |
| 58 | #include "hw/i386/acpi-dsdt.hex" |
| 59 | |
Igor Mammedov | 19934e0 | 2015-01-30 13:29:36 +0000 | [diff] [blame] | 60 | #include "hw/acpi/aml-build.h" |
| 61 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 62 | #include "qapi/qmp/qint.h" |
| 63 | #include "qom/qom-qobject.h" |
| 64 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 65 | /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and |
| 66 | * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows |
| 67 | * a little bit, there should be plenty of free space since the DSDT |
| 68 | * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1. |
| 69 | */ |
| 70 | #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97 |
| 71 | #define ACPI_BUILD_ALIGN_SIZE 0x1000 |
| 72 | |
Michael S. Tsirkin | 868270f | 2014-07-28 23:07:11 +0200 | [diff] [blame] | 73 | #define ACPI_BUILD_TABLE_SIZE 0x20000 |
Paolo Bonzini | 18045fb | 2014-07-28 17:34:16 +0200 | [diff] [blame] | 74 | |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 75 | /* #define DEBUG_ACPI_BUILD */ |
| 76 | #ifdef DEBUG_ACPI_BUILD |
| 77 | #define ACPI_BUILD_DPRINTF(fmt, ...) \ |
| 78 | do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0) |
| 79 | #else |
| 80 | #define ACPI_BUILD_DPRINTF(fmt, ...) |
| 81 | #endif |
| 82 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 83 | typedef struct AcpiCpuInfo { |
Eduardo Habkost | 798325e | 2014-03-14 16:33:53 -0300 | [diff] [blame] | 84 | DECLARE_BITMAP(found_cpus, ACPI_CPU_HOTPLUG_ID_LIMIT); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 85 | } AcpiCpuInfo; |
| 86 | |
| 87 | typedef struct AcpiMcfgInfo { |
| 88 | uint64_t mcfg_base; |
| 89 | uint32_t mcfg_size; |
| 90 | } AcpiMcfgInfo; |
| 91 | |
| 92 | typedef struct AcpiPmInfo { |
| 93 | bool s3_disabled; |
| 94 | bool s4_disabled; |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 95 | bool pcihp_bridge_en; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 96 | uint8_t s4_val; |
| 97 | uint16_t sci_int; |
| 98 | uint8_t acpi_enable_cmd; |
| 99 | uint8_t acpi_disable_cmd; |
| 100 | uint32_t gpe0_blk; |
| 101 | uint32_t gpe0_blk_len; |
| 102 | uint32_t io_base; |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 103 | uint16_t cpu_hp_io_base; |
| 104 | uint16_t cpu_hp_io_len; |
Igor Mammedov | 2c6b94d | 2015-02-18 19:14:47 +0000 | [diff] [blame] | 105 | uint16_t mem_hp_io_base; |
| 106 | uint16_t mem_hp_io_len; |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 107 | uint16_t pcihp_io_base; |
| 108 | uint16_t pcihp_io_len; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 109 | } AcpiPmInfo; |
| 110 | |
| 111 | typedef struct AcpiMiscInfo { |
| 112 | bool has_hpet; |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 113 | TPMVersion tpm_version; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 114 | const unsigned char *dsdt_code; |
| 115 | unsigned dsdt_size; |
| 116 | uint16_t pvpanic_port; |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 117 | uint16_t applesmc_io_base; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 118 | } AcpiMiscInfo; |
| 119 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 120 | typedef struct AcpiBuildPciBusHotplugState { |
| 121 | GArray *device_table; |
| 122 | GArray *notify_table; |
| 123 | struct AcpiBuildPciBusHotplugState *parent; |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 124 | bool pcihp_bridge_en; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 125 | } AcpiBuildPciBusHotplugState; |
| 126 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 127 | static void acpi_get_dsdt(AcpiMiscInfo *info) |
| 128 | { |
| 129 | Object *piix = piix4_pm_find(); |
| 130 | Object *lpc = ich9_lpc_find(); |
| 131 | assert(!!piix != !!lpc); |
| 132 | |
| 133 | if (piix) { |
| 134 | info->dsdt_code = AcpiDsdtAmlCode; |
| 135 | info->dsdt_size = sizeof AcpiDsdtAmlCode; |
| 136 | } |
| 137 | if (lpc) { |
| 138 | info->dsdt_code = Q35AcpiDsdtAmlCode; |
| 139 | info->dsdt_size = sizeof Q35AcpiDsdtAmlCode; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static |
| 144 | int acpi_add_cpu_info(Object *o, void *opaque) |
| 145 | { |
| 146 | AcpiCpuInfo *cpu = opaque; |
| 147 | uint64_t apic_id; |
| 148 | |
| 149 | if (object_dynamic_cast(o, TYPE_CPU)) { |
| 150 | apic_id = object_property_get_int(o, "apic-id", NULL); |
Eduardo Habkost | 798325e | 2014-03-14 16:33:53 -0300 | [diff] [blame] | 151 | assert(apic_id < ACPI_CPU_HOTPLUG_ID_LIMIT); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 152 | |
| 153 | set_bit(apic_id, cpu->found_cpus); |
| 154 | } |
| 155 | |
| 156 | object_child_foreach(o, acpi_add_cpu_info, opaque); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void acpi_get_cpu_info(AcpiCpuInfo *cpu) |
| 161 | { |
| 162 | Object *root = object_get_root(); |
| 163 | |
| 164 | memset(cpu->found_cpus, 0, sizeof cpu->found_cpus); |
| 165 | object_child_foreach(root, acpi_add_cpu_info, cpu); |
| 166 | } |
| 167 | |
| 168 | static void acpi_get_pm_info(AcpiPmInfo *pm) |
| 169 | { |
| 170 | Object *piix = piix4_pm_find(); |
| 171 | Object *lpc = ich9_lpc_find(); |
| 172 | Object *obj = NULL; |
| 173 | QObject *o; |
| 174 | |
Daniel P. Berrange | 94aaca6 | 2015-07-31 11:14:35 +0100 | [diff] [blame] | 175 | pm->cpu_hp_io_base = 0; |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 176 | pm->pcihp_io_base = 0; |
| 177 | pm->pcihp_io_len = 0; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 178 | if (piix) { |
| 179 | obj = piix; |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 180 | pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE; |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 181 | pm->pcihp_io_base = |
| 182 | object_property_get_int(obj, ACPI_PCIHP_IO_BASE_PROP, NULL); |
| 183 | pm->pcihp_io_len = |
| 184 | object_property_get_int(obj, ACPI_PCIHP_IO_LEN_PROP, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 185 | } |
| 186 | if (lpc) { |
| 187 | obj = lpc; |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 188 | pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 189 | } |
| 190 | assert(obj); |
| 191 | |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 192 | pm->cpu_hp_io_len = ACPI_GPE_PROC_LEN; |
Igor Mammedov | 2c6b94d | 2015-02-18 19:14:47 +0000 | [diff] [blame] | 193 | pm->mem_hp_io_base = ACPI_MEMORY_HOTPLUG_BASE; |
| 194 | pm->mem_hp_io_len = ACPI_MEMORY_HOTPLUG_IO_LEN; |
| 195 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 196 | /* Fill in optional s3/s4 related properties */ |
| 197 | o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL); |
| 198 | if (o) { |
| 199 | pm->s3_disabled = qint_get_int(qobject_to_qint(o)); |
| 200 | } else { |
| 201 | pm->s3_disabled = false; |
| 202 | } |
Kirill Batuzov | 097a97a | 2014-04-24 18:15:57 +0400 | [diff] [blame] | 203 | qobject_decref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 204 | o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL); |
| 205 | if (o) { |
| 206 | pm->s4_disabled = qint_get_int(qobject_to_qint(o)); |
| 207 | } else { |
| 208 | pm->s4_disabled = false; |
| 209 | } |
Kirill Batuzov | 097a97a | 2014-04-24 18:15:57 +0400 | [diff] [blame] | 210 | qobject_decref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 211 | o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL); |
| 212 | if (o) { |
| 213 | pm->s4_val = qint_get_int(qobject_to_qint(o)); |
| 214 | } else { |
| 215 | pm->s4_val = false; |
| 216 | } |
Kirill Batuzov | 097a97a | 2014-04-24 18:15:57 +0400 | [diff] [blame] | 217 | qobject_decref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 218 | |
| 219 | /* Fill in mandatory properties */ |
| 220 | pm->sci_int = object_property_get_int(obj, ACPI_PM_PROP_SCI_INT, NULL); |
| 221 | |
| 222 | pm->acpi_enable_cmd = object_property_get_int(obj, |
| 223 | ACPI_PM_PROP_ACPI_ENABLE_CMD, |
| 224 | NULL); |
| 225 | pm->acpi_disable_cmd = object_property_get_int(obj, |
| 226 | ACPI_PM_PROP_ACPI_DISABLE_CMD, |
| 227 | NULL); |
| 228 | pm->io_base = object_property_get_int(obj, ACPI_PM_PROP_PM_IO_BASE, |
| 229 | NULL); |
| 230 | pm->gpe0_blk = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK, |
| 231 | NULL); |
| 232 | pm->gpe0_blk_len = object_property_get_int(obj, ACPI_PM_PROP_GPE0_BLK_LEN, |
| 233 | NULL); |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 234 | pm->pcihp_bridge_en = |
| 235 | object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support", |
| 236 | NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 237 | } |
| 238 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 239 | static void acpi_get_misc_info(AcpiMiscInfo *info) |
| 240 | { |
| 241 | info->has_hpet = hpet_find(); |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 242 | info->tpm_version = tpm_get_version(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 243 | info->pvpanic_port = pvpanic_port(); |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 244 | info->applesmc_io_base = applesmc_port(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 245 | } |
| 246 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 247 | /* |
| 248 | * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE. |
| 249 | * On i386 arch we only have two pci hosts, so we can look only for them. |
| 250 | */ |
| 251 | static Object *acpi_get_i386_pci_host(void) |
| 252 | { |
| 253 | PCIHostState *host; |
| 254 | |
| 255 | host = OBJECT_CHECK(PCIHostState, |
| 256 | object_resolve_path("/machine/i440fx", NULL), |
| 257 | TYPE_PCI_HOST_BRIDGE); |
| 258 | if (!host) { |
| 259 | host = OBJECT_CHECK(PCIHostState, |
| 260 | object_resolve_path("/machine/q35", NULL), |
| 261 | TYPE_PCI_HOST_BRIDGE); |
| 262 | } |
| 263 | |
| 264 | return OBJECT(host); |
| 265 | } |
| 266 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 267 | static void acpi_get_pci_info(PcPciInfo *info) |
| 268 | { |
| 269 | Object *pci_host; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 270 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 271 | |
| 272 | pci_host = acpi_get_i386_pci_host(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 273 | g_assert(pci_host); |
| 274 | |
| 275 | info->w32.begin = object_property_get_int(pci_host, |
| 276 | PCI_HOST_PROP_PCI_HOLE_START, |
| 277 | NULL); |
| 278 | info->w32.end = object_property_get_int(pci_host, |
| 279 | PCI_HOST_PROP_PCI_HOLE_END, |
| 280 | NULL); |
| 281 | info->w64.begin = object_property_get_int(pci_host, |
| 282 | PCI_HOST_PROP_PCI_HOLE64_START, |
| 283 | NULL); |
| 284 | info->w64.end = object_property_get_int(pci_host, |
| 285 | PCI_HOST_PROP_PCI_HOLE64_END, |
| 286 | NULL); |
| 287 | } |
| 288 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 289 | #define ACPI_PORT_SMI_CMD 0x00b2 /* TODO: this is APM_CNT_IOPORT */ |
| 290 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 291 | static void acpi_align_size(GArray *blob, unsigned align) |
| 292 | { |
| 293 | /* Align size to multiple of given size. This reduces the chance |
| 294 | * we need to change size in the future (breaking cross version migration). |
| 295 | */ |
Michael S. Tsirkin | 134d42d | 2013-11-26 00:00:39 +0200 | [diff] [blame] | 296 | g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 297 | } |
| 298 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 299 | /* FACS */ |
| 300 | static void |
| 301 | build_facs(GArray *table_data, GArray *linker, PcGuestInfo *guest_info) |
| 302 | { |
| 303 | AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs); |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 304 | memcpy(&facs->signature, "FACS", 4); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 305 | facs->length = cpu_to_le32(sizeof(*facs)); |
| 306 | } |
| 307 | |
| 308 | /* Load chipset information in FADT */ |
| 309 | static void fadt_setup(AcpiFadtDescriptorRev1 *fadt, AcpiPmInfo *pm) |
| 310 | { |
| 311 | fadt->model = 1; |
| 312 | fadt->reserved1 = 0; |
| 313 | fadt->sci_int = cpu_to_le16(pm->sci_int); |
| 314 | fadt->smi_cmd = cpu_to_le32(ACPI_PORT_SMI_CMD); |
| 315 | fadt->acpi_enable = pm->acpi_enable_cmd; |
| 316 | fadt->acpi_disable = pm->acpi_disable_cmd; |
| 317 | /* EVT, CNT, TMR offset matches hw/acpi/core.c */ |
| 318 | fadt->pm1a_evt_blk = cpu_to_le32(pm->io_base); |
| 319 | fadt->pm1a_cnt_blk = cpu_to_le32(pm->io_base + 0x04); |
| 320 | fadt->pm_tmr_blk = cpu_to_le32(pm->io_base + 0x08); |
| 321 | fadt->gpe0_blk = cpu_to_le32(pm->gpe0_blk); |
| 322 | /* EVT, CNT, TMR length matches hw/acpi/core.c */ |
| 323 | fadt->pm1_evt_len = 4; |
| 324 | fadt->pm1_cnt_len = 2; |
| 325 | fadt->pm_tmr_len = 4; |
| 326 | fadt->gpe0_blk_len = pm->gpe0_blk_len; |
| 327 | fadt->plvl2_lat = cpu_to_le16(0xfff); /* C2 state not supported */ |
| 328 | fadt->plvl3_lat = cpu_to_le16(0xfff); /* C3 state not supported */ |
| 329 | fadt->flags = cpu_to_le32((1 << ACPI_FADT_F_WBINVD) | |
| 330 | (1 << ACPI_FADT_F_PROC_C1) | |
| 331 | (1 << ACPI_FADT_F_SLP_BUTTON) | |
| 332 | (1 << ACPI_FADT_F_RTC_S4)); |
| 333 | fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_USE_PLATFORM_CLOCK); |
zhanghailiang | 07b81ed | 2014-08-29 11:52:51 +0800 | [diff] [blame] | 334 | /* APIC destination mode ("Flat Logical") has an upper limit of 8 CPUs |
| 335 | * For more than 8 CPUs, "Clustered Logical" mode has to be used |
| 336 | */ |
| 337 | if (max_cpus > 8) { |
| 338 | fadt->flags |= cpu_to_le32(1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL); |
| 339 | } |
Laszlo Ersek | f070efa | 2015-12-10 18:25:34 +0100 | [diff] [blame] | 340 | fadt->century = RTC_CENTURY; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | /* FADT */ |
| 345 | static void |
| 346 | build_fadt(GArray *table_data, GArray *linker, AcpiPmInfo *pm, |
| 347 | unsigned facs, unsigned dsdt) |
| 348 | { |
| 349 | AcpiFadtDescriptorRev1 *fadt = acpi_data_push(table_data, sizeof(*fadt)); |
| 350 | |
| 351 | fadt->firmware_ctrl = cpu_to_le32(facs); |
| 352 | /* FACS address to be filled by Guest linker */ |
| 353 | bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, |
| 354 | ACPI_BUILD_TABLE_FILE, |
| 355 | table_data, &fadt->firmware_ctrl, |
| 356 | sizeof fadt->firmware_ctrl); |
| 357 | |
| 358 | fadt->dsdt = cpu_to_le32(dsdt); |
| 359 | /* DSDT address to be filled by Guest linker */ |
| 360 | bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, |
| 361 | ACPI_BUILD_TABLE_FILE, |
| 362 | table_data, &fadt->dsdt, |
| 363 | sizeof fadt->dsdt); |
| 364 | |
| 365 | fadt_setup(fadt, pm); |
| 366 | |
| 367 | build_header(linker, table_data, |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 368 | (void *)fadt, "FACP", sizeof(*fadt), 1, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static void |
| 372 | build_madt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu, |
| 373 | PcGuestInfo *guest_info) |
| 374 | { |
| 375 | int madt_start = table_data->len; |
| 376 | |
| 377 | AcpiMultipleApicTable *madt; |
| 378 | AcpiMadtIoApic *io_apic; |
| 379 | AcpiMadtIntsrcovr *intsrcovr; |
| 380 | AcpiMadtLocalNmi *local_nmi; |
| 381 | int i; |
| 382 | |
| 383 | madt = acpi_data_push(table_data, sizeof *madt); |
| 384 | madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS); |
| 385 | madt->flags = cpu_to_le32(1); |
| 386 | |
| 387 | for (i = 0; i < guest_info->apic_id_limit; i++) { |
| 388 | AcpiMadtProcessorApic *apic = acpi_data_push(table_data, sizeof *apic); |
| 389 | apic->type = ACPI_APIC_PROCESSOR; |
| 390 | apic->length = sizeof(*apic); |
| 391 | apic->processor_id = i; |
| 392 | apic->local_apic_id = i; |
| 393 | if (test_bit(i, cpu->found_cpus)) { |
| 394 | apic->flags = cpu_to_le32(1); |
| 395 | } else { |
| 396 | apic->flags = cpu_to_le32(0); |
| 397 | } |
| 398 | } |
| 399 | io_apic = acpi_data_push(table_data, sizeof *io_apic); |
| 400 | io_apic->type = ACPI_APIC_IO; |
| 401 | io_apic->length = sizeof(*io_apic); |
| 402 | #define ACPI_BUILD_IOAPIC_ID 0x0 |
| 403 | io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID; |
| 404 | io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS); |
| 405 | io_apic->interrupt = cpu_to_le32(0); |
| 406 | |
| 407 | if (guest_info->apic_xrupt_override) { |
| 408 | intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr); |
| 409 | intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE; |
| 410 | intsrcovr->length = sizeof(*intsrcovr); |
| 411 | intsrcovr->source = 0; |
| 412 | intsrcovr->gsi = cpu_to_le32(2); |
| 413 | intsrcovr->flags = cpu_to_le16(0); /* conforms to bus specifications */ |
| 414 | } |
| 415 | for (i = 1; i < 16; i++) { |
| 416 | #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11)) |
| 417 | if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) { |
| 418 | /* No need for a INT source override structure. */ |
| 419 | continue; |
| 420 | } |
| 421 | intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr); |
| 422 | intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE; |
| 423 | intsrcovr->length = sizeof(*intsrcovr); |
| 424 | intsrcovr->source = i; |
| 425 | intsrcovr->gsi = cpu_to_le32(i); |
| 426 | intsrcovr->flags = cpu_to_le16(0xd); /* active high, level triggered */ |
| 427 | } |
| 428 | |
| 429 | local_nmi = acpi_data_push(table_data, sizeof *local_nmi); |
| 430 | local_nmi->type = ACPI_APIC_LOCAL_NMI; |
| 431 | local_nmi->length = sizeof(*local_nmi); |
| 432 | local_nmi->processor_id = 0xff; /* all processors */ |
| 433 | local_nmi->flags = cpu_to_le16(0); |
| 434 | local_nmi->lint = 1; /* ACPI_LINT1 */ |
| 435 | |
| 436 | build_header(linker, table_data, |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 437 | (void *)(table_data->data + madt_start), "APIC", |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 438 | table_data->len - madt_start, 1, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 439 | } |
| 440 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 441 | /* Assign BSEL property to all buses. In the future, this can be changed |
| 442 | * to only assign to buses that support hotplug. |
| 443 | */ |
| 444 | static void *acpi_set_bsel(PCIBus *bus, void *opaque) |
| 445 | { |
| 446 | unsigned *bsel_alloc = opaque; |
| 447 | unsigned *bus_bsel; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 448 | |
Igor Mammedov | 39b888b | 2014-09-26 09:28:17 +0000 | [diff] [blame] | 449 | if (qbus_is_hotpluggable(BUS(bus))) { |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 450 | bus_bsel = g_malloc(sizeof *bus_bsel); |
| 451 | |
| 452 | *bus_bsel = (*bsel_alloc)++; |
| 453 | object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, |
| 454 | bus_bsel, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 455 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 456 | |
| 457 | return bsel_alloc; |
| 458 | } |
| 459 | |
| 460 | static void acpi_set_pci_info(void) |
| 461 | { |
| 462 | PCIBus *bus = find_i440fx(); /* TODO: Q35 support */ |
| 463 | unsigned bsel_alloc = 0; |
| 464 | |
| 465 | if (bus) { |
| 466 | /* Scan all PCI buses. Set property to enable acpi based hotplug. */ |
| 467 | pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc); |
| 468 | } |
| 469 | } |
| 470 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 471 | static void build_append_pcihp_notify_entry(Aml *method, int slot) |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 472 | { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 473 | Aml *if_ctx; |
| 474 | int32_t devfn = PCI_DEVFN(slot, 0); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 475 | |
Igor Mammedov | 5530427 | 2015-12-10 00:41:17 +0100 | [diff] [blame] | 476 | if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL)); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 477 | aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1))); |
| 478 | aml_append(method, if_ctx); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 479 | } |
| 480 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 481 | static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus, |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 482 | bool pcihp_bridge_en) |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 483 | { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 484 | Aml *dev, *notify_method, *method; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 485 | QObject *bsel; |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 486 | PCIBus *sec; |
| 487 | int i; |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 488 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 489 | bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL); |
| 490 | if (bsel) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 491 | int64_t bsel_val = qint_get_int(qobject_to_qint(bsel)); |
| 492 | |
| 493 | aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val))); |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 494 | notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED); |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 495 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 496 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 497 | for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) { |
| 498 | DeviceClass *dc; |
| 499 | PCIDeviceClass *pc; |
| 500 | PCIDevice *pdev = bus->devices[i]; |
| 501 | int slot = PCI_SLOT(i); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 502 | bool hotplug_enabled_dev; |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 503 | bool bridge_in_acpi; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 504 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 505 | if (!pdev) { |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 506 | if (bsel) { /* add hotplug slots for non present devices */ |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 507 | dev = aml_device("S%.02X", PCI_DEVFN(slot, 0)); |
| 508 | aml_append(dev, aml_name_decl("_SUN", aml_int(slot))); |
| 509 | aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16))); |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 510 | method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 511 | aml_append(method, |
| 512 | aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN")) |
| 513 | ); |
| 514 | aml_append(dev, method); |
| 515 | aml_append(parent_scope, dev); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 516 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 517 | build_append_pcihp_notify_entry(notify_method, slot); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 518 | } |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 519 | continue; |
| 520 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 521 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 522 | pc = PCI_DEVICE_GET_CLASS(pdev); |
| 523 | dc = DEVICE_GET_CLASS(pdev); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 524 | |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 525 | /* When hotplug for bridges is enabled, bridges are |
| 526 | * described in ACPI separately (see build_pci_bus_end). |
| 527 | * In this case they aren't themselves hot-pluggable. |
Michael S. Tsirkin | a20275f | 2015-01-28 18:30:38 +0200 | [diff] [blame] | 528 | * Hotplugged bridges *are* hot-pluggable. |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 529 | */ |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 530 | bridge_in_acpi = pc->is_bridge && pcihp_bridge_en && |
| 531 | !DEVICE(pdev)->hotplugged; |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 532 | |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 533 | hotplug_enabled_dev = bsel && dc->hotpluggable && !bridge_in_acpi; |
| 534 | |
| 535 | if (pc->class_id == PCI_CLASS_BRIDGE_ISA) { |
| 536 | continue; |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 539 | /* start to compose PCI slot descriptor */ |
| 540 | dev = aml_device("S%.02X", PCI_DEVFN(slot, 0)); |
| 541 | aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16))); |
| 542 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 543 | if (pc->class_id == PCI_CLASS_DISPLAY_VGA) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 544 | /* add VGA specific AML methods */ |
| 545 | int s3d; |
| 546 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 547 | if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 548 | s3d = 3; |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 549 | } else { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 550 | s3d = 0; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 551 | } |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 552 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 553 | method = aml_method("_S1D", 0, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 554 | aml_append(method, aml_return(aml_int(0))); |
| 555 | aml_append(dev, method); |
| 556 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 557 | method = aml_method("_S2D", 0, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 558 | aml_append(method, aml_return(aml_int(0))); |
| 559 | aml_append(dev, method); |
| 560 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 561 | method = aml_method("_S3D", 0, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 562 | aml_append(method, aml_return(aml_int(s3d))); |
| 563 | aml_append(dev, method); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 564 | } else if (hotplug_enabled_dev) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 565 | /* add _SUN/_EJ0 to make slot hotpluggable */ |
| 566 | aml_append(dev, aml_name_decl("_SUN", aml_int(slot))); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 567 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 568 | method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 569 | aml_append(method, |
| 570 | aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN")) |
| 571 | ); |
| 572 | aml_append(dev, method); |
| 573 | |
| 574 | if (bsel) { |
| 575 | build_append_pcihp_notify_entry(notify_method, slot); |
| 576 | } |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 577 | } else if (bridge_in_acpi) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 578 | /* |
| 579 | * device is coldplugged bridge, |
| 580 | * add child device descriptions into its scope |
| 581 | */ |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 582 | PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 583 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 584 | build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en); |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 585 | } |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 586 | /* slot descriptor has been composed, add it into parent context */ |
| 587 | aml_append(parent_scope, dev); |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | if (bsel) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 591 | aml_append(parent_scope, notify_method); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* Append PCNT method to notify about events on local and child buses. |
| 595 | * Add unconditionally for root since DSDT expects it. |
| 596 | */ |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 597 | method = aml_method("PCNT", 0, AML_NOTSERIALIZED); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 598 | |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 599 | /* If bus supports hotplug select it and notify about local events */ |
| 600 | if (bsel) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 601 | int64_t bsel_val = qint_get_int(qobject_to_qint(bsel)); |
| 602 | aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM"))); |
| 603 | aml_append(method, |
| 604 | aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */) |
| 605 | ); |
| 606 | aml_append(method, |
| 607 | aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */) |
| 608 | ); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 609 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 610 | |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 611 | /* Notify about child bus events in any case */ |
| 612 | if (pcihp_bridge_en) { |
| 613 | QLIST_FOREACH(sec, &bus->child, sibling) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 614 | int32_t devfn = sec->parent_dev->devfn; |
| 615 | |
| 616 | aml_append(method, aml_name("^S%.02X.PCNT", devfn)); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 617 | } |
| 618 | } |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 619 | aml_append(parent_scope, method); |
Shannon Zhao | d370dfa | 2015-05-26 09:46:07 +0800 | [diff] [blame] | 620 | qobject_decref(bsel); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 621 | } |
| 622 | |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 623 | /* |
| 624 | * initialize_route - Initialize the interrupt routing rule |
| 625 | * through a specific LINK: |
| 626 | * if (lnk_idx == idx) |
| 627 | * route using link 'link_name' |
| 628 | */ |
| 629 | static Aml *initialize_route(Aml *route, const char *link_name, |
| 630 | Aml *lnk_idx, int idx) |
| 631 | { |
| 632 | Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx))); |
| 633 | Aml *pkg = aml_package(4); |
| 634 | |
| 635 | aml_append(pkg, aml_int(0)); |
| 636 | aml_append(pkg, aml_int(0)); |
| 637 | aml_append(pkg, aml_name("%s", link_name)); |
| 638 | aml_append(pkg, aml_int(0)); |
| 639 | aml_append(if_ctx, aml_store(pkg, route)); |
| 640 | |
| 641 | return if_ctx; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * build_prt - Define interrupt rounting rules |
| 646 | * |
| 647 | * Returns an array of 128 routes, one for each device, |
| 648 | * based on device location. |
| 649 | * The main goal is to equaly distribute the interrupts |
| 650 | * over the 4 existing ACPI links (works only for i440fx). |
| 651 | * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]". |
| 652 | * |
| 653 | */ |
| 654 | static Aml *build_prt(void) |
| 655 | { |
| 656 | Aml *method, *while_ctx, *pin, *res; |
| 657 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 658 | method = aml_method("_PRT", 0, AML_NOTSERIALIZED); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 659 | res = aml_local(0); |
| 660 | pin = aml_local(1); |
| 661 | aml_append(method, aml_store(aml_package(128), res)); |
| 662 | aml_append(method, aml_store(aml_int(0), pin)); |
| 663 | |
| 664 | /* while (pin < 128) */ |
| 665 | while_ctx = aml_while(aml_lless(pin, aml_int(128))); |
| 666 | { |
| 667 | Aml *slot = aml_local(2); |
| 668 | Aml *lnk_idx = aml_local(3); |
| 669 | Aml *route = aml_local(4); |
| 670 | |
| 671 | /* slot = pin >> 2 */ |
| 672 | aml_append(while_ctx, |
Igor Mammedov | c360639 | 2015-12-10 00:41:06 +0100 | [diff] [blame] | 673 | aml_store(aml_shiftright(pin, aml_int(2), NULL), slot)); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 674 | /* lnk_idx = (slot + pin) & 3 */ |
| 675 | aml_append(while_ctx, |
Igor Mammedov | 5530427 | 2015-12-10 00:41:17 +0100 | [diff] [blame] | 676 | aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL), |
| 677 | lnk_idx)); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 678 | |
| 679 | /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */ |
| 680 | aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0)); |
| 681 | aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1)); |
| 682 | aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2)); |
| 683 | aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3)); |
| 684 | |
| 685 | /* route[0] = 0x[slot]FFFF */ |
| 686 | aml_append(while_ctx, |
Igor Mammedov | ca3df95 | 2015-12-10 00:41:16 +0100 | [diff] [blame] | 687 | aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF), |
| 688 | NULL), |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 689 | aml_index(route, aml_int(0)))); |
| 690 | /* route[1] = pin & 3 */ |
| 691 | aml_append(while_ctx, |
Igor Mammedov | 5530427 | 2015-12-10 00:41:17 +0100 | [diff] [blame] | 692 | aml_store(aml_and(pin, aml_int(3), NULL), |
| 693 | aml_index(route, aml_int(1)))); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 694 | /* res[pin] = route */ |
| 695 | aml_append(while_ctx, aml_store(route, aml_index(res, pin))); |
| 696 | /* pin++ */ |
| 697 | aml_append(while_ctx, aml_increment(pin)); |
| 698 | } |
| 699 | aml_append(method, while_ctx); |
| 700 | /* return res*/ |
| 701 | aml_append(method, aml_return(res)); |
| 702 | |
| 703 | return method; |
| 704 | } |
| 705 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 706 | typedef struct CrsRangeEntry { |
| 707 | uint64_t base; |
| 708 | uint64_t limit; |
| 709 | } CrsRangeEntry; |
| 710 | |
| 711 | static void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit) |
| 712 | { |
| 713 | CrsRangeEntry *entry; |
| 714 | |
| 715 | entry = g_malloc(sizeof(*entry)); |
| 716 | entry->base = base; |
| 717 | entry->limit = limit; |
| 718 | |
| 719 | g_ptr_array_add(ranges, entry); |
| 720 | } |
| 721 | |
| 722 | static void crs_range_free(gpointer data) |
| 723 | { |
| 724 | CrsRangeEntry *entry = (CrsRangeEntry *)data; |
| 725 | g_free(entry); |
| 726 | } |
| 727 | |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 728 | static gint crs_range_compare(gconstpointer a, gconstpointer b) |
| 729 | { |
| 730 | CrsRangeEntry *entry_a = *(CrsRangeEntry **)a; |
| 731 | CrsRangeEntry *entry_b = *(CrsRangeEntry **)b; |
| 732 | |
| 733 | return (int64_t)entry_a->base - (int64_t)entry_b->base; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * crs_replace_with_free_ranges - given the 'used' ranges within [start - end] |
| 738 | * interval, computes the 'free' ranges from the same interval. |
| 739 | * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function |
| 740 | * will return { [base - a1], [a2 - b1], [b2 - limit] }. |
| 741 | */ |
| 742 | static void crs_replace_with_free_ranges(GPtrArray *ranges, |
| 743 | uint64_t start, uint64_t end) |
| 744 | { |
| 745 | GPtrArray *free_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
| 746 | uint64_t free_base = start; |
| 747 | int i; |
| 748 | |
| 749 | g_ptr_array_sort(ranges, crs_range_compare); |
| 750 | for (i = 0; i < ranges->len; i++) { |
| 751 | CrsRangeEntry *used = g_ptr_array_index(ranges, i); |
| 752 | |
| 753 | if (free_base < used->base) { |
| 754 | crs_range_insert(free_ranges, free_base, used->base - 1); |
| 755 | } |
| 756 | |
| 757 | free_base = used->limit + 1; |
| 758 | } |
| 759 | |
| 760 | if (free_base < end) { |
| 761 | crs_range_insert(free_ranges, free_base, end); |
| 762 | } |
| 763 | |
| 764 | g_ptr_array_set_size(ranges, 0); |
| 765 | for (i = 0; i < free_ranges->len; i++) { |
| 766 | g_ptr_array_add(ranges, g_ptr_array_index(free_ranges, i)); |
| 767 | } |
| 768 | |
| 769 | g_ptr_array_free(free_ranges, false); |
| 770 | } |
| 771 | |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 772 | /* |
| 773 | * crs_range_merge - merges adjacent ranges in the given array. |
| 774 | * Array elements are deleted and replaced with the merged ranges. |
| 775 | */ |
| 776 | static void crs_range_merge(GPtrArray *range) |
| 777 | { |
| 778 | GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free); |
| 779 | CrsRangeEntry *entry; |
| 780 | uint64_t range_base, range_limit; |
| 781 | int i; |
| 782 | |
| 783 | if (!range->len) { |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | g_ptr_array_sort(range, crs_range_compare); |
| 788 | |
| 789 | entry = g_ptr_array_index(range, 0); |
| 790 | range_base = entry->base; |
| 791 | range_limit = entry->limit; |
| 792 | for (i = 1; i < range->len; i++) { |
| 793 | entry = g_ptr_array_index(range, i); |
| 794 | if (entry->base - 1 == range_limit) { |
| 795 | range_limit = entry->limit; |
| 796 | } else { |
| 797 | crs_range_insert(tmp, range_base, range_limit); |
| 798 | range_base = entry->base; |
| 799 | range_limit = entry->limit; |
| 800 | } |
| 801 | } |
| 802 | crs_range_insert(tmp, range_base, range_limit); |
| 803 | |
| 804 | g_ptr_array_set_size(range, 0); |
| 805 | for (i = 0; i < tmp->len; i++) { |
| 806 | entry = g_ptr_array_index(tmp, i); |
| 807 | crs_range_insert(range, entry->base, entry->limit); |
| 808 | } |
| 809 | g_ptr_array_free(tmp, true); |
| 810 | } |
| 811 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 812 | static Aml *build_crs(PCIHostState *host, |
| 813 | GPtrArray *io_ranges, GPtrArray *mem_ranges) |
| 814 | { |
| 815 | Aml *crs = aml_resource_template(); |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 816 | GPtrArray *host_io_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
| 817 | GPtrArray *host_mem_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
| 818 | CrsRangeEntry *entry; |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 819 | uint8_t max_bus = pci_bus_num(host->bus); |
| 820 | uint8_t type; |
| 821 | int devfn; |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 822 | int i; |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 823 | |
| 824 | for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) { |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 825 | uint64_t range_base, range_limit; |
| 826 | PCIDevice *dev = host->bus->devices[devfn]; |
| 827 | |
| 828 | if (!dev) { |
| 829 | continue; |
| 830 | } |
| 831 | |
| 832 | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
| 833 | PCIIORegion *r = &dev->io_regions[i]; |
| 834 | |
| 835 | range_base = r->addr; |
| 836 | range_limit = r->addr + r->size - 1; |
| 837 | |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 838 | /* |
| 839 | * Work-around for old bioses |
| 840 | * that do not support multiple root buses |
| 841 | */ |
| 842 | if (!range_base || range_base > range_limit) { |
| 843 | continue; |
| 844 | } |
| 845 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 846 | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) { |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 847 | crs_range_insert(host_io_ranges, range_base, range_limit); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 848 | } else { /* "memory" */ |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 849 | crs_range_insert(host_mem_ranges, range_base, range_limit); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
| 854 | if (type == PCI_HEADER_TYPE_BRIDGE) { |
| 855 | uint8_t subordinate = dev->config[PCI_SUBORDINATE_BUS]; |
| 856 | if (subordinate > max_bus) { |
| 857 | max_bus = subordinate; |
| 858 | } |
| 859 | |
| 860 | range_base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO); |
| 861 | range_limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * Work-around for old bioses |
| 865 | * that do not support multiple root buses |
| 866 | */ |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 867 | if (range_base && range_base <= range_limit) { |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 868 | crs_range_insert(host_io_ranges, range_base, range_limit); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 869 | } |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 870 | |
| 871 | range_base = |
| 872 | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 873 | range_limit = |
| 874 | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | * Work-around for old bioses |
| 878 | * that do not support multiple root buses |
| 879 | */ |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 880 | if (range_base && range_base <= range_limit) { |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 881 | crs_range_insert(host_mem_ranges, range_base, range_limit); |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 882 | } |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 883 | |
| 884 | range_base = |
| 885 | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); |
| 886 | range_limit = |
| 887 | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 888 | |
| 889 | /* |
| 890 | * Work-around for old bioses |
| 891 | * that do not support multiple root buses |
| 892 | */ |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 893 | if (range_base && range_base <= range_limit) { |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 894 | crs_range_insert(host_mem_ranges, range_base, range_limit); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 895 | } |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 899 | crs_range_merge(host_io_ranges); |
| 900 | for (i = 0; i < host_io_ranges->len; i++) { |
| 901 | entry = g_ptr_array_index(host_io_ranges, i); |
| 902 | aml_append(crs, |
| 903 | aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, |
| 904 | AML_POS_DECODE, AML_ENTIRE_RANGE, |
| 905 | 0, entry->base, entry->limit, 0, |
| 906 | entry->limit - entry->base + 1)); |
| 907 | crs_range_insert(io_ranges, entry->base, entry->limit); |
| 908 | } |
| 909 | g_ptr_array_free(host_io_ranges, true); |
| 910 | |
| 911 | crs_range_merge(host_mem_ranges); |
| 912 | for (i = 0; i < host_mem_ranges->len; i++) { |
| 913 | entry = g_ptr_array_index(host_mem_ranges, i); |
| 914 | aml_append(crs, |
| 915 | aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, |
| 916 | AML_MAX_FIXED, AML_NON_CACHEABLE, |
| 917 | AML_READ_WRITE, |
| 918 | 0, entry->base, entry->limit, 0, |
| 919 | entry->limit - entry->base + 1)); |
| 920 | crs_range_insert(mem_ranges, entry->base, entry->limit); |
| 921 | } |
| 922 | g_ptr_array_free(host_mem_ranges, true); |
| 923 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 924 | aml_append(crs, |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 925 | aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE, |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 926 | 0, |
| 927 | pci_bus_num(host->bus), |
| 928 | max_bus, |
| 929 | 0, |
| 930 | max_bus - pci_bus_num(host->bus) + 1)); |
| 931 | |
| 932 | return crs; |
| 933 | } |
| 934 | |
Igor Mammedov | 5ca5efa | 2015-12-28 18:02:28 +0100 | [diff] [blame] | 935 | static void build_processor_devices(Aml *sb_scope, unsigned acpi_cpus, |
| 936 | AcpiCpuInfo *cpu, AcpiPmInfo *pm) |
| 937 | { |
| 938 | int i; |
| 939 | Aml *dev; |
| 940 | Aml *crs; |
| 941 | Aml *pkg; |
| 942 | Aml *field; |
| 943 | Aml *ifctx; |
| 944 | Aml *method; |
| 945 | |
| 946 | /* The current AML generator can cover the APIC ID range [0..255], |
| 947 | * inclusive, for VCPU hotplug. */ |
| 948 | QEMU_BUILD_BUG_ON(ACPI_CPU_HOTPLUG_ID_LIMIT > 256); |
| 949 | g_assert(acpi_cpus <= ACPI_CPU_HOTPLUG_ID_LIMIT); |
| 950 | |
| 951 | /* create PCI0.PRES device and its _CRS to reserve CPU hotplug MMIO */ |
| 952 | dev = aml_device("PCI0." stringify(CPU_HOTPLUG_RESOURCE_DEVICE)); |
| 953 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06"))); |
| 954 | aml_append(dev, |
| 955 | aml_name_decl("_UID", aml_string("CPU Hotplug resources")) |
| 956 | ); |
| 957 | /* device present, functioning, decoding, not shown in UI */ |
| 958 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 959 | crs = aml_resource_template(); |
| 960 | aml_append(crs, |
| 961 | aml_io(AML_DECODE16, pm->cpu_hp_io_base, pm->cpu_hp_io_base, 1, |
| 962 | pm->cpu_hp_io_len) |
| 963 | ); |
| 964 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 965 | aml_append(sb_scope, dev); |
| 966 | /* declare CPU hotplug MMIO region and PRS field to access it */ |
| 967 | aml_append(sb_scope, aml_operation_region( |
| 968 | "PRST", AML_SYSTEM_IO, pm->cpu_hp_io_base, pm->cpu_hp_io_len)); |
| 969 | field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
| 970 | aml_append(field, aml_named_field("PRS", 256)); |
| 971 | aml_append(sb_scope, field); |
| 972 | |
| 973 | /* build Processor object for each processor */ |
| 974 | for (i = 0; i < acpi_cpus; i++) { |
| 975 | dev = aml_processor(i, 0, 0, "CP%.02X", i); |
| 976 | |
| 977 | method = aml_method("_MAT", 0, AML_NOTSERIALIZED); |
| 978 | aml_append(method, |
| 979 | aml_return(aml_call1(CPU_MAT_METHOD, aml_int(i)))); |
| 980 | aml_append(dev, method); |
| 981 | |
| 982 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 983 | aml_append(method, |
| 984 | aml_return(aml_call1(CPU_STATUS_METHOD, aml_int(i)))); |
| 985 | aml_append(dev, method); |
| 986 | |
| 987 | method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); |
| 988 | aml_append(method, |
| 989 | aml_return(aml_call2(CPU_EJECT_METHOD, aml_int(i), aml_arg(0))) |
| 990 | ); |
| 991 | aml_append(dev, method); |
| 992 | |
| 993 | aml_append(sb_scope, dev); |
| 994 | } |
| 995 | |
| 996 | /* build this code: |
| 997 | * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...} |
| 998 | */ |
| 999 | /* Arg0 = Processor ID = APIC ID */ |
| 1000 | method = aml_method(AML_NOTIFY_METHOD, 2, AML_NOTSERIALIZED); |
| 1001 | for (i = 0; i < acpi_cpus; i++) { |
| 1002 | ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i))); |
| 1003 | aml_append(ifctx, |
| 1004 | aml_notify(aml_name("CP%.02X", i), aml_arg(1)) |
| 1005 | ); |
| 1006 | aml_append(method, ifctx); |
| 1007 | } |
| 1008 | aml_append(sb_scope, method); |
| 1009 | |
| 1010 | /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })" |
| 1011 | * |
| 1012 | * Note: The ability to create variable-sized packages was first |
| 1013 | * introduced in ACPI 2.0. ACPI 1.0 only allowed fixed-size packages |
| 1014 | * ith up to 255 elements. Windows guests up to win2k8 fail when |
| 1015 | * VarPackageOp is used. |
| 1016 | */ |
| 1017 | pkg = acpi_cpus <= 255 ? aml_package(acpi_cpus) : |
| 1018 | aml_varpackage(acpi_cpus); |
| 1019 | |
| 1020 | for (i = 0; i < acpi_cpus; i++) { |
| 1021 | uint8_t b = test_bit(i, cpu->found_cpus) ? 0x01 : 0x00; |
| 1022 | aml_append(pkg, aml_int(b)); |
| 1023 | } |
| 1024 | aml_append(sb_scope, aml_name_decl(CPU_ON_BITMAP, pkg)); |
| 1025 | } |
| 1026 | |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1027 | static void build_memory_devices(Aml *sb_scope, int nr_mem, |
| 1028 | uint16_t io_base, uint16_t io_len) |
| 1029 | { |
| 1030 | int i; |
| 1031 | Aml *scope; |
| 1032 | Aml *crs; |
| 1033 | Aml *field; |
| 1034 | Aml *dev; |
| 1035 | Aml *method; |
| 1036 | Aml *ifctx; |
| 1037 | |
| 1038 | /* build memory devices */ |
| 1039 | assert(nr_mem <= ACPI_MAX_RAM_SLOTS); |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1040 | scope = aml_scope("\\_SB.PCI0." MEMORY_HOTPLUG_DEVICE); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1041 | aml_append(scope, |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1042 | aml_name_decl(MEMORY_SLOTS_NUMBER, aml_int(nr_mem)) |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1043 | ); |
| 1044 | |
| 1045 | crs = aml_resource_template(); |
| 1046 | aml_append(crs, |
| 1047 | aml_io(AML_DECODE16, io_base, io_base, 0, io_len) |
| 1048 | ); |
| 1049 | aml_append(scope, aml_name_decl("_CRS", crs)); |
| 1050 | |
| 1051 | aml_append(scope, aml_operation_region( |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1052 | MEMORY_HOTPLUG_IO_REGION, AML_SYSTEM_IO, |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1053 | io_base, io_len) |
| 1054 | ); |
| 1055 | |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1056 | field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_DWORD_ACC, |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1057 | AML_NOLOCK, AML_PRESERVE); |
| 1058 | aml_append(field, /* read only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1059 | aml_named_field(MEMORY_SLOT_ADDR_LOW, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1060 | aml_append(field, /* read only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1061 | aml_named_field(MEMORY_SLOT_ADDR_HIGH, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1062 | aml_append(field, /* read only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1063 | aml_named_field(MEMORY_SLOT_SIZE_LOW, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1064 | aml_append(field, /* read only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1065 | aml_named_field(MEMORY_SLOT_SIZE_HIGH, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1066 | aml_append(field, /* read only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1067 | aml_named_field(MEMORY_SLOT_PROXIMITY, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1068 | aml_append(scope, field); |
| 1069 | |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1070 | field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_BYTE_ACC, |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1071 | AML_NOLOCK, AML_WRITE_AS_ZEROS); |
| 1072 | aml_append(field, aml_reserved_field(160 /* bits, Offset(20) */)); |
| 1073 | aml_append(field, /* 1 if enabled, read only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1074 | aml_named_field(MEMORY_SLOT_ENABLED, 1)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1075 | aml_append(field, |
| 1076 | /*(read) 1 if has a insert event. (write) 1 to clear event */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1077 | aml_named_field(MEMORY_SLOT_INSERT_EVENT, 1)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1078 | aml_append(field, |
| 1079 | /* (read) 1 if has a remove event. (write) 1 to clear event */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1080 | aml_named_field(MEMORY_SLOT_REMOVE_EVENT, 1)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1081 | aml_append(field, |
| 1082 | /* initiates device eject, write only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1083 | aml_named_field(MEMORY_SLOT_EJECT, 1)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1084 | aml_append(scope, field); |
| 1085 | |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1086 | field = aml_field(MEMORY_HOTPLUG_IO_REGION, AML_DWORD_ACC, |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1087 | AML_NOLOCK, AML_PRESERVE); |
| 1088 | aml_append(field, /* DIMM selector, write only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1089 | aml_named_field(MEMORY_SLOT_SLECTOR, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1090 | aml_append(field, /* _OST event code, write only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1091 | aml_named_field(MEMORY_SLOT_OST_EVENT, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1092 | aml_append(field, /* _OST status code, write only */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1093 | aml_named_field(MEMORY_SLOT_OST_STATUS, 32)); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1094 | aml_append(scope, field); |
| 1095 | aml_append(sb_scope, scope); |
| 1096 | |
| 1097 | for (i = 0; i < nr_mem; i++) { |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1098 | #define BASEPATH "\\_SB.PCI0." MEMORY_HOTPLUG_DEVICE "." |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1099 | const char *s; |
| 1100 | |
| 1101 | dev = aml_device("MP%02X", i); |
| 1102 | aml_append(dev, aml_name_decl("_UID", aml_string("0x%02X", i))); |
| 1103 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C80"))); |
| 1104 | |
| 1105 | method = aml_method("_CRS", 0, AML_NOTSERIALIZED); |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1106 | s = BASEPATH MEMORY_SLOT_CRS_METHOD; |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1107 | aml_append(method, aml_return(aml_call1(s, aml_name("_UID")))); |
| 1108 | aml_append(dev, method); |
| 1109 | |
| 1110 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1111 | s = BASEPATH MEMORY_SLOT_STATUS_METHOD; |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1112 | aml_append(method, aml_return(aml_call1(s, aml_name("_UID")))); |
| 1113 | aml_append(dev, method); |
| 1114 | |
| 1115 | method = aml_method("_PXM", 0, AML_NOTSERIALIZED); |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1116 | s = BASEPATH MEMORY_SLOT_PROXIMITY_METHOD; |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1117 | aml_append(method, aml_return(aml_call1(s, aml_name("_UID")))); |
| 1118 | aml_append(dev, method); |
| 1119 | |
| 1120 | method = aml_method("_OST", 3, AML_NOTSERIALIZED); |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1121 | s = BASEPATH MEMORY_SLOT_OST_METHOD; |
| 1122 | |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1123 | aml_append(method, aml_return(aml_call4( |
| 1124 | s, aml_name("_UID"), aml_arg(0), aml_arg(1), aml_arg(2) |
| 1125 | ))); |
| 1126 | aml_append(dev, method); |
| 1127 | |
| 1128 | method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1129 | s = BASEPATH MEMORY_SLOT_EJECT_METHOD; |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1130 | aml_append(method, aml_return(aml_call2( |
| 1131 | s, aml_name("_UID"), aml_arg(0)))); |
| 1132 | aml_append(dev, method); |
| 1133 | |
| 1134 | aml_append(sb_scope, dev); |
| 1135 | } |
| 1136 | |
| 1137 | /* build Method(MEMORY_SLOT_NOTIFY_METHOD, 2) { |
| 1138 | * If (LEqual(Arg0, 0x00)) {Notify(MP00, Arg1)} ... } |
| 1139 | */ |
Igor Mammedov | f84548d | 2015-12-28 18:02:21 +0100 | [diff] [blame] | 1140 | method = aml_method(MEMORY_SLOT_NOTIFY_METHOD, 2, AML_NOTSERIALIZED); |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1141 | for (i = 0; i < nr_mem; i++) { |
| 1142 | ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i))); |
| 1143 | aml_append(ifctx, |
| 1144 | aml_notify(aml_name("MP%.02X", i), aml_arg(1)) |
| 1145 | ); |
| 1146 | aml_append(method, ifctx); |
| 1147 | } |
| 1148 | aml_append(sb_scope, method); |
| 1149 | } |
| 1150 | |
Igor Mammedov | a57d708 | 2015-12-28 18:02:29 +0100 | [diff] [blame] | 1151 | static void build_hpet_aml(Aml *table) |
| 1152 | { |
| 1153 | Aml *crs; |
| 1154 | Aml *field; |
| 1155 | Aml *method; |
| 1156 | Aml *if_ctx; |
| 1157 | Aml *scope = aml_scope("_SB"); |
| 1158 | Aml *dev = aml_device("HPET"); |
| 1159 | Aml *zero = aml_int(0); |
| 1160 | Aml *id = aml_local(0); |
| 1161 | Aml *period = aml_local(1); |
| 1162 | |
| 1163 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103"))); |
| 1164 | aml_append(dev, aml_name_decl("_UID", zero)); |
| 1165 | |
| 1166 | aml_append(dev, |
| 1167 | aml_operation_region("HPTM", AML_SYSTEM_MEMORY, HPET_BASE, HPET_LEN)); |
| 1168 | field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE); |
| 1169 | aml_append(field, aml_named_field("VEND", 32)); |
| 1170 | aml_append(field, aml_named_field("PRD", 32)); |
| 1171 | aml_append(dev, field); |
| 1172 | |
| 1173 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1174 | aml_append(method, aml_store(aml_name("VEND"), id)); |
| 1175 | aml_append(method, aml_store(aml_name("PRD"), period)); |
| 1176 | aml_append(method, aml_shiftright(id, aml_int(16), id)); |
| 1177 | if_ctx = aml_if(aml_lor(aml_equal(id, zero), |
| 1178 | aml_equal(id, aml_int(0xffff)))); |
| 1179 | { |
| 1180 | aml_append(if_ctx, aml_return(zero)); |
| 1181 | } |
| 1182 | aml_append(method, if_ctx); |
| 1183 | |
| 1184 | if_ctx = aml_if(aml_lor(aml_equal(period, zero), |
| 1185 | aml_lgreater(period, aml_int(100000000)))); |
| 1186 | { |
| 1187 | aml_append(if_ctx, aml_return(zero)); |
| 1188 | } |
| 1189 | aml_append(method, if_ctx); |
| 1190 | |
| 1191 | aml_append(method, aml_return(aml_int(0x0F))); |
| 1192 | aml_append(dev, method); |
| 1193 | |
| 1194 | crs = aml_resource_template(); |
| 1195 | aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY)); |
| 1196 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1197 | |
| 1198 | aml_append(scope, dev); |
| 1199 | aml_append(table, scope); |
| 1200 | } |
| 1201 | |
Igor Mammedov | 95ed7e9 | 2015-12-28 18:02:34 +0100 | [diff] [blame] | 1202 | static Aml *build_fdc_device_aml(void) |
| 1203 | { |
| 1204 | Aml *dev; |
| 1205 | Aml *crs; |
| 1206 | Aml *method; |
| 1207 | Aml *if_ctx; |
| 1208 | Aml *else_ctx; |
| 1209 | Aml *zero = aml_int(0); |
| 1210 | Aml *is_present = aml_local(0); |
| 1211 | |
| 1212 | dev = aml_device("FDC0"); |
| 1213 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0700"))); |
| 1214 | |
| 1215 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1216 | aml_append(method, aml_store(aml_name("FDEN"), is_present)); |
| 1217 | if_ctx = aml_if(aml_equal(is_present, zero)); |
| 1218 | { |
| 1219 | aml_append(if_ctx, aml_return(aml_int(0x00))); |
| 1220 | } |
| 1221 | aml_append(method, if_ctx); |
| 1222 | else_ctx = aml_else(); |
| 1223 | { |
| 1224 | aml_append(else_ctx, aml_return(aml_int(0x0f))); |
| 1225 | } |
| 1226 | aml_append(method, else_ctx); |
| 1227 | aml_append(dev, method); |
| 1228 | |
| 1229 | crs = aml_resource_template(); |
| 1230 | aml_append(crs, aml_io(AML_DECODE16, 0x03F2, 0x03F2, 0x00, 0x04)); |
| 1231 | aml_append(crs, aml_io(AML_DECODE16, 0x03F7, 0x03F7, 0x00, 0x01)); |
| 1232 | aml_append(crs, aml_irq_no_flags(6)); |
| 1233 | aml_append(crs, |
| 1234 | aml_dma(AML_COMPATIBILITY, AML_NOTBUSMASTER, AML_TRANSFER8, 2)); |
| 1235 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1236 | |
| 1237 | return dev; |
| 1238 | } |
| 1239 | |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 1240 | static Aml *build_rtc_device_aml(void) |
| 1241 | { |
| 1242 | Aml *dev; |
| 1243 | Aml *crs; |
| 1244 | |
| 1245 | dev = aml_device("RTC"); |
| 1246 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0B00"))); |
| 1247 | crs = aml_resource_template(); |
| 1248 | aml_append(crs, aml_io(AML_DECODE16, 0x0070, 0x0070, 0x10, 0x02)); |
| 1249 | aml_append(crs, aml_irq_no_flags(8)); |
| 1250 | aml_append(crs, aml_io(AML_DECODE16, 0x0072, 0x0072, 0x02, 0x06)); |
Igor Mammedov | 95ed7e9 | 2015-12-28 18:02:34 +0100 | [diff] [blame] | 1251 | aml_append(dev, aml_name_decl("_CRS", crs)); |
Igor Mammedov | f58190e | 2015-12-28 18:02:32 +0100 | [diff] [blame] | 1252 | |
| 1253 | return dev; |
| 1254 | } |
| 1255 | |
| 1256 | static Aml *build_kbd_device_aml(void) |
| 1257 | { |
| 1258 | Aml *dev; |
| 1259 | Aml *crs; |
| 1260 | Aml *method; |
| 1261 | |
| 1262 | dev = aml_device("KBD"); |
| 1263 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0303"))); |
| 1264 | |
| 1265 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1266 | aml_append(method, aml_return(aml_int(0x0f))); |
| 1267 | aml_append(dev, method); |
| 1268 | |
| 1269 | crs = aml_resource_template(); |
| 1270 | aml_append(crs, aml_io(AML_DECODE16, 0x0060, 0x0060, 0x01, 0x01)); |
| 1271 | aml_append(crs, aml_io(AML_DECODE16, 0x0064, 0x0064, 0x01, 0x01)); |
| 1272 | aml_append(crs, aml_irq_no_flags(1)); |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 1273 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1274 | |
| 1275 | return dev; |
| 1276 | } |
| 1277 | |
Igor Mammedov | c355cb2 | 2015-12-28 18:02:33 +0100 | [diff] [blame] | 1278 | static Aml *build_mouse_device_aml(void) |
| 1279 | { |
| 1280 | Aml *dev; |
| 1281 | Aml *crs; |
| 1282 | Aml *method; |
| 1283 | |
| 1284 | dev = aml_device("MOU"); |
| 1285 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0F13"))); |
| 1286 | |
| 1287 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1288 | aml_append(method, aml_return(aml_int(0x0f))); |
| 1289 | aml_append(dev, method); |
| 1290 | |
| 1291 | crs = aml_resource_template(); |
| 1292 | aml_append(crs, aml_irq_no_flags(12)); |
| 1293 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1294 | |
| 1295 | return dev; |
| 1296 | } |
| 1297 | |
Igor Mammedov | 8b1da5f | 2015-12-28 18:02:35 +0100 | [diff] [blame^] | 1298 | static Aml *build_lpt_device_aml(void) |
| 1299 | { |
| 1300 | Aml *dev; |
| 1301 | Aml *crs; |
| 1302 | Aml *method; |
| 1303 | Aml *if_ctx; |
| 1304 | Aml *else_ctx; |
| 1305 | Aml *zero = aml_int(0); |
| 1306 | Aml *is_present = aml_local(0); |
| 1307 | |
| 1308 | dev = aml_device("LPT"); |
| 1309 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0400"))); |
| 1310 | |
| 1311 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1312 | aml_append(method, aml_store(aml_name("LPEN"), is_present)); |
| 1313 | if_ctx = aml_if(aml_equal(is_present, zero)); |
| 1314 | { |
| 1315 | aml_append(if_ctx, aml_return(aml_int(0x00))); |
| 1316 | } |
| 1317 | aml_append(method, if_ctx); |
| 1318 | else_ctx = aml_else(); |
| 1319 | { |
| 1320 | aml_append(else_ctx, aml_return(aml_int(0x0f))); |
| 1321 | } |
| 1322 | aml_append(method, else_ctx); |
| 1323 | aml_append(dev, method); |
| 1324 | |
| 1325 | crs = aml_resource_template(); |
| 1326 | aml_append(crs, aml_io(AML_DECODE16, 0x0378, 0x0378, 0x08, 0x08)); |
| 1327 | aml_append(crs, aml_irq_no_flags(7)); |
| 1328 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1329 | |
| 1330 | return dev; |
| 1331 | } |
| 1332 | |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 1333 | static void build_isa_devices_aml(Aml *table) |
| 1334 | { |
| 1335 | Aml *scope = aml_scope("_SB.PCI0.ISA"); |
| 1336 | |
| 1337 | aml_append(scope, build_rtc_device_aml()); |
Igor Mammedov | f58190e | 2015-12-28 18:02:32 +0100 | [diff] [blame] | 1338 | aml_append(scope, build_kbd_device_aml()); |
Igor Mammedov | c355cb2 | 2015-12-28 18:02:33 +0100 | [diff] [blame] | 1339 | aml_append(scope, build_mouse_device_aml()); |
Igor Mammedov | 95ed7e9 | 2015-12-28 18:02:34 +0100 | [diff] [blame] | 1340 | aml_append(scope, build_fdc_device_aml()); |
Igor Mammedov | 8b1da5f | 2015-12-28 18:02:35 +0100 | [diff] [blame^] | 1341 | aml_append(scope, build_lpt_device_aml()); |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 1342 | |
| 1343 | aml_append(table, scope); |
| 1344 | } |
| 1345 | |
Igor Mammedov | 3892a2b | 2015-12-28 18:02:30 +0100 | [diff] [blame] | 1346 | static void build_dbg_aml(Aml *table) |
| 1347 | { |
| 1348 | Aml *field; |
| 1349 | Aml *method; |
| 1350 | Aml *while_ctx; |
| 1351 | Aml *scope = aml_scope("\\"); |
| 1352 | Aml *buf = aml_local(0); |
| 1353 | Aml *len = aml_local(1); |
| 1354 | Aml *idx = aml_local(2); |
| 1355 | |
| 1356 | aml_append(scope, |
| 1357 | aml_operation_region("DBG", AML_SYSTEM_IO, 0x0402, 0x01)); |
| 1358 | field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
| 1359 | aml_append(field, aml_named_field("DBGB", 8)); |
| 1360 | aml_append(scope, field); |
| 1361 | |
| 1362 | method = aml_method("DBUG", 1, AML_NOTSERIALIZED); |
| 1363 | |
| 1364 | aml_append(method, aml_to_hexstring(aml_arg(0), buf)); |
| 1365 | aml_append(method, aml_to_buffer(buf, buf)); |
| 1366 | aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len)); |
| 1367 | aml_append(method, aml_store(aml_int(0), idx)); |
| 1368 | |
| 1369 | while_ctx = aml_while(aml_lless(idx, len)); |
| 1370 | aml_append(while_ctx, |
| 1371 | aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB"))); |
| 1372 | aml_append(while_ctx, aml_increment(idx)); |
| 1373 | aml_append(method, while_ctx); |
| 1374 | |
| 1375 | aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB"))); |
| 1376 | aml_append(scope, method); |
| 1377 | |
| 1378 | aml_append(table, scope); |
| 1379 | } |
| 1380 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1381 | static void |
| 1382 | build_ssdt(GArray *table_data, GArray *linker, |
| 1383 | AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc, |
| 1384 | PcPciInfo *pci, PcGuestInfo *guest_info) |
| 1385 | { |
Igor Mammedov | bef3492 | 2014-06-02 15:25:26 +0200 | [diff] [blame] | 1386 | MachineState *machine = MACHINE(qdev_get_machine()); |
| 1387 | uint32_t nr_mem = machine->ram_slots; |
Igor Mammedov | 5ca5efa | 2015-12-28 18:02:28 +0100 | [diff] [blame] | 1388 | Aml *ssdt, *sb_scope, *scope, *pkg, *dev, *method, *crs, *field; |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1389 | PCIBus *bus = NULL; |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 1390 | GPtrArray *io_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
| 1391 | GPtrArray *mem_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1392 | CrsRangeEntry *entry; |
| 1393 | int root_bus_limit = 0xFF; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1394 | int i; |
| 1395 | |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1396 | ssdt = init_aml_allocator(); |
Laszlo Ersek | 2fd71f1 | 2014-03-17 17:05:17 +0100 | [diff] [blame] | 1397 | |
Igor Mammedov | 4ec8d2b | 2015-02-20 18:22:09 +0000 | [diff] [blame] | 1398 | /* Reserve space for header */ |
| 1399 | acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1400 | |
Igor Mammedov | 3892a2b | 2015-12-28 18:02:30 +0100 | [diff] [blame] | 1401 | build_dbg_aml(ssdt); |
Igor Mammedov | a57d708 | 2015-12-28 18:02:29 +0100 | [diff] [blame] | 1402 | build_hpet_aml(ssdt); |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 1403 | build_isa_devices_aml(ssdt); |
Igor Mammedov | fbd7a6b | 2015-12-28 18:02:23 +0100 | [diff] [blame] | 1404 | build_cpu_hotplug_aml(ssdt); |
Igor Mammedov | 30bd0cf | 2015-12-28 18:02:09 +0100 | [diff] [blame] | 1405 | build_memory_hotplug_aml(ssdt, nr_mem, pm->mem_hp_io_base, |
| 1406 | pm->mem_hp_io_len); |
| 1407 | |
Igor Mammedov | 7f4495e | 2015-12-28 18:02:20 +0100 | [diff] [blame] | 1408 | scope = aml_scope("\\_GPE"); |
Igor Mammedov | 6b30608 | 2015-12-28 18:02:27 +0100 | [diff] [blame] | 1409 | { |
| 1410 | method = aml_method("_E02", 0, AML_NOTSERIALIZED); |
| 1411 | aml_append(method, aml_call0("\\_SB." CPU_SCAN_METHOD)); |
| 1412 | aml_append(scope, method); |
| 1413 | |
| 1414 | method = aml_method("_E03", 0, AML_NOTSERIALIZED); |
| 1415 | aml_append(method, aml_call0(MEMORY_HOTPLUG_HANDLER_PATH)); |
| 1416 | aml_append(scope, method); |
| 1417 | } |
Igor Mammedov | 7f4495e | 2015-12-28 18:02:20 +0100 | [diff] [blame] | 1418 | aml_append(ssdt, scope); |
| 1419 | |
Marcel Apfelbaum | 81ed648 | 2015-11-26 18:00:28 +0200 | [diff] [blame] | 1420 | bus = PC_MACHINE(machine)->bus; |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1421 | if (bus) { |
| 1422 | QLIST_FOREACH(bus, &bus->child, sibling) { |
| 1423 | uint8_t bus_num = pci_bus_num(bus); |
Marcel Apfelbaum | 0e79e51 | 2015-06-02 14:23:10 +0300 | [diff] [blame] | 1424 | uint8_t numa_node = pci_bus_numa_node(bus); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1425 | |
| 1426 | /* look only for expander root buses */ |
| 1427 | if (!pci_bus_is_root(bus)) { |
| 1428 | continue; |
| 1429 | } |
| 1430 | |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1431 | if (bus_num < root_bus_limit) { |
| 1432 | root_bus_limit = bus_num - 1; |
| 1433 | } |
| 1434 | |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1435 | scope = aml_scope("\\_SB"); |
| 1436 | dev = aml_device("PC%.02X", bus_num); |
Laszlo Ersek | c96d928 | 2015-06-11 02:37:58 +0200 | [diff] [blame] | 1437 | aml_append(dev, aml_name_decl("_UID", aml_int(bus_num))); |
| 1438 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03"))); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1439 | aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num))); |
Marcel Apfelbaum | 0e79e51 | 2015-06-02 14:23:10 +0300 | [diff] [blame] | 1440 | |
| 1441 | if (numa_node != NUMA_NODE_UNASSIGNED) { |
| 1442 | aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node))); |
| 1443 | } |
| 1444 | |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 1445 | aml_append(dev, build_prt()); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 1446 | crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), |
| 1447 | io_ranges, mem_ranges); |
| 1448 | aml_append(dev, aml_name_decl("_CRS", crs)); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1449 | aml_append(scope, dev); |
| 1450 | aml_append(ssdt, scope); |
| 1451 | } |
| 1452 | } |
| 1453 | |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 1454 | scope = aml_scope("\\_SB.PCI0"); |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1455 | /* build PCI0._CRS */ |
| 1456 | crs = aml_resource_template(); |
| 1457 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1458 | aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE, |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1459 | 0x0000, 0x0, root_bus_limit, |
| 1460 | 0x0000, root_bus_limit + 1)); |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1461 | aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08)); |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1462 | |
| 1463 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1464 | aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, |
| 1465 | AML_POS_DECODE, AML_ENTIRE_RANGE, |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1466 | 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8)); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1467 | |
| 1468 | crs_replace_with_free_ranges(io_ranges, 0x0D00, 0xFFFF); |
| 1469 | for (i = 0; i < io_ranges->len; i++) { |
| 1470 | entry = g_ptr_array_index(io_ranges, i); |
| 1471 | aml_append(crs, |
| 1472 | aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, |
| 1473 | AML_POS_DECODE, AML_ENTIRE_RANGE, |
| 1474 | 0x0000, entry->base, entry->limit, |
| 1475 | 0x0000, entry->limit - entry->base + 1)); |
| 1476 | } |
| 1477 | |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1478 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1479 | aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, |
| 1480 | AML_CACHEABLE, AML_READ_WRITE, |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1481 | 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000)); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1482 | |
| 1483 | crs_replace_with_free_ranges(mem_ranges, pci->w32.begin, pci->w32.end - 1); |
| 1484 | for (i = 0; i < mem_ranges->len; i++) { |
| 1485 | entry = g_ptr_array_index(mem_ranges, i); |
| 1486 | aml_append(crs, |
| 1487 | aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, |
| 1488 | AML_NON_CACHEABLE, AML_READ_WRITE, |
| 1489 | 0, entry->base, entry->limit, |
| 1490 | 0, entry->limit - entry->base + 1)); |
| 1491 | } |
| 1492 | |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1493 | if (pci->w64.begin) { |
| 1494 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1495 | aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, |
| 1496 | AML_CACHEABLE, AML_READ_WRITE, |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1497 | 0, pci->w64.begin, pci->w64.end - 1, 0, |
| 1498 | pci->w64.end - pci->w64.begin)); |
| 1499 | } |
| 1500 | aml_append(scope, aml_name_decl("_CRS", crs)); |
| 1501 | |
Igor Mammedov | d31c909 | 2015-02-20 18:22:08 +0000 | [diff] [blame] | 1502 | /* reserve GPE0 block resources */ |
| 1503 | dev = aml_device("GPE0"); |
| 1504 | aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06"))); |
| 1505 | aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources"))); |
| 1506 | /* device present, functioning, decoding, not shown in UI */ |
| 1507 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 1508 | crs = aml_resource_template(); |
| 1509 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1510 | aml_io(AML_DECODE16, pm->gpe0_blk, pm->gpe0_blk, 1, pm->gpe0_blk_len) |
Igor Mammedov | d31c909 | 2015-02-20 18:22:08 +0000 | [diff] [blame] | 1511 | ); |
| 1512 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1513 | aml_append(scope, dev); |
| 1514 | |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1515 | g_ptr_array_free(io_ranges, true); |
| 1516 | g_ptr_array_free(mem_ranges, true); |
| 1517 | |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 1518 | /* reserve PCIHP resources */ |
| 1519 | if (pm->pcihp_io_len) { |
| 1520 | dev = aml_device("PHPR"); |
| 1521 | aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06"))); |
| 1522 | aml_append(dev, |
| 1523 | aml_name_decl("_UID", aml_string("PCI Hotplug resources"))); |
| 1524 | /* device present, functioning, decoding, not shown in UI */ |
| 1525 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 1526 | crs = aml_resource_template(); |
| 1527 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1528 | aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1, |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 1529 | pm->pcihp_io_len) |
| 1530 | ); |
| 1531 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1532 | aml_append(scope, dev); |
| 1533 | } |
| 1534 | aml_append(ssdt, scope); |
| 1535 | |
Igor Mammedov | ebc3028 | 2015-02-18 19:14:29 +0000 | [diff] [blame] | 1536 | /* create S3_ / S4_ / S5_ packages if necessary */ |
| 1537 | scope = aml_scope("\\"); |
| 1538 | if (!pm->s3_disabled) { |
| 1539 | pkg = aml_package(4); |
| 1540 | aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */ |
| 1541 | aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */ |
| 1542 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1543 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1544 | aml_append(scope, aml_name_decl("_S3", pkg)); |
| 1545 | } |
| 1546 | |
| 1547 | if (!pm->s4_disabled) { |
| 1548 | pkg = aml_package(4); |
| 1549 | aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */ |
| 1550 | /* PM1b_CNT.SLP_TYP, FIXME: not impl. */ |
| 1551 | aml_append(pkg, aml_int(pm->s4_val)); |
| 1552 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1553 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1554 | aml_append(scope, aml_name_decl("_S4", pkg)); |
| 1555 | } |
| 1556 | |
| 1557 | pkg = aml_package(4); |
| 1558 | aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */ |
| 1559 | aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */ |
| 1560 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1561 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1562 | aml_append(scope, aml_name_decl("_S5", pkg)); |
| 1563 | aml_append(ssdt, scope); |
| 1564 | |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 1565 | if (misc->applesmc_io_base) { |
| 1566 | scope = aml_scope("\\_SB.PCI0.ISA"); |
| 1567 | dev = aml_device("SMC"); |
| 1568 | |
| 1569 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001"))); |
| 1570 | /* device present, functioning, decoding, not shown in UI */ |
| 1571 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 1572 | |
| 1573 | crs = aml_resource_template(); |
| 1574 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1575 | aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base, |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 1576 | 0x01, APPLESMC_MAX_DATA_LENGTH) |
| 1577 | ); |
| 1578 | aml_append(crs, aml_irq_no_flags(6)); |
| 1579 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1580 | |
| 1581 | aml_append(scope, dev); |
| 1582 | aml_append(ssdt, scope); |
| 1583 | } |
| 1584 | |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1585 | if (misc->pvpanic_port) { |
| 1586 | scope = aml_scope("\\_SB.PCI0.ISA"); |
| 1587 | |
Radim Krčmář | 2332333 | 2015-05-29 21:57:32 +0200 | [diff] [blame] | 1588 | dev = aml_device("PEVT"); |
Igor Mammedov | e65bef6 | 2015-03-30 14:18:27 +0200 | [diff] [blame] | 1589 | aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001"))); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1590 | |
| 1591 | crs = aml_resource_template(); |
| 1592 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1593 | aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1) |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1594 | ); |
| 1595 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1596 | |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1597 | aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO, |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1598 | misc->pvpanic_port, 1)); |
Igor Mammedov | 36de884 | 2015-12-10 00:41:12 +0100 | [diff] [blame] | 1599 | field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1600 | aml_append(field, aml_named_field("PEPT", 8)); |
| 1601 | aml_append(dev, field); |
| 1602 | |
Gal Hammer | 8ef3ea2 | 2015-07-26 11:00:51 +0300 | [diff] [blame] | 1603 | /* device present, functioning, decoding, shown in UI */ |
| 1604 | aml_append(dev, aml_name_decl("_STA", aml_int(0xF))); |
Radim Krčmář | 2332333 | 2015-05-29 21:57:32 +0200 | [diff] [blame] | 1605 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 1606 | method = aml_method("RDPT", 0, AML_NOTSERIALIZED); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1607 | aml_append(method, aml_store(aml_name("PEPT"), aml_local(0))); |
| 1608 | aml_append(method, aml_return(aml_local(0))); |
| 1609 | aml_append(dev, method); |
| 1610 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 1611 | method = aml_method("WRPT", 1, AML_NOTSERIALIZED); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1612 | aml_append(method, aml_store(aml_arg(0), aml_name("PEPT"))); |
| 1613 | aml_append(dev, method); |
| 1614 | |
| 1615 | aml_append(scope, dev); |
| 1616 | aml_append(ssdt, scope); |
| 1617 | } |
| 1618 | |
Gal Hammer | 7824df3 | 2015-04-21 11:26:12 +0300 | [diff] [blame] | 1619 | sb_scope = aml_scope("\\_SB"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1620 | { |
Igor Mammedov | 5ca5efa | 2015-12-28 18:02:28 +0100 | [diff] [blame] | 1621 | build_processor_devices(sb_scope, guest_info->apic_id_limit, cpu, pm); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1622 | |
Igor Mammedov | f177d40 | 2015-12-28 18:02:19 +0100 | [diff] [blame] | 1623 | build_memory_devices(sb_scope, nr_mem, pm->mem_hp_io_base, |
| 1624 | pm->mem_hp_io_len); |
Igor Mammedov | 8698c0c | 2015-02-18 19:14:46 +0000 | [diff] [blame] | 1625 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1626 | { |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 1627 | Object *pci_host; |
| 1628 | PCIBus *bus = NULL; |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 1629 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 1630 | pci_host = acpi_get_i386_pci_host(); |
| 1631 | if (pci_host) { |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 1632 | bus = PCI_HOST_BRIDGE(pci_host)->bus; |
| 1633 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1634 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 1635 | if (bus) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 1636 | Aml *scope = aml_scope("PCI0"); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 1637 | /* Scan all PCI buses. Generate tables to support hotplug. */ |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 1638 | build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en); |
Igor Mammedov | 72d97b3 | 2015-06-09 05:31:53 +0200 | [diff] [blame] | 1639 | |
| 1640 | if (misc->tpm_version != TPM_VERSION_UNSPEC) { |
| 1641 | dev = aml_device("ISA.TPM"); |
| 1642 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C31"))); |
| 1643 | aml_append(dev, aml_name_decl("_STA", aml_int(0xF))); |
| 1644 | crs = aml_resource_template(); |
| 1645 | aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE, |
| 1646 | TPM_TIS_ADDR_SIZE, AML_READ_WRITE)); |
| 1647 | aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); |
| 1648 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1649 | aml_append(scope, dev); |
| 1650 | } |
| 1651 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 1652 | aml_append(sb_scope, scope); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1653 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1654 | } |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1655 | aml_append(ssdt, sb_scope); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1656 | } |
| 1657 | |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1658 | /* copy AML table into ACPI tables blob and patch header there */ |
| 1659 | g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1660 | build_header(linker, table_data, |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1661 | (void *)(table_data->data + table_data->len - ssdt->buf->len), |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1662 | "SSDT", ssdt->buf->len, 1, NULL); |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1663 | free_aml_allocator(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | static void |
| 1667 | build_hpet(GArray *table_data, GArray *linker) |
| 1668 | { |
| 1669 | Acpi20Hpet *hpet; |
| 1670 | |
| 1671 | hpet = acpi_data_push(table_data, sizeof(*hpet)); |
| 1672 | /* Note timer_block_id value must be kept in sync with value advertised by |
| 1673 | * emulated hpet |
| 1674 | */ |
| 1675 | hpet->timer_block_id = cpu_to_le32(0x8086a201); |
| 1676 | hpet->addr.address = cpu_to_le64(HPET_BASE); |
| 1677 | build_header(linker, table_data, |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1678 | (void *)hpet, "HPET", sizeof(*hpet), 1, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1679 | } |
| 1680 | |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1681 | static void |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 1682 | build_tpm_tcpa(GArray *table_data, GArray *linker, GArray *tcpalog) |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1683 | { |
| 1684 | Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa); |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 1685 | uint64_t log_area_start_address = acpi_data_len(tcpalog); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1686 | |
| 1687 | tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT); |
| 1688 | tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE); |
| 1689 | tcpa->log_area_start_address = cpu_to_le64(log_area_start_address); |
| 1690 | |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 1691 | bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, 1, |
| 1692 | false /* high memory */); |
| 1693 | |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1694 | /* log area start address to be filled by Guest linker */ |
| 1695 | bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 1696 | ACPI_BUILD_TPMLOG_FILE, |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1697 | table_data, &tcpa->log_area_start_address, |
| 1698 | sizeof(tcpa->log_area_start_address)); |
| 1699 | |
| 1700 | build_header(linker, table_data, |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1701 | (void *)tcpa, "TCPA", sizeof(*tcpa), 2, NULL); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1702 | |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 1703 | acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | static void |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 1707 | build_tpm2(GArray *table_data, GArray *linker) |
| 1708 | { |
| 1709 | Acpi20TPM2 *tpm2_ptr; |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 1710 | |
| 1711 | tpm2_ptr = acpi_data_push(table_data, sizeof *tpm2_ptr); |
| 1712 | |
| 1713 | tpm2_ptr->platform_class = cpu_to_le16(TPM2_ACPI_CLASS_CLIENT); |
| 1714 | tpm2_ptr->control_area_address = cpu_to_le64(0); |
| 1715 | tpm2_ptr->start_method = cpu_to_le32(TPM2_START_METHOD_MMIO); |
| 1716 | |
| 1717 | build_header(linker, table_data, |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1718 | (void *)tpm2_ptr, "TPM2", sizeof(*tpm2_ptr), 4, NULL); |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 1719 | } |
| 1720 | |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1721 | typedef enum { |
| 1722 | MEM_AFFINITY_NOFLAGS = 0, |
| 1723 | MEM_AFFINITY_ENABLED = (1 << 0), |
| 1724 | MEM_AFFINITY_HOTPLUGGABLE = (1 << 1), |
| 1725 | MEM_AFFINITY_NON_VOLATILE = (1 << 2), |
| 1726 | } MemoryAffinityFlags; |
| 1727 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1728 | static void |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1729 | acpi_build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base, |
| 1730 | uint64_t len, int node, MemoryAffinityFlags flags) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1731 | { |
| 1732 | numamem->type = ACPI_SRAT_MEMORY; |
| 1733 | numamem->length = sizeof(*numamem); |
| 1734 | memset(numamem->proximity, 0, 4); |
| 1735 | numamem->proximity[0] = node; |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1736 | numamem->flags = cpu_to_le32(flags); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1737 | numamem->base_addr = cpu_to_le64(base); |
| 1738 | numamem->range_length = cpu_to_le64(len); |
| 1739 | } |
| 1740 | |
| 1741 | static void |
Igor Mammedov | dd0247e | 2014-11-10 16:20:50 +0000 | [diff] [blame] | 1742 | build_srat(GArray *table_data, GArray *linker, PcGuestInfo *guest_info) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1743 | { |
| 1744 | AcpiSystemResourceAffinityTable *srat; |
| 1745 | AcpiSratProcessorAffinity *core; |
| 1746 | AcpiSratMemoryAffinity *numamem; |
| 1747 | |
| 1748 | int i; |
| 1749 | uint64_t curnode; |
| 1750 | int srat_start, numa_start, slots; |
| 1751 | uint64_t mem_len, mem_base, next_base; |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 1752 | PCMachineState *pcms = PC_MACHINE(qdev_get_machine()); |
| 1753 | ram_addr_t hotplugabble_address_space_size = |
| 1754 | object_property_get_int(OBJECT(pcms), PC_MACHINE_MEMHP_REGION_SIZE, |
| 1755 | NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1756 | |
| 1757 | srat_start = table_data->len; |
| 1758 | |
| 1759 | srat = acpi_data_push(table_data, sizeof *srat); |
| 1760 | srat->reserved1 = cpu_to_le32(1); |
| 1761 | core = (void *)(srat + 1); |
| 1762 | |
| 1763 | for (i = 0; i < guest_info->apic_id_limit; ++i) { |
| 1764 | core = acpi_data_push(table_data, sizeof *core); |
| 1765 | core->type = ACPI_SRAT_PROCESSOR; |
| 1766 | core->length = sizeof(*core); |
| 1767 | core->local_apic_id = i; |
| 1768 | curnode = guest_info->node_cpu[i]; |
| 1769 | core->proximity_lo = curnode; |
| 1770 | memset(core->proximity_hi, 0, 3); |
| 1771 | core->local_sapic_eid = 0; |
Igor Mammedov | dd0247e | 2014-11-10 16:20:50 +0000 | [diff] [blame] | 1772 | core->flags = cpu_to_le32(1); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | /* the memory map is a bit tricky, it contains at least one hole |
| 1777 | * from 640k-1M and possibly another one from 3.5G-4G. |
| 1778 | */ |
| 1779 | next_base = 0; |
| 1780 | numa_start = table_data->len; |
| 1781 | |
| 1782 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1783 | acpi_build_srat_memory(numamem, 0, 640*1024, 0, MEM_AFFINITY_ENABLED); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1784 | next_base = 1024 * 1024; |
| 1785 | for (i = 1; i < guest_info->numa_nodes + 1; ++i) { |
| 1786 | mem_base = next_base; |
| 1787 | mem_len = guest_info->node_mem[i - 1]; |
| 1788 | if (i == 1) { |
| 1789 | mem_len -= 1024 * 1024; |
| 1790 | } |
| 1791 | next_base = mem_base + mem_len; |
| 1792 | |
| 1793 | /* Cut out the ACPI_PCI hole */ |
Eduardo Habkost | 4c8a949 | 2014-01-09 17:12:43 -0200 | [diff] [blame] | 1794 | if (mem_base <= guest_info->ram_size_below_4g && |
| 1795 | next_base > guest_info->ram_size_below_4g) { |
| 1796 | mem_len -= next_base - guest_info->ram_size_below_4g; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1797 | if (mem_len > 0) { |
| 1798 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1799 | acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1, |
| 1800 | MEM_AFFINITY_ENABLED); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1801 | } |
| 1802 | mem_base = 1ULL << 32; |
Eduardo Habkost | 4c8a949 | 2014-01-09 17:12:43 -0200 | [diff] [blame] | 1803 | mem_len = next_base - guest_info->ram_size_below_4g; |
| 1804 | next_base += (1ULL << 32) - guest_info->ram_size_below_4g; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1805 | } |
| 1806 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1807 | acpi_build_srat_memory(numamem, mem_base, mem_len, i - 1, |
| 1808 | MEM_AFFINITY_ENABLED); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1809 | } |
| 1810 | slots = (table_data->len - numa_start) / sizeof *numamem; |
| 1811 | for (; slots < guest_info->numa_nodes + 2; slots++) { |
| 1812 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Igor Mammedov | 04ed3ea | 2014-06-02 15:24:58 +0200 | [diff] [blame] | 1813 | acpi_build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1814 | } |
| 1815 | |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 1816 | /* |
| 1817 | * Entry is required for Windows to enable memory hotplug in OS. |
| 1818 | * Memory devices may override proximity set by this entry, |
| 1819 | * providing _PXM method if necessary. |
| 1820 | */ |
| 1821 | if (hotplugabble_address_space_size) { |
| 1822 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Bharata B Rao | a7d69ff | 2015-06-29 13:50:22 +0530 | [diff] [blame] | 1823 | acpi_build_srat_memory(numamem, pcms->hotplug_memory.base, |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 1824 | hotplugabble_address_space_size, 0, |
| 1825 | MEM_AFFINITY_HOTPLUGGABLE | |
| 1826 | MEM_AFFINITY_ENABLED); |
| 1827 | } |
| 1828 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1829 | build_header(linker, table_data, |
| 1830 | (void *)(table_data->data + srat_start), |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 1831 | "SRAT", |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1832 | table_data->len - srat_start, 1, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | static void |
| 1836 | build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info) |
| 1837 | { |
| 1838 | AcpiTableMcfg *mcfg; |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 1839 | const char *sig; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1840 | int len = sizeof(*mcfg) + 1 * sizeof(mcfg->allocation[0]); |
| 1841 | |
| 1842 | mcfg = acpi_data_push(table_data, len); |
| 1843 | mcfg->allocation[0].address = cpu_to_le64(info->mcfg_base); |
| 1844 | /* Only a single allocation so no need to play with segments */ |
| 1845 | mcfg->allocation[0].pci_segment = cpu_to_le16(0); |
| 1846 | mcfg->allocation[0].start_bus_number = 0; |
| 1847 | mcfg->allocation[0].end_bus_number = PCIE_MMCFG_BUS(info->mcfg_size - 1); |
| 1848 | |
| 1849 | /* MCFG is used for ECAM which can be enabled or disabled by guest. |
| 1850 | * To avoid table size changes (which create migration issues), |
| 1851 | * always create the table even if there are no allocations, |
| 1852 | * but set the signature to a reserved value in this case. |
| 1853 | * ACPI spec requires OSPMs to ignore such tables. |
| 1854 | */ |
| 1855 | if (info->mcfg_base == PCIE_BASE_ADDR_UNMAPPED) { |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 1856 | /* Reserved signature: ignored by OSPM */ |
| 1857 | sig = "QEMU"; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1858 | } else { |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 1859 | sig = "MCFG"; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1860 | } |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1861 | build_header(linker, table_data, (void *)mcfg, sig, len, 1, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | static void |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 1865 | build_dmar_q35(GArray *table_data, GArray *linker) |
| 1866 | { |
| 1867 | int dmar_start = table_data->len; |
| 1868 | |
| 1869 | AcpiTableDmar *dmar; |
| 1870 | AcpiDmarHardwareUnit *drhd; |
| 1871 | |
| 1872 | dmar = acpi_data_push(table_data, sizeof(*dmar)); |
| 1873 | dmar->host_address_width = VTD_HOST_ADDRESS_WIDTH - 1; |
| 1874 | dmar->flags = 0; /* No intr_remap for now */ |
| 1875 | |
| 1876 | /* DMAR Remapping Hardware Unit Definition structure */ |
| 1877 | drhd = acpi_data_push(table_data, sizeof(*drhd)); |
| 1878 | drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT); |
| 1879 | drhd->length = cpu_to_le16(sizeof(*drhd)); /* No device scope now */ |
| 1880 | drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL; |
| 1881 | drhd->pci_segment = cpu_to_le16(0); |
| 1882 | drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR); |
| 1883 | |
| 1884 | build_header(linker, table_data, (void *)(table_data->data + dmar_start), |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1885 | "DMAR", table_data->len - dmar_start, 1, NULL); |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | static void |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1889 | build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc) |
| 1890 | { |
Michael S. Tsirkin | 53db092 | 2013-11-14 13:51:25 +0200 | [diff] [blame] | 1891 | AcpiTableHeader *dsdt; |
| 1892 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1893 | assert(misc->dsdt_code && misc->dsdt_size); |
Michael S. Tsirkin | 53db092 | 2013-11-14 13:51:25 +0200 | [diff] [blame] | 1894 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1895 | dsdt = acpi_data_push(table_data, misc->dsdt_size); |
| 1896 | memcpy(dsdt, misc->dsdt_code, misc->dsdt_size); |
Michael S. Tsirkin | 53db092 | 2013-11-14 13:51:25 +0200 | [diff] [blame] | 1897 | |
| 1898 | memset(dsdt, 0, sizeof *dsdt); |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 1899 | build_header(linker, table_data, dsdt, "DSDT", |
Xiao Guangrong | 8870ca0 | 2015-12-02 15:20:57 +0800 | [diff] [blame] | 1900 | misc->dsdt_size, 1, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1901 | } |
| 1902 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1903 | static GArray * |
| 1904 | build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt) |
| 1905 | { |
| 1906 | AcpiRsdpDescriptor *rsdp = acpi_data_push(rsdp_table, sizeof *rsdp); |
| 1907 | |
Michael S. Tsirkin | d67aadc | 2014-08-04 16:56:57 +0200 | [diff] [blame] | 1908 | bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, 16, |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1909 | true /* fseg memory */); |
| 1910 | |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 1911 | memcpy(&rsdp->signature, "RSD PTR ", 8); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1912 | memcpy(rsdp->oem_id, ACPI_BUILD_APPNAME6, 6); |
| 1913 | rsdp->rsdt_physical_address = cpu_to_le32(rsdt); |
| 1914 | /* Address to be filled by Guest linker */ |
| 1915 | bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE, |
| 1916 | ACPI_BUILD_TABLE_FILE, |
| 1917 | rsdp_table, &rsdp->rsdt_physical_address, |
| 1918 | sizeof rsdp->rsdt_physical_address); |
| 1919 | rsdp->checksum = 0; |
| 1920 | /* Checksum to be filled by Guest linker */ |
| 1921 | bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE, |
| 1922 | rsdp, rsdp, sizeof *rsdp, &rsdp->checksum); |
| 1923 | |
| 1924 | return rsdp_table; |
| 1925 | } |
| 1926 | |
| 1927 | typedef |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1928 | struct AcpiBuildState { |
| 1929 | /* Copy of table in RAM (for patching). */ |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 1930 | MemoryRegion *table_mr; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1931 | /* Is table patched? */ |
| 1932 | uint8_t patched; |
| 1933 | PcGuestInfo *guest_info; |
Michael S. Tsirkin | d70414a | 2015-02-09 13:59:53 +0000 | [diff] [blame] | 1934 | void *rsdp; |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 1935 | MemoryRegion *rsdp_mr; |
| 1936 | MemoryRegion *linker_mr; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1937 | } AcpiBuildState; |
| 1938 | |
| 1939 | static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) |
| 1940 | { |
| 1941 | Object *pci_host; |
| 1942 | QObject *o; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1943 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 1944 | pci_host = acpi_get_i386_pci_host(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1945 | g_assert(pci_host); |
| 1946 | |
| 1947 | o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL); |
| 1948 | if (!o) { |
| 1949 | return false; |
| 1950 | } |
| 1951 | mcfg->mcfg_base = qint_get_int(qobject_to_qint(o)); |
Kirill Batuzov | 097a97a | 2014-04-24 18:15:57 +0400 | [diff] [blame] | 1952 | qobject_decref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1953 | |
| 1954 | o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL); |
| 1955 | assert(o); |
| 1956 | mcfg->mcfg_size = qint_get_int(qobject_to_qint(o)); |
Kirill Batuzov | 097a97a | 2014-04-24 18:15:57 +0400 | [diff] [blame] | 1957 | qobject_decref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1958 | return true; |
| 1959 | } |
| 1960 | |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 1961 | static bool acpi_has_iommu(void) |
| 1962 | { |
| 1963 | bool ambiguous; |
| 1964 | Object *intel_iommu; |
| 1965 | |
| 1966 | intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE, |
| 1967 | &ambiguous); |
| 1968 | return intel_iommu && !ambiguous; |
| 1969 | } |
| 1970 | |
Xiao Guangrong | 87252e1 | 2015-12-02 15:20:58 +0800 | [diff] [blame] | 1971 | static bool acpi_has_nvdimm(void) |
| 1972 | { |
| 1973 | PCMachineState *pcms = PC_MACHINE(qdev_get_machine()); |
| 1974 | |
| 1975 | return pcms->nvdimm; |
| 1976 | } |
| 1977 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1978 | static |
| 1979 | void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables) |
| 1980 | { |
| 1981 | GArray *table_offsets; |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 1982 | unsigned facs, ssdt, dsdt, rsdt; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1983 | AcpiCpuInfo cpu; |
| 1984 | AcpiPmInfo pm; |
| 1985 | AcpiMiscInfo misc; |
| 1986 | AcpiMcfgInfo mcfg; |
| 1987 | PcPciInfo pci; |
| 1988 | uint8_t *u; |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 1989 | size_t aml_len = 0; |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 1990 | GArray *tables_blob = tables->table_data; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1991 | |
| 1992 | acpi_get_cpu_info(&cpu); |
| 1993 | acpi_get_pm_info(&pm); |
| 1994 | acpi_get_dsdt(&misc); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1995 | acpi_get_misc_info(&misc); |
| 1996 | acpi_get_pci_info(&pci); |
| 1997 | |
| 1998 | table_offsets = g_array_new(false, true /* clear */, |
| 1999 | sizeof(uint32_t)); |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2000 | ACPI_BUILD_DPRINTF("init ACPI tables\n"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2001 | |
| 2002 | bios_linker_loader_alloc(tables->linker, ACPI_BUILD_TABLE_FILE, |
| 2003 | 64 /* Ensure FACS is aligned */, |
| 2004 | false /* high memory */); |
| 2005 | |
| 2006 | /* |
| 2007 | * FACS is pointed to by FADT. |
| 2008 | * We place it first since it's the only table that has alignment |
| 2009 | * requirements. |
| 2010 | */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2011 | facs = tables_blob->len; |
| 2012 | build_facs(tables_blob, tables->linker, guest_info); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2013 | |
| 2014 | /* DSDT is pointed to by FADT */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2015 | dsdt = tables_blob->len; |
| 2016 | build_dsdt(tables_blob, tables->linker, &misc); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2017 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2018 | /* Count the size of the DSDT and SSDT, we will need it for legacy |
| 2019 | * sizing of ACPI tables. |
| 2020 | */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2021 | aml_len += tables_blob->len - dsdt; |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2022 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2023 | /* ACPI tables pointed to by RSDT */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2024 | acpi_add_table(table_offsets, tables_blob); |
| 2025 | build_fadt(tables_blob, tables->linker, &pm, facs, dsdt); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2026 | |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2027 | ssdt = tables_blob->len; |
| 2028 | acpi_add_table(table_offsets, tables_blob); |
| 2029 | build_ssdt(tables_blob, tables->linker, &cpu, &pm, &misc, &pci, |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2030 | guest_info); |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2031 | aml_len += tables_blob->len - ssdt; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2032 | |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2033 | acpi_add_table(table_offsets, tables_blob); |
| 2034 | build_madt(tables_blob, tables->linker, &cpu, guest_info); |
Michael S. Tsirkin | 9ac1c4c | 2014-04-28 08:15:32 +0300 | [diff] [blame] | 2035 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2036 | if (misc.has_hpet) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2037 | acpi_add_table(table_offsets, tables_blob); |
| 2038 | build_hpet(tables_blob, tables->linker); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2039 | } |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 2040 | if (misc.tpm_version != TPM_VERSION_UNSPEC) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2041 | acpi_add_table(table_offsets, tables_blob); |
| 2042 | build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 2043 | |
Igor Mammedov | 72d97b3 | 2015-06-09 05:31:53 +0200 | [diff] [blame] | 2044 | if (misc.tpm_version == TPM_VERSION_2_0) { |
| 2045 | acpi_add_table(table_offsets, tables_blob); |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 2046 | build_tpm2(tables_blob, tables->linker); |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 2047 | } |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 2048 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2049 | if (guest_info->numa_nodes) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2050 | acpi_add_table(table_offsets, tables_blob); |
| 2051 | build_srat(tables_blob, tables->linker, guest_info); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2052 | } |
| 2053 | if (acpi_get_mcfg(&mcfg)) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2054 | acpi_add_table(table_offsets, tables_blob); |
| 2055 | build_mcfg_q35(tables_blob, tables->linker, &mcfg); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2056 | } |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2057 | if (acpi_has_iommu()) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2058 | acpi_add_table(table_offsets, tables_blob); |
| 2059 | build_dmar_q35(tables_blob, tables->linker); |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2060 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2061 | |
Xiao Guangrong | 87252e1 | 2015-12-02 15:20:58 +0800 | [diff] [blame] | 2062 | if (acpi_has_nvdimm()) { |
| 2063 | nvdimm_build_acpi(table_offsets, tables_blob, tables->linker); |
| 2064 | } |
| 2065 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2066 | /* Add tables supplied by user (if any) */ |
| 2067 | for (u = acpi_table_first(); u; u = acpi_table_next(u)) { |
| 2068 | unsigned len = acpi_table_len(u); |
| 2069 | |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2070 | acpi_add_table(table_offsets, tables_blob); |
| 2071 | g_array_append_vals(tables_blob, u, len); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | /* RSDT is pointed to by RSDP */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2075 | rsdt = tables_blob->len; |
| 2076 | build_rsdt(tables_blob, tables->linker, table_offsets); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2077 | |
| 2078 | /* RSDP is in FSEG memory, so allocate it separately */ |
| 2079 | build_rsdp(tables->rsdp, tables->linker, rsdt); |
| 2080 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2081 | /* We'll expose it all to Guest so we want to reduce |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2082 | * chance of size changes. |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2083 | * |
| 2084 | * We used to align the tables to 4k, but of course this would |
| 2085 | * too simple to be enough. 4k turned out to be too small an |
| 2086 | * alignment very soon, and in fact it is almost impossible to |
| 2087 | * keep the table size stable for all (max_cpus, max_memory_slots) |
| 2088 | * combinations. So the table size is always 64k for pc-i440fx-2.1 |
| 2089 | * and we give an error if the table grows beyond that limit. |
| 2090 | * |
| 2091 | * We still have the problem of migrating from "-M pc-i440fx-2.0". For |
| 2092 | * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables |
| 2093 | * than 2.0 and we can always pad the smaller tables with zeros. We can |
| 2094 | * then use the exact size of the 2.0 tables. |
| 2095 | * |
| 2096 | * All this is for PIIX4, since QEMU 2.0 didn't support Q35 migration. |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2097 | */ |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2098 | if (guest_info->legacy_acpi_table_size) { |
| 2099 | /* Subtracting aml_len gives the size of fixed tables. Then add the |
| 2100 | * size of the PIIX4 DSDT/SSDT in QEMU 2.0. |
| 2101 | */ |
| 2102 | int legacy_aml_len = |
| 2103 | guest_info->legacy_acpi_table_size + |
| 2104 | ACPI_BUILD_LEGACY_CPU_AML_SIZE * max_cpus; |
| 2105 | int legacy_table_size = |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2106 | ROUND_UP(tables_blob->len - aml_len + legacy_aml_len, |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2107 | ACPI_BUILD_ALIGN_SIZE); |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2108 | if (tables_blob->len > legacy_table_size) { |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2109 | /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */ |
Michael S. Tsirkin | 868270f | 2014-07-28 23:07:11 +0200 | [diff] [blame] | 2110 | error_report("Warning: migration may not work."); |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2111 | } |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2112 | g_array_set_size(tables_blob, legacy_table_size); |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2113 | } else { |
Michael S. Tsirkin | 868270f | 2014-07-28 23:07:11 +0200 | [diff] [blame] | 2114 | /* Make sure we have a buffer in case we need to resize the tables. */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2115 | if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) { |
Paolo Bonzini | 18045fb | 2014-07-28 17:34:16 +0200 | [diff] [blame] | 2116 | /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */ |
Michael S. Tsirkin | 868270f | 2014-07-28 23:07:11 +0200 | [diff] [blame] | 2117 | error_report("Warning: ACPI tables are larger than 64k."); |
| 2118 | error_report("Warning: migration may not work."); |
| 2119 | error_report("Warning: please remove CPUs, NUMA nodes, " |
| 2120 | "memory slots or PCI bridges."); |
Paolo Bonzini | 18045fb | 2014-07-28 17:34:16 +0200 | [diff] [blame] | 2121 | } |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2122 | acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE); |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2123 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2124 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2125 | acpi_align_size(tables->linker, ACPI_BUILD_ALIGN_SIZE); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2126 | |
| 2127 | /* Cleanup memory that's no longer used. */ |
| 2128 | g_array_free(table_offsets, true); |
| 2129 | } |
| 2130 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2131 | static void acpi_ram_update(MemoryRegion *mr, GArray *data) |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2132 | { |
| 2133 | uint32_t size = acpi_data_len(data); |
| 2134 | |
| 2135 | /* Make sure RAM size is correct - in case it got changed e.g. by migration */ |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2136 | memory_region_ram_resize(mr, size, &error_abort); |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2137 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2138 | memcpy(memory_region_get_ram_ptr(mr), data->data, size); |
| 2139 | memory_region_set_dirty(mr, 0, size); |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
Gabriel L. Somlo | 3f8752b | 2015-11-05 09:32:49 -0500 | [diff] [blame] | 2142 | static void acpi_build_update(void *build_opaque) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2143 | { |
| 2144 | AcpiBuildState *build_state = build_opaque; |
| 2145 | AcpiBuildTables tables; |
| 2146 | |
| 2147 | /* No state to update or already patched? Nothing to do. */ |
| 2148 | if (!build_state || build_state->patched) { |
| 2149 | return; |
| 2150 | } |
| 2151 | build_state->patched = 1; |
| 2152 | |
| 2153 | acpi_build_tables_init(&tables); |
| 2154 | |
| 2155 | acpi_build(build_state->guest_info, &tables); |
| 2156 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2157 | acpi_ram_update(build_state->table_mr, tables.table_data); |
Michael S. Tsirkin | a166614 | 2014-11-17 07:51:50 +0200 | [diff] [blame] | 2158 | |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2159 | if (build_state->rsdp) { |
| 2160 | memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp)); |
| 2161 | } else { |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2162 | acpi_ram_update(build_state->rsdp_mr, tables.rsdp); |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2163 | } |
Michael S. Tsirkin | a166614 | 2014-11-17 07:51:50 +0200 | [diff] [blame] | 2164 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2165 | acpi_ram_update(build_state->linker_mr, tables.linker); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2166 | acpi_build_tables_cleanup(&tables, true); |
| 2167 | } |
| 2168 | |
| 2169 | static void acpi_build_reset(void *build_opaque) |
| 2170 | { |
| 2171 | AcpiBuildState *build_state = build_opaque; |
| 2172 | build_state->patched = 0; |
| 2173 | } |
| 2174 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2175 | static MemoryRegion *acpi_add_rom_blob(AcpiBuildState *build_state, |
| 2176 | GArray *blob, const char *name, |
| 2177 | uint64_t max_size) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2178 | { |
Michael S. Tsirkin | a166614 | 2014-11-17 07:51:50 +0200 | [diff] [blame] | 2179 | return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1, |
| 2180 | name, acpi_build_update, build_state); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | static const VMStateDescription vmstate_acpi_build = { |
| 2184 | .name = "acpi_build", |
| 2185 | .version_id = 1, |
| 2186 | .minimum_version_id = 1, |
Juan Quintela | d49805a | 2014-04-16 15:32:32 +0200 | [diff] [blame] | 2187 | .fields = (VMStateField[]) { |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2188 | VMSTATE_UINT8(patched, AcpiBuildState), |
| 2189 | VMSTATE_END_OF_LIST() |
| 2190 | }, |
| 2191 | }; |
| 2192 | |
| 2193 | void acpi_setup(PcGuestInfo *guest_info) |
| 2194 | { |
| 2195 | AcpiBuildTables tables; |
| 2196 | AcpiBuildState *build_state; |
| 2197 | |
| 2198 | if (!guest_info->fw_cfg) { |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2199 | ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2200 | return; |
| 2201 | } |
| 2202 | |
| 2203 | if (!guest_info->has_acpi_build) { |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2204 | ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2205 | return; |
| 2206 | } |
| 2207 | |
Michael S. Tsirkin | 81adc51 | 2013-11-07 14:12:05 +0200 | [diff] [blame] | 2208 | if (!acpi_enabled) { |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2209 | ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n"); |
Michael S. Tsirkin | 81adc51 | 2013-11-07 14:12:05 +0200 | [diff] [blame] | 2210 | return; |
| 2211 | } |
| 2212 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2213 | build_state = g_malloc0(sizeof *build_state); |
| 2214 | |
| 2215 | build_state->guest_info = guest_info; |
| 2216 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 2217 | acpi_set_pci_info(); |
| 2218 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2219 | acpi_build_tables_init(&tables); |
| 2220 | acpi_build(build_state->guest_info, &tables); |
| 2221 | |
| 2222 | /* Now expose it all to Guest */ |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2223 | build_state->table_mr = acpi_add_rom_blob(build_state, tables.table_data, |
Michael S. Tsirkin | a166614 | 2014-11-17 07:51:50 +0200 | [diff] [blame] | 2224 | ACPI_BUILD_TABLE_FILE, |
| 2225 | ACPI_BUILD_TABLE_MAX_SIZE); |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2226 | assert(build_state->table_mr != NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2227 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2228 | build_state->linker_mr = |
Igor Mammedov | 6e00619 | 2015-02-09 13:59:54 +0000 | [diff] [blame] | 2229 | acpi_add_rom_blob(build_state, tables.linker, "etc/table-loader", 0); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2230 | |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 2231 | fw_cfg_add_file(guest_info->fw_cfg, ACPI_BUILD_TPMLOG_FILE, |
| 2232 | tables.tcpalog->data, acpi_data_len(tables.tcpalog)); |
| 2233 | |
Michael S. Tsirkin | 384fb32 | 2015-02-17 10:04:40 +0100 | [diff] [blame] | 2234 | if (!guest_info->rsdp_in_ram) { |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2235 | /* |
| 2236 | * Keep for compatibility with old machine types. |
| 2237 | * Though RSDP is small, its contents isn't immutable, so |
Michael S. Tsirkin | afaa2e4 | 2015-02-17 10:40:30 +0100 | [diff] [blame] | 2238 | * we'll update it along with the rest of tables on guest access. |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2239 | */ |
Michael S. Tsirkin | afaa2e4 | 2015-02-17 10:40:30 +0100 | [diff] [blame] | 2240 | uint32_t rsdp_size = acpi_data_len(tables.rsdp); |
| 2241 | |
| 2242 | build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size); |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2243 | fw_cfg_add_file_callback(guest_info->fw_cfg, ACPI_BUILD_RSDP_FILE, |
| 2244 | acpi_build_update, build_state, |
Michael S. Tsirkin | afaa2e4 | 2015-02-17 10:40:30 +0100 | [diff] [blame] | 2245 | build_state->rsdp, rsdp_size); |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2246 | build_state->rsdp_mr = NULL; |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2247 | } else { |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2248 | build_state->rsdp = NULL; |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2249 | build_state->rsdp_mr = acpi_add_rom_blob(build_state, tables.rsdp, |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2250 | ACPI_BUILD_RSDP_FILE, 0); |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2251 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2252 | |
| 2253 | qemu_register_reset(acpi_build_reset, build_state); |
| 2254 | acpi_build_reset(build_state); |
| 2255 | vmstate_register(NULL, 0, &vmstate_acpi_build, build_state); |
| 2256 | |
| 2257 | /* Cleanup tables but don't free the memory: we track it |
| 2258 | * in build_state. |
| 2259 | */ |
| 2260 | acpi_build_tables_cleanup(&tables, false); |
| 2261 | } |