Statistics
| Branch: | Revision:

root / hw / msix.c @ 3387bf55

History | View | Annotate | Download (11.1 kB)

1 02eb84d0 Michael S. Tsirkin
/*
2 02eb84d0 Michael S. Tsirkin
 * MSI-X device support
3 02eb84d0 Michael S. Tsirkin
 *
4 02eb84d0 Michael S. Tsirkin
 * This module includes support for MSI-X in pci devices.
5 02eb84d0 Michael S. Tsirkin
 *
6 02eb84d0 Michael S. Tsirkin
 * Author: Michael S. Tsirkin <mst@redhat.com>
7 02eb84d0 Michael S. Tsirkin
 *
8 02eb84d0 Michael S. Tsirkin
 *  Copyright (c) 2009, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
9 02eb84d0 Michael S. Tsirkin
 *
10 02eb84d0 Michael S. Tsirkin
 * This work is licensed under the terms of the GNU GPL, version 2.  See
11 02eb84d0 Michael S. Tsirkin
 * the COPYING file in the top-level directory.
12 02eb84d0 Michael S. Tsirkin
 */
13 02eb84d0 Michael S. Tsirkin
14 02eb84d0 Michael S. Tsirkin
#include "hw.h"
15 02eb84d0 Michael S. Tsirkin
#include "msix.h"
16 02eb84d0 Michael S. Tsirkin
#include "pci.h"
17 bf1b0071 Blue Swirl
#include "range.h"
18 02eb84d0 Michael S. Tsirkin
19 02eb84d0 Michael S. Tsirkin
#define MSIX_CAP_LENGTH 12
20 02eb84d0 Michael S. Tsirkin
21 2760952b Michael S. Tsirkin
/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
22 2760952b Michael S. Tsirkin
#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1)
23 02eb84d0 Michael S. Tsirkin
#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
24 5b5cb086 Michael S. Tsirkin
#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8)
25 02eb84d0 Michael S. Tsirkin
26 5a1fc5e8 Michael S. Tsirkin
/* How much space does an MSIX table need. */
27 5a1fc5e8 Michael S. Tsirkin
/* The spec requires giving the table structure
28 5a1fc5e8 Michael S. Tsirkin
 * a 4K aligned region all by itself. */
29 5a1fc5e8 Michael S. Tsirkin
#define MSIX_PAGE_SIZE 0x1000
30 5a1fc5e8 Michael S. Tsirkin
/* Reserve second half of the page for pending bits */
31 5a1fc5e8 Michael S. Tsirkin
#define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2)
32 02eb84d0 Michael S. Tsirkin
#define MSIX_MAX_ENTRIES 32
33 02eb84d0 Michael S. Tsirkin
34 02eb84d0 Michael S. Tsirkin
35 02eb84d0 Michael S. Tsirkin
/* Flag for interrupt controller to declare MSI-X support */
36 02eb84d0 Michael S. Tsirkin
int msix_supported;
37 02eb84d0 Michael S. Tsirkin
38 02eb84d0 Michael S. Tsirkin
/* Add MSI-X capability to the config space for the device. */
39 02eb84d0 Michael S. Tsirkin
/* Given a bar and its size, add MSI-X table on top of it
40 02eb84d0 Michael S. Tsirkin
 * and fill MSI-X capability in the config space.
41 02eb84d0 Michael S. Tsirkin
 * Original bar size must be a power of 2 or 0.
42 02eb84d0 Michael S. Tsirkin
 * New bar size is returned. */
43 02eb84d0 Michael S. Tsirkin
static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,
44 02eb84d0 Michael S. Tsirkin
                           unsigned bar_nr, unsigned bar_size)
45 02eb84d0 Michael S. Tsirkin
{
46 02eb84d0 Michael S. Tsirkin
    int config_offset;
47 02eb84d0 Michael S. Tsirkin
    uint8_t *config;
48 02eb84d0 Michael S. Tsirkin
    uint32_t new_size;
49 02eb84d0 Michael S. Tsirkin
50 02eb84d0 Michael S. Tsirkin
    if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1)
