Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / tests / qtest / device-plug-test.c
blob559d47727af367a13b1b861736f4317a370ef785
1 /*
2 * QEMU device plug/unplug handling
4 * Copyright (C) 2019 Red Hat Inc.
6 * Authors:
7 * David Hildenbrand <david@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "libqos/libqtest.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
18 static void device_del(QTestState *qtest, const char *id)
20 QDict *resp;
22 resp = qtest_qmp(qtest,
23 "{'execute': 'device_del', 'arguments': { 'id': %s } }", id);
25 g_assert(qdict_haskey(resp, "return"));
26 qobject_unref(resp);
29 static void system_reset(QTestState *qtest)
31 QDict *resp;
33 resp = qtest_qmp(qtest, "{'execute': 'system_reset'}");
34 g_assert(qdict_haskey(resp, "return"));
35 qobject_unref(resp);
38 static void wait_device_deleted_event(QTestState *qtest, const char *id)
40 QDict *resp, *data;
41 QString *qstr;
44 * Other devices might get removed along with the removed device. Skip
45 * these. The device of interest will be the last one.
47 for (;;) {
48 resp = qtest_qmp_eventwait_ref(qtest, "DEVICE_DELETED");
49 data = qdict_get_qdict(resp, "data");
50 if (!data || !qdict_get(data, "device")) {
51 qobject_unref(resp);
52 continue;
54 qstr = qobject_to(QString, qdict_get(data, "device"));
55 g_assert(qstr);
56 if (!strcmp(qstring_get_str(qstr), id)) {
57 qobject_unref(resp);
58 break;
60 qobject_unref(resp);
64 static void test_pci_unplug_request(void)
66 QTestState *qtest = qtest_initf("-device virtio-mouse-pci,id=dev0");
69 * Request device removal. As the guest is not running, the request won't
70 * be processed. However during system reset, the removal will be
71 * handled, removing the device.
73 device_del(qtest, "dev0");
74 system_reset(qtest);
75 wait_device_deleted_event(qtest, "dev0");
77 qtest_quit(qtest);
80 static void test_ccw_unplug(void)
82 QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0");
84 device_del(qtest, "dev0");
85 wait_device_deleted_event(qtest, "dev0");
87 qtest_quit(qtest);
90 static void test_spapr_cpu_unplug_request(void)
92 QTestState *qtest;
94 qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 "
95 "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0");
97 /* similar to test_pci_unplug_request */
98 device_del(qtest, "dev0");
99 system_reset(qtest);
100 wait_device_deleted_event(qtest, "dev0");
102 qtest_quit(qtest);
105 static void test_spapr_memory_unplug_request(void)
107 QTestState *qtest;
109 qtest = qtest_initf("-m 256M,slots=1,maxmem=768M "
110 "-object memory-backend-ram,id=mem0,size=512M "
111 "-device pc-dimm,id=dev0,memdev=mem0");
113 /* similar to test_pci_unplug_request */
114 device_del(qtest, "dev0");
115 system_reset(qtest);
116 wait_device_deleted_event(qtest, "dev0");
118 qtest_quit(qtest);
121 static void test_spapr_phb_unplug_request(void)
123 QTestState *qtest;
125 qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0");
127 /* similar to test_pci_unplug_request */
128 device_del(qtest, "dev0");
129 system_reset(qtest);
130 wait_device_deleted_event(qtest, "dev0");
132 qtest_quit(qtest);
135 int main(int argc, char **argv)
137 const char *arch = qtest_get_arch();
139 g_test_init(&argc, &argv, NULL);
142 * We need a system that will process unplug requests during system resets
143 * and does not do PCI surprise removal. This holds for x86 ACPI,
144 * s390x and spapr.
146 qtest_add_func("/device-plug/pci-unplug-request",
147 test_pci_unplug_request);
149 if (!strcmp(arch, "s390x")) {
150 qtest_add_func("/device-plug/ccw-unplug",
151 test_ccw_unplug);
154 if (!strcmp(arch, "ppc64")) {
155 qtest_add_func("/device-plug/spapr-cpu-unplug-request",
156 test_spapr_cpu_unplug_request);
157 qtest_add_func("/device-plug/spapr-memory-unplug-request",
158 test_spapr_memory_unplug_request);
159 qtest_add_func("/device-plug/spapr-phb-unplug-request",
160 test_spapr_phb_unplug_request);
163 return g_test_run();