Statistics
| Branch: | Revision:

root / hw / pci / msix.c @ 95669e69

History | View | Annotate | Download (16.9 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 6b620ca3 Paolo Bonzini
 *
13 6b620ca3 Paolo Bonzini
 * Contributions after 2012-01-13 are licensed under the terms of the
14 6b620ca3 Paolo Bonzini
 * GNU GPL, version 2 or (at your option) any later version.
15 02eb84d0 Michael S. Tsirkin
 */
16 02eb84d0 Michael S. Tsirkin
17 c759b24f Michael S. Tsirkin
#include "hw/hw.h"
18 c759b24f Michael S. Tsirkin
#include "hw/pci/msi.h"
19 c759b24f Michael S. Tsirkin
#include "hw/pci/msix.h"
20 c759b24f Michael S. Tsirkin
#include "hw/pci/pci.h"
21 1de7afc9 Paolo Bonzini
#include "qemu/range.h"
22 02eb84d0 Michael S. Tsirkin
23 02eb84d0 Michael S. Tsirkin
#define MSIX_CAP_LENGTH 12
24 02eb84d0 Michael S. Tsirkin
25 2760952b Michael S. Tsirkin
/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
26 2760952b Michael S. Tsirkin
#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1)
27 02eb84d0 Michael S. Tsirkin
#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
28 5b5cb086 Michael S. Tsirkin
#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8)
29 02eb84d0 Michael S. Tsirkin
30 4c93bfa9 Michael S. Tsirkin
MSIMessage msix_get_message(PCIDevice *dev, unsigned vector)
31 bc4caf49 Jan Kiszka
{
32 d35e428c Alex Williamson
    uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE;
33 bc4caf49 Jan Kiszka
    MSIMessage msg;
34 bc4caf49 Jan Kiszka
35 bc4caf49 Jan Kiszka
    msg.address = pci_get_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR);
36 bc4caf49 Jan Kiszka
    msg.data = pci_get_long(table_entry + PCI_MSIX_ENTRY_DATA);
37 bc4caf49 Jan Kiszka
    return msg;
38 bc4caf49 Jan Kiszka
}
39 02eb84d0 Michael S. Tsirkin
40 932d4a42 Alexey Kardashevskiy
/*
41 932d4a42 Alexey Kardashevskiy
 * Special API for POWER to configure the vectors through
42 932d4a42 Alexey Kardashevskiy
 * a side channel. Should never be used by devices.
43 932d4a42 Alexey Kardashevskiy
 */
44 932d4a42 Alexey Kardashevskiy
void msix_set_message(PCIDevice *dev, int vector, struct MSIMessage msg)
45 932d4a42 Alexey Kardashevskiy
{
46 932d4a42 Alexey Kardashevskiy
    uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE;
47 932d4a42 Alexey Kardashevskiy
48 932d4a42 Alexey Kardashevskiy
    pci_set_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR, msg.address);
49 932d4a42 Alexey Kardashevskiy
    pci_set_long(table_entry + PCI_MSIX_ENTRY_DATA, msg.data);
50 932d4a42 Alexey Kardashevskiy
    table_entry[PCI_MSIX_ENTRY_VECTOR_CTRL] &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
51 932d4a42 Alexey Kardashevskiy
}
52 932d4a42 Alexey Kardashevskiy
53 02eb84d0 Michael S. Tsirkin
static uint8_t msix_pending_mask(int vector)
54 02eb84d0 Michael S. Tsirkin
{
55 02eb84d0 Michael S. Tsirkin
    return 1 << (vector % 8);
56 02eb84d0 Michael S. Tsirkin
}
57 02eb84d0 Michael S. Tsirkin
58 02eb84d0 Michael S. Tsirkin
static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)
59 02eb84d0 Michael S. Tsirkin
{
60 d35e428c Alex Williamson
    return dev->msix_pba + vector / 8;
61 02eb84d0 Michael S. Tsirkin
}
62 02eb84d0 Michael S. Tsirkin
63 02eb84d0 Michael S. Tsirkin
static int msix_is_pending(PCIDevice *dev, int vector)
64 02eb84d0 Michael S. Tsirkin
{
65 02eb84d0 Michael S. Tsirkin
    return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
66 02eb84d0 Michael S. Tsirkin
}
67 02eb84d0 Michael S. Tsirkin
68 70f8ee39 Michael S. Tsirkin
void msix_set_pending(PCIDevice *dev, unsigned int vector)
69 02eb84d0 Michael S. Tsirkin
{
70 02eb84d0 Michael S. Tsirkin
    *msix_pending_byte(dev, vector) |= msix_pending_mask(vector);
71 02eb84d0 Michael S. Tsirkin
}
72 02eb84d0 Michael S. Tsirkin
73 02eb84d0 Michael S. Tsirkin
static void msix_clr_pending(PCIDevice *dev, int vector)
74 02eb84d0 Michael S. Tsirkin
{
75 02eb84d0 Michael S. Tsirkin
    *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector);
76 02eb84d0 Michael S. Tsirkin
}
77 02eb84d0 Michael S. Tsirkin
78 70f8ee39 Michael S. Tsirkin
static bool msix_vector_masked(PCIDevice *dev, unsigned int vector, bool fmask)
79 02eb84d0 Michael S. Tsirkin
{
80 ae392c41 Michael S. Tsirkin
    unsigned offset = vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
81 d35e428c Alex Williamson
    return fmask || dev->msix_table[offset] & PCI_MSIX_ENTRY_CTRL_MASKBIT;
82 5b5cb086 Michael S. Tsirkin
}
83 5b5cb086 Michael S. Tsirkin
84 70f8ee39 Michael S. Tsirkin
bool msix_is_masked(PCIDevice *dev, unsigned int vector)
85 5b5cb086 Michael S. Tsirkin
{
86 ae392c41 Michael S. Tsirkin
    return msix_vector_masked(dev, vector, dev->msix_function_masked);
87 ae392c41 Michael S. Tsirkin
}
88 ae392c41 Michael S. Tsirkin
89 2cdfe53c Jan Kiszka
static void msix_fire_vector_notifier(PCIDevice *dev,
90 2cdfe53c Jan Kiszka
                                      unsigned int vector, bool is_masked)
