Statistics
| Branch: | Revision:

root / hw / msi.c @ 4dad7f1e

History | View | Annotate | Download (11.1 kB)

1 e4c7d2ae Isaku Yamahata
/*
2 e4c7d2ae Isaku Yamahata
 * msi.c
3 e4c7d2ae Isaku Yamahata
 *
4 e4c7d2ae Isaku Yamahata
 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
5 e4c7d2ae Isaku Yamahata
 *                    VA Linux Systems Japan K.K.
6 e4c7d2ae Isaku Yamahata
 *
7 e4c7d2ae Isaku Yamahata
 * This program is free software; you can redistribute it and/or modify
8 e4c7d2ae Isaku Yamahata
 * it under the terms of the GNU General Public License as published by
9 e4c7d2ae Isaku Yamahata
 * the Free Software Foundation; either version 2 of the License, or
10 e4c7d2ae Isaku Yamahata
 * (at your option) any later version.
11 e4c7d2ae Isaku Yamahata

12 e4c7d2ae Isaku Yamahata
 * This program is distributed in the hope that it will be useful,
13 e4c7d2ae Isaku Yamahata
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 e4c7d2ae Isaku Yamahata
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 e4c7d2ae Isaku Yamahata
 * GNU General Public License for more details.
16 e4c7d2ae Isaku Yamahata

17 e4c7d2ae Isaku Yamahata
 * You should have received a copy of the GNU General Public License along
18 e4c7d2ae Isaku Yamahata
 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 e4c7d2ae Isaku Yamahata
 */
20 e4c7d2ae Isaku Yamahata
21 e4c7d2ae Isaku Yamahata
#include "msi.h"
22 5afb9869 Blue Swirl
#include "range.h"
23 e4c7d2ae Isaku Yamahata
24 e4c7d2ae Isaku Yamahata
/* Eventually those constants should go to Linux pci_regs.h */
25 e4c7d2ae Isaku Yamahata
#define PCI_MSI_PENDING_32      0x10
26 e4c7d2ae Isaku Yamahata
#define PCI_MSI_PENDING_64      0x14
27 e4c7d2ae Isaku Yamahata
28 e4c7d2ae Isaku Yamahata
/* PCI_MSI_ADDRESS_LO */
29 e4c7d2ae Isaku Yamahata
#define PCI_MSI_ADDRESS_LO_MASK         (~0x3)
30 e4c7d2ae Isaku Yamahata
31 e4c7d2ae Isaku Yamahata
/* If we get rid of cap allocator, we won't need those. */
32 e4c7d2ae Isaku Yamahata
#define PCI_MSI_32_SIZEOF       0x0a
33 e4c7d2ae Isaku Yamahata
#define PCI_MSI_64_SIZEOF       0x0e
34 e4c7d2ae Isaku Yamahata
#define PCI_MSI_32M_SIZEOF      0x14
35 e4c7d2ae Isaku Yamahata
#define PCI_MSI_64M_SIZEOF      0x18
36 e4c7d2ae Isaku Yamahata
37 e4c7d2ae Isaku Yamahata
#define PCI_MSI_VECTORS_MAX     32
38 e4c7d2ae Isaku Yamahata
39 e4c7d2ae Isaku Yamahata
/* If we get rid of cap allocator, we won't need this. */
40 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_cap_sizeof(uint16_t flags)
41 e4c7d2ae Isaku Yamahata
{
42 e4c7d2ae Isaku Yamahata
    switch (flags & (PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT)) {
43 e4c7d2ae Isaku Yamahata
    case PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT:
44 e4c7d2ae Isaku Yamahata
        return PCI_MSI_64M_SIZEOF;
45 e4c7d2ae Isaku Yamahata
    case PCI_MSI_FLAGS_64BIT:
46 e4c7d2ae Isaku Yamahata
        return PCI_MSI_64_SIZEOF;
47 e4c7d2ae Isaku Yamahata
    case PCI_MSI_FLAGS_MASKBIT:
48 e4c7d2ae Isaku Yamahata
        return PCI_MSI_32M_SIZEOF;
49 e4c7d2ae Isaku Yamahata
    case 0:
50 e4c7d2ae Isaku Yamahata
        return PCI_MSI_32_SIZEOF;
51 e4c7d2ae Isaku Yamahata
    default:
52 e4c7d2ae Isaku Yamahata
        abort();
53 e4c7d2ae Isaku Yamahata
        break;
54 e4c7d2ae Isaku Yamahata
    }
55 e4c7d2ae Isaku Yamahata
    return 0;
56 e4c7d2ae Isaku Yamahata
}
57 e4c7d2ae Isaku Yamahata
58 e4c7d2ae Isaku Yamahata
//#define MSI_DEBUG
59 e4c7d2ae Isaku Yamahata
60 e4c7d2ae Isaku Yamahata
#ifdef MSI_DEBUG
61 e4c7d2ae Isaku Yamahata
# define MSI_DPRINTF(fmt, ...)                                          \
62 e4c7d2ae Isaku Yamahata
    fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
