root / hw / pci / msix.c @ 1de7afc9
History | View | Annotate | Download (15.7 kB)
1 | 02eb84d0 | Michael S. Tsirkin | /*
|
---|---|---|---|
2 | 02eb84d0 | Michael S. Tsirkin | * MSI-X device support
|
3 | 02eb84d0 | Michael S. Tsirkin | *
|
4 | 02eb84d0 | Michael S. Tsirkin | * This module includes support for MSI-X in pci devices.
|
5 | 02eb84d0 | Michael S. Tsirkin | *
|
6 | 02eb84d0 | Michael S. Tsirkin | * Author: Michael S. Tsirkin <mst@redhat.com>
|
7 | 02eb84d0 | Michael S. Tsirkin | *
|
8 | 02eb84d0 | Michael S. Tsirkin | * Copyright (c) 2009, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
|
9 | 02eb84d0 | Michael S. Tsirkin | *
|
10 | 02eb84d0 | Michael S. Tsirkin | * This work is licensed under the terms of the GNU GPL, version 2. See
|
11 | 02eb84d0 | Michael S. Tsirkin | * the COPYING file in the top-level directory.
|
12 | 6b620ca3 | Paolo Bonzini | *
|
13 | 6b620ca3 | Paolo Bonzini | * Contributions after 2012-01-13 are licensed under the terms of the
|
14 | 6b620ca3 | Paolo Bonzini | * GNU GPL, version 2 or (at your option) any later version.
|
15 | 02eb84d0 | Michael S. Tsirkin | */
|
16 | 02eb84d0 | Michael S. Tsirkin | |
17 | c759b24f | Michael S. Tsirkin | #include "hw/hw.h" |
18 | c759b24f | Michael S. Tsirkin | #include "hw/pci/msi.h" |
19 | c759b24f | Michael S. Tsirkin | #include "hw/pci/msix.h" |
20 | c759b24f | Michael S. Tsirkin | #include "hw/pci/pci.h" |
21 | 1de7afc9 | Paolo Bonzini | #include "qemu/range.h" |
22 | 02eb84d0 | Michael S. Tsirkin | |
23 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_CAP_LENGTH 12 |
24 | 02eb84d0 | Michael S. Tsirkin | |
25 | 2760952b | Michael S. Tsirkin | /* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
|
26 | 2760952b | Michael S. Tsirkin | #define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) |
27 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) |
28 | 5b5cb086 | Michael S. Tsirkin | #define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) |
29 | 02eb84d0 | Michael S. Tsirkin | |
30 | bc4caf49 | Jan Kiszka | static MSIMessage msix_get_message(PCIDevice *dev, unsigned vector) |
31 | bc4caf49 | Jan Kiszka | { |
32 | d35e428c | Alex Williamson | uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE; |
33 | bc4caf49 | Jan Kiszka | MSIMessage msg; |
34 | bc4caf49 | Jan Kiszka | |
35 | bc4caf49 | Jan Kiszka | msg.address = pci_get_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR); |
36 | bc4caf49 | Jan Kiszka | msg.data = pci_get_long(table_entry + PCI_MSIX_ENTRY_DATA); |
37 | bc4caf49 | Jan Kiszka | return msg;
|
38 | bc4caf49 | Jan Kiszka | } |
39 | 02eb84d0 | Michael S. Tsirkin | |
40 | 932d4a42 | Alexey Kardashevskiy | /*
|
41 | 932d4a42 | Alexey Kardashevskiy | * Special API for POWER to configure the vectors through
|
42 | 932d4a42 | Alexey Kardashevskiy | * a side channel. Should never be used by devices.
|
43 | 932d4a42 | Alexey Kardashevskiy | */
|
44 | 932d4a42 | Alexey Kardashevskiy | void msix_set_message(PCIDevice *dev, int vector, struct MSIMessage msg) |
45 | 932d4a42 | Alexey Kardashevskiy | { |
46 | 932d4a42 | Alexey Kardashevskiy | uint8_t *table_entry = dev->msix_table + vector * PCI_MSIX_ENTRY_SIZE; |
47 | 932d4a42 | Alexey Kardashevskiy | |
48 | 932d4a42 | Alexey Kardashevskiy | pci_set_quad(table_entry + PCI_MSIX_ENTRY_LOWER_ADDR, msg.address); |
49 | 932d4a42 | Alexey Kardashevskiy | pci_set_long(table_entry + PCI_MSIX_ENTRY_DATA, msg.data); |
50 | 932d4a42 | Alexey Kardashevskiy | table_entry[PCI_MSIX_ENTRY_VECTOR_CTRL] &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; |
51 | 932d4a42 | Alexey Kardashevskiy | } |
52 | 932d4a42 | Alexey Kardashevskiy | |
53 | 02eb84d0 | Michael S. Tsirkin | static uint8_t msix_pending_mask(int vector) |
54 | 02eb84d0 | Michael S. Tsirkin | { |
55 | 02eb84d0 | Michael S. Tsirkin | return 1 << (vector % 8); |
56 | 02eb84d0 | Michael S. Tsirkin | } |
57 | 02eb84d0 | Michael S. Tsirkin | |
58 | 02eb84d0 | Michael S. Tsirkin | static uint8_t *msix_pending_byte(PCIDevice *dev, int vector) |
59 | 02eb84d0 | Michael S. Tsirkin | { |
60 | d35e428c | Alex Williamson | return dev->msix_pba + vector / 8; |
61 | 02eb84d0 | Michael S. Tsirkin | } |
62 | 02eb84d0 | Michael S. Tsirkin | |
63 | 02eb84d0 | Michael S. Tsirkin | static int msix_is_pending(PCIDevice *dev, int vector) |
64 | 02eb84d0 | Michael S. Tsirkin | { |
65 | 02eb84d0 | Michael S. Tsirkin | return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
|
66 | 02eb84d0 | Michael S. Tsirkin | } |
67 | 02eb84d0 | Michael S. Tsirkin | |
68 | 02eb84d0 | Michael S. Tsirkin | static void msix_set_pending(PCIDevice *dev, int vector) |
69 | 02eb84d0 | Michael S. Tsirkin | { |
70 | 02eb84d0 | Michael S. Tsirkin | *msix_pending_byte(dev, vector) |= msix_pending_mask(vector); |
71 | 02eb84d0 | Michael S. Tsirkin | } |
72 | 02eb84d0 | Michael S. Tsirkin | |
73 | 02eb84d0 | Michael S. Tsirkin | static void msix_clr_pending(PCIDevice *dev, int vector) |
74 | 02eb84d0 | Michael S. Tsirkin | { |
75 | 02eb84d0 | Michael S. Tsirkin | *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector); |
76 | 02eb84d0 | Michael S. Tsirkin | } |
77 | 02eb84d0 | Michael S. Tsirkin | |
78 | ae392c41 | Michael S. Tsirkin | static bool msix_vector_masked(PCIDevice *dev, int vector, bool fmask) |
79 | 02eb84d0 | Michael S. Tsirkin | { |
80 | ae392c41 | Michael S. Tsirkin | unsigned offset = vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL;
|
81 | d35e428c | Alex Williamson | return fmask || dev->msix_table[offset] & PCI_MSIX_ENTRY_CTRL_MASKBIT;
|
82 | 5b5cb086 | Michael S. Tsirkin | } |
83 | 5b5cb086 | Michael S. Tsirkin | |
84 | ae392c41 | Michael S. Tsirkin | static bool msix_is_masked(PCIDevice *dev, int vector) |
85 | 5b5cb086 | Michael S. Tsirkin | { |
86 | ae392c41 | Michael S. Tsirkin | return msix_vector_masked(dev, vector, dev->msix_function_masked);
|
87 | ae392c41 | Michael S. Tsirkin | } |
88 | ae392c41 | Michael S. Tsirkin | |
89 | 2cdfe53c | Jan Kiszka | static void msix_fire_vector_notifier(PCIDevice *dev, |
90 | 2cdfe53c | Jan Kiszka | unsigned int vector, bool is_masked) |
91 | 2cdfe53c | Jan Kiszka | { |
92 | 2cdfe53c | Jan Kiszka | MSIMessage msg; |
93 | 2cdfe53c | Jan Kiszka | int ret;
|
94 | 2cdfe53c | Jan Kiszka | |
95 | 2cdfe53c | Jan Kiszka | if (!dev->msix_vector_use_notifier) {
|
96 | 2cdfe53c | Jan Kiszka | return;
|
97 | 2cdfe53c | Jan Kiszka | } |
98 | 2cdfe53c | Jan Kiszka | if (is_masked) {
|
99 | 2cdfe53c | Jan Kiszka | dev->msix_vector_release_notifier(dev, vector); |
100 | 2cdfe53c | Jan Kiszka | } else {
|
101 | 2cdfe53c | Jan Kiszka | msg = msix_get_message(dev, vector); |
102 | 2cdfe53c | Jan Kiszka | ret = dev->msix_vector_use_notifier(dev, vector, msg); |
103 | 2cdfe53c | Jan Kiszka | assert(ret >= 0);
|
104 | 2cdfe53c | Jan Kiszka | } |
105 | 2cdfe53c | Jan Kiszka | } |
106 | 2cdfe53c | Jan Kiszka | |
107 | ae392c41 | Michael S. Tsirkin | static void msix_handle_mask_update(PCIDevice *dev, int vector, bool was_masked) |
108 | ae392c41 | Michael S. Tsirkin | { |
109 | ae392c41 | Michael S. Tsirkin | bool is_masked = msix_is_masked(dev, vector);
|
110 | 2cdfe53c | Jan Kiszka | |
111 | ae392c41 | Michael S. Tsirkin | if (is_masked == was_masked) {
|
112 | ae392c41 | Michael S. Tsirkin | return;
|
113 | ae392c41 | Michael S. Tsirkin | } |
114 | ae392c41 | Michael S. Tsirkin | |
115 | 2cdfe53c | Jan Kiszka | msix_fire_vector_notifier(dev, vector, is_masked); |
116 | 2cdfe53c | Jan Kiszka | |
117 | ae392c41 | Michael S. Tsirkin | if (!is_masked && msix_is_pending(dev, vector)) {
|
118 | 5b5cb086 | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
119 | 5b5cb086 | Michael S. Tsirkin | msix_notify(dev, vector); |
120 | 5b5cb086 | Michael S. Tsirkin | } |
121 | 5b5cb086 | Michael S. Tsirkin | } |
122 | 5b5cb086 | Michael S. Tsirkin | |
123 | 50322249 | Michael S. Tsirkin | static void msix_update_function_masked(PCIDevice *dev) |
124 | 50322249 | Michael S. Tsirkin | { |
125 | 50322249 | Michael S. Tsirkin | dev->msix_function_masked = !msix_enabled(dev) || |
126 | 50322249 | Michael S. Tsirkin | (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK); |
127 | 50322249 | Michael S. Tsirkin | } |
128 | 50322249 | Michael S. Tsirkin | |
129 | 5b5cb086 | Michael S. Tsirkin | /* Handle MSI-X capability config write. */
|
130 | 5b5cb086 | Michael S. Tsirkin | void msix_write_config(PCIDevice *dev, uint32_t addr,
|
131 | 5b5cb086 | Michael S. Tsirkin | uint32_t val, int len)
|
132 | 5b5cb086 | Michael S. Tsirkin | { |
133 | 5b5cb086 | Michael S. Tsirkin | unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
|
134 | 5b5cb086 | Michael S. Tsirkin | int vector;
|
135 | 50322249 | Michael S. Tsirkin | bool was_masked;
|
136 | 5b5cb086 | Michael S. Tsirkin | |
137 | 7c9958b0 | Jan Kiszka | if (!msix_present(dev) || !range_covers_byte(addr, len, enable_pos)) {
|
138 | 5b5cb086 | Michael S. Tsirkin | return;
|
139 | 5b5cb086 | Michael S. Tsirkin | } |
140 | 5b5cb086 | Michael S. Tsirkin | |
141 | 50322249 | Michael S. Tsirkin | was_masked = dev->msix_function_masked; |
142 | 50322249 | Michael S. Tsirkin | msix_update_function_masked(dev); |
143 | 50322249 | Michael S. Tsirkin | |
144 | 5b5cb086 | Michael S. Tsirkin | if (!msix_enabled(dev)) {
|
145 | 5b5cb086 | Michael S. Tsirkin | return;
|
146 | 5b5cb086 | Michael S. Tsirkin | } |
147 | 5b5cb086 | Michael S. Tsirkin | |
148 | e407bf13 | Isaku Yamahata | pci_device_deassert_intx(dev); |
149 | 5b5cb086 | Michael S. Tsirkin | |
150 | 50322249 | Michael S. Tsirkin | if (dev->msix_function_masked == was_masked) {
|
151 | 5b5cb086 | Michael S. Tsirkin | return;
|
152 | 5b5cb086 | Michael S. Tsirkin | } |
153 | 5b5cb086 | Michael S. Tsirkin | |
154 | 5b5cb086 | Michael S. Tsirkin | for (vector = 0; vector < dev->msix_entries_nr; ++vector) { |
155 | ae392c41 | Michael S. Tsirkin | msix_handle_mask_update(dev, vector, |
156 | ae392c41 | Michael S. Tsirkin | msix_vector_masked(dev, vector, was_masked)); |
157 | 5b5cb086 | Michael S. Tsirkin | } |
158 | 02eb84d0 | Michael S. Tsirkin | } |
159 | 02eb84d0 | Michael S. Tsirkin | |
160 | a8170e5e | Avi Kivity | static uint64_t msix_table_mmio_read(void *opaque, hwaddr addr, |
161 | d35e428c | Alex Williamson | unsigned size)
|
162 | eebcb0a7 | Alex Williamson | { |
163 | eebcb0a7 | Alex Williamson | PCIDevice *dev = opaque; |
164 | eebcb0a7 | Alex Williamson | |
165 | d35e428c | Alex Williamson | return pci_get_long(dev->msix_table + addr);
|
166 | eebcb0a7 | Alex Williamson | } |
167 | eebcb0a7 | Alex Williamson | |
168 | a8170e5e | Avi Kivity | static void msix_table_mmio_write(void *opaque, hwaddr addr, |
169 | d35e428c | Alex Williamson | uint64_t val, unsigned size)
|
170 | 02eb84d0 | Michael S. Tsirkin | { |
171 | 02eb84d0 | Michael S. Tsirkin | PCIDevice *dev = opaque; |
172 | d35e428c | Alex Williamson | int vector = addr / PCI_MSIX_ENTRY_SIZE;
|
173 | ae392c41 | Michael S. Tsirkin | bool was_masked;
|
174 | 9a93b617 | Michael S. Tsirkin | |
175 | ae392c41 | Michael S. Tsirkin | was_masked = msix_is_masked(dev, vector); |
176 | d35e428c | Alex Williamson | pci_set_long(dev->msix_table + addr, val); |
177 | ae392c41 | Michael S. Tsirkin | msix_handle_mask_update(dev, vector, was_masked); |
178 | 02eb84d0 | Michael S. Tsirkin | } |
179 | 02eb84d0 | Michael S. Tsirkin | |
180 | d35e428c | Alex Williamson | static const MemoryRegionOps msix_table_mmio_ops = { |
181 | d35e428c | Alex Williamson | .read = msix_table_mmio_read, |
182 | d35e428c | Alex Williamson | .write = msix_table_mmio_write, |
183 | 68d1e1f5 | Alexander Graf | .endianness = DEVICE_LITTLE_ENDIAN, |
184 | d35e428c | Alex Williamson | .valid = { |
185 | d35e428c | Alex Williamson | .min_access_size = 4,
|
186 | d35e428c | Alex Williamson | .max_access_size = 4,
|
187 | d35e428c | Alex Williamson | }, |
188 | d35e428c | Alex Williamson | }; |
189 | d35e428c | Alex Williamson | |
190 | a8170e5e | Avi Kivity | static uint64_t msix_pba_mmio_read(void *opaque, hwaddr addr, |
191 | d35e428c | Alex Williamson | unsigned size)
|
192 | d35e428c | Alex Williamson | { |
193 | d35e428c | Alex Williamson | PCIDevice *dev = opaque; |
194 | d35e428c | Alex Williamson | |
195 | d35e428c | Alex Williamson | return pci_get_long(dev->msix_pba + addr);
|
196 | d35e428c | Alex Williamson | } |
197 | d35e428c | Alex Williamson | |
198 | d35e428c | Alex Williamson | static const MemoryRegionOps msix_pba_mmio_ops = { |
199 | d35e428c | Alex Williamson | .read = msix_pba_mmio_read, |
200 | 68d1e1f5 | Alexander Graf | .endianness = DEVICE_LITTLE_ENDIAN, |
201 | 95524ae8 | Avi Kivity | .valid = { |
202 | 95524ae8 | Avi Kivity | .min_access_size = 4,
|
203 | 95524ae8 | Avi Kivity | .max_access_size = 4,
|
204 | 95524ae8 | Avi Kivity | }, |
205 | 02eb84d0 | Michael S. Tsirkin | }; |
206 | 02eb84d0 | Michael S. Tsirkin | |
207 | ae1be0bb | Michael S. Tsirkin | static void msix_mask_all(struct PCIDevice *dev, unsigned nentries) |
208 | ae1be0bb | Michael S. Tsirkin | { |
209 | ae1be0bb | Michael S. Tsirkin | int vector;
|
210 | 5b5f1330 | Jan Kiszka | |
211 | ae1be0bb | Michael S. Tsirkin | for (vector = 0; vector < nentries; ++vector) { |
212 | 01731cfb | Jan Kiszka | unsigned offset =
|
213 | 01731cfb | Jan Kiszka | vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL; |
214 | 5b5f1330 | Jan Kiszka | bool was_masked = msix_is_masked(dev, vector);
|
215 | 5b5f1330 | Jan Kiszka | |
216 | d35e428c | Alex Williamson | dev->msix_table[offset] |= PCI_MSIX_ENTRY_CTRL_MASKBIT; |
217 | 5b5f1330 | Jan Kiszka | msix_handle_mask_update(dev, vector, was_masked); |
218 | ae1be0bb | Michael S. Tsirkin | } |
219 | ae1be0bb | Michael S. Tsirkin | } |
220 | ae1be0bb | Michael S. Tsirkin | |
221 | 5a2c2029 | Alex Williamson | /* Initialize the MSI-X structures */
|
222 | 02eb84d0 | Michael S. Tsirkin | int msix_init(struct PCIDevice *dev, unsigned short nentries, |
223 | 5a2c2029 | Alex Williamson | MemoryRegion *table_bar, uint8_t table_bar_nr, |
224 | 5a2c2029 | Alex Williamson | unsigned table_offset, MemoryRegion *pba_bar,
|
225 | 5a2c2029 | Alex Williamson | uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos)
|
226 | 02eb84d0 | Michael S. Tsirkin | { |
227 | 5a2c2029 | Alex Williamson | int cap;
|
228 | d35e428c | Alex Williamson | unsigned table_size, pba_size;
|
229 | 5a2c2029 | Alex Williamson | uint8_t *config; |
230 | 60ba3cc2 | Jan Kiszka | |
231 | 02eb84d0 | Michael S. Tsirkin | /* Nothing to do if MSI is not supported by interrupt controller */
|
232 | 60ba3cc2 | Jan Kiszka | if (!msi_supported) {
|
233 | 02eb84d0 | Michael S. Tsirkin | return -ENOTSUP;
|
234 | 60ba3cc2 | Jan Kiszka | } |
235 | 5a2c2029 | Alex Williamson | |
236 | 5a2c2029 | Alex Williamson | if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1) { |
237 | 02eb84d0 | Michael S. Tsirkin | return -EINVAL;
|
238 | 5a2c2029 | Alex Williamson | } |
239 | 02eb84d0 | Michael S. Tsirkin | |
240 | d35e428c | Alex Williamson | table_size = nentries * PCI_MSIX_ENTRY_SIZE; |
241 | d35e428c | Alex Williamson | pba_size = QEMU_ALIGN_UP(nentries, 64) / 8; |
242 | d35e428c | Alex Williamson | |
243 | 5a2c2029 | Alex Williamson | /* Sanity test: table & pba don't overlap, fit within BARs, min aligned */
|
244 | 5a2c2029 | Alex Williamson | if ((table_bar_nr == pba_bar_nr &&
|
245 | 5a2c2029 | Alex Williamson | ranges_overlap(table_offset, table_size, pba_offset, pba_size)) || |
246 | 5a2c2029 | Alex Williamson | table_offset + table_size > memory_region_size(table_bar) || |
247 | 5a2c2029 | Alex Williamson | pba_offset + pba_size > memory_region_size(pba_bar) || |
248 | 5a2c2029 | Alex Williamson | (table_offset | pba_offset) & PCI_MSIX_FLAGS_BIRMASK) { |
249 | 5a2c2029 | Alex Williamson | return -EINVAL;
|
250 | 5a2c2029 | Alex Williamson | } |
251 | 5a2c2029 | Alex Williamson | |
252 | 5a2c2029 | Alex Williamson | cap = pci_add_capability(dev, PCI_CAP_ID_MSIX, cap_pos, MSIX_CAP_LENGTH); |
253 | 5a2c2029 | Alex Williamson | if (cap < 0) { |
254 | 5a2c2029 | Alex Williamson | return cap;
|
255 | 5a2c2029 | Alex Williamson | } |
256 | 5a2c2029 | Alex Williamson | |
257 | 5a2c2029 | Alex Williamson | dev->msix_cap = cap; |
258 | 5a2c2029 | Alex Williamson | dev->cap_present |= QEMU_PCI_CAP_MSIX; |
259 | 5a2c2029 | Alex Williamson | config = dev->config + cap; |
260 | 5a2c2029 | Alex Williamson | |
261 | 5a2c2029 | Alex Williamson | pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
|
262 | 5a2c2029 | Alex Williamson | dev->msix_entries_nr = nentries; |
263 | 5a2c2029 | Alex Williamson | dev->msix_function_masked = true;
|
264 | 5a2c2029 | Alex Williamson | |
265 | 5a2c2029 | Alex Williamson | pci_set_long(config + PCI_MSIX_TABLE, table_offset | table_bar_nr); |
266 | 5a2c2029 | Alex Williamson | pci_set_long(config + PCI_MSIX_PBA, pba_offset | pba_bar_nr); |
267 | 5a2c2029 | Alex Williamson | |
268 | 5a2c2029 | Alex Williamson | /* Make flags bit writable. */
|
269 | 5a2c2029 | Alex Williamson | dev->wmask[cap + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK | |
270 | 5a2c2029 | Alex Williamson | MSIX_MASKALL_MASK; |
271 | 02eb84d0 | Michael S. Tsirkin | |
272 | d35e428c | Alex Williamson | dev->msix_table = g_malloc0(table_size); |
273 | d35e428c | Alex Williamson | dev->msix_pba = g_malloc0(pba_size); |
274 | 5a2c2029 | Alex Williamson | dev->msix_entry_used = g_malloc0(nentries * sizeof *dev->msix_entry_used);
|
275 | 5a2c2029 | Alex Williamson | |
276 | ae1be0bb | Michael S. Tsirkin | msix_mask_all(dev, nentries); |
277 | 02eb84d0 | Michael S. Tsirkin | |
278 | d35e428c | Alex Williamson | memory_region_init_io(&dev->msix_table_mmio, &msix_table_mmio_ops, dev, |
279 | d35e428c | Alex Williamson | "msix-table", table_size);
|
280 | 5a2c2029 | Alex Williamson | memory_region_add_subregion(table_bar, table_offset, &dev->msix_table_mmio); |
281 | d35e428c | Alex Williamson | memory_region_init_io(&dev->msix_pba_mmio, &msix_pba_mmio_ops, dev, |
282 | d35e428c | Alex Williamson | "msix-pba", pba_size);
|
283 | 5a2c2029 | Alex Williamson | memory_region_add_subregion(pba_bar, pba_offset, &dev->msix_pba_mmio); |
284 | 02eb84d0 | Michael S. Tsirkin | |
285 | 02eb84d0 | Michael S. Tsirkin | return 0; |
286 | 02eb84d0 | Michael S. Tsirkin | } |
287 | 02eb84d0 | Michael S. Tsirkin | |
288 | 53f94925 | Alex Williamson | int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries, |
289 | 53f94925 | Alex Williamson | uint8_t bar_nr) |
290 | 53f94925 | Alex Williamson | { |
291 | 53f94925 | Alex Williamson | int ret;
|
292 | 53f94925 | Alex Williamson | char *name;
|
293 | 53f94925 | Alex Williamson | |
294 | 53f94925 | Alex Williamson | /*
|
295 | 53f94925 | Alex Williamson | * Migration compatibility dictates that this remains a 4k
|
296 | 53f94925 | Alex Williamson | * BAR with the vector table in the lower half and PBA in
|
297 | 53f94925 | Alex Williamson | * the upper half. Do not use these elsewhere!
|
298 | 53f94925 | Alex Williamson | */
|
299 | 53f94925 | Alex Williamson | #define MSIX_EXCLUSIVE_BAR_SIZE 4096 |
300 | 5a2c2029 | Alex Williamson | #define MSIX_EXCLUSIVE_BAR_TABLE_OFFSET 0 |
301 | 53f94925 | Alex Williamson | #define MSIX_EXCLUSIVE_BAR_PBA_OFFSET (MSIX_EXCLUSIVE_BAR_SIZE / 2) |
302 | 5a2c2029 | Alex Williamson | #define MSIX_EXCLUSIVE_CAP_OFFSET 0 |
303 | 53f94925 | Alex Williamson | |
304 | 53f94925 | Alex Williamson | if (nentries * PCI_MSIX_ENTRY_SIZE > MSIX_EXCLUSIVE_BAR_PBA_OFFSET) {
|
305 | 53f94925 | Alex Williamson | return -EINVAL;
|
306 | 53f94925 | Alex Williamson | } |
307 | 53f94925 | Alex Williamson | |
308 | 5f893b4e | Gerd Hoffmann | name = g_strdup_printf("%s-msix", dev->name);
|
309 | 53f94925 | Alex Williamson | memory_region_init(&dev->msix_exclusive_bar, name, MSIX_EXCLUSIVE_BAR_SIZE); |
310 | 5f893b4e | Gerd Hoffmann | g_free(name); |
311 | 53f94925 | Alex Williamson | |
312 | 53f94925 | Alex Williamson | ret = msix_init(dev, nentries, &dev->msix_exclusive_bar, bar_nr, |
313 | 5a2c2029 | Alex Williamson | MSIX_EXCLUSIVE_BAR_TABLE_OFFSET, &dev->msix_exclusive_bar, |
314 | 5a2c2029 | Alex Williamson | bar_nr, MSIX_EXCLUSIVE_BAR_PBA_OFFSET, |
315 | 5a2c2029 | Alex Williamson | MSIX_EXCLUSIVE_CAP_OFFSET); |
316 | 53f94925 | Alex Williamson | if (ret) {
|
317 | 53f94925 | Alex Williamson | memory_region_destroy(&dev->msix_exclusive_bar); |
318 | 53f94925 | Alex Williamson | return ret;
|
319 | 53f94925 | Alex Williamson | } |
320 | 53f94925 | Alex Williamson | |
321 | 53f94925 | Alex Williamson | pci_register_bar(dev, bar_nr, PCI_BASE_ADDRESS_SPACE_MEMORY, |
322 | 53f94925 | Alex Williamson | &dev->msix_exclusive_bar); |
323 | 53f94925 | Alex Williamson | |
324 | 53f94925 | Alex Williamson | return 0; |
325 | 53f94925 | Alex Williamson | } |
326 | 53f94925 | Alex Williamson | |
327 | 98304c84 | Michael S. Tsirkin | static void msix_free_irq_entries(PCIDevice *dev) |
328 | 98304c84 | Michael S. Tsirkin | { |
329 | 98304c84 | Michael S. Tsirkin | int vector;
|
330 | 98304c84 | Michael S. Tsirkin | |
331 | 98304c84 | Michael S. Tsirkin | for (vector = 0; vector < dev->msix_entries_nr; ++vector) { |
332 | 98304c84 | Michael S. Tsirkin | dev->msix_entry_used[vector] = 0;
|
333 | 98304c84 | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
334 | 98304c84 | Michael S. Tsirkin | } |
335 | 98304c84 | Michael S. Tsirkin | } |
336 | 98304c84 | Michael S. Tsirkin | |
337 | 3cac001e | Michael S. Tsirkin | static void msix_clear_all_vectors(PCIDevice *dev) |
338 | 3cac001e | Michael S. Tsirkin | { |
339 | 3cac001e | Michael S. Tsirkin | int vector;
|
340 | 3cac001e | Michael S. Tsirkin | |
341 | 3cac001e | Michael S. Tsirkin | for (vector = 0; vector < dev->msix_entries_nr; ++vector) { |
342 | 3cac001e | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
343 | 3cac001e | Michael S. Tsirkin | } |
344 | 3cac001e | Michael S. Tsirkin | } |
345 | 3cac001e | Michael S. Tsirkin | |
346 | 02eb84d0 | Michael S. Tsirkin | /* Clean up resources for the device. */
|
347 | 572992ee | Alex Williamson | void msix_uninit(PCIDevice *dev, MemoryRegion *table_bar, MemoryRegion *pba_bar)
|
348 | 02eb84d0 | Michael S. Tsirkin | { |
349 | 44701ab7 | Jan Kiszka | if (!msix_present(dev)) {
|
350 | 572992ee | Alex Williamson | return;
|
351 | 44701ab7 | Jan Kiszka | } |
352 | 02eb84d0 | Michael S. Tsirkin | pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH); |
353 | 02eb84d0 | Michael S. Tsirkin | dev->msix_cap = 0;
|
354 | 02eb84d0 | Michael S. Tsirkin | msix_free_irq_entries(dev); |
355 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entries_nr = 0;
|
356 | 5a2c2029 | Alex Williamson | memory_region_del_subregion(pba_bar, &dev->msix_pba_mmio); |
357 | d35e428c | Alex Williamson | memory_region_destroy(&dev->msix_pba_mmio); |
358 | d35e428c | Alex Williamson | g_free(dev->msix_pba); |
359 | d35e428c | Alex Williamson | dev->msix_pba = NULL;
|
360 | 5a2c2029 | Alex Williamson | memory_region_del_subregion(table_bar, &dev->msix_table_mmio); |
361 | d35e428c | Alex Williamson | memory_region_destroy(&dev->msix_table_mmio); |
362 | d35e428c | Alex Williamson | g_free(dev->msix_table); |
363 | d35e428c | Alex Williamson | dev->msix_table = NULL;
|
364 | 7267c094 | Anthony Liguori | g_free(dev->msix_entry_used); |
365 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entry_used = NULL;
|
366 | 02eb84d0 | Michael S. Tsirkin | dev->cap_present &= ~QEMU_PCI_CAP_MSIX; |
367 | 02eb84d0 | Michael S. Tsirkin | } |
368 | 02eb84d0 | Michael S. Tsirkin | |
369 | 53f94925 | Alex Williamson | void msix_uninit_exclusive_bar(PCIDevice *dev)
|
370 | 53f94925 | Alex Williamson | { |
371 | 53f94925 | Alex Williamson | if (msix_present(dev)) {
|
372 | 5a2c2029 | Alex Williamson | msix_uninit(dev, &dev->msix_exclusive_bar, &dev->msix_exclusive_bar); |
373 | 53f94925 | Alex Williamson | memory_region_destroy(&dev->msix_exclusive_bar); |
374 | 53f94925 | Alex Williamson | } |
375 | 53f94925 | Alex Williamson | } |
376 | 53f94925 | Alex Williamson | |
377 | 02eb84d0 | Michael S. Tsirkin | void msix_save(PCIDevice *dev, QEMUFile *f)
|
378 | 02eb84d0 | Michael S. Tsirkin | { |
379 | 9a3e12c8 | Michael S. Tsirkin | unsigned n = dev->msix_entries_nr;
|
380 | 9a3e12c8 | Michael S. Tsirkin | |
381 | 44701ab7 | Jan Kiszka | if (!msix_present(dev)) {
|
382 | 9a3e12c8 | Michael S. Tsirkin | return;
|
383 | 72755a70 | Michael S. Tsirkin | } |
384 | 9a3e12c8 | Michael S. Tsirkin | |
385 | d35e428c | Alex Williamson | qemu_put_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE); |
386 | d35e428c | Alex Williamson | qemu_put_buffer(f, dev->msix_pba, (n + 7) / 8); |
387 | 02eb84d0 | Michael S. Tsirkin | } |
388 | 02eb84d0 | Michael S. Tsirkin | |
389 | 02eb84d0 | Michael S. Tsirkin | /* Should be called after restoring the config space. */
|
390 | 02eb84d0 | Michael S. Tsirkin | void msix_load(PCIDevice *dev, QEMUFile *f)
|
391 | 02eb84d0 | Michael S. Tsirkin | { |
392 | 02eb84d0 | Michael S. Tsirkin | unsigned n = dev->msix_entries_nr;
|
393 | 2cdfe53c | Jan Kiszka | unsigned int vector; |
394 | 02eb84d0 | Michael S. Tsirkin | |
395 | 44701ab7 | Jan Kiszka | if (!msix_present(dev)) {
|
396 | 02eb84d0 | Michael S. Tsirkin | return;
|
397 | 98846d73 | Blue Swirl | } |
398 | 02eb84d0 | Michael S. Tsirkin | |
399 | 3cac001e | Michael S. Tsirkin | msix_clear_all_vectors(dev); |
400 | d35e428c | Alex Williamson | qemu_get_buffer(f, dev->msix_table, n * PCI_MSIX_ENTRY_SIZE); |
401 | d35e428c | Alex Williamson | qemu_get_buffer(f, dev->msix_pba, (n + 7) / 8); |
402 | 50322249 | Michael S. Tsirkin | msix_update_function_masked(dev); |
403 | 2cdfe53c | Jan Kiszka | |
404 | 2cdfe53c | Jan Kiszka | for (vector = 0; vector < n; vector++) { |
405 | 2cdfe53c | Jan Kiszka | msix_handle_mask_update(dev, vector, true);
|
406 | 2cdfe53c | Jan Kiszka | } |
407 | 02eb84d0 | Michael S. Tsirkin | } |
408 | 02eb84d0 | Michael S. Tsirkin | |
409 | 02eb84d0 | Michael S. Tsirkin | /* Does device support MSI-X? */
|
410 | 02eb84d0 | Michael S. Tsirkin | int msix_present(PCIDevice *dev)
|
411 | 02eb84d0 | Michael S. Tsirkin | { |
412 | 02eb84d0 | Michael S. Tsirkin | return dev->cap_present & QEMU_PCI_CAP_MSIX;
|
413 | 02eb84d0 | Michael S. Tsirkin | } |
414 | 02eb84d0 | Michael S. Tsirkin | |
415 | 02eb84d0 | Michael S. Tsirkin | /* Is MSI-X enabled? */
|
416 | 02eb84d0 | Michael S. Tsirkin | int msix_enabled(PCIDevice *dev)
|
417 | 02eb84d0 | Michael S. Tsirkin | { |
418 | 02eb84d0 | Michael S. Tsirkin | return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
|
419 | 2760952b | Michael S. Tsirkin | (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & |
420 | 02eb84d0 | Michael S. Tsirkin | MSIX_ENABLE_MASK); |
421 | 02eb84d0 | Michael S. Tsirkin | } |
422 | 02eb84d0 | Michael S. Tsirkin | |
423 | 02eb84d0 | Michael S. Tsirkin | /* Send an MSI-X message */
|
424 | 02eb84d0 | Michael S. Tsirkin | void msix_notify(PCIDevice *dev, unsigned vector) |
425 | 02eb84d0 | Michael S. Tsirkin | { |
426 | bc4caf49 | Jan Kiszka | MSIMessage msg; |
427 | 02eb84d0 | Michael S. Tsirkin | |
428 | 02eb84d0 | Michael S. Tsirkin | if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector])
|
429 | 02eb84d0 | Michael S. Tsirkin | return;
|
430 | 02eb84d0 | Michael S. Tsirkin | if (msix_is_masked(dev, vector)) {
|
431 | 02eb84d0 | Michael S. Tsirkin | msix_set_pending(dev, vector); |
432 | 02eb84d0 | Michael S. Tsirkin | return;
|
433 | 02eb84d0 | Michael S. Tsirkin | } |
434 | 02eb84d0 | Michael S. Tsirkin | |
435 | bc4caf49 | Jan Kiszka | msg = msix_get_message(dev, vector); |
436 | bc4caf49 | Jan Kiszka | |
437 | bc4caf49 | Jan Kiszka | stl_le_phys(msg.address, msg.data); |
438 | 02eb84d0 | Michael S. Tsirkin | } |
439 | 02eb84d0 | Michael S. Tsirkin | |
440 | 02eb84d0 | Michael S. Tsirkin | void msix_reset(PCIDevice *dev)
|
441 | 02eb84d0 | Michael S. Tsirkin | { |
442 | 44701ab7 | Jan Kiszka | if (!msix_present(dev)) {
|
443 | 02eb84d0 | Michael S. Tsirkin | return;
|
444 | 44701ab7 | Jan Kiszka | } |
445 | 3cac001e | Michael S. Tsirkin | msix_clear_all_vectors(dev); |
446 | 2760952b | Michael S. Tsirkin | dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &= |
447 | 2760952b | Michael S. Tsirkin | ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET]; |
448 | d35e428c | Alex Williamson | memset(dev->msix_table, 0, dev->msix_entries_nr * PCI_MSIX_ENTRY_SIZE);
|
449 | d35e428c | Alex Williamson | memset(dev->msix_pba, 0, QEMU_ALIGN_UP(dev->msix_entries_nr, 64) / 8); |
450 | ae1be0bb | Michael S. Tsirkin | msix_mask_all(dev, dev->msix_entries_nr); |
451 | 02eb84d0 | Michael S. Tsirkin | } |
452 | 02eb84d0 | Michael S. Tsirkin | |
453 | 02eb84d0 | Michael S. Tsirkin | /* PCI spec suggests that devices make it possible for software to configure
|
454 | 02eb84d0 | Michael S. Tsirkin | * less vectors than supported by the device, but does not specify a standard
|
455 | 02eb84d0 | Michael S. Tsirkin | * mechanism for devices to do so.
|
456 | 02eb84d0 | Michael S. Tsirkin | *
|
457 | 02eb84d0 | Michael S. Tsirkin | * We support this by asking devices to declare vectors software is going to
|
458 | 02eb84d0 | Michael S. Tsirkin | * actually use, and checking this on the notification path. Devices that
|
459 | 02eb84d0 | Michael S. Tsirkin | * don't want to follow the spec suggestion can declare all vectors as used. */
|
460 | 02eb84d0 | Michael S. Tsirkin | |
461 | 02eb84d0 | Michael S. Tsirkin | /* Mark vector as used. */
|
462 | 02eb84d0 | Michael S. Tsirkin | int msix_vector_use(PCIDevice *dev, unsigned vector) |
463 | 02eb84d0 | Michael S. Tsirkin | { |
464 | 02eb84d0 | Michael S. Tsirkin | if (vector >= dev->msix_entries_nr)
|
465 | 02eb84d0 | Michael S. Tsirkin | return -EINVAL;
|
466 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entry_used[vector]++; |
467 | 02eb84d0 | Michael S. Tsirkin | return 0; |
468 | 02eb84d0 | Michael S. Tsirkin | } |
469 | 02eb84d0 | Michael S. Tsirkin | |
470 | 02eb84d0 | Michael S. Tsirkin | /* Mark vector as unused. */
|
471 | 02eb84d0 | Michael S. Tsirkin | void msix_vector_unuse(PCIDevice *dev, unsigned vector) |
472 | 02eb84d0 | Michael S. Tsirkin | { |
473 | 98304c84 | Michael S. Tsirkin | if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
|
474 | 98304c84 | Michael S. Tsirkin | return;
|
475 | 98304c84 | Michael S. Tsirkin | } |
476 | 98304c84 | Michael S. Tsirkin | if (--dev->msix_entry_used[vector]) {
|
477 | 98304c84 | Michael S. Tsirkin | return;
|
478 | 98304c84 | Michael S. Tsirkin | } |
479 | 98304c84 | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
480 | 02eb84d0 | Michael S. Tsirkin | } |
481 | b5f28bca | Michael S. Tsirkin | |
482 | b5f28bca | Michael S. Tsirkin | void msix_unuse_all_vectors(PCIDevice *dev)
|
483 | b5f28bca | Michael S. Tsirkin | { |
484 | 44701ab7 | Jan Kiszka | if (!msix_present(dev)) {
|
485 | b5f28bca | Michael S. Tsirkin | return;
|
486 | 44701ab7 | Jan Kiszka | } |
487 | b5f28bca | Michael S. Tsirkin | msix_free_irq_entries(dev); |
488 | b5f28bca | Michael S. Tsirkin | } |
489 | 2cdfe53c | Jan Kiszka | |
490 | cb697aaa | Jan Kiszka | unsigned int msix_nr_vectors_allocated(const PCIDevice *dev) |
491 | cb697aaa | Jan Kiszka | { |
492 | cb697aaa | Jan Kiszka | return dev->msix_entries_nr;
|
493 | cb697aaa | Jan Kiszka | } |
494 | cb697aaa | Jan Kiszka | |
495 | 2cdfe53c | Jan Kiszka | static int msix_set_notifier_for_vector(PCIDevice *dev, unsigned int vector) |
496 | 2cdfe53c | Jan Kiszka | { |
497 | 2cdfe53c | Jan Kiszka | MSIMessage msg; |
498 | 2cdfe53c | Jan Kiszka | |
499 | 2cdfe53c | Jan Kiszka | if (msix_is_masked(dev, vector)) {
|
500 | 2cdfe53c | Jan Kiszka | return 0; |
501 | 2cdfe53c | Jan Kiszka | } |
502 | 2cdfe53c | Jan Kiszka | msg = msix_get_message(dev, vector); |
503 | 2cdfe53c | Jan Kiszka | return dev->msix_vector_use_notifier(dev, vector, msg);
|
504 | 2cdfe53c | Jan Kiszka | } |
505 | 2cdfe53c | Jan Kiszka | |
506 | 2cdfe53c | Jan Kiszka | static void msix_unset_notifier_for_vector(PCIDevice *dev, unsigned int vector) |
507 | 2cdfe53c | Jan Kiszka | { |
508 | 2cdfe53c | Jan Kiszka | if (msix_is_masked(dev, vector)) {
|
509 | 2cdfe53c | Jan Kiszka | return;
|
510 | 2cdfe53c | Jan Kiszka | } |
511 | 2cdfe53c | Jan Kiszka | dev->msix_vector_release_notifier(dev, vector); |
512 | 2cdfe53c | Jan Kiszka | } |
513 | 2cdfe53c | Jan Kiszka | |
514 | 2cdfe53c | Jan Kiszka | int msix_set_vector_notifiers(PCIDevice *dev,
|
515 | 2cdfe53c | Jan Kiszka | MSIVectorUseNotifier use_notifier, |
516 | 2cdfe53c | Jan Kiszka | MSIVectorReleaseNotifier release_notifier) |
517 | 2cdfe53c | Jan Kiszka | { |
518 | 2cdfe53c | Jan Kiszka | int vector, ret;
|
519 | 2cdfe53c | Jan Kiszka | |
520 | 2cdfe53c | Jan Kiszka | assert(use_notifier && release_notifier); |
521 | 2cdfe53c | Jan Kiszka | |
522 | 2cdfe53c | Jan Kiszka | dev->msix_vector_use_notifier = use_notifier; |
523 | 2cdfe53c | Jan Kiszka | dev->msix_vector_release_notifier = release_notifier; |
524 | 2cdfe53c | Jan Kiszka | |
525 | 2cdfe53c | Jan Kiszka | if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
|
526 | 2cdfe53c | Jan Kiszka | (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) { |
527 | 2cdfe53c | Jan Kiszka | for (vector = 0; vector < dev->msix_entries_nr; vector++) { |
528 | 2cdfe53c | Jan Kiszka | ret = msix_set_notifier_for_vector(dev, vector); |
529 | 2cdfe53c | Jan Kiszka | if (ret < 0) { |
530 | 2cdfe53c | Jan Kiszka | goto undo;
|
531 | 2cdfe53c | Jan Kiszka | } |
532 | 2cdfe53c | Jan Kiszka | } |
533 | 2cdfe53c | Jan Kiszka | } |
534 | 2cdfe53c | Jan Kiszka | return 0; |
535 | 2cdfe53c | Jan Kiszka | |
536 | 2cdfe53c | Jan Kiszka | undo:
|
537 | 2cdfe53c | Jan Kiszka | while (--vector >= 0) { |
538 | 2cdfe53c | Jan Kiszka | msix_unset_notifier_for_vector(dev, vector); |
539 | 2cdfe53c | Jan Kiszka | } |
540 | 2cdfe53c | Jan Kiszka | dev->msix_vector_use_notifier = NULL;
|
541 | 2cdfe53c | Jan Kiszka | dev->msix_vector_release_notifier = NULL;
|
542 | 2cdfe53c | Jan Kiszka | return ret;
|
543 | 2cdfe53c | Jan Kiszka | } |
544 | 2cdfe53c | Jan Kiszka | |
545 | 2cdfe53c | Jan Kiszka | void msix_unset_vector_notifiers(PCIDevice *dev)
|
546 | 2cdfe53c | Jan Kiszka | { |
547 | 2cdfe53c | Jan Kiszka | int vector;
|
548 | 2cdfe53c | Jan Kiszka | |
549 | 2cdfe53c | Jan Kiszka | assert(dev->msix_vector_use_notifier && |
550 | 2cdfe53c | Jan Kiszka | dev->msix_vector_release_notifier); |
551 | 2cdfe53c | Jan Kiszka | |
552 | 2cdfe53c | Jan Kiszka | if ((dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &
|
553 | 2cdfe53c | Jan Kiszka | (MSIX_ENABLE_MASK | MSIX_MASKALL_MASK)) == MSIX_ENABLE_MASK) { |
554 | 2cdfe53c | Jan Kiszka | for (vector = 0; vector < dev->msix_entries_nr; vector++) { |
555 | 2cdfe53c | Jan Kiszka | msix_unset_notifier_for_vector(dev, vector); |
556 | 2cdfe53c | Jan Kiszka | } |
557 | 2cdfe53c | Jan Kiszka | } |
558 | 2cdfe53c | Jan Kiszka | dev->msix_vector_use_notifier = NULL;
|
559 | 2cdfe53c | Jan Kiszka | dev->msix_vector_release_notifier = NULL;
|
560 | 2cdfe53c | Jan Kiszka | } |