91 2cdfe53c Jan Kiszka
{
92 2cdfe53c Jan Kiszka
    MSIMessage msg;
93 2cdfe53c Jan Kiszka
    int ret;
94 2cdfe53c Jan Kiszka
95 2cdfe53c Jan Kiszka
    if (!dev->msix_vector_use_notifier) {
96 2cdfe53c Jan Kiszka
        return;
97 2cdfe53c Jan Kiszka
    }
98 2cdfe53c Jan Kiszka
    if (is_masked) {
99 2cdfe53c Jan Kiszka
        dev->msix_vector_release_notifier(dev, vector);
100 2cdfe53c Jan Kiszka
    } else {
101 2cdfe53c Jan Kiszka
        msg = msix_get_message(dev, vector);
102 2cdfe53c Jan Kiszka
        ret = dev->msix_vector_use_notifier(dev, vector, msg);
103 2cdfe53c Jan Kiszka
        assert(ret >= 0);
104 2cdfe53c Jan Kiszka
    }
105 2cdfe53c Jan Kiszka
}
106 2cdfe53c Jan Kiszka
107 ae392c41 Michael S. Tsirkin
static void msix_handle_mask_update(PCIDevice *dev, int vector, bool was_masked)
108 ae392c41 Michael S. Tsirkin
{
109 ae392c41 Michael S. Tsirkin
    bool is_masked = msix_is_masked(dev, vector);
110 2cdfe53c Jan Kiszka
111 ae392c41 Michael S. Tsirkin
    if (is_masked == was_masked) {
112 ae392c41 Michael S. Tsirkin
        return;
113 ae392c41 Michael S. Tsirkin
    }
114 ae392c41 Michael S. Tsirkin
115 2cdfe53c Jan Kiszka
    msix_fire_vector_notifier(dev, vector, is_masked);
116 2cdfe53c Jan Kiszka
117 ae392c41 Michael S. Tsirkin
    if (!is_masked && msix_is_pending(dev, vector)) {
118 5b5cb086 Michael S. Tsirkin
        msix_clr_pending(dev, vector);
119 5b5cb086 Michael S. Tsirkin
        msix_notify(dev, vector);
120 5b5cb086 Michael S. Tsirkin
    }
121 5b5cb086 Michael S. Tsirkin
}
122 5b5cb086 Michael S. Tsirkin
123 50322249 Michael S. Tsirkin
static void msix_update_function_masked(PCIDevice *dev)
124 50322249 Michael S. Tsirkin
{
125 50322249 Michael S. Tsirkin
    dev->msix_function_masked = !msix_enabled(dev) ||
126 50322249 Michael S. Tsirkin
        (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK);
127 50322249 Michael S. Tsirkin
}
128 50322249 Michael S. Tsirkin
129 5b5cb086 Michael S. Tsirkin
/* Handle MSI-X capability config write. */
130 5b5cb086 Michael S. Tsirkin
void msix_write_config(PCIDevice *dev, uint32_t addr,
131 5b5cb086 Michael S. Tsirkin
                       uint32_t val, int len)
132 5b5cb086 Michael S. Tsirkin
{
133 5b5cb086 Michael S. Tsirkin
    unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
134 5b5cb086 Michael S. Tsirkin
    int vector;
135 50322249 Michael S. Tsirkin
    bool was_masked;
136 5b5cb086 Michael S. Tsirkin
137 7c9958b0 Jan Kiszka
    if (!msix_present(dev) || !range_covers_byte(addr, len, enable_pos)) {
138 5b5cb086 Michael S. Tsirkin
        return;
139 5b5cb086 Michael S. Tsirkin
    }
140 5b5cb086 Michael S. Tsirkin
141 50322249 Michael S. Tsirkin
    was_masked = dev->msix_function_masked;
142 50322249 Michael S. Tsirkin
    msix_update_function_masked(dev);
143 50322249 Michael S. Tsirkin
144 5b5cb086 Michael S. Tsirkin
    if (!msix_enabled(dev)) {
145 5b5cb086 Michael S. Tsirkin
        return;
146 5b5cb086 Michael S. Tsirkin
    }
147 5b5cb086 Michael S. Tsirkin
148 e407bf13 Isaku Yamahata
    pci_device_deassert_intx(dev);
149 5b5cb086 Michael S. Tsirkin
150 50322249 Michael S. Tsirkin
    if (dev->msix_function_masked == was_masked) {
151 5b5cb086 Michael S. Tsirkin
        return;
152 5b5cb086 Michael S. Tsirkin
    }
153 5b5cb086 Michael S. Tsirkin
154 5b5cb086 Michael S. Tsirkin
    for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
155 ae392c41 Michael S. Tsirkin
        msix_handle_mask_update(dev, vector,
156 ae392c41 Michael S. Tsirkin
                                msix_vector_masked(dev, vector, was_masked));
157 5b5cb086 Michael S. Tsirkin
    }
158 02eb84d0 Michael S. Tsirkin
}
159 02eb84d0 Michael S. Tsirkin
160 a8170e5e Avi Kivity
static uint64_t msix_table_mmio_read(void *opaque, hwaddr addr,
161 d35e428c Alex Williamson
                                     unsigned size)
