Skip to contentSkip to conversation

multiboot: Use DMA instead port-based transfer

PATCH In review Paolo Bonzini 3 participants 8 messages Oct 20, 2021 – Oct 21, 2021 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover multiboot: Use DMA instead port-based transfer

An updated version of the patch at
https://patchew.org/QEMU/[email protected]/,
which includes the necessary glue for compatibility with older machine types. When fw_cfg DMA is disabled, the existing ROM is used instead.

Marcus Hähnel (1):
optionrom: add a DMA-enabled multiboot ROM

Paolo Bonzini (2):
target/i386: move linuxboot_dma_enabled to X86MachineState target/i386: use DMA-enabled multiboot ROM for new-enough QEMU machine
types

 hw/i386/microvm.c                 |   5 ++-
 hw/i386/multiboot.c               |  10 ++++-
 hw/i386/multiboot.h               |   4 +-
 hw/i386/pc.c                      |   8 ++--
 hw/i386/pc_piix.c                 |   3 +-
 hw/i386/pc_q35.c                  |   3 +-
 hw/i386/x86.c                     |   7 ++--
 include/hw/i386/pc.h              |   3 --
 include/hw/i386/x86.h             |   5 ++-
 pc-bios/meson.build               |   1 +
 pc-bios/multiboot_dma.bin         | Bin 0 -> 1024 bytes
 pc-bios/optionrom/Makefile        |   4 +-
 pc-bios/optionrom/multiboot.S     |   4 +-
 pc-bios/optionrom/multiboot_dma.S |   2 +
 pc-bios/optionrom/optionrom.h     |  66 ++++++++++++++++++++++++++++++
 15 files changed, 102 insertions(+), 23 deletions(-)
 create mode 100644 pc-bios/multiboot_dma.bin
 create mode 100644 pc-bios/optionrom/multiboot_dma.S
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/3 target/i386: move linuxboot_dma_enabled to X86MachineState

This removes a parameter from x86_load_linux, and will avoid code duplication between the linux and multiboot cases once multiboot starts to support DMA.

---
 hw/i386/microvm.c     | 5 ++++-
 hw/i386/pc.c          | 5 ++---
 hw/i386/pc_piix.c     | 3 ++-
 hw/i386/pc_q35.c      | 3 ++-
 hw/i386/x86.c         | 5 +++--
 include/hw/i386/pc.h  | 3 ---
 include/hw/i386/x86.h | 5 +++--
 7 files changed, 16 insertions(+), 13 deletions(-)