63 e4c7d2ae Isaku Yamahata
#else
64 e4c7d2ae Isaku Yamahata
# define MSI_DPRINTF(fmt, ...)  do { } while (0)
65 e4c7d2ae Isaku Yamahata
#endif
66 e4c7d2ae Isaku Yamahata
#define MSI_DEV_PRINTF(dev, fmt, ...)                                   \
67 e4c7d2ae Isaku Yamahata
    MSI_DPRINTF("%s:%x " fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
68 e4c7d2ae Isaku Yamahata
69 e4c7d2ae Isaku Yamahata
static inline unsigned int msi_nr_vectors(uint16_t flags)
70 e4c7d2ae Isaku Yamahata
{
71 e4c7d2ae Isaku Yamahata
    return 1U <<
72 e4c7d2ae Isaku Yamahata
        ((flags & PCI_MSI_FLAGS_QSIZE) >> (ffs(PCI_MSI_FLAGS_QSIZE) - 1));
73 e4c7d2ae Isaku Yamahata
}
74 e4c7d2ae Isaku Yamahata
75 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_flags_off(const PCIDevice* dev)
76 e4c7d2ae Isaku Yamahata
{
77 e4c7d2ae Isaku Yamahata
    return dev->msi_cap + PCI_MSI_FLAGS;
78 e4c7d2ae Isaku Yamahata
}
79 e4c7d2ae Isaku Yamahata
80 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_address_lo_off(const PCIDevice* dev)
81 e4c7d2ae Isaku Yamahata
{
82 e4c7d2ae Isaku Yamahata
    return dev->msi_cap + PCI_MSI_ADDRESS_LO;
83 e4c7d2ae Isaku Yamahata
}
84 e4c7d2ae Isaku Yamahata
85 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_address_hi_off(const PCIDevice* dev)
86 e4c7d2ae Isaku Yamahata
{
87 e4c7d2ae Isaku Yamahata
    return dev->msi_cap + PCI_MSI_ADDRESS_HI;
88 e4c7d2ae Isaku Yamahata
}
89 e4c7d2ae Isaku Yamahata
90 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_data_off(const PCIDevice* dev, bool msi64bit)
91 e4c7d2ae Isaku Yamahata
{
92 e4c7d2ae Isaku Yamahata
    return dev->msi_cap + (msi64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32);
93 e4c7d2ae Isaku Yamahata
}
94 e4c7d2ae Isaku Yamahata
95 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_mask_off(const PCIDevice* dev, bool msi64bit)
96 e4c7d2ae Isaku Yamahata
{
97 e4c7d2ae Isaku Yamahata
    return dev->msi_cap + (msi64bit ? PCI_MSI_MASK_64 : PCI_MSI_MASK_32);
98 e4c7d2ae Isaku Yamahata
}
99 e4c7d2ae Isaku Yamahata
100 e4c7d2ae Isaku Yamahata
static inline uint8_t msi_pending_off(const PCIDevice* dev, bool msi64bit)
101 e4c7d2ae Isaku Yamahata
{
102 e4c7d2ae Isaku Yamahata
    return dev->msi_cap + (msi64bit ? PCI_MSI_PENDING_64 : PCI_MSI_PENDING_32);
103 e4c7d2ae Isaku Yamahata
}
104 e4c7d2ae Isaku Yamahata
105 e4c7d2ae Isaku Yamahata
bool msi_enabled(const PCIDevice *dev)
106 e4c7d2ae Isaku Yamahata
{
107 e4c7d2ae Isaku Yamahata
    return msi_present(dev) &&
108 e4c7d2ae Isaku Yamahata
        (pci_get_word(dev->config + msi_flags_off(dev)) &
109 e4c7d2ae Isaku Yamahata
         PCI_MSI_FLAGS_ENABLE);
110 e4c7d2ae Isaku Yamahata
}
111 e4c7d2ae Isaku Yamahata
112 e4c7d2ae Isaku Yamahata
int msi_init(struct PCIDevice *dev, uint8_t offset,
113 e4c7d2ae Isaku Yamahata
             unsigned int nr_vectors, bool msi64bit, bool msi_per_vector_mask)
114 e4c7d2ae Isaku Yamahata
{
115 e4c7d2ae Isaku Yamahata
    unsigned int vectors_order;
116 e4c7d2ae Isaku Yamahata
    uint16_t flags;
117 e4c7d2ae Isaku Yamahata
    uint8_t cap_size;
118 e4c7d2ae Isaku Yamahata
    int config_offset;
119 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev,
120 e4c7d2ae Isaku Yamahata
                   "init offset: 0x%"PRIx8" vector: %"PRId8
121 e4c7d2ae Isaku Yamahata
                   " 64bit %d mask %d\n",
122 e4c7d2ae Isaku Yamahata
                   offset, nr_vectors, msi64bit, msi_per_vector_mask);
123 e4c7d2ae Isaku Yamahata
124 e4c7d2ae Isaku Yamahata
    assert(!(nr_vectors & (nr_vectors - 1)));   /* power of 2 */
125 e4c7d2ae Isaku Yamahata
    assert(nr_vectors > 0);
126 e4c7d2ae Isaku Yamahata
    assert(nr_vectors <= PCI_MSI_VECTORS_MAX);
127 e4c7d2ae Isaku Yamahata
    /* the nr of MSI vectors is up to 32 */
128 e4c7d2ae Isaku Yamahata
    vectors_order = ffs(nr_vectors) - 1;
129 e4c7d2ae Isaku Yamahata
130 e4c7d2ae Isaku Yamahata
    flags = vectors_order << (ffs(PCI_MSI_FLAGS_QMASK) - 1);
131 e4c7d2ae Isaku Yamahata
    if (msi64bit) {
132 e4c7d2ae Isaku Yamahata
        flags |= PCI_MSI_FLAGS_64BIT;
133 e4c7d2ae Isaku Yamahata
    }
134 e4c7d2ae Isaku Yamahata
    if (msi_per_vector_mask) {
135 e4c7d2ae Isaku Yamahata
        flags |= PCI_MSI_FLAGS_MASKBIT;
136 e4c7d2ae Isaku Yamahata
    }
137 e4c7d2ae Isaku Yamahata
138 e4c7d2ae Isaku Yamahata
    cap_size = msi_cap_sizeof(flags);
139 e4c7d2ae Isaku Yamahata
    config_offset = pci_add_capability(dev, PCI_CAP_ID_MSI, offset, cap_size);
140 e4c7d2ae Isaku Yamahata
    if (config_offset < 0) {
141 e4c7d2ae Isaku Yamahata
        return config_offset;
142 e4c7d2ae Isaku Yamahata
    }
143 e4c7d2ae Isaku Yamahata
144 e4c7d2ae Isaku Yamahata
    dev->msi_cap = config_offset;
145 e4c7d2ae Isaku Yamahata
    dev->cap_present |= QEMU_PCI_CAP_MSI;
146 e4c7d2ae Isaku Yamahata
147 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->config + msi_flags_off(dev), flags);
148 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->wmask + msi_flags_off(dev),
149 e4c7d2ae Isaku Yamahata
                 PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
