Statistics
| Branch: | Revision:

root / hw / msi.c @ b903a0f7

History | View | Annotate | Download (11 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 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
168 e4c7d2ae Isaku Yamahata
    uint8_t cap_size = msi_cap_sizeof(flags);
169 e4c7d2ae Isaku Yamahata
    pci_del_capability(dev, PCI_CAP_ID_MSIX, cap_size);
170 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev, "uninit\n");
171 e4c7d2ae Isaku Yamahata
}
172 e4c7d2ae Isaku Yamahata
173 e4c7d2ae Isaku Yamahata
void msi_reset(PCIDevice *dev)
174 e4c7d2ae Isaku Yamahata
{
175 e4c7d2ae Isaku Yamahata
    uint16_t flags;
176 e4c7d2ae Isaku Yamahata
    bool msi64bit;
177 e4c7d2ae Isaku Yamahata
178 e4c7d2ae Isaku Yamahata
    flags = pci_get_word(dev->config + msi_flags_off(dev));
179 e4c7d2ae Isaku Yamahata
    flags &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
180 e4c7d2ae Isaku Yamahata
    msi64bit = flags & PCI_MSI_FLAGS_64BIT;
181 e4c7d2ae Isaku Yamahata
182 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->config + msi_flags_off(dev), flags);
183 e4c7d2ae Isaku Yamahata
    pci_set_long(dev->config + msi_address_lo_off(dev), 0);
184 e4c7d2ae Isaku Yamahata
    if (msi64bit) {
185 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->config + msi_address_hi_off(dev), 0);
186 e4c7d2ae Isaku Yamahata
    }
187 e4c7d2ae Isaku Yamahata
    pci_set_word(dev->config + msi_data_off(dev, msi64bit), 0);
188 e4c7d2ae Isaku Yamahata
    if (flags & PCI_MSI_FLAGS_MASKBIT) {
189 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->config + msi_mask_off(dev, msi64bit), 0);
190 e4c7d2ae Isaku Yamahata
        pci_set_long(dev->config + msi_pending_off(dev, msi64bit), 0);
191 e4c7d2ae Isaku Yamahata
    }
