Line | Count | Source |
1 | | /* |
2 | | * msi.c |
3 | | * |
4 | | * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp> |
5 | | * VA Linux Systems Japan K.K. |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | |
17 | | * You should have received a copy of the GNU General Public License along |
18 | | * with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "qemu/osdep.h" |
22 | | #include "hw/pci/msi.h" |
23 | | #include "hw/xen/xen.h" |
24 | | #include "qemu/range.h" |
25 | | #include "qapi/error.h" |
26 | | #include "system/xen.h" |
27 | | |
28 | | #include "hw/i386/kvm/xen_evtchn.h" |
29 | | |
30 | | /* PCI_MSI_ADDRESS_LO */ |
31 | 65 | #define PCI_MSI_ADDRESS_LO_MASK (~0x3) |
32 | | |
33 | | /* If we get rid of cap allocator, we won't need those. */ |
34 | 0 | #define PCI_MSI_32_SIZEOF 0x0a |
35 | 27.8k | #define PCI_MSI_64_SIZEOF 0x0e |
36 | 0 | #define PCI_MSI_32M_SIZEOF 0x14 |
37 | 105 | #define PCI_MSI_64M_SIZEOF 0x18 |
38 | | |
39 | 2 | #define PCI_MSI_VECTORS_MAX 32 |
40 | | |
41 | | /* |
42 | | * Flag for interrupt controllers to declare broken MSI/MSI-X support. |
43 | | * values: false - broken; true - non-broken. |
44 | | * |
45 | | * Setting this flag to false will remove MSI/MSI-X capability from all devices. |
46 | | * |
47 | | * It is preferable for controllers to set this to true (non-broken) even if |
48 | | * they do not actually support MSI/MSI-X: guests normally probe the controller |
49 | | * type and do not attempt to enable MSI/MSI-X with interrupt controllers not |
50 | | * supporting such, so removing the capability is not required, and |
51 | | * it seems cleaner to have a given device look the same for all boards. |
52 | | * |
53 | | * TODO: some existing controllers violate the above rule. Identify and fix them. |
54 | | */ |
55 | | bool msi_nonbroken; |
56 | | |
57 | | /* If we get rid of cap allocator, we won't need this. */ |
58 | | static inline uint8_t msi_cap_sizeof(uint16_t flags) |
59 | 27.9k | { |
60 | 27.9k | switch (flags & (PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT)) { |
61 | 105 | case PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT: |
62 | 105 | return PCI_MSI_64M_SIZEOF; |
63 | 27.8k | case PCI_MSI_FLAGS_64BIT: |
64 | 27.8k | return PCI_MSI_64_SIZEOF; |
65 | 0 | case PCI_MSI_FLAGS_MASKBIT: |
66 | 0 | return PCI_MSI_32M_SIZEOF; |
67 | 0 | case 0: |
68 | 0 | return PCI_MSI_32_SIZEOF; |
69 | 0 | default: |
70 | 0 | abort(); |
71 | 0 | break; |
72 | 27.9k | } |
73 | 0 | return 0; |
74 | 27.9k | } |
75 | | |
76 | | //#define MSI_DEBUG |
77 | | |
78 | | #ifdef MSI_DEBUG |
79 | | # define MSI_DPRINTF(fmt, ...) \ |
80 | | fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__) |
81 | | #else |
82 | 70.2k | # define MSI_DPRINTF(fmt, ...) do { } while (0) |
83 | | #endif |
84 | | #define MSI_DEV_PRINTF(dev, fmt, ...) \ |
85 | 70.2k | MSI_DPRINTF("%s:%x " fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__) |
86 | | |
87 | | static inline unsigned int msi_nr_vectors(uint16_t flags) |
88 | 0 | { |
89 | 0 | return 1U << |
90 | 0 | ((flags & PCI_MSI_FLAGS_QSIZE) >> ctz32(PCI_MSI_FLAGS_QSIZE)); |
91 | 0 | } |
92 | | |
93 | | static inline uint8_t msi_flags_off(const PCIDevice* dev) |
94 | 1.86M | { |
95 | 1.86M | return dev->msi_cap + PCI_MSI_FLAGS; |
96 | 1.86M | } |
97 | | |
98 | | static inline uint8_t msi_address_lo_off(const PCIDevice* dev) |
99 | 70.2k | { |
100 | 70.2k | return dev->msi_cap + PCI_MSI_ADDRESS_LO; |
101 | 70.2k | } |
102 | | |
103 | | static inline uint8_t msi_address_hi_off(const PCIDevice* dev) |
104 | 70.2k | { |
105 | 70.2k | return dev->msi_cap + PCI_MSI_ADDRESS_HI; |
106 | 70.2k | } |
107 | | |
108 | | static inline uint8_t msi_data_off(const PCIDevice* dev, bool msi64bit) |
109 | 70.2k | { |
110 | 70.2k | return dev->msi_cap + (msi64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32); |
111 | 70.2k | } |
112 | | |
113 | | static inline uint8_t msi_mask_off(const PCIDevice* dev, bool msi64bit) |
114 | 15 | { |
115 | 15 | return dev->msi_cap + (msi64bit ? PCI_MSI_MASK_64 : PCI_MSI_MASK_32); |
116 | 15 | } |
117 | | |
118 | | static inline uint8_t msi_pending_off(const PCIDevice* dev, bool msi64bit) |
119 | 13 | { |
120 | 13 | return dev->msi_cap + (msi64bit ? PCI_MSI_PENDING_64 : PCI_MSI_PENDING_32); |
121 | 13 | } |
122 | | |
123 | | /* |
124 | | * Special API for POWER to configure the vectors through |
125 | | * a side channel. Should never be used by devices. |
126 | | */ |
127 | | void msi_set_message(PCIDevice *dev, MSIMessage msg) |
128 | 0 | { |
129 | 0 | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
130 | 0 | bool msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
131 | |
|
132 | 0 | if (msi64bit) { |
133 | 0 | pci_set_quad(dev->config + msi_address_lo_off(dev), msg.address); |
134 | 0 | } else { |
135 | 0 | pci_set_long(dev->config + msi_address_lo_off(dev), msg.address); |
136 | 0 | } |
137 | 0 | pci_set_word(dev->config + msi_data_off(dev, msi64bit), msg.data); |
138 | 0 | } |
139 | | |
140 | | static MSIMessage msi_prepare_message(PCIDevice *dev, unsigned int vector) |
141 | 0 | { |
142 | 0 | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
143 | 0 | bool msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
144 | 0 | unsigned int nr_vectors = msi_nr_vectors(flags); |
145 | 0 | MSIMessage msg; |
146 | |
|
147 | 0 | assert(vector < nr_vectors); |
148 | |
|
149 | 0 | if (msi64bit) { |
150 | 0 | msg.address = pci_get_quad(dev->config + msi_address_lo_off(dev)); |
151 | 0 | } else { |
152 | 0 | msg.address = pci_get_long(dev->config + msi_address_lo_off(dev)); |
153 | 0 | } |
154 | | |
155 | | /* upper bit 31:16 is zero */ |
156 | 0 | msg.data = pci_get_word(dev->config + msi_data_off(dev, msi64bit)); |
157 | 0 | if (nr_vectors > 1) { |
158 | 0 | msg.data &= ~(nr_vectors - 1); |
159 | 0 | msg.data |= vector; |
160 | 0 | } |
161 | |
|
162 | 0 | return msg; |
163 | 0 | } |
164 | | |
165 | | MSIMessage msi_get_message(PCIDevice *dev, unsigned int vector) |
166 | 0 | { |
167 | 0 | return dev->msi_prepare_message(dev, vector); |
168 | 0 | } |
169 | | |
170 | | bool msi_enabled(const PCIDevice *dev) |
171 | 274k | { |
172 | 274k | return msi_present(dev) && |
173 | 3.48k | (pci_get_word(dev->config + msi_flags_off(dev)) & |
174 | 3.48k | PCI_MSI_FLAGS_ENABLE); |
175 | 274k | } |
176 | | |
177 | | /* |
178 | | * Make PCI device @dev MSI-capable. |
179 | | * Non-zero @offset puts capability MSI at that offset in PCI config |
180 | | * space. |
181 | | * @nr_vectors is the number of MSI vectors (1, 2, 4, 8, 16 or 32). |
182 | | * If @msi64bit, make the device capable of sending a 64-bit message |
183 | | * address. |
184 | | * If @msi_per_vector_mask, make the device support per-vector masking. |
185 | | * @errp is for returning errors. |
186 | | * Return 0 on success; set @errp and return -errno on error. |
187 | | * |
188 | | * -ENOTSUP means lacking msi support for a msi-capable platform. |
189 | | * -EINVAL means capability overlap, happens when @offset is non-zero, |
190 | | * also means a programming error, except device assignment, which can check |
191 | | * if a real HW is broken. |
192 | | */ |
193 | | int msi_init(struct PCIDevice *dev, uint8_t offset, |
194 | | unsigned int nr_vectors, bool msi64bit, |
195 | | bool msi_per_vector_mask, Error **errp) |
196 | 65 | { |
197 | 65 | unsigned int vectors_order; |
198 | 65 | uint16_t flags; |
199 | 65 | uint8_t cap_size; |
200 | 65 | int config_offset; |
201 | | |
202 | 65 | if (!msi_nonbroken) { |
203 | 0 | error_setg(errp, "MSI is not supported by interrupt controller"); |
204 | 0 | return -ENOTSUP; |
205 | 0 | } |
206 | | |
207 | 65 | MSI_DEV_PRINTF(dev, |
208 | 65 | "init offset: 0x%"PRIx8" vector: %"PRId8 |
209 | 65 | " 64bit %d mask %d\n", |
210 | 65 | offset, nr_vectors, msi64bit, msi_per_vector_mask); |
211 | | |
212 | 65 | assert(!(nr_vectors & (nr_vectors - 1))); /* power of 2 */ |
213 | 65 | assert(nr_vectors > 0); |
214 | 65 | assert(nr_vectors <= PCI_MSI_VECTORS_MAX); |
215 | | /* the nr of MSI vectors is up to 32 */ |
216 | 65 | vectors_order = ctz32(nr_vectors); |
217 | | |
218 | 65 | flags = vectors_order << ctz32(PCI_MSI_FLAGS_QMASK); |
219 | 65 | if (msi64bit) { |
220 | 65 | flags |= PCI_MSI_FLAGS_64BIT; |
221 | 65 | } |
222 | 65 | if (msi_per_vector_mask) { |
223 | 2 | flags |= PCI_MSI_FLAGS_MASKBIT; |
224 | 2 | } |
225 | | |
226 | 65 | cap_size = msi_cap_sizeof(flags); |
227 | 65 | config_offset = pci_add_capability(dev, PCI_CAP_ID_MSI, offset, |
228 | 65 | cap_size, errp); |
229 | 65 | if (config_offset < 0) { |
230 | 0 | return config_offset; |
231 | 0 | } |
232 | | |
233 | 65 | dev->msi_cap = config_offset; |
234 | 65 | dev->cap_present |= QEMU_PCI_CAP_MSI; |
235 | | |
236 | 65 | pci_set_word(dev->config + msi_flags_off(dev), flags); |
237 | 65 | pci_set_word(dev->wmask + msi_flags_off(dev), |
238 | 65 | PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE); |
239 | 65 | pci_set_long(dev->wmask + msi_address_lo_off(dev), |
240 | 65 | PCI_MSI_ADDRESS_LO_MASK); |
241 | 65 | if (msi64bit) { |
242 | 65 | pci_set_long(dev->wmask + msi_address_hi_off(dev), 0xffffffff); |
243 | 65 | } |
244 | 65 | pci_set_word(dev->wmask + msi_data_off(dev, msi64bit), 0xffff); |
245 | | |
246 | 65 | if (msi_per_vector_mask) { |
247 | | /* Make mask bits 0 to nr_vectors - 1 writable. */ |
248 | 2 | pci_set_long(dev->wmask + msi_mask_off(dev, msi64bit), |
249 | 2 | 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors)); |
250 | 2 | } |
251 | | |
252 | 65 | dev->msi_prepare_message = msi_prepare_message; |
253 | | |
254 | 65 | return 0; |
255 | 65 | } |
256 | | |
257 | | void msi_uninit(struct PCIDevice *dev) |
258 | 0 | { |
259 | 0 | uint16_t flags; |
260 | 0 | uint8_t cap_size; |
261 | |
|
262 | 0 | if (!msi_present(dev)) { |
263 | 0 | return; |
264 | 0 | } |
265 | 0 | flags = pci_get_word(dev->config + msi_flags_off(dev)); |
266 | 0 | cap_size = msi_cap_sizeof(flags); |
267 | 0 | pci_del_capability(dev, PCI_CAP_ID_MSI, cap_size); |
268 | 0 | dev->cap_present &= ~QEMU_PCI_CAP_MSI; |
269 | 0 | dev->msi_prepare_message = NULL; |
270 | |
|
271 | 0 | MSI_DEV_PRINTF(dev, "uninit\n"); |
272 | 0 | } |
273 | | |
274 | | void msi_reset(PCIDevice *dev) |
275 | 356k | { |
276 | 356k | uint16_t flags; |
277 | 356k | bool msi64bit; |
278 | | |
279 | 356k | if (!msi_present(dev)) { |
280 | 285k | return; |
281 | 285k | } |
282 | | |
283 | 70.1k | flags = pci_get_word(dev->config + msi_flags_off(dev)); |
284 | 70.1k | flags &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE); |
285 | 70.1k | msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
286 | | |
287 | 70.1k | pci_set_word(dev->config + msi_flags_off(dev), flags); |
288 | 70.1k | pci_set_long(dev->config + msi_address_lo_off(dev), 0); |
289 | 70.1k | if (msi64bit) { |
290 | 70.1k | pci_set_long(dev->config + msi_address_hi_off(dev), 0); |
291 | 70.1k | } |
292 | 70.1k | pci_set_word(dev->config + msi_data_off(dev, msi64bit), 0); |
293 | 70.1k | if (flags & PCI_MSI_FLAGS_MASKBIT) { |
294 | 13 | pci_set_long(dev->config + msi_mask_off(dev, msi64bit), 0); |
295 | 13 | pci_set_long(dev->config + msi_pending_off(dev, msi64bit), 0); |
296 | 13 | } |
297 | 70.1k | MSI_DEV_PRINTF(dev, "reset\n"); |
298 | 70.1k | } |
299 | | |
300 | | bool msi_is_masked(const PCIDevice *dev, unsigned int vector) |
301 | 0 | { |
302 | 0 | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
303 | 0 | uint32_t mask, data; |
304 | 0 | bool msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
305 | 0 | assert(vector < PCI_MSI_VECTORS_MAX); |
306 | |
|
307 | 0 | if (!(flags & PCI_MSI_FLAGS_MASKBIT)) { |
308 | 0 | return false; |
309 | 0 | } |
310 | | |
311 | 0 | data = pci_get_word(dev->config + msi_data_off(dev, msi64bit)); |
312 | 0 | if (xen_enabled() && xen_is_pirq_msi(data)) { |
313 | 0 | return false; |
314 | 0 | } |
315 | | |
316 | 0 | mask = pci_get_long(dev->config + |
317 | 0 | msi_mask_off(dev, flags & PCI_MSI_FLAGS_64BIT)); |
318 | 0 | return mask & (1U << vector); |
319 | 0 | } |
320 | | |
321 | | void msi_set_mask(PCIDevice *dev, int vector, bool mask, Error **errp) |
322 | 0 | { |
323 | 0 | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
324 | 0 | bool msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
325 | 0 | uint32_t irq_state, vector_mask, pending; |
326 | |
|
327 | 0 | if (vector >= PCI_MSI_VECTORS_MAX) { |
328 | 0 | error_setg(errp, "msi: vector %d not allocated. max vector is %d", |
329 | 0 | vector, (PCI_MSI_VECTORS_MAX - 1)); |
330 | 0 | return; |
331 | 0 | } |
332 | | |
333 | 0 | vector_mask = (1U << vector); |
334 | |
|
335 | 0 | irq_state = pci_get_long(dev->config + msi_mask_off(dev, msi64bit)); |
336 | |
|
337 | 0 | if (mask) { |
338 | 0 | irq_state |= vector_mask; |
339 | 0 | } else { |
340 | 0 | irq_state &= ~vector_mask; |
341 | 0 | } |
342 | |
|
343 | 0 | pci_set_long(dev->config + msi_mask_off(dev, msi64bit), irq_state); |
344 | |
|
345 | 0 | pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit)); |
346 | 0 | if (!mask && (pending & vector_mask)) { |
347 | 0 | pending &= ~vector_mask; |
348 | 0 | pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending); |
349 | 0 | msi_notify(dev, vector); |
350 | 0 | } |
351 | 0 | } |
352 | | |
353 | | void msi_notify(PCIDevice *dev, unsigned int vector) |
354 | 0 | { |
355 | 0 | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
356 | 0 | bool msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
357 | 0 | unsigned int nr_vectors = msi_nr_vectors(flags); |
358 | 0 | MSIMessage msg; |
359 | |
|
360 | 0 | assert(vector < nr_vectors); |
361 | 0 | if (msi_is_masked(dev, vector)) { |
362 | 0 | assert(flags & PCI_MSI_FLAGS_MASKBIT); |
363 | 0 | pci_long_test_and_set_mask( |
364 | 0 | dev->config + msi_pending_off(dev, msi64bit), 1U << vector); |
365 | 0 | MSI_DEV_PRINTF(dev, "pending vector 0x%x\n", vector); |
366 | 0 | return; |
367 | 0 | } |
368 | | |
369 | 0 | msg = msi_get_message(dev, vector); |
370 | |
|
371 | 0 | MSI_DEV_PRINTF(dev, |
372 | 0 | "notify vector 0x%x" |
373 | 0 | " address: 0x%"PRIx64" data: 0x%"PRIx32"\n", |
374 | 0 | vector, msg.address, msg.data); |
375 | 0 | msi_send_message(dev, msg); |
376 | 0 | } |
377 | | |
378 | | void msi_send_message(PCIDevice *dev, MSIMessage msg) |
379 | 15.3k | { |
380 | 15.3k | dev->msi_trigger(dev, msg); |
381 | 15.3k | } |
382 | | |
383 | | /* Normally called by pci_default_write_config(). */ |
384 | | void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len) |
385 | 1.72M | { |
386 | 1.72M | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
387 | 1.72M | bool msi64bit = flags & PCI_MSI_FLAGS_64BIT; |
388 | 1.72M | bool msi_per_vector_mask = flags & PCI_MSI_FLAGS_MASKBIT; |
389 | 1.72M | unsigned int nr_vectors; |
390 | 1.72M | uint8_t log_num_vecs; |
391 | 1.72M | uint8_t log_max_vecs; |
392 | 1.72M | unsigned int vector; |
393 | 1.72M | uint32_t pending; |
394 | | |
395 | 1.72M | if (!msi_present(dev) || |
396 | 1.72M | !ranges_overlap(addr, len, dev->msi_cap, msi_cap_sizeof(flags))) { |
397 | 1.72M | return; |
398 | 1.72M | } |
399 | | |
400 | | #ifdef MSI_DEBUG |
401 | | MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n", |
402 | | addr, val, len); |
403 | | MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32, |
404 | | flags, |
405 | | pci_get_long(dev->config + msi_address_lo_off(dev))); |
406 | | if (msi64bit) { |
407 | | fprintf(stderr, " address-hi: 0x%"PRIx32, |
408 | | pci_get_long(dev->config + msi_address_hi_off(dev))); |
409 | | } |
410 | | fprintf(stderr, " data: 0x%"PRIx16, |
411 | | pci_get_word(dev->config + msi_data_off(dev, msi64bit))); |
412 | | if (flags & PCI_MSI_FLAGS_MASKBIT) { |
413 | | fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32, |
414 | | pci_get_long(dev->config + msi_mask_off(dev, msi64bit)), |
415 | | pci_get_long(dev->config + msi_pending_off(dev, msi64bit))); |
416 | | } |
417 | | fprintf(stderr, "\n"); |
418 | | #endif |
419 | | |
420 | 0 | if (xen_mode == XEN_EMULATE) { |
421 | 0 | for (vector = 0; vector < msi_nr_vectors(flags); vector++) { |
422 | 0 | MSIMessage msg = msi_prepare_message(dev, vector); |
423 | |
|
424 | 0 | xen_evtchn_snoop_msi(dev, false, vector, msg.address, msg.data, |
425 | 0 | msi_is_masked(dev, vector)); |
426 | 0 | } |
427 | 0 | } |
428 | |
|
429 | 0 | if (!(flags & PCI_MSI_FLAGS_ENABLE)) { |
430 | 0 | return; |
431 | 0 | } |
432 | | |
433 | | /* |
434 | | * Now MSI is enabled, clear INTx# interrupts. |
435 | | * the driver is prohibited from writing enable bit to mask |
436 | | * a service request. But the guest OS could do this. |
437 | | * So we just discard the interrupts as moderate fallback. |
438 | | * |
439 | | * 6.8.3.3. Enabling Operation |
440 | | * While enabled for MSI or MSI-X operation, a function is prohibited |
441 | | * from using its INTx# pin (if implemented) to request |
442 | | * service (MSI, MSI-X, and INTx# are mutually exclusive). |
443 | | */ |
444 | 0 | pci_device_deassert_intx(dev); |
445 | | |
446 | | /* |
447 | | * nr_vectors might be set bigger than capable. So clamp it. |
448 | | * This is not legal by spec, so we can do anything we like, |
449 | | * just don't crash the host |
450 | | */ |
451 | 0 | log_num_vecs = |
452 | 0 | (flags & PCI_MSI_FLAGS_QSIZE) >> ctz32(PCI_MSI_FLAGS_QSIZE); |
453 | 0 | log_max_vecs = |
454 | 0 | (flags & PCI_MSI_FLAGS_QMASK) >> ctz32(PCI_MSI_FLAGS_QMASK); |
455 | 0 | if (log_num_vecs > log_max_vecs) { |
456 | 0 | flags &= ~PCI_MSI_FLAGS_QSIZE; |
457 | 0 | flags |= log_max_vecs << ctz32(PCI_MSI_FLAGS_QSIZE); |
458 | 0 | pci_set_word(dev->config + msi_flags_off(dev), flags); |
459 | 0 | } |
460 | |
|
461 | 0 | if (!msi_per_vector_mask) { |
462 | | /* if per vector masking isn't supported, |
463 | | there is no pending interrupt. */ |
464 | 0 | return; |
465 | 0 | } |
466 | | |
467 | 0 | nr_vectors = msi_nr_vectors(flags); |
468 | | |
469 | | /* This will discard pending interrupts, if any. */ |
470 | 0 | pending = pci_get_long(dev->config + msi_pending_off(dev, msi64bit)); |
471 | 0 | pending &= 0xffffffff >> (PCI_MSI_VECTORS_MAX - nr_vectors); |
472 | 0 | pci_set_long(dev->config + msi_pending_off(dev, msi64bit), pending); |
473 | | |
474 | | /* deliver pending interrupts which are unmasked */ |
475 | 0 | for (vector = 0; vector < nr_vectors; ++vector) { |
476 | 0 | if (msi_is_masked(dev, vector) || !(pending & (1U << vector))) { |
477 | 0 | continue; |
478 | 0 | } |
479 | | |
480 | 0 | pci_long_test_and_clear_mask( |
481 | 0 | dev->config + msi_pending_off(dev, msi64bit), 1U << vector); |
482 | 0 | msi_notify(dev, vector); |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | | unsigned int msi_nr_vectors_allocated(const PCIDevice *dev) |
487 | 0 | { |
488 | 0 | uint16_t flags = pci_get_word(dev->config + msi_flags_off(dev)); |
489 | 0 | return msi_nr_vectors(flags); |
490 | 0 | } |