150 e4c7d2ae Isaku Yamahata
    pci_set_long(dev->wmask + msi_address_lo_off(dev),
151 e4c7d2ae Isaku Yamahata
                 PCI_MSI_ADDRESS_LO_MASK);
152 e4c7d2ae Isaku Yamahata
    if (msi64bit) {
153 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->wmask + msi_address_hi_off(dev), 0xffffffff);
154 e4c7d2ae Isaku Yamahata
    }
155 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->wmask + msi_data_off(dev, msi64bit), 0xffff);
156 e4c7d2ae Isaku Yamahata
157 e4c7d2ae Isaku Yamahata
    if (msi_per_vector_mask) {
158 b794ec7c Michael S. Tsirkin
        /* Make mask bits 0 to nr_vectors - 1 writeable. */
159 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->wmask + msi_mask_off(dev, msi64bit),
160 e4c7d2ae Isaku Yamahata
                     0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors));
161 e4c7d2ae Isaku Yamahata
    }
162 e4c7d2ae Isaku Yamahata
    return config_offset;
163 e4c7d2ae Isaku Yamahata
}
164 e4c7d2ae Isaku Yamahata
165 e4c7d2ae Isaku Yamahata
void msi_uninit(struct PCIDevice *dev)
166 e4c7d2ae Isaku Yamahata
{
167 45fe15c2 Jan Kiszka
    uint16_t flags;
168 45fe15c2 Jan Kiszka
    uint8_t cap_size;
169 45fe15c2 Jan Kiszka
170 45fe15c2 Jan Kiszka
    if (!(dev->cap_present & QEMU_PCI_CAP_MSI)) {
171 45fe15c2 Jan Kiszka
        return;
172 45fe15c2 Jan Kiszka
    }
173 45fe15c2 Jan Kiszka
    flags = pci_get_word(dev->config + msi_flags_off(dev));
174 45fe15c2 Jan Kiszka
    cap_size = msi_cap_sizeof(flags);
175 4dad7f1e Jan Kiszka
    pci_del_capability(dev, PCI_CAP_ID_MSI, cap_size);
176 45fe15c2 Jan Kiszka
    dev->cap_present &= ~QEMU_PCI_CAP_MSI;
177 45fe15c2 Jan Kiszka
178 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev, "uninit\n");
179 e4c7d2ae Isaku Yamahata
}
180 e4c7d2ae Isaku Yamahata
181 e4c7d2ae Isaku Yamahata
void msi_reset(PCIDevice *dev)
182 e4c7d2ae Isaku Yamahata
{
183 e4c7d2ae Isaku Yamahata
    uint16_t flags;
184 e4c7d2ae Isaku Yamahata
    bool msi64bit;
185 e4c7d2ae Isaku Yamahata
186 e4c7d2ae Isaku Yamahata
    flags = pci_get_word(dev->config + msi_flags_off(dev));
187 e4c7d2ae Isaku Yamahata
    flags &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
188 e4c7d2ae Isaku Yamahata
    msi64bit = flags & PCI_MSI_FLAGS_64BIT;
189 e4c7d2ae Isaku Yamahata
190 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->config + msi_flags_off(dev), flags);
191 e4c7d2ae Isaku Yamahata
    pci_set_long(dev->config + msi_address_lo_off(dev), 0);
