2 * libqos driver framework for risc-v
4 * Initial version based on arm-virt-machine.c
6 * Copyright (c) 2024 Ventana Micro
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.
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.
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/>
21 #include "qemu/osdep.h"
22 #include "../libqtest.h"
23 #include "qemu/module.h"
24 #include "libqos-malloc.h"
26 #include "virtio-mmio.h"
27 #include "generic-pcihost.h"
28 #include "hw/pci/pci_regs.h"
30 #define RISCV_PAGE_SIZE 4096
33 #define RISCV_VIRT_RAM_ADDR 0x80000000
34 #define RISCV_VIRT_RAM_SIZE 0x20000000
37 * VIRT_VIRTIO. BASE_ADDR points to the last
40 #define VIRTIO_MMIO_BASE_ADDR 0x10008000
41 #define VIRTIO_MMIO_SIZE 0x00001000
44 #define RISCV_GPEX_PIO_BASE 0x3000000
45 #define RISCV_BUS_PIO_LIMIT 0x10000
48 #define RISCV_BUS_MMIO_ALLOC_PTR 0x40000000
49 #define RISCV_BUS_MMIO_LIMIT 0x80000000
52 #define RISCV_ECAM_ALLOC_PTR 0x30000000
54 typedef struct QVirtMachine QVirtMachine
;
58 QGuestAllocator alloc
;
59 QVirtioMMIODevice virtio_mmio
;
60 QGenericPCIHost bridge
;
63 static void virt_destructor(QOSGraphObject
*obj
)
65 QVirtMachine
*machine
= (QVirtMachine
*) obj
;
66 alloc_destroy(&machine
->alloc
);
69 static void *virt_get_driver(void *object
, const char *interface
)
71 QVirtMachine
*machine
= object
;
72 if (!g_strcmp0(interface
, "memory")) {
73 return &machine
->alloc
;
76 fprintf(stderr
, "%s not present in riscv/virtio\n", interface
);
77 g_assert_not_reached();
80 static QOSGraphObject
*virt_get_device(void *obj
, const char *device
)
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
;
89 fprintf(stderr
, "%s not present in riscv/virt\n", device
);
90 g_assert_not_reached();
93 static void riscv_config_qpci_bus(QGenericPCIBus
*qpci
)
95 qpci
->gpex_pio_base
= RISCV_GPEX_PIO_BASE
;
96 qpci
->bus
.pio_limit
= RISCV_BUS_PIO_LIMIT
;
98 qpci
->bus
.mmio_alloc_ptr
= RISCV_BUS_MMIO_ALLOC_PTR
;
99 qpci
->bus
.mmio_limit
= RISCV_BUS_MMIO_LIMIT
;
101 qpci
->ecam_alloc_ptr
= RISCV_ECAM_ALLOC_PTR
;
104 static void *qos_create_machine_riscv_virt(QTestState
*qts
)
106 QVirtMachine
*machine
= g_new0(QVirtMachine
, 1);
108 alloc_init(&machine
->alloc
, 0,
110 RISCV_VIRT_RAM_ADDR
+ RISCV_VIRT_RAM_SIZE
,
112 qvirtio_mmio_init_device(&machine
->virtio_mmio
, qts
, VIRTIO_MMIO_BASE_ADDR
,
115 qos_create_generic_pcihost(&machine
->bridge
, qts
, &machine
->alloc
);
116 riscv_config_qpci_bus(&machine
->bridge
.pci
);
118 machine
->obj
.get_device
= virt_get_device
;
119 machine
->obj
.get_driver
= virt_get_driver
;
120 machine
->obj
.destructor
= virt_destructor
;
124 static void virt_machine_register_nodes(void)
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
);
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
);
137 libqos_init(virt_machine_register_nodes
);