Statistics
| Branch: | Revision:

root / hw / msi.c @ 43997225

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