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 | |
Peter Maydell | b6a0aa0 | 2016-01-26 18:17:03 +0000 | [diff] [blame] | 23 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 24 | #include "qapi/error.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 25 | #include "qapi/qmp/qnum.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 26 | #include "acpi-build.h" |
Gerd Hoffmann | eb66ffa | 2020-05-20 15:19:47 +0200 | [diff] [blame] | 27 | #include "acpi-common.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 28 | #include "qemu/bitmap.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" |
Markus Armbruster | 2e5b09f | 2019-07-09 17:20:52 +0200 | [diff] [blame] | 31 | #include "hw/core/cpu.h" |
Thomas Huth | fcf5ef2 | 2016-10-11 08:56:52 +0200 | [diff] [blame] | 32 | #include "target/i386/cpu.h" |
Philippe Mathieu-Daudé | 0d5d8a3 | 2017-10-17 13:44:23 -0300 | [diff] [blame] | 33 | #include "hw/misc/pvpanic.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 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" |
Igor Mammedov | 679dd1a | 2016-06-15 11:25:23 +0200 | [diff] [blame] | 37 | #include "hw/acpi/cpu.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 38 | #include "hw/nvram/fw_cfg.h" |
Michael S. Tsirkin | 0058ae1 | 2015-01-19 23:58:55 +0200 | [diff] [blame] | 39 | #include "hw/acpi/bios-linker-loader.h" |
Gabriel L. Somlo | 15bce1b | 2013-12-22 10:34:56 -0500 | [diff] [blame] | 40 | #include "hw/isa/isa.h" |
Roman Kagan | 27b9fc5 | 2016-02-17 21:25:33 +0300 | [diff] [blame] | 41 | #include "hw/block/fdc.h" |
Igor Mammedov | bef3492 | 2014-06-02 15:25:26 +0200 | [diff] [blame] | 42 | #include "hw/acpi/memory_hotplug.h" |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 43 | #include "sysemu/tpm.h" |
| 44 | #include "hw/acpi/tpm.h" |
Ben Warren | d03637b | 2017-02-16 15:15:36 -0800 | [diff] [blame] | 45 | #include "hw/acpi/vmgenid.h" |
Like Xu | 0e11fc6 | 2019-05-19 04:54:25 +0800 | [diff] [blame] | 46 | #include "hw/boards.h" |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 47 | #include "sysemu/tpm_backend.h" |
Philippe Mathieu-Daudé | bcdb906 | 2019-10-04 01:03:53 +0200 | [diff] [blame] | 48 | #include "hw/rtc/mc146818rtc_regs.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 49 | #include "migration/vmstate.h" |
David Hildenbrand | 2cc0e2e | 2018-04-23 18:51:16 +0200 | [diff] [blame] | 50 | #include "hw/mem/memory-device.h" |
Philippe Mathieu-Daudé | 4b99769 | 2020-02-28 12:46:47 +0100 | [diff] [blame] | 51 | #include "hw/mem/nvdimm.h" |
Igor Mammedov | 1f3aba3 | 2016-06-16 14:23:48 +0200 | [diff] [blame] | 52 | #include "sysemu/numa.h" |
Markus Armbruster | 71e8a91 | 2019-08-12 07:23:38 +0200 | [diff] [blame] | 53 | #include "sysemu/reset.h" |
Jon Doron | 6775d15 | 2020-04-24 15:34:43 +0300 | [diff] [blame] | 54 | #include "hw/hyperv/vmbus-bridge.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 55 | |
| 56 | /* Supported chipsets: */ |
Philippe Mathieu-Daudé | fff123b | 2018-01-06 16:37:26 +0100 | [diff] [blame] | 57 | #include "hw/southbridge/piix.h" |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 58 | #include "hw/acpi/pcihp.h" |
Paolo Bonzini | 89a289c | 2019-12-12 14:14:40 +0100 | [diff] [blame] | 59 | #include "hw/i386/fw_cfg.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 60 | #include "hw/i386/ich9.h" |
| 61 | #include "hw/pci/pci_bus.h" |
| 62 | #include "hw/pci-host/q35.h" |
Peter Xu | 1cf5fd5 | 2016-07-14 13:56:12 +0800 | [diff] [blame] | 63 | #include "hw/i386/x86-iommu.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 64 | |
Igor Mammedov | 19934e0 | 2015-01-30 13:29:36 +0000 | [diff] [blame] | 65 | #include "hw/acpi/aml-build.h" |
Wei Yang | 82f76c6 | 2019-06-10 09:18:30 +0800 | [diff] [blame] | 66 | #include "hw/acpi/utils.h" |
Wei Yang | 48cefd9 | 2019-04-19 08:30:51 +0800 | [diff] [blame] | 67 | #include "hw/acpi/pci.h" |
Igor Mammedov | 19934e0 | 2015-01-30 13:29:36 +0000 | [diff] [blame] | 68 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 69 | #include "qom/qom-qobject.h" |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 70 | #include "hw/i386/amd_iommu.h" |
| 71 | #include "hw/i386/intel_iommu.h" |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 72 | |
Corey Minyard | 86e91dd | 2016-06-10 04:15:42 -0500 | [diff] [blame] | 73 | #include "hw/acpi/ipmi.h" |
Liu Jingqi | e6f123c | 2019-12-13 09:19:25 +0800 | [diff] [blame] | 74 | #include "hw/acpi/hmat.h" |
Corey Minyard | 86e91dd | 2016-06-10 04:15:42 -0500 | [diff] [blame] | 75 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 76 | /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and |
| 77 | * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows |
| 78 | * a little bit, there should be plenty of free space since the DSDT |
| 79 | * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1. |
| 80 | */ |
| 81 | #define ACPI_BUILD_LEGACY_CPU_AML_SIZE 97 |
| 82 | #define ACPI_BUILD_ALIGN_SIZE 0x1000 |
| 83 | |
Michael S. Tsirkin | 868270f | 2014-07-28 23:07:11 +0200 | [diff] [blame] | 84 | #define ACPI_BUILD_TABLE_SIZE 0x20000 |
Paolo Bonzini | 18045fb | 2014-07-28 17:34:16 +0200 | [diff] [blame] | 85 | |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 86 | /* #define DEBUG_ACPI_BUILD */ |
| 87 | #ifdef DEBUG_ACPI_BUILD |
| 88 | #define ACPI_BUILD_DPRINTF(fmt, ...) \ |
| 89 | do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0) |
| 90 | #else |
| 91 | #define ACPI_BUILD_DPRINTF(fmt, ...) |
| 92 | #endif |
| 93 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 94 | typedef struct AcpiPmInfo { |
| 95 | bool s3_disabled; |
| 96 | bool s4_disabled; |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 97 | bool pcihp_bridge_en; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 98 | uint8_t s4_val; |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 99 | AcpiFadtData fadt; |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 100 | uint16_t cpu_hp_io_base; |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 101 | uint16_t pcihp_io_base; |
| 102 | uint16_t pcihp_io_len; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 103 | } AcpiPmInfo; |
| 104 | |
| 105 | typedef struct AcpiMiscInfo { |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 106 | bool is_piix4; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 107 | bool has_hpet; |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 108 | TPMVersion tpm_version; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 109 | const unsigned char *dsdt_code; |
| 110 | unsigned dsdt_size; |
| 111 | uint16_t pvpanic_port; |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 112 | uint16_t applesmc_io_base; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 113 | } AcpiMiscInfo; |
| 114 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 115 | typedef struct AcpiBuildPciBusHotplugState { |
| 116 | GArray *device_table; |
| 117 | GArray *notify_table; |
| 118 | struct AcpiBuildPciBusHotplugState *parent; |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 119 | bool pcihp_bridge_en; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 120 | } AcpiBuildPciBusHotplugState; |
| 121 | |
Stefan Berger | 0fe2466 | 2019-01-15 02:27:51 +0400 | [diff] [blame] | 122 | typedef struct FwCfgTPMConfig { |
| 123 | uint32_t tpmppi_address; |
| 124 | uint8_t tpm_version; |
| 125 | uint8_t tpmppi_version; |
| 126 | } QEMU_PACKED FwCfgTPMConfig; |
| 127 | |
Gerd Hoffmann | 4a44183 | 2019-06-07 09:34:29 +0200 | [diff] [blame] | 128 | static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg); |
| 129 | |
Kwangwoo Lee | 5c94b82 | 2020-04-21 13:59:29 +0100 | [diff] [blame] | 130 | const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = { |
| 131 | .space_id = AML_AS_SYSTEM_IO, |
| 132 | .address = NVDIMM_ACPI_IO_BASE, |
| 133 | .bit_width = NVDIMM_ACPI_IO_LEN << 3 |
| 134 | }; |
| 135 | |
Like Xu | 0e11fc6 | 2019-05-19 04:54:25 +0800 | [diff] [blame] | 136 | static void init_common_fadt_data(MachineState *ms, Object *o, |
| 137 | AcpiFadtData *data) |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 138 | { |
| 139 | uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL); |
| 140 | AmlAddressSpace as = AML_AS_SYSTEM_IO; |
| 141 | AcpiFadtData fadt = { |
| 142 | .rev = 3, |
| 143 | .flags = |
| 144 | (1 << ACPI_FADT_F_WBINVD) | |
| 145 | (1 << ACPI_FADT_F_PROC_C1) | |
| 146 | (1 << ACPI_FADT_F_SLP_BUTTON) | |
| 147 | (1 << ACPI_FADT_F_RTC_S4) | |
| 148 | (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) | |
| 149 | /* APIC destination mode ("Flat Logical") has an upper limit of 8 |
| 150 | * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be |
| 151 | * used |
| 152 | */ |
Like Xu | 0e11fc6 | 2019-05-19 04:54:25 +0800 | [diff] [blame] | 153 | ((ms->smp.max_cpus > 8) ? |
| 154 | (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0), |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 155 | .int_model = 1 /* Multiple APIC */, |
| 156 | .rtc_century = RTC_CENTURY, |
| 157 | .plvl2_lat = 0xfff /* C2 state not supported */, |
| 158 | .plvl3_lat = 0xfff /* C3 state not supported */, |
| 159 | .smi_cmd = ACPI_PORT_SMI_CMD, |
| 160 | .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL), |
| 161 | .acpi_enable_cmd = |
| 162 | object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL), |
| 163 | .acpi_disable_cmd = |
| 164 | object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL), |
| 165 | .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io }, |
| 166 | .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8, |
| 167 | .address = io + 0x04 }, |
| 168 | .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 }, |
| 169 | .gpe0_blk = { .space_id = as, .bit_width = |
| 170 | object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8, |
| 171 | .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL) |
| 172 | }, |
| 173 | }; |
| 174 | *data = fadt; |
| 175 | } |
| 176 | |
Philippe Mathieu-Daudé | 81c48dd | 2019-04-27 16:40:24 +0200 | [diff] [blame] | 177 | static Object *object_resolve_type_unambiguous(const char *typename) |
| 178 | { |
| 179 | bool ambig; |
| 180 | Object *o = object_resolve_path_type("", typename, &ambig); |
| 181 | |
| 182 | if (ambig || !o) { |
| 183 | return NULL; |
| 184 | } |
| 185 | return o; |
| 186 | } |
| 187 | |
Like Xu | 0e11fc6 | 2019-05-19 04:54:25 +0800 | [diff] [blame] | 188 | static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 189 | { |
Philippe Mathieu-Daudé | 81c48dd | 2019-04-27 16:40:24 +0200 | [diff] [blame] | 190 | Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM); |
| 191 | Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE); |
Igor Mammedov | 697155c | 2018-02-28 15:23:47 +0100 | [diff] [blame] | 192 | Object *obj = piix ? piix : lpc; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 193 | QObject *o; |
Daniel P. Berrange | 94aaca6 | 2015-07-31 11:14:35 +0100 | [diff] [blame] | 194 | pm->cpu_hp_io_base = 0; |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 195 | pm->pcihp_io_base = 0; |
| 196 | pm->pcihp_io_len = 0; |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 197 | |
Philippe Mathieu-Daudé | 6fa5171 | 2019-04-27 16:40:25 +0200 | [diff] [blame] | 198 | assert(obj); |
Like Xu | a062859 | 2019-05-19 04:54:20 +0800 | [diff] [blame] | 199 | init_common_fadt_data(machine, obj, &pm->fadt); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 200 | if (piix) { |
Igor Mammedov | 3a3fcc7 | 2017-07-24 15:50:20 +0200 | [diff] [blame] | 201 | /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */ |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 202 | pm->fadt.rev = 1; |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 203 | pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE; |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 204 | pm->pcihp_io_base = |
Marc-André Lureau | 35f91e5 | 2017-06-07 20:36:19 +0400 | [diff] [blame] | 205 | object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL); |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 206 | pm->pcihp_io_len = |
Marc-André Lureau | 35f91e5 | 2017-06-07 20:36:19 +0400 | [diff] [blame] | 207 | object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 208 | } |
| 209 | if (lpc) { |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 210 | struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO, |
| 211 | .bit_width = 8, .address = ICH9_RST_CNT_IOPORT }; |
| 212 | pm->fadt.reset_reg = r; |
| 213 | pm->fadt.reset_val = 0xf; |
| 214 | pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP; |
Igor Mammedov | ddf1ec2 | 2015-02-18 19:14:44 +0000 | [diff] [blame] | 215 | pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 216 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 217 | |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 218 | /* The above need not be conditional on machine type because the reset port |
| 219 | * happens to be the same on PIIX (pc) and ICH9 (q35). */ |
Philippe Mathieu-Daudé | 0063454 | 2019-02-02 20:48:46 +0100 | [diff] [blame] | 220 | QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT); |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 221 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 222 | /* Fill in optional s3/s4 related properties */ |
| 223 | o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL); |
| 224 | if (o) { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 225 | pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 226 | } else { |
| 227 | pm->s3_disabled = false; |
| 228 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 229 | qobject_unref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 230 | o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL); |
| 231 | if (o) { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 232 | pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 233 | } else { |
| 234 | pm->s4_disabled = false; |
| 235 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 236 | qobject_unref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 237 | o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL); |
| 238 | if (o) { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 239 | pm->s4_val = qnum_get_uint(qobject_to(QNum, o)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 240 | } else { |
| 241 | pm->s4_val = false; |
| 242 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 243 | qobject_unref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 244 | |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 245 | pm->pcihp_bridge_en = |
| 246 | object_property_get_bool(obj, "acpi-pci-hotplug-with-bridge-support", |
| 247 | NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 248 | } |
| 249 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 250 | static void acpi_get_misc_info(AcpiMiscInfo *info) |
| 251 | { |
Philippe Mathieu-Daudé | 81c48dd | 2019-04-27 16:40:24 +0200 | [diff] [blame] | 252 | Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM); |
| 253 | Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE); |
Igor Mammedov | 3db119d | 2015-12-28 18:02:57 +0100 | [diff] [blame] | 254 | assert(!!piix != !!lpc); |
| 255 | |
| 256 | if (piix) { |
| 257 | info->is_piix4 = true; |
| 258 | } |
| 259 | if (lpc) { |
| 260 | info->is_piix4 = false; |
| 261 | } |
| 262 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 263 | info->has_hpet = hpet_find(); |
Marc-André Lureau | 3dfd5a2 | 2017-11-06 19:39:15 +0100 | [diff] [blame] | 264 | info->tpm_version = tpm_get_version(tpm_find()); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 265 | info->pvpanic_port = pvpanic_port(); |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 266 | info->applesmc_io_base = applesmc_port(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 267 | } |
| 268 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 269 | /* |
| 270 | * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE. |
| 271 | * On i386 arch we only have two pci hosts, so we can look only for them. |
| 272 | */ |
| 273 | static Object *acpi_get_i386_pci_host(void) |
| 274 | { |
| 275 | PCIHostState *host; |
| 276 | |
| 277 | host = OBJECT_CHECK(PCIHostState, |
| 278 | object_resolve_path("/machine/i440fx", NULL), |
| 279 | TYPE_PCI_HOST_BRIDGE); |
| 280 | if (!host) { |
| 281 | host = OBJECT_CHECK(PCIHostState, |
| 282 | object_resolve_path("/machine/q35", NULL), |
| 283 | TYPE_PCI_HOST_BRIDGE); |
| 284 | } |
| 285 | |
| 286 | return OBJECT(host); |
| 287 | } |
| 288 | |
Markus Armbruster | 01c9742 | 2016-06-15 19:56:31 +0200 | [diff] [blame] | 289 | static void acpi_get_pci_holes(Range *hole, Range *hole64) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 290 | { |
| 291 | Object *pci_host; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 292 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 293 | pci_host = acpi_get_i386_pci_host(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 294 | g_assert(pci_host); |
| 295 | |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 296 | range_set_bounds1(hole, |
Marc-André Lureau | 6055536 | 2017-06-07 20:36:21 +0400 | [diff] [blame] | 297 | object_property_get_uint(pci_host, |
| 298 | PCI_HOST_PROP_PCI_HOLE_START, |
| 299 | NULL), |
| 300 | object_property_get_uint(pci_host, |
| 301 | PCI_HOST_PROP_PCI_HOLE_END, |
| 302 | NULL)); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 303 | range_set_bounds1(hole64, |
Marc-André Lureau | 6055536 | 2017-06-07 20:36:21 +0400 | [diff] [blame] | 304 | object_property_get_uint(pci_host, |
| 305 | PCI_HOST_PROP_PCI_HOLE64_START, |
| 306 | NULL), |
| 307 | object_property_get_uint(pci_host, |
| 308 | PCI_HOST_PROP_PCI_HOLE64_END, |
| 309 | NULL)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 310 | } |
| 311 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 312 | static void acpi_align_size(GArray *blob, unsigned align) |
| 313 | { |
| 314 | /* Align size to multiple of given size. This reduces the chance |
| 315 | * we need to change size in the future (breaking cross version migration). |
| 316 | */ |
Michael S. Tsirkin | 134d42d | 2013-11-26 00:00:39 +0200 | [diff] [blame] | 317 | 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] | 318 | } |
| 319 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 320 | /* FACS */ |
| 321 | static void |
Wei Yang | 009180b | 2019-01-30 11:02:07 +0800 | [diff] [blame] | 322 | build_facs(GArray *table_data) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 323 | { |
| 324 | AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs); |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 325 | memcpy(&facs->signature, "FACS", 4); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 326 | facs->length = cpu_to_le32(sizeof(*facs)); |
| 327 | } |
| 328 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 329 | static void build_append_pcihp_notify_entry(Aml *method, int slot) |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 330 | { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 331 | Aml *if_ctx; |
| 332 | int32_t devfn = PCI_DEVFN(slot, 0); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 333 | |
Igor Mammedov | 5530427 | 2015-12-10 00:41:17 +0100 | [diff] [blame] | 334 | 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] | 335 | aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1))); |
| 336 | aml_append(method, if_ctx); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 337 | } |
| 338 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 339 | static void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus, |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 340 | bool pcihp_bridge_en) |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 341 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 342 | Aml *dev, *notify_method = NULL, *method; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 343 | QObject *bsel; |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 344 | PCIBus *sec; |
| 345 | int i; |
Igor Mammedov | 133a2da | 2014-07-28 17:34:18 +0200 | [diff] [blame] | 346 | |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 347 | bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL); |
| 348 | if (bsel) { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 349 | uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel)); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 350 | |
| 351 | aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val))); |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 352 | notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED); |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 353 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 354 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 355 | for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) { |
| 356 | DeviceClass *dc; |
| 357 | PCIDeviceClass *pc; |
| 358 | PCIDevice *pdev = bus->devices[i]; |
| 359 | int slot = PCI_SLOT(i); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 360 | bool hotplug_enabled_dev; |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 361 | bool bridge_in_acpi; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 362 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 363 | if (!pdev) { |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 364 | if (bsel) { /* add hotplug slots for non present devices */ |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 365 | dev = aml_device("S%.02X", PCI_DEVFN(slot, 0)); |
| 366 | aml_append(dev, aml_name_decl("_SUN", aml_int(slot))); |
| 367 | aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16))); |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 368 | method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 369 | aml_append(method, |
| 370 | aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN")) |
| 371 | ); |
| 372 | aml_append(dev, method); |
| 373 | aml_append(parent_scope, dev); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 374 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 375 | build_append_pcihp_notify_entry(notify_method, slot); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 376 | } |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 377 | continue; |
| 378 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 379 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 380 | pc = PCI_DEVICE_GET_CLASS(pdev); |
| 381 | dc = DEVICE_GET_CLASS(pdev); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 382 | |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 383 | /* When hotplug for bridges is enabled, bridges are |
| 384 | * described in ACPI separately (see build_pci_bus_end). |
| 385 | * In this case they aren't themselves hot-pluggable. |
Michael S. Tsirkin | a20275f | 2015-01-28 18:30:38 +0200 | [diff] [blame] | 386 | * Hotplugged bridges *are* hot-pluggable. |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 387 | */ |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 388 | bridge_in_acpi = pc->is_bridge && pcihp_bridge_en && |
| 389 | !DEVICE(pdev)->hotplugged; |
Michael S. Tsirkin | 093a35e | 2014-07-28 22:56:45 +0200 | [diff] [blame] | 390 | |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 391 | hotplug_enabled_dev = bsel && dc->hotpluggable && !bridge_in_acpi; |
| 392 | |
| 393 | if (pc->class_id == PCI_CLASS_BRIDGE_ISA) { |
| 394 | continue; |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 397 | /* start to compose PCI slot descriptor */ |
| 398 | dev = aml_device("S%.02X", PCI_DEVFN(slot, 0)); |
| 399 | aml_append(dev, aml_name_decl("_ADR", aml_int(slot << 16))); |
| 400 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 401 | if (pc->class_id == PCI_CLASS_DISPLAY_VGA) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 402 | /* add VGA specific AML methods */ |
| 403 | int s3d; |
| 404 | |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 405 | if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 406 | s3d = 3; |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 407 | } else { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 408 | s3d = 0; |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 409 | } |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 410 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 411 | method = aml_method("_S1D", 0, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 412 | aml_append(method, aml_return(aml_int(0))); |
| 413 | aml_append(dev, method); |
| 414 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 415 | method = aml_method("_S2D", 0, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 416 | aml_append(method, aml_return(aml_int(0))); |
| 417 | aml_append(dev, method); |
| 418 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 419 | method = aml_method("_S3D", 0, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 420 | aml_append(method, aml_return(aml_int(s3d))); |
| 421 | aml_append(dev, method); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 422 | } else if (hotplug_enabled_dev) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 423 | /* add _SUN/_EJ0 to make slot hotpluggable */ |
| 424 | aml_append(dev, aml_name_decl("_SUN", aml_int(slot))); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 425 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 426 | method = aml_method("_EJ0", 1, AML_NOTSERIALIZED); |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 427 | aml_append(method, |
| 428 | aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN")) |
| 429 | ); |
| 430 | aml_append(dev, method); |
| 431 | |
| 432 | if (bsel) { |
| 433 | build_append_pcihp_notify_entry(notify_method, slot); |
| 434 | } |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 435 | } else if (bridge_in_acpi) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 436 | /* |
| 437 | * device is coldplugged bridge, |
| 438 | * add child device descriptions into its scope |
| 439 | */ |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 440 | PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 441 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 442 | build_append_pci_bus_devices(dev, sec_bus, pcihp_bridge_en); |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 443 | } |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 444 | /* slot descriptor has been composed, add it into parent context */ |
| 445 | aml_append(parent_scope, dev); |
Michael S. Tsirkin | 8dcf525 | 2014-02-04 17:43:47 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (bsel) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 449 | aml_append(parent_scope, notify_method); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Append PCNT method to notify about events on local and child buses. |
| 453 | * Add unconditionally for root since DSDT expects it. |
| 454 | */ |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 455 | method = aml_method("PCNT", 0, AML_NOTSERIALIZED); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 456 | |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 457 | /* If bus supports hotplug select it and notify about local events */ |
| 458 | if (bsel) { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 459 | uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel)); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 460 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 461 | aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM"))); |
| 462 | aml_append(method, |
| 463 | aml_call2("DVNT", aml_name("PCIU"), aml_int(1) /* Device Check */) |
| 464 | ); |
| 465 | aml_append(method, |
| 466 | aml_call2("DVNT", aml_name("PCID"), aml_int(3)/* Eject Request */) |
| 467 | ); |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 468 | } |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 469 | |
Igor Mammedov | b23046a | 2015-02-20 18:22:16 +0000 | [diff] [blame] | 470 | /* Notify about child bus events in any case */ |
| 471 | if (pcihp_bridge_en) { |
| 472 | QLIST_FOREACH(sec, &bus->child, sibling) { |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 473 | int32_t devfn = sec->parent_dev->devfn; |
| 474 | |
Marcel Apfelbaum | c99cb18 | 2016-07-17 19:53:11 +0300 | [diff] [blame] | 475 | if (pci_bus_is_root(sec) || pci_bus_is_express(sec)) { |
| 476 | continue; |
| 477 | } |
| 478 | |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 479 | aml_append(method, aml_name("^S%.02X.PCNT", devfn)); |
Michael S. Tsirkin | 99fd437 | 2013-10-14 18:01:29 +0300 | [diff] [blame] | 480 | } |
| 481 | } |
Igor Mammedov | 62b52c2 | 2015-02-20 18:22:18 +0000 | [diff] [blame] | 482 | aml_append(parent_scope, method); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 483 | qobject_unref(bsel); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 484 | } |
| 485 | |
Igor Mammedov | 196e213 | 2015-12-28 18:02:42 +0100 | [diff] [blame] | 486 | /** |
| 487 | * build_prt_entry: |
| 488 | * @link_name: link name for PCI route entry |
| 489 | * |
| 490 | * build AML package containing a PCI route entry for @link_name |
| 491 | */ |
| 492 | static Aml *build_prt_entry(const char *link_name) |
| 493 | { |
| 494 | Aml *a_zero = aml_int(0); |
| 495 | Aml *pkg = aml_package(4); |
| 496 | aml_append(pkg, a_zero); |
| 497 | aml_append(pkg, a_zero); |
| 498 | aml_append(pkg, aml_name("%s", link_name)); |
| 499 | aml_append(pkg, a_zero); |
| 500 | return pkg; |
| 501 | } |
| 502 | |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 503 | /* |
| 504 | * initialize_route - Initialize the interrupt routing rule |
| 505 | * through a specific LINK: |
| 506 | * if (lnk_idx == idx) |
| 507 | * route using link 'link_name' |
| 508 | */ |
| 509 | static Aml *initialize_route(Aml *route, const char *link_name, |
| 510 | Aml *lnk_idx, int idx) |
| 511 | { |
| 512 | Aml *if_ctx = aml_if(aml_equal(lnk_idx, aml_int(idx))); |
Igor Mammedov | 196e213 | 2015-12-28 18:02:42 +0100 | [diff] [blame] | 513 | Aml *pkg = build_prt_entry(link_name); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 514 | |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 515 | aml_append(if_ctx, aml_store(pkg, route)); |
| 516 | |
| 517 | return if_ctx; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * build_prt - Define interrupt rounting rules |
| 522 | * |
| 523 | * Returns an array of 128 routes, one for each device, |
| 524 | * based on device location. |
| 525 | * The main goal is to equaly distribute the interrupts |
| 526 | * over the 4 existing ACPI links (works only for i440fx). |
| 527 | * The hash function is (slot + pin) & 3 -> "LNK[D|A|B|C]". |
| 528 | * |
| 529 | */ |
Igor Mammedov | 196e213 | 2015-12-28 18:02:42 +0100 | [diff] [blame] | 530 | static Aml *build_prt(bool is_pci0_prt) |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 531 | { |
| 532 | Aml *method, *while_ctx, *pin, *res; |
| 533 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 534 | method = aml_method("_PRT", 0, AML_NOTSERIALIZED); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 535 | res = aml_local(0); |
| 536 | pin = aml_local(1); |
| 537 | aml_append(method, aml_store(aml_package(128), res)); |
| 538 | aml_append(method, aml_store(aml_int(0), pin)); |
| 539 | |
| 540 | /* while (pin < 128) */ |
| 541 | while_ctx = aml_while(aml_lless(pin, aml_int(128))); |
| 542 | { |
| 543 | Aml *slot = aml_local(2); |
| 544 | Aml *lnk_idx = aml_local(3); |
| 545 | Aml *route = aml_local(4); |
| 546 | |
| 547 | /* slot = pin >> 2 */ |
| 548 | aml_append(while_ctx, |
Igor Mammedov | c360639 | 2015-12-10 00:41:06 +0100 | [diff] [blame] | 549 | aml_store(aml_shiftright(pin, aml_int(2), NULL), slot)); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 550 | /* lnk_idx = (slot + pin) & 3 */ |
| 551 | aml_append(while_ctx, |
Igor Mammedov | 5530427 | 2015-12-10 00:41:17 +0100 | [diff] [blame] | 552 | aml_store(aml_and(aml_add(pin, slot, NULL), aml_int(3), NULL), |
| 553 | lnk_idx)); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 554 | |
| 555 | /* route[2] = "LNK[D|A|B|C]", selection based on pin % 3 */ |
| 556 | aml_append(while_ctx, initialize_route(route, "LNKD", lnk_idx, 0)); |
Igor Mammedov | 196e213 | 2015-12-28 18:02:42 +0100 | [diff] [blame] | 557 | if (is_pci0_prt) { |
| 558 | Aml *if_device_1, *if_pin_4, *else_pin_4; |
| 559 | |
| 560 | /* device 1 is the power-management device, needs SCI */ |
| 561 | if_device_1 = aml_if(aml_equal(lnk_idx, aml_int(1))); |
| 562 | { |
| 563 | if_pin_4 = aml_if(aml_equal(pin, aml_int(4))); |
| 564 | { |
| 565 | aml_append(if_pin_4, |
| 566 | aml_store(build_prt_entry("LNKS"), route)); |
| 567 | } |
| 568 | aml_append(if_device_1, if_pin_4); |
| 569 | else_pin_4 = aml_else(); |
| 570 | { |
| 571 | aml_append(else_pin_4, |
| 572 | aml_store(build_prt_entry("LNKA"), route)); |
| 573 | } |
| 574 | aml_append(if_device_1, else_pin_4); |
| 575 | } |
| 576 | aml_append(while_ctx, if_device_1); |
| 577 | } else { |
| 578 | aml_append(while_ctx, initialize_route(route, "LNKA", lnk_idx, 1)); |
| 579 | } |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 580 | aml_append(while_ctx, initialize_route(route, "LNKB", lnk_idx, 2)); |
| 581 | aml_append(while_ctx, initialize_route(route, "LNKC", lnk_idx, 3)); |
| 582 | |
| 583 | /* route[0] = 0x[slot]FFFF */ |
| 584 | aml_append(while_ctx, |
Igor Mammedov | ca3df95 | 2015-12-10 00:41:16 +0100 | [diff] [blame] | 585 | aml_store(aml_or(aml_shiftleft(slot, aml_int(16)), aml_int(0xFFFF), |
| 586 | NULL), |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 587 | aml_index(route, aml_int(0)))); |
| 588 | /* route[1] = pin & 3 */ |
| 589 | aml_append(while_ctx, |
Igor Mammedov | 5530427 | 2015-12-10 00:41:17 +0100 | [diff] [blame] | 590 | aml_store(aml_and(pin, aml_int(3), NULL), |
| 591 | aml_index(route, aml_int(1)))); |
Marcel Apfelbaum | 0d8935e | 2015-06-02 14:23:02 +0300 | [diff] [blame] | 592 | /* res[pin] = route */ |
| 593 | aml_append(while_ctx, aml_store(route, aml_index(res, pin))); |
| 594 | /* pin++ */ |
| 595 | aml_append(while_ctx, aml_increment(pin)); |
| 596 | } |
| 597 | aml_append(method, while_ctx); |
| 598 | /* return res*/ |
| 599 | aml_append(method, aml_return(res)); |
| 600 | |
| 601 | return method; |
| 602 | } |
| 603 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 604 | typedef struct CrsRangeEntry { |
| 605 | uint64_t base; |
| 606 | uint64_t limit; |
| 607 | } CrsRangeEntry; |
| 608 | |
| 609 | static void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit) |
| 610 | { |
| 611 | CrsRangeEntry *entry; |
| 612 | |
| 613 | entry = g_malloc(sizeof(*entry)); |
| 614 | entry->base = base; |
| 615 | entry->limit = limit; |
| 616 | |
| 617 | g_ptr_array_add(ranges, entry); |
| 618 | } |
| 619 | |
| 620 | static void crs_range_free(gpointer data) |
| 621 | { |
| 622 | CrsRangeEntry *entry = (CrsRangeEntry *)data; |
| 623 | g_free(entry); |
| 624 | } |
| 625 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 626 | typedef struct CrsRangeSet { |
| 627 | GPtrArray *io_ranges; |
| 628 | GPtrArray *mem_ranges; |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 629 | GPtrArray *mem_64bit_ranges; |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 630 | } CrsRangeSet; |
| 631 | |
| 632 | static void crs_range_set_init(CrsRangeSet *range_set) |
| 633 | { |
| 634 | range_set->io_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
| 635 | range_set->mem_ranges = g_ptr_array_new_with_free_func(crs_range_free); |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 636 | range_set->mem_64bit_ranges = |
| 637 | g_ptr_array_new_with_free_func(crs_range_free); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void crs_range_set_free(CrsRangeSet *range_set) |
| 641 | { |
| 642 | g_ptr_array_free(range_set->io_ranges, true); |
| 643 | g_ptr_array_free(range_set->mem_ranges, true); |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 644 | g_ptr_array_free(range_set->mem_64bit_ranges, true); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 645 | } |
| 646 | |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 647 | static gint crs_range_compare(gconstpointer a, gconstpointer b) |
| 648 | { |
Evgeny Yakovlev | 21e2acd | 2019-07-18 19:14:23 +0300 | [diff] [blame] | 649 | CrsRangeEntry *entry_a = *(CrsRangeEntry **)a; |
| 650 | CrsRangeEntry *entry_b = *(CrsRangeEntry **)b; |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 651 | |
Evgeny Yakovlev | 21e2acd | 2019-07-18 19:14:23 +0300 | [diff] [blame] | 652 | if (entry_a->base < entry_b->base) { |
| 653 | return -1; |
| 654 | } else if (entry_a->base > entry_b->base) { |
| 655 | return 1; |
| 656 | } else { |
| 657 | return 0; |
| 658 | } |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * crs_replace_with_free_ranges - given the 'used' ranges within [start - end] |
| 663 | * interval, computes the 'free' ranges from the same interval. |
| 664 | * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function |
| 665 | * will return { [base - a1], [a2 - b1], [b2 - limit] }. |
| 666 | */ |
| 667 | static void crs_replace_with_free_ranges(GPtrArray *ranges, |
| 668 | uint64_t start, uint64_t end) |
| 669 | { |
Marc-André Lureau | 354fb47 | 2016-07-13 12:56:01 +0200 | [diff] [blame] | 670 | GPtrArray *free_ranges = g_ptr_array_new(); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 671 | uint64_t free_base = start; |
| 672 | int i; |
| 673 | |
| 674 | g_ptr_array_sort(ranges, crs_range_compare); |
| 675 | for (i = 0; i < ranges->len; i++) { |
| 676 | CrsRangeEntry *used = g_ptr_array_index(ranges, i); |
| 677 | |
| 678 | if (free_base < used->base) { |
| 679 | crs_range_insert(free_ranges, free_base, used->base - 1); |
| 680 | } |
| 681 | |
| 682 | free_base = used->limit + 1; |
| 683 | } |
| 684 | |
| 685 | if (free_base < end) { |
| 686 | crs_range_insert(free_ranges, free_base, end); |
| 687 | } |
| 688 | |
| 689 | g_ptr_array_set_size(ranges, 0); |
| 690 | for (i = 0; i < free_ranges->len; i++) { |
| 691 | g_ptr_array_add(ranges, g_ptr_array_index(free_ranges, i)); |
| 692 | } |
| 693 | |
Marc-André Lureau | 354fb47 | 2016-07-13 12:56:01 +0200 | [diff] [blame] | 694 | g_ptr_array_free(free_ranges, true); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 695 | } |
| 696 | |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 697 | /* |
| 698 | * crs_range_merge - merges adjacent ranges in the given array. |
| 699 | * Array elements are deleted and replaced with the merged ranges. |
| 700 | */ |
| 701 | static void crs_range_merge(GPtrArray *range) |
| 702 | { |
| 703 | GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free); |
| 704 | CrsRangeEntry *entry; |
| 705 | uint64_t range_base, range_limit; |
| 706 | int i; |
| 707 | |
| 708 | if (!range->len) { |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | g_ptr_array_sort(range, crs_range_compare); |
| 713 | |
| 714 | entry = g_ptr_array_index(range, 0); |
| 715 | range_base = entry->base; |
| 716 | range_limit = entry->limit; |
| 717 | for (i = 1; i < range->len; i++) { |
| 718 | entry = g_ptr_array_index(range, i); |
| 719 | if (entry->base - 1 == range_limit) { |
| 720 | range_limit = entry->limit; |
| 721 | } else { |
| 722 | crs_range_insert(tmp, range_base, range_limit); |
| 723 | range_base = entry->base; |
| 724 | range_limit = entry->limit; |
| 725 | } |
| 726 | } |
| 727 | crs_range_insert(tmp, range_base, range_limit); |
| 728 | |
| 729 | g_ptr_array_set_size(range, 0); |
| 730 | for (i = 0; i < tmp->len; i++) { |
| 731 | entry = g_ptr_array_index(tmp, i); |
| 732 | crs_range_insert(range, entry->base, entry->limit); |
| 733 | } |
| 734 | g_ptr_array_free(tmp, true); |
| 735 | } |
| 736 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 737 | static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set) |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 738 | { |
| 739 | Aml *crs = aml_resource_template(); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 740 | CrsRangeSet temp_range_set; |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 741 | CrsRangeEntry *entry; |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 742 | uint8_t max_bus = pci_bus_num(host->bus); |
| 743 | uint8_t type; |
| 744 | int devfn; |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 745 | int i; |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 746 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 747 | crs_range_set_init(&temp_range_set); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 748 | for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) { |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 749 | uint64_t range_base, range_limit; |
| 750 | PCIDevice *dev = host->bus->devices[devfn]; |
| 751 | |
| 752 | if (!dev) { |
| 753 | continue; |
| 754 | } |
| 755 | |
| 756 | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
| 757 | PCIIORegion *r = &dev->io_regions[i]; |
| 758 | |
| 759 | range_base = r->addr; |
| 760 | range_limit = r->addr + r->size - 1; |
| 761 | |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 762 | /* |
| 763 | * Work-around for old bioses |
| 764 | * that do not support multiple root buses |
| 765 | */ |
| 766 | if (!range_base || range_base > range_limit) { |
| 767 | continue; |
| 768 | } |
| 769 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 770 | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) { |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 771 | crs_range_insert(temp_range_set.io_ranges, |
| 772 | range_base, range_limit); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 773 | } else { /* "memory" */ |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 774 | crs_range_insert(temp_range_set.mem_ranges, |
| 775 | range_base, range_limit); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
| 780 | if (type == PCI_HEADER_TYPE_BRIDGE) { |
| 781 | uint8_t subordinate = dev->config[PCI_SUBORDINATE_BUS]; |
| 782 | if (subordinate > max_bus) { |
| 783 | max_bus = subordinate; |
| 784 | } |
| 785 | |
| 786 | range_base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO); |
| 787 | range_limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * Work-around for old bioses |
| 791 | * that do not support multiple root buses |
| 792 | */ |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 793 | if (range_base && range_base <= range_limit) { |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 794 | crs_range_insert(temp_range_set.io_ranges, |
| 795 | range_base, range_limit); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 796 | } |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 797 | |
| 798 | range_base = |
| 799 | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 800 | range_limit = |
| 801 | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 802 | |
| 803 | /* |
| 804 | * Work-around for old bioses |
| 805 | * that do not support multiple root buses |
| 806 | */ |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 807 | if (range_base && range_base <= range_limit) { |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 808 | uint64_t length = range_limit - range_base + 1; |
| 809 | if (range_limit <= UINT32_MAX && length <= UINT32_MAX) { |
| 810 | crs_range_insert(temp_range_set.mem_ranges, |
| 811 | range_base, range_limit); |
| 812 | } else { |
| 813 | crs_range_insert(temp_range_set.mem_64bit_ranges, |
| 814 | range_base, range_limit); |
| 815 | } |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 816 | } |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 817 | |
| 818 | range_base = |
| 819 | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); |
| 820 | range_limit = |
| 821 | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 822 | |
| 823 | /* |
| 824 | * Work-around for old bioses |
| 825 | * that do not support multiple root buses |
| 826 | */ |
Laszlo Ersek | 4ebc736 | 2015-06-11 02:37:59 +0200 | [diff] [blame] | 827 | if (range_base && range_base <= range_limit) { |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 828 | uint64_t length = range_limit - range_base + 1; |
| 829 | if (range_limit <= UINT32_MAX && length <= UINT32_MAX) { |
| 830 | crs_range_insert(temp_range_set.mem_ranges, |
| 831 | range_base, range_limit); |
| 832 | } else { |
| 833 | crs_range_insert(temp_range_set.mem_64bit_ranges, |
| 834 | range_base, range_limit); |
| 835 | } |
Marcel Apfelbaum | 0f6dd8e | 2015-06-02 14:23:11 +0300 | [diff] [blame] | 836 | } |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 840 | crs_range_merge(temp_range_set.io_ranges); |
| 841 | for (i = 0; i < temp_range_set.io_ranges->len; i++) { |
| 842 | entry = g_ptr_array_index(temp_range_set.io_ranges, i); |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 843 | aml_append(crs, |
| 844 | aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, |
| 845 | AML_POS_DECODE, AML_ENTIRE_RANGE, |
| 846 | 0, entry->base, entry->limit, 0, |
| 847 | entry->limit - entry->base + 1)); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 848 | crs_range_insert(range_set->io_ranges, entry->base, entry->limit); |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 849 | } |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 850 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 851 | crs_range_merge(temp_range_set.mem_ranges); |
| 852 | for (i = 0; i < temp_range_set.mem_ranges->len; i++) { |
| 853 | entry = g_ptr_array_index(temp_range_set.mem_ranges, i); |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 854 | aml_append(crs, |
| 855 | aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, |
| 856 | AML_MAX_FIXED, AML_NON_CACHEABLE, |
| 857 | AML_READ_WRITE, |
| 858 | 0, entry->base, entry->limit, 0, |
| 859 | entry->limit - entry->base + 1)); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 860 | crs_range_insert(range_set->mem_ranges, entry->base, entry->limit); |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 861 | } |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 862 | |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 863 | crs_range_merge(temp_range_set.mem_64bit_ranges); |
| 864 | for (i = 0; i < temp_range_set.mem_64bit_ranges->len; i++) { |
| 865 | entry = g_ptr_array_index(temp_range_set.mem_64bit_ranges, i); |
| 866 | aml_append(crs, |
| 867 | aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, |
| 868 | AML_MAX_FIXED, AML_NON_CACHEABLE, |
| 869 | AML_READ_WRITE, |
| 870 | 0, entry->base, entry->limit, 0, |
| 871 | entry->limit - entry->base + 1)); |
| 872 | crs_range_insert(range_set->mem_64bit_ranges, |
| 873 | entry->base, entry->limit); |
| 874 | } |
| 875 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 876 | crs_range_set_free(&temp_range_set); |
Marcel Apfelbaum | d7fd0e6 | 2015-11-26 18:00:26 +0200 | [diff] [blame] | 877 | |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 878 | aml_append(crs, |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 879 | 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] | 880 | 0, |
| 881 | pci_bus_num(host->bus), |
| 882 | max_bus, |
| 883 | 0, |
| 884 | max_bus - pci_bus_num(host->bus) + 1)); |
| 885 | |
| 886 | return crs; |
| 887 | } |
| 888 | |
Igor Mammedov | a57d708 | 2015-12-28 18:02:29 +0100 | [diff] [blame] | 889 | static void build_hpet_aml(Aml *table) |
| 890 | { |
| 891 | Aml *crs; |
| 892 | Aml *field; |
| 893 | Aml *method; |
| 894 | Aml *if_ctx; |
| 895 | Aml *scope = aml_scope("_SB"); |
| 896 | Aml *dev = aml_device("HPET"); |
| 897 | Aml *zero = aml_int(0); |
| 898 | Aml *id = aml_local(0); |
| 899 | Aml *period = aml_local(1); |
| 900 | |
| 901 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103"))); |
| 902 | aml_append(dev, aml_name_decl("_UID", zero)); |
| 903 | |
| 904 | aml_append(dev, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 905 | aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE), |
| 906 | HPET_LEN)); |
Igor Mammedov | a57d708 | 2015-12-28 18:02:29 +0100 | [diff] [blame] | 907 | field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE); |
| 908 | aml_append(field, aml_named_field("VEND", 32)); |
| 909 | aml_append(field, aml_named_field("PRD", 32)); |
| 910 | aml_append(dev, field); |
| 911 | |
| 912 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 913 | aml_append(method, aml_store(aml_name("VEND"), id)); |
| 914 | aml_append(method, aml_store(aml_name("PRD"), period)); |
| 915 | aml_append(method, aml_shiftright(id, aml_int(16), id)); |
| 916 | if_ctx = aml_if(aml_lor(aml_equal(id, zero), |
| 917 | aml_equal(id, aml_int(0xffff)))); |
| 918 | { |
| 919 | aml_append(if_ctx, aml_return(zero)); |
| 920 | } |
| 921 | aml_append(method, if_ctx); |
| 922 | |
| 923 | if_ctx = aml_if(aml_lor(aml_equal(period, zero), |
| 924 | aml_lgreater(period, aml_int(100000000)))); |
| 925 | { |
| 926 | aml_append(if_ctx, aml_return(zero)); |
| 927 | } |
| 928 | aml_append(method, if_ctx); |
| 929 | |
| 930 | aml_append(method, aml_return(aml_int(0x0F))); |
| 931 | aml_append(dev, method); |
| 932 | |
| 933 | crs = aml_resource_template(); |
| 934 | aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY)); |
| 935 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 936 | |
| 937 | aml_append(scope, dev); |
| 938 | aml_append(table, scope); |
| 939 | } |
| 940 | |
Jon Doron | 6775d15 | 2020-04-24 15:34:43 +0300 | [diff] [blame] | 941 | static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge) |
| 942 | { |
| 943 | Aml *dev; |
| 944 | Aml *method; |
| 945 | Aml *crs; |
| 946 | |
| 947 | dev = aml_device("VMBS"); |
| 948 | aml_append(dev, aml_name_decl("STA", aml_int(0xF))); |
| 949 | aml_append(dev, aml_name_decl("_HID", aml_string("VMBus"))); |
| 950 | aml_append(dev, aml_name_decl("_UID", aml_int(0x0))); |
| 951 | aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS"))); |
| 952 | |
| 953 | method = aml_method("_DIS", 0, AML_NOTSERIALIZED); |
| 954 | aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL), |
| 955 | aml_name("STA"))); |
| 956 | aml_append(dev, method); |
| 957 | |
| 958 | method = aml_method("_PS0", 0, AML_NOTSERIALIZED); |
| 959 | aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL), |
| 960 | aml_name("STA"))); |
| 961 | aml_append(dev, method); |
| 962 | |
| 963 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 964 | aml_append(method, aml_return(aml_name("STA"))); |
| 965 | aml_append(dev, method); |
| 966 | |
| 967 | aml_append(dev, aml_name_decl("_PS3", aml_int(0x0))); |
| 968 | |
| 969 | crs = aml_resource_template(); |
Jon Doron | 8f06f22 | 2020-06-17 19:09:02 +0300 | [diff] [blame] | 970 | aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq)); |
Jon Doron | 6775d15 | 2020-04-24 15:34:43 +0300 | [diff] [blame] | 971 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 972 | |
| 973 | return dev; |
| 974 | } |
| 975 | |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 976 | static void build_isa_devices_aml(Aml *table) |
| 977 | { |
Jon Doron | 6775d15 | 2020-04-24 15:34:43 +0300 | [diff] [blame] | 978 | VMBusBridge *vmbus_bridge = vmbus_bridge_find(); |
Corey Minyard | 86e91dd | 2016-06-10 04:15:42 -0500 | [diff] [blame] | 979 | bool ambiguous; |
Corey Minyard | 86e91dd | 2016-06-10 04:15:42 -0500 | [diff] [blame] | 980 | Object *obj = object_resolve_path_type("", TYPE_ISA_BUS, &ambiguous); |
Gerd Hoffmann | 13371f9 | 2020-06-19 11:19:01 +0200 | [diff] [blame] | 981 | Aml *scope; |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 982 | |
Gerd Hoffmann | 13371f9 | 2020-06-19 11:19:01 +0200 | [diff] [blame] | 983 | assert(obj && !ambiguous); |
| 984 | |
| 985 | scope = aml_scope("_SB.PCI0.ISA"); |
| 986 | build_acpi_ipmi_devices(scope, BUS(obj), "\\_SB.PCI0.ISA"); |
| 987 | isa_build_aml(ISA_BUS(obj), scope); |
Corey Minyard | 86e91dd | 2016-06-10 04:15:42 -0500 | [diff] [blame] | 988 | |
Jon Doron | 6775d15 | 2020-04-24 15:34:43 +0300 | [diff] [blame] | 989 | if (vmbus_bridge) { |
| 990 | aml_append(scope, build_vmbus_device_aml(vmbus_bridge)); |
| 991 | } |
| 992 | |
Igor Mammedov | ee13584 | 2015-12-28 18:02:31 +0100 | [diff] [blame] | 993 | aml_append(table, scope); |
| 994 | } |
| 995 | |
Igor Mammedov | 3892a2b | 2015-12-28 18:02:30 +0100 | [diff] [blame] | 996 | static void build_dbg_aml(Aml *table) |
| 997 | { |
| 998 | Aml *field; |
| 999 | Aml *method; |
| 1000 | Aml *while_ctx; |
| 1001 | Aml *scope = aml_scope("\\"); |
| 1002 | Aml *buf = aml_local(0); |
| 1003 | Aml *len = aml_local(1); |
| 1004 | Aml *idx = aml_local(2); |
| 1005 | |
| 1006 | aml_append(scope, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1007 | aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01)); |
Igor Mammedov | 3892a2b | 2015-12-28 18:02:30 +0100 | [diff] [blame] | 1008 | field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
| 1009 | aml_append(field, aml_named_field("DBGB", 8)); |
| 1010 | aml_append(scope, field); |
| 1011 | |
| 1012 | method = aml_method("DBUG", 1, AML_NOTSERIALIZED); |
| 1013 | |
| 1014 | aml_append(method, aml_to_hexstring(aml_arg(0), buf)); |
| 1015 | aml_append(method, aml_to_buffer(buf, buf)); |
| 1016 | aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len)); |
| 1017 | aml_append(method, aml_store(aml_int(0), idx)); |
| 1018 | |
| 1019 | while_ctx = aml_while(aml_lless(idx, len)); |
| 1020 | aml_append(while_ctx, |
| 1021 | aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB"))); |
| 1022 | aml_append(while_ctx, aml_increment(idx)); |
| 1023 | aml_append(method, while_ctx); |
| 1024 | |
| 1025 | aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB"))); |
| 1026 | aml_append(scope, method); |
| 1027 | |
| 1028 | aml_append(table, scope); |
| 1029 | } |
| 1030 | |
Igor Mammedov | c35b6e8 | 2015-12-28 18:02:39 +0100 | [diff] [blame] | 1031 | static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg) |
| 1032 | { |
| 1033 | Aml *dev; |
| 1034 | Aml *crs; |
| 1035 | Aml *method; |
| 1036 | uint32_t irqs[] = {5, 10, 11}; |
| 1037 | |
| 1038 | dev = aml_device("%s", name); |
| 1039 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F"))); |
| 1040 | aml_append(dev, aml_name_decl("_UID", aml_int(uid))); |
| 1041 | |
| 1042 | crs = aml_resource_template(); |
| 1043 | aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, |
| 1044 | AML_SHARED, irqs, ARRAY_SIZE(irqs))); |
| 1045 | aml_append(dev, aml_name_decl("_PRS", crs)); |
| 1046 | |
| 1047 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1048 | aml_append(method, aml_return(aml_call1("IQST", reg))); |
| 1049 | aml_append(dev, method); |
| 1050 | |
| 1051 | method = aml_method("_DIS", 0, AML_NOTSERIALIZED); |
| 1052 | aml_append(method, aml_or(reg, aml_int(0x80), reg)); |
| 1053 | aml_append(dev, method); |
| 1054 | |
| 1055 | method = aml_method("_CRS", 0, AML_NOTSERIALIZED); |
| 1056 | aml_append(method, aml_return(aml_call1("IQCR", reg))); |
| 1057 | aml_append(dev, method); |
| 1058 | |
| 1059 | method = aml_method("_SRS", 1, AML_NOTSERIALIZED); |
| 1060 | aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI")); |
| 1061 | aml_append(method, aml_store(aml_name("PRRI"), reg)); |
| 1062 | aml_append(dev, method); |
| 1063 | |
| 1064 | return dev; |
| 1065 | } |
| 1066 | |
Igor Mammedov | 80b32df | 2015-12-28 18:02:45 +0100 | [diff] [blame] | 1067 | static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi) |
| 1068 | { |
| 1069 | Aml *dev; |
| 1070 | Aml *crs; |
| 1071 | Aml *method; |
| 1072 | uint32_t irqs; |
| 1073 | |
| 1074 | dev = aml_device("%s", name); |
| 1075 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F"))); |
| 1076 | aml_append(dev, aml_name_decl("_UID", aml_int(uid))); |
| 1077 | |
| 1078 | crs = aml_resource_template(); |
| 1079 | irqs = gsi; |
| 1080 | aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH, |
| 1081 | AML_SHARED, &irqs, 1)); |
| 1082 | aml_append(dev, aml_name_decl("_PRS", crs)); |
| 1083 | |
| 1084 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1085 | |
Marcel Apfelbaum | c82f503 | 2016-03-07 21:14:37 +0200 | [diff] [blame] | 1086 | /* |
| 1087 | * _DIS can be no-op because the interrupt cannot be disabled. |
| 1088 | */ |
| 1089 | method = aml_method("_DIS", 0, AML_NOTSERIALIZED); |
| 1090 | aml_append(dev, method); |
| 1091 | |
Igor Mammedov | 80b32df | 2015-12-28 18:02:45 +0100 | [diff] [blame] | 1092 | method = aml_method("_SRS", 1, AML_NOTSERIALIZED); |
| 1093 | aml_append(dev, method); |
| 1094 | |
| 1095 | return dev; |
| 1096 | } |
| 1097 | |
Igor Mammedov | 16682a9 | 2015-12-28 18:02:47 +0100 | [diff] [blame] | 1098 | /* _CRS method - get current settings */ |
| 1099 | static Aml *build_iqcr_method(bool is_piix4) |
| 1100 | { |
| 1101 | Aml *if_ctx; |
| 1102 | uint32_t irqs; |
| 1103 | Aml *method = aml_method("IQCR", 1, AML_SERIALIZED); |
| 1104 | Aml *crs = aml_resource_template(); |
| 1105 | |
| 1106 | irqs = 0; |
| 1107 | aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, |
| 1108 | AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1)); |
| 1109 | aml_append(method, aml_name_decl("PRR0", crs)); |
| 1110 | |
| 1111 | aml_append(method, |
| 1112 | aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI")); |
| 1113 | |
| 1114 | if (is_piix4) { |
| 1115 | if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80))); |
| 1116 | aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI"))); |
| 1117 | aml_append(method, if_ctx); |
| 1118 | } else { |
| 1119 | aml_append(method, |
| 1120 | aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL), |
| 1121 | aml_name("PRRI"))); |
| 1122 | } |
| 1123 | |
| 1124 | aml_append(method, aml_return(aml_name("PRR0"))); |
| 1125 | return method; |
| 1126 | } |
| 1127 | |
Igor Mammedov | 78e1ad0 | 2015-12-28 18:02:48 +0100 | [diff] [blame] | 1128 | /* _STA method - get status */ |
| 1129 | static Aml *build_irq_status_method(void) |
| 1130 | { |
| 1131 | Aml *if_ctx; |
| 1132 | Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED); |
| 1133 | |
| 1134 | if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL)); |
| 1135 | aml_append(if_ctx, aml_return(aml_int(0x09))); |
| 1136 | aml_append(method, if_ctx); |
| 1137 | aml_append(method, aml_return(aml_int(0x0B))); |
| 1138 | return method; |
| 1139 | } |
| 1140 | |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1141 | static void build_piix4_pci0_int(Aml *table) |
| 1142 | { |
Igor Mammedov | c35b6e8 | 2015-12-28 18:02:39 +0100 | [diff] [blame] | 1143 | Aml *dev; |
| 1144 | Aml *crs; |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1145 | Aml *field; |
Igor Mammedov | c35b6e8 | 2015-12-28 18:02:39 +0100 | [diff] [blame] | 1146 | Aml *method; |
| 1147 | uint32_t irqs; |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1148 | Aml *sb_scope = aml_scope("_SB"); |
Igor Mammedov | 196e213 | 2015-12-28 18:02:42 +0100 | [diff] [blame] | 1149 | Aml *pci0_scope = aml_scope("PCI0"); |
| 1150 | |
| 1151 | aml_append(pci0_scope, build_prt(true)); |
| 1152 | aml_append(sb_scope, pci0_scope); |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1153 | |
| 1154 | field = aml_field("PCI0.ISA.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
| 1155 | aml_append(field, aml_named_field("PRQ0", 8)); |
| 1156 | aml_append(field, aml_named_field("PRQ1", 8)); |
| 1157 | aml_append(field, aml_named_field("PRQ2", 8)); |
| 1158 | aml_append(field, aml_named_field("PRQ3", 8)); |
| 1159 | aml_append(sb_scope, field); |
| 1160 | |
Igor Mammedov | 78e1ad0 | 2015-12-28 18:02:48 +0100 | [diff] [blame] | 1161 | aml_append(sb_scope, build_irq_status_method()); |
Igor Mammedov | 16682a9 | 2015-12-28 18:02:47 +0100 | [diff] [blame] | 1162 | aml_append(sb_scope, build_iqcr_method(true)); |
Igor Mammedov | 100681c | 2015-12-28 18:02:40 +0100 | [diff] [blame] | 1163 | |
Igor Mammedov | c35b6e8 | 2015-12-28 18:02:39 +0100 | [diff] [blame] | 1164 | aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0"))); |
| 1165 | aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1"))); |
| 1166 | aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2"))); |
| 1167 | aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3"))); |
| 1168 | |
| 1169 | dev = aml_device("LNKS"); |
| 1170 | { |
| 1171 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F"))); |
| 1172 | aml_append(dev, aml_name_decl("_UID", aml_int(4))); |
| 1173 | |
| 1174 | crs = aml_resource_template(); |
| 1175 | irqs = 9; |
| 1176 | aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, |
| 1177 | AML_ACTIVE_HIGH, AML_SHARED, |
| 1178 | &irqs, 1)); |
| 1179 | aml_append(dev, aml_name_decl("_PRS", crs)); |
| 1180 | |
| 1181 | /* The SCI cannot be disabled and is always attached to GSI 9, |
| 1182 | * so these are no-ops. We only need this link to override the |
| 1183 | * polarity to active high and match the content of the MADT. |
| 1184 | */ |
| 1185 | method = aml_method("_STA", 0, AML_NOTSERIALIZED); |
| 1186 | aml_append(method, aml_return(aml_int(0x0b))); |
| 1187 | aml_append(dev, method); |
| 1188 | |
| 1189 | method = aml_method("_DIS", 0, AML_NOTSERIALIZED); |
| 1190 | aml_append(dev, method); |
| 1191 | |
| 1192 | method = aml_method("_CRS", 0, AML_NOTSERIALIZED); |
| 1193 | aml_append(method, aml_return(aml_name("_PRS"))); |
| 1194 | aml_append(dev, method); |
| 1195 | |
| 1196 | method = aml_method("_SRS", 1, AML_NOTSERIALIZED); |
| 1197 | aml_append(dev, method); |
| 1198 | } |
| 1199 | aml_append(sb_scope, dev); |
| 1200 | |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1201 | aml_append(table, sb_scope); |
| 1202 | } |
| 1203 | |
Igor Mammedov | 22b5b8b | 2015-12-28 18:02:51 +0100 | [diff] [blame] | 1204 | static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name) |
| 1205 | { |
| 1206 | int i; |
| 1207 | int head; |
| 1208 | Aml *pkg; |
| 1209 | char base = name[3] < 'E' ? 'A' : 'E'; |
| 1210 | char *s = g_strdup(name); |
| 1211 | Aml *a_nr = aml_int((nr << 16) | 0xffff); |
| 1212 | |
| 1213 | assert(strlen(s) == 4); |
| 1214 | |
| 1215 | head = name[3] - base; |
| 1216 | for (i = 0; i < 4; i++) { |
| 1217 | if (head + i > 3) { |
| 1218 | head = i * -1; |
| 1219 | } |
| 1220 | s[3] = base + head + i; |
| 1221 | pkg = aml_package(4); |
| 1222 | aml_append(pkg, a_nr); |
| 1223 | aml_append(pkg, aml_int(i)); |
| 1224 | aml_append(pkg, aml_name("%s", s)); |
| 1225 | aml_append(pkg, aml_int(0)); |
| 1226 | aml_append(ctx, pkg); |
| 1227 | } |
| 1228 | g_free(s); |
| 1229 | } |
| 1230 | |
| 1231 | static Aml *build_q35_routing_table(const char *str) |
| 1232 | { |
| 1233 | int i; |
| 1234 | Aml *pkg; |
| 1235 | char *name = g_strdup_printf("%s ", str); |
| 1236 | |
| 1237 | pkg = aml_package(128); |
| 1238 | for (i = 0; i < 0x18; i++) { |
| 1239 | name[3] = 'E' + (i & 0x3); |
| 1240 | append_q35_prt_entry(pkg, i, name); |
| 1241 | } |
| 1242 | |
| 1243 | name[3] = 'E'; |
| 1244 | append_q35_prt_entry(pkg, 0x18, name); |
| 1245 | |
| 1246 | /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */ |
| 1247 | for (i = 0x0019; i < 0x1e; i++) { |
| 1248 | name[3] = 'A'; |
| 1249 | append_q35_prt_entry(pkg, i, name); |
| 1250 | } |
| 1251 | |
| 1252 | /* PCIe->PCI bridge. use PIRQ[E-H] */ |
| 1253 | name[3] = 'E'; |
| 1254 | append_q35_prt_entry(pkg, 0x1e, name); |
| 1255 | name[3] = 'A'; |
| 1256 | append_q35_prt_entry(pkg, 0x1f, name); |
| 1257 | |
| 1258 | g_free(name); |
| 1259 | return pkg; |
| 1260 | } |
| 1261 | |
Igor Mammedov | 80b32df | 2015-12-28 18:02:45 +0100 | [diff] [blame] | 1262 | static void build_q35_pci0_int(Aml *table) |
| 1263 | { |
Igor Mammedov | 41f95a5 | 2015-12-28 18:02:49 +0100 | [diff] [blame] | 1264 | Aml *field; |
Igor Mammedov | 0dafe3b | 2015-12-28 18:02:50 +0100 | [diff] [blame] | 1265 | Aml *method; |
Igor Mammedov | 80b32df | 2015-12-28 18:02:45 +0100 | [diff] [blame] | 1266 | Aml *sb_scope = aml_scope("_SB"); |
Igor Mammedov | 0dafe3b | 2015-12-28 18:02:50 +0100 | [diff] [blame] | 1267 | Aml *pci0_scope = aml_scope("PCI0"); |
| 1268 | |
Igor Mammedov | e9fce79 | 2015-12-28 18:02:53 +0100 | [diff] [blame] | 1269 | /* Zero => PIC mode, One => APIC Mode */ |
| 1270 | aml_append(table, aml_name_decl("PICF", aml_int(0))); |
| 1271 | method = aml_method("_PIC", 1, AML_NOTSERIALIZED); |
| 1272 | { |
| 1273 | aml_append(method, aml_store(aml_arg(0), aml_name("PICF"))); |
| 1274 | } |
| 1275 | aml_append(table, method); |
| 1276 | |
Igor Mammedov | 22b5b8b | 2015-12-28 18:02:51 +0100 | [diff] [blame] | 1277 | aml_append(pci0_scope, |
Igor Mammedov | 65aef4d | 2015-12-28 18:02:52 +0100 | [diff] [blame] | 1278 | aml_name_decl("PRTP", build_q35_routing_table("LNK"))); |
| 1279 | aml_append(pci0_scope, |
Igor Mammedov | 22b5b8b | 2015-12-28 18:02:51 +0100 | [diff] [blame] | 1280 | aml_name_decl("PRTA", build_q35_routing_table("GSI"))); |
| 1281 | |
Igor Mammedov | 0dafe3b | 2015-12-28 18:02:50 +0100 | [diff] [blame] | 1282 | method = aml_method("_PRT", 0, AML_NOTSERIALIZED); |
| 1283 | { |
| 1284 | Aml *if_ctx; |
| 1285 | Aml *else_ctx; |
| 1286 | |
| 1287 | /* PCI IRQ routing table, example from ACPI 2.0a specification, |
| 1288 | section 6.2.8.1 */ |
| 1289 | /* Note: we provide the same info as the PCI routing |
| 1290 | table of the Bochs BIOS */ |
| 1291 | if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0))); |
| 1292 | aml_append(if_ctx, aml_return(aml_name("PRTP"))); |
| 1293 | aml_append(method, if_ctx); |
| 1294 | else_ctx = aml_else(); |
| 1295 | aml_append(else_ctx, aml_return(aml_name("PRTA"))); |
| 1296 | aml_append(method, else_ctx); |
| 1297 | } |
| 1298 | aml_append(pci0_scope, method); |
| 1299 | aml_append(sb_scope, pci0_scope); |
Igor Mammedov | 80b32df | 2015-12-28 18:02:45 +0100 | [diff] [blame] | 1300 | |
Igor Mammedov | 41f95a5 | 2015-12-28 18:02:49 +0100 | [diff] [blame] | 1301 | field = aml_field("PCI0.ISA.PIRQ", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
| 1302 | aml_append(field, aml_named_field("PRQA", 8)); |
| 1303 | aml_append(field, aml_named_field("PRQB", 8)); |
| 1304 | aml_append(field, aml_named_field("PRQC", 8)); |
| 1305 | aml_append(field, aml_named_field("PRQD", 8)); |
| 1306 | aml_append(field, aml_reserved_field(0x20)); |
| 1307 | aml_append(field, aml_named_field("PRQE", 8)); |
| 1308 | aml_append(field, aml_named_field("PRQF", 8)); |
| 1309 | aml_append(field, aml_named_field("PRQG", 8)); |
| 1310 | aml_append(field, aml_named_field("PRQH", 8)); |
| 1311 | aml_append(sb_scope, field); |
| 1312 | |
Igor Mammedov | 78e1ad0 | 2015-12-28 18:02:48 +0100 | [diff] [blame] | 1313 | aml_append(sb_scope, build_irq_status_method()); |
Igor Mammedov | 16682a9 | 2015-12-28 18:02:47 +0100 | [diff] [blame] | 1314 | aml_append(sb_scope, build_iqcr_method(false)); |
| 1315 | |
Igor Mammedov | 12e3b1f | 2015-12-28 18:02:46 +0100 | [diff] [blame] | 1316 | aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA"))); |
| 1317 | aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB"))); |
| 1318 | aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC"))); |
| 1319 | aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD"))); |
| 1320 | aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE"))); |
| 1321 | aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF"))); |
| 1322 | aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG"))); |
| 1323 | aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH"))); |
| 1324 | |
Marcel Apfelbaum | 6a991e0 | 2016-03-13 13:40:29 +0200 | [diff] [blame] | 1325 | aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10)); |
| 1326 | aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11)); |
| 1327 | aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12)); |
| 1328 | aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13)); |
| 1329 | aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14)); |
| 1330 | aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15)); |
| 1331 | aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16)); |
| 1332 | aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17)); |
Igor Mammedov | 80b32df | 2015-12-28 18:02:45 +0100 | [diff] [blame] | 1333 | |
| 1334 | aml_append(table, sb_scope); |
| 1335 | } |
| 1336 | |
Igor Mammedov | 41f95a5 | 2015-12-28 18:02:49 +0100 | [diff] [blame] | 1337 | static void build_q35_isa_bridge(Aml *table) |
| 1338 | { |
| 1339 | Aml *dev; |
| 1340 | Aml *scope; |
Igor Mammedov | 41f95a5 | 2015-12-28 18:02:49 +0100 | [diff] [blame] | 1341 | |
| 1342 | scope = aml_scope("_SB.PCI0"); |
| 1343 | dev = aml_device("ISA"); |
| 1344 | aml_append(dev, aml_name_decl("_ADR", aml_int(0x001F0000))); |
| 1345 | |
| 1346 | /* ICH9 PCI to ISA irq remapping */ |
| 1347 | aml_append(dev, aml_operation_region("PIRQ", AML_PCI_CONFIG, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1348 | aml_int(0x60), 0x0C)); |
Igor Mammedov | 41f95a5 | 2015-12-28 18:02:49 +0100 | [diff] [blame] | 1349 | |
Igor Mammedov | 41f95a5 | 2015-12-28 18:02:49 +0100 | [diff] [blame] | 1350 | aml_append(scope, dev); |
| 1351 | aml_append(table, scope); |
| 1352 | } |
| 1353 | |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1354 | static void build_piix4_isa_bridge(Aml *table) |
| 1355 | { |
| 1356 | Aml *dev; |
| 1357 | Aml *scope; |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1358 | |
| 1359 | scope = aml_scope("_SB.PCI0"); |
| 1360 | dev = aml_device("ISA"); |
| 1361 | aml_append(dev, aml_name_decl("_ADR", aml_int(0x00010000))); |
| 1362 | |
| 1363 | /* PIIX PCI to ISA irq remapping */ |
| 1364 | aml_append(dev, aml_operation_region("P40C", AML_PCI_CONFIG, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1365 | aml_int(0x60), 0x04)); |
Igor Mammedov | e4db279 | 2015-12-28 18:02:37 +0100 | [diff] [blame] | 1366 | |
| 1367 | aml_append(scope, dev); |
| 1368 | aml_append(table, scope); |
| 1369 | } |
| 1370 | |
Igor Mammedov | b616ec4 | 2015-12-28 18:02:43 +0100 | [diff] [blame] | 1371 | static void build_piix4_pci_hotplug(Aml *table) |
| 1372 | { |
| 1373 | Aml *scope; |
| 1374 | Aml *field; |
| 1375 | Aml *method; |
| 1376 | |
| 1377 | scope = aml_scope("_SB.PCI0"); |
| 1378 | |
| 1379 | aml_append(scope, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1380 | aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(0xae00), 0x08)); |
Igor Mammedov | b616ec4 | 2015-12-28 18:02:43 +0100 | [diff] [blame] | 1381 | field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS); |
| 1382 | aml_append(field, aml_named_field("PCIU", 32)); |
| 1383 | aml_append(field, aml_named_field("PCID", 32)); |
| 1384 | aml_append(scope, field); |
| 1385 | |
| 1386 | aml_append(scope, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1387 | aml_operation_region("SEJ", AML_SYSTEM_IO, aml_int(0xae08), 0x04)); |
Igor Mammedov | b616ec4 | 2015-12-28 18:02:43 +0100 | [diff] [blame] | 1388 | field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS); |
| 1389 | aml_append(field, aml_named_field("B0EJ", 32)); |
| 1390 | aml_append(scope, field); |
| 1391 | |
| 1392 | aml_append(scope, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1393 | aml_operation_region("BNMR", AML_SYSTEM_IO, aml_int(0xae10), 0x04)); |
Igor Mammedov | b616ec4 | 2015-12-28 18:02:43 +0100 | [diff] [blame] | 1394 | field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS); |
| 1395 | aml_append(field, aml_named_field("BNUM", 32)); |
| 1396 | aml_append(scope, field); |
| 1397 | |
| 1398 | aml_append(scope, aml_mutex("BLCK", 0)); |
| 1399 | |
| 1400 | method = aml_method("PCEJ", 2, AML_NOTSERIALIZED); |
| 1401 | aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF)); |
| 1402 | aml_append(method, aml_store(aml_arg(0), aml_name("BNUM"))); |
| 1403 | aml_append(method, |
| 1404 | aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ"))); |
| 1405 | aml_append(method, aml_release(aml_name("BLCK"))); |
| 1406 | aml_append(method, aml_return(aml_int(0))); |
| 1407 | aml_append(scope, method); |
| 1408 | |
| 1409 | aml_append(table, scope); |
| 1410 | } |
| 1411 | |
Igor Mammedov | f97a88a | 2015-12-28 18:02:54 +0100 | [diff] [blame] | 1412 | static Aml *build_q35_osc_method(void) |
| 1413 | { |
| 1414 | Aml *if_ctx; |
| 1415 | Aml *if_ctx2; |
| 1416 | Aml *else_ctx; |
| 1417 | Aml *method; |
| 1418 | Aml *a_cwd1 = aml_name("CDW1"); |
Michael S. Tsirkin | b3c782d | 2017-02-28 16:13:28 +0200 | [diff] [blame] | 1419 | Aml *a_ctrl = aml_local(0); |
Igor Mammedov | f97a88a | 2015-12-28 18:02:54 +0100 | [diff] [blame] | 1420 | |
| 1421 | method = aml_method("_OSC", 4, AML_NOTSERIALIZED); |
| 1422 | aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1")); |
| 1423 | |
| 1424 | if_ctx = aml_if(aml_equal( |
| 1425 | aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766"))); |
| 1426 | aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2")); |
| 1427 | aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3")); |
| 1428 | |
Igor Mammedov | f97a88a | 2015-12-28 18:02:54 +0100 | [diff] [blame] | 1429 | aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl)); |
| 1430 | |
| 1431 | /* |
| 1432 | * Always allow native PME, AER (no dependencies) |
Aleksandr Bezzubikov | a41c78c | 2017-07-29 02:37:49 +0300 | [diff] [blame] | 1433 | * Allow SHPC (PCI bridges can have SHPC controller) |
Igor Mammedov | f97a88a | 2015-12-28 18:02:54 +0100 | [diff] [blame] | 1434 | */ |
Aleksandr Bezzubikov | a41c78c | 2017-07-29 02:37:49 +0300 | [diff] [blame] | 1435 | aml_append(if_ctx, aml_and(a_ctrl, aml_int(0x1F), a_ctrl)); |
Igor Mammedov | f97a88a | 2015-12-28 18:02:54 +0100 | [diff] [blame] | 1436 | |
| 1437 | if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1)))); |
| 1438 | /* Unknown revision */ |
| 1439 | aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1)); |
| 1440 | aml_append(if_ctx, if_ctx2); |
| 1441 | |
| 1442 | if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl))); |
| 1443 | /* Capabilities bits were masked */ |
| 1444 | aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1)); |
| 1445 | aml_append(if_ctx, if_ctx2); |
| 1446 | |
| 1447 | /* Update DWORD3 in the buffer */ |
| 1448 | aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3"))); |
| 1449 | aml_append(method, if_ctx); |
| 1450 | |
| 1451 | else_ctx = aml_else(); |
| 1452 | /* Unrecognized UUID */ |
| 1453 | aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1)); |
| 1454 | aml_append(method, else_ctx); |
| 1455 | |
| 1456 | aml_append(method, aml_return(aml_arg(3))); |
| 1457 | return method; |
| 1458 | } |
Igor Mammedov | b616ec4 | 2015-12-28 18:02:43 +0100 | [diff] [blame] | 1459 | |
Corey Minyard | ebe1558 | 2016-05-12 20:43:45 -0500 | [diff] [blame] | 1460 | static void build_smb0(Aml *table, I2CBus *smbus, int devnr, int func) |
| 1461 | { |
| 1462 | Aml *scope = aml_scope("_SB.PCI0"); |
| 1463 | Aml *dev = aml_device("SMB0"); |
| 1464 | |
Corey Minyard | ebe1558 | 2016-05-12 20:43:45 -0500 | [diff] [blame] | 1465 | aml_append(dev, aml_name_decl("_ADR", aml_int(devnr << 16 | func))); |
| 1466 | build_acpi_ipmi_devices(dev, BUS(smbus), "\\_SB.PCI0.SMB0"); |
| 1467 | aml_append(scope, dev); |
| 1468 | aml_append(table, scope); |
| 1469 | } |
| 1470 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1471 | static void |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 1472 | build_dsdt(GArray *table_data, BIOSLinker *linker, |
Igor Mammedov | adcb89d | 2016-02-26 14:59:26 +0100 | [diff] [blame] | 1473 | AcpiPmInfo *pm, AcpiMiscInfo *misc, |
Markus Armbruster | 01c9742 | 2016-06-15 19:56:31 +0200 | [diff] [blame] | 1474 | Range *pci_hole, Range *pci_hole64, MachineState *machine) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1475 | { |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1476 | CrsRangeEntry *entry; |
| 1477 | Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs; |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1478 | CrsRangeSet crs_range_set; |
Eduardo Habkost | fb306ff | 2015-12-11 16:42:26 -0200 | [diff] [blame] | 1479 | PCMachineState *pcms = PC_MACHINE(machine); |
Igor Mammedov | 679dd1a | 2016-06-15 11:25:23 +0200 | [diff] [blame] | 1480 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine); |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 1481 | X86MachineState *x86ms = X86_MACHINE(machine); |
Gerd Hoffmann | 4a44183 | 2019-06-07 09:34:29 +0200 | [diff] [blame] | 1482 | AcpiMcfgInfo mcfg; |
Igor Mammedov | bef3492 | 2014-06-02 15:25:26 +0200 | [diff] [blame] | 1483 | uint32_t nr_mem = machine->ram_slots; |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1484 | int root_bus_limit = 0xFF; |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1485 | PCIBus *bus = NULL; |
Stefan Berger | ac6dd31 | 2019-01-15 02:27:52 +0400 | [diff] [blame] | 1486 | TPMIf *tpm = tpm_find(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1487 | int i; |
| 1488 | |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1489 | dsdt = init_aml_allocator(); |
Laszlo Ersek | 2fd71f1 | 2014-03-17 17:05:17 +0100 | [diff] [blame] | 1490 | |
Igor Mammedov | 4ec8d2b | 2015-02-20 18:22:09 +0000 | [diff] [blame] | 1491 | /* Reserve space for header */ |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1492 | acpi_data_push(dsdt->buf, sizeof(AcpiTableHeader)); |
| 1493 | |
| 1494 | build_dbg_aml(dsdt); |
| 1495 | if (misc->is_piix4) { |
| 1496 | sb_scope = aml_scope("_SB"); |
| 1497 | dev = aml_device("PCI0"); |
| 1498 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03"))); |
| 1499 | aml_append(dev, aml_name_decl("_ADR", aml_int(0))); |
Michael S. Tsirkin | af1b80a | 2020-07-30 11:44:06 -0400 | [diff] [blame] | 1500 | aml_append(dev, aml_name_decl("_UID", aml_int(0))); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1501 | aml_append(sb_scope, dev); |
| 1502 | aml_append(dsdt, sb_scope); |
| 1503 | |
| 1504 | build_hpet_aml(dsdt); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1505 | build_piix4_isa_bridge(dsdt); |
| 1506 | build_isa_devices_aml(dsdt); |
| 1507 | build_piix4_pci_hotplug(dsdt); |
| 1508 | build_piix4_pci0_int(dsdt); |
| 1509 | } else { |
| 1510 | sb_scope = aml_scope("_SB"); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1511 | dev = aml_device("PCI0"); |
| 1512 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08"))); |
| 1513 | aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03"))); |
| 1514 | aml_append(dev, aml_name_decl("_ADR", aml_int(0))); |
Michael S. Tsirkin | af1b80a | 2020-07-30 11:44:06 -0400 | [diff] [blame] | 1515 | aml_append(dev, aml_name_decl("_UID", aml_int(0))); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1516 | aml_append(dev, build_q35_osc_method()); |
| 1517 | aml_append(sb_scope, dev); |
| 1518 | aml_append(dsdt, sb_scope); |
| 1519 | |
| 1520 | build_hpet_aml(dsdt); |
| 1521 | build_q35_isa_bridge(dsdt); |
| 1522 | build_isa_devices_aml(dsdt); |
| 1523 | build_q35_pci0_int(dsdt); |
Corey Minyard | ebe1558 | 2016-05-12 20:43:45 -0500 | [diff] [blame] | 1524 | if (pcms->smbus && !pcmc->do_not_add_smb_acpi) { |
| 1525 | build_smb0(dsdt, pcms->smbus, ICH9_SMB_DEV, ICH9_SMB_FUNC); |
| 1526 | } |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Igor Mammedov | 679dd1a | 2016-06-15 11:25:23 +0200 | [diff] [blame] | 1529 | if (pcmc->legacy_cpu_hotplug) { |
| 1530 | build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base); |
| 1531 | } else { |
| 1532 | CPUHotplugFeatures opts = { |
Dr. David Alan Gilbert | 89cb0c0 | 2019-01-25 09:40:46 +0000 | [diff] [blame] | 1533 | .acpi_1_compatible = true, .has_legacy_cphp = true |
Igor Mammedov | 679dd1a | 2016-06-15 11:25:23 +0200 | [diff] [blame] | 1534 | }; |
| 1535 | build_cpus_aml(dsdt, machine, opts, pm->cpu_hp_io_base, |
| 1536 | "\\_SB.PCI0", "\\_GPE._E02"); |
| 1537 | } |
Shameer Kolothum | 091c466 | 2019-09-18 14:06:23 +0100 | [diff] [blame] | 1538 | |
| 1539 | if (pcms->memhp_io_base && nr_mem) { |
| 1540 | build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0", |
| 1541 | "\\_GPE._E03", AML_SYSTEM_IO, |
| 1542 | pcms->memhp_io_base); |
| 1543 | } |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1544 | |
| 1545 | scope = aml_scope("_GPE"); |
| 1546 | { |
| 1547 | aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006"))); |
| 1548 | |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1549 | if (misc->is_piix4) { |
| 1550 | method = aml_method("_E01", 0, AML_NOTSERIALIZED); |
| 1551 | aml_append(method, |
| 1552 | aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF)); |
| 1553 | aml_append(method, aml_call0("\\_SB.PCI0.PCNT")); |
| 1554 | aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK"))); |
| 1555 | aml_append(scope, method); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1556 | } |
| 1557 | |
Eric Auger | f6a0d06 | 2019-03-08 19:20:53 +0100 | [diff] [blame] | 1558 | if (machine->nvdimms_state->is_enabled) { |
Xiao Guangrong | b097cc5 | 2016-10-29 00:35:40 +0800 | [diff] [blame] | 1559 | method = aml_method("_E04", 0, AML_NOTSERIALIZED); |
| 1560 | aml_append(method, aml_notify(aml_name("\\_SB.NVDR"), |
| 1561 | aml_int(0x80))); |
| 1562 | aml_append(scope, method); |
| 1563 | } |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1564 | } |
| 1565 | aml_append(dsdt, scope); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1566 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1567 | crs_range_set_init(&crs_range_set); |
Marcel Apfelbaum | 81ed648 | 2015-11-26 18:00:28 +0200 | [diff] [blame] | 1568 | bus = PC_MACHINE(machine)->bus; |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1569 | if (bus) { |
| 1570 | QLIST_FOREACH(bus, &bus->child, sibling) { |
| 1571 | uint8_t bus_num = pci_bus_num(bus); |
Marcel Apfelbaum | 0e79e51 | 2015-06-02 14:23:10 +0300 | [diff] [blame] | 1572 | uint8_t numa_node = pci_bus_numa_node(bus); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1573 | |
| 1574 | /* look only for expander root buses */ |
| 1575 | if (!pci_bus_is_root(bus)) { |
| 1576 | continue; |
| 1577 | } |
| 1578 | |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1579 | if (bus_num < root_bus_limit) { |
| 1580 | root_bus_limit = bus_num - 1; |
| 1581 | } |
| 1582 | |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1583 | scope = aml_scope("\\_SB"); |
| 1584 | dev = aml_device("PC%.02X", bus_num); |
Laszlo Ersek | c96d928 | 2015-06-11 02:37:58 +0200 | [diff] [blame] | 1585 | aml_append(dev, aml_name_decl("_UID", aml_int(bus_num))); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1586 | aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num))); |
Marcel Apfelbaum | 077dd74 | 2017-02-28 16:13:29 +0200 | [diff] [blame] | 1587 | if (pci_bus_is_express(bus)) { |
Evgeny Yakovlev | ee4b0c8 | 2019-07-19 11:54:29 +0300 | [diff] [blame] | 1588 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08"))); |
| 1589 | aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03"))); |
Marcel Apfelbaum | 077dd74 | 2017-02-28 16:13:29 +0200 | [diff] [blame] | 1590 | aml_append(dev, build_q35_osc_method()); |
Evgeny Yakovlev | ee4b0c8 | 2019-07-19 11:54:29 +0300 | [diff] [blame] | 1591 | } else { |
| 1592 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03"))); |
Marcel Apfelbaum | 077dd74 | 2017-02-28 16:13:29 +0200 | [diff] [blame] | 1593 | } |
Marcel Apfelbaum | 0e79e51 | 2015-06-02 14:23:10 +0300 | [diff] [blame] | 1594 | |
| 1595 | if (numa_node != NUMA_NODE_UNASSIGNED) { |
| 1596 | aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node))); |
| 1597 | } |
| 1598 | |
Igor Mammedov | 196e213 | 2015-12-28 18:02:42 +0100 | [diff] [blame] | 1599 | aml_append(dev, build_prt(false)); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1600 | crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set); |
Marcel Apfelbaum | a43c6e2 | 2015-06-02 14:23:03 +0300 | [diff] [blame] | 1601 | aml_append(dev, aml_name_decl("_CRS", crs)); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1602 | aml_append(scope, dev); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1603 | aml_append(dsdt, scope); |
Marcel Apfelbaum | a489420 | 2015-06-02 14:23:01 +0300 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
Gerd Hoffmann | 4a44183 | 2019-06-07 09:34:29 +0200 | [diff] [blame] | 1607 | /* |
| 1608 | * At this point crs_range_set has all the ranges used by pci |
| 1609 | * busses *other* than PCI0. These ranges will be excluded from |
| 1610 | * the PCI0._CRS. Add mmconfig to the set so it will be excluded |
| 1611 | * too. |
| 1612 | */ |
| 1613 | if (acpi_get_mcfg(&mcfg)) { |
| 1614 | crs_range_insert(crs_range_set.mem_ranges, |
| 1615 | mcfg.base, mcfg.base + mcfg.size - 1); |
| 1616 | } |
| 1617 | |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 1618 | scope = aml_scope("\\_SB.PCI0"); |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1619 | /* build PCI0._CRS */ |
| 1620 | crs = aml_resource_template(); |
| 1621 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1622 | 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] | 1623 | 0x0000, 0x0, root_bus_limit, |
| 1624 | 0x0000, root_bus_limit + 1)); |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1625 | aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08)); |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1626 | |
| 1627 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1628 | aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, |
| 1629 | AML_POS_DECODE, AML_ENTIRE_RANGE, |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1630 | 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8)); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1631 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1632 | crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF); |
| 1633 | for (i = 0; i < crs_range_set.io_ranges->len; i++) { |
| 1634 | entry = g_ptr_array_index(crs_range_set.io_ranges, i); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1635 | aml_append(crs, |
| 1636 | aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED, |
| 1637 | AML_POS_DECODE, AML_ENTIRE_RANGE, |
| 1638 | 0x0000, entry->base, entry->limit, |
| 1639 | 0x0000, entry->limit - entry->base + 1)); |
| 1640 | } |
| 1641 | |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1642 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1643 | aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, |
| 1644 | AML_CACHEABLE, AML_READ_WRITE, |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1645 | 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000)); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1646 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1647 | crs_replace_with_free_ranges(crs_range_set.mem_ranges, |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 1648 | range_lob(pci_hole), |
| 1649 | range_upb(pci_hole)); |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1650 | for (i = 0; i < crs_range_set.mem_ranges->len; i++) { |
| 1651 | entry = g_ptr_array_index(crs_range_set.mem_ranges, i); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1652 | aml_append(crs, |
| 1653 | aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED, |
| 1654 | AML_NON_CACHEABLE, AML_READ_WRITE, |
| 1655 | 0, entry->base, entry->limit, |
| 1656 | 0, entry->limit - entry->base + 1)); |
| 1657 | } |
| 1658 | |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 1659 | if (!range_is_empty(pci_hole64)) { |
Marcel Apfelbaum | 16de88a | 2016-07-17 19:53:13 +0300 | [diff] [blame] | 1660 | crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges, |
| 1661 | range_lob(pci_hole64), |
| 1662 | range_upb(pci_hole64)); |
| 1663 | for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) { |
| 1664 | entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i); |
| 1665 | aml_append(crs, |
| 1666 | aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, |
| 1667 | AML_MAX_FIXED, |
| 1668 | AML_CACHEABLE, AML_READ_WRITE, |
| 1669 | 0, entry->base, entry->limit, |
| 1670 | 0, entry->limit - entry->base + 1)); |
| 1671 | } |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1672 | } |
Igor Mammedov | 2b1c2e8 | 2016-04-08 13:23:13 +0200 | [diff] [blame] | 1673 | |
Eric Auger | 43bc7f8 | 2020-03-05 17:51:40 +0100 | [diff] [blame] | 1674 | if (TPM_IS_TIS_ISA(tpm_find())) { |
Igor Mammedov | 2b1c2e8 | 2016-04-08 13:23:13 +0200 | [diff] [blame] | 1675 | aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE, |
| 1676 | TPM_TIS_ADDR_SIZE, AML_READ_WRITE)); |
| 1677 | } |
Igor Mammedov | 60efd42 | 2015-02-20 18:22:05 +0000 | [diff] [blame] | 1678 | aml_append(scope, aml_name_decl("_CRS", crs)); |
| 1679 | |
Igor Mammedov | d31c909 | 2015-02-20 18:22:08 +0000 | [diff] [blame] | 1680 | /* reserve GPE0 block resources */ |
| 1681 | dev = aml_device("GPE0"); |
| 1682 | aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06"))); |
| 1683 | aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources"))); |
| 1684 | /* device present, functioning, decoding, not shown in UI */ |
| 1685 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 1686 | crs = aml_resource_template(); |
| 1687 | aml_append(crs, |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 1688 | aml_io( |
| 1689 | AML_DECODE16, |
| 1690 | pm->fadt.gpe0_blk.address, |
| 1691 | pm->fadt.gpe0_blk.address, |
| 1692 | 1, |
| 1693 | pm->fadt.gpe0_blk.bit_width / 8) |
Igor Mammedov | d31c909 | 2015-02-20 18:22:08 +0000 | [diff] [blame] | 1694 | ); |
| 1695 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1696 | aml_append(scope, dev); |
| 1697 | |
Marcel Apfelbaum | 2df5a7b | 2016-07-17 19:53:12 +0300 | [diff] [blame] | 1698 | crs_range_set_free(&crs_range_set); |
Marcel Apfelbaum | dcdca29 | 2015-06-02 14:23:04 +0300 | [diff] [blame] | 1699 | |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 1700 | /* reserve PCIHP resources */ |
| 1701 | if (pm->pcihp_io_len) { |
| 1702 | dev = aml_device("PHPR"); |
| 1703 | aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06"))); |
| 1704 | aml_append(dev, |
| 1705 | aml_name_decl("_UID", aml_string("PCI Hotplug resources"))); |
| 1706 | /* device present, functioning, decoding, not shown in UI */ |
| 1707 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 1708 | crs = aml_resource_template(); |
| 1709 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1710 | 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] | 1711 | pm->pcihp_io_len) |
| 1712 | ); |
| 1713 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1714 | aml_append(scope, dev); |
| 1715 | } |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1716 | aml_append(dsdt, scope); |
Igor Mammedov | 500b11e | 2015-02-18 19:14:50 +0000 | [diff] [blame] | 1717 | |
Igor Mammedov | ebc3028 | 2015-02-18 19:14:29 +0000 | [diff] [blame] | 1718 | /* create S3_ / S4_ / S5_ packages if necessary */ |
| 1719 | scope = aml_scope("\\"); |
| 1720 | if (!pm->s3_disabled) { |
| 1721 | pkg = aml_package(4); |
| 1722 | aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */ |
| 1723 | aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */ |
| 1724 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1725 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1726 | aml_append(scope, aml_name_decl("_S3", pkg)); |
| 1727 | } |
| 1728 | |
| 1729 | if (!pm->s4_disabled) { |
| 1730 | pkg = aml_package(4); |
| 1731 | aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */ |
| 1732 | /* PM1b_CNT.SLP_TYP, FIXME: not impl. */ |
| 1733 | aml_append(pkg, aml_int(pm->s4_val)); |
| 1734 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1735 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1736 | aml_append(scope, aml_name_decl("_S4", pkg)); |
| 1737 | } |
| 1738 | |
| 1739 | pkg = aml_package(4); |
| 1740 | aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */ |
| 1741 | aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */ |
| 1742 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1743 | aml_append(pkg, aml_int(0)); /* reserved */ |
| 1744 | aml_append(scope, aml_name_decl("_S5", pkg)); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1745 | aml_append(dsdt, scope); |
Igor Mammedov | ebc3028 | 2015-02-18 19:14:29 +0000 | [diff] [blame] | 1746 | |
Gabriel L. Somlo | e2ec756 | 2016-02-19 13:20:27 -0500 | [diff] [blame] | 1747 | /* create fw_cfg node, unconditionally */ |
| 1748 | { |
Gabriel L. Somlo | e2ec756 | 2016-02-19 13:20:27 -0500 | [diff] [blame] | 1749 | scope = aml_scope("\\_SB.PCI0"); |
Gerd Hoffmann | 0575c2f | 2020-06-19 11:19:00 +0200 | [diff] [blame] | 1750 | fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg); |
Gabriel L. Somlo | e2ec756 | 2016-02-19 13:20:27 -0500 | [diff] [blame] | 1751 | aml_append(dsdt, scope); |
| 1752 | } |
| 1753 | |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 1754 | if (misc->applesmc_io_base) { |
| 1755 | scope = aml_scope("\\_SB.PCI0.ISA"); |
| 1756 | dev = aml_device("SMC"); |
| 1757 | |
| 1758 | aml_append(dev, aml_name_decl("_HID", aml_eisaid("APP0001"))); |
| 1759 | /* device present, functioning, decoding, not shown in UI */ |
| 1760 | aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); |
| 1761 | |
| 1762 | crs = aml_resource_template(); |
| 1763 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1764 | aml_io(AML_DECODE16, misc->applesmc_io_base, misc->applesmc_io_base, |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 1765 | 0x01, APPLESMC_MAX_DATA_LENGTH) |
| 1766 | ); |
| 1767 | aml_append(crs, aml_irq_no_flags(6)); |
| 1768 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1769 | |
| 1770 | aml_append(scope, dev); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1771 | aml_append(dsdt, scope); |
Igor Mammedov | 8ac6f7a | 2015-02-20 18:22:12 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1774 | if (misc->pvpanic_port) { |
| 1775 | scope = aml_scope("\\_SB.PCI0.ISA"); |
| 1776 | |
Radim Krčmář | 2332333 | 2015-05-29 21:57:32 +0200 | [diff] [blame] | 1777 | dev = aml_device("PEVT"); |
Igor Mammedov | e65bef6 | 2015-03-30 14:18:27 +0200 | [diff] [blame] | 1778 | aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0001"))); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1779 | |
| 1780 | crs = aml_resource_template(); |
| 1781 | aml_append(crs, |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1782 | aml_io(AML_DECODE16, misc->pvpanic_port, misc->pvpanic_port, 1, 1) |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1783 | ); |
| 1784 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1785 | |
Shannon Zhao | ff80dc7 | 2015-05-29 11:28:54 +0100 | [diff] [blame] | 1786 | aml_append(dev, aml_operation_region("PEOR", AML_SYSTEM_IO, |
Xiao Guangrong | 3f3009c | 2016-03-01 18:56:05 +0800 | [diff] [blame] | 1787 | aml_int(misc->pvpanic_port), 1)); |
Igor Mammedov | 36de884 | 2015-12-10 00:41:12 +0100 | [diff] [blame] | 1788 | field = aml_field("PEOR", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1789 | aml_append(field, aml_named_field("PEPT", 8)); |
| 1790 | aml_append(dev, field); |
| 1791 | |
Gal Hammer | 8ef3ea2 | 2015-07-26 11:00:51 +0300 | [diff] [blame] | 1792 | /* device present, functioning, decoding, shown in UI */ |
| 1793 | aml_append(dev, aml_name_decl("_STA", aml_int(0xF))); |
Radim Krčmář | 2332333 | 2015-05-29 21:57:32 +0200 | [diff] [blame] | 1794 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 1795 | method = aml_method("RDPT", 0, AML_NOTSERIALIZED); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1796 | aml_append(method, aml_store(aml_name("PEPT"), aml_local(0))); |
| 1797 | aml_append(method, aml_return(aml_local(0))); |
| 1798 | aml_append(dev, method); |
| 1799 | |
Xiao Guangrong | 4dbfc88 | 2015-12-17 13:37:13 +0000 | [diff] [blame] | 1800 | method = aml_method("WRPT", 1, AML_NOTSERIALIZED); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1801 | aml_append(method, aml_store(aml_arg(0), aml_name("PEPT"))); |
| 1802 | aml_append(dev, method); |
| 1803 | |
| 1804 | aml_append(scope, dev); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1805 | aml_append(dsdt, scope); |
Igor Mammedov | cd61cb2 | 2015-02-18 19:14:38 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Gal Hammer | 7824df3 | 2015-04-21 11:26:12 +0300 | [diff] [blame] | 1808 | sb_scope = aml_scope("\\_SB"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1809 | { |
Igor Mammedov | 8b35ab2 | 2016-12-06 00:32:25 +0100 | [diff] [blame] | 1810 | Object *pci_host; |
| 1811 | PCIBus *bus = NULL; |
Igor Mammedov | 8698c0c | 2015-02-18 19:14:46 +0000 | [diff] [blame] | 1812 | |
Igor Mammedov | 8b35ab2 | 2016-12-06 00:32:25 +0100 | [diff] [blame] | 1813 | pci_host = acpi_get_i386_pci_host(); |
| 1814 | if (pci_host) { |
| 1815 | bus = PCI_HOST_BRIDGE(pci_host)->bus; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1816 | } |
Igor Mammedov | 8b35ab2 | 2016-12-06 00:32:25 +0100 | [diff] [blame] | 1817 | |
| 1818 | if (bus) { |
| 1819 | Aml *scope = aml_scope("PCI0"); |
| 1820 | /* Scan all PCI buses. Generate tables to support hotplug. */ |
| 1821 | build_append_pci_bus_devices(scope, bus, pm->pcihp_bridge_en); |
| 1822 | |
Eric Auger | 43bc7f8 | 2020-03-05 17:51:40 +0100 | [diff] [blame] | 1823 | if (TPM_IS_TIS_ISA(tpm)) { |
Stefan Berger | 24cf541 | 2019-01-25 16:00:58 -0500 | [diff] [blame] | 1824 | if (misc->tpm_version == TPM_VERSION_2_0) { |
| 1825 | dev = aml_device("TPM"); |
| 1826 | aml_append(dev, aml_name_decl("_HID", |
| 1827 | aml_string("MSFT0101"))); |
| 1828 | } else { |
| 1829 | dev = aml_device("ISA.TPM"); |
| 1830 | aml_append(dev, aml_name_decl("_HID", |
| 1831 | aml_eisaid("PNP0C31"))); |
| 1832 | } |
| 1833 | |
Igor Mammedov | 8b35ab2 | 2016-12-06 00:32:25 +0100 | [diff] [blame] | 1834 | aml_append(dev, aml_name_decl("_STA", aml_int(0xF))); |
| 1835 | crs = aml_resource_template(); |
| 1836 | aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE, |
| 1837 | TPM_TIS_ADDR_SIZE, AML_READ_WRITE)); |
| 1838 | /* |
| 1839 | FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs, |
| 1840 | Rewrite to take IRQ from TPM device model and |
| 1841 | fix default IRQ value there to use some unused IRQ |
| 1842 | */ |
| 1843 | /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */ |
| 1844 | aml_append(dev, aml_name_decl("_CRS", crs)); |
Stefan Berger | ac6dd31 | 2019-01-15 02:27:52 +0400 | [diff] [blame] | 1845 | |
| 1846 | tpm_build_ppi_acpi(tpm, dev); |
| 1847 | |
Igor Mammedov | 8b35ab2 | 2016-12-06 00:32:25 +0100 | [diff] [blame] | 1848 | aml_append(scope, dev); |
| 1849 | } |
| 1850 | |
| 1851 | aml_append(sb_scope, scope); |
| 1852 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1853 | } |
Marc-André Lureau | 4ab6cb4 | 2018-01-29 19:33:07 +0100 | [diff] [blame] | 1854 | |
Stefan Berger | ac6dd31 | 2019-01-15 02:27:52 +0400 | [diff] [blame] | 1855 | if (TPM_IS_CRB(tpm)) { |
Marc-André Lureau | 4ab6cb4 | 2018-01-29 19:33:07 +0100 | [diff] [blame] | 1856 | dev = aml_device("TPM"); |
| 1857 | aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101"))); |
| 1858 | crs = aml_resource_template(); |
| 1859 | aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE, |
| 1860 | TPM_CRB_ADDR_SIZE, AML_READ_WRITE)); |
| 1861 | aml_append(dev, aml_name_decl("_CRS", crs)); |
| 1862 | |
Gerd Hoffmann | 88b3648 | 2020-04-29 15:59:52 +0200 | [diff] [blame] | 1863 | aml_append(dev, aml_name_decl("_STA", aml_int(0xf))); |
Marc-André Lureau | 4ab6cb4 | 2018-01-29 19:33:07 +0100 | [diff] [blame] | 1864 | |
Stefan Berger | ac6dd31 | 2019-01-15 02:27:52 +0400 | [diff] [blame] | 1865 | tpm_build_ppi_acpi(tpm, dev); |
| 1866 | |
Marc-André Lureau | 4ab6cb4 | 2018-01-29 19:33:07 +0100 | [diff] [blame] | 1867 | aml_append(sb_scope, dev); |
| 1868 | } |
| 1869 | |
Igor Mammedov | 8b35ab2 | 2016-12-06 00:32:25 +0100 | [diff] [blame] | 1870 | aml_append(dsdt, sb_scope); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1871 | |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1872 | /* copy AML table into ACPI tables blob and patch header there */ |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1873 | g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1874 | build_header(linker, table_data, |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 1875 | (void *)(table_data->data + table_data->len - dsdt->buf->len), |
Laszlo Ersek | 37ad223 | 2016-01-18 15:12:10 +0100 | [diff] [blame] | 1876 | "DSDT", dsdt->buf->len, 1, NULL, NULL); |
Igor Mammedov | 011bb74 | 2015-02-18 19:14:16 +0000 | [diff] [blame] | 1877 | free_aml_allocator(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | static void |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 1881 | build_hpet(GArray *table_data, BIOSLinker *linker) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1882 | { |
| 1883 | Acpi20Hpet *hpet; |
| 1884 | |
| 1885 | hpet = acpi_data_push(table_data, sizeof(*hpet)); |
| 1886 | /* Note timer_block_id value must be kept in sync with value advertised by |
| 1887 | * emulated hpet |
| 1888 | */ |
| 1889 | hpet->timer_block_id = cpu_to_le32(0x8086a201); |
| 1890 | hpet->addr.address = cpu_to_le64(HPET_BASE); |
| 1891 | build_header(linker, table_data, |
Laszlo Ersek | 37ad223 | 2016-01-18 15:12:10 +0100 | [diff] [blame] | 1892 | (void *)hpet, "HPET", sizeof(*hpet), 1, NULL, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1893 | } |
| 1894 | |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1895 | static void |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 1896 | build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog) |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1897 | { |
| 1898 | Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa); |
Igor Mammedov | 4678124 | 2016-05-19 15:19:29 +0200 | [diff] [blame] | 1899 | unsigned log_addr_size = sizeof(tcpa->log_area_start_address); |
| 1900 | unsigned log_addr_offset = |
| 1901 | (char *)&tcpa->log_area_start_address - table_data->data; |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1902 | |
| 1903 | tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT); |
| 1904 | tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE); |
Igor Mammedov | 9774ccf | 2016-05-19 15:19:28 +0200 | [diff] [blame] | 1905 | acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length)); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1906 | |
Igor Mammedov | ad9671b | 2016-05-19 15:19:26 +0200 | [diff] [blame] | 1907 | bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1, |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 1908 | false /* high memory */); |
| 1909 | |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1910 | /* log area start address to be filled by Guest linker */ |
Igor Mammedov | 4678124 | 2016-05-19 15:19:29 +0200 | [diff] [blame] | 1911 | bios_linker_loader_add_pointer(linker, |
| 1912 | ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size, |
| 1913 | ACPI_BUILD_TPMLOG_FILE, 0); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1914 | |
| 1915 | build_header(linker, table_data, |
Laszlo Ersek | 37ad223 | 2016-01-18 15:12:10 +0100 | [diff] [blame] | 1916 | (void *)tcpa, "TCPA", sizeof(*tcpa), 2, NULL, NULL); |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 1917 | } |
| 1918 | |
Paolo Bonzini | d471bf3 | 2018-06-29 16:22:13 +0200 | [diff] [blame] | 1919 | #define HOLE_640K_START (640 * KiB) |
| 1920 | #define HOLE_640K_END (1 * MiB) |
Eduardo Habkost | 4926403 | 2017-09-01 10:10:02 +0800 | [diff] [blame] | 1921 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1922 | static void |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 1923 | build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1924 | { |
| 1925 | AcpiSystemResourceAffinityTable *srat; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1926 | AcpiSratMemoryAffinity *numamem; |
| 1927 | |
| 1928 | int i; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1929 | int srat_start, numa_start, slots; |
| 1930 | uint64_t mem_len, mem_base, next_base; |
Igor Mammedov | 5803fce | 2016-02-26 14:59:23 +0100 | [diff] [blame] | 1931 | MachineClass *mc = MACHINE_GET_CLASS(machine); |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 1932 | X86MachineState *x86ms = X86_MACHINE(machine); |
Igor Mammedov | 80e5db3 | 2017-01-18 18:13:20 +0100 | [diff] [blame] | 1933 | const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine); |
Igor Mammedov | 3d3ebca | 2016-02-26 14:59:22 +0100 | [diff] [blame] | 1934 | PCMachineState *pcms = PC_MACHINE(machine); |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 1935 | ram_addr_t hotplugabble_address_space_size = |
David Hildenbrand | f2ffbe2 | 2018-04-23 18:51:24 +0200 | [diff] [blame] | 1936 | object_property_get_int(OBJECT(pcms), PC_MACHINE_DEVMEM_REGION_SIZE, |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 1937 | NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1938 | |
| 1939 | srat_start = table_data->len; |
| 1940 | |
| 1941 | srat = acpi_data_push(table_data, sizeof *srat); |
| 1942 | srat->reserved1 = cpu_to_le32(1); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1943 | |
Igor Mammedov | 5803fce | 2016-02-26 14:59:23 +0100 | [diff] [blame] | 1944 | for (i = 0; i < apic_ids->len; i++) { |
Igor Mammedov | d41f3e7 | 2017-05-30 18:23:58 +0200 | [diff] [blame] | 1945 | int node_id = apic_ids->cpus[i].props.node_id; |
Igor Mammedov | 5eff33a | 2016-10-19 14:05:32 +0200 | [diff] [blame] | 1946 | uint32_t apic_id = apic_ids->cpus[i].arch_id; |
Igor Mammedov | 5803fce | 2016-02-26 14:59:23 +0100 | [diff] [blame] | 1947 | |
Igor Mammedov | 5eff33a | 2016-10-19 14:05:32 +0200 | [diff] [blame] | 1948 | if (apic_id < 255) { |
| 1949 | AcpiSratProcessorAffinity *core; |
| 1950 | |
| 1951 | core = acpi_data_push(table_data, sizeof *core); |
| 1952 | core->type = ACPI_SRAT_PROCESSOR_APIC; |
| 1953 | core->length = sizeof(*core); |
| 1954 | core->local_apic_id = apic_id; |
Igor Mammedov | ea26507 | 2017-05-10 13:29:52 +0200 | [diff] [blame] | 1955 | core->proximity_lo = node_id; |
Igor Mammedov | 5eff33a | 2016-10-19 14:05:32 +0200 | [diff] [blame] | 1956 | memset(core->proximity_hi, 0, 3); |
| 1957 | core->local_sapic_eid = 0; |
| 1958 | core->flags = cpu_to_le32(1); |
| 1959 | } else { |
| 1960 | AcpiSratProcessorX2ApicAffinity *core; |
| 1961 | |
| 1962 | core = acpi_data_push(table_data, sizeof *core); |
| 1963 | core->type = ACPI_SRAT_PROCESSOR_x2APIC; |
| 1964 | core->length = sizeof(*core); |
| 1965 | core->x2apic_id = cpu_to_le32(apic_id); |
Igor Mammedov | ea26507 | 2017-05-10 13:29:52 +0200 | [diff] [blame] | 1966 | core->proximity_domain = cpu_to_le32(node_id); |
Igor Mammedov | 5eff33a | 2016-10-19 14:05:32 +0200 | [diff] [blame] | 1967 | core->flags = cpu_to_le32(1); |
Igor Mammedov | 1f3aba3 | 2016-06-16 14:23:48 +0200 | [diff] [blame] | 1968 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | |
| 1972 | /* the memory map is a bit tricky, it contains at least one hole |
| 1973 | * from 640k-1M and possibly another one from 3.5G-4G. |
| 1974 | */ |
| 1975 | next_base = 0; |
| 1976 | numa_start = table_data->len; |
| 1977 | |
Eduardo Habkost | dd4c2f0 | 2015-12-11 16:42:32 -0200 | [diff] [blame] | 1978 | for (i = 1; i < pcms->numa_nodes + 1; ++i) { |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1979 | mem_base = next_base; |
Eduardo Habkost | dd4c2f0 | 2015-12-11 16:42:32 -0200 | [diff] [blame] | 1980 | mem_len = pcms->node_mem[i - 1]; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 1981 | next_base = mem_base + mem_len; |
| 1982 | |
Eduardo Habkost | 4926403 | 2017-09-01 10:10:02 +0800 | [diff] [blame] | 1983 | /* Cut out the 640K hole */ |
| 1984 | if (mem_base <= HOLE_640K_START && |
| 1985 | next_base > HOLE_640K_START) { |
| 1986 | mem_len -= next_base - HOLE_640K_START; |
| 1987 | if (mem_len > 0) { |
| 1988 | numamem = acpi_data_push(table_data, sizeof *numamem); |
| 1989 | build_srat_memory(numamem, mem_base, mem_len, i - 1, |
| 1990 | MEM_AFFINITY_ENABLED); |
| 1991 | } |
| 1992 | |
| 1993 | /* Check for the rare case: 640K < RAM < 1M */ |
| 1994 | if (next_base <= HOLE_640K_END) { |
| 1995 | next_base = HOLE_640K_END; |
| 1996 | continue; |
| 1997 | } |
| 1998 | mem_base = HOLE_640K_END; |
| 1999 | mem_len = next_base - HOLE_640K_END; |
| 2000 | } |
| 2001 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2002 | /* Cut out the ACPI_PCI hole */ |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2003 | if (mem_base <= x86ms->below_4g_mem_size && |
| 2004 | next_base > x86ms->below_4g_mem_size) { |
| 2005 | mem_len -= next_base - x86ms->below_4g_mem_size; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2006 | if (mem_len > 0) { |
| 2007 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Shannon Zhao | 64b8313 | 2016-05-12 13:22:28 +0100 | [diff] [blame] | 2008 | build_srat_memory(numamem, mem_base, mem_len, i - 1, |
| 2009 | MEM_AFFINITY_ENABLED); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2010 | } |
| 2011 | mem_base = 1ULL << 32; |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2012 | mem_len = next_base - x86ms->below_4g_mem_size; |
Dou Liyang | 6cf6fe3 | 2017-12-14 12:08:55 +0800 | [diff] [blame] | 2013 | next_base = mem_base + mem_len; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2014 | } |
Dou Liyang | 16b4226 | 2018-07-10 16:58:01 +0800 | [diff] [blame] | 2015 | |
| 2016 | if (mem_len > 0) { |
| 2017 | numamem = acpi_data_push(table_data, sizeof *numamem); |
| 2018 | build_srat_memory(numamem, mem_base, mem_len, i - 1, |
| 2019 | MEM_AFFINITY_ENABLED); |
| 2020 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2021 | } |
Vishal Verma | c3b0cf6 | 2020-06-05 18:09:10 -0600 | [diff] [blame] | 2022 | |
| 2023 | if (machine->nvdimms_state->is_enabled) { |
| 2024 | nvdimm_build_srat(table_data); |
| 2025 | } |
| 2026 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2027 | slots = (table_data->len - numa_start) / sizeof *numamem; |
Eduardo Habkost | dd4c2f0 | 2015-12-11 16:42:32 -0200 | [diff] [blame] | 2028 | for (; slots < pcms->numa_nodes + 2; slots++) { |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2029 | numamem = acpi_data_push(table_data, sizeof *numamem); |
Shannon Zhao | 64b8313 | 2016-05-12 13:22:28 +0100 | [diff] [blame] | 2030 | build_srat_memory(numamem, 0, 0, 0, MEM_AFFINITY_NOFLAGS); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2031 | } |
| 2032 | |
Igor Mammedov | dbb6da8 | 2018-08-22 11:46:44 +0200 | [diff] [blame] | 2033 | /* |
| 2034 | * Entry is required for Windows to enable memory hotplug in OS |
| 2035 | * and for Linux to enable SWIOTLB when booted with less than |
| 2036 | * 4G of RAM. Windows works better if the entry sets proximity |
| 2037 | * to the highest NUMA node in the machine. |
| 2038 | * Memory devices may override proximity set by this entry, |
| 2039 | * providing _PXM method if necessary. |
| 2040 | */ |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 2041 | if (hotplugabble_address_space_size) { |
Igor Mammedov | dbb6da8 | 2018-08-22 11:46:44 +0200 | [diff] [blame] | 2042 | numamem = acpi_data_push(table_data, sizeof *numamem); |
| 2043 | build_srat_memory(numamem, machine->device_memory->base, |
| 2044 | hotplugabble_address_space_size, pcms->numa_nodes - 1, |
| 2045 | MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED); |
Igor Mammedov | cec6519 | 2014-06-02 15:25:28 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2048 | build_header(linker, table_data, |
| 2049 | (void *)(table_data->data + srat_start), |
Michael S. Tsirkin | 821e322 | 2014-03-18 15:49:41 +0200 | [diff] [blame] | 2050 | "SRAT", |
Laszlo Ersek | 37ad223 | 2016-01-18 15:12:10 +0100 | [diff] [blame] | 2051 | table_data->len - srat_start, 1, NULL, NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2052 | } |
| 2053 | |
Peter Xu | d46114f | 2016-07-14 13:56:14 +0800 | [diff] [blame] | 2054 | /* |
| 2055 | * VT-d spec 8.1 DMA Remapping Reporting Structure |
| 2056 | * (version Oct. 2014 or later) |
| 2057 | */ |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2058 | static void |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 2059 | build_dmar_q35(GArray *table_data, BIOSLinker *linker) |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2060 | { |
| 2061 | int dmar_start = table_data->len; |
| 2062 | |
| 2063 | AcpiTableDmar *dmar; |
| 2064 | AcpiDmarHardwareUnit *drhd; |
Jason Wang | bd2baac | 2016-12-30 18:09:16 +0800 | [diff] [blame] | 2065 | AcpiDmarRootPortATS *atsr; |
Peter Xu | d46114f | 2016-07-14 13:56:14 +0800 | [diff] [blame] | 2066 | uint8_t dmar_flags = 0; |
| 2067 | X86IOMMUState *iommu = x86_iommu_get_default(); |
Peter Xu | cfc13df | 2016-07-14 13:56:17 +0800 | [diff] [blame] | 2068 | AcpiDmarDeviceScope *scope = NULL; |
| 2069 | /* Root complex IOAPIC use one path[0] only */ |
| 2070 | size_t ioapic_scope_size = sizeof(*scope) + sizeof(scope->path[0]); |
Prasad Singamsetty | 37f5138 | 2017-11-14 18:13:50 -0500 | [diff] [blame] | 2071 | IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu); |
Peter Xu | d46114f | 2016-07-14 13:56:14 +0800 | [diff] [blame] | 2072 | |
| 2073 | assert(iommu); |
Peter Xu | a924b3d | 2018-12-20 13:40:36 +0800 | [diff] [blame] | 2074 | if (x86_iommu_ir_supported(iommu)) { |
Peter Xu | d46114f | 2016-07-14 13:56:14 +0800 | [diff] [blame] | 2075 | dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */ |
| 2076 | } |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2077 | |
| 2078 | dmar = acpi_data_push(table_data, sizeof(*dmar)); |
Prasad Singamsetty | 37f5138 | 2017-11-14 18:13:50 -0500 | [diff] [blame] | 2079 | dmar->host_address_width = intel_iommu->aw_bits - 1; |
Peter Xu | d46114f | 2016-07-14 13:56:14 +0800 | [diff] [blame] | 2080 | dmar->flags = dmar_flags; |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2081 | |
| 2082 | /* DMAR Remapping Hardware Unit Definition structure */ |
Peter Xu | cfc13df | 2016-07-14 13:56:17 +0800 | [diff] [blame] | 2083 | drhd = acpi_data_push(table_data, sizeof(*drhd) + ioapic_scope_size); |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2084 | drhd->type = cpu_to_le16(ACPI_DMAR_TYPE_HARDWARE_UNIT); |
Peter Xu | cfc13df | 2016-07-14 13:56:17 +0800 | [diff] [blame] | 2085 | drhd->length = cpu_to_le16(sizeof(*drhd) + ioapic_scope_size); |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2086 | drhd->flags = ACPI_DMAR_INCLUDE_PCI_ALL; |
| 2087 | drhd->pci_segment = cpu_to_le16(0); |
| 2088 | drhd->address = cpu_to_le64(Q35_HOST_BRIDGE_IOMMU_ADDR); |
| 2089 | |
Peter Xu | cfc13df | 2016-07-14 13:56:17 +0800 | [diff] [blame] | 2090 | /* Scope definition for the root-complex IOAPIC. See VT-d spec |
| 2091 | * 8.3.1 (version Oct. 2014 or later). */ |
| 2092 | scope = &drhd->scope[0]; |
| 2093 | scope->entry_type = 0x03; /* Type: 0x03 for IOAPIC */ |
| 2094 | scope->length = ioapic_scope_size; |
| 2095 | scope->enumeration_id = ACPI_BUILD_IOAPIC_ID; |
| 2096 | scope->bus = Q35_PSEUDO_BUS_PLATFORM; |
Peter Xu | 1b39bc1 | 2016-10-31 15:34:39 +0800 | [diff] [blame] | 2097 | scope->path[0].device = PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC); |
| 2098 | scope->path[0].function = PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC); |
Peter Xu | cfc13df | 2016-07-14 13:56:17 +0800 | [diff] [blame] | 2099 | |
Jason Wang | bd2baac | 2016-12-30 18:09:16 +0800 | [diff] [blame] | 2100 | if (iommu->dt_supported) { |
| 2101 | atsr = acpi_data_push(table_data, sizeof(*atsr)); |
| 2102 | atsr->type = cpu_to_le16(ACPI_DMAR_TYPE_ATSR); |
| 2103 | atsr->length = cpu_to_le16(sizeof(*atsr)); |
| 2104 | atsr->flags = ACPI_DMAR_ATSR_ALL_PORTS; |
| 2105 | atsr->pci_segment = cpu_to_le16(0); |
| 2106 | } |
| 2107 | |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2108 | build_header(linker, table_data, (void *)(table_data->data + dmar_start), |
Laszlo Ersek | 37ad223 | 2016-01-18 15:12:10 +0100 | [diff] [blame] | 2109 | "DMAR", table_data->len - dmar_start, 1, NULL, NULL); |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2110 | } |
Liran Alon | 14cda35 | 2020-03-13 16:50:08 +0200 | [diff] [blame] | 2111 | |
| 2112 | /* |
| 2113 | * Windows ACPI Emulated Devices Table |
| 2114 | * (Version 1.0 - April 6, 2009) |
| 2115 | * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx |
| 2116 | * |
| 2117 | * Helpful to speedup Windows guests and ignored by others. |
| 2118 | */ |
| 2119 | static void |
| 2120 | build_waet(GArray *table_data, BIOSLinker *linker) |
| 2121 | { |
| 2122 | int waet_start = table_data->len; |
| 2123 | |
| 2124 | /* WAET header */ |
| 2125 | acpi_data_push(table_data, sizeof(AcpiTableHeader)); |
| 2126 | /* |
| 2127 | * Set "ACPI PM timer good" flag. |
| 2128 | * |
| 2129 | * Tells Windows guests that our ACPI PM timer is reliable in the |
| 2130 | * sense that guest can read it only once to obtain a reliable value. |
| 2131 | * Which avoids costly VMExits caused by guest re-reading it unnecessarily. |
| 2132 | */ |
| 2133 | build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4); |
| 2134 | |
| 2135 | build_header(linker, table_data, (void *)(table_data->data + waet_start), |
| 2136 | "WAET", table_data->len - waet_start, 1, NULL, NULL); |
| 2137 | } |
| 2138 | |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2139 | /* |
| 2140 | * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2 |
| 2141 | * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf |
| 2142 | */ |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2143 | #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0)) |
| 2144 | |
Alex Williamson | 977aff1 | 2019-10-23 16:47:28 -0600 | [diff] [blame] | 2145 | /* |
| 2146 | * Insert IVHD entry for device and recurse, insert alias, or insert range as |
| 2147 | * necessary for the PCI topology. |
| 2148 | */ |
| 2149 | static void |
| 2150 | insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque) |
| 2151 | { |
| 2152 | GArray *table_data = opaque; |
| 2153 | uint32_t entry; |
| 2154 | |
| 2155 | /* "Select" IVHD entry, type 0x2 */ |
| 2156 | entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2; |
| 2157 | build_append_int_noprefix(table_data, entry, 4); |
| 2158 | |
| 2159 | if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { |
| 2160 | PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev)); |
| 2161 | uint8_t sec = pci_bus_num(sec_bus); |
| 2162 | uint8_t sub = dev->config[PCI_SUBORDINATE_BUS]; |
| 2163 | |
| 2164 | if (pci_bus_is_express(sec_bus)) { |
| 2165 | /* |
| 2166 | * Walk the bus if there are subordinates, otherwise use a range |
| 2167 | * to cover an entire leaf bus. We could potentially also use a |
| 2168 | * range for traversed buses, but we'd need to take care not to |
| 2169 | * create both Select and Range entries covering the same device. |
| 2170 | * This is easier and potentially more compact. |
| 2171 | * |
| 2172 | * An example bare metal system seems to use Select entries for |
| 2173 | * root ports without a slot (ie. built-ins) and Range entries |
| 2174 | * when there is a slot. The same system also only hard-codes |
| 2175 | * the alias range for an onboard PCIe-to-PCI bridge, apparently |
| 2176 | * making no effort to support nested bridges. We attempt to |
| 2177 | * be more thorough here. |
| 2178 | */ |
| 2179 | if (sec == sub) { /* leaf bus */ |
| 2180 | /* "Start of Range" IVHD entry, type 0x3 */ |
| 2181 | entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3; |
| 2182 | build_append_int_noprefix(table_data, entry, 4); |
| 2183 | /* "End of Range" IVHD entry, type 0x4 */ |
| 2184 | entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4; |
| 2185 | build_append_int_noprefix(table_data, entry, 4); |
| 2186 | } else { |
| 2187 | pci_for_each_device(sec_bus, sec, insert_ivhd, table_data); |
| 2188 | } |
| 2189 | } else { |
| 2190 | /* |
| 2191 | * If the secondary bus is conventional, then we need to create an |
| 2192 | * Alias range for everything downstream. The range covers the |
| 2193 | * first devfn on the secondary bus to the last devfn on the |
| 2194 | * subordinate bus. The alias target depends on legacy versus |
| 2195 | * express bridges, just as in pci_device_iommu_address_space(). |
| 2196 | * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec. |
| 2197 | */ |
| 2198 | uint16_t dev_id_a, dev_id_b; |
| 2199 | |
| 2200 | dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)); |
| 2201 | |
| 2202 | if (pci_is_express(dev) && |
| 2203 | pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) { |
| 2204 | dev_id_b = dev_id_a; |
| 2205 | } else { |
| 2206 | dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn); |
| 2207 | } |
| 2208 | |
| 2209 | /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */ |
| 2210 | build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4); |
| 2211 | build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4); |
| 2212 | |
| 2213 | /* "End of Range" IVHD entry, type 0x4 */ |
| 2214 | entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4; |
| 2215 | build_append_int_noprefix(table_data, entry, 4); |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | /* For all PCI host bridges, walk and insert IVHD entries */ |
| 2221 | static int |
| 2222 | ivrs_host_bridges(Object *obj, void *opaque) |
| 2223 | { |
| 2224 | GArray *ivhd_blob = opaque; |
| 2225 | |
| 2226 | if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { |
| 2227 | PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus; |
| 2228 | |
| 2229 | if (bus) { |
| 2230 | pci_for_each_device(bus, pci_bus_num(bus), insert_ivhd, ivhd_blob); |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2237 | static void |
| 2238 | build_amd_iommu(GArray *table_data, BIOSLinker *linker) |
| 2239 | { |
Alex Williamson | 977aff1 | 2019-10-23 16:47:28 -0600 | [diff] [blame] | 2240 | int ivhd_table_len = 24; |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2241 | int iommu_start = table_data->len; |
| 2242 | AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default()); |
Alex Williamson | 977aff1 | 2019-10-23 16:47:28 -0600 | [diff] [blame] | 2243 | GArray *ivhd_blob = g_array_new(false, true, 1); |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2244 | |
| 2245 | /* IVRS header */ |
| 2246 | acpi_data_push(table_data, sizeof(AcpiTableHeader)); |
| 2247 | /* IVinfo - IO virtualization information common to all |
| 2248 | * IOMMU units in a system |
| 2249 | */ |
| 2250 | build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4); |
| 2251 | /* reserved */ |
| 2252 | build_append_int_noprefix(table_data, 0, 8); |
| 2253 | |
| 2254 | /* IVHD definition - type 10h */ |
| 2255 | build_append_int_noprefix(table_data, 0x10, 1); |
| 2256 | /* virtualization flags */ |
| 2257 | build_append_int_noprefix(table_data, |
| 2258 | (1UL << 0) | /* HtTunEn */ |
| 2259 | (1UL << 4) | /* iotblSup */ |
| 2260 | (1UL << 6) | /* PrefSup */ |
| 2261 | (1UL << 7), /* PPRSup */ |
| 2262 | 1); |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2263 | |
| 2264 | /* |
Alex Williamson | 977aff1 | 2019-10-23 16:47:28 -0600 | [diff] [blame] | 2265 | * A PCI bus walk, for each PCI host bridge, is necessary to create a |
| 2266 | * complete set of IVHD entries. Do this into a separate blob so that we |
| 2267 | * can calculate the total IVRS table length here and then append the new |
| 2268 | * blob further below. Fall back to an entry covering all devices, which |
| 2269 | * is sufficient when no aliases are present. |
| 2270 | */ |
| 2271 | object_child_foreach_recursive(object_get_root(), |
| 2272 | ivrs_host_bridges, ivhd_blob); |
| 2273 | |
| 2274 | if (!ivhd_blob->len) { |
| 2275 | /* |
| 2276 | * Type 1 device entry reporting all devices |
| 2277 | * These are 4-byte device entries currently reporting the range of |
| 2278 | * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) |
| 2279 | */ |
| 2280 | build_append_int_noprefix(ivhd_blob, 0x0000001, 4); |
| 2281 | } |
| 2282 | |
| 2283 | ivhd_table_len += ivhd_blob->len; |
| 2284 | |
| 2285 | /* |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2286 | * When interrupt remapping is supported, we add a special IVHD device |
| 2287 | * for type IO-APIC. |
| 2288 | */ |
Peter Xu | a924b3d | 2018-12-20 13:40:36 +0800 | [diff] [blame] | 2289 | if (x86_iommu_ir_supported(x86_iommu_get_default())) { |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2290 | ivhd_table_len += 8; |
| 2291 | } |
Alex Williamson | 977aff1 | 2019-10-23 16:47:28 -0600 | [diff] [blame] | 2292 | |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2293 | /* IVHD length */ |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2294 | build_append_int_noprefix(table_data, ivhd_table_len, 2); |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2295 | /* DeviceID */ |
| 2296 | build_append_int_noprefix(table_data, s->devid, 2); |
| 2297 | /* Capability offset */ |
| 2298 | build_append_int_noprefix(table_data, s->capab_offset, 2); |
| 2299 | /* IOMMU base address */ |
| 2300 | build_append_int_noprefix(table_data, s->mmio.addr, 8); |
| 2301 | /* PCI Segment Group */ |
| 2302 | build_append_int_noprefix(table_data, 0, 2); |
| 2303 | /* IOMMU info */ |
| 2304 | build_append_int_noprefix(table_data, 0, 2); |
| 2305 | /* IOMMU Feature Reporting */ |
| 2306 | build_append_int_noprefix(table_data, |
| 2307 | (48UL << 30) | /* HATS */ |
| 2308 | (48UL << 28) | /* GATS */ |
Singh, Brijesh | 12499b2 | 2018-10-01 19:44:45 +0000 | [diff] [blame] | 2309 | (1UL << 2) | /* GTSup */ |
| 2310 | (1UL << 6), /* GASup */ |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2311 | 4); |
Alex Williamson | 977aff1 | 2019-10-23 16:47:28 -0600 | [diff] [blame] | 2312 | |
| 2313 | /* IVHD entries as found above */ |
| 2314 | g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len); |
| 2315 | g_array_free(ivhd_blob, TRUE); |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2316 | |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2317 | /* |
| 2318 | * Add a special IVHD device type. |
| 2319 | * Refer to spec - Table 95: IVHD device entry type codes |
| 2320 | * |
| 2321 | * Linux IOMMU driver checks for the special IVHD device (type IO-APIC). |
| 2322 | * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059' |
| 2323 | */ |
Peter Xu | a924b3d | 2018-12-20 13:40:36 +0800 | [diff] [blame] | 2324 | if (x86_iommu_ir_supported(x86_iommu_get_default())) { |
Singh, Brijesh | c028818 | 2018-10-01 19:44:41 +0000 | [diff] [blame] | 2325 | build_append_int_noprefix(table_data, |
| 2326 | (0x1ull << 56) | /* type IOAPIC */ |
| 2327 | (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */ |
| 2328 | 0x48, /* special device */ |
| 2329 | 8); |
| 2330 | } |
| 2331 | |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2332 | build_header(linker, table_data, (void *)(table_data->data + iommu_start), |
| 2333 | "IVRS", table_data->len - iommu_start, 1, NULL, NULL); |
| 2334 | } |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2335 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2336 | typedef |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2337 | struct AcpiBuildState { |
| 2338 | /* Copy of table in RAM (for patching). */ |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2339 | MemoryRegion *table_mr; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2340 | /* Is table patched? */ |
| 2341 | uint8_t patched; |
Michael S. Tsirkin | d70414a | 2015-02-09 13:59:53 +0000 | [diff] [blame] | 2342 | void *rsdp; |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2343 | MemoryRegion *rsdp_mr; |
| 2344 | MemoryRegion *linker_mr; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2345 | } AcpiBuildState; |
| 2346 | |
| 2347 | static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) |
| 2348 | { |
| 2349 | Object *pci_host; |
| 2350 | QObject *o; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2351 | |
Marcel Apfelbaum | ca6c185 | 2015-06-02 14:22:59 +0300 | [diff] [blame] | 2352 | pci_host = acpi_get_i386_pci_host(); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2353 | g_assert(pci_host); |
| 2354 | |
| 2355 | o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL); |
| 2356 | if (!o) { |
| 2357 | return false; |
| 2358 | } |
Wei Yang | c309434 | 2019-04-19 08:30:50 +0800 | [diff] [blame] | 2359 | mcfg->base = qnum_get_uint(qobject_to(QNum, o)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2360 | qobject_unref(o); |
Wei Yang | c309434 | 2019-04-19 08:30:50 +0800 | [diff] [blame] | 2361 | if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) { |
Igor Mammedov | fe4970a | 2019-04-09 17:00:37 +0200 | [diff] [blame] | 2362 | return false; |
| 2363 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2364 | |
| 2365 | o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL); |
| 2366 | assert(o); |
Wei Yang | c309434 | 2019-04-19 08:30:50 +0800 | [diff] [blame] | 2367 | mcfg->size = qnum_get_uint(qobject_to(QNum, o)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2368 | qobject_unref(o); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2369 | return true; |
| 2370 | } |
| 2371 | |
| 2372 | static |
Igor Mammedov | 3d3ebca | 2016-02-26 14:59:22 +0100 | [diff] [blame] | 2373 | void acpi_build(AcpiBuildTables *tables, MachineState *machine) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2374 | { |
Igor Mammedov | 3d3ebca | 2016-02-26 14:59:22 +0100 | [diff] [blame] | 2375 | PCMachineState *pcms = PC_MACHINE(machine); |
Eduardo Habkost | bb292f5 | 2015-12-11 16:42:28 -0200 | [diff] [blame] | 2376 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2377 | X86MachineState *x86ms = X86_MACHINE(machine); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2378 | GArray *table_offsets; |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 2379 | unsigned facs, dsdt, rsdt, fadt; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2380 | AcpiPmInfo pm; |
| 2381 | AcpiMiscInfo misc; |
| 2382 | AcpiMcfgInfo mcfg; |
Markus Armbruster | 01c9742 | 2016-06-15 19:56:31 +0200 | [diff] [blame] | 2383 | Range pci_hole, pci_hole64; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2384 | uint8_t *u; |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2385 | size_t aml_len = 0; |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2386 | GArray *tables_blob = tables->table_data; |
Laszlo Ersek | ae12374 | 2016-01-18 15:12:13 +0100 | [diff] [blame] | 2387 | AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL }; |
Ben Warren | d03637b | 2017-02-16 15:15:36 -0800 | [diff] [blame] | 2388 | Object *vmgenid_dev; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2389 | |
Like Xu | 0e11fc6 | 2019-05-19 04:54:25 +0800 | [diff] [blame] | 2390 | acpi_get_pm_info(machine, &pm); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2391 | acpi_get_misc_info(&misc); |
Markus Armbruster | 01c9742 | 2016-06-15 19:56:31 +0200 | [diff] [blame] | 2392 | acpi_get_pci_holes(&pci_hole, &pci_hole64); |
Laszlo Ersek | ae12374 | 2016-01-18 15:12:13 +0100 | [diff] [blame] | 2393 | acpi_get_slic_oem(&slic_oem); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2394 | |
| 2395 | table_offsets = g_array_new(false, true /* clear */, |
| 2396 | sizeof(uint32_t)); |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2397 | ACPI_BUILD_DPRINTF("init ACPI tables\n"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2398 | |
Igor Mammedov | ad9671b | 2016-05-19 15:19:26 +0200 | [diff] [blame] | 2399 | bios_linker_loader_alloc(tables->linker, |
| 2400 | ACPI_BUILD_TABLE_FILE, tables_blob, |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2401 | 64 /* Ensure FACS is aligned */, |
| 2402 | false /* high memory */); |
| 2403 | |
| 2404 | /* |
| 2405 | * FACS is pointed to by FADT. |
| 2406 | * We place it first since it's the only table that has alignment |
| 2407 | * requirements. |
| 2408 | */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2409 | facs = tables_blob->len; |
Wei Yang | 009180b | 2019-01-30 11:02:07 +0800 | [diff] [blame] | 2410 | build_facs(tables_blob); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2411 | |
| 2412 | /* DSDT is pointed to by FADT */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2413 | dsdt = tables_blob->len; |
Markus Armbruster | 01c9742 | 2016-06-15 19:56:31 +0200 | [diff] [blame] | 2414 | build_dsdt(tables_blob, tables->linker, &pm, &misc, |
| 2415 | &pci_hole, &pci_hole64, machine); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2416 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2417 | /* Count the size of the DSDT and SSDT, we will need it for legacy |
| 2418 | * sizing of ACPI tables. |
| 2419 | */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2420 | aml_len += tables_blob->len - dsdt; |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2421 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2422 | /* ACPI tables pointed to by RSDT */ |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 2423 | fadt = tables_blob->len; |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2424 | acpi_add_table(table_offsets, tables_blob); |
Igor Mammedov | 937d1b5 | 2018-02-28 15:23:51 +0100 | [diff] [blame] | 2425 | pm.fadt.facs_tbl_offset = &facs; |
| 2426 | pm.fadt.dsdt_tbl_offset = &dsdt; |
| 2427 | pm.fadt.xdsdt_tbl_offset = &dsdt; |
| 2428 | build_fadt(tables_blob, tables->linker, &pm.fadt, |
Laszlo Ersek | ae12374 | 2016-01-18 15:12:13 +0100 | [diff] [blame] | 2429 | slic_oem.id, slic_oem.table_id); |
Igor Mammedov | 41fa5c0 | 2016-01-22 15:36:06 +0100 | [diff] [blame] | 2430 | aml_len += tables_blob->len - fadt; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2431 | |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2432 | acpi_add_table(table_offsets, tables_blob); |
Gerd Hoffmann | eb66ffa | 2020-05-20 15:19:47 +0200 | [diff] [blame] | 2433 | acpi_build_madt(tables_blob, tables->linker, x86ms, |
Gerd Hoffmann | 5794d34 | 2020-05-20 15:19:48 +0200 | [diff] [blame] | 2434 | ACPI_DEVICE_IF(pcms->acpi_dev), true); |
Michael S. Tsirkin | 9ac1c4c | 2014-04-28 08:15:32 +0300 | [diff] [blame] | 2435 | |
Ben Warren | d03637b | 2017-02-16 15:15:36 -0800 | [diff] [blame] | 2436 | vmgenid_dev = find_vmgenid_dev(); |
| 2437 | if (vmgenid_dev) { |
| 2438 | acpi_add_table(table_offsets, tables_blob); |
| 2439 | vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob, |
| 2440 | tables->vmgenid, tables->linker); |
| 2441 | } |
| 2442 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2443 | if (misc.has_hpet) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2444 | acpi_add_table(table_offsets, tables_blob); |
| 2445 | build_hpet(tables_blob, tables->linker); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2446 | } |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 2447 | if (misc.tpm_version != TPM_VERSION_UNSPEC) { |
Stefan Berger | 7e7c1b8 | 2020-05-29 15:28:40 -0400 | [diff] [blame] | 2448 | if (misc.tpm_version == TPM_VERSION_1_2) { |
| 2449 | acpi_add_table(table_offsets, tables_blob); |
| 2450 | build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog); |
| 2451 | } else { /* TPM_VERSION_2_0 */ |
Igor Mammedov | 72d97b3 | 2015-06-09 05:31:53 +0200 | [diff] [blame] | 2452 | acpi_add_table(table_offsets, tables_blob); |
Stefan Berger | 4a42fa0 | 2017-11-14 13:42:42 -0500 | [diff] [blame] | 2453 | build_tpm2(tables_blob, tables->linker, tables->tcpalog); |
Stefan Berger | 5cb18b3 | 2015-05-26 16:51:07 -0400 | [diff] [blame] | 2454 | } |
Stefan Berger | 711b20b | 2014-08-11 16:33:36 -0400 | [diff] [blame] | 2455 | } |
Eduardo Habkost | dd4c2f0 | 2015-12-11 16:42:32 -0200 | [diff] [blame] | 2456 | if (pcms->numa_nodes) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2457 | acpi_add_table(table_offsets, tables_blob); |
Igor Mammedov | 3d3ebca | 2016-02-26 14:59:22 +0100 | [diff] [blame] | 2458 | build_srat(tables_blob, tables->linker, machine); |
Tao Xu | 118154b | 2019-08-09 14:57:23 +0800 | [diff] [blame] | 2459 | if (machine->numa_state->have_numa_distance) { |
He Chen | 0f20343 | 2017-04-27 10:35:58 +0800 | [diff] [blame] | 2460 | acpi_add_table(table_offsets, tables_blob); |
Tao Xu | aa57020 | 2019-08-09 14:57:22 +0800 | [diff] [blame] | 2461 | build_slit(tables_blob, tables->linker, machine); |
He Chen | 0f20343 | 2017-04-27 10:35:58 +0800 | [diff] [blame] | 2462 | } |
Liu Jingqi | e6f123c | 2019-12-13 09:19:25 +0800 | [diff] [blame] | 2463 | if (machine->numa_state->hmat_enabled) { |
| 2464 | acpi_add_table(table_offsets, tables_blob); |
| 2465 | build_hmat(tables_blob, tables->linker, machine->numa_state); |
| 2466 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2467 | } |
| 2468 | if (acpi_get_mcfg(&mcfg)) { |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2469 | acpi_add_table(table_offsets, tables_blob); |
Wei Yang | f13a944 | 2019-05-21 14:28:35 +0800 | [diff] [blame] | 2470 | build_mcfg(tables_blob, tables->linker, &mcfg); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2471 | } |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 2472 | if (x86_iommu_get_default()) { |
| 2473 | IommuType IOMMUType = x86_iommu_get_type(); |
| 2474 | if (IOMMUType == TYPE_AMD) { |
| 2475 | acpi_add_table(table_offsets, tables_blob); |
| 2476 | build_amd_iommu(tables_blob, tables->linker); |
| 2477 | } else if (IOMMUType == TYPE_INTEL) { |
| 2478 | acpi_add_table(table_offsets, tables_blob); |
| 2479 | build_dmar_q35(tables_blob, tables->linker); |
| 2480 | } |
Le Tan | d4eb911 | 2014-08-16 13:55:39 +0800 | [diff] [blame] | 2481 | } |
Eric Auger | f6a0d06 | 2019-03-08 19:20:53 +0100 | [diff] [blame] | 2482 | if (machine->nvdimms_state->is_enabled) { |
Igor Mammedov | ad9671b | 2016-05-19 15:19:26 +0200 | [diff] [blame] | 2483 | nvdimm_build_acpi(table_offsets, tables_blob, tables->linker, |
Eric Auger | f6a0d06 | 2019-03-08 19:20:53 +0100 | [diff] [blame] | 2484 | machine->nvdimms_state, machine->ram_slots); |
Xiao Guangrong | 87252e1 | 2015-12-02 15:20:58 +0800 | [diff] [blame] | 2485 | } |
| 2486 | |
Liran Alon | 14cda35 | 2020-03-13 16:50:08 +0200 | [diff] [blame] | 2487 | acpi_add_table(table_offsets, tables_blob); |
| 2488 | build_waet(tables_blob, tables->linker); |
| 2489 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2490 | /* Add tables supplied by user (if any) */ |
| 2491 | for (u = acpi_table_first(); u; u = acpi_table_next(u)) { |
| 2492 | unsigned len = acpi_table_len(u); |
| 2493 | |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2494 | acpi_add_table(table_offsets, tables_blob); |
| 2495 | g_array_append_vals(tables_blob, u, len); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | /* RSDT is pointed to by RSDP */ |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2499 | rsdt = tables_blob->len; |
Laszlo Ersek | ae12374 | 2016-01-18 15:12:13 +0100 | [diff] [blame] | 2500 | build_rsdt(tables_blob, tables->linker, table_offsets, |
| 2501 | slic_oem.id, slic_oem.table_id); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2502 | |
| 2503 | /* RSDP is in FSEG memory, so allocate it separately */ |
Samuel Ortiz | a46ce1c | 2018-12-17 16:34:48 +0100 | [diff] [blame] | 2504 | { |
| 2505 | AcpiRsdpData rsdp_data = { |
| 2506 | .revision = 0, |
| 2507 | .oem_id = ACPI_BUILD_APPNAME6, |
| 2508 | .xsdt_tbl_offset = NULL, |
| 2509 | .rsdt_tbl_offset = &rsdt, |
| 2510 | }; |
| 2511 | build_rsdp(tables->rsdp, tables->linker, &rsdp_data); |
| 2512 | if (!pcmc->rsdp_in_ram) { |
| 2513 | /* We used to allocate some extra space for RSDP revision 2 but |
| 2514 | * only used the RSDP revision 0 space. The extra bytes were |
| 2515 | * zeroed out and not used. |
| 2516 | * Here we continue wasting those extra 16 bytes to make sure we |
| 2517 | * don't break migration for machine types 2.2 and older due to |
| 2518 | * RSDP blob size mismatch. |
| 2519 | */ |
| 2520 | build_append_int_noprefix(tables->rsdp, 0, 16); |
| 2521 | } |
| 2522 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2523 | |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2524 | /* 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] | 2525 | * chance of size changes. |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2526 | * |
| 2527 | * We used to align the tables to 4k, but of course this would |
| 2528 | * too simple to be enough. 4k turned out to be too small an |
| 2529 | * alignment very soon, and in fact it is almost impossible to |
| 2530 | * keep the table size stable for all (max_cpus, max_memory_slots) |
| 2531 | * combinations. So the table size is always 64k for pc-i440fx-2.1 |
| 2532 | * and we give an error if the table grows beyond that limit. |
| 2533 | * |
| 2534 | * We still have the problem of migrating from "-M pc-i440fx-2.0". For |
| 2535 | * that, we exploit the fact that QEMU 2.1 generates _smaller_ tables |
| 2536 | * than 2.0 and we can always pad the smaller tables with zeros. We can |
| 2537 | * then use the exact size of the 2.0 tables. |
| 2538 | * |
| 2539 | * 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] | 2540 | */ |
Eduardo Habkost | bb292f5 | 2015-12-11 16:42:28 -0200 | [diff] [blame] | 2541 | if (pcmc->legacy_acpi_table_size) { |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2542 | /* Subtracting aml_len gives the size of fixed tables. Then add the |
| 2543 | * size of the PIIX4 DSDT/SSDT in QEMU 2.0. |
| 2544 | */ |
| 2545 | int legacy_aml_len = |
Eduardo Habkost | bb292f5 | 2015-12-11 16:42:28 -0200 | [diff] [blame] | 2546 | pcmc->legacy_acpi_table_size + |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2547 | ACPI_BUILD_LEGACY_CPU_AML_SIZE * x86ms->apic_id_limit; |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2548 | int legacy_table_size = |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2549 | ROUND_UP(tables_blob->len - aml_len + legacy_aml_len, |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2550 | ACPI_BUILD_ALIGN_SIZE); |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2551 | if (tables_blob->len > legacy_table_size) { |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2552 | /* Should happen only with PCI bridges and -M pc-i440fx-2.0. */ |
Alistair Francis | 9e5d2c5 | 2017-09-11 12:52:43 -0700 | [diff] [blame] | 2553 | warn_report("ACPI table size %u exceeds %d bytes," |
| 2554 | " migration may not work", |
| 2555 | tables_blob->len, legacy_table_size); |
| 2556 | error_printf("Try removing CPUs, NUMA nodes, memory slots" |
| 2557 | " or PCI bridges."); |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2558 | } |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2559 | g_array_set_size(tables_blob, legacy_table_size); |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2560 | } else { |
Michael S. Tsirkin | 868270f | 2014-07-28 23:07:11 +0200 | [diff] [blame] | 2561 | /* 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] | 2562 | if (tables_blob->len > ACPI_BUILD_TABLE_SIZE / 2) { |
Paolo Bonzini | 18045fb | 2014-07-28 17:34:16 +0200 | [diff] [blame] | 2563 | /* As of QEMU 2.1, this fires with 160 VCPUs and 255 memory slots. */ |
Alistair Francis | 9e5d2c5 | 2017-09-11 12:52:43 -0700 | [diff] [blame] | 2564 | warn_report("ACPI table size %u exceeds %d bytes," |
| 2565 | " migration may not work", |
| 2566 | tables_blob->len, ACPI_BUILD_TABLE_SIZE / 2); |
| 2567 | error_printf("Try removing CPUs, NUMA nodes, memory slots" |
| 2568 | " or PCI bridges."); |
Paolo Bonzini | 18045fb | 2014-07-28 17:34:16 +0200 | [diff] [blame] | 2569 | } |
Igor Mammedov | 7c2c1fa | 2015-02-09 10:53:24 +0000 | [diff] [blame] | 2570 | acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE); |
Paolo Bonzini | 07fb617 | 2014-07-28 17:34:15 +0200 | [diff] [blame] | 2571 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2572 | |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 2573 | acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2574 | |
| 2575 | /* Cleanup memory that's no longer used. */ |
| 2576 | g_array_free(table_offsets, true); |
| 2577 | } |
| 2578 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2579 | static void acpi_ram_update(MemoryRegion *mr, GArray *data) |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2580 | { |
| 2581 | uint32_t size = acpi_data_len(data); |
| 2582 | |
| 2583 | /* 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] | 2584 | memory_region_ram_resize(mr, size, &error_abort); |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2585 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2586 | memcpy(memory_region_get_ram_ptr(mr), data->data, size); |
| 2587 | memory_region_set_dirty(mr, 0, size); |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
Gabriel L. Somlo | 3f8752b | 2015-11-05 09:32:49 -0500 | [diff] [blame] | 2590 | static void acpi_build_update(void *build_opaque) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2591 | { |
| 2592 | AcpiBuildState *build_state = build_opaque; |
| 2593 | AcpiBuildTables tables; |
| 2594 | |
| 2595 | /* No state to update or already patched? Nothing to do. */ |
| 2596 | if (!build_state || build_state->patched) { |
| 2597 | return; |
| 2598 | } |
| 2599 | build_state->patched = 1; |
| 2600 | |
| 2601 | acpi_build_tables_init(&tables); |
| 2602 | |
Igor Mammedov | 3d3ebca | 2016-02-26 14:59:22 +0100 | [diff] [blame] | 2603 | acpi_build(&tables, MACHINE(qdev_get_machine())); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2604 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2605 | acpi_ram_update(build_state->table_mr, tables.table_data); |
Michael S. Tsirkin | a166614 | 2014-11-17 07:51:50 +0200 | [diff] [blame] | 2606 | |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2607 | if (build_state->rsdp) { |
| 2608 | memcpy(build_state->rsdp, tables.rsdp->data, acpi_data_len(tables.rsdp)); |
| 2609 | } else { |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2610 | acpi_ram_update(build_state->rsdp_mr, tables.rsdp); |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2611 | } |
Michael S. Tsirkin | a166614 | 2014-11-17 07:51:50 +0200 | [diff] [blame] | 2612 | |
Igor Mammedov | 0e9b9ed | 2016-05-19 15:19:25 +0200 | [diff] [blame] | 2613 | acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2614 | acpi_build_tables_cleanup(&tables, true); |
| 2615 | } |
| 2616 | |
| 2617 | static void acpi_build_reset(void *build_opaque) |
| 2618 | { |
| 2619 | AcpiBuildState *build_state = build_opaque; |
| 2620 | build_state->patched = 0; |
| 2621 | } |
| 2622 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2623 | static const VMStateDescription vmstate_acpi_build = { |
| 2624 | .name = "acpi_build", |
| 2625 | .version_id = 1, |
| 2626 | .minimum_version_id = 1, |
Juan Quintela | d49805a | 2014-04-16 15:32:32 +0200 | [diff] [blame] | 2627 | .fields = (VMStateField[]) { |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2628 | VMSTATE_UINT8(patched, AcpiBuildState), |
| 2629 | VMSTATE_END_OF_LIST() |
| 2630 | }, |
| 2631 | }; |
| 2632 | |
Eduardo Habkost | fb306ff | 2015-12-11 16:42:26 -0200 | [diff] [blame] | 2633 | void acpi_setup(void) |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2634 | { |
Eduardo Habkost | fb306ff | 2015-12-11 16:42:26 -0200 | [diff] [blame] | 2635 | PCMachineState *pcms = PC_MACHINE(qdev_get_machine()); |
Eduardo Habkost | bb292f5 | 2015-12-11 16:42:28 -0200 | [diff] [blame] | 2636 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2637 | X86MachineState *x86ms = X86_MACHINE(pcms); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2638 | AcpiBuildTables tables; |
| 2639 | AcpiBuildState *build_state; |
Ben Warren | d03637b | 2017-02-16 15:15:36 -0800 | [diff] [blame] | 2640 | Object *vmgenid_dev; |
Stefan Berger | 0fe2466 | 2019-01-15 02:27:51 +0400 | [diff] [blame] | 2641 | TPMIf *tpm; |
| 2642 | static FwCfgTPMConfig tpm_config; |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2643 | |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2644 | if (!x86ms->fw_cfg) { |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2645 | ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2646 | return; |
| 2647 | } |
| 2648 | |
Wei Liu | 021746c | 2016-11-01 17:44:16 +0000 | [diff] [blame] | 2649 | if (!pcms->acpi_build_enabled) { |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2650 | ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n"); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2651 | return; |
| 2652 | } |
| 2653 | |
Gerd Hoffmann | 17e8907 | 2020-03-20 11:01:36 +0100 | [diff] [blame] | 2654 | if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) { |
Gonglei | 8b310fc | 2014-11-13 10:59:37 +0800 | [diff] [blame] | 2655 | ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n"); |
Michael S. Tsirkin | 81adc51 | 2013-11-07 14:12:05 +0200 | [diff] [blame] | 2656 | return; |
| 2657 | } |
| 2658 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2659 | build_state = g_malloc0(sizeof *build_state); |
| 2660 | |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2661 | acpi_build_tables_init(&tables); |
Igor Mammedov | 3d3ebca | 2016-02-26 14:59:22 +0100 | [diff] [blame] | 2662 | acpi_build(&tables, MACHINE(pcms)); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2663 | |
| 2664 | /* Now expose it all to Guest */ |
Wei Yang | 82f76c6 | 2019-06-10 09:18:30 +0800 | [diff] [blame] | 2665 | build_state->table_mr = acpi_add_rom_blob(acpi_build_update, |
| 2666 | build_state, tables.table_data, |
| 2667 | ACPI_BUILD_TABLE_FILE, |
| 2668 | ACPI_BUILD_TABLE_MAX_SIZE); |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2669 | assert(build_state->table_mr != NULL); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2670 | |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2671 | build_state->linker_mr = |
Wei Yang | 82f76c6 | 2019-06-10 09:18:30 +0800 | [diff] [blame] | 2672 | acpi_add_rom_blob(acpi_build_update, build_state, |
Shameer Kolothum | bac78f9 | 2020-04-03 11:18:25 +0100 | [diff] [blame] | 2673 | tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE, 0); |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2674 | |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2675 | fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE, |
Stefan Berger | 42a5b30 | 2014-10-24 13:21:04 -0400 | [diff] [blame] | 2676 | tables.tcpalog->data, acpi_data_len(tables.tcpalog)); |
| 2677 | |
Stefan Berger | 0fe2466 | 2019-01-15 02:27:51 +0400 | [diff] [blame] | 2678 | tpm = tpm_find(); |
| 2679 | if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) { |
| 2680 | tpm_config = (FwCfgTPMConfig) { |
| 2681 | .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE), |
| 2682 | .tpm_version = tpm_get_version(tpm), |
Stefan Berger | ac6dd31 | 2019-01-15 02:27:52 +0400 | [diff] [blame] | 2683 | .tpmppi_version = TPM_PPI_VERSION_1_30 |
Stefan Berger | 0fe2466 | 2019-01-15 02:27:51 +0400 | [diff] [blame] | 2684 | }; |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2685 | fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config", |
Stefan Berger | 0fe2466 | 2019-01-15 02:27:51 +0400 | [diff] [blame] | 2686 | &tpm_config, sizeof tpm_config); |
| 2687 | } |
| 2688 | |
Ben Warren | d03637b | 2017-02-16 15:15:36 -0800 | [diff] [blame] | 2689 | vmgenid_dev = find_vmgenid_dev(); |
| 2690 | if (vmgenid_dev) { |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2691 | vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg, |
Ben Warren | d03637b | 2017-02-16 15:15:36 -0800 | [diff] [blame] | 2692 | tables.vmgenid); |
| 2693 | } |
| 2694 | |
Eduardo Habkost | bb292f5 | 2015-12-11 16:42:28 -0200 | [diff] [blame] | 2695 | if (!pcmc->rsdp_in_ram) { |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2696 | /* |
| 2697 | * Keep for compatibility with old machine types. |
| 2698 | * Though RSDP is small, its contents isn't immutable, so |
Michael S. Tsirkin | afaa2e4 | 2015-02-17 10:40:30 +0100 | [diff] [blame] | 2699 | * 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] | 2700 | */ |
Michael S. Tsirkin | afaa2e4 | 2015-02-17 10:40:30 +0100 | [diff] [blame] | 2701 | uint32_t rsdp_size = acpi_data_len(tables.rsdp); |
| 2702 | |
| 2703 | build_state->rsdp = g_memdup(tables.rsdp->data, rsdp_size); |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 2704 | fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE, |
Marc-André Lureau | 5f9252f | 2017-09-11 18:59:23 +0200 | [diff] [blame] | 2705 | acpi_build_update, NULL, build_state, |
Michael S. Tsirkin | baf2d5b | 2017-01-12 19:24:14 +0100 | [diff] [blame] | 2706 | build_state->rsdp, rsdp_size, true); |
Paolo Bonzini | 339240b | 2015-03-23 10:24:16 +0100 | [diff] [blame] | 2707 | build_state->rsdp_mr = NULL; |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2708 | } else { |
Michael S. Tsirkin | 42d8590 | 2015-02-15 17:12:11 +0100 | [diff] [blame] | 2709 | build_state->rsdp = NULL; |
Wei Yang | 82f76c6 | 2019-06-10 09:18:30 +0800 | [diff] [blame] | 2710 | build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update, |
| 2711 | build_state, tables.rsdp, |
| 2712 | ACPI_BUILD_RSDP_FILE, 0); |
Igor Mammedov | 358774d | 2015-02-09 13:59:55 +0000 | [diff] [blame] | 2713 | } |
Michael S. Tsirkin | 72c194f | 2013-07-24 18:56:14 +0300 | [diff] [blame] | 2714 | |
| 2715 | qemu_register_reset(acpi_build_reset, build_state); |
| 2716 | acpi_build_reset(build_state); |
| 2717 | vmstate_register(NULL, 0, &vmstate_acpi_build, build_state); |
| 2718 | |
| 2719 | /* Cleanup tables but don't free the memory: we track it |
| 2720 | * in build_state. |
| 2721 | */ |
| 2722 | acpi_build_tables_cleanup(&tables, false); |
| 2723 | } |