xref: /qemu/tests/qtest/libqos/loongarch-virt-machine.c (revision dec9742cbc59415a8b83e382e7ae36395394e4bd)
1*fe43cc5bSBibo Mao /*
2*fe43cc5bSBibo Mao  * libqos driver framework
3*fe43cc5bSBibo Mao  *
4*fe43cc5bSBibo Mao  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5*fe43cc5bSBibo Mao  *
6*fe43cc5bSBibo Mao  * This library is free software; you can redistribute it and/or
7*fe43cc5bSBibo Mao  * modify it under the terms of the GNU Lesser General Public
8*fe43cc5bSBibo Mao  * License version 2.1 as published by the Free Software Foundation.
9*fe43cc5bSBibo Mao  *
10*fe43cc5bSBibo Mao  * This library is distributed in the hope that it will be useful,
11*fe43cc5bSBibo Mao  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*fe43cc5bSBibo Mao  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*fe43cc5bSBibo Mao  * Lesser General Public License for more details.
14*fe43cc5bSBibo Mao  *
15*fe43cc5bSBibo Mao  * You should have received a copy of the GNU Lesser General Public
16*fe43cc5bSBibo Mao  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17*fe43cc5bSBibo Mao  */
18*fe43cc5bSBibo Mao 
19*fe43cc5bSBibo Mao #include "qemu/osdep.h"
20*fe43cc5bSBibo Mao #include "../libqtest.h"
21*fe43cc5bSBibo Mao #include "qemu/module.h"
22*fe43cc5bSBibo Mao #include "libqos-malloc.h"
23*fe43cc5bSBibo Mao #include "qgraph.h"
24*fe43cc5bSBibo Mao #include "virtio-mmio.h"
25*fe43cc5bSBibo Mao #include "generic-pcihost.h"
26*fe43cc5bSBibo Mao #include "hw/pci/pci_regs.h"
27*fe43cc5bSBibo Mao 
28*fe43cc5bSBibo Mao #define LOONGARCH_PAGE_SIZE               0x1000
29*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_RAM_ADDR           0x100000
30*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_RAM_SIZE           0xFF00000
31*fe43cc5bSBibo Mao 
32*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_PIO_BASE           0x18000000
33*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_PCIE_PIO_OFFSET    0x4000
34*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_PCIE_PIO_LIMIT     0x10000
35*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_PCIE_ECAM_BASE     0x20000000
36*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_PCIE_MMIO32_BASE   0x40000000
37*fe43cc5bSBibo Mao #define LOONGARCH_VIRT_PCIE_MMIO32_LIMIT  0x80000000
38*fe43cc5bSBibo Mao 
39*fe43cc5bSBibo Mao typedef struct QVirtMachine QVirtMachine;
40*fe43cc5bSBibo Mao 
41*fe43cc5bSBibo Mao struct QVirtMachine {
42*fe43cc5bSBibo Mao     QOSGraphObject obj;
43*fe43cc5bSBibo Mao     QGuestAllocator alloc;
44*fe43cc5bSBibo Mao     QVirtioMMIODevice virtio_mmio;
45*fe43cc5bSBibo Mao     QGenericPCIHost bridge;
46*fe43cc5bSBibo Mao };
47*fe43cc5bSBibo Mao 
virt_destructor(QOSGraphObject * obj)48*fe43cc5bSBibo Mao static void virt_destructor(QOSGraphObject *obj)
49*fe43cc5bSBibo Mao {
50*fe43cc5bSBibo Mao     QVirtMachine *machine = (QVirtMachine *) obj;
51*fe43cc5bSBibo Mao     alloc_destroy(&machine->alloc);
52*fe43cc5bSBibo Mao }
53*fe43cc5bSBibo Mao 
virt_get_driver(void * object,const char * interface)54*fe43cc5bSBibo Mao static void *virt_get_driver(void *object, const char *interface)
55*fe43cc5bSBibo Mao {
56*fe43cc5bSBibo Mao     QVirtMachine *machine = object;
57*fe43cc5bSBibo Mao     if (!g_strcmp0(interface, "memory")) {
58*fe43cc5bSBibo Mao         return &machine->alloc;
59*fe43cc5bSBibo Mao     }
60*fe43cc5bSBibo Mao 
61*fe43cc5bSBibo Mao     fprintf(stderr, "%s not present in loongarch/virtio\n", interface);
62*fe43cc5bSBibo Mao     g_assert_not_reached();
63*fe43cc5bSBibo Mao }
64*fe43cc5bSBibo Mao 
virt_get_device(void * obj,const char * device)65*fe43cc5bSBibo Mao static QOSGraphObject *virt_get_device(void *obj, const char *device)
66*fe43cc5bSBibo Mao {
67*fe43cc5bSBibo Mao     QVirtMachine *machine = obj;
68*fe43cc5bSBibo Mao     if (!g_strcmp0(device, "generic-pcihost")) {
69*fe43cc5bSBibo Mao         return &machine->bridge.obj;
70*fe43cc5bSBibo Mao     } else if (!g_strcmp0(device, "virtio-mmio")) {
71*fe43cc5bSBibo Mao         return &machine->virtio_mmio.obj;
72*fe43cc5bSBibo Mao     }
73*fe43cc5bSBibo Mao 
74*fe43cc5bSBibo Mao     fprintf(stderr, "%s not present in loongarch/virt\n", device);
75*fe43cc5bSBibo Mao     g_assert_not_reached();
76*fe43cc5bSBibo Mao }
77*fe43cc5bSBibo Mao 
loongarch_config_qpci_bus(QGenericPCIBus * qpci)78*fe43cc5bSBibo Mao static void loongarch_config_qpci_bus(QGenericPCIBus *qpci)
79*fe43cc5bSBibo Mao {
80*fe43cc5bSBibo Mao     qpci->gpex_pio_base = LOONGARCH_VIRT_PIO_BASE;
81*fe43cc5bSBibo Mao     qpci->bus.pio_alloc_ptr = LOONGARCH_VIRT_PCIE_PIO_OFFSET;
82*fe43cc5bSBibo Mao     qpci->bus.pio_limit = LOONGARCH_VIRT_PCIE_PIO_LIMIT;
83*fe43cc5bSBibo Mao     qpci->bus.mmio_alloc_ptr = LOONGARCH_VIRT_PCIE_MMIO32_BASE;
84*fe43cc5bSBibo Mao     qpci->bus.mmio_limit = LOONGARCH_VIRT_PCIE_MMIO32_LIMIT;
85*fe43cc5bSBibo Mao     qpci->ecam_alloc_ptr = LOONGARCH_VIRT_PCIE_ECAM_BASE;
86*fe43cc5bSBibo Mao }
87*fe43cc5bSBibo Mao 
qos_create_machine_loongarch_virt(QTestState * qts)88*fe43cc5bSBibo Mao static void *qos_create_machine_loongarch_virt(QTestState *qts)
89*fe43cc5bSBibo Mao {
90*fe43cc5bSBibo Mao     QVirtMachine *machine = g_new0(QVirtMachine, 1);
91*fe43cc5bSBibo Mao 
92*fe43cc5bSBibo Mao     alloc_init(&machine->alloc, 0,
93*fe43cc5bSBibo Mao                LOONGARCH_VIRT_RAM_ADDR,
94*fe43cc5bSBibo Mao                LOONGARCH_VIRT_RAM_ADDR + LOONGARCH_VIRT_RAM_SIZE,
95*fe43cc5bSBibo Mao                LOONGARCH_PAGE_SIZE);
96*fe43cc5bSBibo Mao 
97*fe43cc5bSBibo Mao     qos_create_generic_pcihost(&machine->bridge, qts, &machine->alloc);
98*fe43cc5bSBibo Mao     loongarch_config_qpci_bus(&machine->bridge.pci);
99*fe43cc5bSBibo Mao 
100*fe43cc5bSBibo Mao     machine->obj.get_device = virt_get_device;
101*fe43cc5bSBibo Mao     machine->obj.get_driver = virt_get_driver;
102*fe43cc5bSBibo Mao     machine->obj.destructor = virt_destructor;
103*fe43cc5bSBibo Mao     return machine;
104*fe43cc5bSBibo Mao }
105*fe43cc5bSBibo Mao 
virt_machine_register_nodes(void)106*fe43cc5bSBibo Mao static void virt_machine_register_nodes(void)
107*fe43cc5bSBibo Mao {
108*fe43cc5bSBibo Mao     qos_node_create_machine_args("loongarch64/virt",
109*fe43cc5bSBibo Mao                                  qos_create_machine_loongarch_virt,
110*fe43cc5bSBibo Mao                                  " -cpu la464");
111*fe43cc5bSBibo Mao     qos_node_contains("loongarch64/virt", "generic-pcihost", NULL);
112*fe43cc5bSBibo Mao }
113*fe43cc5bSBibo Mao 
114*fe43cc5bSBibo Mao libqos_init(virt_machine_register_nodes);
115