hw/i386/microvm.c +4 -1
index f257ec5a0b..837bafb64a 100644
@@ -331,7 +331,7 @@ static void microvm_memory_init(MicrovmMachineState *mms)
     rom_set_fw(fw_cfg);
 
     if (machine->kernel_filename != NULL) {
-        x86_load_linux(x86ms, fw_cfg, 0, true, true);
+        x86_load_linux(x86ms, fw_cfg, 0, true);
     }
 
     if (mms->option_roms) {
@@ -667,6 +667,7 @@ static void microvm_machine_initfn(Object *obj)
 
 static void microvm_class_init(ObjectClass *oc, void *data)
 {
+    X86MachineClass *x86mc = X86_MACHINE_CLASS(oc);
     MachineClass *mc = MACHINE_CLASS(oc);
     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
 
@@ -697,6 +698,8 @@ static void microvm_class_init(ObjectClass *oc, void *data)
     hc->unplug_request = microvm_device_unplug_request_cb;
     hc->unplug = microvm_device_unplug_cb;
 
+    x86mc->fwcfg_dma_enabled = true;
+
     object_class_property_add(oc, MICROVM_MACHINE_PIC, "OnOffAuto",
                               microvm_machine_get_pic,
                               microvm_machine_set_pic,
hw/i386/pc.c +2 -3
index 86223acfd3..d203db7845 100644
@@ -775,7 +775,7 @@ void xen_load_linux(PCMachineState *pcms)
     rom_set_fw(fw_cfg);
 
     x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
-                   pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled);
+                   pcmc->pvh_enabled);
     for (i = 0; i < nb_option_roms; i++) {
         assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
                !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
@@ -927,7 +927,7 @@ void pc_memory_init(PCMachineState *pcms,
 
     if (linux_boot) {
         x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
-                       pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled);
+                       pcmc->pvh_enabled);
     }
 
     for (i = 0; i < nb_option_roms; i++) {
@@ -1664,7 +1664,6 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
      * to be used at the moment, 32K should be enough for a while.  */
     pcmc->acpi_data_size = 0x20000 + 0x8000;
-    pcmc->linuxboot_dma_enabled = true;
     pcmc->pvh_enabled = true;
     pcmc->kvmclock_create_always = true;
     assert(!mc->get_hotplug_handler);
hw/i386/pc_piix.c +2 -1
index 6ad0d763c5..223dd3e05d 100644
@@ -620,11 +620,12 @@ DEFINE_I440FX_MACHINE(v2_7, "pc-i440fx-2.7", NULL,
 
 static void pc_i440fx_2_6_machine_options(MachineClass *m)
 {
+    X86MachineClass *x86mc = X86_MACHINE_CLASS(m);
     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
 
     pc_i440fx_2_7_machine_options(m);
     pcmc->legacy_cpu_hotplug = true;
-    pcmc->linuxboot_dma_enabled = false;
+    x86mc->fwcfg_dma_enabled = false;
     compat_props_add(m->compat_props, hw_compat_2_6, hw_compat_2_6_len);
     compat_props_add(m->compat_props, pc_compat_2_6, pc_compat_2_6_len);
 }
hw/i386/pc_q35.c +2 -1
index fcc6e4eb2b..797e09500b 100644
@@ -573,11 +573,12 @@ DEFINE_Q35_MACHINE(v2_7, "pc-q35-2.7", NULL,
 
 static void pc_q35_2_6_machine_options(MachineClass *m)
 {
+    X86MachineClass *x86mc = X86_MACHINE_CLASS(m);
     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
 
     pc_q35_2_7_machine_options(m);
     pcmc->legacy_cpu_hotplug = true;
-    pcmc->linuxboot_dma_enabled = false;
+    x86mc->fwcfg_dma_enabled = false;
     compat_props_add(m->compat_props, hw_compat_2_6, hw_compat_2_6_len);
     compat_props_add(m->compat_props, pc_compat_2_6, pc_compat_2_6_len);
 }
hw/i386/x86.c +3 -2
index 76de7e2265..a34498fe16 100644
@@ -764,9 +764,9 @@ static bool load_elfboot(const char *kernel_filename,
 void x86_load_linux(X86MachineState *x86ms,
                     FWCfgState *fw_cfg,
                     int acpi_data_size,
-                    bool pvh_enabled,
-                    bool linuxboot_dma_enabled)
+                    bool pvh_enabled)
 {
+    bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
     uint16_t protocol;
     int setup_size, kernel_size, cmdline_size;
     int dtb_size, setup_data_offset;
@@ -1332,6 +1332,7 @@ static void x86_machine_class_init(ObjectClass *oc, void *data)
     mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
     x86mc->compat_apic_id_mode = false;
     x86mc->save_tsc_khz = true;
+    x86mc->fwcfg_dma_enabled = true;
     nc->nmi_monitor_handler = x86_nmi;
 
     object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto",
include/hw/i386/pc.h +0 -3
index 11426e26dc..9162aded21 100644
@@ -116,9 +116,6 @@ struct PCMachineClass {
     /* generate legacy CPU hotplug AML */
     bool legacy_cpu_hotplug;
 
-    /* use DMA capable linuxboot option rom */
-    bool linuxboot_dma_enabled;
-
     /* use PVH to load kernels that support this feature */
     bool pvh_enabled;
 
include/hw/i386/x86.h +3 -2
index 23267a3674..bb1cfb8896 100644
@@ -38,6 +38,8 @@ struct X86MachineClass {
     bool save_tsc_khz;
     /* Enables contiguous-apic-ID mode */
     bool compat_apic_id_mode;
+    /* use DMA capable linuxboot option rom */
+    bool fwcfg_dma_enabled;
 };
 
 struct X86MachineState {
@@ -120,8 +122,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware,
 void x86_load_linux(X86MachineState *x86ms,
                     FWCfgState *fw_cfg,
                     int acpi_data_size,
-                    bool pvh_enabled,
-                    bool linuxboot_dma_enabled);
+                    bool pvh_enabled);
 
 bool x86_machine_is_smm_enabled(const X86MachineState *x86ms);
 bool x86_machine_is_acpi_enabled(const X86MachineState *x86ms);
Signature · 1 line
2.31.1
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH 1/3] target/i386: move linuxboot_dma_enabled to X86MachineState

Quoted 5 lines · Paolo Bonzini

This removes a parameter from x86_load_linux, and will avoid code duplication between the linux and multiboot cases once multiboot starts to support DMA.

Quoted 9 lines
---
 hw/i386/microvm.c     | 5 ++++-
 hw/i386/pc.c          | 5 ++---
 hw/i386/pc_piix.c     | 3 ++-
 hw/i386/pc_q35.c      | 3 ++-
 hw/i386/x86.c         | 5 +++--
 include/hw/i386/pc.h  | 3 ---
 include/hw/i386/x86.h | 5 +++--
 7 files changed, 16 insertions(+), 13 deletions(-)
#3 lore raw

PATCH v2 2/3 optionrom: add a DMA-enabled multiboot ROM

In reply to Paolo Bonzini

From: Marcus Hähnel <[email protected]>

Add a new option rom for the multiboot loader, using DMA transfers to copy data instead of "rep insb".

This significantly lowers QEMU's startup latency by a factor of about 40, for example, going from 30sec to 0.8sec when loading modules of 120MB in size.

[Modified to keep the non-DMA code depending on #ifdef USE_FW_CFG_DMA;
do not write below stack. - Paolo]

---
 pc-bios/meson.build               |   1 +
 pc-bios/multiboot_dma.bin         | Bin 0 -> 1024 bytes
 pc-bios/optionrom/Makefile        |   4 +-
 pc-bios/optionrom/multiboot.S     |   4 +-
 pc-bios/optionrom/multiboot_dma.S |   2 +
 pc-bios/optionrom/optionrom.h     |  66 ++++++++++++++++++++++++++++++
 6 files changed, 72 insertions(+), 5 deletions(-)
 create mode 100644 pc-bios/multiboot_dma.bin
 create mode 100644 pc-bios/optionrom/multiboot_dma.S
pc-bios/meson.build +1 -0
index a44c9bc127..b40ff3f2bd 100644
@@ -63,6 +63,7 @@ blobs = files(
   'petalogix-s3adsp1800.dtb',
   'petalogix-ml605.dtb',
   'multiboot.bin',
+  'multiboot_dma.bin',
   'linuxboot.bin',
   'linuxboot_dma.bin',
   'kvmvapic.bin',
pc-bios/multiboot_dma.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c0e2c3102a3358207c61d3ae113524fb6007abc3
GIT binary patch
pc-bios/optionrom/Makefile +1 -3
index 3482508a86..5d55d25acc 100644
@@ -2,7 +2,7 @@ include config.mak
 SRC_DIR := $(TOPSRC_DIR)/pc-bios/optionrom
 VPATH = $(SRC_DIR)
 
-all: multiboot.bin linuxboot.bin linuxboot_dma.bin kvmvapic.bin pvh.bin
+all: multiboot.bin multiboot_dma.bin linuxboot.bin linuxboot_dma.bin kvmvapic.bin pvh.bin
 # Dummy command so that make thinks it has done something
 	@true
 
@@ -41,8 +41,6 @@ override CFLAGS += $(call cc-option, $(Wa)-32)
 LD_I386_EMULATION ?= elf_i386
 override LDFLAGS = -m $(LD_I386_EMULATION) -T $(SRC_DIR)/flat.lds
 
-all: multiboot.bin linuxboot.bin linuxboot_dma.bin kvmvapic.bin pvh.bin
-
 pvh.img: pvh.o pvh_main.o
 
 %.o: %.S
pc-bios/optionrom/multiboot.S +2 -2
index b7efe4de34..181a4b03a3 100644
@@ -68,7 +68,7 @@ run_multiboot:
 	mov		%eax, %es
 
 	/* Read the bootinfo struct into RAM */
-	read_fw_blob(FW_CFG_INITRD)
+	read_fw_blob_dma(FW_CFG_INITRD)
 
 	/* FS = bootinfo_struct */
 	read_fw		FW_CFG_INITRD_ADDR
@@ -188,7 +188,7 @@ prot_mode:
 	movl		%eax, %gs
 
 	/* Read the kernel and modules into RAM */
-	read_fw_blob(FW_CFG_KERNEL)
+	read_fw_blob_dma(FW_CFG_KERNEL)
 
 	/* Jump off to the kernel */
 	read_fw		FW_CFG_KERNEL_ENTRY
pc-bios/optionrom/multiboot_dma.S new file +2 -0
new file mode 100644
index 0000000000..d809af3e23
@@ -0,0 +1,2 @@
+#define USE_FW_CFG_DMA 1
+#include "multiboot.S"
pc-bios/optionrom/optionrom.h +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.31.1
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH 2/3] optionrom: add a DMA-enabled multiboot ROM

pc-bios/multiboot_dma.bin

Quoted 40 lines · Paolo Bonzini

From: Marcus Hähnel <[email protected]>

Add a new option rom for the multiboot loader, using DMA transfers to copy data instead of "rep insb".

This significantly lowers QEMU's startup latency by a factor of about 40, for example, going from 30sec to 0.8sec when loading modules of 120MB in size.

[Modified to keep the non-DMA code depending on #ifdef USE_FW_CFG_DMA;
do not write below stack. - Paolo]

---
 pc-bios/meson.build               |   1 +
 pc-bios/multiboot_dma.bin         | Bin 0 -> 1024 bytes
 pc-bios/optionrom/Makefile        |   4 +-
 pc-bios/optionrom/multiboot.S     |   4 +-
 pc-bios/optionrom/multiboot_dma.S |   2 +
 pc-bios/optionrom/optionrom.h     |  66 ++++++++++++++++++++++++++++++
 6 files changed, 72 insertions(+), 5 deletions(-)
 create mode 100644 pc-bios/multiboot_dma.bin
 create mode 100644 pc-bios/optionrom/multiboot_dma.S
pc-bios/multiboot_dma.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c0e2c3102a3358207c61d3ae113524fb6007abc3
GIT binary patch

Ideally this should be generated on CI as artifact, archived and we commit the CI generated file.

Quoted 37 lines
pc-bios/optionrom/optionrom.h +22 -0
index a2b612f1a7..8d74c0ddf3 100644
@@ -37,6 +37,17 @@
 #define BIOS_CFG_IOPORT_CFG	0x510
 #define BIOS_CFG_IOPORT_DATA	0x511
 
+#define FW_CFG_DMA_CTL_ERROR   0x01
+#define FW_CFG_DMA_CTL_READ    0x02
+#define FW_CFG_DMA_CTL_SKIP    0x04
+#define FW_CFG_DMA_CTL_SELECT  0x08
+#define FW_CFG_DMA_CTL_WRITE   0x10
+
+#define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
+
+#define BIOS_CFG_DMA_ADDR_HIGH  0x514
+#define BIOS_CFG_DMA_ADDR_LOW   0x518
+
 /* Break the translation block flow so -d cpu shows us values */
 #define DEBUG_HERE \
 	jmp		1f;				\
@@ -62,6 +73,61 @@
 	bswap		%eax
 .endm
 
+
+/*
+ * Read data from the fw_cfg device using DMA.
+ * Clobbers:	%edx, %eax, ADDR, SIZE, memory[%esp-16] to memory[%esp]
+ */
+.macro read_fw_dma VAR, SIZE, ADDR
+        /* Address */
+	bswapl		\ADDR
+	pushl		\ADDR
+
+	/* We only support 32 bit target addresses */

pc-bios/optionrom/optionrom.h line 96

Quoted 10 lines
pc-bios/optionrom/optionrom.h +10 -0
+	xorl		%eax, %eax
+	pushl		%eax
+	mov		$BIOS_CFG_DMA_ADDR_HIGH, %dx
+	outl		%eax, (%dx)
+
+	/* Size */
+	bswapl		\SIZE
+	pushl		\SIZE
+
+        /* Control */

Indent off.

Quoted 4 lines
pc-bios/optionrom/optionrom.h +4 -0
+	movl		$(\VAR << 16) | (FW_CFG_DMA_CTL_READ | FW_CFG_DMA_CTL_SELECT), %eax
+	bswapl		%eax
+	pushl		%eax
+

pc-bios/optionrom/optionrom.h line 110

Quoted 10 lines
pc-bios/optionrom/optionrom.h +10 -0
+	movl		%esp, %eax /* Address of the struct we generated */
+	bswapl		%eax
+	mov		$BIOS_CFG_DMA_ADDR_LOW, %dx
+	outl		%eax, (%dx) /* Initiate DMA */
+
+1:  mov		(%esp), %eax /* Wait for completion */
+	bswapl		%eax
+	testl		$~FW_CFG_DMA_CTL_ERROR, %eax
+	jnz		1b
+       addl            $16, %esp

Indent off.

#5 lore raw

PATCH v2 3/3 target/i386: use DMA-enabled multiboot ROM for new-enough QEMU machine types

In reply to Paolo Bonzini

As long as fw_cfg supports DMA, the new ROM can be used also on older machine types because it has the same size as the existing one.

---
 hw/i386/multiboot.c | 10 ++++++++--
 hw/i386/multiboot.h |  4 +++-
 hw/i386/pc.c        |  3 ++-
 hw/i386/x86.c       |  2 +-
 4 files changed, 14 insertions(+), 5 deletions(-)
hw/i386/multiboot.c +8 -2
index 9e7d69d470..0a10089f14 100644
@@ -143,7 +143,8 @@ static void mb_add_mod(MultibootState *s,
     s->mb_mods_count++;
 }
 
-int load_multiboot(FWCfgState *fw_cfg,
+int load_multiboot(X86MachineState *x86ms,
+                   FWCfgState *fw_cfg,
                    FILE *f,
                    const char *kernel_filename,
                    const char *initrd_filename,
@@ -151,6 +152,7 @@ int load_multiboot(FWCfgState *fw_cfg,
                    int kernel_file_size,
                    uint8_t *header)
 {
+    bool multiboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
     int i, is_multiboot = 0;
     uint32_t flags = 0;
     uint32_t mh_entry_addr;
@@ -401,7 +403,11 @@ int load_multiboot(FWCfgState *fw_cfg,
     fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
                      sizeof(bootinfo));
 
-    option_rom[nb_option_roms].name = "multiboot.bin";
+    if (multiboot_dma_enabled) {
+        option_rom[nb_option_roms].name = "multiboot_dma.bin";
+    } else {
+        option_rom[nb_option_roms].name = "multiboot.bin";
+    }
     option_rom[nb_option_roms].bootindex = 0;
     nb_option_roms++;
 
hw/i386/multiboot.h +3 -1
index 60de309cd1..2b9182a8ea 100644
@@ -2,8 +2,10 @@
 #define QEMU_MULTIBOOT_H
 
 #include "hw/nvram/fw_cfg.h"
+#include "hw/i386/x86.h"
 
-int load_multiboot(FWCfgState *fw_cfg,
+int load_multiboot(X86MachineState *x86ms,
+                   FWCfgState *fw_cfg,
                    FILE *f,
                    const char *kernel_filename,
                    const char *initrd_filename,
hw/i386/pc.c +2 -1
index d203db7845..fcfb7f7139 100644
@@ -780,7 +780,8 @@ void xen_load_linux(PCMachineState *pcms)
         assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
                !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
                !strcmp(option_rom[i].name, "pvh.bin") ||
-               !strcmp(option_rom[i].name, "multiboot.bin"));
+               !strcmp(option_rom[i].name, "multiboot.bin") ||
+               !strcmp(option_rom[i].name, "multiboot_dma.bin"));
         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
     }
     x86ms->fw_cfg = fw_cfg;
hw/i386/x86.c +1 -1
index a34498fe16..b84840a1bb 100644
@@ -814,7 +814,7 @@ void x86_load_linux(X86MachineState *x86ms,
          * PVH), so we try multiboot first since we check the multiboot magic
          * header before to load it.
          */
-        if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
+        if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
         }
Signature · 1 line
2.31.1
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH 3/3] target/i386: use DMA-enabled multiboot ROM for new-enough QEMU machine types

Quoted 10 lines · Paolo Bonzini

As long as fw_cfg supports DMA, the new ROM can be used also on older machine types because it has the same size as the existing one.

---
 hw/i386/multiboot.c | 10 ++++++++--
 hw/i386/multiboot.h |  4 +++-
 hw/i386/pc.c        |  3 ++-
 hw/i386/x86.c       |  2 +-
 4 files changed, 14 insertions(+), 5 deletions(-)
Reviewed-by on the v2 series
#7 lore raw
In reply to Paolo Bonzini
Quoted 30 lines · Paolo Bonzini

An updated version of the patch at
https://patchew.org/QEMU/[email protected]/,
which includes the necessary glue for compatibility with older machine types. When fw_cfg DMA is disabled, the existing ROM is used instead.

Marcus Hähnel (1):
optionrom: add a DMA-enabled multiboot ROM

Paolo Bonzini (2):
target/i386: move linuxboot_dma_enabled to X86MachineState target/i386: use DMA-enabled multiboot ROM for new-enough QEMU machine
types

hw/i386/microvm.c                 |   5 ++-
hw/i386/multiboot.c               |  10 ++++-
hw/i386/multiboot.h               |   4 +-
hw/i386/pc.c                      |   8 ++--
hw/i386/pc_piix.c                 |   3 +-
hw/i386/pc_q35.c                  |   3 +-
hw/i386/x86.c                     |   7 ++--
include/hw/i386/pc.h              |   3 --
include/hw/i386/x86.h             |   5 ++-
pc-bios/meson.build               |   1 +
pc-bios/multiboot_dma.bin         | Bin 0 -> 1024 bytes
pc-bios/optionrom/Makefile        |   4 +-
pc-bios/optionrom/multiboot.S     |   4 +-
pc-bios/optionrom/multiboot_dma.S |   2 +
pc-bios/optionrom/optionrom.h     |  66 ++++++++++++++++++++++++++++++
15 files changed, 102 insertions(+), 23 deletions(-)
Quoted 7 lines

create mode 100644 pc-bios/multiboot_dma.bin create mode 100644 pc-bios/optionrom/multiboot_dma.S

Signature · 1 line
2.31.1