51 02eb84d0 Michael S. Tsirkin
        return -EINVAL;
52 02eb84d0 Michael S. Tsirkin
    if (bar_size > 0x80000000)
53 02eb84d0 Michael S. Tsirkin
        return -ENOSPC;
54 02eb84d0 Michael S. Tsirkin
55 02eb84d0 Michael S. Tsirkin
    /* Add space for MSI-X structures */
56 5e520a7d Blue Swirl
    if (!bar_size) {
57 5a1fc5e8 Michael S. Tsirkin
        new_size = MSIX_PAGE_SIZE;
58 5a1fc5e8 Michael S. Tsirkin
    } else if (bar_size < MSIX_PAGE_SIZE) {
59 5a1fc5e8 Michael S. Tsirkin
        bar_size = MSIX_PAGE_SIZE;
60 5a1fc5e8 Michael S. Tsirkin
        new_size = MSIX_PAGE_SIZE * 2;
61 5a1fc5e8 Michael S. Tsirkin
    } else {
62 02eb84d0 Michael S. Tsirkin
        new_size = bar_size * 2;
63 5a1fc5e8 Michael S. Tsirkin
    }
64 02eb84d0 Michael S. Tsirkin
65 02eb84d0 Michael S. Tsirkin
    pdev->msix_bar_size = new_size;
66 ca77089d Isaku Yamahata
    config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX,
67 ca77089d Isaku Yamahata
                                       0, MSIX_CAP_LENGTH);
68 02eb84d0 Michael S. Tsirkin
    if (config_offset < 0)
69 02eb84d0 Michael S. Tsirkin
        return config_offset;
70 02eb84d0 Michael S. Tsirkin
    config = pdev->config + config_offset;
71 02eb84d0 Michael S. Tsirkin
72 02eb84d0 Michael S. Tsirkin
    pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
73 02eb84d0 Michael S. Tsirkin
    /* Table on top of BAR */
74 01731cfb Jan Kiszka
    pci_set_long(config + PCI_MSIX_TABLE, bar_size | bar_nr);
75 02eb84d0 Michael S. Tsirkin
    /* Pending bits on top of that */
76 01731cfb Jan Kiszka
    pci_set_long(config + PCI_MSIX_PBA, (bar_size + MSIX_PAGE_PENDING) |
77 5a1fc5e8 Michael S. Tsirkin
                 bar_nr);
78 02eb84d0 Michael S. Tsirkin
    pdev->msix_cap = config_offset;
79 ebabb67a Stefan Weil
    /* Make flags bit writable. */
80 5b5cb086 Michael S. Tsirkin
    pdev->wmask[config_offset + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK |
81 5b5cb086 Michael S. Tsirkin
            MSIX_MASKALL_MASK;
82 02eb84d0 Michael S. Tsirkin
    return 0;
83 02eb84d0 Michael S. Tsirkin
}
84 02eb84d0 Michael S. Tsirkin
85 95524ae8 Avi Kivity
static uint64_t msix_mmio_read(void *opaque, target_phys_addr_t addr,
86 95524ae8 Avi Kivity
                               unsigned size)