162 eebcb0a7 Alex Williamson
{
163 eebcb0a7 Alex Williamson
    PCIDevice *dev = opaque;
164 eebcb0a7 Alex Williamson
165 d35e428c Alex Williamson
    return pci_get_long(dev->msix_table + addr);
166 eebcb0a7 Alex Williamson
}
167 eebcb0a7 Alex Williamson
168 a8170e5e Avi Kivity
static void msix_table_mmio_write(void *opaque, hwaddr addr,
169 d35e428c Alex Williamson
                                  uint64_t val, unsigned size)
170 02eb84d0 Michael S. Tsirkin
{
171 02eb84d0 Michael S. Tsirkin
    PCIDevice *dev = opaque;
172 d35e428c Alex Williamson
    int vector = addr / PCI_MSIX_ENTRY_SIZE;
173 ae392c41 Michael S. Tsirkin
    bool was_masked;
174 9a93b617 Michael S. Tsirkin
175 ae392c41 Michael S. Tsirkin
    was_masked = msix_is_masked(dev, vector);
176 d35e428c Alex Williamson
    pci_set_long(dev->msix_table + addr, val);
177 ae392c41 Michael S. Tsirkin
    msix_handle_mask_update(dev, vector, was_masked);
178 02eb84d0 Michael S. Tsirkin
}
179 02eb84d0 Michael S. Tsirkin
180 d35e428c Alex Williamson
static const MemoryRegionOps msix_table_mmio_ops = {
181 d35e428c Alex Williamson
    .read = msix_table_mmio_read,
182 d35e428c Alex Williamson
    .write = msix_table_mmio_write,
183 68d1e1f5 Alexander Graf
    .endianness = DEVICE_LITTLE_ENDIAN,
184 d35e428c Alex Williamson
    .valid = {
185 d35e428c Alex Williamson
        .min_access_size = 4,
186 d35e428c Alex Williamson
        .max_access_size = 4,
187 d35e428c Alex Williamson
    },
188 d35e428c Alex Williamson
};
189 d35e428c Alex Williamson
190 a8170e5e Avi Kivity
static uint64_t msix_pba_mmio_read(void *opaque, hwaddr addr,
191 d35e428c Alex Williamson
                                   unsigned size)
