migration/vmstate: renames in (load|save)_state
[qemu/kevin.git] / tests / libqos / pci.c
blob2dcdeade2a63e483e18880d6e174954a64d7fb59
1 /*
2 * libqos PCI bindings
4 * Copyright IBM, Corp. 2012-2013
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
13 #include "qemu/osdep.h"
14 #include "libqos/pci.h"
16 #include "hw/pci/pci_regs.h"
17 #include "qemu/host-utils.h"
19 void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
20 void (*func)(QPCIDevice *dev, int devfn, void *data),
21 void *data)
23 int slot;
25 for (slot = 0; slot < 32; slot++) {
26 int fn;
28 for (fn = 0; fn < 8; fn++) {
29 QPCIDevice *dev;
31 dev = qpci_device_find(bus, QPCI_DEVFN(slot, fn));
32 if (!dev) {
33 continue;
36 if (vendor_id != -1 &&
37 qpci_config_readw(dev, PCI_VENDOR_ID) != vendor_id) {
38 g_free(dev);
39 continue;
42 if (device_id != -1 &&
43 qpci_config_readw(dev, PCI_DEVICE_ID) != device_id) {
44 g_free(dev);
45 continue;
48 func(dev, QPCI_DEVFN(slot, fn), data);
53 QPCIDevice *qpci_device_find(QPCIBus *bus, int devfn)
55 QPCIDevice *dev;
57 dev = g_malloc0(sizeof(*dev));
58 dev->bus = bus;
59 dev->devfn = devfn;
61 if (qpci_config_readw(dev, PCI_VENDOR_ID) == 0xFFFF) {
62 g_free(dev);
63 return NULL;
66 return dev;
69 void qpci_device_enable(QPCIDevice *dev)
71 uint16_t cmd;
73 /* FIXME -- does this need to be a bus callout? */
74 cmd = qpci_config_readw(dev, PCI_COMMAND);
75 cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
76 qpci_config_writew(dev, PCI_COMMAND, cmd);
78 /* Verify the bits are now set. */
79 cmd = qpci_config_readw(dev, PCI_COMMAND);
80 g_assert_cmphex(cmd & PCI_COMMAND_IO, ==, PCI_COMMAND_IO);
81 g_assert_cmphex(cmd & PCI_COMMAND_MEMORY, ==, PCI_COMMAND_MEMORY);
82 g_assert_cmphex(cmd & PCI_COMMAND_MASTER, ==, PCI_COMMAND_MASTER);
85 uint8_t qpci_find_capability(QPCIDevice *dev, uint8_t id)
87 uint8_t cap;
88 uint8_t addr = qpci_config_readb(dev, PCI_CAPABILITY_LIST);
90 do {
91 cap = qpci_config_readb(dev, addr);
92 if (cap != id) {
93 addr = qpci_config_readb(dev, addr + PCI_CAP_LIST_NEXT);
95 } while (cap != id && addr != 0);
97 return addr;
100 void qpci_msix_enable(QPCIDevice *dev)
102 uint8_t addr;
103 uint16_t val;
104 uint32_t table;
105 uint8_t bir_table;
106 uint8_t bir_pba;
108 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
109 g_assert_cmphex(addr, !=, 0);
111 val = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
112 qpci_config_writew(dev, addr + PCI_MSIX_FLAGS, val | PCI_MSIX_FLAGS_ENABLE);
114 table = qpci_config_readl(dev, addr + PCI_MSIX_TABLE);
115 bir_table = table & PCI_MSIX_FLAGS_BIRMASK;
116 dev->msix_table_bar = qpci_iomap(dev, bir_table, NULL);
117 dev->msix_table_off = table & ~PCI_MSIX_FLAGS_BIRMASK;
119 table = qpci_config_readl(dev, addr + PCI_MSIX_PBA);
120 bir_pba = table & PCI_MSIX_FLAGS_BIRMASK;
121 if (bir_pba != bir_table) {
122 dev->msix_pba_bar = qpci_iomap(dev, bir_pba, NULL);
124 dev->msix_pba_off = table & ~PCI_MSIX_FLAGS_BIRMASK;
126 dev->msix_enabled = true;
129 void qpci_msix_disable(QPCIDevice *dev)
131 uint8_t addr;
132 uint16_t val;
134 g_assert(dev->msix_enabled);
135 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
136 g_assert_cmphex(addr, !=, 0);
137 val = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
138 qpci_config_writew(dev, addr + PCI_MSIX_FLAGS,
139 val & ~PCI_MSIX_FLAGS_ENABLE);
141 qpci_iounmap(dev, dev->msix_table_bar);
142 qpci_iounmap(dev, dev->msix_pba_bar);
143 dev->msix_enabled = 0;
144 dev->msix_table_off = 0;
145 dev->msix_pba_off = 0;
148 bool qpci_msix_pending(QPCIDevice *dev, uint16_t entry)
150 uint32_t pba_entry;
151 uint8_t bit_n = entry % 32;
152 uint64_t off = (entry / 32) * PCI_MSIX_ENTRY_SIZE / 4;
154 g_assert(dev->msix_enabled);
155 pba_entry = qpci_io_readl(dev, dev->msix_pba_bar, dev->msix_pba_off + off);
156 qpci_io_writel(dev, dev->msix_pba_bar, dev->msix_pba_off + off,
157 pba_entry & ~(1 << bit_n));
158 return (pba_entry & (1 << bit_n)) != 0;
161 bool qpci_msix_masked(QPCIDevice *dev, uint16_t entry)
163 uint8_t addr;
164 uint16_t val;
165 uint64_t vector_off = dev->msix_table_off + entry * PCI_MSIX_ENTRY_SIZE;
167 g_assert(dev->msix_enabled);
168 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
169 g_assert_cmphex(addr, !=, 0);
170 val = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
172 if (val & PCI_MSIX_FLAGS_MASKALL) {
173 return true;
174 } else {
175 return (qpci_io_readl(dev, dev->msix_table_bar,
176 vector_off + PCI_MSIX_ENTRY_VECTOR_CTRL)
177 & PCI_MSIX_ENTRY_CTRL_MASKBIT) != 0;
181 uint16_t qpci_msix_table_size(QPCIDevice *dev)
183 uint8_t addr;
184 uint16_t control;
186 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
187 g_assert_cmphex(addr, !=, 0);
189 control = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
190 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
193 uint8_t qpci_config_readb(QPCIDevice *dev, uint8_t offset)
195 return dev->bus->config_readb(dev->bus, dev->devfn, offset);
198 uint16_t qpci_config_readw(QPCIDevice *dev, uint8_t offset)
200 return dev->bus->config_readw(dev->bus, dev->devfn, offset);
203 uint32_t qpci_config_readl(QPCIDevice *dev, uint8_t offset)
205 return dev->bus->config_readl(dev->bus, dev->devfn, offset);
209 void qpci_config_writeb(QPCIDevice *dev, uint8_t offset, uint8_t value)
211 dev->bus->config_writeb(dev->bus, dev->devfn, offset, value);
214 void qpci_config_writew(QPCIDevice *dev, uint8_t offset, uint16_t value)
216 dev->bus->config_writew(dev->bus, dev->devfn, offset, value);
219 void qpci_config_writel(QPCIDevice *dev, uint8_t offset, uint32_t value)
221 dev->bus->config_writel(dev->bus, dev->devfn, offset, value);
224 uint8_t qpci_io_readb(QPCIDevice *dev, QPCIBar token, uint64_t off)
226 if (token.addr < QPCI_PIO_LIMIT) {
227 return dev->bus->pio_readb(dev->bus, token.addr + off);
228 } else {
229 uint8_t val;
230 dev->bus->memread(dev->bus, token.addr + off, &val, sizeof(val));
231 return val;
235 uint16_t qpci_io_readw(QPCIDevice *dev, QPCIBar token, uint64_t off)
237 if (token.addr < QPCI_PIO_LIMIT) {
238 return dev->bus->pio_readw(dev->bus, token.addr + off);
239 } else {
240 uint16_t val;
241 dev->bus->memread(dev->bus, token.addr + off, &val, sizeof(val));
242 return le16_to_cpu(val);
246 uint32_t qpci_io_readl(QPCIDevice *dev, QPCIBar token, uint64_t off)
248 if (token.addr < QPCI_PIO_LIMIT) {
249 return dev->bus->pio_readl(dev->bus, token.addr + off);
250 } else {
251 uint32_t val;
252 dev->bus->memread(dev->bus, token.addr + off, &val, sizeof(val));
253 return le32_to_cpu(val);
257 uint64_t qpci_io_readq(QPCIDevice *dev, QPCIBar token, uint64_t off)
259 if (token.addr < QPCI_PIO_LIMIT) {
260 return dev->bus->pio_readq(dev->bus, token.addr + off);
261 } else {
262 uint64_t val;
263 dev->bus->memread(dev->bus, token.addr + off, &val, sizeof(val));
264 return le64_to_cpu(val);
268 void qpci_io_writeb(QPCIDevice *dev, QPCIBar token, uint64_t off,
269 uint8_t value)
271 if (token.addr < QPCI_PIO_LIMIT) {
272 dev->bus->pio_writeb(dev->bus, token.addr + off, value);
273 } else {
274 dev->bus->memwrite(dev->bus, token.addr + off, &value, sizeof(value));
278 void qpci_io_writew(QPCIDevice *dev, QPCIBar token, uint64_t off,
279 uint16_t value)
281 if (token.addr < QPCI_PIO_LIMIT) {
282 dev->bus->pio_writew(dev->bus, token.addr + off, value);
283 } else {
284 value = cpu_to_le16(value);
285 dev->bus->memwrite(dev->bus, token.addr + off, &value, sizeof(value));
289 void qpci_io_writel(QPCIDevice *dev, QPCIBar token, uint64_t off,
290 uint32_t value)
292 if (token.addr < QPCI_PIO_LIMIT) {
293 dev->bus->pio_writel(dev->bus, token.addr + off, value);
294 } else {
295 value = cpu_to_le32(value);
296 dev->bus->memwrite(dev->bus, token.addr + off, &value, sizeof(value));
300 void qpci_io_writeq(QPCIDevice *dev, QPCIBar token, uint64_t off,
301 uint64_t value)
303 if (token.addr < QPCI_PIO_LIMIT) {
304 dev->bus->pio_writeq(dev->bus, token.addr + off, value);
305 } else {
306 value = cpu_to_le64(value);
307 dev->bus->memwrite(dev->bus, token.addr + off, &value, sizeof(value));
311 void qpci_memread(QPCIDevice *dev, QPCIBar token, uint64_t off,
312 void *buf, size_t len)
314 g_assert(token.addr >= QPCI_PIO_LIMIT);
315 dev->bus->memread(dev->bus, token.addr + off, buf, len);
318 void qpci_memwrite(QPCIDevice *dev, QPCIBar token, uint64_t off,
319 const void *buf, size_t len)
321 g_assert(token.addr >= QPCI_PIO_LIMIT);
322 dev->bus->memwrite(dev->bus, token.addr + off, buf, len);
325 QPCIBar qpci_iomap(QPCIDevice *dev, int barno, uint64_t *sizeptr)
327 QPCIBus *bus = dev->bus;
328 static const int bar_reg_map[] = {
329 PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_2,
330 PCI_BASE_ADDRESS_3, PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5,
332 QPCIBar bar;
333 int bar_reg;
334 uint32_t addr, size;
335 uint32_t io_type;
336 uint64_t loc;
338 g_assert(barno >= 0 && barno <= 5);
339 bar_reg = bar_reg_map[barno];
341 qpci_config_writel(dev, bar_reg, 0xFFFFFFFF);
342 addr = qpci_config_readl(dev, bar_reg);
344 io_type = addr & PCI_BASE_ADDRESS_SPACE;
345 if (io_type == PCI_BASE_ADDRESS_SPACE_IO) {
346 addr &= PCI_BASE_ADDRESS_IO_MASK;
347 } else {
348 addr &= PCI_BASE_ADDRESS_MEM_MASK;
351 g_assert(addr); /* Must have *some* size bits */
353 size = 1U << ctz32(addr);
354 if (sizeptr) {
355 *sizeptr = size;
358 if (io_type == PCI_BASE_ADDRESS_SPACE_IO) {
359 loc = QEMU_ALIGN_UP(bus->pio_alloc_ptr, size);
361 g_assert(loc >= bus->pio_alloc_ptr);
362 g_assert(loc + size <= QPCI_PIO_LIMIT); /* Keep PIO below 64kiB */
364 bus->pio_alloc_ptr = loc + size;
366 qpci_config_writel(dev, bar_reg, loc | PCI_BASE_ADDRESS_SPACE_IO);
367 } else {
368 loc = QEMU_ALIGN_UP(bus->mmio_alloc_ptr, size);
370 /* Check for space */
371 g_assert(loc >= bus->mmio_alloc_ptr);
372 g_assert(loc + size <= bus->mmio_limit);
374 bus->mmio_alloc_ptr = loc + size;
376 qpci_config_writel(dev, bar_reg, loc);
379 bar.addr = loc;
380 return bar;
383 void qpci_iounmap(QPCIDevice *dev, QPCIBar bar)
385 /* FIXME */
388 QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr)
390 QPCIBar bar = { .addr = addr };
391 return bar;
394 void qpci_plug_device_test(const char *driver, const char *id,
395 uint8_t slot, const char *opts)
397 QDict *response;
398 char *cmd;
400 cmd = g_strdup_printf("{'execute': 'device_add',"
401 " 'arguments': {"
402 " 'driver': '%s',"
403 " 'addr': '%d',"
404 " %s%s"
405 " 'id': '%s'"
406 "}}", driver, slot,
407 opts ? opts : "", opts ? "," : "",
408 id);
409 response = qmp(cmd);
410 g_free(cmd);
411 g_assert(response);
412 g_assert(!qdict_haskey(response, "error"));
413 QDECREF(response);