11cf4323eSThomas Huth /* 21cf4323eSThomas Huth * libqos driver framework 31cf4323eSThomas Huth * 41cf4323eSThomas Huth * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com> 51cf4323eSThomas Huth * 61cf4323eSThomas Huth * This library is free software; you can redistribute it and/or 71cf4323eSThomas Huth * modify it under the terms of the GNU Lesser General Public 8dc0ad02dSThomas Huth * License version 2.1 as published by the Free Software Foundation. 91cf4323eSThomas Huth * 101cf4323eSThomas Huth * This library is distributed in the hope that it will be useful, 111cf4323eSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 121cf4323eSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 131cf4323eSThomas Huth * Lesser General Public License for more details. 141cf4323eSThomas Huth * 151cf4323eSThomas Huth * You should have received a copy of the GNU Lesser General Public 161cf4323eSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/> 171cf4323eSThomas Huth */ 181cf4323eSThomas Huth 191cf4323eSThomas Huth #include "qemu/osdep.h" 20*907b5105SMarc-André Lureau #include "../libqtest.h" 211cf4323eSThomas Huth #include "qemu/module.h" 22a2ce7dbdSPaolo Bonzini #include "qgraph.h" 23a2ce7dbdSPaolo Bonzini #include "virtio-serial.h" 241cf4323eSThomas Huth 251cf4323eSThomas Huth static void *qvirtio_serial_get_driver(QVirtioSerial *v_serial, 261cf4323eSThomas Huth const char *interface) 271cf4323eSThomas Huth { 281cf4323eSThomas Huth if (!g_strcmp0(interface, "virtio-serial")) { 291cf4323eSThomas Huth return v_serial; 301cf4323eSThomas Huth } 311cf4323eSThomas Huth if (!g_strcmp0(interface, "virtio")) { 321cf4323eSThomas Huth return v_serial->vdev; 331cf4323eSThomas Huth } 341cf4323eSThomas Huth 351cf4323eSThomas Huth fprintf(stderr, "%s not present in virtio-serial-device\n", interface); 361cf4323eSThomas Huth g_assert_not_reached(); 371cf4323eSThomas Huth } 381cf4323eSThomas Huth 391cf4323eSThomas Huth static void *qvirtio_serial_device_get_driver(void *object, 401cf4323eSThomas Huth const char *interface) 411cf4323eSThomas Huth { 421cf4323eSThomas Huth QVirtioSerialDevice *v_serial = object; 431cf4323eSThomas Huth return qvirtio_serial_get_driver(&v_serial->serial, interface); 441cf4323eSThomas Huth } 451cf4323eSThomas Huth 461cf4323eSThomas Huth static void *virtio_serial_device_create(void *virtio_dev, 471cf4323eSThomas Huth QGuestAllocator *t_alloc, 481cf4323eSThomas Huth void *addr) 491cf4323eSThomas Huth { 501cf4323eSThomas Huth QVirtioSerialDevice *virtio_device = g_new0(QVirtioSerialDevice, 1); 511cf4323eSThomas Huth QVirtioSerial *interface = &virtio_device->serial; 521cf4323eSThomas Huth 531cf4323eSThomas Huth interface->vdev = virtio_dev; 541cf4323eSThomas Huth 551cf4323eSThomas Huth virtio_device->obj.get_driver = qvirtio_serial_device_get_driver; 561cf4323eSThomas Huth 571cf4323eSThomas Huth return &virtio_device->obj; 581cf4323eSThomas Huth } 591cf4323eSThomas Huth 601cf4323eSThomas Huth /* virtio-serial-pci */ 611cf4323eSThomas Huth static void *qvirtio_serial_pci_get_driver(void *object, const char *interface) 621cf4323eSThomas Huth { 631cf4323eSThomas Huth QVirtioSerialPCI *v_serial = object; 641cf4323eSThomas Huth if (!g_strcmp0(interface, "pci-device")) { 651cf4323eSThomas Huth return v_serial->pci_vdev.pdev; 661cf4323eSThomas Huth } 671cf4323eSThomas Huth return qvirtio_serial_get_driver(&v_serial->serial, interface); 681cf4323eSThomas Huth } 691cf4323eSThomas Huth 701cf4323eSThomas Huth static void *virtio_serial_pci_create(void *pci_bus, QGuestAllocator *t_alloc, 711cf4323eSThomas Huth void *addr) 721cf4323eSThomas Huth { 731cf4323eSThomas Huth QVirtioSerialPCI *virtio_spci = g_new0(QVirtioSerialPCI, 1); 741cf4323eSThomas Huth QVirtioSerial *interface = &virtio_spci->serial; 751cf4323eSThomas Huth QOSGraphObject *obj = &virtio_spci->pci_vdev.obj; 761cf4323eSThomas Huth 771cf4323eSThomas Huth virtio_pci_init(&virtio_spci->pci_vdev, pci_bus, addr); 781cf4323eSThomas Huth interface->vdev = &virtio_spci->pci_vdev.vdev; 791cf4323eSThomas Huth 801cf4323eSThomas Huth obj->get_driver = qvirtio_serial_pci_get_driver; 811cf4323eSThomas Huth 821cf4323eSThomas Huth return obj; 831cf4323eSThomas Huth } 841cf4323eSThomas Huth 851cf4323eSThomas Huth static void virtio_serial_register_nodes(void) 861cf4323eSThomas Huth { 871cf4323eSThomas Huth QPCIAddress addr = { 881cf4323eSThomas Huth .devfn = QPCI_DEVFN(4, 0), 891cf4323eSThomas Huth }; 901cf4323eSThomas Huth 911cf4323eSThomas Huth QOSGraphEdgeOptions edge_opts = { }; 921cf4323eSThomas Huth 931cf4323eSThomas Huth /* virtio-serial-device */ 941cf4323eSThomas Huth edge_opts.extra_device_opts = "id=vser0"; 951cf4323eSThomas Huth qos_node_create_driver("virtio-serial-device", 961cf4323eSThomas Huth virtio_serial_device_create); 971cf4323eSThomas Huth qos_node_consumes("virtio-serial-device", "virtio-bus", &edge_opts); 981cf4323eSThomas Huth qos_node_produces("virtio-serial-device", "virtio"); 991cf4323eSThomas Huth qos_node_produces("virtio-serial-device", "virtio-serial"); 1001cf4323eSThomas Huth 1011cf4323eSThomas Huth /* virtio-serial-pci */ 1021cf4323eSThomas Huth edge_opts.extra_device_opts = "id=vser0,addr=04.0"; 1031cf4323eSThomas Huth add_qpci_address(&edge_opts, &addr); 1041cf4323eSThomas Huth qos_node_create_driver("virtio-serial-pci", virtio_serial_pci_create); 1051cf4323eSThomas Huth qos_node_consumes("virtio-serial-pci", "pci-bus", &edge_opts); 1061cf4323eSThomas Huth qos_node_produces("virtio-serial-pci", "pci-device"); 1071cf4323eSThomas Huth qos_node_produces("virtio-serial-pci", "virtio"); 1081cf4323eSThomas Huth qos_node_produces("virtio-serial-pci", "virtio-serial"); 1091cf4323eSThomas Huth } 1101cf4323eSThomas Huth 1111cf4323eSThomas Huth libqos_init(virtio_serial_register_nodes); 112