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 18c45a70d8SPaolo Bonzini static void device_del(QTestState *qtest, const char *id) 191e8a1faeSThomas Huth { 20c45a70d8SPaolo Bonzini QDict *resp; 211e8a1faeSThomas Huth 22c45a70d8SPaolo Bonzini resp = qtest_qmp(qtest, 23c45a70d8SPaolo 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 */ 73c45a70d8SPaolo 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 80*64b4529aSDaniel P. Berrangé static void test_pci_unplug_json_request(void) 81*64b4529aSDaniel P. Berrangé { 82*64b4529aSDaniel P. Berrangé QTestState *qtest = qtest_initf( 83*64b4529aSDaniel P. Berrangé "-device '{\"driver\": \"virtio-mouse-pci\", \"id\": \"dev0\"}'"); 84*64b4529aSDaniel P. Berrangé 85*64b4529aSDaniel P. Berrangé /* 86*64b4529aSDaniel P. Berrangé * Request device removal. As the guest is not running, the request won't 87*64b4529aSDaniel P. Berrangé * be processed. However during system reset, the removal will be 88*64b4529aSDaniel P. Berrangé * handled, removing the device. 89*64b4529aSDaniel P. Berrangé */ 90*64b4529aSDaniel P. Berrangé device_del(qtest, "dev0"); 91*64b4529aSDaniel P. Berrangé system_reset(qtest); 92*64b4529aSDaniel P. Berrangé wait_device_deleted_event(qtest, "dev0"); 93*64b4529aSDaniel P. Berrangé 94*64b4529aSDaniel P. Berrangé qtest_quit(qtest); 95*64b4529aSDaniel P. Berrangé } 96*64b4529aSDaniel P. Berrangé 971e8a1faeSThomas Huth static void test_ccw_unplug(void) 981e8a1faeSThomas Huth { 991e8a1faeSThomas Huth QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0"); 1001e8a1faeSThomas Huth 101c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1021e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1031e8a1faeSThomas Huth 1041e8a1faeSThomas Huth qtest_quit(qtest); 1051e8a1faeSThomas Huth } 1061e8a1faeSThomas Huth 1071e8a1faeSThomas Huth static void test_spapr_cpu_unplug_request(void) 1081e8a1faeSThomas Huth { 1091e8a1faeSThomas Huth QTestState *qtest; 1101e8a1faeSThomas Huth 1111e8a1faeSThomas Huth qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 " 1121e8a1faeSThomas Huth "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0"); 1131e8a1faeSThomas Huth 1141e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 115c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1161e8a1faeSThomas Huth system_reset(qtest); 1171e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1181e8a1faeSThomas Huth 1191e8a1faeSThomas Huth qtest_quit(qtest); 1201e8a1faeSThomas Huth } 1211e8a1faeSThomas Huth 1221e8a1faeSThomas Huth static void test_spapr_memory_unplug_request(void) 1231e8a1faeSThomas Huth { 1241e8a1faeSThomas Huth QTestState *qtest; 1251e8a1faeSThomas Huth 1261e8a1faeSThomas Huth qtest = qtest_initf("-m 256M,slots=1,maxmem=768M " 1271e8a1faeSThomas Huth "-object memory-backend-ram,id=mem0,size=512M " 1281e8a1faeSThomas Huth "-device pc-dimm,id=dev0,memdev=mem0"); 1291e8a1faeSThomas Huth 1301e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 131c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1321e8a1faeSThomas Huth system_reset(qtest); 1331e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1341e8a1faeSThomas Huth 1351e8a1faeSThomas Huth qtest_quit(qtest); 1361e8a1faeSThomas Huth } 1371e8a1faeSThomas Huth 1381e8a1faeSThomas Huth static void test_spapr_phb_unplug_request(void) 1391e8a1faeSThomas Huth { 1401e8a1faeSThomas Huth QTestState *qtest; 1411e8a1faeSThomas Huth 1421e8a1faeSThomas Huth qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0"); 1431e8a1faeSThomas Huth 1441e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 145c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1461e8a1faeSThomas Huth system_reset(qtest); 1471e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1481e8a1faeSThomas Huth 1491e8a1faeSThomas Huth qtest_quit(qtest); 1501e8a1faeSThomas Huth } 1511e8a1faeSThomas Huth 1521e8a1faeSThomas Huth int main(int argc, char **argv) 1531e8a1faeSThomas Huth { 1541e8a1faeSThomas Huth const char *arch = qtest_get_arch(); 1551e8a1faeSThomas Huth 1561e8a1faeSThomas Huth g_test_init(&argc, &argv, NULL); 1571e8a1faeSThomas Huth 1581e8a1faeSThomas Huth /* 1591e8a1faeSThomas Huth * We need a system that will process unplug requests during system resets 1601e8a1faeSThomas Huth * and does not do PCI surprise removal. This holds for x86 ACPI, 1611e8a1faeSThomas Huth * s390x and spapr. 1621e8a1faeSThomas Huth */ 1631e8a1faeSThomas Huth qtest_add_func("/device-plug/pci-unplug-request", 1641e8a1faeSThomas Huth test_pci_unplug_request); 165*64b4529aSDaniel P. Berrangé qtest_add_func("/device-plug/pci-unplug-json-request", 166*64b4529aSDaniel P. Berrangé test_pci_unplug_json_request); 1671e8a1faeSThomas Huth 1681e8a1faeSThomas Huth if (!strcmp(arch, "s390x")) { 1691e8a1faeSThomas Huth qtest_add_func("/device-plug/ccw-unplug", 1701e8a1faeSThomas Huth test_ccw_unplug); 1711e8a1faeSThomas Huth } 1721e8a1faeSThomas Huth 1731e8a1faeSThomas Huth if (!strcmp(arch, "ppc64")) { 1741e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-cpu-unplug-request", 1751e8a1faeSThomas Huth test_spapr_cpu_unplug_request); 1761e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-memory-unplug-request", 1771e8a1faeSThomas Huth test_spapr_memory_unplug_request); 1781e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-phb-unplug-request", 1791e8a1faeSThomas Huth test_spapr_phb_unplug_request); 1801e8a1faeSThomas Huth } 1811e8a1faeSThomas Huth 1821e8a1faeSThomas Huth return g_test_run(); 1831e8a1faeSThomas Huth } 184