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 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 "libqos/malloc.h"
23 #include "libqos/qgraph.h"
24 #include "libqos/virtio-mmio.h"
25 
26 #define ARM_PAGE_SIZE               4096
27 #define VIRTIO_MMIO_BASE_ADDR       0x0A003E00
28 #define ARM_VIRT_RAM_ADDR           0x40000000
29 #define ARM_VIRT_RAM_SIZE           0x20000000
30 #define VIRTIO_MMIO_SIZE            0x00000200
31 
32 typedef struct QVirtMachine QVirtMachine;
33 
34 struct QVirtMachine {
35     QOSGraphObject obj;
36     QGuestAllocator alloc;
37     QVirtioMMIODevice virtio_mmio;
38 };
39 
40 static void virt_destructor(QOSGraphObject *obj)
41 {
42     QVirtMachine *machine = (QVirtMachine *) obj;
43     alloc_destroy(&machine->alloc);
44 }
45 
46 static void *virt_get_driver(void *object, const char *interface)
47 {
48     QVirtMachine *machine = object;
49     if (!g_strcmp0(interface, "memory")) {
50         return &machine->alloc;
51     }
52 
53     fprintf(stderr, "%s not present in arm/virtio\n", interface);
54     g_assert_not_reached();
55 }
56 
57 static QOSGraphObject *virt_get_device(void *obj, const char *device)
58 {
59     QVirtMachine *machine = obj;
60     if (!g_strcmp0(device, "virtio-mmio")) {
61         return &machine->virtio_mmio.obj;
62     }
63 
64     fprintf(stderr, "%s not present in arm/virtio\n", device);
65     g_assert_not_reached();
66 }
67 
68 static void *qos_create_machine_arm_virt(QTestState *qts)
69 {
70     QVirtMachine *machine = g_new0(QVirtMachine, 1);
71 
72     alloc_init(&machine->alloc, 0,
73                ARM_VIRT_RAM_ADDR,
74                ARM_VIRT_RAM_ADDR + ARM_VIRT_RAM_SIZE,
75                ARM_PAGE_SIZE);
76     qvirtio_mmio_init_device(&machine->virtio_mmio, qts, VIRTIO_MMIO_BASE_ADDR,
77                              VIRTIO_MMIO_SIZE);
78 
79     machine->obj.get_device = virt_get_device;
80     machine->obj.get_driver = virt_get_driver;
81     machine->obj.destructor = virt_destructor;
82     return machine;
83 }
84 
85 static void virtio_mmio_register_nodes(void)
86 {
87     qos_node_create_machine("arm/virt", qos_create_machine_arm_virt);
88     qos_node_contains("arm/virt", "virtio-mmio", NULL);
89 }
90 
91 libqos_init(virtio_mmio_register_nodes);
92