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