]>
Commit | Line | Data |
---|---|---|
6cbf4c8c CM |
1 | /* |
2 | * Inter-VM Shared Memory PCI device. | |
3 | * | |
4 | * Author: | |
5 | * Cam Macdonell <[email protected]> | |
6 | * | |
7 | * Based On: cirrus_vga.c | |
8 | * Copyright (c) 2004 Fabrice Bellard | |
9 | * Copyright (c) 2004 Makoto Suzuki (suzu) | |
10 | * | |
11 | * and rtl8139.c | |
12 | * Copyright (c) 2006 Igor Kovalenko | |
13 | * | |
14 | * This code is licensed under the GNU GPL v2. | |
6b620ca3 PB |
15 | * |
16 | * Contributions after 2012-01-13 are licensed under the terms of the | |
17 | * GNU GPL, version 2 or (at your option) any later version. | |
6cbf4c8c | 18 | */ |
0d1c9782 | 19 | #include "qemu/osdep.h" |
519abcdf | 20 | #include "qemu/units.h" |
da34e65c | 21 | #include "qapi/error.h" |
f348b6d1 | 22 | #include "qemu/cutils.h" |
83c9f4ca | 23 | #include "hw/hw.h" |
83c9f4ca | 24 | #include "hw/pci/pci.h" |
660c97ee | 25 | #include "hw/pci/msi.h" |
83c9f4ca | 26 | #include "hw/pci/msix.h" |
9c17d615 | 27 | #include "sysemu/kvm.h" |
795c40b8 | 28 | #include "migration/blocker.h" |
d49b6836 | 29 | #include "qemu/error-report.h" |
1de7afc9 | 30 | #include "qemu/event_notifier.h" |
5503e285 | 31 | #include "qom/object_interfaces.h" |
4d43a603 | 32 | #include "chardev/char-fe.h" |
d9453c93 | 33 | #include "sysemu/hostmem.h" |
5400c02b | 34 | #include "sysemu/qtest.h" |
d9453c93 | 35 | #include "qapi/visitor.h" |
6cbf4c8c | 36 | |
5105b1d8 DM |
37 | #include "hw/misc/ivshmem.h" |
38 | ||
b8ef62a9 PB |
39 | #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET |
40 | #define PCI_DEVICE_ID_IVSHMEM 0x1110 | |
41 | ||
cd9953f7 | 42 | #define IVSHMEM_MAX_PEERS UINT16_MAX |
6cbf4c8c CM |
43 | #define IVSHMEM_IOEVENTFD 0 |
44 | #define IVSHMEM_MSI 1 | |
45 | ||
6cbf4c8c CM |
46 | #define IVSHMEM_REG_BAR_SIZE 0x100 |
47 | ||
a4fa93bf MA |
48 | #define IVSHMEM_DEBUG 0 |
49 | #define IVSHMEM_DPRINTF(fmt, ...) \ | |
50 | do { \ | |
51 | if (IVSHMEM_DEBUG) { \ | |
52 | printf("IVSHMEM: " fmt, ## __VA_ARGS__); \ | |
53 | } \ | |
54 | } while (0) | |
6cbf4c8c | 55 | |
5400c02b MA |
56 | #define TYPE_IVSHMEM_COMMON "ivshmem-common" |
57 | #define IVSHMEM_COMMON(obj) \ | |
58 | OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_COMMON) | |
59 | ||
60 | #define TYPE_IVSHMEM_PLAIN "ivshmem-plain" | |
61 | #define IVSHMEM_PLAIN(obj) \ | |
62 | OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_PLAIN) | |
63 | ||
64 | #define TYPE_IVSHMEM_DOORBELL "ivshmem-doorbell" | |
65 | #define IVSHMEM_DOORBELL(obj) \ | |
66 | OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_DOORBELL) | |
67 | ||
eb3fedf3 PC |
68 | #define TYPE_IVSHMEM "ivshmem" |
69 | #define IVSHMEM(obj) \ | |
70 | OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM) | |
71 | ||
6cbf4c8c CM |
72 | typedef struct Peer { |
73 | int nb_eventfds; | |
563027cc | 74 | EventNotifier *eventfds; |
6cbf4c8c CM |
75 | } Peer; |
76 | ||
0f57350e | 77 | typedef struct MSIVector { |
6cbf4c8c | 78 | PCIDevice *pdev; |
660c97ee | 79 | int virq; |
089fd803 | 80 | bool unmasked; |
0f57350e | 81 | } MSIVector; |
6cbf4c8c CM |
82 | |
83 | typedef struct IVShmemState { | |
b7578eaa AF |
84 | /*< private >*/ |
85 | PCIDevice parent_obj; | |
86 | /*< public >*/ | |
87 | ||
ddc85284 MA |
88 | uint32_t features; |
89 | ||
90 | /* exactly one of these two may be set */ | |
91 | HostMemoryBackend *hostmem; /* with interrupts */ | |
becdfa00 | 92 | CharBackend server_chr; /* without interrupts */ |
ddc85284 MA |
93 | |
94 | /* registers */ | |
6cbf4c8c CM |
95 | uint32_t intrmask; |
96 | uint32_t intrstatus; | |
ddc85284 | 97 | int vm_id; |
6cbf4c8c | 98 | |
ddc85284 MA |
99 | /* BARs */ |
100 | MemoryRegion ivshmem_mmio; /* BAR 0 (registers) */ | |
c2d8019c MA |
101 | MemoryRegion *ivshmem_bar2; /* BAR 2 (shared memory) */ |
102 | MemoryRegion server_bar2; /* used with server_chr */ | |
6cbf4c8c | 103 | |
ddc85284 | 104 | /* interrupt support */ |
6cbf4c8c | 105 | Peer *peers; |
cd9953f7 | 106 | int nb_peers; /* space in @peers[] */ |
6cbf4c8c | 107 | uint32_t vectors; |
0f57350e | 108 | MSIVector *msi_vectors; |
ee276391 MA |
109 | uint64_t msg_buf; /* buffer for receiving server messages */ |
110 | int msg_buffered_bytes; /* #bytes in @msg_buf */ | |
6cbf4c8c | 111 | |
ddc85284 | 112 | /* migration stuff */ |
2a845da7 | 113 | OnOffAuto master; |
38e0735e | 114 | Error *migration_blocker; |
6cbf4c8c CM |
115 | } IVShmemState; |
116 | ||
117 | /* registers for the Inter-VM shared memory device */ | |
118 | enum ivshmem_registers { | |
119 | INTRMASK = 0, | |
120 | INTRSTATUS = 4, | |
121 | IVPOSITION = 8, | |
122 | DOORBELL = 12, | |
123 | }; | |
124 | ||
125 | static inline uint32_t ivshmem_has_feature(IVShmemState *ivs, | |
126 | unsigned int feature) { | |
127 | return (ivs->features & (1 << feature)); | |
128 | } | |
129 | ||
2a845da7 MA |
130 | static inline bool ivshmem_is_master(IVShmemState *s) |
131 | { | |
132 | assert(s->master != ON_OFF_AUTO_AUTO); | |
133 | return s->master == ON_OFF_AUTO_ON; | |
134 | } | |
135 | ||
d8a5da07 | 136 | static void ivshmem_update_irq(IVShmemState *s) |
6cbf4c8c | 137 | { |
b7578eaa | 138 | PCIDevice *d = PCI_DEVICE(s); |
434ad76d | 139 | uint32_t isr = s->intrstatus & s->intrmask; |
6cbf4c8c | 140 | |
5400c02b MA |
141 | /* |
142 | * Do nothing unless the device actually uses INTx. Here's how | |
143 | * the device variants signal interrupts, what they put in PCI | |
144 | * config space: | |
145 | * Device variant Interrupt Interrupt Pin MSI-X cap. | |
146 | * ivshmem-plain none 0 no | |
147 | * ivshmem-doorbell MSI-X 1 yes(1) | |
148 | * ivshmem,msi=off INTx 1 no | |
149 | * ivshmem,msi=on MSI-X 1(2) yes(1) | |
150 | * (1) if guest enabled MSI-X | |
151 | * (2) the device lies | |
152 | * Leads to the condition for doing nothing: | |
153 | */ | |
154 | if (ivshmem_has_feature(s, IVSHMEM_MSI) | |
155 | || !d->config[PCI_INTERRUPT_PIN]) { | |
2d1d422d MA |
156 | return; |
157 | } | |
158 | ||
6cbf4c8c CM |
159 | /* don't print ISR resets */ |
160 | if (isr) { | |
161 | IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n", | |
dbc464d4 | 162 | isr ? 1 : 0, s->intrstatus, s->intrmask); |
6cbf4c8c CM |
163 | } |
164 | ||
434ad76d | 165 | pci_set_irq(d, isr != 0); |
6cbf4c8c CM |
166 | } |
167 | ||
168 | static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val) | |
169 | { | |
170 | IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val); | |
171 | ||
172 | s->intrmask = val; | |
d8a5da07 | 173 | ivshmem_update_irq(s); |
6cbf4c8c CM |
174 | } |
175 | ||
176 | static uint32_t ivshmem_IntrMask_read(IVShmemState *s) | |
177 | { | |
178 | uint32_t ret = s->intrmask; | |
179 | ||
180 | IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret); | |
6cbf4c8c CM |
181 | return ret; |
182 | } | |
183 | ||
184 | static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val) | |
185 | { | |
186 | IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val); | |
187 | ||
188 | s->intrstatus = val; | |
d8a5da07 | 189 | ivshmem_update_irq(s); |
6cbf4c8c CM |
190 | } |
191 | ||
192 | static uint32_t ivshmem_IntrStatus_read(IVShmemState *s) | |
193 | { | |
194 | uint32_t ret = s->intrstatus; | |
195 | ||
196 | /* reading ISR clears all interrupts */ | |
197 | s->intrstatus = 0; | |
d8a5da07 | 198 | ivshmem_update_irq(s); |
6cbf4c8c CM |
199 | return ret; |
200 | } | |
201 | ||
a8170e5e | 202 | static void ivshmem_io_write(void *opaque, hwaddr addr, |
cb06608e | 203 | uint64_t val, unsigned size) |
6cbf4c8c CM |
204 | { |
205 | IVShmemState *s = opaque; | |
206 | ||
6cbf4c8c CM |
207 | uint16_t dest = val >> 16; |
208 | uint16_t vector = val & 0xff; | |
209 | ||
210 | addr &= 0xfc; | |
211 | ||
212 | IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr); | |
213 | switch (addr) | |
214 | { | |
215 | case INTRMASK: | |
216 | ivshmem_IntrMask_write(s, val); | |
217 | break; | |
218 | ||
219 | case INTRSTATUS: | |
220 | ivshmem_IntrStatus_write(s, val); | |
221 | break; | |
222 | ||
223 | case DOORBELL: | |
224 | /* check that dest VM ID is reasonable */ | |
95c8425c | 225 | if (dest >= s->nb_peers) { |
6cbf4c8c CM |
226 | IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest); |
227 | break; | |
228 | } | |
229 | ||
230 | /* check doorbell range */ | |
1b27d7a1 | 231 | if (vector < s->peers[dest].nb_eventfds) { |
563027cc PB |
232 | IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector); |
233 | event_notifier_set(&s->peers[dest].eventfds[vector]); | |
f59bb378 MAL |
234 | } else { |
235 | IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n", | |
236 | vector, dest); | |
6cbf4c8c CM |
237 | } |
238 | break; | |
239 | default: | |
f59bb378 | 240 | IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx "\n", addr); |
6cbf4c8c CM |
241 | } |
242 | } | |
243 | ||
a8170e5e | 244 | static uint64_t ivshmem_io_read(void *opaque, hwaddr addr, |
cb06608e | 245 | unsigned size) |
6cbf4c8c CM |
246 | { |
247 | ||
248 | IVShmemState *s = opaque; | |
249 | uint32_t ret; | |
250 | ||
251 | switch (addr) | |
252 | { | |
253 | case INTRMASK: | |
254 | ret = ivshmem_IntrMask_read(s); | |
255 | break; | |
256 | ||
257 | case INTRSTATUS: | |
258 | ret = ivshmem_IntrStatus_read(s); | |
259 | break; | |
260 | ||
261 | case IVPOSITION: | |
1309cf44 | 262 | ret = s->vm_id; |
6cbf4c8c CM |
263 | break; |
264 | ||
265 | default: | |
266 | IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr); | |
267 | ret = 0; | |
268 | } | |
269 | ||
270 | return ret; | |
271 | } | |
272 | ||
cb06608e AK |
273 | static const MemoryRegionOps ivshmem_mmio_ops = { |
274 | .read = ivshmem_io_read, | |
275 | .write = ivshmem_io_write, | |
276 | .endianness = DEVICE_NATIVE_ENDIAN, | |
277 | .impl = { | |
278 | .min_access_size = 4, | |
279 | .max_access_size = 4, | |
280 | }, | |
6cbf4c8c CM |
281 | }; |
282 | ||
9940c323 MAL |
283 | static void ivshmem_vector_notify(void *opaque) |
284 | { | |
0f57350e | 285 | MSIVector *entry = opaque; |
6cbf4c8c | 286 | PCIDevice *pdev = entry->pdev; |
5400c02b | 287 | IVShmemState *s = IVSHMEM_COMMON(pdev); |
0f57350e | 288 | int vector = entry - s->msi_vectors; |
9940c323 MAL |
289 | EventNotifier *n = &s->peers[s->vm_id].eventfds[vector]; |
290 | ||
291 | if (!event_notifier_test_and_clear(n)) { | |
292 | return; | |
293 | } | |
6cbf4c8c | 294 | |
d160f3f7 | 295 | IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, vector); |
9940c323 | 296 | if (ivshmem_has_feature(s, IVSHMEM_MSI)) { |
082751e8 MA |
297 | if (msix_enabled(pdev)) { |
298 | msix_notify(pdev, vector); | |
299 | } | |
9940c323 MAL |
300 | } else { |
301 | ivshmem_IntrStatus_write(s, 1); | |
302 | } | |
6cbf4c8c CM |
303 | } |
304 | ||
660c97ee MAL |
305 | static int ivshmem_vector_unmask(PCIDevice *dev, unsigned vector, |
306 | MSIMessage msg) | |
307 | { | |
5400c02b | 308 | IVShmemState *s = IVSHMEM_COMMON(dev); |
660c97ee MAL |
309 | EventNotifier *n = &s->peers[s->vm_id].eventfds[vector]; |
310 | MSIVector *v = &s->msi_vectors[vector]; | |
311 | int ret; | |
312 | ||
313 | IVSHMEM_DPRINTF("vector unmask %p %d\n", dev, vector); | |
e6a354be LP |
314 | if (!v->pdev) { |
315 | error_report("ivshmem: vector %d route does not exist", vector); | |
316 | return -EINVAL; | |
317 | } | |
089fd803 | 318 | assert(!v->unmasked); |
660c97ee MAL |
319 | |
320 | ret = kvm_irqchip_update_msi_route(kvm_state, v->virq, msg, dev); | |
321 | if (ret < 0) { | |
322 | return ret; | |
323 | } | |
3f1fea0f | 324 | kvm_irqchip_commit_routes(kvm_state); |
660c97ee | 325 | |
089fd803 LP |
326 | ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, v->virq); |
327 | if (ret < 0) { | |
328 | return ret; | |
329 | } | |
330 | v->unmasked = true; | |
331 | ||
332 | return 0; | |
660c97ee MAL |
333 | } |
334 | ||
335 | static void ivshmem_vector_mask(PCIDevice *dev, unsigned vector) | |
336 | { | |
5400c02b | 337 | IVShmemState *s = IVSHMEM_COMMON(dev); |
660c97ee | 338 | EventNotifier *n = &s->peers[s->vm_id].eventfds[vector]; |
e6a354be | 339 | MSIVector *v = &s->msi_vectors[vector]; |
660c97ee MAL |
340 | int ret; |
341 | ||
342 | IVSHMEM_DPRINTF("vector mask %p %d\n", dev, vector); | |
e6a354be LP |
343 | if (!v->pdev) { |
344 | error_report("ivshmem: vector %d route does not exist", vector); | |
345 | return; | |
346 | } | |
089fd803 | 347 | assert(v->unmasked); |
660c97ee | 348 | |
e6a354be | 349 | ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, v->virq); |
089fd803 | 350 | if (ret < 0) { |
660c97ee | 351 | error_report("remove_irqfd_notifier_gsi failed"); |
089fd803 | 352 | return; |
660c97ee | 353 | } |
089fd803 | 354 | v->unmasked = false; |
660c97ee MAL |
355 | } |
356 | ||
357 | static void ivshmem_vector_poll(PCIDevice *dev, | |
358 | unsigned int vector_start, | |
359 | unsigned int vector_end) | |
360 | { | |
5400c02b | 361 | IVShmemState *s = IVSHMEM_COMMON(dev); |
660c97ee MAL |
362 | unsigned int vector; |
363 | ||
364 | IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev, vector_start, vector_end); | |
365 | ||
366 | vector_end = MIN(vector_end, s->vectors); | |
367 | ||
368 | for (vector = vector_start; vector < vector_end; vector++) { | |
369 | EventNotifier *notifier = &s->peers[s->vm_id].eventfds[vector]; | |
370 | ||
371 | if (!msix_is_masked(dev, vector)) { | |
372 | continue; | |
373 | } | |
374 | ||
375 | if (event_notifier_test_and_clear(notifier)) { | |
376 | msix_set_pending(dev, vector); | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
9940c323 MAL |
381 | static void watch_vector_notifier(IVShmemState *s, EventNotifier *n, |
382 | int vector) | |
6cbf4c8c | 383 | { |
563027cc | 384 | int eventfd = event_notifier_get_fd(n); |
6cbf4c8c | 385 | |
3c27969b | 386 | assert(!s->msi_vectors[vector].pdev); |
9940c323 | 387 | s->msi_vectors[vector].pdev = PCI_DEVICE(s); |
6cbf4c8c | 388 | |
9940c323 MAL |
389 | qemu_set_fd_handler(eventfd, ivshmem_vector_notify, |
390 | NULL, &s->msi_vectors[vector]); | |
6cbf4c8c CM |
391 | } |
392 | ||
563027cc PB |
393 | static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i) |
394 | { | |
395 | memory_region_add_eventfd(&s->ivshmem_mmio, | |
396 | DOORBELL, | |
397 | 4, | |
398 | true, | |
399 | (posn << 16) | i, | |
753d5e14 | 400 | &s->peers[posn].eventfds[i]); |
563027cc PB |
401 | } |
402 | ||
403 | static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i) | |
404 | { | |
405 | memory_region_del_eventfd(&s->ivshmem_mmio, | |
406 | DOORBELL, | |
407 | 4, | |
408 | true, | |
409 | (posn << 16) | i, | |
753d5e14 | 410 | &s->peers[posn].eventfds[i]); |
563027cc PB |
411 | } |
412 | ||
f456179f | 413 | static void close_peer_eventfds(IVShmemState *s, int posn) |
6cbf4c8c | 414 | { |
f456179f | 415 | int i, n; |
6cbf4c8c | 416 | |
9db51b4d | 417 | assert(posn >= 0 && posn < s->nb_peers); |
f456179f | 418 | n = s->peers[posn].nb_eventfds; |
6cbf4c8c | 419 | |
9db51b4d MA |
420 | if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) { |
421 | memory_region_transaction_begin(); | |
422 | for (i = 0; i < n; i++) { | |
423 | ivshmem_del_eventfd(s, posn, i); | |
424 | } | |
425 | memory_region_transaction_commit(); | |
b6a1f3a5 | 426 | } |
9db51b4d | 427 | |
f456179f | 428 | for (i = 0; i < n; i++) { |
563027cc | 429 | event_notifier_cleanup(&s->peers[posn].eventfds[i]); |
6cbf4c8c CM |
430 | } |
431 | ||
7267c094 | 432 | g_free(s->peers[posn].eventfds); |
6cbf4c8c CM |
433 | s->peers[posn].nb_eventfds = 0; |
434 | } | |
435 | ||
cd9953f7 | 436 | static void resize_peers(IVShmemState *s, int nb_peers) |
34bc07c5 | 437 | { |
cd9953f7 MA |
438 | int old_nb_peers = s->nb_peers; |
439 | int i; | |
6cbf4c8c | 440 | |
cd9953f7 MA |
441 | assert(nb_peers > old_nb_peers); |
442 | IVSHMEM_DPRINTF("bumping storage to %d peers\n", nb_peers); | |
6cbf4c8c | 443 | |
cd9953f7 MA |
444 | s->peers = g_realloc(s->peers, nb_peers * sizeof(Peer)); |
445 | s->nb_peers = nb_peers; | |
1300b273 | 446 | |
cd9953f7 MA |
447 | for (i = old_nb_peers; i < nb_peers; i++) { |
448 | s->peers[i].eventfds = g_new0(EventNotifier, s->vectors); | |
449 | s->peers[i].nb_eventfds = 0; | |
6cbf4c8c CM |
450 | } |
451 | } | |
452 | ||
1309cf44 MA |
453 | static void ivshmem_add_kvm_msi_virq(IVShmemState *s, int vector, |
454 | Error **errp) | |
660c97ee MAL |
455 | { |
456 | PCIDevice *pdev = PCI_DEVICE(s); | |
660c97ee MAL |
457 | int ret; |
458 | ||
459 | IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector); | |
3c27969b | 460 | assert(!s->msi_vectors[vector].pdev); |
660c97ee | 461 | |
d1f6af6a | 462 | ret = kvm_irqchip_add_msi_route(kvm_state, vector, pdev); |
660c97ee | 463 | if (ret < 0) { |
1309cf44 MA |
464 | error_setg(errp, "kvm_irqchip_add_msi_route failed"); |
465 | return; | |
660c97ee MAL |
466 | } |
467 | ||
468 | s->msi_vectors[vector].virq = ret; | |
469 | s->msi_vectors[vector].pdev = pdev; | |
660c97ee MAL |
470 | } |
471 | ||
1309cf44 | 472 | static void setup_interrupt(IVShmemState *s, int vector, Error **errp) |
660c97ee MAL |
473 | { |
474 | EventNotifier *n = &s->peers[s->vm_id].eventfds[vector]; | |
475 | bool with_irqfd = kvm_msi_via_irqfd_enabled() && | |
476 | ivshmem_has_feature(s, IVSHMEM_MSI); | |
477 | PCIDevice *pdev = PCI_DEVICE(s); | |
1309cf44 | 478 | Error *err = NULL; |
660c97ee MAL |
479 | |
480 | IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector); | |
481 | ||
482 | if (!with_irqfd) { | |
97553976 | 483 | IVSHMEM_DPRINTF("with eventfd\n"); |
9940c323 | 484 | watch_vector_notifier(s, n, vector); |
660c97ee | 485 | } else if (msix_enabled(pdev)) { |
97553976 | 486 | IVSHMEM_DPRINTF("with irqfd\n"); |
1309cf44 MA |
487 | ivshmem_add_kvm_msi_virq(s, vector, &err); |
488 | if (err) { | |
489 | error_propagate(errp, err); | |
660c97ee MAL |
490 | return; |
491 | } | |
492 | ||
493 | if (!msix_is_masked(pdev, vector)) { | |
494 | kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, | |
495 | s->msi_vectors[vector].virq); | |
1309cf44 | 496 | /* TODO handle error */ |
660c97ee MAL |
497 | } |
498 | } else { | |
499 | /* it will be delayed until msix is enabled, in write_config */ | |
97553976 | 500 | IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled\n"); |
660c97ee MAL |
501 | } |
502 | } | |
503 | ||
1309cf44 | 504 | static void process_msg_shmem(IVShmemState *s, int fd, Error **errp) |
6cbf4c8c | 505 | { |
8381d89b | 506 | Error *local_err = NULL; |
8baeb22b | 507 | struct stat buf; |
5400c02b | 508 | size_t size; |
6cbf4c8c | 509 | |
c2d8019c | 510 | if (s->ivshmem_bar2) { |
1309cf44 | 511 | error_setg(errp, "server sent unexpected shared memory message"); |
ca0b7566 | 512 | close(fd); |
0f14fd71 | 513 | return; |
a2e9011b SH |
514 | } |
515 | ||
8baeb22b MA |
516 | if (fstat(fd, &buf) < 0) { |
517 | error_setg_errno(errp, errno, | |
518 | "can't determine size of shared memory sent by server"); | |
519 | close(fd); | |
520 | return; | |
521 | } | |
522 | ||
5400c02b MA |
523 | size = buf.st_size; |
524 | ||
ca0b7566 | 525 | /* mmap the region and map into the BAR2 */ |
8381d89b MAL |
526 | memory_region_init_ram_from_fd(&s->server_bar2, OBJECT(s), |
527 | "ivshmem.bar2", size, true, fd, &local_err); | |
528 | if (local_err) { | |
529 | error_propagate(errp, local_err); | |
ca0b7566 | 530 | return; |
6cbf4c8c | 531 | } |
8381d89b | 532 | |
c2d8019c | 533 | s->ivshmem_bar2 = &s->server_bar2; |
ca0b7566 MA |
534 | } |
535 | ||
1309cf44 MA |
536 | static void process_msg_disconnect(IVShmemState *s, uint16_t posn, |
537 | Error **errp) | |
ca0b7566 MA |
538 | { |
539 | IVSHMEM_DPRINTF("posn %d has gone away\n", posn); | |
9db51b4d | 540 | if (posn >= s->nb_peers || posn == s->vm_id) { |
1309cf44 | 541 | error_setg(errp, "invalid peer %d", posn); |
9db51b4d MA |
542 | return; |
543 | } | |
ca0b7566 MA |
544 | close_peer_eventfds(s, posn); |
545 | } | |
6cbf4c8c | 546 | |
1309cf44 MA |
547 | static void process_msg_connect(IVShmemState *s, uint16_t posn, int fd, |
548 | Error **errp) | |
ca0b7566 MA |
549 | { |
550 | Peer *peer = &s->peers[posn]; | |
551 | int vector; | |
9a2f0e64 | 552 | |
ca0b7566 MA |
553 | /* |
554 | * The N-th connect message for this peer comes with the file | |
555 | * descriptor for vector N-1. Count messages to find the vector. | |
556 | */ | |
557 | if (peer->nb_eventfds >= s->vectors) { | |
1309cf44 MA |
558 | error_setg(errp, "Too many eventfd received, device has %d vectors", |
559 | s->vectors); | |
ca0b7566 | 560 | close(fd); |
6f8a16d5 | 561 | return; |
6cbf4c8c | 562 | } |
ca0b7566 | 563 | vector = peer->nb_eventfds++; |
6cbf4c8c | 564 | |
ca0b7566 MA |
565 | IVSHMEM_DPRINTF("eventfds[%d][%d] = %d\n", posn, vector, fd); |
566 | event_notifier_init_fd(&peer->eventfds[vector], fd); | |
567 | fcntl_setfl(fd, O_NONBLOCK); /* msix/irqfd poll non block */ | |
945001a1 | 568 | |
ca0b7566 | 569 | if (posn == s->vm_id) { |
1309cf44 MA |
570 | setup_interrupt(s, vector, errp); |
571 | /* TODO do we need to handle the error? */ | |
ca0b7566 | 572 | } |
6cbf4c8c | 573 | |
ca0b7566 MA |
574 | if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) { |
575 | ivshmem_add_eventfd(s, posn, vector); | |
576 | } | |
577 | } | |
6cbf4c8c | 578 | |
1309cf44 | 579 | static void process_msg(IVShmemState *s, int64_t msg, int fd, Error **errp) |
ca0b7566 MA |
580 | { |
581 | IVSHMEM_DPRINTF("posn is %" PRId64 ", fd is %d\n", msg, fd); | |
6cbf4c8c | 582 | |
ca0b7566 | 583 | if (msg < -1 || msg > IVSHMEM_MAX_PEERS) { |
1309cf44 | 584 | error_setg(errp, "server sent invalid message %" PRId64, msg); |
ca0b7566 | 585 | close(fd); |
6cbf4c8c CM |
586 | return; |
587 | } | |
588 | ||
ca0b7566 | 589 | if (msg == -1) { |
1309cf44 | 590 | process_msg_shmem(s, fd, errp); |
1ee57de4 MAL |
591 | return; |
592 | } | |
593 | ||
ca0b7566 MA |
594 | if (msg >= s->nb_peers) { |
595 | resize_peers(s, msg + 1); | |
596 | } | |
6cbf4c8c | 597 | |
ca0b7566 | 598 | if (fd >= 0) { |
1309cf44 | 599 | process_msg_connect(s, msg, fd, errp); |
ca0b7566 | 600 | } else { |
1309cf44 | 601 | process_msg_disconnect(s, msg, errp); |
6cbf4c8c | 602 | } |
ca0b7566 | 603 | } |
6cbf4c8c | 604 | |
ee276391 MA |
605 | static int ivshmem_can_receive(void *opaque) |
606 | { | |
607 | IVShmemState *s = opaque; | |
608 | ||
609 | assert(s->msg_buffered_bytes < sizeof(s->msg_buf)); | |
610 | return sizeof(s->msg_buf) - s->msg_buffered_bytes; | |
611 | } | |
612 | ||
ca0b7566 MA |
613 | static void ivshmem_read(void *opaque, const uint8_t *buf, int size) |
614 | { | |
615 | IVShmemState *s = opaque; | |
1309cf44 | 616 | Error *err = NULL; |
ca0b7566 MA |
617 | int fd; |
618 | int64_t msg; | |
619 | ||
ee276391 MA |
620 | assert(size >= 0 && s->msg_buffered_bytes + size <= sizeof(s->msg_buf)); |
621 | memcpy((unsigned char *)&s->msg_buf + s->msg_buffered_bytes, buf, size); | |
622 | s->msg_buffered_bytes += size; | |
623 | if (s->msg_buffered_bytes < sizeof(s->msg_buf)) { | |
ca0b7566 | 624 | return; |
6cbf4c8c | 625 | } |
ee276391 MA |
626 | msg = le64_to_cpu(s->msg_buf); |
627 | s->msg_buffered_bytes = 0; | |
ca0b7566 | 628 | |
5345fdb4 | 629 | fd = qemu_chr_fe_get_msgfd(&s->server_chr); |
ca0b7566 | 630 | |
1309cf44 MA |
631 | process_msg(s, msg, fd, &err); |
632 | if (err) { | |
633 | error_report_err(err); | |
634 | } | |
6cbf4c8c CM |
635 | } |
636 | ||
1309cf44 | 637 | static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd, Error **errp) |
5105b1d8 | 638 | { |
3a55fc0f MA |
639 | int64_t msg; |
640 | int n, ret; | |
641 | ||
642 | n = 0; | |
643 | do { | |
5345fdb4 MAL |
644 | ret = qemu_chr_fe_read_all(&s->server_chr, (uint8_t *)&msg + n, |
645 | sizeof(msg) - n); | |
b7b1e9dd PMD |
646 | if (ret < 0) { |
647 | if (ret == -EINTR) { | |
648 | continue; | |
649 | } | |
1309cf44 | 650 | error_setg_errno(errp, -ret, "read from server failed"); |
3a55fc0f MA |
651 | return INT64_MIN; |
652 | } | |
653 | n += ret; | |
654 | } while (n < sizeof(msg)); | |
5105b1d8 | 655 | |
5345fdb4 | 656 | *pfd = qemu_chr_fe_get_msgfd(&s->server_chr); |
51af0ec9 | 657 | return le64_to_cpu(msg); |
3a55fc0f | 658 | } |
5105b1d8 | 659 | |
1309cf44 | 660 | static void ivshmem_recv_setup(IVShmemState *s, Error **errp) |
3a55fc0f | 661 | { |
1309cf44 | 662 | Error *err = NULL; |
3a55fc0f MA |
663 | int64_t msg; |
664 | int fd; | |
665 | ||
1309cf44 MA |
666 | msg = ivshmem_recv_msg(s, &fd, &err); |
667 | if (err) { | |
668 | error_propagate(errp, err); | |
669 | return; | |
670 | } | |
671 | if (msg != IVSHMEM_PROTOCOL_VERSION) { | |
672 | error_setg(errp, "server sent version %" PRId64 ", expecting %d", | |
673 | msg, IVSHMEM_PROTOCOL_VERSION); | |
674 | return; | |
675 | } | |
676 | if (fd != -1) { | |
677 | error_setg(errp, "server sent invalid version message"); | |
5105b1d8 DM |
678 | return; |
679 | } | |
680 | ||
a3feb086 MA |
681 | /* |
682 | * ivshmem-server sends the remaining initial messages in a fixed | |
683 | * order, but the device has always accepted them in any order. | |
684 | * Stay as compatible as practical, just in case people use | |
685 | * servers that behave differently. | |
686 | */ | |
687 | ||
688 | /* | |
689 | * ivshmem_device_spec.txt has always required the ID message | |
690 | * right here, and ivshmem-server has always complied. However, | |
691 | * older versions of the device accepted it out of order, but | |
692 | * broke when an interrupt setup message arrived before it. | |
693 | */ | |
694 | msg = ivshmem_recv_msg(s, &fd, &err); | |
695 | if (err) { | |
696 | error_propagate(errp, err); | |
697 | return; | |
698 | } | |
699 | if (fd != -1 || msg < 0 || msg > IVSHMEM_MAX_PEERS) { | |
700 | error_setg(errp, "server sent invalid ID message"); | |
701 | return; | |
702 | } | |
703 | s->vm_id = msg; | |
704 | ||
3a55fc0f MA |
705 | /* |
706 | * Receive more messages until we got shared memory. | |
707 | */ | |
708 | do { | |
1309cf44 MA |
709 | msg = ivshmem_recv_msg(s, &fd, &err); |
710 | if (err) { | |
711 | error_propagate(errp, err); | |
712 | return; | |
713 | } | |
714 | process_msg(s, msg, fd, &err); | |
715 | if (err) { | |
716 | error_propagate(errp, err); | |
717 | return; | |
718 | } | |
3a55fc0f | 719 | } while (msg != -1); |
1309cf44 MA |
720 | |
721 | /* | |
722 | * This function must either map the shared memory or fail. The | |
723 | * loop above ensures that: it terminates normally only after it | |
724 | * successfully processed the server's shared memory message. | |
725 | * Assert that actually mapped the shared memory: | |
726 | */ | |
c2d8019c | 727 | assert(s->ivshmem_bar2); |
5105b1d8 DM |
728 | } |
729 | ||
4490c711 MT |
730 | /* Select the MSI-X vectors used by device. |
731 | * ivshmem maps events to vectors statically, so | |
732 | * we just enable all vectors on init and after reset. */ | |
082751e8 | 733 | static void ivshmem_msix_vector_use(IVShmemState *s) |
4490c711 | 734 | { |
b7578eaa | 735 | PCIDevice *d = PCI_DEVICE(s); |
4490c711 MT |
736 | int i; |
737 | ||
4490c711 | 738 | for (i = 0; i < s->vectors; i++) { |
b7578eaa | 739 | msix_vector_use(d, i); |
4490c711 MT |
740 | } |
741 | } | |
742 | ||
a4022791 LP |
743 | static void ivshmem_disable_irqfd(IVShmemState *s); |
744 | ||
6cbf4c8c CM |
745 | static void ivshmem_reset(DeviceState *d) |
746 | { | |
5400c02b | 747 | IVShmemState *s = IVSHMEM_COMMON(d); |
6cbf4c8c | 748 | |
a4022791 LP |
749 | ivshmem_disable_irqfd(s); |
750 | ||
6cbf4c8c | 751 | s->intrstatus = 0; |
972ad215 | 752 | s->intrmask = 0; |
082751e8 MA |
753 | if (ivshmem_has_feature(s, IVSHMEM_MSI)) { |
754 | ivshmem_msix_vector_use(s); | |
755 | } | |
6cbf4c8c CM |
756 | } |
757 | ||
ee640c62 | 758 | static int ivshmem_setup_interrupts(IVShmemState *s, Error **errp) |
4490c711 | 759 | { |
fd47bfe5 MAL |
760 | /* allocate QEMU callback data for receiving interrupts */ |
761 | s->msi_vectors = g_malloc0(s->vectors * sizeof(MSIVector)); | |
6cbf4c8c | 762 | |
fd47bfe5 | 763 | if (ivshmem_has_feature(s, IVSHMEM_MSI)) { |
ee640c62 | 764 | if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1, errp)) { |
fd47bfe5 MAL |
765 | return -1; |
766 | } | |
1116b539 | 767 | |
fd47bfe5 | 768 | IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors); |
082751e8 | 769 | ivshmem_msix_vector_use(s); |
fd47bfe5 | 770 | } |
4490c711 | 771 | |
d58d7e84 | 772 | return 0; |
6cbf4c8c CM |
773 | } |
774 | ||
0b88dd94 LP |
775 | static void ivshmem_remove_kvm_msi_virq(IVShmemState *s, int vector) |
776 | { | |
777 | IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector); | |
778 | ||
779 | if (s->msi_vectors[vector].pdev == NULL) { | |
780 | return; | |
781 | } | |
782 | ||
783 | /* it was cleaned when masked in the frontend. */ | |
784 | kvm_irqchip_release_virq(kvm_state, s->msi_vectors[vector].virq); | |
785 | ||
786 | s->msi_vectors[vector].pdev = NULL; | |
787 | } | |
788 | ||
660c97ee MAL |
789 | static void ivshmem_enable_irqfd(IVShmemState *s) |
790 | { | |
791 | PCIDevice *pdev = PCI_DEVICE(s); | |
792 | int i; | |
793 | ||
794 | for (i = 0; i < s->peers[s->vm_id].nb_eventfds; i++) { | |
1309cf44 MA |
795 | Error *err = NULL; |
796 | ||
797 | ivshmem_add_kvm_msi_virq(s, i, &err); | |
798 | if (err) { | |
799 | error_report_err(err); | |
0b88dd94 | 800 | goto undo; |
1309cf44 | 801 | } |
660c97ee MAL |
802 | } |
803 | ||
804 | if (msix_set_vector_notifiers(pdev, | |
805 | ivshmem_vector_unmask, | |
806 | ivshmem_vector_mask, | |
807 | ivshmem_vector_poll)) { | |
808 | error_report("ivshmem: msix_set_vector_notifiers failed"); | |
0b88dd94 | 809 | goto undo; |
660c97ee | 810 | } |
0b88dd94 | 811 | return; |
660c97ee | 812 | |
0b88dd94 LP |
813 | undo: |
814 | while (--i >= 0) { | |
815 | ivshmem_remove_kvm_msi_virq(s, i); | |
660c97ee | 816 | } |
660c97ee MAL |
817 | } |
818 | ||
819 | static void ivshmem_disable_irqfd(IVShmemState *s) | |
820 | { | |
821 | PCIDevice *pdev = PCI_DEVICE(s); | |
822 | int i; | |
823 | ||
0b88dd94 LP |
824 | if (!pdev->msix_vector_use_notifier) { |
825 | return; | |
826 | } | |
827 | ||
089fd803 LP |
828 | msix_unset_vector_notifiers(pdev); |
829 | ||
660c97ee | 830 | for (i = 0; i < s->peers[s->vm_id].nb_eventfds; i++) { |
089fd803 LP |
831 | /* |
832 | * MSI-X is already disabled here so msix_unset_vector_notifiers() | |
833 | * didn't call our release notifier. Do it now to keep our masks and | |
834 | * unmasks balanced. | |
835 | */ | |
836 | if (s->msi_vectors[i].unmasked) { | |
837 | ivshmem_vector_mask(pdev, i); | |
838 | } | |
660c97ee MAL |
839 | ivshmem_remove_kvm_msi_virq(s, i); |
840 | } | |
841 | ||
660c97ee MAL |
842 | } |
843 | ||
844 | static void ivshmem_write_config(PCIDevice *pdev, uint32_t address, | |
d58d7e84 | 845 | uint32_t val, int len) |
4490c711 | 846 | { |
5400c02b | 847 | IVShmemState *s = IVSHMEM_COMMON(pdev); |
660c97ee MAL |
848 | int is_enabled, was_enabled = msix_enabled(pdev); |
849 | ||
850 | pci_default_write_config(pdev, address, val, len); | |
851 | is_enabled = msix_enabled(pdev); | |
852 | ||
1309cf44 | 853 | if (kvm_msi_via_irqfd_enabled()) { |
660c97ee MAL |
854 | if (!was_enabled && is_enabled) { |
855 | ivshmem_enable_irqfd(s); | |
856 | } else if (was_enabled && !is_enabled) { | |
857 | ivshmem_disable_irqfd(s); | |
858 | } | |
859 | } | |
4490c711 MT |
860 | } |
861 | ||
5400c02b | 862 | static void ivshmem_common_realize(PCIDevice *dev, Error **errp) |
6cbf4c8c | 863 | { |
5400c02b | 864 | IVShmemState *s = IVSHMEM_COMMON(dev); |
d855e275 | 865 | Error *err = NULL; |
6cbf4c8c | 866 | uint8_t *pci_conf; |
fe44dc91 | 867 | Error *local_err = NULL; |
6cbf4c8c | 868 | |
6cbf4c8c CM |
869 | /* IRQFD requires MSI */ |
870 | if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) && | |
871 | !ivshmem_has_feature(s, IVSHMEM_MSI)) { | |
d58d7e84 MAL |
872 | error_setg(errp, "ioeventfd/irqfd requires MSI"); |
873 | return; | |
6cbf4c8c CM |
874 | } |
875 | ||
b7578eaa | 876 | pci_conf = dev->config; |
6cbf4c8c | 877 | pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY; |
6cbf4c8c | 878 | |
3c161542 | 879 | memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s, |
cb06608e AK |
880 | "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE); |
881 | ||
6cbf4c8c | 882 | /* region for registers*/ |
b7578eaa | 883 | pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, |
e824b2cc | 884 | &s->ivshmem_mmio); |
cb06608e | 885 | |
d9453c93 | 886 | if (s->hostmem != NULL) { |
d9453c93 MAL |
887 | IVSHMEM_DPRINTF("using hostmem\n"); |
888 | ||
7943e97b | 889 | s->ivshmem_bar2 = host_memory_backend_get_memory(s->hostmem); |
b266f1d1 | 890 | host_memory_backend_set_mapped(s->hostmem, true); |
5503e285 | 891 | } else { |
0ec7b3e7 | 892 | Chardev *chr = qemu_chr_fe_get_driver(&s->server_chr); |
5345fdb4 | 893 | assert(chr); |
6dc64780 | 894 | |
6cbf4c8c | 895 | IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n", |
5345fdb4 | 896 | chr->filename); |
6cbf4c8c | 897 | |
f456179f | 898 | /* we allocate enough space for 16 peers and grow as needed */ |
1300b273 | 899 | resize_peers(s, 16); |
6cbf4c8c | 900 | |
3a55fc0f MA |
901 | /* |
902 | * Receive setup messages from server synchronously. | |
903 | * Older versions did it asynchronously, but that creates a | |
904 | * number of entertaining race conditions. | |
3a55fc0f | 905 | */ |
1309cf44 MA |
906 | ivshmem_recv_setup(s, &err); |
907 | if (err) { | |
908 | error_propagate(errp, err); | |
909 | return; | |
3a55fc0f MA |
910 | } |
911 | ||
62a830b6 MA |
912 | if (s->master == ON_OFF_AUTO_ON && s->vm_id != 0) { |
913 | error_setg(errp, | |
914 | "master must connect to the server before any peers"); | |
915 | return; | |
916 | } | |
917 | ||
5345fdb4 | 918 | qemu_chr_fe_set_handlers(&s->server_chr, ivshmem_can_receive, |
81517ba3 | 919 | ivshmem_read, NULL, NULL, s, NULL, true); |
1309cf44 | 920 | |
ee640c62 C |
921 | if (ivshmem_setup_interrupts(s, errp) < 0) { |
922 | error_prepend(errp, "Failed to initialize interrupts: "); | |
3a55fc0f MA |
923 | return; |
924 | } | |
d855e275 MA |
925 | } |
926 | ||
2a845da7 MA |
927 | if (s->master == ON_OFF_AUTO_AUTO) { |
928 | s->master = s->vm_id == 0 ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; | |
929 | } | |
930 | ||
931 | if (!ivshmem_is_master(s)) { | |
d855e275 MA |
932 | error_setg(&s->migration_blocker, |
933 | "Migration is disabled when using feature 'peer mode' in device 'ivshmem'"); | |
fe44dc91 AA |
934 | migrate_add_blocker(s->migration_blocker, &local_err); |
935 | if (local_err) { | |
936 | error_propagate(errp, local_err); | |
937 | error_free(s->migration_blocker); | |
938 | return; | |
939 | } | |
6cbf4c8c | 940 | } |
fe44dc91 AA |
941 | |
942 | vmstate_register_ram(s->ivshmem_bar2, DEVICE(s)); | |
5a0e75f0 TH |
943 | pci_register_bar(PCI_DEVICE(s), 2, |
944 | PCI_BASE_ADDRESS_SPACE_MEMORY | | |
945 | PCI_BASE_ADDRESS_MEM_PREFETCH | | |
946 | PCI_BASE_ADDRESS_MEM_TYPE_64, | |
947 | s->ivshmem_bar2); | |
6cbf4c8c CM |
948 | } |
949 | ||
5400c02b MA |
950 | static void ivshmem_exit(PCIDevice *dev) |
951 | { | |
952 | IVShmemState *s = IVSHMEM_COMMON(dev); | |
f64a078d MAL |
953 | int i; |
954 | ||
38e0735e AL |
955 | if (s->migration_blocker) { |
956 | migrate_del_blocker(s->migration_blocker); | |
957 | error_free(s->migration_blocker); | |
958 | } | |
959 | ||
c2d8019c | 960 | if (memory_region_is_mapped(s->ivshmem_bar2)) { |
d9453c93 | 961 | if (!s->hostmem) { |
c2d8019c | 962 | void *addr = memory_region_get_ram_ptr(s->ivshmem_bar2); |
56a571d9 | 963 | int fd; |
d9453c93 | 964 | |
5400c02b | 965 | if (munmap(addr, memory_region_size(s->ivshmem_bar2) == -1)) { |
d9453c93 MAL |
966 | error_report("Failed to munmap shared memory %s", |
967 | strerror(errno)); | |
968 | } | |
56a571d9 | 969 | |
4ff87573 | 970 | fd = memory_region_get_fd(s->ivshmem_bar2); |
c2d8019c | 971 | close(fd); |
d9453c93 | 972 | } |
f64a078d | 973 | |
c2d8019c | 974 | vmstate_unregister_ram(s->ivshmem_bar2, DEVICE(dev)); |
f64a078d MAL |
975 | } |
976 | ||
b266f1d1 MA |
977 | if (s->hostmem) { |
978 | host_memory_backend_set_mapped(s->hostmem, false); | |
979 | } | |
980 | ||
f64a078d MAL |
981 | if (s->peers) { |
982 | for (i = 0; i < s->nb_peers; i++) { | |
f456179f | 983 | close_peer_eventfds(s, i); |
f64a078d MAL |
984 | } |
985 | g_free(s->peers); | |
986 | } | |
987 | ||
988 | if (ivshmem_has_feature(s, IVSHMEM_MSI)) { | |
989 | msix_uninit_exclusive_bar(dev); | |
990 | } | |
991 | ||
0f57350e | 992 | g_free(s->msi_vectors); |
6cbf4c8c CM |
993 | } |
994 | ||
1f8552df MAL |
995 | static int ivshmem_pre_load(void *opaque) |
996 | { | |
997 | IVShmemState *s = opaque; | |
998 | ||
2a845da7 | 999 | if (!ivshmem_is_master(s)) { |
1f8552df MAL |
1000 | error_report("'peer' devices are not migratable"); |
1001 | return -EINVAL; | |
1002 | } | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | static int ivshmem_post_load(void *opaque, int version_id) | |
1008 | { | |
1009 | IVShmemState *s = opaque; | |
1010 | ||
1011 | if (ivshmem_has_feature(s, IVSHMEM_MSI)) { | |
082751e8 | 1012 | ivshmem_msix_vector_use(s); |
1f8552df | 1013 | } |
1f8552df MAL |
1014 | return 0; |
1015 | } | |
1016 | ||
5400c02b | 1017 | static void ivshmem_common_class_init(ObjectClass *klass, void *data) |
40021f08 | 1018 | { |
39bffca2 | 1019 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
1020 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
1021 | ||
5400c02b MA |
1022 | k->realize = ivshmem_common_realize; |
1023 | k->exit = ivshmem_exit; | |
d58d7e84 | 1024 | k->config_write = ivshmem_write_config; |
b8ef62a9 PB |
1025 | k->vendor_id = PCI_VENDOR_ID_IVSHMEM; |
1026 | k->device_id = PCI_DEVICE_ID_IVSHMEM; | |
40021f08 | 1027 | k->class_id = PCI_CLASS_MEMORY_RAM; |
5400c02b | 1028 | k->revision = 1; |
39bffca2 | 1029 | dc->reset = ivshmem_reset; |
125ee0ed | 1030 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
d383537d | 1031 | dc->desc = "Inter-VM shared memory"; |
40021f08 AL |
1032 | } |
1033 | ||
ddc85284 MA |
1034 | static const TypeInfo ivshmem_common_info = { |
1035 | .name = TYPE_IVSHMEM_COMMON, | |
1036 | .parent = TYPE_PCI_DEVICE, | |
1037 | .instance_size = sizeof(IVShmemState), | |
1038 | .abstract = true, | |
1039 | .class_init = ivshmem_common_class_init, | |
fd3b02c8 EH |
1040 | .interfaces = (InterfaceInfo[]) { |
1041 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | |
1042 | { }, | |
1043 | }, | |
ddc85284 | 1044 | }; |
5400c02b | 1045 | |
5400c02b MA |
1046 | static const VMStateDescription ivshmem_plain_vmsd = { |
1047 | .name = TYPE_IVSHMEM_PLAIN, | |
1048 | .version_id = 0, | |
1049 | .minimum_version_id = 0, | |
1050 | .pre_load = ivshmem_pre_load, | |
1051 | .post_load = ivshmem_post_load, | |
1052 | .fields = (VMStateField[]) { | |
1053 | VMSTATE_PCI_DEVICE(parent_obj, IVShmemState), | |
1054 | VMSTATE_UINT32(intrstatus, IVShmemState), | |
1055 | VMSTATE_UINT32(intrmask, IVShmemState), | |
1056 | VMSTATE_END_OF_LIST() | |
1057 | }, | |
1058 | }; | |
1059 | ||
1060 | static Property ivshmem_plain_properties[] = { | |
1061 | DEFINE_PROP_ON_OFF_AUTO("master", IVShmemState, master, ON_OFF_AUTO_OFF), | |
e9cb190a FZ |
1062 | DEFINE_PROP_LINK("memdev", IVShmemState, hostmem, TYPE_MEMORY_BACKEND, |
1063 | HostMemoryBackend *), | |
5400c02b MA |
1064 | DEFINE_PROP_END_OF_LIST(), |
1065 | }; | |
1066 | ||
6dc64780 MAL |
1067 | static void ivshmem_plain_realize(PCIDevice *dev, Error **errp) |
1068 | { | |
1069 | IVShmemState *s = IVSHMEM_COMMON(dev); | |
1070 | ||
1071 | if (!s->hostmem) { | |
1072 | error_setg(errp, "You must specify a 'memdev'"); | |
1073 | return; | |
e9cb190a FZ |
1074 | } else if (host_memory_backend_is_mapped(s->hostmem)) { |
1075 | char *path = object_get_canonical_path_component(OBJECT(s->hostmem)); | |
1076 | error_setg(errp, "can't use already busy memdev: %s", path); | |
1077 | g_free(path); | |
1078 | return; | |
6dc64780 MAL |
1079 | } |
1080 | ||
1081 | ivshmem_common_realize(dev, errp); | |
1082 | } | |
1083 | ||
5400c02b MA |
1084 | static void ivshmem_plain_class_init(ObjectClass *klass, void *data) |
1085 | { | |
1086 | DeviceClass *dc = DEVICE_CLASS(klass); | |
6dc64780 | 1087 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
5400c02b | 1088 | |
6dc64780 | 1089 | k->realize = ivshmem_plain_realize; |
5400c02b MA |
1090 | dc->props = ivshmem_plain_properties; |
1091 | dc->vmsd = &ivshmem_plain_vmsd; | |
1092 | } | |
1093 | ||
1094 | static const TypeInfo ivshmem_plain_info = { | |
1095 | .name = TYPE_IVSHMEM_PLAIN, | |
1096 | .parent = TYPE_IVSHMEM_COMMON, | |
1097 | .instance_size = sizeof(IVShmemState), | |
5400c02b MA |
1098 | .class_init = ivshmem_plain_class_init, |
1099 | }; | |
1100 | ||
1101 | static const VMStateDescription ivshmem_doorbell_vmsd = { | |
1102 | .name = TYPE_IVSHMEM_DOORBELL, | |
1103 | .version_id = 0, | |
1104 | .minimum_version_id = 0, | |
1105 | .pre_load = ivshmem_pre_load, | |
1106 | .post_load = ivshmem_post_load, | |
1107 | .fields = (VMStateField[]) { | |
1108 | VMSTATE_PCI_DEVICE(parent_obj, IVShmemState), | |
1109 | VMSTATE_MSIX(parent_obj, IVShmemState), | |
1110 | VMSTATE_UINT32(intrstatus, IVShmemState), | |
1111 | VMSTATE_UINT32(intrmask, IVShmemState), | |
1112 | VMSTATE_END_OF_LIST() | |
1113 | }, | |
1114 | }; | |
1115 | ||
1116 | static Property ivshmem_doorbell_properties[] = { | |
1117 | DEFINE_PROP_CHR("chardev", IVShmemState, server_chr), | |
1118 | DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1), | |
1119 | DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, | |
1120 | true), | |
1121 | DEFINE_PROP_ON_OFF_AUTO("master", IVShmemState, master, ON_OFF_AUTO_OFF), | |
1122 | DEFINE_PROP_END_OF_LIST(), | |
1123 | }; | |
1124 | ||
1125 | static void ivshmem_doorbell_init(Object *obj) | |
1126 | { | |
1127 | IVShmemState *s = IVSHMEM_DOORBELL(obj); | |
1128 | ||
1129 | s->features |= (1 << IVSHMEM_MSI); | |
5400c02b MA |
1130 | } |
1131 | ||
6dc64780 MAL |
1132 | static void ivshmem_doorbell_realize(PCIDevice *dev, Error **errp) |
1133 | { | |
1134 | IVShmemState *s = IVSHMEM_COMMON(dev); | |
1135 | ||
30650701 | 1136 | if (!qemu_chr_fe_backend_connected(&s->server_chr)) { |
6dc64780 MAL |
1137 | error_setg(errp, "You must specify a 'chardev'"); |
1138 | return; | |
1139 | } | |
1140 | ||
1141 | ivshmem_common_realize(dev, errp); | |
1142 | } | |
1143 | ||
5400c02b MA |
1144 | static void ivshmem_doorbell_class_init(ObjectClass *klass, void *data) |
1145 | { | |
1146 | DeviceClass *dc = DEVICE_CLASS(klass); | |
6dc64780 | 1147 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
5400c02b | 1148 | |
6dc64780 | 1149 | k->realize = ivshmem_doorbell_realize; |
5400c02b MA |
1150 | dc->props = ivshmem_doorbell_properties; |
1151 | dc->vmsd = &ivshmem_doorbell_vmsd; | |
1152 | } | |
1153 | ||
1154 | static const TypeInfo ivshmem_doorbell_info = { | |
1155 | .name = TYPE_IVSHMEM_DOORBELL, | |
1156 | .parent = TYPE_IVSHMEM_COMMON, | |
1157 | .instance_size = sizeof(IVShmemState), | |
1158 | .instance_init = ivshmem_doorbell_init, | |
1159 | .class_init = ivshmem_doorbell_class_init, | |
1160 | }; | |
1161 | ||
83f7d43a | 1162 | static void ivshmem_register_types(void) |
6cbf4c8c | 1163 | { |
5400c02b MA |
1164 | type_register_static(&ivshmem_common_info); |
1165 | type_register_static(&ivshmem_plain_info); | |
1166 | type_register_static(&ivshmem_doorbell_info); | |
6cbf4c8c CM |
1167 | } |
1168 | ||
83f7d43a | 1169 | type_init(ivshmem_register_types) |