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