xref: /openbmc/qemu/tests/qtest/libqos/riscv-iommu.c (revision feb58e3b)
1 /*
2  * libqos driver riscv-iommu-pci framework
3  *
4  * Copyright (c) 2024 Ventana Micro Systems Inc.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
7  * option) any later version.  See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #include "qemu/osdep.h"
12 #include "../libqtest.h"
13 #include "qemu/module.h"
14 #include "qgraph.h"
15 #include "pci.h"
16 #include "riscv-iommu.h"
17 
18 static void *riscv_iommu_pci_get_driver(void *obj, const char *interface)
19 {
20     QRISCVIOMMU *r_iommu_pci = obj;
21 
22     if (!g_strcmp0(interface, "pci-device")) {
23         return &r_iommu_pci->dev;
24     }
25 
26     fprintf(stderr, "%s not present in riscv_iommu_pci\n", interface);
27     g_assert_not_reached();
28 }
29 
30 static void riscv_iommu_pci_start_hw(QOSGraphObject *obj)
31 {
32     QRISCVIOMMU *pci = (QRISCVIOMMU *)obj;
33     qpci_device_enable(&pci->dev);
34 }
35 
36 static void riscv_iommu_pci_destructor(QOSGraphObject *obj)
37 {
38     QRISCVIOMMU *pci = (QRISCVIOMMU *)obj;
39     qpci_iounmap(&pci->dev, pci->reg_bar);
40 }
41 
42 static void *riscv_iommu_pci_create(void *pci_bus, QGuestAllocator *alloc,
43                                     void *addr)
44 {
45     QRISCVIOMMU *r_iommu_pci = g_new0(QRISCVIOMMU, 1);
46     QPCIBus *bus = pci_bus;
47 
48     qpci_device_init(&r_iommu_pci->dev, bus, addr);
49     r_iommu_pci->reg_bar = qpci_iomap(&r_iommu_pci->dev, 0, NULL);
50 
51     r_iommu_pci->obj.get_driver = riscv_iommu_pci_get_driver;
52     r_iommu_pci->obj.start_hw = riscv_iommu_pci_start_hw;
53     r_iommu_pci->obj.destructor = riscv_iommu_pci_destructor;
54     return &r_iommu_pci->obj;
55 }
56 
57 static void riscv_iommu_pci_register_nodes(void)
58 {
59     QPCIAddress addr = {
60         .vendor_id = RISCV_IOMMU_PCI_VENDOR_ID,
61         .device_id = RISCV_IOMMU_PCI_DEVICE_ID,
62         .devfn = QPCI_DEVFN(1, 0),
63     };
64 
65     QOSGraphEdgeOptions opts = {
66         .extra_device_opts = "addr=01.0",
67     };
68 
69     add_qpci_address(&opts, &addr);
70 
71     qos_node_create_driver("riscv-iommu-pci", riscv_iommu_pci_create);
72     qos_node_produces("riscv-iommu-pci", "pci-device");
73     qos_node_consumes("riscv-iommu-pci", "pci-bus", &opts);
74 }
75 
76 libqos_init(riscv_iommu_pci_register_nodes);
77