2 * QEMU device plug/unplug handling
4 * Copyright (C) 2019 Red Hat Inc.
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"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
18 static void system_reset(QTestState *qtest)
22 resp = qtest_qmp(qtest, "{'execute': 'system_reset'}");
23 g_assert(qdict_haskey(resp, "return"));
27 static void wait_device_deleted_event(QTestState *qtest, const char *id)
33 * Other devices might get removed along with the removed device. Skip
34 * these. The device of interest will be the last one.
37 resp = qtest_qmp_eventwait_ref(qtest, "DEVICE_DELETED");
38 data = qdict_get_qdict(resp, "data");
39 if (!data || !qdict_get(data, "device")) {
43 qstr = qobject_to(QString, qdict_get(data, "device"));
45 if (!strcmp(qstring_get_str(qstr), id)) {
53 static void process_device_remove(QTestState *qtest, const char *id)
56 * Request device removal. As the guest is not running, the request won't
57 * be processed. However during system reset, the removal will be
58 * handled, removing the device.
60 qtest_qmp_device_del_send(qtest, id);
62 wait_device_deleted_event(qtest, id);
65 static void test_pci_unplug_request(void)
68 const char *arch = qtest_get_arch();
69 const char *machine_addition = "";
71 if (!qtest_has_device("virtio-mouse-pci")) {
72 g_test_skip("Device virtio-mouse-pci not available");
76 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
77 machine_addition = "-machine pc";
80 qtest = qtest_initf("%s -device virtio-mouse-pci,id=dev0",
83 process_device_remove(qtest, "dev0");
88 static void test_q35_pci_unplug_request(void)
92 if (!qtest_has_device("virtio-mouse-pci")) {
93 g_test_skip("Device virtio-mouse-pci not available");
97 qtest = qtest_initf("-machine q35 "
98 "-device pcie-root-port,id=p1 "
99 "-device pcie-pci-bridge,bus=p1,id=b1 "
100 "-device virtio-mouse-pci,bus=b1,id=dev0");
102 process_device_remove(qtest, "dev0");
107 static void test_pci_unplug_json_request(void)
110 const char *arch = qtest_get_arch();
111 const char *machine_addition = "";
113 if (!qtest_has_device("virtio-mouse-pci")) {
114 g_test_skip("Device virtio-mouse-pci not available");
118 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
119 machine_addition = "-machine pc";
123 "%s -device \"{'driver': 'virtio-mouse-pci', 'id': 'dev0'}\"",
126 process_device_remove(qtest, "dev0");
131 static void test_q35_pci_unplug_json_request(void)
134 const char *port = "-device \"{'driver': 'pcie-root-port', "
137 const char *bridge = "-device \"{'driver': 'pcie-pci-bridge', "
141 const char *device = "-device \"{'driver': 'virtio-mouse-pci', "
145 if (!qtest_has_device("virtio-mouse-pci")) {
146 g_test_skip("Device virtio-mouse-pci not available");
150 qtest = qtest_initf("-machine q35 %s %s %s", port, bridge, device);
152 process_device_remove(qtest, "dev0");
157 static void test_ccw_unplug(void)
161 if (!qtest_has_device("virtio-balloon-ccw")) {
162 g_test_skip("Device virtio-balloon-ccw not available");
166 qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0");
168 qtest_qmp_device_del_send(qtest, "dev0");
169 wait_device_deleted_event(qtest, "dev0");
174 static void test_spapr_cpu_unplug_request(void)
178 qtest = qtest_initf("-cpu power9_v2.2 -smp 1,maxcpus=2 "
179 "-device power9_v2.2-spapr-cpu-core,core-id=1,id=dev0");
181 /* similar to test_pci_unplug_request */
182 process_device_remove(qtest, "dev0");
187 static void test_spapr_memory_unplug_request(void)
191 qtest = qtest_initf("-m 256M,slots=1,maxmem=768M "
192 "-object memory-backend-ram,id=mem0,size=512M "
193 "-device pc-dimm,id=dev0,memdev=mem0");
195 /* similar to test_pci_unplug_request */
196 process_device_remove(qtest, "dev0");
201 static void test_spapr_phb_unplug_request(void)
205 qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0");
207 /* similar to test_pci_unplug_request */
208 process_device_remove(qtest, "dev0");
213 int main(int argc, char **argv)
215 const char *arch = qtest_get_arch();
217 g_test_init(&argc, &argv, NULL);
220 * We need a system that will process unplug requests during system resets
221 * and does not do PCI surprise removal. This holds for x86 ACPI,
224 qtest_add_func("/device-plug/pci-unplug-request",
225 test_pci_unplug_request);
226 qtest_add_func("/device-plug/pci-unplug-json-request",
227 test_pci_unplug_json_request);
229 if (!strcmp(arch, "s390x")) {
230 qtest_add_func("/device-plug/ccw-unplug",
234 if (!strcmp(arch, "ppc64")) {
235 qtest_add_func("/device-plug/spapr-cpu-unplug-request",
236 test_spapr_cpu_unplug_request);
237 qtest_add_func("/device-plug/spapr-memory-unplug-request",
238 test_spapr_memory_unplug_request);
239 qtest_add_func("/device-plug/spapr-phb-unplug-request",
240 test_spapr_phb_unplug_request);
243 if (!strcmp(arch, "x86_64") && qtest_has_machine("q35")) {
244 qtest_add_func("/device-plug/q35-pci-unplug-request",
245 test_q35_pci_unplug_request);
246 qtest_add_func("/device-plug/q35-pci-unplug-json-request",
247 test_q35_pci_unplug_json_request);