hw/timer/exynos4210_mct.c: Switch ltick to transaction-based ptimer API
[qemu/ar7.git] / tests / device-plug-test.c
blob318e422d518c012c2b303d0ec0c19268b7402e42
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 "libqtest.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
18 static void device_del_start(QTestState *qtest, const char *id)
20 qtest_qmp_send(qtest,
21 "{'execute': 'device_del', 'arguments': { 'id': %s } }", id);
24 static void device_del_finish(QTestState *qtest)
26 QDict *resp = qtest_qmp_receive(qtest);
28 g_assert(qdict_haskey(resp, "return"));
29 qobject_unref(resp);
32 static void device_del_request(QTestState *qtest, const char *id)
34 device_del_start(qtest, id);
35 device_del_finish(qtest);
38 static void system_reset(QTestState *qtest)
40 QDict *resp;
42 resp = qtest_qmp(qtest, "{'execute': 'system_reset'}");
43 g_assert(qdict_haskey(resp, "return"));
44 qobject_unref(resp);
47 static void wait_device_deleted_event(QTestState *qtest, const char *id)
49 QDict *resp, *data;
50 QString *qstr;
53 * Other devices might get removed along with the removed device. Skip
54 * these. The device of interest will be the last one.
56 for (;;) {
57 resp = qtest_qmp_eventwait_ref(qtest, "DEVICE_DELETED");
58 data = qdict_get_qdict(resp, "data");
59 if (!data || !qdict_get(data, "device")) {
60 qobject_unref(resp);
61 continue;
63 qstr = qobject_to(QString, qdict_get(data, "device"));
64 g_assert(qstr);
65 if (!strcmp(qstring_get_str(qstr), id)) {
66 qobject_unref(resp);
67 break;
69 qobject_unref(resp);
73 static void test_pci_unplug_request(void)
75 QTestState *qtest = qtest_initf("-device virtio-mouse-pci,id=dev0");
78 * Request device removal. As the guest is not running, the request won't
79 * be processed. However during system reset, the removal will be
80 * handled, removing the device.
82 device_del_request(qtest, "dev0");
83 system_reset(qtest);
84 wait_device_deleted_event(qtest, "dev0");
86 qtest_quit(qtest);
89 static void test_ccw_unplug(void)
91 QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0");
94 * The DEVICE_DELETED events will be sent before the command
95 * completes.
97 device_del_start(qtest, "dev0");
98 wait_device_deleted_event(qtest, "dev0");
99 device_del_finish(qtest);
101 qtest_quit(qtest);
104 static void test_spapr_cpu_unplug_request(void)
106 QTestState *qtest;
108 qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 "
109 "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0");
111 /* similar to test_pci_unplug_request */
112 device_del_request(qtest, "dev0");
113 system_reset(qtest);
114 wait_device_deleted_event(qtest, "dev0");
116 qtest_quit(qtest);
119 static void test_spapr_memory_unplug_request(void)
121 QTestState *qtest;
123 qtest = qtest_initf("-m 256M,slots=1,maxmem=768M "
124 "-object memory-backend-ram,id=mem0,size=512M "
125 "-device pc-dimm,id=dev0,memdev=mem0");
127 /* similar to test_pci_unplug_request */
128 device_del_request(qtest, "dev0");
129 system_reset(qtest);
130 wait_device_deleted_event(qtest, "dev0");
132 qtest_quit(qtest);
135 static void test_spapr_phb_unplug_request(void)
137 QTestState *qtest;
139 qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0");
141 /* similar to test_pci_unplug_request */
142 device_del_request(qtest, "dev0");
143 system_reset(qtest);
144 wait_device_deleted_event(qtest, "dev0");
146 qtest_quit(qtest);
149 int main(int argc, char **argv)
151 const char *arch = qtest_get_arch();
153 g_test_init(&argc, &argv, NULL);
156 * We need a system that will process unplug requests during system resets
157 * and does not do PCI surprise removal. This holds for x86 ACPI,
158 * s390x and spapr.
160 qtest_add_func("/device-plug/pci-unplug-request",
161 test_pci_unplug_request);
163 if (!strcmp(arch, "s390x")) {
164 qtest_add_func("/device-plug/ccw-unplug",
165 test_ccw_unplug);
168 if (!strcmp(arch, "ppc64")) {
169 qtest_add_func("/device-plug/spapr-cpu-unplug-request",
170 test_spapr_cpu_unplug_request);
171 qtest_add_func("/device-plug/spapr-memory-unplug-request",
172 test_spapr_memory_unplug_request);
173 qtest_add_func("/device-plug/spapr-phb-unplug-request",
174 test_spapr_phb_unplug_request);
177 return g_test_run();