192 d35e428c Alex Williamson
{
193 d35e428c Alex Williamson
    PCIDevice *dev = opaque;
194 bbef882c Michael S. Tsirkin
    if (dev->msix_vector_poll_notifier) {
195 bbef882c Michael S. Tsirkin
        unsigned vector_start = addr * 8;
196 bbef882c Michael S. Tsirkin
        unsigned vector_end = MIN(addr + size * 8, dev->msix_entries_nr);
197 bbef882c Michael S. Tsirkin
        dev->msix_vector_poll_notifier(dev, vector_start, vector_end);
198 bbef882c Michael S. Tsirkin
    }
199 d35e428c Alex Williamson
200 d35e428c Alex Williamson
    return pci_get_long(dev->msix_pba + addr);
201 d35e428c Alex Williamson
}
202 d35e428c Alex Williamson
203 d35e428c Alex Williamson
static const MemoryRegionOps msix_pba_mmio_ops = {
204 d35e428c Alex Williamson
    .read = msix_pba_mmio_read,
205 68d1e1f5 Alexander Graf
    .endianness = DEVICE_LITTLE_ENDIAN,
206 95524ae8 Avi Kivity
    .valid = {
207 95524ae8 Avi Kivity
        .min_access_size = 4,
208 95524ae8 Avi Kivity
        .max_access_size = 4,
209 95524ae8 Avi Kivity
    },
210 02eb84d0 Michael S. Tsirkin
};
211 02eb84d0 Michael S. Tsirkin
212 ae1be0bb Michael S. Tsirkin
static void msix_mask_all(struct PCIDevice *dev, unsigned nentries)
213 ae1be0bb Michael S. Tsirkin
{
214 ae1be0bb Michael S. Tsirkin
    int vector;
215 5b5f1330 Jan Kiszka
216 ae1be0bb Michael S. Tsirkin
    for (vector = 0; vector < nentries; ++vector) {
217 01731cfb Jan Kiszka
        unsigned offset =
218 01731cfb Jan Kiszka
            vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
219 5b5f1330 Jan Kiszka
        bool was_masked = msix_is_masked(dev, vector);
220 5b5f1330 Jan Kiszka
221 d35e428c Alex Williamson
        dev->msix_table[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
222 5b5f1330 Jan Kiszka
        msix_handle_mask_update(dev, vector, was_masked);
223 ae1be0bb Michael S. Tsirkin
    }
224 ae1be0bb Michael S. Tsirkin
}
225 ae1be0bb Michael S. Tsirkin
226 5a2c2029 Alex Williamson
/* Initialize the MSI-X structures */
227 02eb84d0 Michael S. Tsirkin
int msix_init(struct PCIDevice *dev, unsigned short nentries,
228 5a2c2029 Alex Williamson
              MemoryRegion *table_bar, uint8_t table_bar_nr,
229 5a2c2029 Alex Williamson
              unsigned table_offset, MemoryRegion *pba_bar,
230 5a2c2029 Alex Williamson
              uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos)
231 02eb84d0 Michael S. Tsirkin
{
232 5a2c2029 Alex Williamson
    int cap;
233 d35e428c Alex Williamson
    unsigned table_size, pba_size;
234 5a2c2029 Alex Williamson
    uint8_t *config;
235 60ba3cc2 Jan Kiszka
236 02eb84d0 Michael S. Tsirkin
    /* Nothing to do if MSI is not supported by interrupt controller */
237 60ba3cc2 Jan Kiszka
    if (!msi_supported) {
238 02eb84d0 Michael S. Tsirkin
        return -ENOTSUP;
239 60ba3cc2 Jan Kiszka
    }
240 5a2c2029 Alex Williamson
241 5a2c2029 Alex Williamson
    if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1) {
242 02eb84d0 Michael S. Tsirkin
        return -EINVAL;
243 5a2c2029 Alex Williamson
    }
244 02eb84d0 Michael S. Tsirkin
245 d35e428c Alex Williamson
    table_size = nentries * PCI_MSIX_ENTRY_SIZE;
246 d35e428c Alex Williamson
    pba_size = QEMU_ALIGN_UP(nentries, 64) / 8;
247 d35e428c Alex Williamson
248 5a2c2029 Alex Williamson
    /* Sanity test: table & pba don't overlap, fit within BARs, min aligned */
249 5a2c2029 Alex Williamson
    if ((table_bar_nr == pba_bar_nr &&
250 5a2c2029 Alex Williamson
         ranges_overlap(table_offset, table_size, pba_offset, pba_size)) ||
251 5a2c2029 Alex Williamson
        table_offset + table_size > memory_region_size(table_bar) ||
252 5a2c2029 Alex Williamson
        pba_offset + pba_size > memory_region_size(pba_bar) ||
253 5a2c2029 Alex Williamson
        (table_offset | pba_offset) & PCI_MSIX_FLAGS_BIRMASK) {
254 5a2c2029 Alex Williamson
        return -EINVAL;
255 5a2c2029 Alex Williamson
    }
256 5a2c2029 Alex Williamson
257 5a2c2029 Alex Williamson
    cap = pci_add_capability(dev, PCI_CAP_ID_MSIX, cap_pos, MSIX_CAP_LENGTH);
258 5a2c2029 Alex Williamson
    if (cap < 0) {
259 5a2c2029 Alex Williamson
        return cap;
260 5a2c2029 Alex Williamson
    }
261 5a2c2029 Alex Williamson
262 5a2c2029 Alex Williamson
    dev->msix_cap = cap;
263 5a2c2029 Alex Williamson
    dev->cap_present |= QEMU_PCI_CAP_MSIX;
264 5a2c2029 Alex Williamson
    config = dev->config + cap;
265 5a2c2029 Alex Williamson
266 5a2c2029 Alex Williamson
    pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
267 5a2c2029 Alex Williamson
    dev->msix_entries_nr = nentries;
268 5a2c2029 Alex Williamson
    dev->msix_function_masked = true;
269 5a2c2029 Alex Williamson
270 5a2c2029 Alex Williamson
    pci_set_long(config + PCI_MSIX_TABLE, table_offset | table_bar_nr);
271 5a2c2029 Alex Williamson
    pci_set_long(config + PCI_MSIX_PBA, pba_offset | pba_bar_nr);
272 5a2c2029 Alex Williamson
273 5a2c2029 Alex Williamson
    /* Make flags bit writable. */
274 5a2c2029 Alex Williamson
    dev->wmask[cap + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK |
275 5a2c2029 Alex Williamson
                                             MSIX_MASKALL_MASK;
276 02eb84d0 Michael S. Tsirkin
277 d35e428c Alex Williamson
    dev->msix_table = g_malloc0(table_size);
278 d35e428c Alex Williamson
    dev->msix_pba = g_malloc0(pba_size);
279 5a2c2029 Alex Williamson
    dev->msix_entry_used = g_malloc0(nentries * sizeof *dev->msix_entry_used);
280 5a2c2029 Alex Williamson
281 ae1be0bb Michael S. Tsirkin
    msix_mask_all(dev, nentries);
282 02eb84d0 Michael S. Tsirkin
283 d35e428c Alex Williamson
    memory_region_init_io(&dev->msix_table_mmio, &msix_table_mmio_ops, dev,
284 d35e428c Alex Williamson
                          "msix-table", table_size);
285 5a2c2029 Alex Williamson
    memory_region_add_subregion(table_bar, table_offset, &dev->msix_table_mmio);
286 d35e428c Alex Williamson
    memory_region_init_io(&dev->msix_pba_mmio, &msix_pba_mmio_ops, dev,
287 d35e428c Alex Williamson
                          "msix-pba", pba_size);
288 5a2c2029 Alex Williamson
    memory_region_add_subregion(pba_bar, pba_offset, &dev->msix_pba_mmio);
289 02eb84d0 Michael S. Tsirkin
290 02eb84d0 Michael S. Tsirkin
    return 0;
291 02eb84d0 Michael S. Tsirkin
}
292 02eb84d0 Michael S. Tsirkin
293 53f94925 Alex Williamson
int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries,
294 53f94925 Alex Williamson
                            uint8_t bar_nr)
