blob: c7bdbeda9e95a9ec936ca74da5c98b967a2cfa57 [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
Peter Maydell97d54082016-01-26 18:17:15 +000017#include "qemu/osdep.h"
Michael S. Tsirkinc759b242012-12-12 23:05:42 +020018#include "hw/hw.h"
19#include "hw/pci/msi.h"
20#include "hw/pci/msix.h"
21#include "hw/pci/pci.h"
Stefano Stabellini428c3ec2016-01-13 14:59:09 +000022#include "hw/xen/xen.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/range.h"
Cao jinee640c62017-01-17 14:18:48 +080024#include "qapi/error.h"
Peter Xu993b1f42017-05-09 14:00:43 +080025#include "trace.h"
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030026
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030027#define MSIX_CAP_LENGTH 12
28
Michael S. Tsirkin27609522009-11-25 12:18:00 +020029/* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
30#define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030031#define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +020032#define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030033
Michael S. Tsirkin4c93bfa2012-12-21 00:27:02 +020034MSIMessage msix_get_message(PCIDevice *dev, unsigned vector)
Jan Kiszkabc4caf42012-05-17 10:32:29 -030035{
Alex Williamsond35e4282012-06-14 12:16:37 -060036 uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE;
Jan Kiszkabc4caf42012-05-17 10:32:29 -030037 MSIMessage msg;
38
39 msg.address = pci_get_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR);
40 msg.data = pci_get_long(table_entry + PCI_MSIX_ENTRY_DATA);
41 return msg;
42}
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030043
Alexey Kardashevskiy932d4a42012-07-19 10:35:07 +100044/*
45 * Special API for POWER to configure the vectors through
46 * a side channel. Should never be used by devices.
47 */
48void msix_set_message(PCIDevice *dev, int vector, struct MSIMessage msg)
49{
50 uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE;
51
52 pci_set_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR, msg.address);
53 pci_set_long(table_entry + PCI_MSIX_ENTRY_DATA, msg.data);
54 table_entry[PCI_MSIX_ENTRY_VECTOR_CTRL] &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
55}
56
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030057static uint8_t msix_pending_mask(int vector)
58{
59 return 1 << (vector % 8);
60}
61
62static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)
63{
Alex Williamsond35e4282012-06-14 12:16:37 -060064 return dev->msix_pba + vector / 8;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030065}
66
67static int msix_is_pending(PCIDevice *dev, int vector)
68{
69 return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
70}
71
Michael S. Tsirkin70f8ee32012-12-18 13:54:32 +020072void msix_set_pending(PCIDevice *dev, unsigned int vector)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030073{
74 *msix_pending_byte(dev, vector) |= msix_pending_mask(vector);
75}
76
Dmitry Fleytman3bdfaab2016-06-01 11:23:31 +030077void msix_clr_pending(PCIDevice *dev, int vector)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030078{
79 *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector);
80}
81
Michael S. Tsirkin70f8ee32012-12-18 13:54:32 +020082static bool msix_vector_masked(PCIDevice *dev, unsigned int vector, bool fmask)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +030083{
Stefano Stabellini428c3ec2016-01-13 14:59:09 +000084 unsigned offset = vector * PCI_MSIX_ENTRY_SIZE;
Michael S. Tsirkine1e4bf22016-02-13 20:50:50 +020085 uint8_t *data = &dev->msix_table[offset + PCI_MSIX_ENTRY_DATA];
Stefano Stabellini428c3ec2016-01-13 14:59:09 +000086 /* MSIs on Xen can be remapped into pirqs. In those cases, masking
87 * and unmasking go through the PV evtchn path. */
Michael S. Tsirkine1e4bf22016-02-13 20:50:50 +020088 if (xen_enabled() && xen_is_pirq_msi(pci_get_long(data))) {
Stefano Stabellini428c3ec2016-01-13 14:59:09 +000089 return false;
90 }
91 return fmask || dev->msix_table[offset + PCI_MSIX_ENTRY_VECTOR_CTRL] &
92 PCI_MSIX_ENTRY_CTRL_MASKBIT;
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +020093}
94
Michael S. Tsirkin70f8ee32012-12-18 13:54:32 +020095bool msix_is_masked(PCIDevice *dev, unsigned int vector)
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +020096{
Michael S. Tsirkinae392c42011-11-21 18:57:50 +020097 return msix_vector_masked(dev, vector, dev->msix_function_masked);
98}
99
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300100static void msix_fire_vector_notifier(PCIDevice *dev,
101 unsigned int vector, bool is_masked)
102{
103 MSIMessage msg;
104 int ret;
105
106 if (!dev->msix_vector_use_notifier) {
107 return;
108 }
109 if (is_masked) {
110 dev->msix_vector_release_notifier(dev, vector);
111 } else {
112 msg = msix_get_message(dev, vector);
113 ret = dev->msix_vector_use_notifier(dev, vector, msg);
114 assert(ret >= 0);
115 }
116}
117
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200118static void msix_handle_mask_update(PCIDevice *dev, int vector, bool was_masked)
119{
120 bool is_masked = msix_is_masked(dev, vector);
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300121
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200122 if (is_masked == was_masked) {
123 return;
124 }
125
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300126 msix_fire_vector_notifier(dev, vector, is_masked);
127
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200128 if (!is_masked && msix_is_pending(dev, vector)) {
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200129 msix_clr_pending(dev, vector);
130 msix_notify(dev, vector);
131 }
132}
133
Peter Xu993b1f42017-05-09 14:00:43 +0800134static bool msix_masked(PCIDevice *dev)
135{
136 return dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK;
137}
138
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200139static void msix_update_function_masked(PCIDevice *dev)
140{
Peter Xu993b1f42017-05-09 14:00:43 +0800141 dev->msix_function_masked = !msix_enabled(dev) || msix_masked(dev);
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200142}
143
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200144/* Handle MSI-X capability config write. */
145void msix_write_config(PCIDevice *dev, uint32_t addr,
146 uint32_t val, int len)
147{
148 unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
149 int vector;
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200150 bool was_masked;
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200151
Jan Kiszka7c9958b2012-05-11 11:42:39 -0300152 if (!msix_present(dev) || !range_covers_byte(addr, len, enable_pos)) {
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200153 return;
154 }
155
Peter Xu993b1f42017-05-09 14:00:43 +0800156 trace_msix_write_config(dev->name, msix_enabled(dev), msix_masked(dev));
157
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200158 was_masked = dev->msix_function_masked;
159 msix_update_function_masked(dev);
160
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200161 if (!msix_enabled(dev)) {
162 return;
163 }
164
Isaku Yamahatae407bf12011-01-20 16:21:40 +0900165 pci_device_deassert_intx(dev);
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200166
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200167 if (dev->msix_function_masked == was_masked) {
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200168 return;
169 }
170
171 for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200172 msix_handle_mask_update(dev, vector,
173 msix_vector_masked(dev, vector, was_masked));
Michael S. Tsirkin5b5cb082009-11-25 12:19:32 +0200174 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300175}
176
Avi Kivitya8170e52012-10-23 12:30:10 +0200177static uint64_t msix_table_mmio_read(void *opaque, hwaddr addr,
Alex Williamsond35e4282012-06-14 12:16:37 -0600178 unsigned size)
Alex Williamsoneebcb0a2012-06-14 12:16:19 -0600179{
180 PCIDevice *dev = opaque;
Alex Williamsoneebcb0a2012-06-14 12:16:19 -0600181
Alex Williamsond35e4282012-06-14 12:16:37 -0600182 return pci_get_long(dev->msix_table + addr);
Alex Williamsoneebcb0a2012-06-14 12:16:19 -0600183}
184
Avi Kivitya8170e52012-10-23 12:30:10 +0200185static void msix_table_mmio_write(void *opaque, hwaddr addr,
Alex Williamsond35e4282012-06-14 12:16:37 -0600186 uint64_t val, unsigned size)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300187{
188 PCIDevice *dev = opaque;
Alex Williamsond35e4282012-06-14 12:16:37 -0600189 int vector = addr / PCI_MSIX_ENTRY_SIZE;
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200190 bool was_masked;
Michael S. Tsirkin9a93b612011-11-21 18:57:31 +0200191
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200192 was_masked = msix_is_masked(dev, vector);
Alex Williamsond35e4282012-06-14 12:16:37 -0600193 pci_set_long(dev->msix_table + addr, val);
Michael S. Tsirkinae392c42011-11-21 18:57:50 +0200194 msix_handle_mask_update(dev, vector, was_masked);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300195}
196
Alex Williamsond35e4282012-06-14 12:16:37 -0600197static const MemoryRegionOps msix_table_mmio_ops = {
198 .read = msix_table_mmio_read,
199 .write = msix_table_mmio_write,
Alexander Graf68d1e1f2012-12-06 04:11:33 +0100200 .endianness = DEVICE_LITTLE_ENDIAN,
Alex Williamsond35e4282012-06-14 12:16:37 -0600201 .valid = {
202 .min_access_size = 4,
203 .max_access_size = 4,
204 },
205};
206
Avi Kivitya8170e52012-10-23 12:30:10 +0200207static uint64_t msix_pba_mmio_read(void *opaque, hwaddr addr,
Alex Williamsond35e4282012-06-14 12:16:37 -0600208 unsigned size)
209{
210 PCIDevice *dev = opaque;
Michael S. Tsirkinbbef8822012-12-12 16:10:02 +0200211 if (dev->msix_vector_poll_notifier) {
212 unsigned vector_start = addr * 8;
213 unsigned vector_end = MIN(addr + size * 8, dev->msix_entries_nr);
214 dev->msix_vector_poll_notifier(dev, vector_start, vector_end);
215 }
Alex Williamsond35e4282012-06-14 12:16:37 -0600216
217 return pci_get_long(dev->msix_pba + addr);
218}
219
Marc-André Lureau43b11a92015-06-26 14:25:29 +0200220static void msix_pba_mmio_write(void *opaque, hwaddr addr,
221 uint64_t val, unsigned size)
222{
223}
224
Alex Williamsond35e4282012-06-14 12:16:37 -0600225static const MemoryRegionOps msix_pba_mmio_ops = {
226 .read = msix_pba_mmio_read,
Marc-André Lureau43b11a92015-06-26 14:25:29 +0200227 .write = msix_pba_mmio_write,
Alexander Graf68d1e1f2012-12-06 04:11:33 +0100228 .endianness = DEVICE_LITTLE_ENDIAN,
Avi Kivity95524ae2011-08-08 16:09:26 +0300229 .valid = {
230 .min_access_size = 4,
231 .max_access_size = 4,
232 },
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300233};
234
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200235static void msix_mask_all(struct PCIDevice *dev, unsigned nentries)
236{
237 int vector;
Jan Kiszka5b5f1332012-05-17 10:32:30 -0300238
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200239 for (vector = 0; vector < nentries; ++vector) {
Jan Kiszka01731cf2011-06-09 09:39:56 +0200240 unsigned offset =
241 vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
Jan Kiszka5b5f1332012-05-17 10:32:30 -0300242 bool was_masked = msix_is_masked(dev, vector);
243
Alex Williamsond35e4282012-06-14 12:16:37 -0600244 dev->msix_table[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Jan Kiszka5b5f1332012-05-17 10:32:30 -0300245 msix_handle_mask_update(dev, vector, was_masked);
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200246 }
247}
248
Cao jinee640c62017-01-17 14:18:48 +0800249/*
250 * Make PCI device @dev MSI-X capable
251 * @nentries is the max number of MSI-X vectors that the device support.
252 * @table_bar is the MemoryRegion that MSI-X table structure resides.
253 * @table_bar_nr is number of base address register corresponding to @table_bar.
254 * @table_offset indicates the offset that the MSI-X table structure starts with
255 * in @table_bar.
256 * @pba_bar is the MemoryRegion that the Pending Bit Array structure resides.
257 * @pba_bar_nr is number of base address register corresponding to @pba_bar.
258 * @pba_offset indicates the offset that the Pending Bit Array structure
259 * starts with in @pba_bar.
260 * Non-zero @cap_pos puts capability MSI-X at that offset in PCI config space.
261 * @errp is for returning errors.
262 *
263 * Return 0 on success; set @errp and return -errno on error:
264 * -ENOTSUP means lacking msi support for a msi-capable platform.
265 * -EINVAL means capability overlap, happens when @cap_pos is non-zero,
266 * also means a programming error, except device assignment, which can check
267 * if a real HW is broken.
268 */
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300269int msix_init(struct PCIDevice *dev, unsigned short nentries,
Alex Williamson5a2c2022012-06-14 12:16:47 -0600270 MemoryRegion *table_bar, uint8_t table_bar_nr,
271 unsigned table_offset, MemoryRegion *pba_bar,
Cao jinee640c62017-01-17 14:18:48 +0800272 uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos,
273 Error **errp)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300274{
Alex Williamson5a2c2022012-06-14 12:16:47 -0600275 int cap;
Alex Williamsond35e4282012-06-14 12:16:37 -0600276 unsigned table_size, pba_size;
Alex Williamson5a2c2022012-06-14 12:16:47 -0600277 uint8_t *config;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300278
Jan Kiszka60ba3cc2011-10-15 14:33:17 +0200279 /* Nothing to do if MSI is not supported by interrupt controller */
Michael S. Tsirkin226419d2016-03-04 11:24:28 +0200280 if (!msi_nonbroken) {
Cao jinee640c62017-01-17 14:18:48 +0800281 error_setg(errp, "MSI-X is not supported by interrupt controller");
Jan Kiszka60ba3cc2011-10-15 14:33:17 +0200282 return -ENOTSUP;
283 }
Alex Williamson5a2c2022012-06-14 12:16:47 -0600284
285 if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1) {
Cao jinee640c62017-01-17 14:18:48 +0800286 error_setg(errp, "The number of MSI-X vectors is invalid");
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300287 return -EINVAL;
Alex Williamson5a2c2022012-06-14 12:16:47 -0600288 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300289
Alex Williamsond35e4282012-06-14 12:16:37 -0600290 table_size = nentries * PCI_MSIX_ENTRY_SIZE;
291 pba_size = QEMU_ALIGN_UP(nentries, 64) / 8;
292
Alex Williamson5a2c2022012-06-14 12:16:47 -0600293 /* Sanity test: table & pba don't overlap, fit within BARs, min aligned */
294 if ((table_bar_nr == pba_bar_nr &&
295 ranges_overlap(table_offset, table_size, pba_offset, pba_size)) ||
296 table_offset + table_size > memory_region_size(table_bar) ||
297 pba_offset + pba_size > memory_region_size(pba_bar) ||
298 (table_offset | pba_offset) & PCI_MSIX_FLAGS_BIRMASK) {
Cao jinee640c62017-01-17 14:18:48 +0800299 error_setg(errp, "table & pba overlap, or they don't fit in BARs,"
300 " or don't align");
Alex Williamson5a2c2022012-06-14 12:16:47 -0600301 return -EINVAL;
302 }
303
Mao Zhongyi27841272017-06-27 14:16:51 +0800304 cap = pci_add_capability(dev, PCI_CAP_ID_MSIX,
Cao jinee640c62017-01-17 14:18:48 +0800305 cap_pos, MSIX_CAP_LENGTH, errp);
Alex Williamson5a2c2022012-06-14 12:16:47 -0600306 if (cap < 0) {
307 return cap;
308 }
309
310 dev->msix_cap = cap;
311 dev->cap_present |= QEMU_PCI_CAP_MSIX;
312 config = dev->config + cap;
313
314 pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
315 dev->msix_entries_nr = nentries;
316 dev->msix_function_masked = true;
317
318 pci_set_long(config + PCI_MSIX_TABLE, table_offset | table_bar_nr);
319 pci_set_long(config + PCI_MSIX_PBA, pba_offset | pba_bar_nr);
320
321 /* Make flags bit writable. */
322 dev->wmask[cap + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK |
323 MSIX_MASKALL_MASK;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300324
Alex Williamsond35e4282012-06-14 12:16:37 -0600325 dev->msix_table = g_malloc0(table_size);
326 dev->msix_pba = g_malloc0(pba_size);
Alex Williamson5a2c2022012-06-14 12:16:47 -0600327 dev->msix_entry_used = g_malloc0(nentries * sizeof *dev->msix_entry_used);
328
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200329 msix_mask_all(dev, nentries);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300330
Paolo Bonzini40c5dce2013-06-06 21:25:08 -0400331 memory_region_init_io(&dev->msix_table_mmio, OBJECT(dev), &msix_table_mmio_ops, dev,
Alex Williamsond35e4282012-06-14 12:16:37 -0600332 "msix-table", table_size);
Alex Williamson5a2c2022012-06-14 12:16:47 -0600333 memory_region_add_subregion(table_bar, table_offset, &dev->msix_table_mmio);
Paolo Bonzini40c5dce2013-06-06 21:25:08 -0400334 memory_region_init_io(&dev->msix_pba_mmio, OBJECT(dev), &msix_pba_mmio_ops, dev,
Alex Williamsond35e4282012-06-14 12:16:37 -0600335 "msix-pba", pba_size);
Alex Williamson5a2c2022012-06-14 12:16:47 -0600336 memory_region_add_subregion(pba_bar, pba_offset, &dev->msix_pba_mmio);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300337
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300338 return 0;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300339}
340
Alex Williamson53f94922012-06-14 12:15:51 -0600341int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries,
Cao jinee640c62017-01-17 14:18:48 +0800342 uint8_t bar_nr, Error **errp)
Alex Williamson53f94922012-06-14 12:15:51 -0600343{
344 int ret;
345 char *name;
Jason Wanga0ccd212015-04-23 14:21:49 +0800346 uint32_t bar_size = 4096;
347 uint32_t bar_pba_offset = bar_size / 2;
348 uint32_t bar_pba_size = (nentries / 8 + 1) * 8;
Alex Williamson53f94922012-06-14 12:15:51 -0600349
350 /*
351 * Migration compatibility dictates that this remains a 4k
352 * BAR with the vector table in the lower half and PBA in
Jason Wanga0ccd212015-04-23 14:21:49 +0800353 * the upper half for nentries which is lower or equal to 128.
354 * No need to care about using more than 65 entries for legacy
355 * machine types who has at most 64 queues.
Alex Williamson53f94922012-06-14 12:15:51 -0600356 */
Jason Wanga0ccd212015-04-23 14:21:49 +0800357 if (nentries * PCI_MSIX_ENTRY_SIZE > bar_pba_offset) {
358 bar_pba_offset = nentries * PCI_MSIX_ENTRY_SIZE;
359 }
Alex Williamson53f94922012-06-14 12:15:51 -0600360
Jason Wanga0ccd212015-04-23 14:21:49 +0800361 if (bar_pba_offset + bar_pba_size > 4096) {
362 bar_size = bar_pba_offset + bar_pba_size;
363 }
364
Peter Maydell9bff5d82015-07-24 13:33:07 +0100365 bar_size = pow2ceil(bar_size);
Alex Williamson53f94922012-06-14 12:15:51 -0600366
Gerd Hoffmann5f893b42012-08-13 13:05:43 +0200367 name = g_strdup_printf("%s-msix", dev->name);
Jason Wanga0ccd212015-04-23 14:21:49 +0800368 memory_region_init(&dev->msix_exclusive_bar, OBJECT(dev), name, bar_size);
Gerd Hoffmann5f893b42012-08-13 13:05:43 +0200369 g_free(name);
Alex Williamson53f94922012-06-14 12:15:51 -0600370
371 ret = msix_init(dev, nentries, &dev->msix_exclusive_bar, bar_nr,
Jason Wanga0ccd212015-04-23 14:21:49 +0800372 0, &dev->msix_exclusive_bar,
373 bar_nr, bar_pba_offset,
Cao jinee640c62017-01-17 14:18:48 +0800374 0, errp);
Alex Williamson53f94922012-06-14 12:15:51 -0600375 if (ret) {
Alex Williamson53f94922012-06-14 12:15:51 -0600376 return ret;
377 }
378
379 pci_register_bar(dev, bar_nr, PCI_BASE_ADDRESS_SPACE_MEMORY,
380 &dev->msix_exclusive_bar);
381
382 return 0;
383}
384
Michael S. Tsirkin98304c82009-11-25 12:24:14 +0200385static void msix_free_irq_entries(PCIDevice *dev)
386{
387 int vector;
388
389 for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
390 dev->msix_entry_used[vector] = 0;
391 msix_clr_pending(dev, vector);
392 }
393}
394
Michael S. Tsirkin3cac0012012-08-29 19:40:56 +0300395static void msix_clear_all_vectors(PCIDevice *dev)
396{
397 int vector;
398
399 for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
400 msix_clr_pending(dev, vector);
401 }
402}
403
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300404/* Clean up resources for the device. */
Alex Williamson572992e2012-06-14 12:16:57 -0600405void msix_uninit(PCIDevice *dev, MemoryRegion *table_bar, MemoryRegion *pba_bar)
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300406{
Jan Kiszka44701ab2012-06-04 16:53:48 +0200407 if (!msix_present(dev)) {
Alex Williamson572992e2012-06-14 12:16:57 -0600408 return;
Jan Kiszka44701ab2012-06-04 16:53:48 +0200409 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300410 pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
411 dev->msix_cap = 0;
412 msix_free_irq_entries(dev);
413 dev->msix_entries_nr = 0;
Alex Williamson5a2c2022012-06-14 12:16:47 -0600414 memory_region_del_subregion(pba_bar, &dev->msix_pba_mmio);
Alex Williamsond35e4282012-06-14 12:16:37 -0600415 g_free(dev->msix_pba);
416 dev->msix_pba = NULL;
Alex Williamson5a2c2022012-06-14 12:16:47 -0600417 memory_region_del_subregion(table_bar, &dev->msix_table_mmio);
Alex Williamsond35e4282012-06-14 12:16:37 -0600418 g_free(dev->msix_table);
419 dev->msix_table = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500420 g_free(dev->msix_entry_used);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300421 dev->msix_entry_used = NULL;
422 dev->cap_present &= ~QEMU_PCI_CAP_MSIX;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300423}
424
Alex Williamson53f94922012-06-14 12:15:51 -0600425void msix_uninit_exclusive_bar(PCIDevice *dev)
426{
427 if (msix_present(dev)) {
Alex Williamson5a2c2022012-06-14 12:16:47 -0600428 msix_uninit(dev, &dev->msix_exclusive_bar, &dev->msix_exclusive_bar);
Alex Williamson53f94922012-06-14 12:15:51 -0600429 }
430}
431
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300432void msix_save(PCIDevice *dev, QEMUFile *f)
433{
Michael S. Tsirkin9a3e12c2009-07-01 16:28:00 +0300434 unsigned n = dev->msix_entries_nr;
435
Jan Kiszka44701ab2012-06-04 16:53:48 +0200436 if (!msix_present(dev)) {
Michael S. Tsirkin9a3e12c2009-07-01 16:28:00 +0300437 return;
Michael S. Tsirkin72755a72009-07-05 15:58:52 +0300438 }
Michael S. Tsirkin9a3e12c2009-07-01 16:28:00 +0300439
Alex Williamsond35e4282012-06-14 12:16:37 -0600440 qemu_put_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE);
Marc-André Lureau0ef1efc2017-06-22 13:04:16 +0200441 qemu_put_buffer(f, dev->msix_pba, DIV_ROUND_UP(n, 8));
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300442}
443
444/* Should be called after restoring the config space. */
445void msix_load(PCIDevice *dev, QEMUFile *f)
446{
447 unsigned n = dev->msix_entries_nr;
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300448 unsigned int vector;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300449
Jan Kiszka44701ab2012-06-04 16:53:48 +0200450 if (!msix_present(dev)) {
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300451 return;
Blue Swirl98846d72009-07-05 08:11:39 +0000452 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300453
Michael S. Tsirkin3cac0012012-08-29 19:40:56 +0300454 msix_clear_all_vectors(dev);
Alex Williamsond35e4282012-06-14 12:16:37 -0600455 qemu_get_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE);
Marc-André Lureau0ef1efc2017-06-22 13:04:16 +0200456 qemu_get_buffer(f, dev->msix_pba, DIV_ROUND_UP(n, 8));
Michael S. Tsirkin50322242011-11-21 18:57:21 +0200457 msix_update_function_masked(dev);
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300458
459 for (vector = 0; vector < n; vector++) {
460 msix_handle_mask_update(dev, vector, true);
461 }
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300462}
463
464/* Does device support MSI-X? */
465int msix_present(PCIDevice *dev)
466{
467 return dev->cap_present & QEMU_PCI_CAP_MSIX;
468}
469
470/* Is MSI-X enabled? */
471int msix_enabled(PCIDevice *dev)
472{
473 return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
Michael S. Tsirkin27609522009-11-25 12:18:00 +0200474 (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300475 MSIX_ENABLE_MASK);
476}
477
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300478/* Send an MSI-X message */
479void msix_notify(PCIDevice *dev, unsigned vector)
480{
Jan Kiszkabc4caf42012-05-17 10:32:29 -0300481 MSIMessage msg;
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300482
Cao jin93482432017-01-17 14:18:46 +0800483 if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300484 return;
Cao jin93482432017-01-17 14:18:46 +0800485 }
486
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300487 if (msix_is_masked(dev, vector)) {
488 msix_set_pending(dev, vector);
489 return;
490 }
491
Jan Kiszkabc4caf42012-05-17 10:32:29 -0300492 msg = msix_get_message(dev, vector);
493
Pavel Fedin38d40ff2015-05-27 15:59:59 +0300494 msi_send_message(dev, msg);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300495}
496
497void msix_reset(PCIDevice *dev)
498{
Jan Kiszka44701ab2012-06-04 16:53:48 +0200499 if (!msix_present(dev)) {
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300500 return;
Jan Kiszka44701ab2012-06-04 16:53:48 +0200501 }
Michael S. Tsirkin3cac0012012-08-29 19:40:56 +0300502 msix_clear_all_vectors(dev);
Michael S. Tsirkin27609522009-11-25 12:18:00 +0200503 dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &=
Paolo Bonzini7d374352018-12-13 23:37:37 +0100504 ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET];
Alex Williamsond35e4282012-06-14 12:16:37 -0600505 memset(dev->msix_table, 0, dev->msix_entries_nr * PCI_MSIX_ENTRY_SIZE);
506 memset(dev->msix_pba, 0, QEMU_ALIGN_UP(dev->msix_entries_nr, 64) / 8);
Michael S. Tsirkinae1be0b2009-11-25 11:41:48 +0200507 msix_mask_all(dev, dev->msix_entries_nr);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300508}
509
510/* PCI spec suggests that devices make it possible for software to configure
511 * less vectors than supported by the device, but does not specify a standard
512 * mechanism for devices to do so.
513 *
514 * We support this by asking devices to declare vectors software is going to
515 * actually use, and checking this on the notification path. Devices that
516 * don't want to follow the spec suggestion can declare all vectors as used. */
517
518/* Mark vector as used. */
519int msix_vector_use(PCIDevice *dev, unsigned vector)
520{
Cao jin93482432017-01-17 14:18:46 +0800521 if (vector >= dev->msix_entries_nr) {
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300522 return -EINVAL;
Cao jin93482432017-01-17 14:18:46 +0800523 }
524
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300525 dev->msix_entry_used[vector]++;
526 return 0;
527}
528
529/* Mark vector as unused. */
530void msix_vector_unuse(PCIDevice *dev, unsigned vector)
531{
Michael S. Tsirkin98304c82009-11-25 12:24:14 +0200532 if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
533 return;
534 }
535 if (--dev->msix_entry_used[vector]) {
536 return;
537 }
538 msix_clr_pending(dev, vector);
Michael S. Tsirkin02eb84d2009-06-21 19:49:54 +0300539}
Michael S. Tsirkinb5f28bc2009-11-24 16:44:15 +0200540
541void msix_unuse_all_vectors(PCIDevice *dev)
542{
Jan Kiszka44701ab2012-06-04 16:53:48 +0200543 if (!msix_present(dev)) {
Michael S. Tsirkinb5f28bc2009-11-24 16:44:15 +0200544 return;
Jan Kiszka44701ab2012-06-04 16:53:48 +0200545 }
Michael S. Tsirkinb5f28bc2009-11-24 16:44:15 +0200546 msix_free_irq_entries(dev);
547}
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300548
Jan Kiszkacb697aa2012-05-17 10:32:38 -0300549unsigned int msix_nr_vectors_allocated(const PCIDevice *dev)
550{
551 return dev->msix_entries_nr;
552}
553
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300554static int msix_set_notifier_for_vector(PCIDevice *dev, unsigned int vector)
555{
556 MSIMessage msg;
557
558 if (msix_is_masked(dev, vector)) {
559 return 0;
560 }
561 msg = msix_get_message(dev, vector);
562 return dev->msix_vector_use_notifier(dev, vector, msg);
563}
564
565static void msix_unset_notifier_for_vector(PCIDevice *dev, unsigned int vector)
566{
567 if (msix_is_masked(dev, vector)) {
568 return;
569 }
570 dev->msix_vector_release_notifier(dev, vector);
571}
572
573int msix_set_vector_notifiers(PCIDevice *dev,
574 MSIVectorUseNotifier use_notifier,
Michael S. Tsirkinbbef8822012-12-12 16:10:02 +0200575 MSIVectorReleaseNotifier release_notifier,
576 MSIVectorPollNotifier poll_notifier)
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300577{
578 int vector, ret;
579
580 assert(use_notifier && release_notifier);
581
582 dev->msix_vector_use_notifier = use_notifier;
583 dev->msix_vector_release_notifier = release_notifier;
Michael S. Tsirkinbbef8822012-12-12 16:10:02 +0200584 dev->msix_vector_poll_notifier = poll_notifier;
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300585
586 if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
587 (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) {
588 for (vector = 0; vector < dev->msix_entries_nr; vector++) {
589 ret = msix_set_notifier_for_vector(dev, vector);
590 if (ret < 0) {
591 goto undo;
592 }
593 }
594 }
Michael S. Tsirkinbbef8822012-12-12 16:10:02 +0200595 if (dev->msix_vector_poll_notifier) {
596 dev->msix_vector_poll_notifier(dev, 0, dev->msix_entries_nr);
597 }
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300598 return 0;
599
600undo:
601 while (--vector >= 0) {
602 msix_unset_notifier_for_vector(dev, vector);
603 }
604 dev->msix_vector_use_notifier = NULL;
605 dev->msix_vector_release_notifier = NULL;
606 return ret;
607}
608
609void msix_unset_vector_notifiers(PCIDevice *dev)
610{
611 int vector;
612
613 assert(dev->msix_vector_use_notifier &&
614 dev->msix_vector_release_notifier);
615
616 if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
617 (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) {
618 for (vector = 0; vector < dev->msix_entries_nr; vector++) {
619 msix_unset_notifier_for_vector(dev, vector);
620 }
621 }
622 dev->msix_vector_use_notifier = NULL;
623 dev->msix_vector_release_notifier = NULL;
Michael S. Tsirkinbbef8822012-12-12 16:10:02 +0200624 dev->msix_vector_poll_notifier = NULL;
Jan Kiszka2cdfe532012-05-17 10:32:31 -0300625}
Gerd Hoffmann340b50c2013-05-07 15:16:58 +0200626
Jianjun Duan2c21ee72017-01-19 11:00:50 -0800627static int put_msix_state(QEMUFile *f, void *pv, size_t size,
Marc-André Lureau03fee662018-11-14 17:29:30 +0400628 const VMStateField *field, QJSON *vmdesc)
Gerd Hoffmann340b50c2013-05-07 15:16:58 +0200629{
630 msix_save(pv, f);
Jianjun Duan2c21ee72017-01-19 11:00:50 -0800631
632 return 0;
Gerd Hoffmann340b50c2013-05-07 15:16:58 +0200633}
634
Jianjun Duan2c21ee72017-01-19 11:00:50 -0800635static int get_msix_state(QEMUFile *f, void *pv, size_t size,
Marc-André Lureau03fee662018-11-14 17:29:30 +0400636 const VMStateField *field)
Gerd Hoffmann340b50c2013-05-07 15:16:58 +0200637{
638 msix_load(pv, f);
639 return 0;
640}
641
642static VMStateInfo vmstate_info_msix = {
643 .name = "msix state",
644 .get = get_msix_state,
645 .put = put_msix_state,
646};
647
648const VMStateDescription vmstate_msix = {
649 .name = "msix",
650 .fields = (VMStateField[]) {
651 {
652 .name = "msix",
653 .version_id = 0,
654 .field_exists = NULL,
655 .size = 0, /* ouch */
656 .info = &vmstate_info_msix,
657 .flags = VMS_SINGLE,
658 .offset = 0,
659 },
660 VMSTATE_END_OF_LIST()
661 }
662};