11e8a1faeSThomas Huth /* 21e8a1faeSThomas Huth * QEMU device plug/unplug handling 31e8a1faeSThomas Huth * 41e8a1faeSThomas Huth * Copyright (C) 2019 Red Hat Inc. 51e8a1faeSThomas Huth * 61e8a1faeSThomas Huth * Authors: 71e8a1faeSThomas Huth * David Hildenbrand <david@redhat.com> 81e8a1faeSThomas Huth * 91e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later. 101e8a1faeSThomas Huth * See the COPYING file in the top-level directory. 111e8a1faeSThomas Huth */ 121e8a1faeSThomas Huth 131e8a1faeSThomas Huth #include "qemu/osdep.h" 14a2ce7dbdSPaolo Bonzini #include "libqos/libqtest.h" 151e8a1faeSThomas Huth #include "qapi/qmp/qdict.h" 161e8a1faeSThomas Huth #include "qapi/qmp/qstring.h" 171e8a1faeSThomas Huth 18*c45a70d8SPaolo Bonzini static void device_del(QTestState *qtest, const char *id) 191e8a1faeSThomas Huth { 20*c45a70d8SPaolo Bonzini QDict *resp; 211e8a1faeSThomas Huth 22*c45a70d8SPaolo Bonzini resp = qtest_qmp(qtest, 23*c45a70d8SPaolo Bonzini "{'execute': 'device_del', 'arguments': { 'id': %s } }", id); 241e8a1faeSThomas Huth 251e8a1faeSThomas Huth g_assert(qdict_haskey(resp, "return")); 261e8a1faeSThomas Huth qobject_unref(resp); 271e8a1faeSThomas Huth } 281e8a1faeSThomas Huth 291e8a1faeSThomas Huth static void system_reset(QTestState *qtest) 301e8a1faeSThomas Huth { 311e8a1faeSThomas Huth QDict *resp; 321e8a1faeSThomas Huth 331e8a1faeSThomas Huth resp = qtest_qmp(qtest, "{'execute': 'system_reset'}"); 341e8a1faeSThomas Huth g_assert(qdict_haskey(resp, "return")); 351e8a1faeSThomas Huth qobject_unref(resp); 361e8a1faeSThomas Huth } 371e8a1faeSThomas Huth 381e8a1faeSThomas Huth static void wait_device_deleted_event(QTestState *qtest, const char *id) 391e8a1faeSThomas Huth { 401e8a1faeSThomas Huth QDict *resp, *data; 411e8a1faeSThomas Huth QString *qstr; 421e8a1faeSThomas Huth 431e8a1faeSThomas Huth /* 441e8a1faeSThomas Huth * Other devices might get removed along with the removed device. Skip 451e8a1faeSThomas Huth * these. The device of interest will be the last one. 461e8a1faeSThomas Huth */ 471e8a1faeSThomas Huth for (;;) { 481e8a1faeSThomas Huth resp = qtest_qmp_eventwait_ref(qtest, "DEVICE_DELETED"); 491e8a1faeSThomas Huth data = qdict_get_qdict(resp, "data"); 501e8a1faeSThomas Huth if (!data || !qdict_get(data, "device")) { 511e8a1faeSThomas Huth qobject_unref(resp); 521e8a1faeSThomas Huth continue; 531e8a1faeSThomas Huth } 541e8a1faeSThomas Huth qstr = qobject_to(QString, qdict_get(data, "device")); 551e8a1faeSThomas Huth g_assert(qstr); 561e8a1faeSThomas Huth if (!strcmp(qstring_get_str(qstr), id)) { 571e8a1faeSThomas Huth qobject_unref(resp); 581e8a1faeSThomas Huth break; 591e8a1faeSThomas Huth } 601e8a1faeSThomas Huth qobject_unref(resp); 611e8a1faeSThomas Huth } 621e8a1faeSThomas Huth } 631e8a1faeSThomas Huth 641e8a1faeSThomas Huth static void test_pci_unplug_request(void) 651e8a1faeSThomas Huth { 661e8a1faeSThomas Huth QTestState *qtest = qtest_initf("-device virtio-mouse-pci,id=dev0"); 671e8a1faeSThomas Huth 681e8a1faeSThomas Huth /* 691e8a1faeSThomas Huth * Request device removal. As the guest is not running, the request won't 701e8a1faeSThomas Huth * be processed. However during system reset, the removal will be 711e8a1faeSThomas Huth * handled, removing the device. 721e8a1faeSThomas Huth */ 73*c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 741e8a1faeSThomas Huth system_reset(qtest); 751e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 761e8a1faeSThomas Huth 771e8a1faeSThomas Huth qtest_quit(qtest); 781e8a1faeSThomas Huth } 791e8a1faeSThomas Huth 801e8a1faeSThomas Huth static void test_ccw_unplug(void) 811e8a1faeSThomas Huth { 821e8a1faeSThomas Huth QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0"); 831e8a1faeSThomas Huth 84*c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 851e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 861e8a1faeSThomas Huth 871e8a1faeSThomas Huth qtest_quit(qtest); 881e8a1faeSThomas Huth } 891e8a1faeSThomas Huth 901e8a1faeSThomas Huth static void test_spapr_cpu_unplug_request(void) 911e8a1faeSThomas Huth { 921e8a1faeSThomas Huth QTestState *qtest; 931e8a1faeSThomas Huth 941e8a1faeSThomas Huth qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 " 951e8a1faeSThomas Huth "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0"); 961e8a1faeSThomas Huth 971e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 98*c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 991e8a1faeSThomas Huth system_reset(qtest); 1001e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1011e8a1faeSThomas Huth 1021e8a1faeSThomas Huth qtest_quit(qtest); 1031e8a1faeSThomas Huth } 1041e8a1faeSThomas Huth 1051e8a1faeSThomas Huth static void test_spapr_memory_unplug_request(void) 1061e8a1faeSThomas Huth { 1071e8a1faeSThomas Huth QTestState *qtest; 1081e8a1faeSThomas Huth 1091e8a1faeSThomas Huth qtest = qtest_initf("-m 256M,slots=1,maxmem=768M " 1101e8a1faeSThomas Huth "-object memory-backend-ram,id=mem0,size=512M " 1111e8a1faeSThomas Huth "-device pc-dimm,id=dev0,memdev=mem0"); 1121e8a1faeSThomas Huth 1131e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 114*c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1151e8a1faeSThomas Huth system_reset(qtest); 1161e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1171e8a1faeSThomas Huth 1181e8a1faeSThomas Huth qtest_quit(qtest); 1191e8a1faeSThomas Huth } 1201e8a1faeSThomas Huth 1211e8a1faeSThomas Huth static void test_spapr_phb_unplug_request(void) 1221e8a1faeSThomas Huth { 1231e8a1faeSThomas Huth QTestState *qtest; 1241e8a1faeSThomas Huth 1251e8a1faeSThomas Huth qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0"); 1261e8a1faeSThomas Huth 1271e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 128*c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1291e8a1faeSThomas Huth system_reset(qtest); 1301e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1311e8a1faeSThomas Huth 1321e8a1faeSThomas Huth qtest_quit(qtest); 1331e8a1faeSThomas Huth } 1341e8a1faeSThomas Huth 1351e8a1faeSThomas Huth int main(int argc, char **argv) 1361e8a1faeSThomas Huth { 1371e8a1faeSThomas Huth const char *arch = qtest_get_arch(); 1381e8a1faeSThomas Huth 1391e8a1faeSThomas Huth g_test_init(&argc, &argv, NULL); 1401e8a1faeSThomas Huth 1411e8a1faeSThomas Huth /* 1421e8a1faeSThomas Huth * We need a system that will process unplug requests during system resets 1431e8a1faeSThomas Huth * and does not do PCI surprise removal. This holds for x86 ACPI, 1441e8a1faeSThomas Huth * s390x and spapr. 1451e8a1faeSThomas Huth */ 1461e8a1faeSThomas Huth qtest_add_func("/device-plug/pci-unplug-request", 1471e8a1faeSThomas Huth test_pci_unplug_request); 1481e8a1faeSThomas Huth 1491e8a1faeSThomas Huth if (!strcmp(arch, "s390x")) { 1501e8a1faeSThomas Huth qtest_add_func("/device-plug/ccw-unplug", 1511e8a1faeSThomas Huth test_ccw_unplug); 1521e8a1faeSThomas Huth } 1531e8a1faeSThomas Huth 1541e8a1faeSThomas Huth if (!strcmp(arch, "ppc64")) { 1551e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-cpu-unplug-request", 1561e8a1faeSThomas Huth test_spapr_cpu_unplug_request); 1571e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-memory-unplug-request", 1581e8a1faeSThomas Huth test_spapr_memory_unplug_request); 1591e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-phb-unplug-request", 1601e8a1faeSThomas Huth test_spapr_phb_unplug_request); 1611e8a1faeSThomas Huth } 1621e8a1faeSThomas Huth 1631e8a1faeSThomas Huth return g_test_run(); 1641e8a1faeSThomas Huth } 165