QEMU creates -device objects in order as specified by the user's cmdline. However that ordering may not be the ideal order. For example, some platform devices (vIOMMUs) may want to be created earlier than most of the rest devices (e.g., vfio-pci, virtio).
This patch orders the QemuOptsList of '-device's so they'll be sorted first before kicking off the device realizations. This will allow the device realization code to be able to use APIs like pci_device_iommu_address_space() correctly, because those functions rely on the platfrom devices being realized. Now we rely on vmsd->priority which is defined as MigrationPriority to provide the ordering, as either VM init and migration completes will need such an ordering. In the future we can move that priority information out of vmsd. Signed-off-by: Peter Xu <pet...@redhat.com> --- softmmu/vl.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/softmmu/vl.c b/softmmu/vl.c index 5ca11e7469..3a30dfe27d 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -126,6 +126,7 @@ #include "qapi/qmp/qerror.h" #include "sysemu/iothread.h" #include "qemu/guest-random.h" +#include "migration/vmstate.h" #include "config-host.h" @@ -2627,6 +2628,35 @@ static void qemu_init_board(void) } } +/* Return the priority of the device option; zero is the default priority */ +static int qemu_opts_device_priority(QemuOpts *opts) +{ + const char *driver; + DeviceClass *dc; + + driver = qemu_opt_get(opts, "driver"); + if (!driver) { + return 0; + } + + dc = qdev_get_device_class(&driver, NULL); + if (!dc) { + return 0; + } + + if (!dc->vmsd) { + return 0; + } + + /* + * Currently we rely on vmsd priority because that's solving the same + * problem for device realization ordering but just for migration. In the + * future, we can move it out of vmsd, but that's not urgently needed. + * Return the negative of it so it'll be sorted with descendant order. + */ + return -dc->vmsd->priority; +} + static void qemu_create_cli_devices(void) { soundhw_init(); @@ -2642,6 +2672,11 @@ static void qemu_create_cli_devices(void) /* init generic devices */ rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE); + /* + * Sort all the -device parameters; e.g., platform devices like vIOMMU + * should be initialized earlier. + */ + qemu_sort_opts("device", qemu_opts_device_priority); qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, &error_fatal); rom_reset_order_override(); -- 2.31.1