295 53f94925 Alex Williamson
{
296 53f94925 Alex Williamson
    int ret;
297 53f94925 Alex Williamson
    char *name;
298 53f94925 Alex Williamson
299 53f94925 Alex Williamson
    /*
300 53f94925 Alex Williamson
     * Migration compatibility dictates that this remains a 4k
301 53f94925 Alex Williamson
     * BAR with the vector table in the lower half and PBA in
302 53f94925 Alex Williamson
     * the upper half.  Do not use these elsewhere!
303 53f94925 Alex Williamson
     */
304 53f94925 Alex Williamson
#define MSIX_EXCLUSIVE_BAR_SIZE 4096
305 5a2c2029 Alex Williamson
#define MSIX_EXCLUSIVE_BAR_TABLE_OFFSET 0
306 53f94925 Alex Williamson
#define MSIX_EXCLUSIVE_BAR_PBA_OFFSET (MSIX_EXCLUSIVE_BAR_SIZE / 2)
307 5a2c2029 Alex Williamson
#define MSIX_EXCLUSIVE_CAP_OFFSET 0
308 53f94925 Alex Williamson
309 53f94925 Alex Williamson
    if (nentries * PCI_MSIX_ENTRY_SIZE > MSIX_EXCLUSIVE_BAR_PBA_OFFSET) {
310 53f94925 Alex Williamson
        return -EINVAL;
311 53f94925 Alex Williamson
    }
312 53f94925 Alex Williamson
313 5f893b4e Gerd Hoffmann
    name = g_strdup_printf("%s-msix", dev->name);
314 53f94925 Alex Williamson
    memory_region_init(&dev->msix_exclusive_bar, name, MSIX_EXCLUSIVE_BAR_SIZE);
315 5f893b4e Gerd Hoffmann
    g_free(name);
316 53f94925 Alex Williamson
317 53f94925 Alex Williamson
    ret = msix_init(dev, nentries, &dev->msix_exclusive_bar, bar_nr,
318 5a2c2029 Alex Williamson
                    MSIX_EXCLUSIVE_BAR_TABLE_OFFSET, &dev->msix_exclusive_bar,
319 5a2c2029 Alex Williamson
                    bar_nr, MSIX_EXCLUSIVE_BAR_PBA_OFFSET,
320 5a2c2029 Alex Williamson
                    MSIX_EXCLUSIVE_CAP_OFFSET);
321 53f94925 Alex Williamson
    if (ret) {
322 53f94925 Alex Williamson
        memory_region_destroy(&dev->msix_exclusive_bar);
323 53f94925 Alex Williamson
        return ret;
324 53f94925 Alex Williamson
    }
325 53f94925 Alex Williamson
326 53f94925 Alex Williamson
    pci_register_bar(dev, bar_nr, PCI_BASE_ADDRESS_SPACE_MEMORY,
327 53f94925 Alex Williamson
                     &dev->msix_exclusive_bar);
328 53f94925 Alex Williamson
329 53f94925 Alex Williamson
    return 0;
330 53f94925 Alex Williamson
}
331 53f94925 Alex Williamson
332 98304c84 Michael S. Tsirkin
static void msix_free_irq_entries(PCIDevice *dev)
333 98304c84 Michael S. Tsirkin
{
334 98304c84 Michael S. Tsirkin
    int vector;
335 98304c84 Michael S. Tsirkin
336 98304c84 Michael S. Tsirkin
    for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
337 98304c84 Michael S. Tsirkin
        dev->msix_entry_used[vector] = 0;
338 98304c84 Michael S. Tsirkin
        msix_clr_pending(dev, vector);
339 98304c84 Michael S. Tsirkin
    }
340 98304c84 Michael S. Tsirkin
}
341 98304c84 Michael S. Tsirkin
342 3cac001e Michael S. Tsirkin
static void msix_clear_all_vectors(PCIDevice *dev)
343 3cac001e Michael S. Tsirkin
{
344 3cac001e Michael S. Tsirkin
    int vector;
345 3cac001e Michael S. Tsirkin
346 3cac001e Michael S. Tsirkin
    for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
347 3cac001e Michael S. Tsirkin
        msix_clr_pending(dev, vector);
348 3cac001e Michael S. Tsirkin
    }
349 3cac001e Michael S. Tsirkin
}
350 3cac001e Michael S. Tsirkin
351 02eb84d0 Michael S. Tsirkin
/* Clean up resources for the device. */
352 572992ee Alex Williamson
void msix_uninit(PCIDevice *dev, MemoryRegion *table_bar, MemoryRegion *pba_bar)
353 02eb84d0 Michael S. Tsirkin
{
354 44701ab7 Jan Kiszka
    if (!msix_present(dev)) {
355 572992ee Alex Williamson
        return;
356 44701ab7 Jan Kiszka
    }
357 02eb84d0 Michael S. Tsirkin
    pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
358 02eb84d0 Michael S. Tsirkin
    dev->msix_cap = 0;
359 02eb84d0 Michael S. Tsirkin
    msix_free_irq_entries(dev);
360 02eb84d0 Michael S. Tsirkin
    dev->msix_entries_nr = 0;
361 5a2c2029 Alex Williamson
    memory_region_del_subregion(pba_bar, &dev->msix_pba_mmio);
362 d35e428c Alex Williamson
    memory_region_destroy(&dev->msix_pba_mmio);
363 d35e428c Alex Williamson
    g_free(dev->msix_pba);
364 d35e428c Alex Williamson
    dev->msix_pba = NULL;
365 5a2c2029 Alex Williamson
    memory_region_del_subregion(table_bar, &dev->msix_table_mmio);
366 d35e428c Alex Williamson
    memory_region_destroy(&dev->msix_table_mmio);
367 d35e428c Alex Williamson
    g_free(dev->msix_table);
368 d35e428c Alex Williamson
    dev->msix_table = NULL;
369 7267c094 Anthony Liguori
    g_free(dev->msix_entry_used);
370 02eb84d0 Michael S. Tsirkin
    dev->msix_entry_used = NULL;
371 02eb84d0 Michael S. Tsirkin
    dev->cap_present &= ~QEMU_PCI_CAP_MSIX;
372 02eb84d0 Michael S. Tsirkin
}
373 02eb84d0 Michael S. Tsirkin
374 53f94925 Alex Williamson
void msix_uninit_exclusive_bar(PCIDevice *dev)
375 53f94925 Alex Williamson
{
376 53f94925 Alex Williamson
    if (msix_present(dev)) {
377 5a2c2029 Alex Williamson
        msix_uninit(dev, &dev->msix_exclusive_bar, &dev->msix_exclusive_bar);
378 53f94925 Alex Williamson
        memory_region_destroy(&dev->msix_exclusive_bar);
379 53f94925 Alex Williamson
    }
380 53f94925 Alex Williamson
}
381 53f94925 Alex Williamson
382 02eb84d0 Michael S. Tsirkin
void msix_save(PCIDevice *dev, QEMUFile *f)
383 02eb84d0 Michael S. Tsirkin
{
384 9a3e12c8 Michael S. Tsirkin
    unsigned n = dev->msix_entries_nr;
385 9a3e12c8 Michael S. Tsirkin
386 44701ab7 Jan Kiszka
    if (!msix_present(dev)) {
387 9a3e12c8 Michael S. Tsirkin
        return;
388 72755a70 Michael S. Tsirkin
    }
389 9a3e12c8 Michael S. Tsirkin
390 d35e428c Alex Williamson
    qemu_put_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE);
