1c4efe1caSAnthony Liguori /* 2c4efe1caSAnthony Liguori * libqos PCI bindings 3c4efe1caSAnthony Liguori * 4c4efe1caSAnthony Liguori * Copyright IBM, Corp. 2012-2013 5c4efe1caSAnthony Liguori * 6c4efe1caSAnthony Liguori * Authors: 7c4efe1caSAnthony Liguori * Anthony Liguori <aliguori@us.ibm.com> 8c4efe1caSAnthony Liguori * 9c4efe1caSAnthony Liguori * This work is licensed under the terms of the GNU GPL, version 2 or later. 10c4efe1caSAnthony Liguori * See the COPYING file in the top-level directory. 11c4efe1caSAnthony Liguori */ 12c4efe1caSAnthony Liguori 13c4efe1caSAnthony Liguori #ifndef LIBQOS_PCI_H 14c4efe1caSAnthony Liguori #define LIBQOS_PCI_H 15c4efe1caSAnthony Liguori 16907b5105SMarc-André Lureau #include "../libqtest.h" 17a2ce7dbdSPaolo Bonzini #include "qgraph.h" 18c4efe1caSAnthony Liguori 19c4efe1caSAnthony Liguori #define QPCI_DEVFN(dev, fn) (((dev) << 3) | (fn)) 20c4efe1caSAnthony Liguori 21c4efe1caSAnthony Liguori typedef struct QPCIDevice QPCIDevice; 22c4efe1caSAnthony Liguori typedef struct QPCIBus QPCIBus; 23b4ba67d9SDavid Gibson typedef struct QPCIBar QPCIBar; 2485af0057SEmanuele Giuseppe Esposito typedef struct QPCIAddress QPCIAddress; 25c4efe1caSAnthony Liguori 26b8cc4d02SDavid Gibson struct QPCIBus { 27a795fc08SDavid Gibson uint8_t (*pio_readb)(QPCIBus *bus, uint32_t addr); 28a795fc08SDavid Gibson uint16_t (*pio_readw)(QPCIBus *bus, uint32_t addr); 29a795fc08SDavid Gibson uint32_t (*pio_readl)(QPCIBus *bus, uint32_t addr); 30f775f45aSDavid Gibson uint64_t (*pio_readq)(QPCIBus *bus, uint32_t addr); 31c4efe1caSAnthony Liguori 32a795fc08SDavid Gibson void (*pio_writeb)(QPCIBus *bus, uint32_t addr, uint8_t value); 33a795fc08SDavid Gibson void (*pio_writew)(QPCIBus *bus, uint32_t addr, uint16_t value); 34a795fc08SDavid Gibson void (*pio_writel)(QPCIBus *bus, uint32_t addr, uint32_t value); 35f775f45aSDavid Gibson void (*pio_writeq)(QPCIBus *bus, uint32_t addr, uint64_t value); 36a795fc08SDavid Gibson 379a84f889SDavid Gibson void (*memread)(QPCIBus *bus, uint32_t addr, void *buf, size_t len); 389a84f889SDavid Gibson void (*memwrite)(QPCIBus *bus, uint32_t addr, const void *buf, size_t len); 399a84f889SDavid Gibson 40c4efe1caSAnthony Liguori uint8_t (*config_readb)(QPCIBus *bus, int devfn, uint8_t offset); 41c4efe1caSAnthony Liguori uint16_t (*config_readw)(QPCIBus *bus, int devfn, uint8_t offset); 42c4efe1caSAnthony Liguori uint32_t (*config_readl)(QPCIBus *bus, int devfn, uint8_t offset); 43c4efe1caSAnthony Liguori 44c4efe1caSAnthony Liguori void (*config_writeb)(QPCIBus *bus, int devfn, 45c4efe1caSAnthony Liguori uint8_t offset, uint8_t value); 46c4efe1caSAnthony Liguori void (*config_writew)(QPCIBus *bus, int devfn, 47c4efe1caSAnthony Liguori uint8_t offset, uint16_t value); 48c4efe1caSAnthony Liguori void (*config_writel)(QPCIBus *bus, int devfn, 49c4efe1caSAnthony Liguori uint8_t offset, uint32_t value); 50c4efe1caSAnthony Liguori 51e5d1730dSEric Blake QTestState *qts; 523df72d1cSEric Auger uint64_t pio_alloc_ptr, pio_limit; 53b8cc4d02SDavid Gibson uint64_t mmio_alloc_ptr, mmio_limit; 5492bbafc7SEmanuele Giuseppe Esposito bool has_buggy_msi; /* TRUE for spapr, FALSE for pci */ 55*02ee7a8aSEric Auger bool not_hotpluggable; /* TRUE if devices cannot be hotplugged */ 5685af0057SEmanuele Giuseppe Esposito 57c4efe1caSAnthony Liguori }; 58c4efe1caSAnthony Liguori 59b4ba67d9SDavid Gibson struct QPCIBar { 60b4ba67d9SDavid Gibson uint64_t addr; 613df72d1cSEric Auger bool is_io; 62b4ba67d9SDavid Gibson }; 63b4ba67d9SDavid Gibson 64c4efe1caSAnthony Liguori struct QPCIDevice 65c4efe1caSAnthony Liguori { 66c4efe1caSAnthony Liguori QPCIBus *bus; 67c4efe1caSAnthony Liguori int devfn; 6858368113SMarc Marí bool msix_enabled; 69b4ba67d9SDavid Gibson QPCIBar msix_table_bar, msix_pba_bar; 70b4ba67d9SDavid Gibson uint64_t msix_table_off, msix_pba_off; 71c4efe1caSAnthony Liguori }; 72c4efe1caSAnthony Liguori 7385af0057SEmanuele Giuseppe Esposito struct QPCIAddress { 7485af0057SEmanuele Giuseppe Esposito uint32_t devfn; 7585af0057SEmanuele Giuseppe Esposito uint16_t vendor_id; 7685af0057SEmanuele Giuseppe Esposito uint16_t device_id; 7785af0057SEmanuele Giuseppe Esposito }; 7885af0057SEmanuele Giuseppe Esposito 79c4efe1caSAnthony Liguori void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id, 80c4efe1caSAnthony Liguori void (*func)(QPCIDevice *dev, int devfn, void *data), 81c4efe1caSAnthony Liguori void *data); 82c4efe1caSAnthony Liguori QPCIDevice *qpci_device_find(QPCIBus *bus, int devfn); 8385af0057SEmanuele Giuseppe Esposito void qpci_device_init(QPCIDevice *dev, QPCIBus *bus, QPCIAddress *addr); 84efe84f03SLaurent Vivier int qpci_secondary_buses_init(QPCIBus *bus); 85c4efe1caSAnthony Liguori 8692bbafc7SEmanuele Giuseppe Esposito bool qpci_has_buggy_msi(QPCIDevice *dev); 8792bbafc7SEmanuele Giuseppe Esposito bool qpci_check_buggy_msi(QPCIDevice *dev); 8892bbafc7SEmanuele Giuseppe Esposito 89c4efe1caSAnthony Liguori void qpci_device_enable(QPCIDevice *dev); 90a32a028aSStefan Hajnoczi uint8_t qpci_find_capability(QPCIDevice *dev, uint8_t id, uint8_t start_addr); 9158368113SMarc Marí void qpci_msix_enable(QPCIDevice *dev); 9258368113SMarc Marí void qpci_msix_disable(QPCIDevice *dev); 9358368113SMarc Marí bool qpci_msix_pending(QPCIDevice *dev, uint16_t entry); 9458368113SMarc Marí bool qpci_msix_masked(QPCIDevice *dev, uint16_t entry); 9558368113SMarc Marí uint16_t qpci_msix_table_size(QPCIDevice *dev); 96c4efe1caSAnthony Liguori 97c4efe1caSAnthony Liguori uint8_t qpci_config_readb(QPCIDevice *dev, uint8_t offset); 98c4efe1caSAnthony Liguori uint16_t qpci_config_readw(QPCIDevice *dev, uint8_t offset); 99c4efe1caSAnthony Liguori uint32_t qpci_config_readl(QPCIDevice *dev, uint8_t offset); 100c4efe1caSAnthony Liguori 101c4efe1caSAnthony Liguori void qpci_config_writeb(QPCIDevice *dev, uint8_t offset, uint8_t value); 102c4efe1caSAnthony Liguori void qpci_config_writew(QPCIDevice *dev, uint8_t offset, uint16_t value); 103c4efe1caSAnthony Liguori void qpci_config_writel(QPCIDevice *dev, uint8_t offset, uint32_t value); 104c4efe1caSAnthony Liguori 105b4ba67d9SDavid Gibson uint8_t qpci_io_readb(QPCIDevice *dev, QPCIBar token, uint64_t off); 106b4ba67d9SDavid Gibson uint16_t qpci_io_readw(QPCIDevice *dev, QPCIBar token, uint64_t off); 107b4ba67d9SDavid Gibson uint32_t qpci_io_readl(QPCIDevice *dev, QPCIBar token, uint64_t off); 108b4ba67d9SDavid Gibson uint64_t qpci_io_readq(QPCIDevice *dev, QPCIBar token, uint64_t off); 109c4efe1caSAnthony Liguori 110b4ba67d9SDavid Gibson void qpci_io_writeb(QPCIDevice *dev, QPCIBar token, uint64_t off, 111b4ba67d9SDavid Gibson uint8_t value); 112b4ba67d9SDavid Gibson void qpci_io_writew(QPCIDevice *dev, QPCIBar token, uint64_t off, 113b4ba67d9SDavid Gibson uint16_t value); 114b4ba67d9SDavid Gibson void qpci_io_writel(QPCIDevice *dev, QPCIBar token, uint64_t off, 115b4ba67d9SDavid Gibson uint32_t value); 116b4ba67d9SDavid Gibson void qpci_io_writeq(QPCIDevice *dev, QPCIBar token, uint64_t off, 117b4ba67d9SDavid Gibson uint64_t value); 118c4efe1caSAnthony Liguori 119b4ba67d9SDavid Gibson void qpci_memread(QPCIDevice *bus, QPCIBar token, uint64_t off, 120b4ba67d9SDavid Gibson void *buf, size_t len); 121b4ba67d9SDavid Gibson void qpci_memwrite(QPCIDevice *bus, QPCIBar token, uint64_t off, 122b4ba67d9SDavid Gibson const void *buf, size_t len); 123b4ba67d9SDavid Gibson QPCIBar qpci_iomap(QPCIDevice *dev, int barno, uint64_t *sizeptr); 124b4ba67d9SDavid Gibson void qpci_iounmap(QPCIDevice *dev, QPCIBar addr); 125b4ba67d9SDavid Gibson QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr); 126c4efe1caSAnthony Liguori 1276ebb8d2aSThomas Huth void qpci_unplug_acpi_device_test(QTestState *qs, const char *id, uint8_t slot); 12885af0057SEmanuele Giuseppe Esposito 12985af0057SEmanuele Giuseppe Esposito void add_qpci_address(QOSGraphEdgeOptions *opts, QPCIAddress *addr); 130c4efe1caSAnthony Liguori #endif 131