xref: /qemu/hw/pci/msix.c (revision 8e5e0890)
1315a1350SMichael S. Tsirkin /*
2315a1350SMichael S. Tsirkin  * MSI-X device support
3315a1350SMichael S. Tsirkin  *
4315a1350SMichael S. Tsirkin  * This module includes support for MSI-X in pci devices.
5315a1350SMichael S. Tsirkin  *
6315a1350SMichael S. Tsirkin  * Author: Michael S. Tsirkin <mst@redhat.com>
7315a1350SMichael S. Tsirkin  *
8315a1350SMichael S. Tsirkin  *  Copyright (c) 2009, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
9315a1350SMichael S. Tsirkin  *
10315a1350SMichael S. Tsirkin  * This work is licensed under the terms of the GNU GPL, version 2.  See
11315a1350SMichael S. Tsirkin  * the COPYING file in the top-level directory.
12315a1350SMichael S. Tsirkin  *
13315a1350SMichael S. Tsirkin  * Contributions after 2012-01-13 are licensed under the terms of the
14315a1350SMichael S. Tsirkin  * GNU GPL, version 2 or (at your option) any later version.
15315a1350SMichael S. Tsirkin  */
16315a1350SMichael S. Tsirkin 
1797d5408fSPeter Maydell #include "qemu/osdep.h"
18c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h"
19c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h"
20c759b24fSMichael S. Tsirkin #include "hw/pci/pci.h"
21428c3eceSStefano Stabellini #include "hw/xen/xen.h"
22da278d58SPhilippe Mathieu-Daudé #include "sysemu/xen.h"
23ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h"
24d6454270SMarkus Armbruster #include "migration/vmstate.h"
251de7afc9SPaolo Bonzini #include "qemu/range.h"
26ee640c62SCao jin #include "qapi/error.h"
27993b1f4bSPeter Xu #include "trace.h"
28315a1350SMichael S. Tsirkin 
296096cf78SDavid Woodhouse #include "hw/i386/kvm/xen_evtchn.h"
306096cf78SDavid Woodhouse 
31315a1350SMichael S. Tsirkin /* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
32315a1350SMichael S. Tsirkin #define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1)
33315a1350SMichael S. Tsirkin #define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8)
34315a1350SMichael S. Tsirkin #define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8)
35315a1350SMichael S. Tsirkin 
msix_prepare_message(PCIDevice * dev,unsigned vector)3608cf3dc6SJagannathan Raman static MSIMessage msix_prepare_message(PCIDevice *dev, unsigned vector)
37315a1350SMichael S. Tsirkin {
38315a1350SMichael S. Tsirkin     uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE;
39315a1350SMichael S. Tsirkin     MSIMessage msg;
40315a1350SMichael S. Tsirkin 
41315a1350SMichael S. Tsirkin     msg.address = pci_get_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR);
42315a1350SMichael S. Tsirkin     msg.data = pci_get_long(table_entry + PCI_MSIX_ENTRY_DATA);
43315a1350SMichael S. Tsirkin     return msg;
44315a1350SMichael S. Tsirkin }
45315a1350SMichael S. Tsirkin 
msix_get_message(PCIDevice * dev,unsigned vector)4608cf3dc6SJagannathan Raman MSIMessage msix_get_message(PCIDevice *dev, unsigned vector)
4708cf3dc6SJagannathan Raman {
4808cf3dc6SJagannathan Raman     return dev->msix_prepare_message(dev, vector);
4908cf3dc6SJagannathan Raman }
5008cf3dc6SJagannathan Raman 
51315a1350SMichael S. Tsirkin /*
52315a1350SMichael S. Tsirkin  * Special API for POWER to configure the vectors through
53315a1350SMichael S. Tsirkin  * a side channel. Should never be used by devices.
54315a1350SMichael S. Tsirkin  */
msix_set_message(PCIDevice * dev,int vector,struct MSIMessage msg)55315a1350SMichael S. Tsirkin void msix_set_message(PCIDevice *dev, int vector, struct MSIMessage msg)
56315a1350SMichael S. Tsirkin {
57315a1350SMichael S. Tsirkin     uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE;
58315a1350SMichael S. Tsirkin 
59315a1350SMichael S. Tsirkin     pci_set_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR, msg.address);
60315a1350SMichael S. Tsirkin     pci_set_long(table_entry + PCI_MSIX_ENTRY_DATA, msg.data);
61315a1350SMichael S. Tsirkin     table_entry[PCI_MSIX_ENTRY_VECTOR_CTRL] &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
62315a1350SMichael S. Tsirkin }
63315a1350SMichael S. Tsirkin 
msix_pending_mask(int vector)64315a1350SMichael S. Tsirkin static uint8_t msix_pending_mask(int vector)
65315a1350SMichael S. Tsirkin {
66315a1350SMichael S. Tsirkin     return 1 << (vector % 8);
67315a1350SMichael S. Tsirkin }
68315a1350SMichael S. Tsirkin 
msix_pending_byte(PCIDevice * dev,int vector)69315a1350SMichael S. Tsirkin static uint8_t *msix_pending_byte(PCIDevice *dev, int vector)
70315a1350SMichael S. Tsirkin {
71315a1350SMichael S. Tsirkin     return dev->msix_pba + vector / 8;
72315a1350SMichael S. Tsirkin }
73315a1350SMichael S. Tsirkin 
msix_is_pending(PCIDevice * dev,int vector)74315a1350SMichael S. Tsirkin static int msix_is_pending(PCIDevice *dev, int vector)
75315a1350SMichael S. Tsirkin {
76315a1350SMichael S. Tsirkin     return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
77315a1350SMichael S. Tsirkin }
78315a1350SMichael S. Tsirkin 
msix_set_pending(PCIDevice * dev,unsigned int vector)7970f8ee39SMichael S. Tsirkin void msix_set_pending(PCIDevice *dev, unsigned int vector)
80315a1350SMichael S. Tsirkin {
81315a1350SMichael S. Tsirkin     *msix_pending_byte(dev, vector) |= msix_pending_mask(vector);
82315a1350SMichael S. Tsirkin }
83315a1350SMichael S. Tsirkin 
msix_clr_pending(PCIDevice * dev,int vector)843bdfaabbSDmitry Fleytman void msix_clr_pending(PCIDevice *dev, int vector)
85315a1350SMichael S. Tsirkin {
86315a1350SMichael S. Tsirkin     *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector);
87315a1350SMichael S. Tsirkin }
88315a1350SMichael S. Tsirkin 
msix_vector_masked(PCIDevice * dev,unsigned int vector,bool fmask)8970f8ee39SMichael S. Tsirkin static bool msix_vector_masked(PCIDevice *dev, unsigned int vector, bool fmask)
90315a1350SMichael S. Tsirkin {
91428c3eceSStefano Stabellini     unsigned offset = vector * PCI_MSIX_ENTRY_SIZE;
92e1e4bf22SMichael S. Tsirkin     uint8_t *data = &dev->msix_table[offset + PCI_MSIX_ENTRY_DATA];
93428c3eceSStefano Stabellini     /* MSIs on Xen can be remapped into pirqs. In those cases, masking
94428c3eceSStefano Stabellini      * and unmasking go through the PV evtchn path. */
95e1e4bf22SMichael S. Tsirkin     if (xen_enabled() && xen_is_pirq_msi(pci_get_long(data))) {
96428c3eceSStefano Stabellini         return false;
97428c3eceSStefano Stabellini     }
98428c3eceSStefano Stabellini     return fmask || dev->msix_table[offset + PCI_MSIX_ENTRY_VECTOR_CTRL] &
99428c3eceSStefano Stabellini         PCI_MSIX_ENTRY_CTRL_MASKBIT;
100315a1350SMichael S. Tsirkin }
101315a1350SMichael S. Tsirkin 
msix_is_masked(PCIDevice * dev,unsigned int vector)10270f8ee39SMichael S. Tsirkin bool msix_is_masked(PCIDevice *dev, unsigned int vector)
103315a1350SMichael S. Tsirkin {
104315a1350SMichael S. Tsirkin     return msix_vector_masked(dev, vector, dev->msix_function_masked);
105315a1350SMichael S. Tsirkin }
106315a1350SMichael S. Tsirkin 
msix_fire_vector_notifier(PCIDevice * dev,unsigned int vector,bool is_masked)107315a1350SMichael S. Tsirkin static void msix_fire_vector_notifier(PCIDevice *dev,
108315a1350SMichael S. Tsirkin                                       unsigned int vector, bool is_masked)
109315a1350SMichael S. Tsirkin {
110315a1350SMichael S. Tsirkin     MSIMessage msg;
111315a1350SMichael S. Tsirkin     int ret;
112315a1350SMichael S. Tsirkin 
113315a1350SMichael S. Tsirkin     if (!dev->msix_vector_use_notifier) {
114315a1350SMichael S. Tsirkin         return;
115315a1350SMichael S. Tsirkin     }
116315a1350SMichael S. Tsirkin     if (is_masked) {
117315a1350SMichael S. Tsirkin         dev->msix_vector_release_notifier(dev, vector);
118315a1350SMichael S. Tsirkin     } else {
119315a1350SMichael S. Tsirkin         msg = msix_get_message(dev, vector);
120315a1350SMichael S. Tsirkin         ret = dev->msix_vector_use_notifier(dev, vector, msg);
121315a1350SMichael S. Tsirkin         assert(ret >= 0);
122315a1350SMichael S. Tsirkin     }
123315a1350SMichael S. Tsirkin }
124315a1350SMichael S. Tsirkin 
msix_handle_mask_update(PCIDevice * dev,int vector,bool was_masked)125315a1350SMichael S. Tsirkin static void msix_handle_mask_update(PCIDevice *dev, int vector, bool was_masked)
126315a1350SMichael S. Tsirkin {
127315a1350SMichael S. Tsirkin     bool is_masked = msix_is_masked(dev, vector);
128315a1350SMichael S. Tsirkin 
1296096cf78SDavid Woodhouse     if (xen_mode == XEN_EMULATE) {
1306096cf78SDavid Woodhouse         MSIMessage msg = msix_prepare_message(dev, vector);
1316096cf78SDavid Woodhouse 
1326096cf78SDavid Woodhouse         xen_evtchn_snoop_msi(dev, true, vector, msg.address, msg.data,
1336096cf78SDavid Woodhouse                              is_masked);
1346096cf78SDavid Woodhouse     }
1356096cf78SDavid Woodhouse 
136315a1350SMichael S. Tsirkin     if (is_masked == was_masked) {
137315a1350SMichael S. Tsirkin         return;
138315a1350SMichael S. Tsirkin     }
139315a1350SMichael S. Tsirkin 
140315a1350SMichael S. Tsirkin     msix_fire_vector_notifier(dev, vector, is_masked);
141315a1350SMichael S. Tsirkin 
142315a1350SMichael S. Tsirkin     if (!is_masked && msix_is_pending(dev, vector)) {
143315a1350SMichael S. Tsirkin         msix_clr_pending(dev, vector);
144315a1350SMichael S. Tsirkin         msix_notify(dev, vector);
145315a1350SMichael S. Tsirkin     }
146315a1350SMichael S. Tsirkin }
147315a1350SMichael S. Tsirkin 
msix_set_mask(PCIDevice * dev,int vector,bool mask)14815377f6eSAkihiko Odaki void msix_set_mask(PCIDevice *dev, int vector, bool mask)
14908cf3dc6SJagannathan Raman {
15008cf3dc6SJagannathan Raman     unsigned offset;
15108cf3dc6SJagannathan Raman     bool was_masked;
15208cf3dc6SJagannathan Raman 
15315377f6eSAkihiko Odaki     assert(vector < dev->msix_entries_nr);
15408cf3dc6SJagannathan Raman 
15508cf3dc6SJagannathan Raman     offset = vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
15608cf3dc6SJagannathan Raman 
15708cf3dc6SJagannathan Raman     was_masked = msix_is_masked(dev, vector);
15808cf3dc6SJagannathan Raman 
15908cf3dc6SJagannathan Raman     if (mask) {
16008cf3dc6SJagannathan Raman         dev->msix_table[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
16108cf3dc6SJagannathan Raman     } else {
16208cf3dc6SJagannathan Raman         dev->msix_table[offset] &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
16308cf3dc6SJagannathan Raman     }
16408cf3dc6SJagannathan Raman 
16508cf3dc6SJagannathan Raman     msix_handle_mask_update(dev, vector, was_masked);
16608cf3dc6SJagannathan Raman }
16708cf3dc6SJagannathan Raman 
msix_masked(PCIDevice * dev)168993b1f4bSPeter Xu static bool msix_masked(PCIDevice *dev)
169993b1f4bSPeter Xu {
170993b1f4bSPeter Xu     return dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK;
171993b1f4bSPeter Xu }
172993b1f4bSPeter Xu 
msix_update_function_masked(PCIDevice * dev)173315a1350SMichael S. Tsirkin static void msix_update_function_masked(PCIDevice *dev)
174315a1350SMichael S. Tsirkin {
175993b1f4bSPeter Xu     dev->msix_function_masked = !msix_enabled(dev) || msix_masked(dev);
176315a1350SMichael S. Tsirkin }
177315a1350SMichael S. Tsirkin 
178315a1350SMichael S. Tsirkin /* Handle MSI-X capability config write. */
msix_write_config(PCIDevice * dev,uint32_t addr,uint32_t val,int len)179315a1350SMichael S. Tsirkin void msix_write_config(PCIDevice *dev, uint32_t addr,
180315a1350SMichael S. Tsirkin                        uint32_t val, int len)
181315a1350SMichael S. Tsirkin {
182315a1350SMichael S. Tsirkin     unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
183315a1350SMichael S. Tsirkin     int vector;
184315a1350SMichael S. Tsirkin     bool was_masked;
185315a1350SMichael S. Tsirkin 
186315a1350SMichael S. Tsirkin     if (!msix_present(dev) || !range_covers_byte(addr, len, enable_pos)) {
187315a1350SMichael S. Tsirkin         return;
188315a1350SMichael S. Tsirkin     }
189315a1350SMichael S. Tsirkin 
190993b1f4bSPeter Xu     trace_msix_write_config(dev->name, msix_enabled(dev), msix_masked(dev));
191993b1f4bSPeter Xu 
192315a1350SMichael S. Tsirkin     was_masked = dev->msix_function_masked;
193315a1350SMichael S. Tsirkin     msix_update_function_masked(dev);
194315a1350SMichael S. Tsirkin 
195315a1350SMichael S. Tsirkin     if (!msix_enabled(dev)) {
196315a1350SMichael S. Tsirkin         return;
197315a1350SMichael S. Tsirkin     }
198315a1350SMichael S. Tsirkin 
199315a1350SMichael S. Tsirkin     pci_device_deassert_intx(dev);
200315a1350SMichael S. Tsirkin 
201315a1350SMichael S. Tsirkin     if (dev->msix_function_masked == was_masked) {
202315a1350SMichael S. Tsirkin         return;
203315a1350SMichael S. Tsirkin     }
204315a1350SMichael S. Tsirkin 
205315a1350SMichael S. Tsirkin     for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
206315a1350SMichael S. Tsirkin         msix_handle_mask_update(dev, vector,
207315a1350SMichael S. Tsirkin                                 msix_vector_masked(dev, vector, was_masked));
208315a1350SMichael S. Tsirkin     }
209315a1350SMichael S. Tsirkin }
210315a1350SMichael S. Tsirkin 
msix_table_mmio_read(void * opaque,hwaddr addr,unsigned size)211315a1350SMichael S. Tsirkin static uint64_t msix_table_mmio_read(void *opaque, hwaddr addr,
212315a1350SMichael S. Tsirkin                                      unsigned size)
213315a1350SMichael S. Tsirkin {
214315a1350SMichael S. Tsirkin     PCIDevice *dev = opaque;
215315a1350SMichael S. Tsirkin 
21658cf0f86SPaolo Bonzini     assert(addr + size <= dev->msix_entries_nr * PCI_MSIX_ENTRY_SIZE);
217315a1350SMichael S. Tsirkin     return pci_get_long(dev->msix_table + addr);
218315a1350SMichael S. Tsirkin }
219315a1350SMichael S. Tsirkin 
msix_table_mmio_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)220315a1350SMichael S. Tsirkin static void msix_table_mmio_write(void *opaque, hwaddr addr,
221315a1350SMichael S. Tsirkin                                   uint64_t val, unsigned size)
222315a1350SMichael S. Tsirkin {
223315a1350SMichael S. Tsirkin     PCIDevice *dev = opaque;
224315a1350SMichael S. Tsirkin     int vector = addr / PCI_MSIX_ENTRY_SIZE;
225315a1350SMichael S. Tsirkin     bool was_masked;
226315a1350SMichael S. Tsirkin 
22758cf0f86SPaolo Bonzini     assert(addr + size <= dev->msix_entries_nr * PCI_MSIX_ENTRY_SIZE);
22858cf0f86SPaolo Bonzini 
229315a1350SMichael S. Tsirkin     was_masked = msix_is_masked(dev, vector);
230315a1350SMichael S. Tsirkin     pci_set_long(dev->msix_table + addr, val);
231315a1350SMichael S. Tsirkin     msix_handle_mask_update(dev, vector, was_masked);
232315a1350SMichael S. Tsirkin }
233315a1350SMichael S. Tsirkin 
234315a1350SMichael S. Tsirkin static const MemoryRegionOps msix_table_mmio_ops = {
235315a1350SMichael S. Tsirkin     .read = msix_table_mmio_read,
236315a1350SMichael S. Tsirkin     .write = msix_table_mmio_write,
2376f991980SPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
238315a1350SMichael S. Tsirkin     .valid = {
239315a1350SMichael S. Tsirkin         .min_access_size = 4,
240191f90cbSMichael S. Tsirkin         .max_access_size = 8,
241191f90cbSMichael S. Tsirkin     },
242191f90cbSMichael S. Tsirkin     .impl = {
243315a1350SMichael S. Tsirkin         .max_access_size = 4,
244315a1350SMichael S. Tsirkin     },
245315a1350SMichael S. Tsirkin };
246315a1350SMichael S. Tsirkin 
msix_pba_mmio_read(void * opaque,hwaddr addr,unsigned size)247315a1350SMichael S. Tsirkin static uint64_t msix_pba_mmio_read(void *opaque, hwaddr addr,
248315a1350SMichael S. Tsirkin                                    unsigned size)
249315a1350SMichael S. Tsirkin {
250315a1350SMichael S. Tsirkin     PCIDevice *dev = opaque;
251bbef882cSMichael S. Tsirkin     if (dev->msix_vector_poll_notifier) {
252bbef882cSMichael S. Tsirkin         unsigned vector_start = addr * 8;
253bbef882cSMichael S. Tsirkin         unsigned vector_end = MIN(addr + size * 8, dev->msix_entries_nr);
254bbef882cSMichael S. Tsirkin         dev->msix_vector_poll_notifier(dev, vector_start, vector_end);
255bbef882cSMichael S. Tsirkin     }
256315a1350SMichael S. Tsirkin 
257315a1350SMichael S. Tsirkin     return pci_get_long(dev->msix_pba + addr);
258315a1350SMichael S. Tsirkin }
259315a1350SMichael S. Tsirkin 
msix_pba_mmio_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)26043b11a91SMarc-André Lureau static void msix_pba_mmio_write(void *opaque, hwaddr addr,
26143b11a91SMarc-André Lureau                                 uint64_t val, unsigned size)
26243b11a91SMarc-André Lureau {
26343b11a91SMarc-André Lureau }
26443b11a91SMarc-André Lureau 
265315a1350SMichael S. Tsirkin static const MemoryRegionOps msix_pba_mmio_ops = {
266315a1350SMichael S. Tsirkin     .read = msix_pba_mmio_read,
26743b11a91SMarc-André Lureau     .write = msix_pba_mmio_write,
2686f991980SPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
269315a1350SMichael S. Tsirkin     .valid = {
270315a1350SMichael S. Tsirkin         .min_access_size = 4,
271191f90cbSMichael S. Tsirkin         .max_access_size = 8,
272191f90cbSMichael S. Tsirkin     },
273191f90cbSMichael S. Tsirkin     .impl = {
274315a1350SMichael S. Tsirkin         .max_access_size = 4,
275315a1350SMichael S. Tsirkin     },
276315a1350SMichael S. Tsirkin };
277315a1350SMichael S. Tsirkin 
msix_mask_all(struct PCIDevice * dev,unsigned nentries)278315a1350SMichael S. Tsirkin static void msix_mask_all(struct PCIDevice *dev, unsigned nentries)
279315a1350SMichael S. Tsirkin {
280315a1350SMichael S. Tsirkin     int vector;
281315a1350SMichael S. Tsirkin 
282315a1350SMichael S. Tsirkin     for (vector = 0; vector < nentries; ++vector) {
283315a1350SMichael S. Tsirkin         unsigned offset =
284315a1350SMichael S. Tsirkin             vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
285315a1350SMichael S. Tsirkin         bool was_masked = msix_is_masked(dev, vector);
286315a1350SMichael S. Tsirkin 
287315a1350SMichael S. Tsirkin         dev->msix_table[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
288315a1350SMichael S. Tsirkin         msix_handle_mask_update(dev, vector, was_masked);
289315a1350SMichael S. Tsirkin     }
290315a1350SMichael S. Tsirkin }
291315a1350SMichael S. Tsirkin 
292ee640c62SCao jin /*
293ee640c62SCao jin  * Make PCI device @dev MSI-X capable
294ee640c62SCao jin  * @nentries is the max number of MSI-X vectors that the device support.
295ee640c62SCao jin  * @table_bar is the MemoryRegion that MSI-X table structure resides.
296ee640c62SCao jin  * @table_bar_nr is number of base address register corresponding to @table_bar.
297ee640c62SCao jin  * @table_offset indicates the offset that the MSI-X table structure starts with
298ee640c62SCao jin  * in @table_bar.
299ee640c62SCao jin  * @pba_bar is the MemoryRegion that the Pending Bit Array structure resides.
300ee640c62SCao jin  * @pba_bar_nr is number of base address register corresponding to @pba_bar.
301ee640c62SCao jin  * @pba_offset indicates the offset that the Pending Bit Array structure
302ee640c62SCao jin  * starts with in @pba_bar.
303ee640c62SCao jin  * Non-zero @cap_pos puts capability MSI-X at that offset in PCI config space.
304ee640c62SCao jin  * @errp is for returning errors.
305ee640c62SCao jin  *
306ee640c62SCao jin  * Return 0 on success; set @errp and return -errno on error:
307ee640c62SCao jin  * -ENOTSUP means lacking msi support for a msi-capable platform.
308ee640c62SCao jin  * -EINVAL means capability overlap, happens when @cap_pos is non-zero,
309ee640c62SCao jin  * also means a programming error, except device assignment, which can check
310ee640c62SCao jin  * if a real HW is broken.
311ee640c62SCao jin  */
msix_init(struct PCIDevice * dev,unsigned short nentries,MemoryRegion * table_bar,uint8_t table_bar_nr,unsigned table_offset,MemoryRegion * pba_bar,uint8_t pba_bar_nr,unsigned pba_offset,uint8_t cap_pos,Error ** errp)312315a1350SMichael S. Tsirkin int msix_init(struct PCIDevice *dev, unsigned short nentries,
313315a1350SMichael S. Tsirkin               MemoryRegion *table_bar, uint8_t table_bar_nr,
314315a1350SMichael S. Tsirkin               unsigned table_offset, MemoryRegion *pba_bar,
315ee640c62SCao jin               uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos,
316ee640c62SCao jin               Error **errp)
317315a1350SMichael S. Tsirkin {
318315a1350SMichael S. Tsirkin     int cap;
319315a1350SMichael S. Tsirkin     unsigned table_size, pba_size;
320315a1350SMichael S. Tsirkin     uint8_t *config;
321315a1350SMichael S. Tsirkin 
322315a1350SMichael S. Tsirkin     /* Nothing to do if MSI is not supported by interrupt controller */
323226419d6SMichael S. Tsirkin     if (!msi_nonbroken) {
324ee640c62SCao jin         error_setg(errp, "MSI-X is not supported by interrupt controller");
325315a1350SMichael S. Tsirkin         return -ENOTSUP;
326315a1350SMichael S. Tsirkin     }
327315a1350SMichael S. Tsirkin 
328315a1350SMichael S. Tsirkin     if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1) {
329ee640c62SCao jin         error_setg(errp, "The number of MSI-X vectors is invalid");
330315a1350SMichael S. Tsirkin         return -EINVAL;
331315a1350SMichael S. Tsirkin     }
332315a1350SMichael S. Tsirkin 
333315a1350SMichael S. Tsirkin     table_size = nentries * PCI_MSIX_ENTRY_SIZE;
334315a1350SMichael S. Tsirkin     pba_size = QEMU_ALIGN_UP(nentries, 64) / 8;
335315a1350SMichael S. Tsirkin 
336315a1350SMichael S. Tsirkin     /* Sanity test: table & pba don't overlap, fit within BARs, min aligned */
337315a1350SMichael S. Tsirkin     if ((table_bar_nr == pba_bar_nr &&
338315a1350SMichael S. Tsirkin          ranges_overlap(table_offset, table_size, pba_offset, pba_size)) ||
339315a1350SMichael S. Tsirkin         table_offset + table_size > memory_region_size(table_bar) ||
340315a1350SMichael S. Tsirkin         pba_offset + pba_size > memory_region_size(pba_bar) ||
341315a1350SMichael S. Tsirkin         (table_offset | pba_offset) & PCI_MSIX_FLAGS_BIRMASK) {
342ee640c62SCao jin         error_setg(errp, "table & pba overlap, or they don't fit in BARs,"
343ee640c62SCao jin                    " or don't align");
344315a1350SMichael S. Tsirkin         return -EINVAL;
345315a1350SMichael S. Tsirkin     }
346315a1350SMichael S. Tsirkin 
34727841278SMao Zhongyi     cap = pci_add_capability(dev, PCI_CAP_ID_MSIX,
348ee640c62SCao jin                               cap_pos, MSIX_CAP_LENGTH, errp);
349315a1350SMichael S. Tsirkin     if (cap < 0) {
350315a1350SMichael S. Tsirkin         return cap;
351315a1350SMichael S. Tsirkin     }
352315a1350SMichael S. Tsirkin 
353315a1350SMichael S. Tsirkin     dev->msix_cap = cap;
354315a1350SMichael S. Tsirkin     dev->cap_present |= QEMU_PCI_CAP_MSIX;
355315a1350SMichael S. Tsirkin     config = dev->config + cap;
356315a1350SMichael S. Tsirkin 
357315a1350SMichael S. Tsirkin     pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
358315a1350SMichael S. Tsirkin     dev->msix_entries_nr = nentries;
359315a1350SMichael S. Tsirkin     dev->msix_function_masked = true;
360315a1350SMichael S. Tsirkin 
361315a1350SMichael S. Tsirkin     pci_set_long(config + PCI_MSIX_TABLE, table_offset | table_bar_nr);
362315a1350SMichael S. Tsirkin     pci_set_long(config + PCI_MSIX_PBA, pba_offset | pba_bar_nr);
363315a1350SMichael S. Tsirkin 
364315a1350SMichael S. Tsirkin     /* Make flags bit writable. */
365315a1350SMichael S. Tsirkin     dev->wmask[cap + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK |
366315a1350SMichael S. Tsirkin                                              MSIX_MASKALL_MASK;
367315a1350SMichael S. Tsirkin 
368315a1350SMichael S. Tsirkin     dev->msix_table = g_malloc0(table_size);
369315a1350SMichael S. Tsirkin     dev->msix_pba = g_malloc0(pba_size);
370315a1350SMichael S. Tsirkin     dev->msix_entry_used = g_malloc0(nentries * sizeof *dev->msix_entry_used);
371315a1350SMichael S. Tsirkin 
372315a1350SMichael S. Tsirkin     msix_mask_all(dev, nentries);
373315a1350SMichael S. Tsirkin 
37440c5dce9SPaolo Bonzini     memory_region_init_io(&dev->msix_table_mmio, OBJECT(dev), &msix_table_mmio_ops, dev,
375315a1350SMichael S. Tsirkin                           "msix-table", table_size);
376315a1350SMichael S. Tsirkin     memory_region_add_subregion(table_bar, table_offset, &dev->msix_table_mmio);
37740c5dce9SPaolo Bonzini     memory_region_init_io(&dev->msix_pba_mmio, OBJECT(dev), &msix_pba_mmio_ops, dev,
378315a1350SMichael S. Tsirkin                           "msix-pba", pba_size);
379315a1350SMichael S. Tsirkin     memory_region_add_subregion(pba_bar, pba_offset, &dev->msix_pba_mmio);
380315a1350SMichael S. Tsirkin 
38108cf3dc6SJagannathan Raman     dev->msix_prepare_message = msix_prepare_message;
38208cf3dc6SJagannathan Raman 
383315a1350SMichael S. Tsirkin     return 0;
384315a1350SMichael S. Tsirkin }
385315a1350SMichael S. Tsirkin 
msix_init_exclusive_bar(PCIDevice * dev,unsigned short nentries,uint8_t bar_nr,Error ** errp)386315a1350SMichael S. Tsirkin int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries,
387ee640c62SCao jin                             uint8_t bar_nr, Error **errp)
388315a1350SMichael S. Tsirkin {
389315a1350SMichael S. Tsirkin     int ret;
390315a1350SMichael S. Tsirkin     char *name;
391a0ccd212SJason Wang     uint32_t bar_size = 4096;
392a0ccd212SJason Wang     uint32_t bar_pba_offset = bar_size / 2;
39317323e8bSDongli Zhang     uint32_t bar_pba_size = QEMU_ALIGN_UP(nentries, 64) / 8;
394315a1350SMichael S. Tsirkin 
395315a1350SMichael S. Tsirkin     /*
396315a1350SMichael S. Tsirkin      * Migration compatibility dictates that this remains a 4k
397315a1350SMichael S. Tsirkin      * BAR with the vector table in the lower half and PBA in
398a0ccd212SJason Wang      * the upper half for nentries which is lower or equal to 128.
399a0ccd212SJason Wang      * No need to care about using more than 65 entries for legacy
400a0ccd212SJason Wang      * machine types who has at most 64 queues.
401315a1350SMichael S. Tsirkin      */
402a0ccd212SJason Wang     if (nentries * PCI_MSIX_ENTRY_SIZE > bar_pba_offset) {
403a0ccd212SJason Wang         bar_pba_offset = nentries * PCI_MSIX_ENTRY_SIZE;
404a0ccd212SJason Wang     }
405315a1350SMichael S. Tsirkin 
406a0ccd212SJason Wang     if (bar_pba_offset + bar_pba_size > 4096) {
407a0ccd212SJason Wang         bar_size = bar_pba_offset + bar_pba_size;
408a0ccd212SJason Wang     }
409a0ccd212SJason Wang 
4109bff5d81SPeter Maydell     bar_size = pow2ceil(bar_size);
411315a1350SMichael S. Tsirkin 
412315a1350SMichael S. Tsirkin     name = g_strdup_printf("%s-msix", dev->name);
413a0ccd212SJason Wang     memory_region_init(&dev->msix_exclusive_bar, OBJECT(dev), name, bar_size);
414315a1350SMichael S. Tsirkin     g_free(name);
415315a1350SMichael S. Tsirkin 
416315a1350SMichael S. Tsirkin     ret = msix_init(dev, nentries, &dev->msix_exclusive_bar, bar_nr,
417a0ccd212SJason Wang                     0, &dev->msix_exclusive_bar,
418a0ccd212SJason Wang                     bar_nr, bar_pba_offset,
419ee640c62SCao jin                     0, errp);
420315a1350SMichael S. Tsirkin     if (ret) {
421315a1350SMichael S. Tsirkin         return ret;
422315a1350SMichael S. Tsirkin     }
423315a1350SMichael S. Tsirkin 
424315a1350SMichael S. Tsirkin     pci_register_bar(dev, bar_nr, PCI_BASE_ADDRESS_SPACE_MEMORY,
425315a1350SMichael S. Tsirkin                      &dev->msix_exclusive_bar);
426315a1350SMichael S. Tsirkin 
427315a1350SMichael S. Tsirkin     return 0;
428315a1350SMichael S. Tsirkin }
429315a1350SMichael S. Tsirkin 
msix_free_irq_entries(PCIDevice * dev)430315a1350SMichael S. Tsirkin static void msix_free_irq_entries(PCIDevice *dev)
431315a1350SMichael S. Tsirkin {
432315a1350SMichael S. Tsirkin     int vector;
433315a1350SMichael S. Tsirkin 
434315a1350SMichael S. Tsirkin     for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
435315a1350SMichael S. Tsirkin         dev->msix_entry_used[vector] = 0;
436315a1350SMichael S. Tsirkin         msix_clr_pending(dev, vector);
437315a1350SMichael S. Tsirkin     }
438315a1350SMichael S. Tsirkin }
439315a1350SMichael S. Tsirkin 
msix_clear_all_vectors(PCIDevice * dev)440315a1350SMichael S. Tsirkin static void msix_clear_all_vectors(PCIDevice *dev)
441315a1350SMichael S. Tsirkin {
442315a1350SMichael S. Tsirkin     int vector;
443315a1350SMichael S. Tsirkin 
444315a1350SMichael S. Tsirkin     for (vector = 0; vector < dev->msix_entries_nr; ++vector) {
445315a1350SMichael S. Tsirkin         msix_clr_pending(dev, vector);
446315a1350SMichael S. Tsirkin     }
447315a1350SMichael S. Tsirkin }
448315a1350SMichael S. Tsirkin 
449315a1350SMichael S. Tsirkin /* Clean up resources for the device. */
msix_uninit(PCIDevice * dev,MemoryRegion * table_bar,MemoryRegion * pba_bar)450315a1350SMichael S. Tsirkin void msix_uninit(PCIDevice *dev, MemoryRegion *table_bar, MemoryRegion *pba_bar)
451315a1350SMichael S. Tsirkin {
452315a1350SMichael S. Tsirkin     if (!msix_present(dev)) {
453315a1350SMichael S. Tsirkin         return;
454315a1350SMichael S. Tsirkin     }
455315a1350SMichael S. Tsirkin     pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
456315a1350SMichael S. Tsirkin     dev->msix_cap = 0;
457315a1350SMichael S. Tsirkin     msix_free_irq_entries(dev);
458315a1350SMichael S. Tsirkin     dev->msix_entries_nr = 0;
459315a1350SMichael S. Tsirkin     memory_region_del_subregion(pba_bar, &dev->msix_pba_mmio);
460315a1350SMichael S. Tsirkin     g_free(dev->msix_pba);
461315a1350SMichael S. Tsirkin     dev->msix_pba = NULL;
462315a1350SMichael S. Tsirkin     memory_region_del_subregion(table_bar, &dev->msix_table_mmio);
463315a1350SMichael S. Tsirkin     g_free(dev->msix_table);
464315a1350SMichael S. Tsirkin     dev->msix_table = NULL;
465315a1350SMichael S. Tsirkin     g_free(dev->msix_entry_used);
466315a1350SMichael S. Tsirkin     dev->msix_entry_used = NULL;
467315a1350SMichael S. Tsirkin     dev->cap_present &= ~QEMU_PCI_CAP_MSIX;
46808cf3dc6SJagannathan Raman     dev->msix_prepare_message = NULL;
469315a1350SMichael S. Tsirkin }
470315a1350SMichael S. Tsirkin 
msix_uninit_exclusive_bar(PCIDevice * dev)471315a1350SMichael S. Tsirkin void msix_uninit_exclusive_bar(PCIDevice *dev)
472315a1350SMichael S. Tsirkin {
473315a1350SMichael S. Tsirkin     if (msix_present(dev)) {
474315a1350SMichael S. Tsirkin         msix_uninit(dev, &dev->msix_exclusive_bar, &dev->msix_exclusive_bar);
475315a1350SMichael S. Tsirkin     }
476315a1350SMichael S. Tsirkin }
477315a1350SMichael S. Tsirkin 
msix_save(PCIDevice * dev,QEMUFile * f)478315a1350SMichael S. Tsirkin void msix_save(PCIDevice *dev, QEMUFile *f)
479315a1350SMichael S. Tsirkin {
480315a1350SMichael S. Tsirkin     unsigned n = dev->msix_entries_nr;
481315a1350SMichael S. Tsirkin 
482315a1350SMichael S. Tsirkin     if (!msix_present(dev)) {
483315a1350SMichael S. Tsirkin         return;
484315a1350SMichael S. Tsirkin     }
485315a1350SMichael S. Tsirkin 
486315a1350SMichael S. Tsirkin     qemu_put_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE);
4870ef1efcfSMarc-André Lureau     qemu_put_buffer(f, dev->msix_pba, DIV_ROUND_UP(n, 8));
488315a1350SMichael S. Tsirkin }
489315a1350SMichael S. Tsirkin 
490315a1350SMichael S. Tsirkin /* Should be called after restoring the config space. */
msix_load(PCIDevice * dev,QEMUFile * f)491315a1350SMichael S. Tsirkin void msix_load(PCIDevice *dev, QEMUFile *f)
492315a1350SMichael S. Tsirkin {
493315a1350SMichael S. Tsirkin     unsigned n = dev->msix_entries_nr;
494315a1350SMichael S. Tsirkin     unsigned int vector;
495315a1350SMichael S. Tsirkin 
496315a1350SMichael S. Tsirkin     if (!msix_present(dev)) {
497315a1350SMichael S. Tsirkin         return;
498315a1350SMichael S. Tsirkin     }
499315a1350SMichael S. Tsirkin 
500315a1350SMichael S. Tsirkin     msix_clear_all_vectors(dev);
501315a1350SMichael S. Tsirkin     qemu_get_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE);
5020ef1efcfSMarc-André Lureau     qemu_get_buffer(f, dev->msix_pba, DIV_ROUND_UP(n, 8));
503315a1350SMichael S. Tsirkin     msix_update_function_masked(dev);
504315a1350SMichael S. Tsirkin 
505315a1350SMichael S. Tsirkin     for (vector = 0; vector < n; vector++) {
506315a1350SMichael S. Tsirkin         msix_handle_mask_update(dev, vector, true);
507315a1350SMichael S. Tsirkin     }
508315a1350SMichael S. Tsirkin }
509315a1350SMichael S. Tsirkin 
510315a1350SMichael S. Tsirkin /* Does device support MSI-X? */
msix_present(PCIDevice * dev)511315a1350SMichael S. Tsirkin int msix_present(PCIDevice *dev)
512315a1350SMichael S. Tsirkin {
513315a1350SMichael S. Tsirkin     return dev->cap_present & QEMU_PCI_CAP_MSIX;
514315a1350SMichael S. Tsirkin }
515315a1350SMichael S. Tsirkin 
516315a1350SMichael S. Tsirkin /* Is MSI-X enabled? */
msix_enabled(PCIDevice * dev)517315a1350SMichael S. Tsirkin int msix_enabled(PCIDevice *dev)
518315a1350SMichael S. Tsirkin {
519315a1350SMichael S. Tsirkin     return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
520315a1350SMichael S. Tsirkin         (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
521315a1350SMichael S. Tsirkin          MSIX_ENABLE_MASK);
522315a1350SMichael S. Tsirkin }
523315a1350SMichael S. Tsirkin 
524315a1350SMichael S. Tsirkin /* Send an MSI-X message */
msix_notify(PCIDevice * dev,unsigned vector)525315a1350SMichael S. Tsirkin void msix_notify(PCIDevice *dev, unsigned vector)
526315a1350SMichael S. Tsirkin {
527315a1350SMichael S. Tsirkin     MSIMessage msg;
528315a1350SMichael S. Tsirkin 
52915377f6eSAkihiko Odaki     assert(vector < dev->msix_entries_nr);
53015377f6eSAkihiko Odaki 
53115377f6eSAkihiko Odaki     if (!dev->msix_entry_used[vector]) {
532315a1350SMichael S. Tsirkin         return;
53393482436SCao jin     }
53493482436SCao jin 
535315a1350SMichael S. Tsirkin     if (msix_is_masked(dev, vector)) {
536315a1350SMichael S. Tsirkin         msix_set_pending(dev, vector);
537315a1350SMichael S. Tsirkin         return;
538315a1350SMichael S. Tsirkin     }
539315a1350SMichael S. Tsirkin 
540315a1350SMichael S. Tsirkin     msg = msix_get_message(dev, vector);
541315a1350SMichael S. Tsirkin 
54238d40ff1SPavel Fedin     msi_send_message(dev, msg);
543315a1350SMichael S. Tsirkin }
544315a1350SMichael S. Tsirkin 
msix_reset(PCIDevice * dev)545315a1350SMichael S. Tsirkin void msix_reset(PCIDevice *dev)
546315a1350SMichael S. Tsirkin {
547315a1350SMichael S. Tsirkin     if (!msix_present(dev)) {
548315a1350SMichael S. Tsirkin         return;
549315a1350SMichael S. Tsirkin     }
550315a1350SMichael S. Tsirkin     msix_clear_all_vectors(dev);
551315a1350SMichael S. Tsirkin     dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &=
552315a1350SMichael S. Tsirkin             ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET];
553315a1350SMichael S. Tsirkin     memset(dev->msix_table, 0, dev->msix_entries_nr * PCI_MSIX_ENTRY_SIZE);
554315a1350SMichael S. Tsirkin     memset(dev->msix_pba, 0, QEMU_ALIGN_UP(dev->msix_entries_nr, 64) / 8);
555315a1350SMichael S. Tsirkin     msix_mask_all(dev, dev->msix_entries_nr);
556315a1350SMichael S. Tsirkin }
557315a1350SMichael S. Tsirkin 
558315a1350SMichael S. Tsirkin /* PCI spec suggests that devices make it possible for software to configure
559315a1350SMichael S. Tsirkin  * less vectors than supported by the device, but does not specify a standard
560315a1350SMichael S. Tsirkin  * mechanism for devices to do so.
561315a1350SMichael S. Tsirkin  *
562315a1350SMichael S. Tsirkin  * We support this by asking devices to declare vectors software is going to
563315a1350SMichael S. Tsirkin  * actually use, and checking this on the notification path. Devices that
564315a1350SMichael S. Tsirkin  * don't want to follow the spec suggestion can declare all vectors as used. */
565315a1350SMichael S. Tsirkin 
566315a1350SMichael S. Tsirkin /* Mark vector as used. */
msix_vector_use(PCIDevice * dev,unsigned vector)56715377f6eSAkihiko Odaki void msix_vector_use(PCIDevice *dev, unsigned vector)
568315a1350SMichael S. Tsirkin {
56915377f6eSAkihiko Odaki     assert(vector < dev->msix_entries_nr);
570315a1350SMichael S. Tsirkin     dev->msix_entry_used[vector]++;
571315a1350SMichael S. Tsirkin }
572315a1350SMichael S. Tsirkin 
573315a1350SMichael S. Tsirkin /* Mark vector as unused. */
msix_vector_unuse(PCIDevice * dev,unsigned vector)574315a1350SMichael S. Tsirkin void msix_vector_unuse(PCIDevice *dev, unsigned vector)
575315a1350SMichael S. Tsirkin {
57615377f6eSAkihiko Odaki     assert(vector < dev->msix_entries_nr);
57715377f6eSAkihiko Odaki     if (!dev->msix_entry_used[vector]) {
578315a1350SMichael S. Tsirkin         return;
579315a1350SMichael S. Tsirkin     }
580315a1350SMichael S. Tsirkin     if (--dev->msix_entry_used[vector]) {
581315a1350SMichael S. Tsirkin         return;
582315a1350SMichael S. Tsirkin     }
583315a1350SMichael S. Tsirkin     msix_clr_pending(dev, vector);
584315a1350SMichael S. Tsirkin }
585315a1350SMichael S. Tsirkin 
msix_unuse_all_vectors(PCIDevice * dev)586315a1350SMichael S. Tsirkin void msix_unuse_all_vectors(PCIDevice *dev)
587315a1350SMichael S. Tsirkin {
588315a1350SMichael S. Tsirkin     if (!msix_present(dev)) {
589315a1350SMichael S. Tsirkin         return;
590315a1350SMichael S. Tsirkin     }
591315a1350SMichael S. Tsirkin     msix_free_irq_entries(dev);
592315a1350SMichael S. Tsirkin }
593315a1350SMichael S. Tsirkin 
msix_nr_vectors_allocated(const PCIDevice * dev)594315a1350SMichael S. Tsirkin unsigned int msix_nr_vectors_allocated(const PCIDevice *dev)
595315a1350SMichael S. Tsirkin {
596315a1350SMichael S. Tsirkin     return dev->msix_entries_nr;
597315a1350SMichael S. Tsirkin }
598315a1350SMichael S. Tsirkin 
msix_set_notifier_for_vector(PCIDevice * dev,unsigned int vector)599315a1350SMichael S. Tsirkin static int msix_set_notifier_for_vector(PCIDevice *dev, unsigned int vector)
600315a1350SMichael S. Tsirkin {
601315a1350SMichael S. Tsirkin     MSIMessage msg;
602315a1350SMichael S. Tsirkin 
603315a1350SMichael S. Tsirkin     if (msix_is_masked(dev, vector)) {
604315a1350SMichael S. Tsirkin         return 0;
605315a1350SMichael S. Tsirkin     }
606315a1350SMichael S. Tsirkin     msg = msix_get_message(dev, vector);
607315a1350SMichael S. Tsirkin     return dev->msix_vector_use_notifier(dev, vector, msg);
608315a1350SMichael S. Tsirkin }
609315a1350SMichael S. Tsirkin 
msix_unset_notifier_for_vector(PCIDevice * dev,unsigned int vector)610315a1350SMichael S. Tsirkin static void msix_unset_notifier_for_vector(PCIDevice *dev, unsigned int vector)
611315a1350SMichael S. Tsirkin {
612315a1350SMichael S. Tsirkin     if (msix_is_masked(dev, vector)) {
613315a1350SMichael S. Tsirkin         return;
614315a1350SMichael S. Tsirkin     }
615315a1350SMichael S. Tsirkin     dev->msix_vector_release_notifier(dev, vector);
616315a1350SMichael S. Tsirkin }
617315a1350SMichael S. Tsirkin 
msix_set_vector_notifiers(PCIDevice * dev,MSIVectorUseNotifier use_notifier,MSIVectorReleaseNotifier release_notifier,MSIVectorPollNotifier poll_notifier)618315a1350SMichael S. Tsirkin int msix_set_vector_notifiers(PCIDevice *dev,
619315a1350SMichael S. Tsirkin                               MSIVectorUseNotifier use_notifier,
620bbef882cSMichael S. Tsirkin                               MSIVectorReleaseNotifier release_notifier,
621bbef882cSMichael S. Tsirkin                               MSIVectorPollNotifier poll_notifier)
622315a1350SMichael S. Tsirkin {
623315a1350SMichael S. Tsirkin     int vector, ret;
624315a1350SMichael S. Tsirkin 
625315a1350SMichael S. Tsirkin     assert(use_notifier && release_notifier);
626315a1350SMichael S. Tsirkin 
627315a1350SMichael S. Tsirkin     dev->msix_vector_use_notifier = use_notifier;
628315a1350SMichael S. Tsirkin     dev->msix_vector_release_notifier = release_notifier;
629bbef882cSMichael S. Tsirkin     dev->msix_vector_poll_notifier = poll_notifier;
630315a1350SMichael S. Tsirkin 
631315a1350SMichael S. Tsirkin     if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
632315a1350SMichael S. Tsirkin         (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) {
633315a1350SMichael S. Tsirkin         for (vector = 0; vector < dev->msix_entries_nr; vector++) {
634315a1350SMichael S. Tsirkin             ret = msix_set_notifier_for_vector(dev, vector);
635315a1350SMichael S. Tsirkin             if (ret < 0) {
636315a1350SMichael S. Tsirkin                 goto undo;
637315a1350SMichael S. Tsirkin             }
638315a1350SMichael S. Tsirkin         }
639315a1350SMichael S. Tsirkin     }
640bbef882cSMichael S. Tsirkin     if (dev->msix_vector_poll_notifier) {
641bbef882cSMichael S. Tsirkin         dev->msix_vector_poll_notifier(dev, 0, dev->msix_entries_nr);
642bbef882cSMichael S. Tsirkin     }
643315a1350SMichael S. Tsirkin     return 0;
644315a1350SMichael S. Tsirkin 
645315a1350SMichael S. Tsirkin undo:
646315a1350SMichael S. Tsirkin     while (--vector >= 0) {
647315a1350SMichael S. Tsirkin         msix_unset_notifier_for_vector(dev, vector);
648315a1350SMichael S. Tsirkin     }
649315a1350SMichael S. Tsirkin     dev->msix_vector_use_notifier = NULL;
650315a1350SMichael S. Tsirkin     dev->msix_vector_release_notifier = NULL;
6512d37fe9eSRobert Hoo     dev->msix_vector_poll_notifier = NULL;
652315a1350SMichael S. Tsirkin     return ret;
653315a1350SMichael S. Tsirkin }
654315a1350SMichael S. Tsirkin 
msix_unset_vector_notifiers(PCIDevice * dev)655315a1350SMichael S. Tsirkin void msix_unset_vector_notifiers(PCIDevice *dev)
656315a1350SMichael S. Tsirkin {
657315a1350SMichael S. Tsirkin     int vector;
658315a1350SMichael S. Tsirkin 
659315a1350SMichael S. Tsirkin     assert(dev->msix_vector_use_notifier &&
660315a1350SMichael S. Tsirkin            dev->msix_vector_release_notifier);
661315a1350SMichael S. Tsirkin 
662315a1350SMichael S. Tsirkin     if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
663315a1350SMichael S. Tsirkin         (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) {
664315a1350SMichael S. Tsirkin         for (vector = 0; vector < dev->msix_entries_nr; vector++) {
665315a1350SMichael S. Tsirkin             msix_unset_notifier_for_vector(dev, vector);
666315a1350SMichael S. Tsirkin         }
667315a1350SMichael S. Tsirkin     }
668315a1350SMichael S. Tsirkin     dev->msix_vector_use_notifier = NULL;
669315a1350SMichael S. Tsirkin     dev->msix_vector_release_notifier = NULL;
670bbef882cSMichael S. Tsirkin     dev->msix_vector_poll_notifier = NULL;
671315a1350SMichael S. Tsirkin }
672340b50c7SGerd Hoffmann 
put_msix_state(QEMUFile * f,void * pv,size_t size,const VMStateField * field,JSONWriter * vmdesc)6732c21ee76SJianjun Duan static int put_msix_state(QEMUFile *f, void *pv, size_t size,
6743ddba9a9SMarkus Armbruster                           const VMStateField *field, JSONWriter *vmdesc)
675340b50c7SGerd Hoffmann {
676340b50c7SGerd Hoffmann     msix_save(pv, f);
6772c21ee76SJianjun Duan 
6782c21ee76SJianjun Duan     return 0;
679340b50c7SGerd Hoffmann }
680340b50c7SGerd Hoffmann 
get_msix_state(QEMUFile * f,void * pv,size_t size,const VMStateField * field)6812c21ee76SJianjun Duan static int get_msix_state(QEMUFile *f, void *pv, size_t size,
68203fee66fSMarc-André Lureau                           const VMStateField *field)
683340b50c7SGerd Hoffmann {
684340b50c7SGerd Hoffmann     msix_load(pv, f);
685340b50c7SGerd Hoffmann     return 0;
686340b50c7SGerd Hoffmann }
687340b50c7SGerd Hoffmann 
688*8e5e0890SRichard Henderson static const VMStateInfo vmstate_info_msix = {
689340b50c7SGerd Hoffmann     .name = "msix state",
690340b50c7SGerd Hoffmann     .get  = get_msix_state,
691340b50c7SGerd Hoffmann     .put  = put_msix_state,
692340b50c7SGerd Hoffmann };
693340b50c7SGerd Hoffmann 
694340b50c7SGerd Hoffmann const VMStateDescription vmstate_msix = {
695340b50c7SGerd Hoffmann     .name = "msix",
696*8e5e0890SRichard Henderson     .fields = (const VMStateField[]) {
697340b50c7SGerd Hoffmann         {
698340b50c7SGerd Hoffmann             .name         = "msix",
699340b50c7SGerd Hoffmann             .version_id   = 0,
700340b50c7SGerd Hoffmann             .field_exists = NULL,
701340b50c7SGerd Hoffmann             .size         = 0,   /* ouch */
702340b50c7SGerd Hoffmann             .info         = &vmstate_info_msix,
703340b50c7SGerd Hoffmann             .flags        = VMS_SINGLE,
704340b50c7SGerd Hoffmann             .offset       = 0,
705340b50c7SGerd Hoffmann         },
706340b50c7SGerd Hoffmann         VMSTATE_END_OF_LIST()
707340b50c7SGerd Hoffmann     }
708340b50c7SGerd Hoffmann };
709