391 d35e428c Alex Williamson
    qemu_put_buffer(f, dev->msix_pba, (n + 7) / 8);
392 02eb84d0 Michael S. Tsirkin
}
393 02eb84d0 Michael S. Tsirkin
394 02eb84d0 Michael S. Tsirkin
/* Should be called after restoring the config space. */
395 02eb84d0 Michael S. Tsirkin
void msix_load(PCIDevice *dev, QEMUFile *f)
396 02eb84d0 Michael S. Tsirkin
{
397 02eb84d0 Michael S. Tsirkin
    unsigned n = dev->msix_entries_nr;
398 2cdfe53c Jan Kiszka
    unsigned int vector;
399 02eb84d0 Michael S. Tsirkin
400 44701ab7 Jan Kiszka
    if (!msix_present(dev)) {
401 02eb84d0 Michael S. Tsirkin
        return;
402 98846d73 Blue Swirl
    }
403 02eb84d0 Michael S. Tsirkin
404 3cac001e Michael S. Tsirkin
    msix_clear_all_vectors(dev);
405 d35e428c Alex Williamson
    qemu_get_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE);
406 d35e428c Alex Williamson
    qemu_get_buffer(f, dev->msix_pba, (n + 7) / 8);
407 50322249 Michael S. Tsirkin
    msix_update_function_masked(dev);
408 2cdfe53c Jan Kiszka
409 2cdfe53c Jan Kiszka
    for (vector = 0; vector < n; vector++) {
410 2cdfe53c Jan Kiszka
        msix_handle_mask_update(dev, vector, true);
411 2cdfe53c Jan Kiszka
    }
412 02eb84d0 Michael S. Tsirkin
}
413 02eb84d0 Michael S. Tsirkin
414 02eb84d0 Michael S. Tsirkin
/* Does device support MSI-X? */
415 02eb84d0 Michael S. Tsirkin
int msix_present(PCIDevice *dev)
416 02eb84d0 Michael S. Tsirkin
{
417 02eb84d0 Michael S. Tsirkin
    return dev->cap_present & QEMU_PCI_CAP_MSIX;
418 02eb84d0 Michael S. Tsirkin
}
419 02eb84d0 Michael S. Tsirkin
420 02eb84d0 Michael S. Tsirkin
/* Is MSI-X enabled? */
421 02eb84d0 Michael S. Tsirkin
int msix_enabled(PCIDevice *dev)
422 02eb84d0 Michael S. Tsirkin
{
423 02eb84d0 Michael S. Tsirkin
    return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
424 2760952b Michael S. Tsirkin
        (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
425 02eb84d0 Michael S. Tsirkin
         MSIX_ENABLE_MASK);
426 02eb84d0 Michael S. Tsirkin
}
427 02eb84d0 Michael S. Tsirkin
428 02eb84d0 Michael S. Tsirkin
/* Send an MSI-X message */
429 02eb84d0 Michael S. Tsirkin
void msix_notify(PCIDevice *dev, unsigned vector)
430 02eb84d0 Michael S. Tsirkin
{
431 bc4caf49 Jan Kiszka
    MSIMessage msg;
432 02eb84d0 Michael S. Tsirkin
433 02eb84d0 Michael S. Tsirkin
    if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector])
434 02eb84d0 Michael S. Tsirkin
        return;
435 02eb84d0 Michael S. Tsirkin
    if (msix_is_masked(dev, vector)) {
436 02eb84d0 Michael S. Tsirkin
        msix_set_pending(dev, vector);
437 02eb84d0 Michael S. Tsirkin
        return;
438 02eb84d0 Michael S. Tsirkin
    }
439 02eb84d0 Michael S. Tsirkin
440 bc4caf49 Jan Kiszka
    msg = msix_get_message(dev, vector);
441 bc4caf49 Jan Kiszka
442 bc4caf49 Jan Kiszka
    stl_le_phys(msg.address, msg.data);
443 02eb84d0 Michael S. Tsirkin
}
444 02eb84d0 Michael S. Tsirkin
445 02eb84d0 Michael S. Tsirkin
void msix_reset(PCIDevice *dev)
446 02eb84d0 Michael S. Tsirkin
{
447 44701ab7 Jan Kiszka
    if (!msix_present(dev)) {
448 02eb84d0 Michael S. Tsirkin
        return;
449 44701ab7 Jan Kiszka
    }
450 3cac001e Michael S. Tsirkin
    msix_clear_all_vectors(dev);
451 2760952b Michael S. Tsirkin
    dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &=
452 2760952b Michael S. Tsirkin
            ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET];
453 d35e428c Alex Williamson
    memset(dev->msix_table, 0, dev->msix_entries_nr * PCI_MSIX_ENTRY_SIZE);