87 02eb84d0 Michael S. Tsirkin
{
88 02eb84d0 Michael S. Tsirkin
    PCIDevice *dev = opaque;
89 76f5159d Michael S. Tsirkin
    unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3;
90 02eb84d0 Michael S. Tsirkin
    void *page = dev->msix_table_page;
91 02eb84d0 Michael S. Tsirkin
92 76f5159d Michael S. Tsirkin
    return pci_get_long(page + offset);
93 02eb84d0 Michael S. Tsirkin
}
94 02eb84d0 Michael S. Tsirkin
95 02eb84d0 Michael S. Tsirkin
static uint8_t msix_pending_mask(int vector)
96 02eb84d0 Michael S. Tsirkin
{
97 02eb84d0 Michael S. Tsirkin
    return 1 << (vector % 8);
98 02eb84d0 Michael S. Tsirkin
}
99 02eb84d0 Michael S. Tsirkin
100 02eb84d0 Michael S. Tsirkin
static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)
101 02eb84d0 Michael S. Tsirkin
{
102 5a1fc5e8 Michael S. Tsirkin
    return dev->msix_table_page + MSIX_PAGE_PENDING + vector / 8;
103 02eb84d0 Michael S. Tsirkin
}
104 02eb84d0 Michael S. Tsirkin
105 02eb84d0 Michael S. Tsirkin
static int msix_is_pending(PCIDevice *dev, int vector)
106 02eb84d0 Michael S. Tsirkin
{
107 02eb84d0 Michael S. Tsirkin
    return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
108 02eb84d0 Michael S. Tsirkin
}
109 02eb84d0 Michael S. Tsirkin
110 02eb84d0 Michael S. Tsirkin
static void msix_set_pending(PCIDevice *dev, int vector)
111 02eb84d0 Michael S. Tsirkin
{
112 02eb84d0 Michael S. Tsirkin
    *msix_pending_byte(dev, vector) |= msix_pending_mask(vector);
113 02eb84d0 Michael S. Tsirkin
}
114 02eb84d0 Michael S. Tsirkin
115 02eb84d0 Michael S. Tsirkin
static void msix_clr_pending(PCIDevice *dev, int vector)
116 02eb84d0 Michael S. Tsirkin
{
117 02eb84d0 Michael S. Tsirkin
    *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector);
118 02eb84d0 Michael S. Tsirkin
}
119 02eb84d0 Michael S. Tsirkin
120 5b5cb086 Michael S. Tsirkin
static int msix_function_masked(PCIDevice *dev)
121 5b5cb086 Michael S. Tsirkin
{
122 5b5cb086 Michael S. Tsirkin
    return dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK;
123 5b5cb086 Michael S. Tsirkin
}
124 5b5cb086 Michael S. Tsirkin
125 02eb84d0 Michael S. Tsirkin
static int msix_is_masked(PCIDevice *dev, int vector)
126 02eb84d0 Michael S. Tsirkin
{
127 01731cfb Jan Kiszka
    unsigned offset =
128 01731cfb Jan Kiszka
        vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
129 5b5cb086 Michael S. Tsirkin
    return msix_function_masked(dev) ||
130 01731cfb Jan Kiszka
           dev->msix_table_page[offset] & PCI_MSIX_ENTRY_CTRL_MASKBIT;
131 5b5cb086 Michael S. Tsirkin
}
132 5b5cb086 Michael S. Tsirkin
133 5b5cb086 Michael S. Tsirkin
static void msix_handle_mask_update(PCIDevice *dev, int vector)
134 5b5cb086 Michael S. Tsirkin
{
135 5b5cb086 Michael S. Tsirkin
    if (!msix_is_masked(dev, vector) && msix_is_pending(dev, vector)) {
136 5b5cb086 Michael S. Tsirkin
        msix_clr_pending(dev, vector);
137 5b5cb086 Michael S. Tsirkin
        msix_notify(dev, vector);
138 5b5cb086 Michael S. Tsirkin
    }
139 5b5cb086 Michael S. Tsirkin
}
140 5b5cb086 Michael S. Tsirkin
141 5b5cb086 Michael S. Tsirkin
/* Handle MSI-X capability config write. */
142 5b5cb086 Michael S. Tsirkin
void msix_write_config(PCIDevice *dev, uint32_t addr,
143 5b5cb086 Michael S. Tsirkin
                       uint32_t val, int len)