192 e4c7d2ae Isaku Yamahata
    if (msi64bit) {
193 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->config + msi_address_hi_off(dev), 0);
194 e4c7d2ae Isaku Yamahata
    }
195 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->config + msi_data_off(dev, msi64bit), 0);
196 e4c7d2ae Isaku Yamahata
    if (flags & PCI_MSI_FLAGS_MASKBIT) {
197 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->config + msi_mask_off(dev, msi64bit), 0);
198 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->config + msi_pending_off(dev, msi64bit), 0);
199 e4c7d2ae Isaku Yamahata
    }
200 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev, "reset\n");
201 e4c7d2ae Isaku Yamahata
}
202 e4c7d2ae Isaku Yamahata
203 e4c7d2ae Isaku Yamahata
static bool msi_is_masked(const PCIDevice *dev, unsigned int vector)
204 e4c7d2ae Isaku Yamahata
{
205 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
206 e4c7d2ae Isaku Yamahata
    uint32_t mask;
207 e4c7d2ae Isaku Yamahata
    assert(vector < PCI_MSI_VECTORS_MAX);
208 e4c7d2ae Isaku Yamahata
209 e4c7d2ae Isaku Yamahata
    if (!(flags & PCI_MSI_FLAGS_MASKBIT)) {
210 e4c7d2ae Isaku Yamahata
        return false;
211 e4c7d2ae Isaku Yamahata
    }
212 e4c7d2ae Isaku Yamahata
213 e4c7d2ae Isaku Yamahata
    mask = pci_get_long(dev->config +
214 e4c7d2ae Isaku Yamahata
                        msi_mask_off(dev, flags & PCI_MSI_FLAGS_64BIT));
215 e4c7d2ae Isaku Yamahata
    return mask & (1U << vector);
216 e4c7d2ae Isaku Yamahata
}
217 e4c7d2ae Isaku Yamahata
218 e4c7d2ae Isaku Yamahata
void msi_notify(PCIDevice *dev, unsigned int vector)
219 e4c7d2ae Isaku Yamahata
{
220 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
221 e4c7d2ae Isaku Yamahata
    bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
222 e4c7d2ae Isaku Yamahata
    unsigned int nr_vectors = msi_nr_vectors(flags);
223 e4c7d2ae Isaku Yamahata
    uint64_t address;
224 e4c7d2ae Isaku Yamahata
    uint32_t data;
225 e4c7d2ae Isaku Yamahata
226 e4c7d2ae Isaku Yamahata
    assert(vector < nr_vectors);
227 e4c7d2ae Isaku Yamahata
    if (msi_is_masked(dev, vector)) {
228 e4c7d2ae Isaku Yamahata
        assert(flags & PCI_MSI_FLAGS_MASKBIT);
229 e4c7d2ae Isaku Yamahata
        pci_long_test_and_set_mask(
230 e4c7d2ae Isaku Yamahata
            dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
231 e4c7d2ae Isaku Yamahata
        MSI_DEV_PRINTF(dev, "pending vector 0x%x\n", vector);
232 e4c7d2ae Isaku Yamahata
        return;
233 e4c7d2ae Isaku Yamahata
    }
234 e4c7d2ae Isaku Yamahata
235 b794ec7c Michael S. Tsirkin
    if (msi64bit) {
236 e4c7d2ae Isaku Yamahata
        address = pci_get_quad(dev->config + msi_address_lo_off(dev));
237 e4c7d2ae Isaku Yamahata
    } else {
238 e4c7d2ae Isaku Yamahata
        address = pci_get_long(dev->config + msi_address_lo_off(dev));
239 e4c7d2ae Isaku Yamahata
    }
240 e4c7d2ae Isaku Yamahata
241 e4c7d2ae Isaku Yamahata
    /* upper bit 31:16 is zero */
242 e4c7d2ae Isaku Yamahata
    data = pci_get_word(dev->config + msi_data_off(dev, msi64bit));
243 e4c7d2ae Isaku Yamahata
    if (nr_vectors > 1) {
244 e4c7d2ae Isaku Yamahata
        data &= ~(nr_vectors - 1);
245 e4c7d2ae Isaku Yamahata
        data |= vector;
246 e4c7d2ae Isaku Yamahata
    }
247 e4c7d2ae Isaku Yamahata
248 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev,
249 e4c7d2ae Isaku Yamahata
                   "notify vector 0x%x"
250 e4c7d2ae Isaku Yamahata
                   " address: 0x%"PRIx64" data: 0x%"PRIx32"\n",
251 e4c7d2ae Isaku Yamahata
                   vector, address, data);
252 e4c7d2ae Isaku Yamahata
    stl_phys(address, data);
253 e4c7d2ae Isaku Yamahata
}
254 e4c7d2ae Isaku Yamahata
255 e4c7d2ae Isaku Yamahata
/* call this function after updating configs by pci_default_write_config(). */
256 e4c7d2ae Isaku Yamahata
void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
257 e4c7d2ae Isaku Yamahata
{
258 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
259 e4c7d2ae Isaku Yamahata
    bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
260 e4c7d2ae Isaku Yamahata
    bool msi_per_vector_mask = flags & PCI_MSI_FLAGS_MASKBIT;
261 e4c7d2ae Isaku Yamahata
    unsigned int nr_vectors;
262 e4c7d2ae Isaku Yamahata
    uint8_t log_num_vecs;
263 e4c7d2ae Isaku Yamahata
    uint8_t log_max_vecs;
264 e4c7d2ae Isaku Yamahata
    unsigned int vector;
265 e4c7d2ae Isaku Yamahata
    uint32_t pending;
266 e4c7d2ae Isaku Yamahata
267 531a0b82 Michael S. Tsirkin
    if (!ranges_overlap(addr, len, dev->msi_cap, msi_cap_sizeof(flags))) {
268 531a0b82 Michael S. Tsirkin
        return;
269 e4c7d2ae Isaku Yamahata
    }
270 e4c7d2ae Isaku Yamahata
271 531a0b82 Michael S. Tsirkin
#ifdef MSI_DEBUG
272 531a0b82 Michael S. Tsirkin
    MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n",
273 531a0b82 Michael S. Tsirkin
                   addr, val, len);
