qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

acpi: create acpi-common.c and move madt code

We'll need madt support for microvm.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Message-Id: <20200520132003.9492-7-kraxel@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Gerd Hoffmann and committed by
Michael S. Tsirkin
eb66ffab 1dc32f9a

+170 -123
+1
hw/i386/Makefile.objs
··· 16 16 obj-$(CONFIG_PC) += port92.o 17 17 18 18 obj-y += kvmvapic.o 19 + obj-$(CONFIG_ACPI) += acpi-common.o 19 20 obj-$(CONFIG_PC) += acpi-build.o
+3 -123
hw/i386/acpi-build.c
··· 24 24 #include "qapi/error.h" 25 25 #include "qapi/qmp/qnum.h" 26 26 #include "acpi-build.h" 27 + #include "acpi-common.h" 27 28 #include "qemu/bitmap.h" 28 29 #include "qemu/error-report.h" 29 30 #include "hw/pci/pci.h" ··· 88 89 #else 89 90 #define ACPI_BUILD_DPRINTF(fmt, ...) 90 91 #endif 91 - 92 - /* Default IOAPIC ID */ 93 - #define ACPI_BUILD_IOAPIC_ID 0x0 94 92 95 93 typedef struct AcpiPmInfo { 96 94 bool s3_disabled; ··· 325 323 AcpiFacsDescriptorRev1 *facs = acpi_data_push(table_data, sizeof *facs); 326 324 memcpy(&facs->signature, "FACS", 4); 327 325 facs->length = cpu_to_le32(sizeof(*facs)); 328 - } 329 - 330 - void pc_madt_cpu_entry(AcpiDeviceIf *adev, int uid, 331 - const CPUArchIdList *apic_ids, GArray *entry) 332 - { 333 - uint32_t apic_id = apic_ids->cpus[uid].arch_id; 334 - 335 - /* ACPI spec says that LAPIC entry for non present 336 - * CPU may be omitted from MADT or it must be marked 337 - * as disabled. However omitting non present CPU from 338 - * MADT breaks hotplug on linux. So possible CPUs 339 - * should be put in MADT but kept disabled. 340 - */ 341 - if (apic_id < 255) { 342 - AcpiMadtProcessorApic *apic = acpi_data_push(entry, sizeof *apic); 343 - 344 - apic->type = ACPI_APIC_PROCESSOR; 345 - apic->length = sizeof(*apic); 346 - apic->processor_id = uid; 347 - apic->local_apic_id = apic_id; 348 - if (apic_ids->cpus[uid].cpu != NULL) { 349 - apic->flags = cpu_to_le32(1); 350 - } else { 351 - apic->flags = cpu_to_le32(0); 352 - } 353 - } else { 354 - AcpiMadtProcessorX2Apic *apic = acpi_data_push(entry, sizeof *apic); 355 - 356 - apic->type = ACPI_APIC_LOCAL_X2APIC; 357 - apic->length = sizeof(*apic); 358 - apic->uid = cpu_to_le32(uid); 359 - apic->x2apic_id = cpu_to_le32(apic_id); 360 - if (apic_ids->cpus[uid].cpu != NULL) { 361 - apic->flags = cpu_to_le32(1); 362 - } else { 363 - apic->flags = cpu_to_le32(0); 364 - } 365 - } 366 - } 367 - 368 - static void 369 - build_madt(GArray *table_data, BIOSLinker *linker, 370 - X86MachineState *x86ms, AcpiDeviceIf *adev) 371 - { 372 - MachineClass *mc = MACHINE_GET_CLASS(x86ms); 373 - const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(MACHINE(x86ms)); 374 - int madt_start = table_data->len; 375 - AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(adev); 376 - bool x2apic_mode = false; 377 - 378 - AcpiMultipleApicTable *madt; 379 - AcpiMadtIoApic *io_apic; 380 - AcpiMadtIntsrcovr *intsrcovr; 381 - int i; 382 - 383 - madt = acpi_data_push(table_data, sizeof *madt); 384 - madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS); 385 - madt->flags = cpu_to_le32(1); 386 - 387 - for (i = 0; i < apic_ids->len; i++) { 388 - adevc->madt_cpu(adev, i, apic_ids, table_data); 389 - if (apic_ids->cpus[i].arch_id > 254) { 390 - x2apic_mode = true; 391 - } 392 - } 393 - 394 - io_apic = acpi_data_push(table_data, sizeof *io_apic); 395 - io_apic->type = ACPI_APIC_IO; 396 - io_apic->length = sizeof(*io_apic); 397 - io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID; 398 - io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS); 399 - io_apic->interrupt = cpu_to_le32(0); 400 - 401 - if (x86ms->apic_xrupt_override) { 402 - intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr); 403 - intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE; 404 - intsrcovr->length = sizeof(*intsrcovr); 405 - intsrcovr->source = 0; 406 - intsrcovr->gsi = cpu_to_le32(2); 407 - intsrcovr->flags = cpu_to_le16(0); /* conforms to bus specifications */ 408 - } 409 - for (i = 1; i < 16; i++) { 410 - #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11)) 411 - if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) { 412 - /* No need for a INT source override structure. */ 413 - continue; 414 - } 415 - intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr); 416 - intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE; 417 - intsrcovr->length = sizeof(*intsrcovr); 418 - intsrcovr->source = i; 419 - intsrcovr->gsi = cpu_to_le32(i); 420 - intsrcovr->flags = cpu_to_le16(0xd); /* active high, level triggered */ 421 - } 422 - 423 - if (x2apic_mode) { 424 - AcpiMadtLocalX2ApicNmi *local_nmi; 425 - 426 - local_nmi = acpi_data_push(table_data, sizeof *local_nmi); 427 - local_nmi->type = ACPI_APIC_LOCAL_X2APIC_NMI; 428 - local_nmi->length = sizeof(*local_nmi); 429 - local_nmi->uid = 0xFFFFFFFF; /* all processors */ 430 - local_nmi->flags = cpu_to_le16(0); 431 - local_nmi->lint = 1; /* ACPI_LINT1 */ 432 - } else { 433 - AcpiMadtLocalNmi *local_nmi; 434 - 435 - local_nmi = acpi_data_push(table_data, sizeof *local_nmi); 436 - local_nmi->type = ACPI_APIC_LOCAL_NMI; 437 - local_nmi->length = sizeof(*local_nmi); 438 - local_nmi->processor_id = 0xff; /* all processors */ 439 - local_nmi->flags = cpu_to_le16(0); 440 - local_nmi->lint = 1; /* ACPI_LINT1 */ 441 - } 442 - 443 - build_header(linker, table_data, 444 - (void *)(table_data->data + madt_start), "APIC", 445 - table_data->len - madt_start, 1, NULL, NULL); 446 326 } 447 327 448 328 static void build_append_pcihp_notify_entry(Aml *method, int slot) ··· 2707 2587 aml_len += tables_blob->len - fadt; 2708 2588 2709 2589 acpi_add_table(table_offsets, tables_blob); 2710 - build_madt(tables_blob, tables->linker, x86ms, 2711 - ACPI_DEVICE_IF(pcms->acpi_dev)); 2590 + acpi_build_madt(tables_blob, tables->linker, x86ms, 2591 + ACPI_DEVICE_IF(pcms->acpi_dev)); 2712 2592 2713 2593 vmgenid_dev = find_vmgenid_dev(); 2714 2594 if (vmgenid_dev) {
+152
hw/i386/acpi-common.c
··· 1 + /* Support for generating ACPI tables and passing them to Guests 2 + * 3 + * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net> 4 + * Copyright (C) 2006 Fabrice Bellard 5 + * Copyright (C) 2013 Red Hat Inc 6 + * 7 + * Author: Michael S. Tsirkin <mst@redhat.com> 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License as published by 11 + * the Free Software Foundation; either version 2 of the License, or 12 + * (at your option) any later version. 13 + 14 + * This program is distributed in the hope that it will be useful, 15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 + * GNU General Public License for more details. 18 + 19 + * You should have received a copy of the GNU General Public License along 20 + * with this program; if not, see <http://www.gnu.org/licenses/>. 21 + */ 22 + 23 + #include "qemu/osdep.h" 24 + #include "qapi/error.h" 25 + 26 + #include "exec/memory.h" 27 + #include "hw/acpi/acpi.h" 28 + #include "hw/acpi/aml-build.h" 29 + #include "hw/acpi/utils.h" 30 + #include "hw/i386/pc.h" 31 + #include "target/i386/cpu.h" 32 + 33 + #include "acpi-build.h" 34 + #include "acpi-common.h" 35 + 36 + void pc_madt_cpu_entry(AcpiDeviceIf *adev, int uid, 37 + const CPUArchIdList *apic_ids, GArray *entry) 38 + { 39 + uint32_t apic_id = apic_ids->cpus[uid].arch_id; 40 + 41 + /* ACPI spec says that LAPIC entry for non present 42 + * CPU may be omitted from MADT or it must be marked 43 + * as disabled. However omitting non present CPU from 44 + * MADT breaks hotplug on linux. So possible CPUs 45 + * should be put in MADT but kept disabled. 46 + */ 47 + if (apic_id < 255) { 48 + AcpiMadtProcessorApic *apic = acpi_data_push(entry, sizeof *apic); 49 + 50 + apic->type = ACPI_APIC_PROCESSOR; 51 + apic->length = sizeof(*apic); 52 + apic->processor_id = uid; 53 + apic->local_apic_id = apic_id; 54 + if (apic_ids->cpus[uid].cpu != NULL) { 55 + apic->flags = cpu_to_le32(1); 56 + } else { 57 + apic->flags = cpu_to_le32(0); 58 + } 59 + } else { 60 + AcpiMadtProcessorX2Apic *apic = acpi_data_push(entry, sizeof *apic); 61 + 62 + apic->type = ACPI_APIC_LOCAL_X2APIC; 63 + apic->length = sizeof(*apic); 64 + apic->uid = cpu_to_le32(uid); 65 + apic->x2apic_id = cpu_to_le32(apic_id); 66 + if (apic_ids->cpus[uid].cpu != NULL) { 67 + apic->flags = cpu_to_le32(1); 68 + } else { 69 + apic->flags = cpu_to_le32(0); 70 + } 71 + } 72 + } 73 + 74 + void acpi_build_madt(GArray *table_data, BIOSLinker *linker, 75 + X86MachineState *x86ms, AcpiDeviceIf *adev) 76 + { 77 + MachineClass *mc = MACHINE_GET_CLASS(x86ms); 78 + const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(MACHINE(x86ms)); 79 + int madt_start = table_data->len; 80 + AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(adev); 81 + bool x2apic_mode = false; 82 + 83 + AcpiMultipleApicTable *madt; 84 + AcpiMadtIoApic *io_apic; 85 + AcpiMadtIntsrcovr *intsrcovr; 86 + int i; 87 + 88 + madt = acpi_data_push(table_data, sizeof *madt); 89 + madt->local_apic_address = cpu_to_le32(APIC_DEFAULT_ADDRESS); 90 + madt->flags = cpu_to_le32(1); 91 + 92 + for (i = 0; i < apic_ids->len; i++) { 93 + adevc->madt_cpu(adev, i, apic_ids, table_data); 94 + if (apic_ids->cpus[i].arch_id > 254) { 95 + x2apic_mode = true; 96 + } 97 + } 98 + 99 + io_apic = acpi_data_push(table_data, sizeof *io_apic); 100 + io_apic->type = ACPI_APIC_IO; 101 + io_apic->length = sizeof(*io_apic); 102 + io_apic->io_apic_id = ACPI_BUILD_IOAPIC_ID; 103 + io_apic->address = cpu_to_le32(IO_APIC_DEFAULT_ADDRESS); 104 + io_apic->interrupt = cpu_to_le32(0); 105 + 106 + if (x86ms->apic_xrupt_override) { 107 + intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr); 108 + intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE; 109 + intsrcovr->length = sizeof(*intsrcovr); 110 + intsrcovr->source = 0; 111 + intsrcovr->gsi = cpu_to_le32(2); 112 + intsrcovr->flags = cpu_to_le16(0); /* conforms to bus specifications */ 113 + } 114 + for (i = 1; i < 16; i++) { 115 + #define ACPI_BUILD_PCI_IRQS ((1<<5) | (1<<9) | (1<<10) | (1<<11)) 116 + if (!(ACPI_BUILD_PCI_IRQS & (1 << i))) { 117 + /* No need for a INT source override structure. */ 118 + continue; 119 + } 120 + intsrcovr = acpi_data_push(table_data, sizeof *intsrcovr); 121 + intsrcovr->type = ACPI_APIC_XRUPT_OVERRIDE; 122 + intsrcovr->length = sizeof(*intsrcovr); 123 + intsrcovr->source = i; 124 + intsrcovr->gsi = cpu_to_le32(i); 125 + intsrcovr->flags = cpu_to_le16(0xd); /* active high, level triggered */ 126 + } 127 + 128 + if (x2apic_mode) { 129 + AcpiMadtLocalX2ApicNmi *local_nmi; 130 + 131 + local_nmi = acpi_data_push(table_data, sizeof *local_nmi); 132 + local_nmi->type = ACPI_APIC_LOCAL_X2APIC_NMI; 133 + local_nmi->length = sizeof(*local_nmi); 134 + local_nmi->uid = 0xFFFFFFFF; /* all processors */ 135 + local_nmi->flags = cpu_to_le16(0); 136 + local_nmi->lint = 1; /* ACPI_LINT1 */ 137 + } else { 138 + AcpiMadtLocalNmi *local_nmi; 139 + 140 + local_nmi = acpi_data_push(table_data, sizeof *local_nmi); 141 + local_nmi->type = ACPI_APIC_LOCAL_NMI; 142 + local_nmi->length = sizeof(*local_nmi); 143 + local_nmi->processor_id = 0xff; /* all processors */ 144 + local_nmi->flags = cpu_to_le16(0); 145 + local_nmi->lint = 1; /* ACPI_LINT1 */ 146 + } 147 + 148 + build_header(linker, table_data, 149 + (void *)(table_data->data + madt_start), "APIC", 150 + table_data->len - madt_start, 1, NULL, NULL); 151 + } 152 +
+14
hw/i386/acpi-common.h
··· 1 + #ifndef HW_I386_ACPI_COMMON_H 2 + #define HW_I386_ACPI_COMMON_H 3 + #include "include/hw/acpi/acpi_dev_interface.h" 4 + 5 + #include "include/hw/acpi/bios-linker-loader.h" 6 + #include "include/hw/i386/x86.h" 7 + 8 + /* Default IOAPIC ID */ 9 + #define ACPI_BUILD_IOAPIC_ID 0x0 10 + 11 + void acpi_build_madt(GArray *table_data, BIOSLinker *linker, 12 + X86MachineState *x86ms, AcpiDeviceIf *adev); 13 + 14 + #endif