Skip to contentSkip to conversation

hw/i386: let -usb create qemu-xhci on new pc and q35 machine types

RFC PATCH New Junjie Cao 1 participant 5 messages Nov 4 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
author
#2 lore raw

RFC PATCH 2/4 hw/i386: let -usb create qemu-xhci on new pc and q35 machine types

In reply to Junjie Cao

-usb gives the PIIX3 UHCI on pc and the ICH9 EHCI with three UHCI companions on q35, so the one-switch way to get USB still picks a USB 1.1 or 2.0 controller. libvirt disables machine-created USB controllers with usb=off and adds controllers explicitly.

From pc-i440fx-12.0 and pc-q35-12.0 on, create qemu-xhci at 1d.0 instead. On q35 that is where the first UHCI sat, so every other device keeps its PCI address. On pc the PIIX3's own USB function is
out: the i440fx DSDT routes slot 1 INTA to the SCI link and qemu-xhci interrupts on INTA, so a guest that does not use MSI gets "PCI INT A:
no GSI" and never enumerates its devices; the UHCI there uses INTD. Slot 1d is free on pc and keeps the two machines alike.

The device stays optional in Kconfig. A build without it fails -usb with a message instead of a fatal "unknown type" error.

Guests without an xHCI driver (Windows 7 and earlier) need an older machine type or an explicit -device piix3-usb-uhci.

---
 hw/i386/Kconfig      |  2 ++
 hw/i386/pc.c         | 18 ++++++++++++++++++
 hw/i386/pc_piix.c    | 11 ++++++++++-
 hw/i386/pc_q35.c     | 12 ++++++++++--
 include/hw/i386/pc.h |  4 ++++
 5 files changed, 44 insertions(+), 3 deletions(-)
hw/i386/Kconfig +2 -0
index e27d8816e5..1d88726618 100644
@@ -78,6 +78,7 @@ config I440FX
     imply E1000_PCI
     imply VMPORT
     imply VMMOUSE
+    imply USB_XHCI_PCI
     select ACPI_PIIX4
     select PC_PCI
     select PC_ACPI
@@ -108,6 +109,7 @@ config Q35
     imply VMPORT
     imply VMMOUSE
     imply IOMMUFD
+    imply USB_XHCI_PCI
     select PC_PCI
     select PC_ACPI
     select PCI_EXPRESS_Q35
hw/i386/pc.c +18 -0
index 8a6b7978cc..6b8fc2914e 100644
@@ -55,6 +55,7 @@
 #include "hw/mem/nvdimm.h"
 #include "hw/cxl/cxl_host.h"
 #include "hw/usb/usb.h"
+#include "hw/usb/xhci.h"
 #include "hw/i386/intel_iommu.h"
 #include "hw/net/ne2000-isa.h"
 #include "hw/virtio/virtio-iommu.h"
@@ -1164,6 +1165,23 @@ void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
     }
 }
 
+/*
+ * 1d.0 is where q35's first UHCI sat. On pc the PIIX3's own USB function
+ * is out: the DSDT routes slot 1 INTA to the SCI link and qemu-xhci
+ * interrupts on INTA.
+ */
+void pc_usb_xhci_create(PCIBus *pci_bus)
+{
+    DeviceState *dev = qdev_try_new(TYPE_QEMU_XHCI);
+
+    if (!dev) {
+        error_report("-usb: this build has no %s device", TYPE_QEMU_XHCI);
+        exit(1);
+    }
+    qdev_prop_set_int32(dev, "addr", PCI_DEVFN(0x1d, 0));
+    qdev_realize_and_unref(dev, BUS(pci_bus), &error_fatal);
+}
+
 void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs)
 {
     qemu_irq *i8259;
hw/i386/pc_piix.c +10 -1
index c0ddf6011f..845092f5f1 100644
@@ -245,7 +245,8 @@ static void pc_init1(MachineState *machine, const char *pci_type)
     pci_dev = pci_new_multifunction(-1,
                                     pc_south_bridge_type[pcms->south_bridge]);
     object_property_set_bool(OBJECT(pci_dev), "has-usb",
-                             machine_usb(machine), &error_abort);
+                             machine_usb(machine) && !pcmc->usb_xhci,
+                             &error_abort);
     object_property_set_bool(OBJECT(pci_dev), "has-acpi",
                              x86_machine_is_acpi_enabled(x86ms),
                              &error_abort);
@@ -305,6 +306,10 @@ static void pc_init1(MachineState *machine, const char *pci_type)
 
     pc_nic_init(pcmc, isa_bus, pcms->pcibus);
 