274 531a0b82 Michael S. Tsirkin
    MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32,
275 531a0b82 Michael S. Tsirkin
                   flags,
276 531a0b82 Michael S. Tsirkin
                   pci_get_long(dev->config + msi_address_lo_off(dev)));
277 531a0b82 Michael S. Tsirkin
    if (msi64bit) {
278 b794ec7c Michael S. Tsirkin
        fprintf(stderr, " address-hi: 0x%"PRIx32,
279 531a0b82 Michael S. Tsirkin
                pci_get_long(dev->config + msi_address_hi_off(dev)));
280 531a0b82 Michael S. Tsirkin
    }
281 531a0b82 Michael S. Tsirkin
    fprintf(stderr, " data: 0x%"PRIx16,
282 531a0b82 Michael S. Tsirkin
            pci_get_word(dev->config + msi_data_off(dev, msi64bit)));
283 531a0b82 Michael S. Tsirkin
    if (flags & PCI_MSI_FLAGS_MASKBIT) {
284 531a0b82 Michael S. Tsirkin
        fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32,
285 531a0b82 Michael S. Tsirkin
                pci_get_long(dev->config + msi_mask_off(dev, msi64bit)),
286 531a0b82 Michael S. Tsirkin
                pci_get_long(dev->config + msi_pending_off(dev, msi64bit)));