454 d35e428c Alex Williamson
    memset(dev->msix_pba, 0, QEMU_ALIGN_UP(dev->msix_entries_nr, 64) / 8);
455 ae1be0bb Michael S. Tsirkin
    msix_mask_all(dev, dev->msix_entries_nr);
456 02eb84d0 Michael S. Tsirkin
}
457 02eb84d0 Michael S. Tsirkin
458 02eb84d0 Michael S. Tsirkin
/* PCI spec suggests that devices make it possible for software to configure
459 02eb84d0 Michael S. Tsirkin
 * less vectors than supported by the device, but does not specify a standard
460 02eb84d0 Michael S. Tsirkin
 * mechanism for devices to do so.
461 02eb84d0 Michael S. Tsirkin
 *
462 02eb84d0 Michael S. Tsirkin
 * We support this by asking devices to declare vectors software is going to
463 02eb84d0 Michael S. Tsirkin
 * actually use, and checking this on the notification path. Devices that
464 02eb84d0 Michael S. Tsirkin
 * don't want to follow the spec suggestion can declare all vectors as used. */
465 02eb84d0 Michael S. Tsirkin
466 02eb84d0 Michael S. Tsirkin
/* Mark vector as used. */
467 02eb84d0 Michael S. Tsirkin
int msix_vector_use(PCIDevice *dev, unsigned vector)
468 02eb84d0 Michael S. Tsirkin
{
469 02eb84d0 Michael S. Tsirkin
    if (vector >= dev->msix_entries_nr)
470 02eb84d0 Michael S. Tsirkin
        return -EINVAL;
471 02eb84d0 Michael S. Tsirkin
    dev->msix_entry_used[vector]++;
472 02eb84d0 Michael S. Tsirkin
    return 0;
473 02eb84d0 Michael S. Tsirkin
}
474 02eb84d0 Michael S. Tsirkin
475 02eb84d0 Michael S. Tsirkin
/* Mark vector as unused. */
476 02eb84d0 Michael S. Tsirkin
void msix_vector_unuse(PCIDevice *dev, unsigned vector)
477 02eb84d0 Michael S. Tsirkin
{
478 98304c84 Michael S. Tsirkin
    if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
479 98304c84 Michael S. Tsirkin
        return;
480 98304c84 Michael S. Tsirkin
    }
481 98304c84 Michael S. Tsirkin
    if (--dev->msix_entry_used[vector]) {
482 98304c84 Michael S. Tsirkin
        return;
483 98304c84 Michael S. Tsirkin
    }
484 98304c84 Michael S. Tsirkin
    msix_clr_pending(dev, vector);
485 02eb84d0 Michael S. Tsirkin
}
486 b5f28bca Michael S. Tsirkin
487 b5f28bca Michael S. Tsirkin
void msix_unuse_all_vectors(PCIDevice *dev)
488 b5f28bca Michael S. Tsirkin
{
489 44701ab7 Jan Kiszka
    if (!msix_present(dev)) {
490 b5f28bca Michael S. Tsirkin
        return;
491 44701ab7 Jan Kiszka
    }
492 b5f28bca Michael S. Tsirkin
    msix_free_irq_entries(dev);
493 b5f28bca Michael S. Tsirkin
}
494 2cdfe53c Jan Kiszka
495 cb697aaa Jan Kiszka
unsigned int msix_nr_vectors_allocated(const PCIDevice *dev)
496 cb697aaa Jan Kiszka
{
497 cb697aaa Jan Kiszka
    return dev->msix_entries_nr;
498 cb697aaa Jan Kiszka
}
499 cb697aaa Jan Kiszka
500 2cdfe53c Jan Kiszka
static int msix_set_notifier_for_vector(PCIDevice *dev, unsigned int vector)
501 2cdfe53c Jan Kiszka
{
502 2cdfe53c Jan Kiszka
    MSIMessage msg;
503 2cdfe53c Jan Kiszka
504 2cdfe53c Jan Kiszka
    if (msix_is_masked(dev, vector)) {
505 2cdfe53c Jan Kiszka
        return 0;
506 2cdfe53c Jan Kiszka
    }
507 2cdfe53c Jan Kiszka
    msg = msix_get_message(dev, vector);
508 2cdfe53c Jan Kiszka
    return dev->msix_vector_use_notifier(dev, vector, msg);
509 2cdfe53c Jan Kiszka
}
510 2cdfe53c Jan Kiszka
511 2cdfe53c Jan Kiszka
static void msix_unset_notifier_for_vector(PCIDevice *dev, unsigned int vector)
512 2cdfe53c Jan Kiszka
{
513 2cdfe53c Jan Kiszka
    if (msix_is_masked(dev, vector)) {
514 2cdfe53c Jan Kiszka
        return;
515 2cdfe53c Jan Kiszka
    }
516 2cdfe53c Jan Kiszka
    dev->msix_vector_release_notifier(dev, vector);
517 2cdfe53c Jan Kiszka
}
518 2cdfe53c Jan Kiszka
519 2cdfe53c Jan Kiszka
int msix_set_vector_notifiers(PCIDevice *dev,
520 2cdfe53c Jan Kiszka
                              MSIVectorUseNotifier use_notifier,
521 bbef882c Michael S. Tsirkin
                              MSIVectorReleaseNotifier release_notifier,
522 bbef882c Michael S. Tsirkin
                              MSIVectorPollNotifier poll_notifier)
