xref: /qemu/tests/qtest/libqos/virtio-serial.c (revision b3e7dc877187073628ec090728fd1aa58ccd60c8)
1 /*
2  * libqos driver framework
3  *
4  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License version 2 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17  */
18 
19 #include "qemu/osdep.h"
20 #include "libqtest.h"
21 #include "libqos/qgraph.h"
22 #include "libqos/virtio-serial.h"
23 
24 static void *qvirtio_serial_get_driver(QVirtioSerial *v_serial,
25                                        const char *interface)
26 {
27     if (!g_strcmp0(interface, "virtio-serial")) {
28         return v_serial;
29     }
30     if (!g_strcmp0(interface, "virtio")) {
31         return v_serial->vdev;
32     }
33 
34     fprintf(stderr, "%s not present in virtio-serial-device\n", interface);
35     g_assert_not_reached();
36 }
37 
38 static void *qvirtio_serial_device_get_driver(void *object,
39                                               const char *interface)
40 {
41     QVirtioSerialDevice *v_serial = object;
42     return qvirtio_serial_get_driver(&v_serial->serial, interface);
43 }
44 
45 static void *virtio_serial_device_create(void *virtio_dev,
46                                          QGuestAllocator *t_alloc,
47                                          void *addr)
48 {
49     QVirtioSerialDevice *virtio_device = g_new0(QVirtioSerialDevice, 1);
50     QVirtioSerial *interface = &virtio_device->serial;
51 
52     interface->vdev = virtio_dev;
53 
54     virtio_device->obj.get_driver = qvirtio_serial_device_get_driver;
55 
56     return &virtio_device->obj;
57 }
58 
59 /* virtio-serial-pci */
60 static void *qvirtio_serial_pci_get_driver(void *object, const char *interface)
61 {
62     QVirtioSerialPCI *v_serial = object;
63     if (!g_strcmp0(interface, "pci-device")) {
64         return v_serial->pci_vdev.pdev;
65     }
66     return qvirtio_serial_get_driver(&v_serial->serial, interface);
67 }
68 
69 static void *virtio_serial_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
70                                       void *addr)
71 {
72     QVirtioSerialPCI *virtio_spci = g_new0(QVirtioSerialPCI, 1);
73     QVirtioSerial *interface = &virtio_spci->serial;
74     QOSGraphObject *obj = &virtio_spci->pci_vdev.obj;
75 
76     virtio_pci_init(&virtio_spci->pci_vdev, pci_bus, addr);
77     interface->vdev = &virtio_spci->pci_vdev.vdev;
78 
79     obj->get_driver = qvirtio_serial_pci_get_driver;
80 
81     return obj;
82 }
83 
84 static void virtio_serial_register_nodes(void)
85 {
86    QPCIAddress addr = {
87         .devfn = QPCI_DEVFN(4, 0),
88     };
89 
90     QOSGraphEdgeOptions edge_opts = { };
91 
92     /* virtio-serial-device */
93     edge_opts.extra_device_opts = "id=vser0";
94     qos_node_create_driver("virtio-serial-device",
95                             virtio_serial_device_create);
96     qos_node_consumes("virtio-serial-device", "virtio-bus", &edge_opts);
97     qos_node_produces("virtio-serial-device", "virtio");
98     qos_node_produces("virtio-serial-device", "virtio-serial");
99 
100     /* virtio-serial-pci */
101     edge_opts.extra_device_opts = "id=vser0,addr=04.0";
102     add_qpci_address(&edge_opts, &addr);
103     qos_node_create_driver("virtio-serial-pci", virtio_serial_pci_create);
104     qos_node_consumes("virtio-serial-pci", "pci-bus", &edge_opts);
105     qos_node_produces("virtio-serial-pci", "pci-device");
106     qos_node_produces("virtio-serial-pci", "virtio");
107     qos_node_produces("virtio-serial-pci", "virtio-serial");
108 }
109 
110 libqos_init(virtio_serial_register_nodes);
111