+    if (machine_usb(machine) && pcmc->usb_xhci) {
+        pc_usb_xhci_create(pcms->pcibus);
+    }
+
     if (piix4_pm) {
         smi_irq = qemu_allocate_irq(pc_acpi_smi_interrupt, first_cpu, 0);
 
@@ -375,6 +380,7 @@ static void pc_i440fx_machine_options(MachineClass *m)
     pcmc->default_south_bridge = PC_SOUTH_BRIDGE_OPTION_PIIX3;
     pcmc->pci_root_uid = 0;
     pcmc->default_cpu_version = 1;
+    pcmc->usb_xhci = true;
 
     m->family = "pc_piix";
     m->desc = "Standard PC (i440FX + PIIX, 1996)";
@@ -406,7 +412,10 @@ DEFINE_I440FX_MACHINE_AS_LATEST(12, 0);
 
 static void pc_i440fx_machine_11_2_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
+
     pc_i440fx_machine_12_0_options(m);
+    pcmc->usb_xhci = false;
     compat_props_add(m->compat_props, hw_compat_11_2, hw_compat_11_2_len);
     compat_props_add(m->compat_props, pc_compat_11_2, pc_compat_11_2_len);
 }
hw/i386/pc_q35.c +10 -2
index e302ff283c..8a856bf199 100644
@@ -298,8 +298,12 @@ static void pc_q35_init(MachineState *machine)
     }
 
     if (machine_usb(machine)) {
-        /* Should we create 6 UHCI according to ich9 spec? */
-        ehci_create_ich9_with_companions(pcms->pcibus, 0x1d);
+        if (pcmc->usb_xhci) {
+            pc_usb_xhci_create(pcms->pcibus);
+        } else {
+            /* Should we create 6 UHCI according to ich9 spec? */
+            ehci_create_ich9_with_companions(pcms->pcibus, 0x1d);
+        }
     }
 
     if (pcms->smbus_enabled) {
@@ -353,6 +357,7 @@ static void pc_q35_machine_options(MachineClass *m)
     ObjectClass *oc = OBJECT_CLASS(m);
     pcmc->pci_root_uid = 0;
     pcmc->default_cpu_version = 1;
+    pcmc->usb_xhci = true;
 
     m->family = "pc_q35";
     m->desc = "Standard PC (Q35 + ICH9, 2009)";
@@ -387,7 +392,10 @@ DEFINE_Q35_MACHINE_AS_LATEST(12, 0);
 
 static void pc_q35_machine_11_2_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
+
     pc_q35_machine_12_0_options(m);
+    pcmc->usb_xhci = false;
     compat_props_add(m->compat_props, hw_compat_11_2, hw_compat_11_2_len);
     compat_props_add(m->compat_props, pc_compat_11_2, pc_compat_11_2_len);
 }
