4 * Copyright IBM, Corp. 2012-2013
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),
25 for (slot = 0; slot < 32; slot++) {
28 for (fn = 0; fn < 8; fn++) {
31 dev = qpci_device_find(bus, QPCI_DEVFN(slot, fn));
36 if (vendor_id != -1 &&
37 qpci_config_readw(dev, PCI_VENDOR_ID) != vendor_id) {
42 if (device_id != -1 &&
43 qpci_config_readw(dev, PCI_DEVICE_ID) != device_id) {
48 func(dev, QPCI_DEVFN(slot, fn), data);
53 QPCIDevice *qpci_device_find(QPCIBus *bus, int devfn)
57 dev = g_malloc0(sizeof(*dev));
61 if (qpci_config_readw(dev, PCI_VENDOR_ID) == 0xFFFF) {
69 void qpci_device_enable(QPCIDevice *dev)
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)
88 uint8_t addr = qpci_config_readb(dev, PCI_CAPABILITY_LIST);
91 cap = qpci_config_readb(dev, addr);
93 addr = qpci_config_readb(dev, addr + PCI_CAP_LIST_NEXT);
95 } while (cap != id && addr != 0);
100 void qpci_msix_enable(QPCIDevice *dev)
109 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
110 g_assert_cmphex(addr, !=, 0);
112 val = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
113 qpci_config_writew(dev, addr + PCI_MSIX_FLAGS, val | PCI_MSIX_FLAGS_ENABLE);
115 table = qpci_config_readl(dev, addr + PCI_MSIX_TABLE);
116 bir_table = table & PCI_MSIX_FLAGS_BIRMASK;
117 offset = qpci_iomap(dev, bir_table, NULL);
118 dev->msix_table = offset + (table & ~PCI_MSIX_FLAGS_BIRMASK);
120 table = qpci_config_readl(dev, addr + PCI_MSIX_PBA);
121 bir_pba = table & PCI_MSIX_FLAGS_BIRMASK;
122 if (bir_pba != bir_table) {
123 offset = qpci_iomap(dev, bir_pba, NULL);
125 dev->msix_pba = offset + (table & ~PCI_MSIX_FLAGS_BIRMASK);
127 g_assert(dev->msix_table != NULL);
128 g_assert(dev->msix_pba != NULL);
129 dev->msix_enabled = true;
132 void qpci_msix_disable(QPCIDevice *dev)
137 g_assert(dev->msix_enabled);
138 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
139 g_assert_cmphex(addr, !=, 0);
140 val = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
141 qpci_config_writew(dev, addr + PCI_MSIX_FLAGS,
142 val & ~PCI_MSIX_FLAGS_ENABLE);
144 qpci_iounmap(dev, dev->msix_table);
145 qpci_iounmap(dev, dev->msix_pba);
146 dev->msix_enabled = 0;
147 dev->msix_table = NULL;
148 dev->msix_pba = NULL;
151 bool qpci_msix_pending(QPCIDevice *dev, uint16_t entry)
154 uint8_t bit_n = entry % 32;
155 void *addr = dev->msix_pba + (entry / 32) * PCI_MSIX_ENTRY_SIZE / 4;
157 g_assert(dev->msix_enabled);
158 pba_entry = qpci_io_readl(dev, addr);
159 qpci_io_writel(dev, addr, pba_entry & ~(1 << bit_n));
160 return (pba_entry & (1 << bit_n)) != 0;
163 bool qpci_msix_masked(QPCIDevice *dev, uint16_t entry)
167 void *vector_addr = dev->msix_table + (entry * PCI_MSIX_ENTRY_SIZE);
169 g_assert(dev->msix_enabled);
170 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
171 g_assert_cmphex(addr, !=, 0);
172 val = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
174 if (val & PCI_MSIX_FLAGS_MASKALL) {
177 return (qpci_io_readl(dev, vector_addr + PCI_MSIX_ENTRY_VECTOR_CTRL)
178 & PCI_MSIX_ENTRY_CTRL_MASKBIT) != 0;
182 uint16_t qpci_msix_table_size(QPCIDevice *dev)
187 addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
188 g_assert_cmphex(addr, !=, 0);
190 control = qpci_config_readw(dev, addr + PCI_MSIX_FLAGS);
191 return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
194 uint8_t qpci_config_readb(QPCIDevice *dev, uint8_t offset)
196 return dev->bus->config_readb(dev->bus, dev->devfn, offset);
199 uint16_t qpci_config_readw(QPCIDevice *dev, uint8_t offset)
201 return dev->bus->config_readw(dev->bus, dev->devfn, offset);
204 uint32_t qpci_config_readl(QPCIDevice *dev, uint8_t offset)
206 return dev->bus->config_readl(dev->bus, dev->devfn, offset);
210 void qpci_config_writeb(QPCIDevice *dev, uint8_t offset, uint8_t value)
212 dev->bus->config_writeb(dev->bus, dev->devfn, offset, value);
215 void qpci_config_writew(QPCIDevice *dev, uint8_t offset, uint16_t value)
217 dev->bus->config_writew(dev->bus, dev->devfn, offset, value);
220 void qpci_config_writel(QPCIDevice *dev, uint8_t offset, uint32_t value)
222 dev->bus->config_writel(dev->bus, dev->devfn, offset, value);
226 uint8_t qpci_io_readb(QPCIDevice *dev, void *data)
228 uintptr_t addr = (uintptr_t)data;
230 if (addr < QPCI_PIO_LIMIT) {
231 return dev->bus->pio_readb(dev->bus, addr);
234 dev->bus->memread(dev->bus, addr, &val, sizeof(val));
239 uint16_t qpci_io_readw(QPCIDevice *dev, void *data)
241 uintptr_t addr = (uintptr_t)data;
243 if (addr < QPCI_PIO_LIMIT) {
244 return dev->bus->pio_readw(dev->bus, addr);
247 dev->bus->memread(dev->bus, addr, &val, sizeof(val));
248 return le16_to_cpu(val);
252 uint32_t qpci_io_readl(QPCIDevice *dev, void *data)
254 uintptr_t addr = (uintptr_t)data;
256 if (addr < QPCI_PIO_LIMIT) {
257 return dev->bus->pio_readl(dev->bus, addr);
260 dev->bus->memread(dev->bus, addr, &val, sizeof(val));
261 return le32_to_cpu(val);
265 void qpci_io_writeb(QPCIDevice *dev, void *data, uint8_t value)
267 uintptr_t addr = (uintptr_t)data;
269 if (addr < QPCI_PIO_LIMIT) {
270 dev->bus->pio_writeb(dev->bus, addr, value);
272 dev->bus->memwrite(dev->bus, addr, &value, sizeof(value));
276 void qpci_io_writew(QPCIDevice *dev, void *data, uint16_t value)
278 uintptr_t addr = (uintptr_t)data;
280 if (addr < QPCI_PIO_LIMIT) {
281 dev->bus->pio_writew(dev->bus, addr, value);
283 value = cpu_to_le16(value);
284 dev->bus->memwrite(dev->bus, addr, &value, sizeof(value));
288 void qpci_io_writel(QPCIDevice *dev, void *data, uint32_t value)
290 uintptr_t addr = (uintptr_t)data;
292 if (addr < QPCI_PIO_LIMIT) {
293 dev->bus->pio_writel(dev->bus, addr, value);
295 value = cpu_to_le32(value);
296 dev->bus->memwrite(dev->bus, addr, &value, sizeof(value));
300 void qpci_memread(QPCIDevice *dev, void *data, void *buf, size_t len)
302 uintptr_t addr = (uintptr_t)data;
304 g_assert(addr >= QPCI_PIO_LIMIT);
305 dev->bus->memread(dev->bus, addr, buf, len);
308 void qpci_memwrite(QPCIDevice *dev, void *data, const void *buf, size_t len)
310 uintptr_t addr = (uintptr_t)data;
312 g_assert(addr >= QPCI_PIO_LIMIT);
313 dev->bus->memwrite(dev->bus, addr, buf, len);
316 void *qpci_iomap(QPCIDevice *dev, int barno, uint64_t *sizeptr)
318 QPCIBus *bus = dev->bus;
319 static const int bar_reg_map[] = {
320 PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_2,
321 PCI_BASE_ADDRESS_3, PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5,
328 g_assert(barno >= 0 && barno <= 5);
329 bar_reg = bar_reg_map[barno];
331 qpci_config_writel(dev, bar_reg, 0xFFFFFFFF);
332 addr = qpci_config_readl(dev, bar_reg);
334 io_type = addr & PCI_BASE_ADDRESS_SPACE;
335 if (io_type == PCI_BASE_ADDRESS_SPACE_IO) {
336 addr &= PCI_BASE_ADDRESS_IO_MASK;
338 addr &= PCI_BASE_ADDRESS_MEM_MASK;
341 g_assert(addr); /* Must have *some* size bits */
343 size = 1U << ctz32(addr);
348 if (io_type == PCI_BASE_ADDRESS_SPACE_IO) {
349 loc = QEMU_ALIGN_UP(bus->pio_alloc_ptr, size);
351 g_assert(loc >= bus->pio_alloc_ptr);
352 g_assert(loc + size <= QPCI_PIO_LIMIT); /* Keep PIO below 64kiB */
354 bus->pio_alloc_ptr = loc + size;
356 qpci_config_writel(dev, bar_reg, loc | PCI_BASE_ADDRESS_SPACE_IO);
358 loc = QEMU_ALIGN_UP(bus->mmio_alloc_ptr, size);
360 /* Check for space */
361 g_assert(loc >= bus->mmio_alloc_ptr);
362 g_assert(loc + size <= bus->mmio_limit);
364 bus->mmio_alloc_ptr = loc + size;
366 qpci_config_writel(dev, bar_reg, loc);
369 return (void *)(uintptr_t)loc;
372 void qpci_iounmap(QPCIDevice *dev, void *data)
377 void *qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr)
379 return (void *)(uintptr_t)addr;
382 void qpci_plug_device_test(const char *driver, const char *id,
383 uint8_t slot, const char *opts)
388 cmd = g_strdup_printf("{'execute': 'device_add',"
395 opts ? opts : "", opts ? "," : "",
400 g_assert(!qdict_haskey(response, "error"));