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