1*1cf4323eSThomas Huth /*
2*1cf4323eSThomas Huth  * libqos driver framework
3*1cf4323eSThomas Huth  *
4*1cf4323eSThomas Huth  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5*1cf4323eSThomas Huth  *
6*1cf4323eSThomas Huth  * This library is free software; you can redistribute it and/or
7*1cf4323eSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8*1cf4323eSThomas Huth  * License version 2 as published by the Free Software Foundation.
9*1cf4323eSThomas Huth  *
10*1cf4323eSThomas Huth  * This library is distributed in the hope that it will be useful,
11*1cf4323eSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*1cf4323eSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*1cf4323eSThomas Huth  * Lesser General Public License for more details.
14*1cf4323eSThomas Huth  *
15*1cf4323eSThomas Huth  * You should have received a copy of the GNU Lesser General Public
16*1cf4323eSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17*1cf4323eSThomas Huth  */
18*1cf4323eSThomas Huth 
19*1cf4323eSThomas Huth #include "qemu/osdep.h"
20*1cf4323eSThomas Huth #include "libqtest.h"
21*1cf4323eSThomas Huth #include "libqos/qgraph.h"
22*1cf4323eSThomas Huth #include "pci-pc.h"
23*1cf4323eSThomas Huth #include "qemu/module.h"
24*1cf4323eSThomas Huth #include "malloc-pc.h"
25*1cf4323eSThomas Huth 
26*1cf4323eSThomas Huth typedef struct QX86PCMachine QX86PCMachine;
27*1cf4323eSThomas Huth typedef struct i440FX_pcihost i440FX_pcihost;
28*1cf4323eSThomas Huth typedef struct QSDHCI_PCI  QSDHCI_PCI;
29*1cf4323eSThomas Huth 
30*1cf4323eSThomas Huth struct i440FX_pcihost {
31*1cf4323eSThomas Huth     QOSGraphObject obj;
32*1cf4323eSThomas Huth     QPCIBusPC pci;
33*1cf4323eSThomas Huth };
34*1cf4323eSThomas Huth 
35*1cf4323eSThomas Huth struct QX86PCMachine {
36*1cf4323eSThomas Huth     QOSGraphObject obj;
37*1cf4323eSThomas Huth     QGuestAllocator alloc;
38*1cf4323eSThomas Huth     i440FX_pcihost bridge;
39*1cf4323eSThomas Huth };
40*1cf4323eSThomas Huth 
41*1cf4323eSThomas Huth /* i440FX_pcihost */
42*1cf4323eSThomas Huth 
43*1cf4323eSThomas Huth static QOSGraphObject *i440FX_host_get_device(void *obj, const char *device)
44*1cf4323eSThomas Huth {
45*1cf4323eSThomas Huth     i440FX_pcihost *host = obj;
46*1cf4323eSThomas Huth     if (!g_strcmp0(device, "pci-bus-pc")) {
47*1cf4323eSThomas Huth         return &host->pci.obj;
48*1cf4323eSThomas Huth     }
49*1cf4323eSThomas Huth     fprintf(stderr, "%s not present in i440FX-pcihost\n", device);
50*1cf4323eSThomas Huth     g_assert_not_reached();
51*1cf4323eSThomas Huth }
52*1cf4323eSThomas Huth 
53*1cf4323eSThomas Huth static void qos_create_i440FX_host(i440FX_pcihost *host,
54*1cf4323eSThomas Huth                                    QTestState *qts,
55*1cf4323eSThomas Huth                                    QGuestAllocator *alloc)
56*1cf4323eSThomas Huth {
57*1cf4323eSThomas Huth     host->obj.get_device = i440FX_host_get_device;
58*1cf4323eSThomas Huth     qpci_init_pc(&host->pci, qts, alloc);
59*1cf4323eSThomas Huth }
60*1cf4323eSThomas Huth 
61*1cf4323eSThomas Huth /* x86_64/pc machine */
62*1cf4323eSThomas Huth 
63*1cf4323eSThomas Huth static void pc_destructor(QOSGraphObject *obj)
64*1cf4323eSThomas Huth {
65*1cf4323eSThomas Huth     QX86PCMachine *machine = (QX86PCMachine *) obj;
66*1cf4323eSThomas Huth     alloc_destroy(&machine->alloc);
67*1cf4323eSThomas Huth }
68*1cf4323eSThomas Huth 
69*1cf4323eSThomas Huth static void *pc_get_driver(void *object, const char *interface)
70*1cf4323eSThomas Huth {
71*1cf4323eSThomas Huth     QX86PCMachine *machine = object;
72*1cf4323eSThomas Huth     if (!g_strcmp0(interface, "memory")) {
73*1cf4323eSThomas Huth         return &machine->alloc;
74*1cf4323eSThomas Huth     }
75*1cf4323eSThomas Huth 
76*1cf4323eSThomas Huth     fprintf(stderr, "%s not present in x86_64/pc\n", interface);
77*1cf4323eSThomas Huth     g_assert_not_reached();
78*1cf4323eSThomas Huth }
79*1cf4323eSThomas Huth 
80*1cf4323eSThomas Huth static QOSGraphObject *pc_get_device(void *obj, const char *device)
81*1cf4323eSThomas Huth {
82*1cf4323eSThomas Huth     QX86PCMachine *machine = obj;
83*1cf4323eSThomas Huth     if (!g_strcmp0(device, "i440FX-pcihost")) {
84*1cf4323eSThomas Huth         return &machine->bridge.obj;
85*1cf4323eSThomas Huth     }
86*1cf4323eSThomas Huth 
87*1cf4323eSThomas Huth     fprintf(stderr, "%s not present in x86_64/pc\n", device);
88*1cf4323eSThomas Huth     g_assert_not_reached();
89*1cf4323eSThomas Huth }
90*1cf4323eSThomas Huth 
91*1cf4323eSThomas Huth static void *qos_create_machine_pc(QTestState *qts)
92*1cf4323eSThomas Huth {
93*1cf4323eSThomas Huth     QX86PCMachine *machine = g_new0(QX86PCMachine, 1);
94*1cf4323eSThomas Huth     machine->obj.get_device = pc_get_device;
95*1cf4323eSThomas Huth     machine->obj.get_driver = pc_get_driver;
96*1cf4323eSThomas Huth     machine->obj.destructor = pc_destructor;
97*1cf4323eSThomas Huth     pc_alloc_init(&machine->alloc, qts, ALLOC_NO_FLAGS);
98*1cf4323eSThomas Huth     qos_create_i440FX_host(&machine->bridge, qts, &machine->alloc);
99*1cf4323eSThomas Huth 
100*1cf4323eSThomas Huth     return &machine->obj;
101*1cf4323eSThomas Huth }
102*1cf4323eSThomas Huth 
103*1cf4323eSThomas Huth static void pc_machine_register_nodes(void)
104*1cf4323eSThomas Huth {
105*1cf4323eSThomas Huth     qos_node_create_machine("i386/pc", qos_create_machine_pc);
106*1cf4323eSThomas Huth     qos_node_contains("i386/pc", "i440FX-pcihost", NULL);
107*1cf4323eSThomas Huth 
108*1cf4323eSThomas Huth     qos_node_create_machine("x86_64/pc", qos_create_machine_pc);
109*1cf4323eSThomas Huth     qos_node_contains("x86_64/pc", "i440FX-pcihost", NULL);
110*1cf4323eSThomas Huth 
111*1cf4323eSThomas Huth     qos_node_create_driver("i440FX-pcihost", NULL);
112*1cf4323eSThomas Huth     qos_node_contains("i440FX-pcihost", "pci-bus-pc", NULL);
113*1cf4323eSThomas Huth }
114*1cf4323eSThomas Huth 
115*1cf4323eSThomas Huth libqos_init(pc_machine_register_nodes);
116