1 /* 2 * QEMU fw_cfg helpers (X86 specific) 3 * 4 * Copyright (c) 2019 Red Hat, Inc. 5 * 6 * Author: 7 * Philippe Mathieu-Daudé <philmd@redhat.com> 8 * 9 * SPDX-License-Identifier: GPL-2.0-or-later 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2 or later. 12 * See the COPYING file in the top-level directory. 13 */ 14 15 #include "qemu/osdep.h" 16 #include "sysemu/numa.h" 17 #include "hw/acpi/acpi.h" 18 #include "hw/acpi/aml-build.h" 19 #include "hw/firmware/smbios.h" 20 #include "hw/i386/fw_cfg.h" 21 #include "hw/timer/hpet.h" 22 #include "hw/nvram/fw_cfg.h" 23 #include "e820_memory_layout.h" 24 #include "kvm/kvm_i386.h" 25 #include "qapi/error.h" 26 #include CONFIG_DEVICES 27 #include "target/i386/cpu.h" 28 29 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX}; 30 31 const char *fw_cfg_arch_key_name(uint16_t key) 32 { 33 static const struct { 34 uint16_t key; 35 const char *name; 36 } fw_cfg_arch_wellknown_keys[] = { 37 {FW_CFG_ACPI_TABLES, "acpi_tables"}, 38 {FW_CFG_SMBIOS_ENTRIES, "smbios_entries"}, 39 {FW_CFG_IRQ0_OVERRIDE, "irq0_override"}, 40 {FW_CFG_HPET, "hpet"}, 41 }; 42 43 for (size_t i = 0; i < ARRAY_SIZE(fw_cfg_arch_wellknown_keys); i++) { 44 if (fw_cfg_arch_wellknown_keys[i].key == key) { 45 return fw_cfg_arch_wellknown_keys[i].name; 46 } 47 } 48 return NULL; 49 } 50 51 void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState *fw_cfg) 52 { 53 #ifdef CONFIG_SMBIOS 54 uint8_t *smbios_tables, *smbios_anchor; 55 size_t smbios_tables_len, smbios_anchor_len; 56 struct smbios_phys_mem_area *mem_array; 57 unsigned i, array_count; 58 MachineState *ms = MACHINE(pcms); 59 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 60 MachineClass *mc = MACHINE_GET_CLASS(pcms); 61 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu); 62 63 if (pcmc->smbios_defaults) { 64 /* These values are guest ABI, do not change */ 65 smbios_set_defaults("QEMU", mc->desc, mc->name, 66 pcmc->smbios_legacy_mode, pcmc->smbios_uuid_encoded, 67 pcms->smbios_entry_point_type); 68 } 69 70 /* tell smbios about cpuid version and features */ 71 smbios_set_cpuid(cpu->env.cpuid_version, cpu->env.features[FEAT_1_EDX]); 72 73 smbios_tables = smbios_get_table_legacy(ms, &smbios_tables_len); 74 if (smbios_tables) { 75 fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES, 76 smbios_tables, smbios_tables_len); 77 } 78 79 /* build the array of physical mem area from e820 table */ 80 mem_array = g_malloc0(sizeof(*mem_array) * e820_get_num_entries()); 81 for (i = 0, array_count = 0; i < e820_get_num_entries(); i++) { 82 uint64_t addr, len; 83 84 if (e820_get_entry(i, E820_RAM, &addr, &len)) { 85 mem_array[array_count].address = addr; 86 mem_array[array_count].length = len; 87 array_count++; 88 } 89 } 90 smbios_get_tables(ms, mem_array, array_count, 91 &smbios_tables, &smbios_tables_len, 92 &smbios_anchor, &smbios_anchor_len, 93 &error_fatal); 94 g_free(mem_array); 95 96 if (smbios_anchor) { 97 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables", 98 smbios_tables, smbios_tables_len); 99 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor", 100 smbios_anchor, smbios_anchor_len); 101 } 102 #endif 103 } 104 105 FWCfgState *fw_cfg_arch_create(MachineState *ms, 106 uint16_t boot_cpus, 107 uint16_t apic_id_limit) 108 { 109 FWCfgState *fw_cfg; 110 uint64_t *numa_fw_cfg; 111 int i; 112 MachineClass *mc = MACHINE_GET_CLASS(ms); 113 const CPUArchIdList *cpus = mc->possible_cpu_arch_ids(ms); 114 int nb_numa_nodes = ms->numa_state->num_nodes; 115 116 fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4, 117 &address_space_memory); 118 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, boot_cpus); 119 120 /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86: 121 * 122 * For machine types prior to 1.8, SeaBIOS needs FW_CFG_MAX_CPUS for 123 * building MPTable, ACPI MADT, ACPI CPU hotplug and ACPI SRAT table, 124 * that tables are based on xAPIC ID and QEMU<->SeaBIOS interface 125 * for CPU hotplug also uses APIC ID and not "CPU index". 126 * This means that FW_CFG_MAX_CPUS is not the "maximum number of CPUs", 127 * but the "limit to the APIC ID values SeaBIOS may see". 128 * 129 * So for compatibility reasons with old BIOSes we are stuck with 130 * "etc/max-cpus" actually being apic_id_limit 131 */ 132 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, apic_id_limit); 133 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, ms->ram_size); 134 #ifdef CONFIG_ACPI 135 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, 136 acpi_tables, acpi_tables_len); 137 #endif 138 fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1); 139 140 fw_cfg_add_file(fw_cfg, "etc/e820", e820_table, 141 sizeof(struct e820_entry) * e820_get_num_entries()); 142 143 fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg)); 144 /* allocate memory for the NUMA channel: one (64bit) word for the number 145 * of nodes, one word for each VCPU->node and one word for each node to 146 * hold the amount of memory. 147 */ 148 numa_fw_cfg = g_new0(uint64_t, 1 + apic_id_limit + nb_numa_nodes); 149 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes); 150 for (i = 0; i < cpus->len; i++) { 151 unsigned int apic_id = cpus->cpus[i].arch_id; 152 assert(apic_id < apic_id_limit); 153 numa_fw_cfg[apic_id + 1] = cpu_to_le64(cpus->cpus[i].props.node_id); 154 } 155 for (i = 0; i < nb_numa_nodes; i++) { 156 numa_fw_cfg[apic_id_limit + 1 + i] = 157 cpu_to_le64(ms->numa_state->nodes[i].node_mem); 158 } 159 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg, 160 (1 + apic_id_limit + nb_numa_nodes) * 161 sizeof(*numa_fw_cfg)); 162 163 return fw_cfg; 164 } 165 166 void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg) 167 { 168 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu); 169 CPUX86State *env = &cpu->env; 170 uint32_t unused, ebx, ecx, edx; 171 uint64_t feature_control_bits = 0; 172 uint64_t *val; 173 174 cpu_x86_cpuid(env, 1, 0, &unused, &unused, &ecx, &edx); 175 if (ecx & CPUID_EXT_VMX) { 176 feature_control_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; 177 } 178 179 if ((edx & (CPUID_EXT2_MCE | CPUID_EXT2_MCA)) == 180 (CPUID_EXT2_MCE | CPUID_EXT2_MCA) && 181 (env->mcg_cap & MCG_LMCE_P)) { 182 feature_control_bits |= FEATURE_CONTROL_LMCE; 183 } 184 185 if (env->cpuid_level >= 7) { 186 cpu_x86_cpuid(env, 0x7, 0, &unused, &ebx, &ecx, &unused); 187 if (ebx & CPUID_7_0_EBX_SGX) { 188 feature_control_bits |= FEATURE_CONTROL_SGX; 189 } 190 if (ecx & CPUID_7_0_ECX_SGX_LC) { 191 feature_control_bits |= FEATURE_CONTROL_SGX_LC; 192 } 193 } 194 195 if (!feature_control_bits) { 196 return; 197 } 198 199 val = g_malloc(sizeof(*val)); 200 *val = cpu_to_le64(feature_control_bits | FEATURE_CONTROL_LOCKED); 201 fw_cfg_add_file(fw_cfg, "etc/msr_feature_control", val, sizeof(*val)); 202 } 203 204 void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg) 205 { 206 /* 207 * when using port i/o, the 8-bit data register *always* overlaps 208 * with half of the 16-bit control register. Hence, the total size 209 * of the i/o region used is FW_CFG_CTL_SIZE; when using DMA, the 210 * DMA control register is located at FW_CFG_DMA_IO_BASE + 4 211 */ 212 Object *obj = OBJECT(fw_cfg); 213 uint8_t io_size = object_property_get_bool(obj, "dma_enabled", NULL) ? 214 ROUND_UP(FW_CFG_CTL_SIZE, 4) + sizeof(dma_addr_t) : 215 FW_CFG_CTL_SIZE; 216 Aml *dev = aml_device("FWCF"); 217 Aml *crs = aml_resource_template(); 218 219 aml_append(dev, aml_name_decl("_HID", aml_string("QEMU0002"))); 220 221 /* device present, functioning, decoding, not shown in UI */ 222 aml_append(dev, aml_name_decl("_STA", aml_int(0xB))); 223 224 aml_append(crs, 225 aml_io(AML_DECODE16, FW_CFG_IO_BASE, FW_CFG_IO_BASE, 0x01, io_size)); 226 227 aml_append(dev, aml_name_decl("_CRS", crs)); 228 aml_append(scope, dev); 229 } 230