287 e4c7d2ae Isaku Yamahata
    }
288 531a0b82 Michael S. Tsirkin
    fprintf(stderr, "\n");
289 531a0b82 Michael S. Tsirkin
#endif
290 e4c7d2ae Isaku Yamahata
291 e4c7d2ae Isaku Yamahata
    if (!(flags & PCI_MSI_FLAGS_ENABLE)) {
292 e4c7d2ae Isaku Yamahata
        return;
293 e4c7d2ae Isaku Yamahata
    }
294 e4c7d2ae Isaku Yamahata
295 e4c7d2ae Isaku Yamahata
    /*
296 e4c7d2ae Isaku Yamahata
     * Now MSI is enabled, clear INTx# interrupts.
297 e4c7d2ae Isaku Yamahata
     * the driver is prohibited from writing enable bit to mask
298 e4c7d2ae Isaku Yamahata
     * a service request. But the guest OS could do this.
299 e4c7d2ae Isaku Yamahata
     * So we just discard the interrupts as moderate fallback.
300 e4c7d2ae Isaku Yamahata
     *
301 e4c7d2ae Isaku Yamahata
     * 6.8.3.3. Enabling Operation
302 e4c7d2ae Isaku Yamahata
     *   While enabled for MSI or MSI-X operation, a function is prohibited
303 e4c7d2ae Isaku Yamahata
     *   from using its INTx# pin (if implemented) to request
304 e4c7d2ae Isaku Yamahata
     *   service (MSI, MSI-X, and INTx# are mutually exclusive).
305 e4c7d2ae Isaku Yamahata
     */