144 5b5cb086 Michael S. Tsirkin
{
145 5b5cb086 Michael S. Tsirkin
    unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
146 5b5cb086 Michael S. Tsirkin
    int vector;
147 5b5cb086 Michael S. Tsirkin
148 98a3cb02 Isaku Yamahata
    if (!range_covers_byte(addr, len, enable_pos)) {
149 5b5cb086 Michael S. Tsirkin
        return;
150 5b5cb086 Michael S. Tsirkin
    }
151 5b5cb086 Michael S. Tsirkin
152 5b5cb086 Michael S. Tsirkin
    if (!msix_enabled(dev)) {
153 5b5cb086 Michael S. Tsirkin
        return;
154 5b5cb086 Michael S. Tsirkin
    }
155 5b5cb086 Michael S. Tsirkin
156 e407bf13 Isaku Yamahata
    pci_device_deassert_intx(dev);
157 5b5cb086 Michael S. Tsirkin
158 5b5cb086 Michael S. Tsirkin
    if (msix_function_masked(dev)) {
159 5b5cb086 Michael S. Tsirkin
        return;
160 5b5cb086 Michael S. Tsirkin
    }
161 5b5cb086 Michael S. Tsirkin
162 5b5cb086 Michael S. Tsirkin
    for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
163 5b5cb086 Michael S. Tsirkin
        msix_handle_mask_update(dev, vector);
164 5b5cb086 Michael S. Tsirkin
    }
165 02eb84d0 Michael S. Tsirkin
}
166 02eb84d0 Michael S. Tsirkin
167 95524ae8 Avi Kivity
static void msix_mmio_write(void *opaque, target_phys_addr_t addr,
168 95524ae8 Avi Kivity
                            uint64_t val, unsigned size)