include/hw/i386/pc.h +4 -0
index caceddc74b..c378c804f8 100644
@@ -119,6 +119,9 @@ struct PCMachineClass {
     /* create kvmclock device even when KVM PV features are not exposed */
     bool kvmclock_create_always;
 
+    /* -usb creates qemu-xhci instead of the chipset's UHCI/EHCI */
+    bool usb_xhci;
+
     /*
      * whether the machine type implements broken 32-bit address space bound
      * check for memory.
@@ -195,6 +198,7 @@ void pc_basic_device_init(struct PCMachineState *pcms,
                           bool create_fdctrl,
                           uint32_t hpet_irqs);
 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus);
+void pc_usb_xhci_create(PCIBus *pci_bus);
 
 void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs);
 
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/4 hw/i386: add 12.0 machine types (NOT FOR MERGE)

In reply to Junjie Cao

Placeholder for the rest of the series, which changes a default on the next machine version. The real "hw: add compat machines for 12.0" covers every versioned board; this only adds pc-i440fx-12.0 and pc-q35-12.0. Drop it once that commit lands.

---
 hw/core/machine.c        |  3 +++
 hw/i386/pc.c             |  3 +++
 hw/i386/pc_piix.c        | 13 +++++++++++--
 hw/i386/pc_q35.c         | 13 +++++++++++--
 include/hw/core/boards.h |  3 +++
 include/hw/i386/pc.h     |  3 +++
 6 files changed, 34 insertions(+), 4 deletions(-)
hw/core/machine.c +3 -0
index ed9bf8f500..5305521ac5 100644
@@ -41,6 +41,9 @@
 #include "qemu/audio.h"
 #include "hw/arm/smmuv3.h"
 
+GlobalProperty hw_compat_11_2[] = {};
+const size_t hw_compat_11_2_len = G_N_ELEMENTS(hw_compat_11_2);
+
 GlobalProperty hw_compat_11_1[] = {
     { "sysbus-ehci-usb", "x-migrate-fetch-addr-64bit", "off" },
     { "pci-ehci-usb", "x-migrate-fetch-addr-64bit", "off" },
hw/i386/pc.c +3 -0
index 4737eb471c..8a6b7978cc 100644
@@ -75,6 +75,9 @@
 #include "hw/xen/xen-bus.h"
 #endif
 
+GlobalProperty pc_compat_11_2[] = {};
+const size_t pc_compat_11_2_len = G_N_ELEMENTS(pc_compat_11_2);
+
 GlobalProperty pc_compat_11_1[] = {};
 const size_t pc_compat_11_1_len = G_N_ELEMENTS(pc_compat_11_1);
 
hw/i386/pc_piix.c +11 -2
index 446590e4e5..c0ddf6011f 100644
@@ -397,12 +397,21 @@ static void pc_i440fx_machine_options(MachineClass *m)
                      pc_piix_compat_defaults, pc_piix_compat_defaults_len);
 }
 
-static void pc_i440fx_machine_11_2_options(MachineClass *m)
+static void pc_i440fx_machine_12_0_options(MachineClass *m)
 {
     pc_i440fx_machine_options(m);
 }
 
-DEFINE_I440FX_MACHINE_AS_LATEST(11, 2);
+DEFINE_I440FX_MACHINE_AS_LATEST(12, 0);
+
+static void pc_i440fx_machine_11_2_options(MachineClass *m)
+{
+    pc_i440fx_machine_12_0_options(m);
+    compat_props_add(m->compat_props, hw_compat_11_2, hw_compat_11_2_len);
+    compat_props_add(m->compat_props, pc_compat_11_2, pc_compat_11_2_len);
+}
+
+DEFINE_I440FX_MACHINE(11, 2);
 
 static void pc_i440fx_machine_11_1_options(MachineClass *m)
 {
hw/i386/pc_q35.c +11 -2
index c3ce9c5649..e302ff283c 100644
@@ -378,12 +378,21 @@ static void pc_q35_machine_options(MachineClass *m)
                      pc_q35_compat_defaults, pc_q35_compat_defaults_len);
 }
 
-static void pc_q35_machine_11_2_options(MachineClass *m)
+static void pc_q35_machine_12_0_options(MachineClass *m)
 {
     pc_q35_machine_options(m);
 }
 
-DEFINE_Q35_MACHINE_AS_LATEST(11, 2);
+DEFINE_Q35_MACHINE_AS_LATEST(12, 0);
+
+static void pc_q35_machine_11_2_options(MachineClass *m)
+{
+    pc_q35_machine_12_0_options(m);
+    compat_props_add(m->compat_props, hw_compat_11_2, hw_compat_11_2_len);
+    compat_props_add(m->compat_props, pc_compat_11_2, pc_compat_11_2_len);
+}
+
+DEFINE_Q35_MACHINE(11, 2);
 
 static void pc_q35_machine_11_1_options(MachineClass *m)
 {
include/hw/core/boards.h +3 -0
index d0e4ad9820..2f2327079c 100644
@@ -830,6 +830,9 @@ compat_props_add(GPtrArray *arr,
     }
 }
 
+extern GlobalProperty hw_compat_11_2[];
+extern const size_t hw_compat_11_2_len;
+
 extern GlobalProperty hw_compat_11_1[];
 extern const size_t hw_compat_11_1_len;
 
include/hw/i386/pc.h +3 -0
index 844ae83578..caceddc74b 100644
@@ -214,6 +214,9 @@ void pc_system_parse_ovmf_flash(uint8_t *flash_ptr, size_t flash_size);
 /* sgx.c */
 void pc_machine_init_sgx_epc(PCMachineState *pcms);
 
+extern GlobalProperty pc_compat_11_2[];
+extern const size_t pc_compat_11_2_len;
+
 extern GlobalProperty pc_compat_11_1[];
 extern const size_t pc_compat_11_1_len;
 
Signature · 1 line
2.43.0
author
#3 lore raw

RFC PATCH 3/4 tests/qtest: check which host controller -usb creates on pc and q35

In reply to Junjie Cao

Run query-pci on the pc and q35 aliases, the 12.0 and the 11.2 types. With -usb the former two pairs get qemu-xhci at 1d.0, the 11.2 ones PIIX3 UHCI and ICH9 EHCI plus companions; without -usb there is no USB controller.

---
 tests/qtest/meson.build           |   3 +
 tests/qtest/usb-pc-default-test.c | 135 ++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 tests/qtest/usb-pc-default-test.c
tests/qtest/meson.build +3 -0
index ff4bf9528a..615056876d 100644
@@ -91,6 +91,9 @@ qtests_i386 = \
   (config_all_devices.has_key('CONFIG_USB_UHCI') and                                        \
    config_all_devices.has_key('CONFIG_USB_EHCI') ? ['usb-hcd-ehci-test'] : []) +            \
   (config_all_devices.has_key('CONFIG_USB_XHCI_NEC') ? ['usb-hcd-xhci-test'] : []) +        \
+  (config_all_devices.has_key('CONFIG_USB_UHCI') and                                        \
+   config_all_devices.has_key('CONFIG_USB_EHCI_PCI') and                                    \
+   config_all_devices.has_key('CONFIG_USB_XHCI_PCI') ? ['usb-pc-default-test'] : []) +      \
   (config_all_devices.has_key('CONFIG_TPM_CRB') ? ['tpm-crb-test'] : []) +                  \
   (config_all_devices.has_key('CONFIG_TPM_CRB') ? ['tpm-crb-swtpm-test'] : []) +            \
   (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-test'] : []) +              \
tests/qtest/usb-pc-default-test.c new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
2.43.0

Junjie Cao

author
#0 lore raw

cover hw/i386: let -usb create qemu-xhci on new pc and q35 machine types

-usb still gives the PIIX3 UHCI on pc and the ICH9 EHCI plus three UHCI companions on q35. In May Peter asked whether -usb could give the PC machines an xHCI on new machine versions and Gerd agreed [1]. Daniel's secure-devices series keeps UHCI/OHCI/EHCI in the virtualization scope partly because -usb still creates them [2]; older machine types keep doing so. From pc-i440fx-12.0 and pc-q35-12.0 on, -usb creates qemu-xhci.

Patch 1 adds the 12.0 machine types so the rest has a version to hang off; drop it once the real "hw: add compat machines for 12.0" lands.

Open question: where the controller sits. It takes 1d.0 on both machines; patch 2 says why the PIIX3's own USB function is out. Slot 1d has no hardware meaning on i440fx. Alternatives: an interrupt-pin property on qemu-xhci so 01.2 can use INTD like the UHCI, or an automatic slot as spapr does for nec-usb-xhci, which shifts user -device addresses on the new types by one slot. I prefer the fixed slot.

Checked:

- SeaBIOS and OVMF (Ubuntu 24.04) drive the xHCI at 00:1d.0 and find the keyboard on both machines.
- Debian 12 under KVM: xhci_hcd at 1d.0, HID at 480 Mb/s, with MSI and with -global qemu-xhci.msi=off -global qemu-xhci.msix=off (INTx routed, IRQ 10 on pc and 16 on q35). pc-q35-11.2 unchanged: ehci-pci plus three uhci_hcd.
- pc-i440fx-11.2 and pc-q35-11.2 with -usb: a running Debian guest saved by master at d29412cca5 and loaded by this series keeps its USB devices and interrupts.
- check-qtest-x86_64 passes.

Windows 7 and earlier do not include an xHCI driver; usb.rst now says to use an older machine type or -device piix3-usb-uhci for them.

Jamin's usbredir-server series turns auto-hub off on new machine types [3]. With both, -usb on the new types gives four USB 2 root ports and no automatic hub. The two series are independent; Jamin's compat entries have to move to hw_compat_11_2 either way.

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/qemu-devel/[email protected]/

Junjie Cao (4):
hw/i386: add 12.0 machine types (NOT FOR MERGE)
hw/i386: let -usb create qemu-xhci on new pc and q35 machine types tests/qtest: check which host controller -usb creates on pc and q35 docs/system/devices/usb: describe -usb after the qemu-xhci switch

 docs/system/devices/usb.rst       |  23 +++--
 hw/core/machine.c                 |   3 +
 hw/i386/Kconfig                   |   2 +
 hw/i386/pc.c                      |  21 +++++
 hw/i386/pc_piix.c                 |  24 +++++-
 hw/i386/pc_q35.c                  |  25 +++++-
 include/hw/core/boards.h          |   3 +
 include/hw/i386/pc.h              |   7 ++
 qemu-options.hx                   |   9 +-
 tests/qtest/meson.build           |   3 +
 tests/qtest/usb-pc-default-test.c | 135 ++++++++++++++++++++++++++++++
 11 files changed, 236 insertions(+), 19 deletions(-)
 create mode 100644 tests/qtest/usb-pc-default-test.c
Signature · 1 line
2.43.0
author
#4 lore raw

RFC PATCH 4/4 docs/system/devices/usb: describe -usb after the qemu-xhci switch

Say which machine types still get UHCI or EHCI from -usb, and point guests without an xHCI driver at them. The two examples that used -usb for a UHCI next to an EHCI now create it with -device, so they keep showing separate UHCI and EHCI buses.

---
 docs/system/devices/usb.rst | 23 ++++++++++++++---------
 qemu-options.hx             |  9 ++++++---
 2 files changed, 20 insertions(+), 12 deletions(-)
docs/system/devices/usb.rst +14 -9
index dc694d23c2..b3cde7fc8e 100644
@@ -20,6 +20,11 @@ released around 2010 or later) we recommend using it:
 
     |qemu_system| -device qemu-xhci
 
+From the 12.0 machine types on, ``-usb`` creates a ``qemu-xhci`` on
+the ``pc`` and ``q35`` machines.  Guests without an XHCI driver, such
+as Windows 7, need an older machine type or an explicit
+``-device piix3-usb-uhci`` or ``-device usb-ehci``.
+
 XHCI supports USB 1.1, USB 2.0 and USB 3.0 devices, so this is the
 only controller you need.  With only a single USB controller (and
 therefore only a single USB bus) present in the system there is no
@@ -42,9 +47,9 @@ bus though, so there are two completely separate USB buses: One USB
 the EHCI controller.  Devices must be attached to the correct
 controller manually.
 
-The easiest way to add a UHCI controller to a ``pc`` machine is the
-``-usb`` switch.  QEMU will create the UHCI controller as function of
-the PIIX3 chipset.  The USB 1.1 bus will carry the name ``usb-bus.0``.
+On ``pc`` machine types up to 11.2 the ``-usb`` switch creates the
+UHCI controller as function of the PIIX3 chipset.  The USB 1.1 bus
+will carry the name ``usb-bus.0``.
 
 You can use the standard ``-device`` switch to add a EHCI controller to
 your virtual machine.  It is strongly recommended to specify an ID for
@@ -59,9 +64,9 @@ bus they should be attached to.  Here is a complete example:
 
     |qemu_system| -M pc ${otheroptions}                        \\
         -drive if=none,id=usbstick,format=raw,file=/path/to/image   \\
-        -usb                                                        \\
+        -device piix3-usb-uhci,id=uhci                              \\
         -device usb-ehci,id=ehci                                    \\
-        -device usb-tablet,bus=usb-bus.0                            \\
+        -device usb-tablet,bus=uhci.0                               \\
         -device usb-storage,bus=ehci.0,drive=usbstick
 
 This attaches a USB tablet to the UHCI adapter and a USB mass storage
@@ -88,8 +93,8 @@ you, which you can use like this:
 
 Then use ``bus=ehci.0`` to assign your USB devices to that bus.
 
-Using the ``-usb`` switch for ``q35`` machines will create a similar
-USB controller configuration.
+On ``q35`` machine types up to 11.2 the ``-usb`` switch creates this
+configuration.
 
 
 .. _Connecting USB devices:
@@ -359,9 +364,9 @@ and also assign it to the correct USB bus in QEMU like this:
 .. parsed-literal::
 
    |qemu_system| -M pc [...]                            \\
-        -usb                                                 \\
+        -device piix3-usb-uhci,id=uhci                       \\
         -device usb-ehci,id=ehci                             \\
-        -device usb-host,bus=usb-bus.0,hostbus=3,hostport=1  \\
+        -device usb-host,bus=uhci.0,hostbus=3,hostport=1     \\
         -device usb-host,bus=ehci.0,hostbus=1,hostport=1
 
 ``usb-host`` properties for reset behavior
qemu-options.hx +6 -3
index 5fe9d73eaa..2429461350 100644
@@ -2193,9 +2193,12 @@ DEF("usb", 0, QEMU_OPTION_usb,
 SRST
 ``-usb``
     Enable USB emulation on machine types with an on-board USB host
-    controller (if not enabled by default). Note that on-board USB host
-    controllers may not support USB 3.0. In this case
-    ``-device qemu-xhci`` can be used instead on machines with PCI.
+    controller (if not enabled by default). On the ``pc`` and ``q35``
+    machines this is a ``qemu-xhci`` from the 12.0 machine types on;
+    older types get the chipset's UHCI (``pc``) or EHCI with UHCI
+    companions (``q35``). Other on-board USB host controllers may not
+    support USB 3.0. In this case ``-device qemu-xhci`` can be used
+    instead on machines with PCI.
 ERST
 
 DEF("usbdevice", HAS_ARG, QEMU_OPTION_usbdevice,
Signature · 1 line
2.43.0