192 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev, "reset\n");
193 e4c7d2ae Isaku Yamahata
}
194 e4c7d2ae Isaku Yamahata
195 e4c7d2ae Isaku Yamahata
static bool msi_is_masked(const PCIDevice *dev, unsigned int vector)
196 e4c7d2ae Isaku Yamahata
{
197 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
198 e4c7d2ae Isaku Yamahata
    uint32_t mask;
199 e4c7d2ae Isaku Yamahata
    assert(vector < PCI_MSI_VECTORS_MAX);
200 e4c7d2ae Isaku Yamahata
201 e4c7d2ae Isaku Yamahata
    if (!(flags & PCI_MSI_FLAGS_MASKBIT)) {
202 e4c7d2ae Isaku Yamahata
        return false;
203 e4c7d2ae Isaku Yamahata
    }
204 e4c7d2ae Isaku Yamahata
205 e4c7d2ae Isaku Yamahata
    mask = pci_get_long(dev->config +
206 e4c7d2ae Isaku Yamahata
                        msi_mask_off(dev, flags & PCI_MSI_FLAGS_64BIT));
207 e4c7d2ae Isaku Yamahata
    return mask & (1U << vector);
208 e4c7d2ae Isaku Yamahata
}
209 e4c7d2ae Isaku Yamahata
210 e4c7d2ae Isaku Yamahata
void msi_notify(PCIDevice *dev, unsigned int vector)
211 e4c7d2ae Isaku Yamahata
{
212 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
213 e4c7d2ae Isaku Yamahata
    bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
214 e4c7d2ae Isaku Yamahata
    unsigned int nr_vectors = msi_nr_vectors(flags);
215 e4c7d2ae Isaku Yamahata
    uint64_t address;
216 e4c7d2ae Isaku Yamahata
    uint32_t data;
217 e4c7d2ae Isaku Yamahata
218 e4c7d2ae Isaku Yamahata
    assert(vector < nr_vectors);
219 e4c7d2ae Isaku Yamahata
    if (msi_is_masked(dev, vector)) {
220 e4c7d2ae Isaku Yamahata
        assert(flags & PCI_MSI_FLAGS_MASKBIT);
221 e4c7d2ae Isaku Yamahata
        pci_long_test_and_set_mask(
222 e4c7d2ae Isaku Yamahata
            dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
223 e4c7d2ae Isaku Yamahata
        MSI_DEV_PRINTF(dev, "pending vector 0x%x\n", vector);
224 e4c7d2ae Isaku Yamahata
        return;
225 e4c7d2ae Isaku Yamahata
    }
226 e4c7d2ae Isaku Yamahata
227 b794ec7c Michael S. Tsirkin
    if (msi64bit) {
228 e4c7d2ae Isaku Yamahata
        address = pci_get_quad(dev->config + msi_address_lo_off(dev));
229 e4c7d2ae Isaku Yamahata
    } else {
230 e4c7d2ae Isaku Yamahata
        address = pci_get_long(dev->config + msi_address_lo_off(dev));
231 e4c7d2ae Isaku Yamahata
    }
232 e4c7d2ae Isaku Yamahata
233 e4c7d2ae Isaku Yamahata
    /* upper bit 31:16 is zero */
234 e4c7d2ae Isaku Yamahata
    data = pci_get_word(dev->config + msi_data_off(dev, msi64bit));
235 e4c7d2ae Isaku Yamahata
    if (nr_vectors > 1) {
236 e4c7d2ae Isaku Yamahata
        data &= ~(nr_vectors - 1);
237 e4c7d2ae Isaku Yamahata
        data |= vector;
238 e4c7d2ae Isaku Yamahata
    }
239 e4c7d2ae Isaku Yamahata
240 e4c7d2ae Isaku Yamahata
    MSI_DEV_PRINTF(dev,
241 e4c7d2ae Isaku Yamahata
                   "notify vector 0x%x"
242 e4c7d2ae Isaku Yamahata
                   " address: 0x%"PRIx64" data: 0x%"PRIx32"\n",
243 e4c7d2ae Isaku Yamahata
                   vector, address, data);
244 e4c7d2ae Isaku Yamahata
    stl_phys(address, data);
245 e4c7d2ae Isaku Yamahata
}
246 e4c7d2ae Isaku Yamahata
247 e4c7d2ae Isaku Yamahata
/* call this function after updating configs by pci_default_write_config(). */
248 e4c7d2ae Isaku Yamahata
void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
249 e4c7d2ae Isaku Yamahata
{
250 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
251 e4c7d2ae Isaku Yamahata
    bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
252 e4c7d2ae Isaku Yamahata
    bool msi_per_vector_mask = flags & PCI_MSI_FLAGS_MASKBIT;
253 e4c7d2ae Isaku Yamahata
    unsigned int nr_vectors;
254 e4c7d2ae Isaku Yamahata
    uint8_t log_num_vecs;
255 e4c7d2ae Isaku Yamahata
    uint8_t log_max_vecs;
256 e4c7d2ae Isaku Yamahata
    unsigned int vector;
257 e4c7d2ae Isaku Yamahata
    uint32_t pending;
258 e4c7d2ae Isaku Yamahata
    int i;
259 e4c7d2ae Isaku Yamahata
260 531a0b82 Michael S. Tsirkin
    if (!ranges_overlap(addr, len, dev->msi_cap, msi_cap_sizeof(flags))) {
261 531a0b82 Michael S. Tsirkin
        return;
262 e4c7d2ae Isaku Yamahata
    }
263 e4c7d2ae Isaku Yamahata
264 531a0b82 Michael S. Tsirkin
#ifdef MSI_DEBUG
265 531a0b82 Michael S. Tsirkin
    MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n",
266 531a0b82 Michael S. Tsirkin
                   addr, val, len);
267 531a0b82 Michael S. Tsirkin
    MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32,
268 531a0b82 Michael S. Tsirkin
                   flags,
269 531a0b82 Michael S. Tsirkin
                   pci_get_long(dev->config + msi_address_lo_off(dev)));
270 531a0b82 Michael S. Tsirkin
    if (msi64bit) {
271 b794ec7c Michael S. Tsirkin
        fprintf(stderr, " address-hi: 0x%"PRIx32,
272 531a0b82 Michael S. Tsirkin
                pci_get_long(dev->config + msi_address_hi_off(dev)));
273 531a0b82 Michael S. Tsirkin
    }
274 531a0b82 Michael S. Tsirkin
    fprintf(stderr, " data: 0x%"PRIx16,
275 531a0b82 Michael S. Tsirkin
            pci_get_word(dev->config + msi_data_off(dev, msi64bit)));
276 531a0b82 Michael S. Tsirkin
    if (flags & PCI_MSI_FLAGS_MASKBIT) {
277 531a0b82 Michael S. Tsirkin
        fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32,
278 531a0b82 Michael S. Tsirkin
                pci_get_long(dev->config + msi_mask_off(dev, msi64bit)),
279 531a0b82 Michael S. Tsirkin
                pci_get_long(dev->config + msi_pending_off(dev, msi64bit)));
280 e4c7d2ae Isaku Yamahata
    }
281 531a0b82 Michael S. Tsirkin
    fprintf(stderr, "\n");
282 531a0b82 Michael S. Tsirkin
#endif
283 e4c7d2ae Isaku Yamahata
284 e4c7d2ae Isaku Yamahata
    if (!(flags & PCI_MSI_FLAGS_ENABLE)) {
285 e4c7d2ae Isaku Yamahata
        return;
286 e4c7d2ae Isaku Yamahata
    }
