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.
18 /* Declaration from linux/pci_regs.h */
19 #define PCI_CAP_ID_MSIX 0x11 /* MSI-X */
20 #define PCI_MSIX_FLAGS 2 /* Table at lower 11 bits */
21 #define PCI_MSIX_FLAGS_QSIZE 0x7FF
22 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
23 #define PCI_MSIX_FLAGS_BIRMASK (7 << 0)
25 /* MSI-X capability structure */
26 #define MSIX_TABLE_OFFSET 4
27 #define MSIX_PBA_OFFSET 8
28 #define MSIX_CAP_LENGTH 12
30 /* MSI enable bit is in byte 1 in FLAGS register */
31 #define MSIX_ENABLE_OFFSET (PCI_MSIX_FLAGS + 1)
32 #define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
34 /* MSI-X table format */
35 #define MSIX_MSG_ADDR 0
36 #define MSIX_MSG_UPPER_ADDR 4
37 #define MSIX_MSG_DATA 8
38 #define MSIX_VECTOR_CTRL 12
39 #define MSIX_ENTRY_SIZE 16
40 #define MSIX_VECTOR_MASK 0x1
42 /* How much space does an MSIX table need. */
43 /* The spec requires giving the table structure
44 * a 4K aligned region all by itself. */
45 #define MSIX_PAGE_SIZE 0x1000
46 /* Reserve second half of the page for pending bits */
47 #define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2)
48 #define MSIX_MAX_ENTRIES 32
52 #define DEBUG(fmt, ...) \
54 fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \
57 #define DEBUG(fmt, ...) do { } while(0)
60 /* Flag for interrupt controller to declare MSI-X support */
63 /* Add MSI-X capability to the config space for the device. */
64 /* Given a bar and its size, add MSI-X table on top of it
65 * and fill MSI-X capability in the config space.
66 * Original bar size must be a power of 2 or 0.
67 * New bar size is returned. */
68 static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,
69 unsigned bar_nr, unsigned bar_size)
75 if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1)
77 if (bar_size > 0x80000000)
80 /* Add space for MSI-X structures */
82 new_size = MSIX_PAGE_SIZE;
83 } else if (bar_size < MSIX_PAGE_SIZE) {
84 bar_size = MSIX_PAGE_SIZE;
85 new_size = MSIX_PAGE_SIZE * 2;
87 new_size = bar_size * 2;
90 pdev->msix_bar_size = new_size;
91 config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
92 if (config_offset < 0)
94 config = pdev->config + config_offset;
96 pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
97 /* Table on top of BAR */
98 pci_set_long(config + MSIX_TABLE_OFFSET, bar_size | bar_nr);
99 /* Pending bits on top of that */
100 pci_set_long(config + MSIX_PBA_OFFSET, (bar_size + MSIX_PAGE_PENDING) |
102 pdev->msix_cap = config_offset;
103 /* Make flags bit writeable. */
104 pdev->wmask[config_offset + MSIX_ENABLE_OFFSET] |= MSIX_ENABLE_MASK;
108 static void msix_free_irq_entries(PCIDevice *dev)
112 for (vector = 0; vector < dev->msix_entries_nr; ++vector)
113 dev->msix_entry_used[vector] = 0;
116 /* Handle MSI-X capability config write. */
117 void msix_write_config(PCIDevice *dev, uint32_t addr,
118 uint32_t val, int len)
120 unsigned enable_pos = dev->msix_cap + MSIX_ENABLE_OFFSET;
121 if (addr + len <= enable_pos || addr > enable_pos)
124 if (msix_enabled(dev))
125 qemu_set_irq(dev->irq[0], 0);
128 static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
130 PCIDevice *dev = opaque;
131 unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3;
132 void *page = dev->msix_table_page;
134 return pci_get_long(page + offset);
137 static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
139 fprintf(stderr, "MSI-X: only dword read is allowed!\n");
143 static uint8_t msix_pending_mask(int vector)
145 return 1 << (vector % 8);
148 static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)
150 return dev->msix_table_page + MSIX_PAGE_PENDING + vector / 8;
153 static int msix_is_pending(PCIDevice *dev, int vector)
155 return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
158 static void msix_set_pending(PCIDevice *dev, int vector)
160 *msix_pending_byte(dev, vector) |= msix_pending_mask(vector);
163 static void msix_clr_pending(PCIDevice *dev, int vector)
165 *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector);
168 static int msix_is_masked(PCIDevice *dev, int vector)
170 unsigned offset = vector * MSIX_ENTRY_SIZE + MSIX_VECTOR_CTRL;
171 return dev->msix_table_page[offset] & MSIX_VECTOR_MASK;
174 static void msix_mmio_writel(void *opaque, target_phys_addr_t addr,
177 PCIDevice *dev = opaque;
178 unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3;
179 int vector = offset / MSIX_ENTRY_SIZE;
180 pci_set_long(dev->msix_table_page + offset, val);
181 if (!msix_is_masked(dev, vector) && msix_is_pending(dev, vector)) {
182 msix_clr_pending(dev, vector);
183 msix_notify(dev, vector);
187 static void msix_mmio_write_unallowed(void *opaque, target_phys_addr_t addr,
190 fprintf(stderr, "MSI-X: only dword write is allowed!\n");
193 static CPUWriteMemoryFunc * const msix_mmio_write[] = {
194 msix_mmio_write_unallowed, msix_mmio_write_unallowed, msix_mmio_writel
197 static CPUReadMemoryFunc * const msix_mmio_read[] = {
198 msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
201 /* Should be called from device's map method. */
202 void msix_mmio_map(PCIDevice *d, int region_num,
203 pcibus_t addr, pcibus_t size, int type)
205 uint8_t *config = d->config + d->msix_cap;
206 uint32_t table = pci_get_long(config + MSIX_TABLE_OFFSET);
207 uint32_t offset = table & ~(MSIX_PAGE_SIZE - 1);
208 /* TODO: for assigned devices, we'll want to make it possible to map
209 * pending bits separately in case they are in a separate bar. */
210 int table_bir = table & PCI_MSIX_FLAGS_BIRMASK;
212 if (table_bir != region_num)
216 cpu_register_physical_memory(addr + offset, size - offset,
220 static void msix_mask_all(struct PCIDevice *dev, unsigned nentries)
223 for (vector = 0; vector < nentries; ++vector) {
224 unsigned offset = vector * MSIX_ENTRY_SIZE + MSIX_VECTOR_CTRL;
225 dev->msix_table_page[offset] |= MSIX_VECTOR_MASK;
229 /* Initialize the MSI-X structures. Note: if MSI-X is supported, BAR size is
230 * modified, it should be retrieved with msix_bar_size. */
231 int msix_init(struct PCIDevice *dev, unsigned short nentries,
232 unsigned bar_nr, unsigned bar_size)
235 /* Nothing to do if MSI is not supported by interrupt controller */
239 if (nentries > MSIX_MAX_ENTRIES)
242 dev->msix_entry_used = qemu_mallocz(MSIX_MAX_ENTRIES *
243 sizeof *dev->msix_entry_used);
245 dev->msix_table_page = qemu_mallocz(MSIX_PAGE_SIZE);
246 msix_mask_all(dev, nentries);
248 dev->msix_mmio_index = cpu_register_io_memory(msix_mmio_read,
249 msix_mmio_write, dev);
250 if (dev->msix_mmio_index == -1) {
255 dev->msix_entries_nr = nentries;
256 ret = msix_add_config(dev, nentries, bar_nr, bar_size);
260 dev->cap_present |= QEMU_PCI_CAP_MSIX;
264 dev->msix_entries_nr = 0;
265 cpu_unregister_io_memory(dev->msix_mmio_index);
267 qemu_free(dev->msix_table_page);
268 dev->msix_table_page = NULL;
269 qemu_free(dev->msix_entry_used);
270 dev->msix_entry_used = NULL;
274 /* Clean up resources for the device. */
275 int msix_uninit(PCIDevice *dev)
277 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
279 pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
281 msix_free_irq_entries(dev);
282 dev->msix_entries_nr = 0;
283 cpu_unregister_io_memory(dev->msix_mmio_index);
284 qemu_free(dev->msix_table_page);
285 dev->msix_table_page = NULL;
286 qemu_free(dev->msix_entry_used);
287 dev->msix_entry_used = NULL;
288 dev->cap_present &= ~QEMU_PCI_CAP_MSIX;
292 void msix_save(PCIDevice *dev, QEMUFile *f)
294 unsigned n = dev->msix_entries_nr;
296 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
300 qemu_put_buffer(f, dev->msix_table_page, n * MSIX_ENTRY_SIZE);
301 qemu_put_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8);
304 /* Should be called after restoring the config space. */
305 void msix_load(PCIDevice *dev, QEMUFile *f)
307 unsigned n = dev->msix_entries_nr;
309 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
313 msix_free_irq_entries(dev);
314 qemu_get_buffer(f, dev->msix_table_page, n * MSIX_ENTRY_SIZE);
315 qemu_get_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8);
318 /* Does device support MSI-X? */
319 int msix_present(PCIDevice *dev)
321 return dev->cap_present & QEMU_PCI_CAP_MSIX;
324 /* Is MSI-X enabled? */
325 int msix_enabled(PCIDevice *dev)
327 return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
328 (dev->config[dev->msix_cap + MSIX_ENABLE_OFFSET] &
332 /* Size of bar where MSI-X table resides, or 0 if MSI-X not supported. */
333 uint32_t msix_bar_size(PCIDevice *dev)
335 return (dev->cap_present & QEMU_PCI_CAP_MSIX) ?
336 dev->msix_bar_size : 0;
339 /* Send an MSI-X message */
340 void msix_notify(PCIDevice *dev, unsigned vector)
342 uint8_t *table_entry = dev->msix_table_page + vector * MSIX_ENTRY_SIZE;
346 if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector])
348 if (msix_is_masked(dev, vector)) {
349 msix_set_pending(dev, vector);
353 address = pci_get_long(table_entry + MSIX_MSG_UPPER_ADDR);
354 address = (address << 32) | pci_get_long(table_entry + MSIX_MSG_ADDR);
355 data = pci_get_long(table_entry + MSIX_MSG_DATA);
356 stl_phys(address, data);
359 void msix_reset(PCIDevice *dev)
361 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
363 msix_free_irq_entries(dev);
364 dev->config[dev->msix_cap + MSIX_ENABLE_OFFSET] &= MSIX_ENABLE_MASK;
365 memset(dev->msix_table_page, 0, MSIX_PAGE_SIZE);
366 msix_mask_all(dev, dev->msix_entries_nr);
369 /* PCI spec suggests that devices make it possible for software to configure
370 * less vectors than supported by the device, but does not specify a standard
371 * mechanism for devices to do so.
373 * We support this by asking devices to declare vectors software is going to
374 * actually use, and checking this on the notification path. Devices that
375 * don't want to follow the spec suggestion can declare all vectors as used. */
377 /* Mark vector as used. */
378 int msix_vector_use(PCIDevice *dev, unsigned vector)
380 if (vector >= dev->msix_entries_nr)
382 dev->msix_entry_used[vector]++;
386 /* Mark vector as unused. */
387 void msix_vector_unuse(PCIDevice *dev, unsigned vector)
389 if (vector < dev->msix_entries_nr && dev->msix_entry_used[vector])
390 --dev->msix_entry_used[vector];
393 void msix_unuse_all_vectors(PCIDevice *dev)
395 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
397 msix_free_irq_entries(dev);