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 "libqos/qgraph.h"
22 #include "pci-pc.h"
23 #include "qemu/module.h"
24 #include "malloc-pc.h"
25 
26 typedef struct QX86PCMachine QX86PCMachine;
27 typedef struct i440FX_pcihost i440FX_pcihost;
28 typedef struct QSDHCI_PCI  QSDHCI_PCI;
29 
30 struct i440FX_pcihost {
31     QOSGraphObject obj;
32     QPCIBusPC pci;
33 };
34 
35 struct QX86PCMachine {
36     QOSGraphObject obj;
37     QGuestAllocator alloc;
38     i440FX_pcihost bridge;
39 };
40 
41 /* i440FX_pcihost */
42 
43 static QOSGraphObject *i440FX_host_get_device(void *obj, const char *device)
44 {
45     i440FX_pcihost *host = obj;
46     if (!g_strcmp0(device, "pci-bus-pc")) {
47         return &host->pci.obj;
48     }
49     fprintf(stderr, "%s not present in i440FX-pcihost\n", device);
50     g_assert_not_reached();
51 }
52 
53 static void qos_create_i440FX_host(i440FX_pcihost *host,
54                                    QTestState *qts,
55                                    QGuestAllocator *alloc)
56 {
57     host->obj.get_device = i440FX_host_get_device;
58     qpci_init_pc(&host->pci, qts, alloc);
59 }
60 
61 /* x86_64/pc machine */
62 
63 static void pc_destructor(QOSGraphObject *obj)
64 {
65     QX86PCMachine *machine = (QX86PCMachine *) obj;
66     alloc_destroy(&machine->alloc);
67 }
68 
69 static void *pc_get_driver(void *object, const char *interface)
70 {
71     QX86PCMachine *machine = object;
72     if (!g_strcmp0(interface, "memory")) {
73         return &machine->alloc;
74     }
75 
76     fprintf(stderr, "%s not present in x86_64/pc\n", interface);
77     g_assert_not_reached();
78 }
79 
80 static QOSGraphObject *pc_get_device(void *obj, const char *device)
81 {
82     QX86PCMachine *machine = obj;
83     if (!g_strcmp0(device, "i440FX-pcihost")) {
84         return &machine->bridge.obj;
85     }
86 
87     fprintf(stderr, "%s not present in x86_64/pc\n", device);
88     g_assert_not_reached();
89 }
90 
91 static void *qos_create_machine_pc(QTestState *qts)
92 {
93     QX86PCMachine *machine = g_new0(QX86PCMachine, 1);
94     machine->obj.get_device = pc_get_device;
95     machine->obj.get_driver = pc_get_driver;
96     machine->obj.destructor = pc_destructor;
97     pc_alloc_init(&machine->alloc, qts, ALLOC_NO_FLAGS);
98     qos_create_i440FX_host(&machine->bridge, qts, &machine->alloc);
99 
100     return &machine->obj;
101 }
102 
103 static void pc_machine_register_nodes(void)
104 {
105     qos_node_create_machine("i386/pc", qos_create_machine_pc);
106     qos_node_contains("i386/pc", "i440FX-pcihost", NULL);
107 
108     qos_node_create_machine("x86_64/pc", qos_create_machine_pc);
109     qos_node_contains("x86_64/pc", "i440FX-pcihost", NULL);
110 
111     qos_node_create_driver("i440FX-pcihost", NULL);
112     qos_node_contains("i440FX-pcihost", "pci-bus-pc", NULL);
113 }
114 
115 libqos_init(pc_machine_register_nodes);
116