2
0

fw_cfg.c 8.1 KB

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