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 "qemu/osdep.h"
14 #include "libqos/pci.h"
16 #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
) {
41 if (device_id
!= -1 &&
42 qpci_config_readw(dev
, PCI_DEVICE_ID
) != device_id
) {
47 func(dev
, QPCI_DEVFN(slot
, fn
), data
);
52 QPCIDevice
*qpci_device_find(QPCIBus
*bus
, int devfn
)
56 dev
= g_malloc0(sizeof(*dev
));
60 if (qpci_config_readw(dev
, PCI_VENDOR_ID
) == 0xFFFF) {
68 void qpci_device_enable(QPCIDevice
*dev
)
72 /* FIXME -- does this need to be a bus callout? */
73 cmd
= qpci_config_readw(dev
, PCI_COMMAND
);
74 cmd
|= PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
;
75 qpci_config_writew(dev
, PCI_COMMAND
, cmd
);
77 /* Verify the bits are now set. */
78 cmd
= qpci_config_readw(dev
, PCI_COMMAND
);
79 g_assert_cmphex(cmd
& PCI_COMMAND_IO
, ==, PCI_COMMAND_IO
);
80 g_assert_cmphex(cmd
& PCI_COMMAND_MEMORY
, ==, PCI_COMMAND_MEMORY
);
81 g_assert_cmphex(cmd
& PCI_COMMAND_MASTER
, ==, PCI_COMMAND_MASTER
);
84 uint8_t qpci_find_capability(QPCIDevice
*dev
, uint8_t id
)
87 uint8_t addr
= qpci_config_readb(dev
, PCI_CAPABILITY_LIST
);
90 cap
= qpci_config_readb(dev
, addr
);
92 addr
= qpci_config_readb(dev
, addr
+ PCI_CAP_LIST_NEXT
);
94 } while (cap
!= id
&& addr
!= 0);
99 void qpci_msix_enable(QPCIDevice
*dev
)
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 offset
= qpci_iomap(dev
, bir_table
, NULL
);
117 dev
->msix_table
= offset
+ (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 offset
= qpci_iomap(dev
, bir_pba
, NULL
);
124 dev
->msix_pba
= offset
+ (table
& ~PCI_MSIX_FLAGS_BIRMASK
);
126 g_assert(dev
->msix_table
!= NULL
);
127 g_assert(dev
->msix_pba
!= NULL
);
128 dev
->msix_enabled
= true;
131 void qpci_msix_disable(QPCIDevice
*dev
)
136 g_assert(dev
->msix_enabled
);
137 addr
= qpci_find_capability(dev
, PCI_CAP_ID_MSIX
);
138 g_assert_cmphex(addr
, !=, 0);
139 val
= qpci_config_readw(dev
, addr
+ PCI_MSIX_FLAGS
);
140 qpci_config_writew(dev
, addr
+ PCI_MSIX_FLAGS
,
141 val
& ~PCI_MSIX_FLAGS_ENABLE
);
143 qpci_iounmap(dev
, dev
->msix_table
);
144 qpci_iounmap(dev
, dev
->msix_pba
);
145 dev
->msix_enabled
= 0;
146 dev
->msix_table
= NULL
;
147 dev
->msix_pba
= NULL
;
150 bool qpci_msix_pending(QPCIDevice
*dev
, uint16_t entry
)
153 uint8_t bit_n
= entry
% 32;
154 void *addr
= dev
->msix_pba
+ (entry
/ 32) * PCI_MSIX_ENTRY_SIZE
/ 4;
156 g_assert(dev
->msix_enabled
);
157 pba_entry
= qpci_io_readl(dev
, addr
);
158 qpci_io_writel(dev
, addr
, pba_entry
& ~(1 << bit_n
));
159 return (pba_entry
& (1 << bit_n
)) != 0;
162 bool qpci_msix_masked(QPCIDevice
*dev
, uint16_t entry
)
166 void *vector_addr
= dev
->msix_table
+ (entry
* PCI_MSIX_ENTRY_SIZE
);
168 g_assert(dev
->msix_enabled
);
169 addr
= qpci_find_capability(dev
, PCI_CAP_ID_MSIX
);
170 g_assert_cmphex(addr
, !=, 0);
171 val
= qpci_config_readw(dev
, addr
+ PCI_MSIX_FLAGS
);
173 if (val
& PCI_MSIX_FLAGS_MASKALL
) {
176 return (qpci_io_readl(dev
, vector_addr
+ PCI_MSIX_ENTRY_VECTOR_CTRL
)
177 & PCI_MSIX_ENTRY_CTRL_MASKBIT
) != 0;
181 uint16_t qpci_msix_table_size(QPCIDevice
*dev
)
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
);
225 uint8_t qpci_io_readb(QPCIDevice
*dev
, void *data
)
227 return dev
->bus
->io_readb(dev
->bus
, data
);
230 uint16_t qpci_io_readw(QPCIDevice
*dev
, void *data
)
232 return dev
->bus
->io_readw(dev
->bus
, data
);
235 uint32_t qpci_io_readl(QPCIDevice
*dev
, void *data
)
237 return dev
->bus
->io_readl(dev
->bus
, data
);
241 void qpci_io_writeb(QPCIDevice
*dev
, void *data
, uint8_t value
)
243 dev
->bus
->io_writeb(dev
->bus
, data
, value
);
246 void qpci_io_writew(QPCIDevice
*dev
, void *data
, uint16_t value
)
248 dev
->bus
->io_writew(dev
->bus
, data
, value
);
251 void qpci_io_writel(QPCIDevice
*dev
, void *data
, uint32_t value
)
253 dev
->bus
->io_writel(dev
->bus
, data
, value
);
256 void *qpci_iomap(QPCIDevice
*dev
, int barno
, uint64_t *sizeptr
)
258 return dev
->bus
->iomap(dev
->bus
, dev
, barno
, sizeptr
);
261 void qpci_iounmap(QPCIDevice
*dev
, void *data
)
263 dev
->bus
->iounmap(dev
->bus
, data
);