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.1 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 "qemu/module.h"
22 #include "qgraph.h"
23 #include "virtio-serial.h"
24 
25 static void *qvirtio_serial_get_driver(QVirtioSerial *v_serial,
26                                        const char *interface)
27 {
28     if (!g_strcmp0(interface, "virtio-serial")) {
29         return v_serial;
30     }
31     if (!g_strcmp0(interface, "virtio")) {
32         return v_serial->vdev;
33     }
34 
35     fprintf(stderr, "%s not present in virtio-serial-device\n", interface);
36     g_assert_not_reached();
37 }
38 
39 static void *qvirtio_serial_device_get_driver(void *object,
40                                               const char *interface)
41 {
42     QVirtioSerialDevice *v_serial = object;
43     return qvirtio_serial_get_driver(&v_serial->serial, interface);
44 }
45 
46 static void *virtio_serial_device_create(void *virtio_dev,
47                                          QGuestAllocator *t_alloc,
48                                          void *addr)
49 {
50     QVirtioSerialDevice *virtio_device = g_new0(QVirtioSerialDevice, 1);
51     QVirtioSerial *interface = &virtio_device->serial;
52 
53     interface->vdev = virtio_dev;
54 
55     virtio_device->obj.get_driver = qvirtio_serial_device_get_driver;
56 
57     return &virtio_device->obj;
58 }
59 
60 /* virtio-serial-pci */
61 static void *qvirtio_serial_pci_get_driver(void *object, const char *interface)
62 {
63     QVirtioSerialPCI *v_serial = object;
64     if (!g_strcmp0(interface, "pci-device")) {
65         return v_serial->pci_vdev.pdev;
66     }
67     return qvirtio_serial_get_driver(&v_serial->serial, interface);
68 }
69 
70 static void *virtio_serial_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
71                                       void *addr)
72 {
73     QVirtioSerialPCI *virtio_spci = g_new0(QVirtioSerialPCI, 1);
74     QVirtioSerial *interface = &virtio_spci->serial;
75     QOSGraphObject *obj = &virtio_spci->pci_vdev.obj;
76 
77     virtio_pci_init(&virtio_spci->pci_vdev, pci_bus, addr);
78     interface->vdev = &virtio_spci->pci_vdev.vdev;
79 
80     obj->get_driver = qvirtio_serial_pci_get_driver;
81 
82     return obj;
83 }
84 
85 static void virtio_serial_register_nodes(void)
86 {
87    QPCIAddress addr = {
88         .devfn = QPCI_DEVFN(4, 0),
89     };
90 
91     QOSGraphEdgeOptions edge_opts = { };
92 
93     /* virtio-serial-device */
94     edge_opts.extra_device_opts = "id=vser0";
95     qos_node_create_driver("virtio-serial-device",
96                             virtio_serial_device_create);
97     qos_node_consumes("virtio-serial-device", "virtio-bus", &edge_opts);
98     qos_node_produces("virtio-serial-device", "virtio");
99     qos_node_produces("virtio-serial-device", "virtio-serial");
100 
101     /* virtio-serial-pci */
102     edge_opts.extra_device_opts = "id=vser0,addr=04.0";
103     add_qpci_address(&edge_opts, &addr);
104     qos_node_create_driver("virtio-serial-pci", virtio_serial_pci_create);
105     qos_node_consumes("virtio-serial-pci", "pci-bus", &edge_opts);
106     qos_node_produces("virtio-serial-pci", "pci-device");
107     qos_node_produces("virtio-serial-pci", "virtio");
108     qos_node_produces("virtio-serial-pci", "virtio-serial");
109 }
110 
111 libqos_init(virtio_serial_register_nodes);
112