169 02eb84d0 Michael S. Tsirkin
{
170 02eb84d0 Michael S. Tsirkin
    PCIDevice *dev = opaque;
171 76f5159d Michael S. Tsirkin
    unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3;
172 01731cfb Jan Kiszka
    int vector = offset / PCI_MSIX_ENTRY_SIZE;
173 76f5159d Michael S. Tsirkin
    pci_set_long(dev->msix_table_page + offset, val);
174 5b5cb086 Michael S. Tsirkin
    msix_handle_mask_update(dev, vector);
175 02eb84d0 Michael S. Tsirkin
}
176 02eb84d0 Michael S. Tsirkin
177 95524ae8 Avi Kivity
static const MemoryRegionOps msix_mmio_ops = {
178 95524ae8 Avi Kivity
    .read = msix_mmio_read,
179 95524ae8 Avi Kivity
    .write = msix_mmio_write,
180 95524ae8 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
181 95524ae8 Avi Kivity
    .valid = {
182 95524ae8 Avi Kivity
        .min_access_size = 4,
183 95524ae8 Avi Kivity
        .max_access_size = 4,
184 95524ae8 Avi Kivity
    },
185 02eb84d0 Michael S. Tsirkin
};
186 02eb84d0 Michael S. Tsirkin
187 95524ae8 Avi Kivity
static void msix_mmio_setup(PCIDevice *d, MemoryRegion *bar)
188 02eb84d0 Michael S. Tsirkin
{
189 02eb84d0 Michael S. Tsirkin
    uint8_t *config = d->config + d->msix_cap;
190 01731cfb Jan Kiszka
    uint32_t table = pci_get_long(config + PCI_MSIX_TABLE);
191 5a1fc5e8 Michael S. Tsirkin
    uint32_t offset = table & ~(MSIX_PAGE_SIZE - 1);
192 02eb84d0 Michael S. Tsirkin
    /* TODO: for assigned devices, we'll want to make it possible to map
193 02eb84d0 Michael S. Tsirkin
     * pending bits separately in case they are in a separate bar. */
194 02eb84d0 Michael S. Tsirkin
195 95524ae8 Avi Kivity
    memory_region_add_subregion(bar, offset, &d->msix_mmio);
196 02eb84d0 Michael S. Tsirkin
}
197 02eb84d0 Michael S. Tsirkin
198 ae1be0bb Michael S. Tsirkin
static void msix_mask_all(struct PCIDevice *dev, unsigned nentries)
199 ae1be0bb Michael S. Tsirkin
{
200 ae1be0bb Michael S. Tsirkin
    int vector;
201 ae1be0bb Michael S. Tsirkin
    for (vector = 0; vector < nentries; ++vector) {
202 01731cfb Jan Kiszka
        unsigned offset =
203 01731cfb Jan Kiszka
            vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
204 01731cfb Jan Kiszka
        dev->msix_table_page[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
205 ae1be0bb Michael S. Tsirkin
    }
206 ae1be0bb Michael S. Tsirkin
}
207 ae1be0bb Michael S. Tsirkin
208 02eb84d0 Michael S. Tsirkin
/* Initialize the MSI-X structures. Note: if MSI-X is supported, BAR size is
209 02eb84d0 Michael S. Tsirkin
 * modified, it should be retrieved with msix_bar_size. */
210 02eb84d0 Michael S. Tsirkin
int msix_init(struct PCIDevice *dev, unsigned short nentries,
211 95524ae8 Avi Kivity
              MemoryRegion *bar,
212 5a1fc5e8 Michael S. Tsirkin
              unsigned bar_nr, unsigned bar_size)
213 02eb84d0 Michael S. Tsirkin
{
214 02eb84d0 Michael S. Tsirkin
    int ret;
215 02eb84d0 Michael S. Tsirkin
    /* Nothing to do if MSI is not supported by interrupt controller */
216 02eb84d0 Michael S. Tsirkin
    if (!msix_supported)
217 02eb84d0 Michael S. Tsirkin
        return -ENOTSUP;
218 02eb84d0 Michael S. Tsirkin
219 02eb84d0 Michael S. Tsirkin
    if (nentries > MSIX_MAX_ENTRIES)
220 02eb84d0 Michael S. Tsirkin
        return -EINVAL;
221 02eb84d0 Michael S. Tsirkin
222 7267c094 Anthony Liguori
    dev->msix_entry_used = g_malloc0(MSIX_MAX_ENTRIES *
223 02eb84d0 Michael S. Tsirkin
                                        sizeof *dev->msix_entry_used);
224 02eb84d0 Michael S. Tsirkin
225 7267c094 Anthony Liguori
    dev->msix_table_page = g_malloc0(MSIX_PAGE_SIZE);
226 ae1be0bb Michael S. Tsirkin
    msix_mask_all(dev, nentries);
227 02eb84d0 Michael S. Tsirkin
228 95524ae8 Avi Kivity
    memory_region_init_io(&dev->msix_mmio, &msix_mmio_ops, dev,
229 95524ae8 Avi Kivity
                          "msix", MSIX_PAGE_SIZE);
230 02eb84d0 Michael S. Tsirkin
231 02eb84d0 Michael S. Tsirkin
    dev->msix_entries_nr = nentries;
232 02eb84d0 Michael S. Tsirkin
    ret = msix_add_config(dev, nentries, bar_nr, bar_size);
233 02eb84d0 Michael S. Tsirkin
    if (ret)
234 02eb84d0 Michael S. Tsirkin
        goto err_config;
235 02eb84d0 Michael S. Tsirkin
236 02eb84d0 Michael S. Tsirkin
    dev->cap_present |= QEMU_PCI_CAP_MSIX;
237 95524ae8 Avi Kivity
    msix_mmio_setup(dev, bar);
238 02eb84d0 Michael S. Tsirkin
    return 0;
239 02eb84d0 Michael S. Tsirkin
240 02eb84d0 Michael S. Tsirkin
err_config:
241 3174ecd1 Michael S. Tsirkin
    dev->msix_entries_nr = 0;
242 95524ae8 Avi Kivity
    memory_region_destroy(&dev->msix_mmio);
243 7267c094 Anthony Liguori
    g_free(dev->msix_table_page);
244 02eb84d0 Michael S. Tsirkin
    dev->msix_table_page = NULL;
245 7267c094 Anthony Liguori
    g_free(dev->msix_entry_used);
246 02eb84d0 Michael S. Tsirkin
    dev->msix_entry_used = NULL;
247 02eb84d0 Michael S. Tsirkin
    return ret;
248 02eb84d0 Michael S. Tsirkin
}
249 02eb84d0 Michael S. Tsirkin
250 98304c84 Michael S. Tsirkin
static void msix_free_irq_entries(PCIDevice *dev)
251 98304c84 Michael S. Tsirkin
{
252 98304c84 Michael S. Tsirkin
    int vector;
253 98304c84 Michael S. Tsirkin
254 98304c84 Michael S. Tsirkin
    for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
255 98304c84 Michael S. Tsirkin
        dev->msix_entry_used[vector] = 0;
256 98304c84 Michael S. Tsirkin
        msix_clr_pending(dev, vector);
257 98304c84 Michael S. Tsirkin
    }
258 98304c84 Michael S. Tsirkin
}
259 98304c84 Michael S. Tsirkin
260 02eb84d0 Michael S. Tsirkin
/* Clean up resources for the device. */
261 95524ae8 Avi Kivity
int msix_uninit(PCIDevice *dev, MemoryRegion *bar)
262 02eb84d0 Michael S. Tsirkin
{
263 02eb84d0 Michael S. Tsirkin
    if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
264 02eb84d0 Michael S. Tsirkin
        return 0;
265 02eb84d0 Michael S. Tsirkin
    pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
266 02eb84d0 Michael S. Tsirkin
    dev->msix_cap = 0;
267 02eb84d0 Michael S. Tsirkin
    msix_free_irq_entries(dev);
268 02eb84d0 Michael S. Tsirkin
    dev->msix_entries_nr = 0;
269 95524ae8 Avi Kivity
    memory_region_del_subregion(bar, &dev->msix_mmio);
270 95524ae8 Avi Kivity
    memory_region_destroy(&dev->msix_mmio);
271 7267c094 Anthony Liguori
    g_free(dev->msix_table_page);
272 02eb84d0 Michael S. Tsirkin
    dev->msix_table_page = NULL;
273 7267c094 Anthony Liguori
    g_free(dev->msix_entry_used);
274 02eb84d0 Michael S. Tsirkin
    dev->msix_entry_used = NULL;
275 02eb84d0 Michael S. Tsirkin
    dev->cap_present &= ~QEMU_PCI_CAP_MSIX;
276 02eb84d0 Michael S. Tsirkin
    return 0;
277 02eb84d0 Michael S. Tsirkin
}
278 02eb84d0 Michael S. Tsirkin
279 02eb84d0 Michael S. Tsirkin
void msix_save(PCIDevice *dev, QEMUFile *f)
280 02eb84d0 Michael S. Tsirkin
{
281 9a3e12c8 Michael S. Tsirkin
    unsigned n = dev->msix_entries_nr;
282 9a3e12c8 Michael S. Tsirkin
283 72755a70 Michael S. Tsirkin
    if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
284 9a3e12c8 Michael S. Tsirkin
        return;
285 72755a70 Michael S. Tsirkin
    }
