blob: 3835eaaf28d6a615de9d1bf049fac4fc6158ddc7 [file] [log] [blame]
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +03001/*
2 * MSI-X device support
3 *
4 * This module includes support for MSI-X in pci devices.
5 *
6 * Author: Michael S. Tsirkin <mst@redhat.com>
7 *
8 * Copyright (c) 2009, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 *
13 * Contributions after 2012-01-13 are licensed under the terms of the
14 * GNU GPL, version 2 or (at your option) any later version.
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030015 */
16
17#include "hw.h"
Jan Kiszka60ba3cc2011-10-15 14:33:17 +020018#include "msi.h"
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030019#include "msix.h"
20#include "pci.h"
Blue Swirlbf1b0072010-09-18 05:53:14 +000021#include "range.h"
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030022
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030023#define MSIX_CAP_LENGTH 12
24
Michael S. Tsirkin27609522009-11-25 12:18:00 +020025/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
26#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030027#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +020028#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030029
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +020030/* How much space does an MSIX table need. */
31/* The spec requires giving the table structure
32 * a 4K aligned region all by itself. */
33#define MSIX_PAGE_SIZE 0x1000
34/* Reserve second half of the page for pending bits */
35#define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030036#define MSIX_MAX_ENTRIES 32
37
38
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030039/* Add MSI-X capability to the config space for the device. */
40/* Given a bar and its size, add MSI-X table on top of it
41 * and fill MSI-X capability in the config space.
42 * Original bar size must be a power of 2 or 0.
43 * New bar size is returned. */
44static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,
45 unsigned bar_nr, unsigned bar_size)
46{
47 int config_offset;
48 uint8_t *config;
49 uint32_t new_size;
50
51 if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1)
52 return -EINVAL;
53 if (bar_size > 0x80000000)
54 return -ENOSPC;
55
56 /* Add space for MSI-X structures */
Blue Swirl5e520a72009-09-20 15:35:55 +000057 if (!bar_size) {
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +020058 new_size = MSIX_PAGE_SIZE;
59 } else if (bar_size < MSIX_PAGE_SIZE) {
60 bar_size = MSIX_PAGE_SIZE;
61 new_size = MSIX_PAGE_SIZE * 2;
62 } else {
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030063 new_size = bar_size * 2;
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +020064 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030065
66 pdev->msix_bar_size = new_size;
Isaku Yamahataca770892010-09-06 16:46:16 +090067 config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX,
68 0, MSIX_CAP_LENGTH);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030069 if (config_offset < 0)
70 return config_offset;
71 config = pdev->config + config_offset;
72
73 pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
74 /* Table on top of BAR */
Jan Kiszka01731cf2011-06-09 09:39:56 +020075 pci_set_long(config + PCI_MSIX_TABLE, bar_size | bar_nr);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030076 /* Pending bits on top of that */
Jan Kiszka01731cf2011-06-09 09:39:56 +020077 pci_set_long(config + PCI_MSIX_PBA, (bar_size + MSIX_PAGE_PENDING) |
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +020078 bar_nr);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030079 pdev->msix_cap = config_offset;
Stefan Weilebabb672011-04-26 10:29:36 +020080 /* Make flags bit writable. */
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +020081 pdev->wmask[config_offset + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK |
82 MSIX_MASKALL_MASK;
Michael S. Tsirkin50322242011-11-21 18:57:21 +020083 pdev->msix_function_masked = true;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030084 return 0;
85}
86
Avi Kivity95524ae2011-08-08 16:09:26 +030087static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
88 unsigned size)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030089{
90 PCIDevice *dev = opaque;
Michael S. Tsirkin76f51592009-10-26 16:22:44 +020091 unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030092 void *page = dev->msix_table_page;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030093
Michael S. Tsirkin76f51592009-10-26 16:22:44 +020094 return pci_get_long(page + offset);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030095}
96
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030097static uint8_t msix_pending_mask(int vector)
98{
99 return 1 << (vector % 8);
100}
101
102static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)
103{
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +0200104 return dev->msix_table_page + MSIX_PAGE_PENDING + vector / 8;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300105}
106
107static int msix_is_pending(PCIDevice *dev, int vector)
108{
109 return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
110}
111
112static void msix_set_pending(PCIDevice *dev, int vector)
113{
114 *msix_pending_byte(dev, vector) |= msix_pending_mask(vector);
115}
116
117static void msix_clr_pending(PCIDevice *dev, int vector)
118{
119 *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector);
120}
121
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200122static bool msix_vector_masked(PCIDevice *dev, int vector, bool fmask)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300123{
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200124 unsigned offset = vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
125 return fmask || dev->msix_table_page[offset] & PCI_MSIX_ENTRY_CTRL_MASKBIT;
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200126}
127
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200128static bool msix_is_masked(PCIDevice *dev, int vector)
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200129{
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200130 return msix_vector_masked(dev, vector, dev->msix_function_masked);
131}
132
133static void msix_handle_mask_update(PCIDevice *dev, int vector, bool was_masked)
134{
135 bool is_masked = msix_is_masked(dev, vector);
136 if (is_masked == was_masked) {
137 return;
138 }
139
140 if (!is_masked && msix_is_pending(dev, vector)) {
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200141 msix_clr_pending(dev, vector);
142 msix_notify(dev, vector);
143 }
144}
145
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200146static void msix_update_function_masked(PCIDevice *dev)
147{
148 dev->msix_function_masked = !msix_enabled(dev) ||
149 (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK);
150}
151
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200152/* Handle MSI-X capability config write. */
153void msix_write_config(PCIDevice *dev, uint32_t addr,
154 uint32_t val, int len)
155{
156 unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
157 int vector;
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200158 bool was_masked;
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200159
Isaku Yamahata98a3cb02009-12-15 20:26:04 +0900160 if (!range_covers_byte(addr, len, enable_pos)) {
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200161 return;
162 }
163
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200164 was_masked = dev->msix_function_masked;
165 msix_update_function_masked(dev);
166
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200167 if (!msix_enabled(dev)) {
168 return;
169 }
170
Isaku Yamahatae407bf12011-01-20 16:21:40 +0900171 pci_device_deassert_intx(dev);
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200172
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200173 if (dev->msix_function_masked == was_masked) {
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200174 return;
175 }
176
177 for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200178 msix_handle_mask_update(dev, vector,
179 msix_vector_masked(dev, vector, was_masked));
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200180 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300181}
182
Avi Kivity95524ae2011-08-08 16:09:26 +0300183static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
184 uint64_t val, unsigned size)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300185{
186 PCIDevice *dev = opaque;
Michael S. Tsirkin76f51592009-10-26 16:22:44 +0200187 unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3;
Jan Kiszka01731cf2011-06-09 09:39:56 +0200188 int vector = offset / PCI_MSIX_ENTRY_SIZE;
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200189 bool was_masked;
Michael S. Tsirkin9a93b612011-11-21 18:57:31 +0200190
191 /* MSI-X page includes a read-only PBA and a writeable Vector Control. */
192 if (vector >= dev->msix_entries_nr) {
193 return;
194 }
195
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200196 was_masked = msix_is_masked(dev, vector);
Michael S. Tsirkin76f51592009-10-26 16:22:44 +0200197 pci_set_long(dev->msix_table_page + offset, val);
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200198 msix_handle_mask_update(dev, vector, was_masked);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300199}
200
Avi Kivity95524ae2011-08-08 16:09:26 +0300201static const MemoryRegionOps msix_mmio_ops = {
202 .read = msix_mmio_read,
203 .write = msix_mmio_write,
204 .endianness = DEVICE_NATIVE_ENDIAN,
205 .valid = {
206 .min_access_size = 4,
207 .max_access_size = 4,
208 },
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300209};
210
Avi Kivity95524ae2011-08-08 16:09:26 +0300211static void msix_mmio_setup(PCIDevice *d, MemoryRegion *bar)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300212{
213 uint8_t *config = d->config + d->msix_cap;
Jan Kiszka01731cf2011-06-09 09:39:56 +0200214 uint32_t table = pci_get_long(config + PCI_MSIX_TABLE);
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +0200215 uint32_t offset = table & ~(MSIX_PAGE_SIZE - 1);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300216 /* TODO: for assigned devices, we'll want to make it possible to map
217 * pending bits separately in case they are in a separate bar. */
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300218
Avi Kivity95524ae2011-08-08 16:09:26 +0300219 memory_region_add_subregion(bar, offset, &d->msix_mmio);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300220}
221
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200222static void msix_mask_all(struct PCIDevice *dev, unsigned nentries)
223{
224 int vector;
225 for (vector = 0; vector < nentries; ++vector) {
Jan Kiszka01731cf2011-06-09 09:39:56 +0200226 unsigned offset =
227 vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
228 dev->msix_table_page[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200229 }
230}
231
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300232/* Initialize the MSI-X structures. Note: if MSI-X is supported, BAR size is
233 * modified, it should be retrieved with msix_bar_size. */
234int msix_init(struct PCIDevice *dev, unsigned short nentries,
Avi Kivity95524ae2011-08-08 16:09:26 +0300235 MemoryRegion *bar,
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +0200236 unsigned bar_nr, unsigned bar_size)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300237{
238 int ret;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300239
Jan Kiszka60ba3cc2011-10-15 14:33:17 +0200240 /* Nothing to do if MSI is not supported by interrupt controller */
241 if (!msi_supported) {
242 return -ENOTSUP;
243 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300244 if (nentries > MSIX_MAX_ENTRIES)
245 return -EINVAL;
246
Anthony Liguori7267c092011-08-20 22:09:37 -0500247 dev->msix_entry_used = g_malloc0(MSIX_MAX_ENTRIES *
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300248 sizeof *dev->msix_entry_used);
249
Anthony Liguori7267c092011-08-20 22:09:37 -0500250 dev->msix_table_page = g_malloc0(MSIX_PAGE_SIZE);
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200251 msix_mask_all(dev, nentries);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300252
Avi Kivity95524ae2011-08-08 16:09:26 +0300253 memory_region_init_io(&dev->msix_mmio, &msix_mmio_ops, dev,
254 "msix", MSIX_PAGE_SIZE);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300255
256 dev->msix_entries_nr = nentries;
257 ret = msix_add_config(dev, nentries, bar_nr, bar_size);
258 if (ret)
259 goto err_config;
260
261 dev->cap_present |= QEMU_PCI_CAP_MSIX;
Avi Kivity95524ae2011-08-08 16:09:26 +0300262 msix_mmio_setup(dev, bar);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300263 return 0;
264
265err_config:
Michael S. Tsirkin3174ecd2009-07-22 18:51:14 +0300266 dev->msix_entries_nr = 0;
Avi Kivity95524ae2011-08-08 16:09:26 +0300267 memory_region_destroy(&dev->msix_mmio);
Anthony Liguori7267c092011-08-20 22:09:37 -0500268 g_free(dev->msix_table_page);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300269 dev->msix_table_page = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500270 g_free(dev->msix_entry_used);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300271 dev->msix_entry_used = NULL;
272 return ret;
273}
274
Michael S. Tsirkin98304c82009-11-25 12:24:14 +0200275static void msix_free_irq_entries(PCIDevice *dev)
276{
277 int vector;
278
279 for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
280 dev->msix_entry_used[vector] = 0;
281 msix_clr_pending(dev, vector);
282 }
283}
284
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300285/* Clean up resources for the device. */
Avi Kivity95524ae2011-08-08 16:09:26 +0300286int msix_uninit(PCIDevice *dev, MemoryRegion *bar)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300287{
288 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
289 return 0;
290 pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
291 dev->msix_cap = 0;
292 msix_free_irq_entries(dev);
293 dev->msix_entries_nr = 0;
Avi Kivity95524ae2011-08-08 16:09:26 +0300294 memory_region_del_subregion(bar, &dev->msix_mmio);
295 memory_region_destroy(&dev->msix_mmio);
Anthony Liguori7267c092011-08-20 22:09:37 -0500296 g_free(dev->msix_table_page);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300297 dev->msix_table_page = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500298 g_free(dev->msix_entry_used);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300299 dev->msix_entry_used = NULL;
300 dev->cap_present &= ~QEMU_PCI_CAP_MSIX;
301 return 0;
302}
303
304void msix_save(PCIDevice *dev, QEMUFile *f)
305{
Michael S. Tsirkin9a3e12c2009-07-01 16:28:00 +0300306 unsigned n = dev->msix_entries_nr;
307
Michael S. Tsirkin72755a72009-07-05 15:58:52 +0300308 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
Michael S. Tsirkin9a3e12c2009-07-01 16:28:00 +0300309 return;
Michael S. Tsirkin72755a72009-07-05 15:58:52 +0300310 }
Michael S. Tsirkin9a3e12c2009-07-01 16:28:00 +0300311
Jan Kiszka01731cf2011-06-09 09:39:56 +0200312 qemu_put_buffer(f, dev->msix_table_page, n * PCI_MSIX_ENTRY_SIZE);
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +0200313 qemu_put_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300314}
315
316/* Should be called after restoring the config space. */
317void msix_load(PCIDevice *dev, QEMUFile *f)
318{
319 unsigned n = dev->msix_entries_nr;
320
Blue Swirl98846d72009-07-05 08:11:39 +0000321 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300322 return;
Blue Swirl98846d72009-07-05 08:11:39 +0000323 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300324
Michael S. Tsirkin4bfd1712009-07-05 15:58:44 +0300325 msix_free_irq_entries(dev);
Jan Kiszka01731cf2011-06-09 09:39:56 +0200326 qemu_get_buffer(f, dev->msix_table_page, n * PCI_MSIX_ENTRY_SIZE);
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +0200327 qemu_get_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8);
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200328 msix_update_function_masked(dev);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300329}
330
331/* Does device support MSI-X? */
332int msix_present(PCIDevice *dev)
333{
334 return dev->cap_present & QEMU_PCI_CAP_MSIX;
335}
336
337/* Is MSI-X enabled? */
338int msix_enabled(PCIDevice *dev)
339{
340 return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
Michael S. Tsirkin27609522009-11-25 12:18:00 +0200341 (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300342 MSIX_ENABLE_MASK);
343}
344
345/* Size of bar where MSI-X table resides, or 0 if MSI-X not supported. */
346uint32_t msix_bar_size(PCIDevice *dev)
347{
348 return (dev->cap_present & QEMU_PCI_CAP_MSIX) ?
349 dev->msix_bar_size : 0;
350}
351
352/* Send an MSI-X message */
353void msix_notify(PCIDevice *dev, unsigned vector)
354{
Jan Kiszka01731cf2011-06-09 09:39:56 +0200355 uint8_t *table_entry = dev->msix_table_page + vector * PCI_MSIX_ENTRY_SIZE;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300356 uint64_t address;
357 uint32_t data;
358
359 if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector])
360 return;
361 if (msix_is_masked(dev, vector)) {
362 msix_set_pending(dev, vector);
363 return;
364 }
365
Jan Kiszka01731cf2011-06-09 09:39:56 +0200366 address = pci_get_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR);
367 data = pci_get_long(table_entry + PCI_MSIX_ENTRY_DATA);
Alexander Grafae5d3eb2011-07-05 18:28:06 +0200368 stl_le_phys(address, data);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300369}
370
371void msix_reset(PCIDevice *dev)
372{
373 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
374 return;
375 msix_free_irq_entries(dev);
Michael S. Tsirkin27609522009-11-25 12:18:00 +0200376 dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &=
377 ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET];
Michael S. Tsirkin5a1fc5e2009-09-29 18:53:26 +0200378 memset(dev->msix_table_page, 0, MSIX_PAGE_SIZE);
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200379 msix_mask_all(dev, dev->msix_entries_nr);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300380}
381
382/* PCI spec suggests that devices make it possible for software to configure
383 * less vectors than supported by the device, but does not specify a standard
384 * mechanism for devices to do so.
385 *
386 * We support this by asking devices to declare vectors software is going to
387 * actually use, and checking this on the notification path. Devices that
388 * don't want to follow the spec suggestion can declare all vectors as used. */
389
390/* Mark vector as used. */
391int msix_vector_use(PCIDevice *dev, unsigned vector)
392{
393 if (vector >= dev->msix_entries_nr)
394 return -EINVAL;
395 dev->msix_entry_used[vector]++;
396 return 0;
397}
398
399/* Mark vector as unused. */
400void msix_vector_unuse(PCIDevice *dev, unsigned vector)
401{
Michael S. Tsirkin98304c82009-11-25 12:24:14 +0200402 if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
403 return;
404 }
405 if (--dev->msix_entry_used[vector]) {
406 return;
407 }
408 msix_clr_pending(dev, vector);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300409}
Michael S. Tsirkinb5f28bc2009-11-24 16:44:15 +0200410
411void msix_unuse_all_vectors(PCIDevice *dev)
412{
413 if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
414 return;
415 msix_free_irq_entries(dev);
416}