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