xref: /qemu/tests/qtest/libqos/virtio-iommu.c (revision becf88730b86c94137cad20c98ef3b8d24559b9b)
1*becf8873SEric Auger /*
2*becf8873SEric Auger  * libqos driver virtio-iommu-pci framework
3*becf8873SEric Auger  *
4*becf8873SEric Auger  * Copyright (c) 2021 Red Hat, Inc.
5*becf8873SEric Auger  *
6*becf8873SEric Auger  * Authors:
7*becf8873SEric Auger  *  Eric Auger <eric.auger@redhat.com>
8*becf8873SEric Auger  *
9*becf8873SEric Auger  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
10*becf8873SEric Auger  * option) any later version.  See the COPYING file in the top-level directory.
11*becf8873SEric Auger  *
12*becf8873SEric Auger  */
13*becf8873SEric Auger 
14*becf8873SEric Auger #include "qemu/osdep.h"
15*becf8873SEric Auger #include "libqtest.h"
16*becf8873SEric Auger #include "qemu/module.h"
17*becf8873SEric Auger #include "qgraph.h"
18*becf8873SEric Auger #include "virtio-iommu.h"
19*becf8873SEric Auger #include "hw/virtio/virtio-iommu.h"
20*becf8873SEric Auger 
21*becf8873SEric Auger static QGuestAllocator *alloc;
22*becf8873SEric Auger 
23*becf8873SEric Auger /* virtio-iommu-device */
24*becf8873SEric Auger static void *qvirtio_iommu_get_driver(QVirtioIOMMU *v_iommu,
25*becf8873SEric Auger                                       const char *interface)
26*becf8873SEric Auger {
27*becf8873SEric Auger     if (!g_strcmp0(interface, "virtio-iommu")) {
28*becf8873SEric Auger         return v_iommu;
29*becf8873SEric Auger     }
30*becf8873SEric Auger     if (!g_strcmp0(interface, "virtio")) {
31*becf8873SEric Auger         return v_iommu->vdev;
32*becf8873SEric Auger     }
33*becf8873SEric Auger 
34*becf8873SEric Auger     fprintf(stderr, "%s not present in virtio-iommu-device\n", interface);
35*becf8873SEric Auger     g_assert_not_reached();
36*becf8873SEric Auger }
37*becf8873SEric Auger 
38*becf8873SEric Auger static void virtio_iommu_cleanup(QVirtioIOMMU *interface)
39*becf8873SEric Auger {
40*becf8873SEric Auger     qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
41*becf8873SEric Auger }
42*becf8873SEric Auger 
43*becf8873SEric Auger static void virtio_iommu_setup(QVirtioIOMMU *interface)
44*becf8873SEric Auger {
45*becf8873SEric Auger     QVirtioDevice *vdev = interface->vdev;
46*becf8873SEric Auger     uint64_t features;
47*becf8873SEric Auger 
48*becf8873SEric Auger     features = qvirtio_get_features(vdev);
49*becf8873SEric Auger     features &= ~(QVIRTIO_F_BAD_FEATURE |
50*becf8873SEric Auger                   (1ull << VIRTIO_RING_F_INDIRECT_DESC) |
51*becf8873SEric Auger                   (1ull << VIRTIO_RING_F_EVENT_IDX) |
52*becf8873SEric Auger                   (1ull << VIRTIO_IOMMU_F_BYPASS));
53*becf8873SEric Auger     qvirtio_set_features(vdev, features);
54*becf8873SEric Auger     interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
55*becf8873SEric Auger     qvirtio_set_driver_ok(interface->vdev);
56*becf8873SEric Auger }
57*becf8873SEric Auger 
58*becf8873SEric Auger /* virtio-iommu-pci */
59*becf8873SEric Auger static void *qvirtio_iommu_pci_get_driver(void *object, const char *interface)
60*becf8873SEric Auger {
61*becf8873SEric Auger     QVirtioIOMMUPCI *v_iommu = object;
62*becf8873SEric Auger     if (!g_strcmp0(interface, "pci-device")) {
63*becf8873SEric Auger         return v_iommu->pci_vdev.pdev;
64*becf8873SEric Auger     }
65*becf8873SEric Auger     return qvirtio_iommu_get_driver(&v_iommu->iommu, interface);
66*becf8873SEric Auger }
67*becf8873SEric Auger 
68*becf8873SEric Auger static void qvirtio_iommu_pci_destructor(QOSGraphObject *obj)
69*becf8873SEric Auger {
70*becf8873SEric Auger     QVirtioIOMMUPCI *iommu_pci = (QVirtioIOMMUPCI *) obj;
71*becf8873SEric Auger     QVirtioIOMMU *interface = &iommu_pci->iommu;
72*becf8873SEric Auger     QOSGraphObject *pci_vobj =  &iommu_pci->pci_vdev.obj;
73*becf8873SEric Auger 
74*becf8873SEric Auger     virtio_iommu_cleanup(interface);
75*becf8873SEric Auger     qvirtio_pci_destructor(pci_vobj);
76*becf8873SEric Auger }
77*becf8873SEric Auger 
78*becf8873SEric Auger static void qvirtio_iommu_pci_start_hw(QOSGraphObject *obj)
79*becf8873SEric Auger {
80*becf8873SEric Auger     QVirtioIOMMUPCI *iommu_pci = (QVirtioIOMMUPCI *) obj;
81*becf8873SEric Auger     QVirtioIOMMU *interface = &iommu_pci->iommu;
82*becf8873SEric Auger     QOSGraphObject *pci_vobj =  &iommu_pci->pci_vdev.obj;
83*becf8873SEric Auger 
84*becf8873SEric Auger     qvirtio_pci_start_hw(pci_vobj);
85*becf8873SEric Auger     virtio_iommu_setup(interface);
86*becf8873SEric Auger }
87*becf8873SEric Auger 
88*becf8873SEric Auger 
89*becf8873SEric Auger static void *virtio_iommu_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
90*becf8873SEric Auger                                    void *addr)
91*becf8873SEric Auger {
92*becf8873SEric Auger     QVirtioIOMMUPCI *virtio_rpci = g_new0(QVirtioIOMMUPCI, 1);
93*becf8873SEric Auger     QVirtioIOMMU *interface = &virtio_rpci->iommu;
94*becf8873SEric Auger     QOSGraphObject *obj = &virtio_rpci->pci_vdev.obj;
95*becf8873SEric Auger 
96*becf8873SEric Auger     virtio_pci_init(&virtio_rpci->pci_vdev, pci_bus, addr);
97*becf8873SEric Auger     interface->vdev = &virtio_rpci->pci_vdev.vdev;
98*becf8873SEric Auger     alloc = t_alloc;
99*becf8873SEric Auger 
100*becf8873SEric Auger     obj->get_driver = qvirtio_iommu_pci_get_driver;
101*becf8873SEric Auger     obj->start_hw = qvirtio_iommu_pci_start_hw;
102*becf8873SEric Auger     obj->destructor = qvirtio_iommu_pci_destructor;
103*becf8873SEric Auger 
104*becf8873SEric Auger     return obj;
105*becf8873SEric Auger }
106*becf8873SEric Auger 
107*becf8873SEric Auger static void virtio_iommu_register_nodes(void)
108*becf8873SEric Auger {
109*becf8873SEric Auger     QPCIAddress addr = {
110*becf8873SEric Auger         .devfn = QPCI_DEVFN(4, 0),
111*becf8873SEric Auger     };
112*becf8873SEric Auger 
113*becf8873SEric Auger     QOSGraphEdgeOptions opts = {
114*becf8873SEric Auger         .extra_device_opts = "addr=04.0",
115*becf8873SEric Auger     };
116*becf8873SEric Auger 
117*becf8873SEric Auger     /* virtio-iommu-pci */
118*becf8873SEric Auger     add_qpci_address(&opts, &addr);
119*becf8873SEric Auger     qos_node_create_driver("virtio-iommu-pci", virtio_iommu_pci_create);
120*becf8873SEric Auger     qos_node_consumes("virtio-iommu-pci", "pci-bus", &opts);
121*becf8873SEric Auger     qos_node_produces("virtio-iommu-pci", "pci-device");
122*becf8873SEric Auger     qos_node_produces("virtio-iommu-pci", "virtio");
123*becf8873SEric Auger     qos_node_produces("virtio-iommu-pci", "virtio-iommu");
124*becf8873SEric Auger }
125*becf8873SEric Auger 
126*becf8873SEric Auger libqos_init(virtio_iommu_register_nodes);
127