4 * Copyright IBM, Corp. 2012-2013
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 "libqos/pci.h"
15 #include "hw/pci/pci_regs.h"
18 void qpci_device_foreach(QPCIBus
*bus
, int vendor_id
, int device_id
,
19 void (*func
)(QPCIDevice
*dev
, int devfn
, void *data
),
24 for (slot
= 0; slot
< 32; slot
++) {
27 for (fn
= 0; fn
< 8; fn
++) {
30 dev
= qpci_device_find(bus
, QPCI_DEVFN(slot
, fn
));
35 if (vendor_id
!= -1 &&
36 qpci_config_readw(dev
, PCI_VENDOR_ID
) != vendor_id
) {
40 if (device_id
!= -1 &&
41 qpci_config_readw(dev
, PCI_DEVICE_ID
) != device_id
) {
45 func(dev
, QPCI_DEVFN(slot
, fn
), data
);
50 QPCIDevice
*qpci_device_find(QPCIBus
*bus
, int devfn
)
54 dev
= g_malloc0(sizeof(*dev
));
58 if (qpci_config_readw(dev
, PCI_VENDOR_ID
) == 0xFFFF) {
66 void qpci_device_enable(QPCIDevice
*dev
)
70 /* FIXME -- does this need to be a bus callout? */
71 cmd
= qpci_config_readw(dev
, PCI_COMMAND
);
72 cmd
|= PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
;
73 qpci_config_writew(dev
, PCI_COMMAND
, cmd
);
75 /* Verify the bits are now set. */
76 cmd
= qpci_config_readw(dev
, PCI_COMMAND
);
77 g_assert_cmphex(cmd
& PCI_COMMAND_IO
, ==, PCI_COMMAND_IO
);
78 g_assert_cmphex(cmd
& PCI_COMMAND_MEMORY
, ==, PCI_COMMAND_MEMORY
);
79 g_assert_cmphex(cmd
& PCI_COMMAND_MASTER
, ==, PCI_COMMAND_MASTER
);
82 uint8_t qpci_find_capability(QPCIDevice
*dev
, uint8_t id
)
85 uint8_t addr
= qpci_config_readb(dev
, PCI_CAPABILITY_LIST
);
88 cap
= qpci_config_readb(dev
, addr
);
90 addr
= qpci_config_readb(dev
, addr
+ PCI_CAP_LIST_NEXT
);
92 } while (cap
!= id
&& addr
!= 0);
97 void qpci_msix_enable(QPCIDevice
*dev
)
106 addr
= qpci_find_capability(dev
, PCI_CAP_ID_MSIX
);
107 g_assert_cmphex(addr
, !=, 0);
109 val
= qpci_config_readw(dev
, addr
+ PCI_MSIX_FLAGS
);
110 qpci_config_writew(dev
, addr
+ PCI_MSIX_FLAGS
, val
| PCI_MSIX_FLAGS_ENABLE
);
112 table
= qpci_config_readl(dev
, addr
+ PCI_MSIX_TABLE
);
113 bir_table
= table
& PCI_MSIX_FLAGS_BIRMASK
;
114 offset
= qpci_iomap(dev
, bir_table
, NULL
);
115 dev
->msix_table
= offset
+ (table
& ~PCI_MSIX_FLAGS_BIRMASK
);
117 table
= qpci_config_readl(dev
, addr
+ PCI_MSIX_PBA
);
118 bir_pba
= table
& PCI_MSIX_FLAGS_BIRMASK
;
119 if (bir_pba
!= bir_table
) {
120 offset
= qpci_iomap(dev
, bir_pba
, NULL
);
122 dev
->msix_pba
= offset
+ (table
& ~PCI_MSIX_FLAGS_BIRMASK
);
124 g_assert(dev
->msix_table
!= NULL
);
125 g_assert(dev
->msix_pba
!= NULL
);
126 dev
->msix_enabled
= true;
129 void qpci_msix_disable(QPCIDevice
*dev
)
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
);
142 qpci_iounmap(dev
, dev
->msix_pba
);
143 dev
->msix_enabled
= 0;
144 dev
->msix_table
= NULL
;
145 dev
->msix_pba
= NULL
;
148 bool qpci_msix_pending(QPCIDevice
*dev
, uint16_t entry
)
151 uint8_t bit_n
= entry
% 32;
152 void *addr
= dev
->msix_pba
+ (entry
/ 32) * PCI_MSIX_ENTRY_SIZE
/ 4;
154 g_assert(dev
->msix_enabled
);
155 pba_entry
= qpci_io_readl(dev
, addr
);
156 qpci_io_writel(dev
, addr
, pba_entry
& ~(1 << bit_n
));
157 return (pba_entry
& (1 << bit_n
)) != 0;
160 bool qpci_msix_masked(QPCIDevice
*dev
, uint16_t entry
)
164 void *vector_addr
= dev
->msix_table
+ (entry
* PCI_MSIX_ENTRY_SIZE
);
166 g_assert(dev
->msix_enabled
);
167 addr
= qpci_find_capability(dev
, PCI_CAP_ID_MSIX
);
168 g_assert_cmphex(addr
, !=, 0);
169 val
= qpci_config_readw(dev
, addr
+ PCI_MSIX_FLAGS
);
171 if (val
& PCI_MSIX_FLAGS_MASKALL
) {
174 return (qpci_io_readl(dev
, vector_addr
+ PCI_MSIX_ENTRY_VECTOR_CTRL
)
175 & PCI_MSIX_ENTRY_CTRL_MASKBIT
) != 0;
179 uint16_t qpci_msix_table_size(QPCIDevice
*dev
)
184 addr
= qpci_find_capability(dev
, PCI_CAP_ID_MSIX
);
185 g_assert_cmphex(addr
, !=, 0);
187 control
= qpci_config_readw(dev
, addr
+ PCI_MSIX_FLAGS
);
188 return (control
& PCI_MSIX_FLAGS_QSIZE
) + 1;
191 uint8_t qpci_config_readb(QPCIDevice
*dev
, uint8_t offset
)
193 return dev
->bus
->config_readb(dev
->bus
, dev
->devfn
, offset
);
196 uint16_t qpci_config_readw(QPCIDevice
*dev
, uint8_t offset
)
198 return dev
->bus
->config_readw(dev
->bus
, dev
->devfn
, offset
);
201 uint32_t qpci_config_readl(QPCIDevice
*dev
, uint8_t offset
)
203 return dev
->bus
->config_readl(dev
->bus
, dev
->devfn
, offset
);
207 void qpci_config_writeb(QPCIDevice
*dev
, uint8_t offset
, uint8_t value
)
209 dev
->bus
->config_writeb(dev
->bus
, dev
->devfn
, offset
, value
);
212 void qpci_config_writew(QPCIDevice
*dev
, uint8_t offset
, uint16_t value
)
214 dev
->bus
->config_writew(dev
->bus
, dev
->devfn
, offset
, value
);
217 void qpci_config_writel(QPCIDevice
*dev
, uint8_t offset
, uint32_t value
)
219 dev
->bus
->config_writel(dev
->bus
, dev
->devfn
, offset
, value
);
223 uint8_t qpci_io_readb(QPCIDevice
*dev
, void *data
)
225 return dev
->bus
->io_readb(dev
->bus
, data
);
228 uint16_t qpci_io_readw(QPCIDevice
*dev
, void *data
)
230 return dev
->bus
->io_readw(dev
->bus
, data
);
233 uint32_t qpci_io_readl(QPCIDevice
*dev
, void *data
)
235 return dev
->bus
->io_readl(dev
->bus
, data
);
239 void qpci_io_writeb(QPCIDevice
*dev
, void *data
, uint8_t value
)
241 dev
->bus
->io_writeb(dev
->bus
, data
, value
);
244 void qpci_io_writew(QPCIDevice
*dev
, void *data
, uint16_t value
)
246 dev
->bus
->io_writew(dev
->bus
, data
, value
);
249 void qpci_io_writel(QPCIDevice
*dev
, void *data
, uint32_t value
)
251 dev
->bus
->io_writel(dev
->bus
, data
, value
);
254 void *qpci_iomap(QPCIDevice
*dev
, int barno
, uint64_t *sizeptr
)
256 return dev
->bus
->iomap(dev
->bus
, dev
, barno
, sizeptr
);
259 void qpci_iounmap(QPCIDevice
*dev
, void *data
)
261 dev
->bus
->iounmap(dev
->bus
, data
);