286 9a3e12c8 Michael S. Tsirkin
287 01731cfb Jan Kiszka
    qemu_put_buffer(f, dev->msix_table_page, n * PCI_MSIX_ENTRY_SIZE);
288 5a1fc5e8 Michael S. Tsirkin
    qemu_put_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8);
289 02eb84d0 Michael S. Tsirkin
}
290 02eb84d0 Michael S. Tsirkin
291 02eb84d0 Michael S. Tsirkin
/* Should be called after restoring the config space. */
292 02eb84d0 Michael S. Tsirkin
void msix_load(PCIDevice *dev, QEMUFile *f)
293 02eb84d0 Michael S. Tsirkin
{
294 02eb84d0 Michael S. Tsirkin
    unsigned n = dev->msix_entries_nr;
295 02eb84d0 Michael S. Tsirkin
296 98846d73 Blue Swirl
    if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
297 02eb84d0 Michael S. Tsirkin
        return;
298 98846d73 Blue Swirl
    }
299 02eb84d0 Michael S. Tsirkin
300 4bfd1712 Michael S. Tsirkin
    msix_free_irq_entries(dev);
301 01731cfb Jan Kiszka
    qemu_get_buffer(f, dev->msix_table_page, n * PCI_MSIX_ENTRY_SIZE);