306 59369b08 Isaku Yamahata
    pci_device_deassert_intx(dev);
307 e4c7d2ae Isaku Yamahata
308 e4c7d2ae Isaku Yamahata
    /*
309 e4c7d2ae Isaku Yamahata
     * nr_vectors might be set bigger than capable. So clamp it.
310 e4c7d2ae Isaku Yamahata
     * This is not legal by spec, so we can do anything we like,
311 e4c7d2ae Isaku Yamahata
     * just don't crash the host
312 e4c7d2ae Isaku Yamahata
     */
313 e4c7d2ae Isaku Yamahata
    log_num_vecs =
314 e4c7d2ae Isaku Yamahata
        (flags & PCI_MSI_FLAGS_QSIZE) >> (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
315 e4c7d2ae Isaku Yamahata
    log_max_vecs =
316 e4c7d2ae Isaku Yamahata
        (flags & PCI_MSI_FLAGS_QMASK) >> (ffs(PCI_MSI_FLAGS_QMASK) - 1);
317 e4c7d2ae Isaku Yamahata
    if (log_num_vecs > log_max_vecs) {
318 e4c7d2ae Isaku Yamahata
        flags &= ~PCI_MSI_FLAGS_QSIZE;
319 e4c7d2ae Isaku Yamahata
        flags |= log_max_vecs << (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
320 e4c7d2ae Isaku Yamahata
        pci_set_word(dev->config + msi_flags_off(dev), flags);
321 e4c7d2ae Isaku Yamahata
    }
322 e4c7d2ae Isaku Yamahata
323 e4c7d2ae Isaku Yamahata
    if (!msi_per_vector_mask) {
324 e4c7d2ae Isaku Yamahata
        /* if per vector masking isn't supported,
325 e4c7d2ae Isaku Yamahata
           there is no pending interrupt. */
326 e4c7d2ae Isaku Yamahata
        return;
327 e4c7d2ae Isaku Yamahata
    }
328 e4c7d2ae Isaku Yamahata
329 e4c7d2ae Isaku Yamahata
    nr_vectors = msi_nr_vectors(flags);
330 e4c7d2ae Isaku Yamahata
331 e4c7d2ae Isaku Yamahata
    /* This will discard pending interrupts, if any. */
332 e4c7d2ae Isaku Yamahata
    pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit));
333 e4c7d2ae Isaku Yamahata
    pending &= 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors);
334 e4c7d2ae Isaku Yamahata
    pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending);
335 e4c7d2ae Isaku Yamahata
336 e4c7d2ae Isaku Yamahata
    /* deliver pending interrupts which are unmasked */
337 e4c7d2ae Isaku Yamahata
    for (vector = 0; vector < nr_vectors; ++vector) {
338 e4c7d2ae Isaku Yamahata
        if (msi_is_masked(dev, vector) || !(pending & (1U << vector))) {
339 e4c7d2ae Isaku Yamahata
            continue;
340 e4c7d2ae Isaku Yamahata
        }
341 e4c7d2ae Isaku Yamahata
342 e4c7d2ae Isaku Yamahata
        pci_long_test_and_clear_mask(
343 e4c7d2ae Isaku Yamahata
            dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
344 e4c7d2ae Isaku Yamahata
        msi_notify(dev, vector);
345 e4c7d2ae Isaku Yamahata
    }
346 e4c7d2ae Isaku Yamahata
}
347 e4c7d2ae Isaku Yamahata
348 e4c7d2ae Isaku Yamahata
unsigned int msi_nr_vectors_allocated(const PCIDevice *dev)
349 e4c7d2ae Isaku Yamahata
{
350 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
351 e4c7d2ae Isaku Yamahata
    return msi_nr_vectors(flags);
352 e4c7d2ae Isaku Yamahata
}