]> Git Repo - qemu.git/blob - tests/libqos/pci-pc.c
tests: Clean up includes
[qemu.git] / tests / libqos / pci-pc.c
1 /*
2  * libqos PCI bindings for PC
3  *
4  * Copyright IBM, Corp. 2012-2013
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12
13 #include "qemu/osdep.h"
14 #include "libqtest.h"
15 #include "libqos/pci-pc.h"
16
17 #include "hw/pci/pci_regs.h"
18
19 #include "qemu-common.h"
20 #include "qemu/host-utils.h"
21
22 #include <glib.h>
23
24 #define ACPI_PCIHP_ADDR         0xae00
25 #define PCI_EJ_BASE             0x0008
26
27 typedef struct QPCIBusPC
28 {
29     QPCIBus bus;
30
31     uint32_t pci_hole_start;
32     uint32_t pci_hole_size;
33     uint32_t pci_hole_alloc;
34
35     uint16_t pci_iohole_start;
36     uint16_t pci_iohole_size;
37     uint16_t pci_iohole_alloc;
38 } QPCIBusPC;
39
40 static uint8_t qpci_pc_io_readb(QPCIBus *bus, void *addr)
41 {
42     uintptr_t port = (uintptr_t)addr;
43     uint8_t value;
44
45     if (port < 0x10000) {
46         value = inb(port);
47     } else {
48         value = readb(port);
49     }
50
51     return value;
52 }
53
54 static uint16_t qpci_pc_io_readw(QPCIBus *bus, void *addr)
55 {
56     uintptr_t port = (uintptr_t)addr;
57     uint16_t value;
58
59     if (port < 0x10000) {
60         value = inw(port);
61     } else {
62         value = readw(port);
63     }
64
65     return value;
66 }
67
68 static uint32_t qpci_pc_io_readl(QPCIBus *bus, void *addr)
69 {
70     uintptr_t port = (uintptr_t)addr;
71     uint32_t value;
72
73     if (port < 0x10000) {
74         value = inl(port);
75     } else {
76         value = readl(port);
77     }
78
79     return value;
80 }
81
82 static void qpci_pc_io_writeb(QPCIBus *bus, void *addr, uint8_t value)
83 {
84     uintptr_t port = (uintptr_t)addr;
85
86     if (port < 0x10000) {
87         outb(port, value);
88     } else {
89         writeb(port, value);
90     }
91 }
92
93 static void qpci_pc_io_writew(QPCIBus *bus, void *addr, uint16_t value)
94 {
95     uintptr_t port = (uintptr_t)addr;
96
97     if (port < 0x10000) {
98         outw(port, value);
99     } else {
100         writew(port, value);
101     }
102 }
103
104 static void qpci_pc_io_writel(QPCIBus *bus, void *addr, uint32_t value)
105 {
106     uintptr_t port = (uintptr_t)addr;
107
108     if (port < 0x10000) {
109         outl(port, value);
110     } else {
111         writel(port, value);
112     }
113 }
114
115 static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
116 {
117     outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
118     return inb(0xcfc);
119 }
120
121 static uint16_t qpci_pc_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
122 {
123     outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
124     return inw(0xcfc);
125 }
126
127 static uint32_t qpci_pc_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
128 {
129     outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
130     return inl(0xcfc);
131 }
132
133 static void qpci_pc_config_writeb(QPCIBus *bus, int devfn, uint8_t offset, uint8_t value)
134 {
135     outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
136     outb(0xcfc, value);
137 }
138
139 static void qpci_pc_config_writew(QPCIBus *bus, int devfn, uint8_t offset, uint16_t value)
140 {
141     outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
142     outw(0xcfc, value);
143 }
144
145 static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint32_t value)
146 {
147     outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
148     outl(0xcfc, value);
149 }
150
151 static void *qpci_pc_iomap(QPCIBus *bus, QPCIDevice *dev, int barno, uint64_t *sizeptr)
152 {
153     QPCIBusPC *s = container_of(bus, QPCIBusPC, bus);
154     static const int bar_reg_map[] = {
155         PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_2,
156         PCI_BASE_ADDRESS_3, PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5,
157     };
158     int bar_reg;
159     uint32_t addr;
160     uint64_t size;
161     uint32_t io_type;
162
163     g_assert(barno >= 0 && barno <= 5);
164     bar_reg = bar_reg_map[barno];
165
166     qpci_config_writel(dev, bar_reg, 0xFFFFFFFF);
167     addr = qpci_config_readl(dev, bar_reg);
168
169     io_type = addr & PCI_BASE_ADDRESS_SPACE;
170     if (io_type == PCI_BASE_ADDRESS_SPACE_IO) {
171         addr &= PCI_BASE_ADDRESS_IO_MASK;
172     } else {
173         addr &= PCI_BASE_ADDRESS_MEM_MASK;
174     }
175
176     size = (1ULL << ctzl(addr));
177     if (size == 0) {
178         return NULL;
179     }
180     if (sizeptr) {
181         *sizeptr = size;
182     }
183
184     if (io_type == PCI_BASE_ADDRESS_SPACE_IO) {
185         uint16_t loc;
186
187         g_assert((s->pci_iohole_alloc + size) <= s->pci_iohole_size);
188         loc = s->pci_iohole_start + s->pci_iohole_alloc;
189         s->pci_iohole_alloc += size;
190
191         qpci_config_writel(dev, bar_reg, loc | PCI_BASE_ADDRESS_SPACE_IO);
192
193         return (void *)(intptr_t)loc;
194     } else {
195         uint64_t loc;
196
197         g_assert((s->pci_hole_alloc + size) <= s->pci_hole_size);
198         loc = s->pci_hole_start + s->pci_hole_alloc;
199         s->pci_hole_alloc += size;
200
201         qpci_config_writel(dev, bar_reg, loc);
202
203         return (void *)(intptr_t)loc;
204     }
205 }
206
207 static void qpci_pc_iounmap(QPCIBus *bus, void *data)
208 {
209     /* FIXME */
210 }
211
212 QPCIBus *qpci_init_pc(void)
213 {
214     QPCIBusPC *ret;
215
216     ret = g_malloc(sizeof(*ret));
217
218     ret->bus.io_readb = qpci_pc_io_readb;
219     ret->bus.io_readw = qpci_pc_io_readw;
220     ret->bus.io_readl = qpci_pc_io_readl;
221
222     ret->bus.io_writeb = qpci_pc_io_writeb;
223     ret->bus.io_writew = qpci_pc_io_writew;
224     ret->bus.io_writel = qpci_pc_io_writel;
225
226     ret->bus.config_readb = qpci_pc_config_readb;
227     ret->bus.config_readw = qpci_pc_config_readw;
228     ret->bus.config_readl = qpci_pc_config_readl;
229
230     ret->bus.config_writeb = qpci_pc_config_writeb;
231     ret->bus.config_writew = qpci_pc_config_writew;
232     ret->bus.config_writel = qpci_pc_config_writel;
233
234     ret->bus.iomap = qpci_pc_iomap;
235     ret->bus.iounmap = qpci_pc_iounmap;
236
237     ret->pci_hole_start = 0xE0000000;
238     ret->pci_hole_size = 0x20000000;
239     ret->pci_hole_alloc = 0;
240
241     ret->pci_iohole_start = 0xc000;
242     ret->pci_iohole_size = 0x4000;
243     ret->pci_iohole_alloc = 0;
244
245     return &ret->bus;
246 }
247
248 void qpci_free_pc(QPCIBus *bus)
249 {
250     QPCIBusPC *s = container_of(bus, QPCIBusPC, bus);
251
252     g_free(s);
253 }
254
255 void qpci_plug_device_test(const char *driver, const char *id,
256                            uint8_t slot, const char *opts)
257 {
258     QDict *response;
259     char *cmd;
260
261     cmd = g_strdup_printf("{'execute': 'device_add',"
262                           " 'arguments': {"
263                           "   'driver': '%s',"
264                           "   'addr': '%d',"
265                           "   %s%s"
266                           "   'id': '%s'"
267                           "}}", driver, slot,
268                           opts ? opts : "", opts ? "," : "",
269                           id);
270     response = qmp(cmd);
271     g_free(cmd);
272     g_assert(response);
273     g_assert(!qdict_haskey(response, "error"));
274     QDECREF(response);
275 }
276
277 void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
278 {
279     QDict *response;
280     char *cmd;
281
282     cmd = g_strdup_printf("{'execute': 'device_del',"
283                           " 'arguments': {"
284                           "   'id': '%s'"
285                           "}}", id);
286     response = qmp(cmd);
287     g_free(cmd);
288     g_assert(response);
289     g_assert(!qdict_haskey(response, "error"));
290     QDECREF(response);
291
292     outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
293
294     response = qmp("");
295     g_assert(response);
296     g_assert(qdict_haskey(response, "event"));
297     g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
298     QDECREF(response);
299 }
This page took 0.039872 seconds and 4 git commands to generate.