287 e4c7d2ae Isaku Yamahata
288 e4c7d2ae Isaku Yamahata
    /*
289 e4c7d2ae Isaku Yamahata
     * Now MSI is enabled, clear INTx# interrupts.
290 e4c7d2ae Isaku Yamahata
     * the driver is prohibited from writing enable bit to mask
291 e4c7d2ae Isaku Yamahata
     * a service request. But the guest OS could do this.
292 e4c7d2ae Isaku Yamahata
     * So we just discard the interrupts as moderate fallback.
293 e4c7d2ae Isaku Yamahata
     *
294 e4c7d2ae Isaku Yamahata
     * 6.8.3.3. Enabling Operation
295 e4c7d2ae Isaku Yamahata
     *   While enabled for MSI or MSI-X operation, a function is prohibited
296 e4c7d2ae Isaku Yamahata
     *   from using its INTx# pin (if implemented) to request
297 e4c7d2ae Isaku Yamahata
     *   service (MSI, MSI-X, and INTx# are mutually exclusive).
298 e4c7d2ae Isaku Yamahata
     */
299 e4c7d2ae Isaku Yamahata
    for (i = 0; i < PCI_NUM_PINS; ++i) {
300 e4c7d2ae Isaku Yamahata
        qemu_set_irq(dev->irq[i], 0);
301 e4c7d2ae Isaku Yamahata
    }
302 e4c7d2ae Isaku Yamahata
303 e4c7d2ae Isaku Yamahata
    /*
304 e4c7d2ae Isaku Yamahata
     * nr_vectors might be set bigger than capable. So clamp it.
305 e4c7d2ae Isaku Yamahata
     * This is not legal by spec, so we can do anything we like,
306 e4c7d2ae Isaku Yamahata
     * just don't crash the host
307 e4c7d2ae Isaku Yamahata
     */
308 e4c7d2ae Isaku Yamahata
    log_num_vecs =
309 e4c7d2ae Isaku Yamahata
        (flags & PCI_MSI_FLAGS_QSIZE) >> (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
310 e4c7d2ae Isaku Yamahata
    log_max_vecs =
311 e4c7d2ae Isaku Yamahata
        (flags & PCI_MSI_FLAGS_QMASK) >> (ffs(PCI_MSI_FLAGS_QMASK) - 1);
312 e4c7d2ae Isaku Yamahata
    if (log_num_vecs > log_max_vecs) {
313 e4c7d2ae Isaku Yamahata
        flags &= ~PCI_MSI_FLAGS_QSIZE;
314 e4c7d2ae Isaku Yamahata
        flags |= log_max_vecs << (ffs(PCI_MSI_FLAGS_QSIZE) - 1);
315 e4c7d2ae Isaku Yamahata
        pci_set_word(dev->config + msi_flags_off(dev), flags);
316 e4c7d2ae Isaku Yamahata
    }
317 e4c7d2ae Isaku Yamahata
318 e4c7d2ae Isaku Yamahata
    if (!msi_per_vector_mask) {
319 e4c7d2ae Isaku Yamahata
        /* if per vector masking isn't supported,
320 e4c7d2ae Isaku Yamahata
           there is no pending interrupt. */
321 e4c7d2ae Isaku Yamahata
        return;
322 e4c7d2ae Isaku Yamahata
    }
323 e4c7d2ae Isaku Yamahata
324 e4c7d2ae Isaku Yamahata
    nr_vectors = msi_nr_vectors(flags);
325 e4c7d2ae Isaku Yamahata
326 e4c7d2ae Isaku Yamahata
    /* This will discard pending interrupts, if any. */
327 e4c7d2ae Isaku Yamahata
    pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit));
328 e4c7d2ae Isaku Yamahata
    pending &= 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors);
329 e4c7d2ae Isaku Yamahata
    pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending);
330 e4c7d2ae Isaku Yamahata
331 e4c7d2ae Isaku Yamahata
    /* deliver pending interrupts which are unmasked */
332 e4c7d2ae Isaku Yamahata
    for (vector = 0; vector < nr_vectors; ++vector) {
333 e4c7d2ae Isaku Yamahata
        if (msi_is_masked(dev, vector) || !(pending & (1U << vector))) {
334 e4c7d2ae Isaku Yamahata
            continue;
335 e4c7d2ae Isaku Yamahata
        }
336 e4c7d2ae Isaku Yamahata
337 e4c7d2ae Isaku Yamahata
        pci_long_test_and_clear_mask(
338 e4c7d2ae Isaku Yamahata
            dev->config + msi_pending_off(dev, msi64bit), 1U << vector);
339 e4c7d2ae Isaku Yamahata
        msi_notify(dev, vector);
340 e4c7d2ae Isaku Yamahata
    }
341 e4c7d2ae Isaku Yamahata
}
342 e4c7d2ae Isaku Yamahata
343 e4c7d2ae Isaku Yamahata
unsigned int msi_nr_vectors_allocated(const PCIDevice *dev)
344 e4c7d2ae Isaku Yamahata
{
345 e4c7d2ae Isaku Yamahata
    uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev));
346 e4c7d2ae Isaku Yamahata
    return msi_nr_vectors(flags);
347 e4c7d2ae Isaku Yamahata
}