302 5a1fc5e8 Michael S. Tsirkin
    qemu_get_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8);
303 02eb84d0 Michael S. Tsirkin
}
304 02eb84d0 Michael S. Tsirkin
305 02eb84d0 Michael S. Tsirkin
/* Does device support MSI-X? */
306 02eb84d0 Michael S. Tsirkin
int msix_present(PCIDevice *dev)
307 02eb84d0 Michael S. Tsirkin
{
308 02eb84d0 Michael S. Tsirkin
    return dev->cap_present & QEMU_PCI_CAP_MSIX;
309 02eb84d0 Michael S. Tsirkin
}
310 02eb84d0 Michael S. Tsirkin
311 02eb84d0 Michael S. Tsirkin
/* Is MSI-X enabled? */
312 02eb84d0 Michael S. Tsirkin
int msix_enabled(PCIDevice *dev)
313 02eb84d0 Michael S. Tsirkin
{
314 02eb84d0 Michael S. Tsirkin
    return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
315 2760952b Michael S. Tsirkin
        (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
316 02eb84d0 Michael S. Tsirkin
         MSIX_ENABLE_MASK);
317 02eb84d0 Michael S. Tsirkin
}
318 02eb84d0 Michael S. Tsirkin
319 02eb84d0 Michael S. Tsirkin
/* Size of bar where MSI-X table resides, or 0 if MSI-X not supported. */
320 02eb84d0 Michael S. Tsirkin
uint32_t msix_bar_size(PCIDevice *dev)
321 02eb84d0 Michael S. Tsirkin
{
322 02eb84d0 Michael S. Tsirkin
    return (dev->cap_present & QEMU_PCI_CAP_MSIX) ?
323 02eb84d0 Michael S. Tsirkin
        dev->msix_bar_size : 0;
324 02eb84d0 Michael S. Tsirkin
}
325 02eb84d0 Michael S. Tsirkin
326 02eb84d0 Michael S. Tsirkin
/* Send an MSI-X message */
327 02eb84d0 Michael S. Tsirkin
void msix_notify(PCIDevice *dev, unsigned vector)
328 02eb84d0 Michael S. Tsirkin
{
329 01731cfb Jan Kiszka
    uint8_t *table_entry = dev->msix_table_page + vector * PCI_MSIX_ENTRY_SIZE;
330 02eb84d0 Michael S. Tsirkin
    uint64_t address;
331 02eb84d0 Michael S. Tsirkin
    uint32_t data;
332 02eb84d0 Michael S. Tsirkin
333 02eb84d0 Michael S. Tsirkin
    if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector])
334 02eb84d0 Michael S. Tsirkin
        return;
335 02eb84d0 Michael S. Tsirkin
    if (msix_is_masked(dev, vector)) {
336 02eb84d0 Michael S. Tsirkin
        msix_set_pending(dev, vector);
337 02eb84d0 Michael S. Tsirkin
        return;
338 02eb84d0 Michael S. Tsirkin
    }
339 02eb84d0 Michael S. Tsirkin
340 01731cfb Jan Kiszka
    address = pci_get_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR);