523 2cdfe53c Jan Kiszka
{
524 2cdfe53c Jan Kiszka
    int vector, ret;
525 2cdfe53c Jan Kiszka
526 2cdfe53c Jan Kiszka
    assert(use_notifier && release_notifier);
527 2cdfe53c Jan Kiszka
528 2cdfe53c Jan Kiszka
    dev->msix_vector_use_notifier = use_notifier;
529 2cdfe53c Jan Kiszka
    dev->msix_vector_release_notifier = release_notifier;
530 bbef882c Michael S. Tsirkin
    dev->msix_vector_poll_notifier = poll_notifier;
531 2cdfe53c Jan Kiszka
532 2cdfe53c Jan Kiszka
    if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
533 2cdfe53c Jan Kiszka
        (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) {
534 2cdfe53c Jan Kiszka
        for (vector = 0; vector < dev->msix_entries_nr; vector++) {
535 2cdfe53c Jan Kiszka
            ret = msix_set_notifier_for_vector(dev, vector);
536 2cdfe53c Jan Kiszka
            if (ret < 0) {
537 2cdfe53c Jan Kiszka
                goto undo;
538 2cdfe53c Jan Kiszka
            }
539 2cdfe53c Jan Kiszka
        }
540 2cdfe53c Jan Kiszka
    }
541 bbef882c Michael S. Tsirkin
    if (dev->msix_vector_poll_notifier) {
542 bbef882c Michael S. Tsirkin
        dev->msix_vector_poll_notifier(dev, 0, dev->msix_entries_nr);
543 bbef882c Michael S. Tsirkin
    }
544 2cdfe53c Jan Kiszka
    return 0;
545 2cdfe53c Jan Kiszka
546 2cdfe53c Jan Kiszka
undo:
547 2cdfe53c Jan Kiszka
    while (--vector >= 0) {
548 2cdfe53c Jan Kiszka
        msix_unset_notifier_for_vector(dev, vector);
549 2cdfe53c Jan Kiszka
    }
550 2cdfe53c Jan Kiszka
    dev->msix_vector_use_notifier = NULL;
551 2cdfe53c Jan Kiszka
    dev->msix_vector_release_notifier = NULL;
552 2cdfe53c Jan Kiszka
    return ret;
553 2cdfe53c Jan Kiszka
}
554 2cdfe53c Jan Kiszka
555 2cdfe53c Jan Kiszka
void msix_unset_vector_notifiers(PCIDevice *dev)
556 2cdfe53c Jan Kiszka
{
557 2cdfe53c Jan Kiszka
    int vector;
558 2cdfe53c Jan Kiszka
559 2cdfe53c Jan Kiszka
    assert(dev->msix_vector_use_notifier &&
560 2cdfe53c Jan Kiszka
           dev->msix_vector_release_notifier);
561 2cdfe53c Jan Kiszka
562 2cdfe53c Jan Kiszka
    if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
563 2cdfe53c Jan Kiszka
        (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) {
564 2cdfe53c Jan Kiszka
        for (vector = 0; vector < dev->msix_entries_nr; vector++) {
565 2cdfe53c Jan Kiszka
            msix_unset_notifier_for_vector(dev, vector);
566 2cdfe53c Jan Kiszka
        }
567 2cdfe53c Jan Kiszka
    }
568 2cdfe53c Jan Kiszka
    dev->msix_vector_use_notifier = NULL;
569 2cdfe53c Jan Kiszka
    dev->msix_vector_release_notifier = NULL;
570 bbef882c Michael S. Tsirkin
    dev->msix_vector_poll_notifier = NULL;
571 2cdfe53c Jan Kiszka
}
572 340b50c7 Gerd Hoffmann
573 340b50c7 Gerd Hoffmann
static void put_msix_state(QEMUFile *f, void *pv, size_t size)
574 340b50c7 Gerd Hoffmann
{
575 340b50c7 Gerd Hoffmann
    msix_save(pv, f);
576 340b50c7 Gerd Hoffmann
}
577 340b50c7 Gerd Hoffmann
578 340b50c7 Gerd Hoffmann
static int get_msix_state(QEMUFile *f, void *pv, size_t size)
579 340b50c7 Gerd Hoffmann
{
580 340b50c7 Gerd Hoffmann
    msix_load(pv, f);
581 340b50c7 Gerd Hoffmann
    return 0;
582 340b50c7 Gerd Hoffmann
}
583 340b50c7 Gerd Hoffmann
584 340b50c7 Gerd Hoffmann
static VMStateInfo vmstate_info_msix = {
585 340b50c7 Gerd Hoffmann
    .name = "msix state",
586 340b50c7 Gerd Hoffmann
    .get  = get_msix_state,
587 340b50c7 Gerd Hoffmann
    .put  = put_msix_state,
588 340b50c7 Gerd Hoffmann
};
589 340b50c7 Gerd Hoffmann
590 340b50c7 Gerd Hoffmann
const VMStateDescription vmstate_msix = {
591 340b50c7 Gerd Hoffmann
    .name = "msix",
592 340b50c7 Gerd Hoffmann
    .fields = (VMStateField[]) {
593 340b50c7 Gerd Hoffmann
        {
594 340b50c7 Gerd Hoffmann
            .name         = "msix",
595 340b50c7 Gerd Hoffmann
            .version_id   = 0,
596 340b50c7 Gerd Hoffmann
            .field_exists = NULL,
597 340b50c7 Gerd Hoffmann
            .size         = 0,   /* ouch */
598 340b50c7 Gerd Hoffmann
            .info         = &vmstate_info_msix,
599 340b50c7 Gerd Hoffmann
            .flags        = VMS_SINGLE,
600 340b50c7 Gerd Hoffmann
            .offset       = 0,
601 340b50c7 Gerd Hoffmann
        },
602 340b50c7 Gerd Hoffmann
        VMSTATE_END_OF_LIST()
603 340b50c7 Gerd Hoffmann
    }
604 340b50c7 Gerd Hoffmann
};