4 * This module includes support for MSI-X in pci devices.
6 * Author: Michael S. Tsirkin <mst@redhat.com>
8 * Copyright (c) 2009, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
19 #define MSIX_CAP_LENGTH 12
21 /* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
22 #define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1)
23 #define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
24 #define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8)
26 /* How much space does an MSIX table need. */
27 /* The spec requires giving the table structure
28 * a 4K aligned region all by itself. */
29 #define MSIX_PAGE_SIZE 0x1000
30 /* Reserve second half of the page for pending bits */
31 #define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2)
32 #define MSIX_MAX_ENTRIES 32
35 /* Flag for interrupt controller to declare MSI-X support */
38 /* Add MSI-X capability to the config space for the device. */
39 /* Given a bar and its size, add MSI-X table on top of it
40 * and fill MSI-X capability in the config space.
41 * Original bar size must be a power of 2 or 0.
42 * New bar size is returned. */
43 static int msix_add_config(struct PCIDevice
*pdev
, unsigned short nentries
,
44 unsigned bar_nr
, unsigned bar_size
)
50 if (nentries
< 1 || nentries
> PCI_MSIX_FLAGS_QSIZE
+ 1)
52 if (bar_size
> 0x80000000)
55 /* Add space for MSI-X structures */
57 new_size
= MSIX_PAGE_SIZE
;
58 } else if (bar_size
< MSIX_PAGE_SIZE
) {
59 bar_size
= MSIX_PAGE_SIZE
;
60 new_size
= MSIX_PAGE_SIZE
* 2;
62 new_size
= bar_size
* 2;
65 pdev
->msix_bar_size
= new_size
;
66 config_offset
= pci_add_capability(pdev
, PCI_CAP_ID_MSIX
,
68 if (config_offset
< 0)
70 config
= pdev
->config
+ config_offset
;
72 pci_set_word(config
+ PCI_MSIX_FLAGS
, nentries
- 1);
73 /* Table on top of BAR */
74 pci_set_long(config
+ PCI_MSIX_TABLE
, bar_size
| bar_nr
);
75 /* Pending bits on top of that */
76 pci_set_long(config
+ PCI_MSIX_PBA
, (bar_size
+ MSIX_PAGE_PENDING
) |
78 pdev
->msix_cap
= config_offset
;
79 /* Make flags bit writable. */
80 pdev
->wmask
[config_offset
+ MSIX_CONTROL_OFFSET
] |= MSIX_ENABLE_MASK
|
85 static uint64_t msix_mmio_read(void *opaque
, target_phys_addr_t addr
,
88 PCIDevice
*dev
= opaque
;
89 unsigned int offset
= addr
& (MSIX_PAGE_SIZE
- 1) & ~0x3;
90 void *page
= dev
->msix_table_page
;
92 return pci_get_long(page
+ offset
);
95 static uint8_t msix_pending_mask(int vector
)
97 return 1 << (vector
% 8);
100 static uint8_t *msix_pending_byte(PCIDevice
*dev
, int vector
)
102 return dev
->msix_table_page
+ MSIX_PAGE_PENDING
+ vector
/ 8;
105 static int msix_is_pending(PCIDevice
*dev
, int vector
)
107 return *msix_pending_byte(dev
, vector
) & msix_pending_mask(vector
);
110 static void msix_set_pending(PCIDevice
*dev
, int vector
)
112 *msix_pending_byte(dev
, vector
) |= msix_pending_mask(vector
);
115 static void msix_clr_pending(PCIDevice
*dev
, int vector
)
117 *msix_pending_byte(dev
, vector
) &= ~msix_pending_mask(vector
);
120 static int msix_function_masked(PCIDevice
*dev
)
122 return dev
->config
[dev
->msix_cap
+ MSIX_CONTROL_OFFSET
] & MSIX_MASKALL_MASK
;
125 static int msix_is_masked(PCIDevice
*dev
, int vector
)
128 vector
* PCI_MSIX_ENTRY_SIZE
+ PCI_MSIX_ENTRY_VECTOR_CTRL
;
129 return msix_function_masked(dev
) ||
130 dev
->msix_table_page
[offset
] & PCI_MSIX_ENTRY_CTRL_MASKBIT
;
133 static void msix_handle_mask_update(PCIDevice
*dev
, int vector
)
135 if (!msix_is_masked(dev
, vector
) && msix_is_pending(dev
, vector
)) {
136 msix_clr_pending(dev
, vector
);
137 msix_notify(dev
, vector
);
141 /* Handle MSI-X capability config write. */
142 void msix_write_config(PCIDevice
*dev
, uint32_t addr
,
143 uint32_t val
, int len
)
145 unsigned enable_pos
= dev
->msix_cap
+ MSIX_CONTROL_OFFSET
;
148 if (!range_covers_byte(addr
, len
, enable_pos
)) {
152 if (!msix_enabled(dev
)) {
156 pci_device_deassert_intx(dev
);
158 if (msix_function_masked(dev
)) {
162 for (vector
= 0; vector
< dev
->msix_entries_nr
; ++vector
) {
163 msix_handle_mask_update(dev
, vector
);
167 static void msix_mmio_write(void *opaque
, target_phys_addr_t addr
,
168 uint64_t val
, unsigned size
)
170 PCIDevice
*dev
= opaque
;
171 unsigned int offset
= addr
& (MSIX_PAGE_SIZE
- 1) & ~0x3;
172 int vector
= offset
/ PCI_MSIX_ENTRY_SIZE
;
173 pci_set_long(dev
->msix_table_page
+ offset
, val
);
174 msix_handle_mask_update(dev
, vector
);
177 static const MemoryRegionOps msix_mmio_ops
= {
178 .read
= msix_mmio_read
,
179 .write
= msix_mmio_write
,
180 .endianness
= DEVICE_NATIVE_ENDIAN
,
182 .min_access_size
= 4,
183 .max_access_size
= 4,
187 static void msix_mmio_setup(PCIDevice
*d
, MemoryRegion
*bar
)
189 uint8_t *config
= d
->config
+ d
->msix_cap
;
190 uint32_t table
= pci_get_long(config
+ PCI_MSIX_TABLE
);
191 uint32_t offset
= table
& ~(MSIX_PAGE_SIZE
- 1);
192 /* TODO: for assigned devices, we'll want to make it possible to map
193 * pending bits separately in case they are in a separate bar. */
195 memory_region_add_subregion(bar
, offset
, &d
->msix_mmio
);
198 static void msix_mask_all(struct PCIDevice
*dev
, unsigned nentries
)
201 for (vector
= 0; vector
< nentries
; ++vector
) {
203 vector
* PCI_MSIX_ENTRY_SIZE
+ PCI_MSIX_ENTRY_VECTOR_CTRL
;
204 dev
->msix_table_page
[offset
] |= PCI_MSIX_ENTRY_CTRL_MASKBIT
;
208 /* Initialize the MSI-X structures. Note: if MSI-X is supported, BAR size is
209 * modified, it should be retrieved with msix_bar_size. */
210 int msix_init(struct PCIDevice
*dev
, unsigned short nentries
,
212 unsigned bar_nr
, unsigned bar_size
)
215 /* Nothing to do if MSI is not supported by interrupt controller */
219 if (nentries
> MSIX_MAX_ENTRIES
)
222 dev
->msix_entry_used
= g_malloc0(MSIX_MAX_ENTRIES
*
223 sizeof *dev
->msix_entry_used
);
225 dev
->msix_table_page
= g_malloc0(MSIX_PAGE_SIZE
);
226 msix_mask_all(dev
, nentries
);
228 memory_region_init_io(&dev
->msix_mmio
, &msix_mmio_ops
, dev
,
229 "msix", MSIX_PAGE_SIZE
);
231 dev
->msix_entries_nr
= nentries
;
232 ret
= msix_add_config(dev
, nentries
, bar_nr
, bar_size
);
236 dev
->cap_present
|= QEMU_PCI_CAP_MSIX
;
237 msix_mmio_setup(dev
, bar
);
241 dev
->msix_entries_nr
= 0;
242 memory_region_destroy(&dev
->msix_mmio
);
243 g_free(dev
->msix_table_page
);
244 dev
->msix_table_page
= NULL
;
245 g_free(dev
->msix_entry_used
);
246 dev
->msix_entry_used
= NULL
;
250 static void msix_free_irq_entries(PCIDevice
*dev
)
254 for (vector
= 0; vector
< dev
->msix_entries_nr
; ++vector
) {
255 dev
->msix_entry_used
[vector
] = 0;
256 msix_clr_pending(dev
, vector
);
260 /* Clean up resources for the device. */
261 int msix_uninit(PCIDevice
*dev
, MemoryRegion
*bar
)
263 if (!(dev
->cap_present
& QEMU_PCI_CAP_MSIX
))
265 pci_del_capability(dev
, PCI_CAP_ID_MSIX
, MSIX_CAP_LENGTH
);
267 msix_free_irq_entries(dev
);
268 dev
->msix_entries_nr
= 0;
269 memory_region_del_subregion(bar
, &dev
->msix_mmio
);
270 memory_region_destroy(&dev
->msix_mmio
);
271 g_free(dev
->msix_table_page
);
272 dev
->msix_table_page
= NULL
;
273 g_free(dev
->msix_entry_used
);
274 dev
->msix_entry_used
= NULL
;
275 dev
->cap_present
&= ~QEMU_PCI_CAP_MSIX
;
279 void msix_save(PCIDevice
*dev
, QEMUFile
*f
)
281 unsigned n
= dev
->msix_entries_nr
;
283 if (!(dev
->cap_present
& QEMU_PCI_CAP_MSIX
)) {
287 qemu_put_buffer(f
, dev
->msix_table_page
, n
* PCI_MSIX_ENTRY_SIZE
);
288 qemu_put_buffer(f
, dev
->msix_table_page
+ MSIX_PAGE_PENDING
, (n
+ 7) / 8);
291 /* Should be called after restoring the config space. */
292 void msix_load(PCIDevice
*dev
, QEMUFile
*f
)
294 unsigned n
= dev
->msix_entries_nr
;
296 if (!(dev
->cap_present
& QEMU_PCI_CAP_MSIX
)) {
300 msix_free_irq_entries(dev
);
301 qemu_get_buffer(f
, dev
->msix_table_page
, n
* PCI_MSIX_ENTRY_SIZE
);
302 qemu_get_buffer(f
, dev
->msix_table_page
+ MSIX_PAGE_PENDING
, (n
+ 7) / 8);
305 /* Does device support MSI-X? */
306 int msix_present(PCIDevice
*dev
)
308 return dev
->cap_present
& QEMU_PCI_CAP_MSIX
;
311 /* Is MSI-X enabled? */
312 int msix_enabled(PCIDevice
*dev
)
314 return (dev
->cap_present
& QEMU_PCI_CAP_MSIX
) &&
315 (dev
->config
[dev
->msix_cap
+ MSIX_CONTROL_OFFSET
] &
319 /* Size of bar where MSI-X table resides, or 0 if MSI-X not supported. */
320 uint32_t msix_bar_size(PCIDevice
*dev
)
322 return (dev
->cap_present
& QEMU_PCI_CAP_MSIX
) ?
323 dev
->msix_bar_size
: 0;
326 /* Send an MSI-X message */
327 void msix_notify(PCIDevice
*dev
, unsigned vector
)
329 uint8_t *table_entry
= dev
->msix_table_page
+ vector
* PCI_MSIX_ENTRY_SIZE
;
333 if (vector
>= dev
->msix_entries_nr
|| !dev
->msix_entry_used
[vector
])
335 if (msix_is_masked(dev
, vector
)) {
336 msix_set_pending(dev
, vector
);
340 address
= pci_get_quad(table_entry
+ PCI_MSIX_ENTRY_LOWER_ADDR
);
341 data
= pci_get_long(table_entry
+ PCI_MSIX_ENTRY_DATA
);
342 stl_le_phys(address
, data
);
345 void msix_reset(PCIDevice
*dev
)
347 if (!(dev
->cap_present
& QEMU_PCI_CAP_MSIX
))
349 msix_free_irq_entries(dev
);
350 dev
->config
[dev
->msix_cap
+ MSIX_CONTROL_OFFSET
] &=
351 ~dev
->wmask
[dev
->msix_cap
+ MSIX_CONTROL_OFFSET
];
352 memset(dev
->msix_table_page
, 0, MSIX_PAGE_SIZE
);
353 msix_mask_all(dev
, dev
->msix_entries_nr
);
356 /* PCI spec suggests that devices make it possible for software to configure
357 * less vectors than supported by the device, but does not specify a standard
358 * mechanism for devices to do so.
360 * We support this by asking devices to declare vectors software is going to
361 * actually use, and checking this on the notification path. Devices that
362 * don't want to follow the spec suggestion can declare all vectors as used. */
364 /* Mark vector as used. */
365 int msix_vector_use(PCIDevice
*dev
, unsigned vector
)
367 if (vector
>= dev
->msix_entries_nr
)
369 dev
->msix_entry_used
[vector
]++;
373 /* Mark vector as unused. */
374 void msix_vector_unuse(PCIDevice
*dev
, unsigned vector
)
376 if (vector
>= dev
->msix_entries_nr
|| !dev
->msix_entry_used
[vector
]) {
379 if (--dev
->msix_entry_used
[vector
]) {
382 msix_clr_pending(dev
, vector
);
385 void msix_unuse_all_vectors(PCIDevice
*dev
)
387 if (!(dev
->cap_present
& QEMU_PCI_CAP_MSIX
))
389 msix_free_irq_entries(dev
);