341 01731cfb Jan Kiszka
    data = pci_get_long(table_entry + PCI_MSIX_ENTRY_DATA);
342 ae5d3eb4 Alexander Graf
    stl_le_phys(address, data);
343 02eb84d0 Michael S. Tsirkin
}
344 02eb84d0 Michael S. Tsirkin
345 02eb84d0 Michael S. Tsirkin
void msix_reset(PCIDevice *dev)
346 02eb84d0 Michael S. Tsirkin
{
347 02eb84d0 Michael S. Tsirkin
    if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
348 02eb84d0 Michael S. Tsirkin
        return;
349 02eb84d0 Michael S. Tsirkin
    msix_free_irq_entries(dev);
350 2760952b Michael S. Tsirkin
    dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &=
351 2760952b Michael S. Tsirkin
            ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET];
352 5a1fc5e8 Michael S. Tsirkin
    memset(dev->msix_table_page, 0, MSIX_PAGE_SIZE);
353 ae1be0bb Michael S. Tsirkin
    msix_mask_all(dev, dev->msix_entries_nr);
354 02eb84d0 Michael S. Tsirkin
}
355 02eb84d0 Michael S. Tsirkin
356 02eb84d0 Michael S. Tsirkin
/* PCI spec suggests that devices make it possible for software to configure
357 02eb84d0 Michael S. Tsirkin
 * less vectors than supported by the device, but does not specify a standard
358 02eb84d0 Michael S. Tsirkin
 * mechanism for devices to do so.
359 02eb84d0 Michael S. Tsirkin
 *
360 02eb84d0 Michael S. Tsirkin
 * We support this by asking devices to declare vectors software is going to
361 02eb84d0 Michael S. Tsirkin
 * actually use, and checking this on the notification path. Devices that
362 02eb84d0 Michael S. Tsirkin
 * don't want to follow the spec suggestion can declare all vectors as used. */
363 02eb84d0 Michael S. Tsirkin
364 02eb84d0 Michael S. Tsirkin
/* Mark vector as used. */
365 02eb84d0 Michael S. Tsirkin
int msix_vector_use(PCIDevice *dev, unsigned vector)
366 02eb84d0 Michael S. Tsirkin
{
367 02eb84d0 Michael S. Tsirkin
    if (vector >= dev->msix_entries_nr)
368 02eb84d0 Michael S. Tsirkin
        return -EINVAL;
369 02eb84d0 Michael S. Tsirkin
    dev->msix_entry_used[vector]++;
370 02eb84d0 Michael S. Tsirkin
    return 0;
371 02eb84d0 Michael S. Tsirkin
}
372 02eb84d0 Michael S. Tsirkin
373 02eb84d0 Michael S. Tsirkin
/* Mark vector as unused. */
374 02eb84d0 Michael S. Tsirkin
void msix_vector_unuse(PCIDevice *dev, unsigned vector)
375 02eb84d0 Michael S. Tsirkin
{
376 98304c84 Michael S. Tsirkin
    if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
377 98304c84 Michael S. Tsirkin
        return;
378 98304c84 Michael S. Tsirkin
    }
379 98304c84 Michael S. Tsirkin
    if (--dev->msix_entry_used[vector]) {
380 98304c84 Michael S. Tsirkin
        return;
381 98304c84 Michael S. Tsirkin
    }
382 98304c84 Michael S. Tsirkin
    msix_clr_pending(dev, vector);
383 02eb84d0 Michael S. Tsirkin
}
384 b5f28bca Michael S. Tsirkin
385 b5f28bca Michael S. Tsirkin
void msix_unuse_all_vectors(PCIDevice *dev)
386 b5f28bca Michael S. Tsirkin
{
387 b5f28bca Michael S. Tsirkin
    if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
388 b5f28bca Michael S. Tsirkin
        return;
389 b5f28bca Michael S. Tsirkin
    msix_free_irq_entries(dev);
390 b5f28bca Michael S. Tsirkin
}