1 /*
2  * libqos driver framework for risc-v
3  *
4  * Initial version based on arm-virt-machine.c
5  *
6  * Copyright (c) 2024 Ventana Micro
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>
19  */
20 
21 #include "qemu/osdep.h"
22 #include "../libqtest.h"
23 #include "qemu/module.h"
24 #include "libqos-malloc.h"
25 #include "qgraph.h"
26 #include "virtio-mmio.h"
27 #include "generic-pcihost.h"
28 #include "hw/pci/pci_regs.h"
29 
30 #define RISCV_PAGE_SIZE            4096
31 
32 /* VIRT_DRAM */
33 #define RISCV_VIRT_RAM_ADDR        0x80000000
34 #define RISCV_VIRT_RAM_SIZE        0x20000000
35 
36 /*
37  * VIRT_VIRTIO. BASE_ADDR  points to the last
38  * virtio_mmio device.
39  */
40 #define VIRTIO_MMIO_BASE_ADDR      0x10008000
41 #define VIRTIO_MMIO_SIZE           0x00001000
42 
43 /* VIRT_PCIE_PIO  */
44 #define RISCV_GPEX_PIO_BASE        0x3000000
45 #define RISCV_BUS_PIO_LIMIT        0x10000
46 
47 /* VIRT_PCIE_MMIO */
48 #define RISCV_BUS_MMIO_ALLOC_PTR   0x40000000
49 #define RISCV_BUS_MMIO_LIMIT       0x80000000
50 
51 /* VIRT_PCIE_ECAM */
52 #define RISCV_ECAM_ALLOC_PTR   0x30000000
53 
54 typedef struct QVirtMachine QVirtMachine;
55 
56 struct QVirtMachine {
57     QOSGraphObject obj;
58     QGuestAllocator alloc;
59     QVirtioMMIODevice virtio_mmio;
60     QGenericPCIHost bridge;
61 };
62 
63 static void virt_destructor(QOSGraphObject *obj)
64 {
65     QVirtMachine *machine = (QVirtMachine *) obj;
66     alloc_destroy(&machine->alloc);
67 }
68 
69 static void *virt_get_driver(void *object, const char *interface)
70 {
71     QVirtMachine *machine = object;
72     if (!g_strcmp0(interface, "memory")) {
73         return &machine->alloc;
74     }
75 
76     fprintf(stderr, "%s not present in riscv/virtio\n", interface);
77     g_assert_not_reached();
78 }
79 
80 static QOSGraphObject *virt_get_device(void *obj, const char *device)
81 {
82     QVirtMachine *machine = obj;
83     if (!g_strcmp0(device, "generic-pcihost")) {
84         return &machine->bridge.obj;
85     } else if (!g_strcmp0(device, "virtio-mmio")) {
86         return &machine->virtio_mmio.obj;
87     }
88 
89     fprintf(stderr, "%s not present in riscv/virt\n", device);
90     g_assert_not_reached();
91 }
92 
93 static void riscv_config_qpci_bus(QGenericPCIBus *qpci)
94 {
95     qpci->gpex_pio_base = RISCV_GPEX_PIO_BASE;
96     qpci->bus.pio_limit = RISCV_BUS_PIO_LIMIT;
97 
98     qpci->bus.mmio_alloc_ptr = RISCV_BUS_MMIO_ALLOC_PTR;
99     qpci->bus.mmio_limit = RISCV_BUS_MMIO_LIMIT;
100 
101     qpci->ecam_alloc_ptr = RISCV_ECAM_ALLOC_PTR;
102 }
103 
104 static void *qos_create_machine_riscv_virt(QTestState *qts)
105 {
106     QVirtMachine *machine = g_new0(QVirtMachine, 1);
107 
108     alloc_init(&machine->alloc, 0,
109                RISCV_VIRT_RAM_ADDR,
110                RISCV_VIRT_RAM_ADDR + RISCV_VIRT_RAM_SIZE,
111                RISCV_PAGE_SIZE);
112     qvirtio_mmio_init_device(&machine->virtio_mmio, qts, VIRTIO_MMIO_BASE_ADDR,
113                               VIRTIO_MMIO_SIZE);
114 
115     qos_create_generic_pcihost(&machine->bridge, qts, &machine->alloc);
116     riscv_config_qpci_bus(&machine->bridge.pci);
117 
118     machine->obj.get_device = virt_get_device;
119     machine->obj.get_driver = virt_get_driver;
120     machine->obj.destructor = virt_destructor;
121     return machine;
122 }
123 
124 static void virt_machine_register_nodes(void)
125 {
126     qos_node_create_machine_args("riscv32/virt", qos_create_machine_riscv_virt,
127                                  "aclint=on,aia=aplic-imsic");
128     qos_node_contains("riscv32/virt", "virtio-mmio", NULL);
129     qos_node_contains("riscv32/virt", "generic-pcihost", NULL);
130 
131     qos_node_create_machine_args("riscv64/virt", qos_create_machine_riscv_virt,
132                                  "aclint=on,aia=aplic-imsic");
133     qos_node_contains("riscv64/virt", "virtio-mmio", NULL);
134     qos_node_contains("riscv64/virt", "generic-pcihost", NULL);
135 }
136 
137 libqos_init(virt_machine_register_nodes);
138