2 * Inter-VM Shared Memory PCI device.
5 * Cam Macdonell <cam@cs.ualberta.ca>
7 * Based On: cirrus_vga.c
8 * Copyright (c) 2004 Fabrice Bellard
9 * Copyright (c) 2004 Makoto Suzuki (suzu)
12 * Copyright (c) 2006 Igor Kovalenko
14 * This code is licensed under the GNU GPL v2.
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.
19 #include "qemu/osdep.h"
20 #include "qemu/units.h"
21 #include "qapi/error.h"
22 #include "qemu/cutils.h"
24 #include "hw/pci/pci.h"
25 #include "hw/pci/msi.h"
26 #include "hw/pci/msix.h"
27 #include "sysemu/kvm.h"
28 #include "migration/blocker.h"
29 #include "qemu/error-report.h"
30 #include "qemu/event_notifier.h"
31 #include "qom/object_interfaces.h"
32 #include "chardev/char-fe.h"
33 #include "sysemu/hostmem.h"
34 #include "sysemu/qtest.h"
35 #include "qapi/visitor.h"
37 #include "hw/misc/ivshmem.h"
39 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
40 #define PCI_DEVICE_ID_IVSHMEM 0x1110
42 #define IVSHMEM_MAX_PEERS UINT16_MAX
43 #define IVSHMEM_IOEVENTFD 0
46 #define IVSHMEM_REG_BAR_SIZE 0x100
48 #define IVSHMEM_DEBUG 0
49 #define IVSHMEM_DPRINTF(fmt, ...) \
51 if (IVSHMEM_DEBUG) { \
52 printf("IVSHMEM: " fmt, ## __VA_ARGS__); \
56 #define TYPE_IVSHMEM_COMMON "ivshmem-common"
57 #define IVSHMEM_COMMON(obj) \
58 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_COMMON)
60 #define TYPE_IVSHMEM_PLAIN "ivshmem-plain"
61 #define IVSHMEM_PLAIN(obj) \
62 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_PLAIN)
64 #define TYPE_IVSHMEM_DOORBELL "ivshmem-doorbell"
65 #define IVSHMEM_DOORBELL(obj) \
66 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_DOORBELL)
68 #define TYPE_IVSHMEM "ivshmem"
69 #define IVSHMEM(obj) \
70 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
74 EventNotifier
*eventfds
;
77 typedef struct MSIVector
{
83 typedef struct IVShmemState
{
90 /* exactly one of these two may be set */
91 HostMemoryBackend
*hostmem
; /* with interrupts */
92 CharBackend server_chr
; /* without interrupts */
100 MemoryRegion ivshmem_mmio
; /* BAR 0 (registers) */
101 MemoryRegion
*ivshmem_bar2
; /* BAR 2 (shared memory) */
102 MemoryRegion server_bar2
; /* used with server_chr */
104 /* interrupt support */
106 int nb_peers
; /* space in @peers[] */
108 MSIVector
*msi_vectors
;
109 uint64_t msg_buf
; /* buffer for receiving server messages */
110 int msg_buffered_bytes
; /* #bytes in @msg_buf */
112 /* migration stuff */
114 Error
*migration_blocker
;
117 /* registers for the Inter-VM shared memory device */
118 enum ivshmem_registers
{
125 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
126 unsigned int feature
) {
127 return (ivs
->features
& (1 << feature
));
130 static inline bool ivshmem_is_master(IVShmemState
*s
)
132 assert(s
->master
!= ON_OFF_AUTO_AUTO
);
133 return s
->master
== ON_OFF_AUTO_ON
;
136 static void ivshmem_update_irq(IVShmemState
*s
)
138 PCIDevice
*d
= PCI_DEVICE(s
);
139 uint32_t isr
= s
->intrstatus
& s
->intrmask
;
142 * Do nothing unless the device actually uses INTx. Here's how
143 * the device variants signal interrupts, what they put in PCI
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:
154 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)
155 || !d
->config
[PCI_INTERRUPT_PIN
]) {
159 /* don't print ISR resets */
161 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
162 isr
? 1 : 0, s
->intrstatus
, s
->intrmask
);
165 pci_set_irq(d
, isr
!= 0);
168 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
170 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
173 ivshmem_update_irq(s
);
176 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
178 uint32_t ret
= s
->intrmask
;
180 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret
);
184 static void ivshmem_IntrStatus_write(IVShmemState
*s
, uint32_t val
)
186 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val
);
189 ivshmem_update_irq(s
);
192 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
194 uint32_t ret
= s
->intrstatus
;
196 /* reading ISR clears all interrupts */
198 ivshmem_update_irq(s
);
202 static void ivshmem_io_write(void *opaque
, hwaddr addr
,
203 uint64_t val
, unsigned size
)
205 IVShmemState
*s
= opaque
;
207 uint16_t dest
= val
>> 16;
208 uint16_t vector
= val
& 0xff;
212 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
216 ivshmem_IntrMask_write(s
, val
);
220 ivshmem_IntrStatus_write(s
, val
);
224 /* check that dest VM ID is reasonable */
225 if (dest
>= s
->nb_peers
) {
226 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
230 /* check doorbell range */
231 if (vector
< s
->peers
[dest
].nb_eventfds
) {
232 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
233 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
235 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
240 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx
"\n", addr
);
244 static uint64_t ivshmem_io_read(void *opaque
, hwaddr addr
,
248 IVShmemState
*s
= opaque
;
254 ret
= ivshmem_IntrMask_read(s
);
258 ret
= ivshmem_IntrStatus_read(s
);
266 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
273 static const MemoryRegionOps ivshmem_mmio_ops
= {
274 .read
= ivshmem_io_read
,
275 .write
= ivshmem_io_write
,
276 .endianness
= DEVICE_NATIVE_ENDIAN
,
278 .min_access_size
= 4,
279 .max_access_size
= 4,
283 static void ivshmem_vector_notify(void *opaque
)
285 MSIVector
*entry
= opaque
;
286 PCIDevice
*pdev
= entry
->pdev
;
287 IVShmemState
*s
= IVSHMEM_COMMON(pdev
);
288 int vector
= entry
- s
->msi_vectors
;
289 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
291 if (!event_notifier_test_and_clear(n
)) {
295 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, vector
);
296 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
297 if (msix_enabled(pdev
)) {
298 msix_notify(pdev
, vector
);
301 ivshmem_IntrStatus_write(s
, 1);
305 static int ivshmem_vector_unmask(PCIDevice
*dev
, unsigned vector
,
308 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
309 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
310 MSIVector
*v
= &s
->msi_vectors
[vector
];
313 IVSHMEM_DPRINTF("vector unmask %p %d\n", dev
, vector
);
315 error_report("ivshmem: vector %d route does not exist", vector
);
318 assert(!v
->unmasked
);
320 ret
= kvm_irqchip_update_msi_route(kvm_state
, v
->virq
, msg
, dev
);
324 kvm_irqchip_commit_routes(kvm_state
);
326 ret
= kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, v
->virq
);
335 static void ivshmem_vector_mask(PCIDevice
*dev
, unsigned vector
)
337 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
338 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
339 MSIVector
*v
= &s
->msi_vectors
[vector
];
342 IVSHMEM_DPRINTF("vector mask %p %d\n", dev
, vector
);
344 error_report("ivshmem: vector %d route does not exist", vector
);
349 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, v
->virq
);
351 error_report("remove_irqfd_notifier_gsi failed");
357 static void ivshmem_vector_poll(PCIDevice
*dev
,
358 unsigned int vector_start
,
359 unsigned int vector_end
)
361 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
364 IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev
, vector_start
, vector_end
);
366 vector_end
= MIN(vector_end
, s
->vectors
);
368 for (vector
= vector_start
; vector
< vector_end
; vector
++) {
369 EventNotifier
*notifier
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
371 if (!msix_is_masked(dev
, vector
)) {
375 if (event_notifier_test_and_clear(notifier
)) {
376 msix_set_pending(dev
, vector
);
381 static void watch_vector_notifier(IVShmemState
*s
, EventNotifier
*n
,
384 int eventfd
= event_notifier_get_fd(n
);
386 assert(!s
->msi_vectors
[vector
].pdev
);
387 s
->msi_vectors
[vector
].pdev
= PCI_DEVICE(s
);
389 qemu_set_fd_handler(eventfd
, ivshmem_vector_notify
,
390 NULL
, &s
->msi_vectors
[vector
]);
393 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
395 memory_region_add_eventfd(&s
->ivshmem_mmio
,
400 &s
->peers
[posn
].eventfds
[i
]);
403 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
405 memory_region_del_eventfd(&s
->ivshmem_mmio
,
410 &s
->peers
[posn
].eventfds
[i
]);
413 static void close_peer_eventfds(IVShmemState
*s
, int posn
)
417 assert(posn
>= 0 && posn
< s
->nb_peers
);
418 n
= s
->peers
[posn
].nb_eventfds
;
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
);
425 memory_region_transaction_commit();
428 for (i
= 0; i
< n
; i
++) {
429 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
432 g_free(s
->peers
[posn
].eventfds
);
433 s
->peers
[posn
].nb_eventfds
= 0;
436 static void resize_peers(IVShmemState
*s
, int nb_peers
)
438 int old_nb_peers
= s
->nb_peers
;
441 assert(nb_peers
> old_nb_peers
);
442 IVSHMEM_DPRINTF("bumping storage to %d peers\n", nb_peers
);
444 s
->peers
= g_realloc(s
->peers
, nb_peers
* sizeof(Peer
));
445 s
->nb_peers
= nb_peers
;
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;
453 static void ivshmem_add_kvm_msi_virq(IVShmemState
*s
, int vector
,
456 PCIDevice
*pdev
= PCI_DEVICE(s
);
459 IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector
);
460 assert(!s
->msi_vectors
[vector
].pdev
);
462 ret
= kvm_irqchip_add_msi_route(kvm_state
, vector
, pdev
);
464 error_setg(errp
, "kvm_irqchip_add_msi_route failed");
468 s
->msi_vectors
[vector
].virq
= ret
;
469 s
->msi_vectors
[vector
].pdev
= pdev
;
472 static void setup_interrupt(IVShmemState
*s
, int vector
, Error
**errp
)
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
);
480 IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector
);
483 IVSHMEM_DPRINTF("with eventfd\n");
484 watch_vector_notifier(s
, n
, vector
);
485 } else if (msix_enabled(pdev
)) {
486 IVSHMEM_DPRINTF("with irqfd\n");
487 ivshmem_add_kvm_msi_virq(s
, vector
, &err
);
489 error_propagate(errp
, err
);
493 if (!msix_is_masked(pdev
, vector
)) {
494 kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
,
495 s
->msi_vectors
[vector
].virq
);
496 /* TODO handle error */
499 /* it will be delayed until msix is enabled, in write_config */
500 IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled\n");
504 static void process_msg_shmem(IVShmemState
*s
, int fd
, Error
**errp
)
506 Error
*local_err
= NULL
;
510 if (s
->ivshmem_bar2
) {
511 error_setg(errp
, "server sent unexpected shared memory message");
516 if (fstat(fd
, &buf
) < 0) {
517 error_setg_errno(errp
, errno
,
518 "can't determine size of shared memory sent by server");
525 /* mmap the region and map into the BAR2 */
526 memory_region_init_ram_from_fd(&s
->server_bar2
, OBJECT(s
),
527 "ivshmem.bar2", size
, true, fd
, &local_err
);
529 error_propagate(errp
, local_err
);
533 s
->ivshmem_bar2
= &s
->server_bar2
;
536 static void process_msg_disconnect(IVShmemState
*s
, uint16_t posn
,
539 IVSHMEM_DPRINTF("posn %d has gone away\n", posn
);
540 if (posn
>= s
->nb_peers
|| posn
== s
->vm_id
) {
541 error_setg(errp
, "invalid peer %d", posn
);
544 close_peer_eventfds(s
, posn
);
547 static void process_msg_connect(IVShmemState
*s
, uint16_t posn
, int fd
,
550 Peer
*peer
= &s
->peers
[posn
];
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.
557 if (peer
->nb_eventfds
>= s
->vectors
) {
558 error_setg(errp
, "Too many eventfd received, device has %d vectors",
563 vector
= peer
->nb_eventfds
++;
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 */
569 if (posn
== s
->vm_id
) {
570 setup_interrupt(s
, vector
, errp
);
571 /* TODO do we need to handle the error? */
574 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
575 ivshmem_add_eventfd(s
, posn
, vector
);
579 static void process_msg(IVShmemState
*s
, int64_t msg
, int fd
, Error
**errp
)
581 IVSHMEM_DPRINTF("posn is %" PRId64
", fd is %d\n", msg
, fd
);
583 if (msg
< -1 || msg
> IVSHMEM_MAX_PEERS
) {
584 error_setg(errp
, "server sent invalid message %" PRId64
, msg
);
590 process_msg_shmem(s
, fd
, errp
);
594 if (msg
>= s
->nb_peers
) {
595 resize_peers(s
, msg
+ 1);
599 process_msg_connect(s
, msg
, fd
, errp
);
601 process_msg_disconnect(s
, msg
, errp
);
605 static int ivshmem_can_receive(void *opaque
)
607 IVShmemState
*s
= opaque
;
609 assert(s
->msg_buffered_bytes
< sizeof(s
->msg_buf
));
610 return sizeof(s
->msg_buf
) - s
->msg_buffered_bytes
;
613 static void ivshmem_read(void *opaque
, const uint8_t *buf
, int size
)
615 IVShmemState
*s
= opaque
;
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
)) {
626 msg
= le64_to_cpu(s
->msg_buf
);
627 s
->msg_buffered_bytes
= 0;
629 fd
= qemu_chr_fe_get_msgfd(&s
->server_chr
);
631 process_msg(s
, msg
, fd
, &err
);
633 error_report_err(err
);
637 static int64_t ivshmem_recv_msg(IVShmemState
*s
, int *pfd
, Error
**errp
)
644 ret
= qemu_chr_fe_read_all(&s
->server_chr
, (uint8_t *)&msg
+ n
,
650 error_setg_errno(errp
, -ret
, "read from server failed");
654 } while (n
< sizeof(msg
));
656 *pfd
= qemu_chr_fe_get_msgfd(&s
->server_chr
);
657 return le64_to_cpu(msg
);
660 static void ivshmem_recv_setup(IVShmemState
*s
, Error
**errp
)
666 msg
= ivshmem_recv_msg(s
, &fd
, &err
);
668 error_propagate(errp
, err
);
671 if (msg
!= IVSHMEM_PROTOCOL_VERSION
) {
672 error_setg(errp
, "server sent version %" PRId64
", expecting %d",
673 msg
, IVSHMEM_PROTOCOL_VERSION
);
677 error_setg(errp
, "server sent invalid version message");
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.
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.
694 msg
= ivshmem_recv_msg(s
, &fd
, &err
);
696 error_propagate(errp
, err
);
699 if (fd
!= -1 || msg
< 0 || msg
> IVSHMEM_MAX_PEERS
) {
700 error_setg(errp
, "server sent invalid ID message");
706 * Receive more messages until we got shared memory.
709 msg
= ivshmem_recv_msg(s
, &fd
, &err
);
711 error_propagate(errp
, err
);
714 process_msg(s
, msg
, fd
, &err
);
716 error_propagate(errp
, err
);
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:
727 assert(s
->ivshmem_bar2
);
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. */
733 static void ivshmem_msix_vector_use(IVShmemState
*s
)
735 PCIDevice
*d
= PCI_DEVICE(s
);
738 for (i
= 0; i
< s
->vectors
; i
++) {
739 msix_vector_use(d
, i
);
743 static void ivshmem_disable_irqfd(IVShmemState
*s
);
745 static void ivshmem_reset(DeviceState
*d
)
747 IVShmemState
*s
= IVSHMEM_COMMON(d
);
749 ivshmem_disable_irqfd(s
);
753 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
754 ivshmem_msix_vector_use(s
);
758 static int ivshmem_setup_interrupts(IVShmemState
*s
, Error
**errp
)
760 /* allocate QEMU callback data for receiving interrupts */
761 s
->msi_vectors
= g_malloc0(s
->vectors
* sizeof(MSIVector
));
763 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
764 if (msix_init_exclusive_bar(PCI_DEVICE(s
), s
->vectors
, 1, errp
)) {
768 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
769 ivshmem_msix_vector_use(s
);
775 static void ivshmem_remove_kvm_msi_virq(IVShmemState
*s
, int vector
)
777 IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector
);
779 if (s
->msi_vectors
[vector
].pdev
== NULL
) {
783 /* it was cleaned when masked in the frontend. */
784 kvm_irqchip_release_virq(kvm_state
, s
->msi_vectors
[vector
].virq
);
786 s
->msi_vectors
[vector
].pdev
= NULL
;
789 static void ivshmem_enable_irqfd(IVShmemState
*s
)
791 PCIDevice
*pdev
= PCI_DEVICE(s
);
794 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
797 ivshmem_add_kvm_msi_virq(s
, i
, &err
);
799 error_report_err(err
);
804 if (msix_set_vector_notifiers(pdev
,
805 ivshmem_vector_unmask
,
807 ivshmem_vector_poll
)) {
808 error_report("ivshmem: msix_set_vector_notifiers failed");
815 ivshmem_remove_kvm_msi_virq(s
, i
);
819 static void ivshmem_disable_irqfd(IVShmemState
*s
)
821 PCIDevice
*pdev
= PCI_DEVICE(s
);
824 if (!pdev
->msix_vector_use_notifier
) {
828 msix_unset_vector_notifiers(pdev
);
830 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
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
836 if (s
->msi_vectors
[i
].unmasked
) {
837 ivshmem_vector_mask(pdev
, i
);
839 ivshmem_remove_kvm_msi_virq(s
, i
);
844 static void ivshmem_write_config(PCIDevice
*pdev
, uint32_t address
,
845 uint32_t val
, int len
)
847 IVShmemState
*s
= IVSHMEM_COMMON(pdev
);
848 int is_enabled
, was_enabled
= msix_enabled(pdev
);
850 pci_default_write_config(pdev
, address
, val
, len
);
851 is_enabled
= msix_enabled(pdev
);
853 if (kvm_msi_via_irqfd_enabled()) {
854 if (!was_enabled
&& is_enabled
) {
855 ivshmem_enable_irqfd(s
);
856 } else if (was_enabled
&& !is_enabled
) {
857 ivshmem_disable_irqfd(s
);
862 static void ivshmem_common_realize(PCIDevice
*dev
, Error
**errp
)
864 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
867 Error
*local_err
= NULL
;
869 /* IRQFD requires MSI */
870 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
) &&
871 !ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
872 error_setg(errp
, "ioeventfd/irqfd requires MSI");
876 pci_conf
= dev
->config
;
877 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
879 memory_region_init_io(&s
->ivshmem_mmio
, OBJECT(s
), &ivshmem_mmio_ops
, s
,
880 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
882 /* region for registers*/
883 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
886 if (s
->hostmem
!= NULL
) {
887 IVSHMEM_DPRINTF("using hostmem\n");
889 s
->ivshmem_bar2
= host_memory_backend_get_memory(s
->hostmem
);
890 host_memory_backend_set_mapped(s
->hostmem
, true);
892 Chardev
*chr
= qemu_chr_fe_get_driver(&s
->server_chr
);
895 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
898 /* we allocate enough space for 16 peers and grow as needed */
902 * Receive setup messages from server synchronously.
903 * Older versions did it asynchronously, but that creates a
904 * number of entertaining race conditions.
906 ivshmem_recv_setup(s
, &err
);
908 error_propagate(errp
, err
);
912 if (s
->master
== ON_OFF_AUTO_ON
&& s
->vm_id
!= 0) {
914 "master must connect to the server before any peers");
918 qemu_chr_fe_set_handlers(&s
->server_chr
, ivshmem_can_receive
,
919 ivshmem_read
, NULL
, NULL
, s
, NULL
, true);
921 if (ivshmem_setup_interrupts(s
, errp
) < 0) {
922 error_prepend(errp
, "Failed to initialize interrupts: ");
927 if (s
->master
== ON_OFF_AUTO_AUTO
) {
928 s
->master
= s
->vm_id
== 0 ? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
931 if (!ivshmem_is_master(s
)) {
932 error_setg(&s
->migration_blocker
,
933 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
934 migrate_add_blocker(s
->migration_blocker
, &local_err
);
936 error_propagate(errp
, local_err
);
937 error_free(s
->migration_blocker
);
942 vmstate_register_ram(s
->ivshmem_bar2
, DEVICE(s
));
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
,
950 static void ivshmem_exit(PCIDevice
*dev
)
952 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
955 if (s
->migration_blocker
) {
956 migrate_del_blocker(s
->migration_blocker
);
957 error_free(s
->migration_blocker
);
960 if (memory_region_is_mapped(s
->ivshmem_bar2
)) {
962 void *addr
= memory_region_get_ram_ptr(s
->ivshmem_bar2
);
965 if (munmap(addr
, memory_region_size(s
->ivshmem_bar2
) == -1)) {
966 error_report("Failed to munmap shared memory %s",
970 fd
= memory_region_get_fd(s
->ivshmem_bar2
);
974 vmstate_unregister_ram(s
->ivshmem_bar2
, DEVICE(dev
));
978 host_memory_backend_set_mapped(s
->hostmem
, false);
982 for (i
= 0; i
< s
->nb_peers
; i
++) {
983 close_peer_eventfds(s
, i
);
988 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
989 msix_uninit_exclusive_bar(dev
);
992 g_free(s
->msi_vectors
);
995 static int ivshmem_pre_load(void *opaque
)
997 IVShmemState
*s
= opaque
;
999 if (!ivshmem_is_master(s
)) {
1000 error_report("'peer' devices are not migratable");
1007 static int ivshmem_post_load(void *opaque
, int version_id
)
1009 IVShmemState
*s
= opaque
;
1011 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1012 ivshmem_msix_vector_use(s
);
1017 static void ivshmem_common_class_init(ObjectClass
*klass
, void *data
)
1019 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1020 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1022 k
->realize
= ivshmem_common_realize
;
1023 k
->exit
= ivshmem_exit
;
1024 k
->config_write
= ivshmem_write_config
;
1025 k
->vendor_id
= PCI_VENDOR_ID_IVSHMEM
;
1026 k
->device_id
= PCI_DEVICE_ID_IVSHMEM
;
1027 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
1029 dc
->reset
= ivshmem_reset
;
1030 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1031 dc
->desc
= "Inter-VM shared memory";
1034 static const TypeInfo ivshmem_common_info
= {
1035 .name
= TYPE_IVSHMEM_COMMON
,
1036 .parent
= TYPE_PCI_DEVICE
,
1037 .instance_size
= sizeof(IVShmemState
),
1039 .class_init
= ivshmem_common_class_init
,
1040 .interfaces
= (InterfaceInfo
[]) {
1041 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1046 static const VMStateDescription ivshmem_plain_vmsd
= {
1047 .name
= TYPE_IVSHMEM_PLAIN
,
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()
1060 static Property ivshmem_plain_properties
[] = {
1061 DEFINE_PROP_ON_OFF_AUTO("master", IVShmemState
, master
, ON_OFF_AUTO_OFF
),
1062 DEFINE_PROP_LINK("memdev", IVShmemState
, hostmem
, TYPE_MEMORY_BACKEND
,
1063 HostMemoryBackend
*),
1064 DEFINE_PROP_END_OF_LIST(),
1067 static void ivshmem_plain_realize(PCIDevice
*dev
, Error
**errp
)
1069 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
1072 error_setg(errp
, "You must specify a 'memdev'");
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
);
1081 ivshmem_common_realize(dev
, errp
);
1084 static void ivshmem_plain_class_init(ObjectClass
*klass
, void *data
)
1086 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1087 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1089 k
->realize
= ivshmem_plain_realize
;
1090 dc
->props
= ivshmem_plain_properties
;
1091 dc
->vmsd
= &ivshmem_plain_vmsd
;
1094 static const TypeInfo ivshmem_plain_info
= {
1095 .name
= TYPE_IVSHMEM_PLAIN
,
1096 .parent
= TYPE_IVSHMEM_COMMON
,
1097 .instance_size
= sizeof(IVShmemState
),
1098 .class_init
= ivshmem_plain_class_init
,
1101 static const VMStateDescription ivshmem_doorbell_vmsd
= {
1102 .name
= TYPE_IVSHMEM_DOORBELL
,
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()
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
,
1121 DEFINE_PROP_ON_OFF_AUTO("master", IVShmemState
, master
, ON_OFF_AUTO_OFF
),
1122 DEFINE_PROP_END_OF_LIST(),
1125 static void ivshmem_doorbell_init(Object
*obj
)
1127 IVShmemState
*s
= IVSHMEM_DOORBELL(obj
);
1129 s
->features
|= (1 << IVSHMEM_MSI
);
1132 static void ivshmem_doorbell_realize(PCIDevice
*dev
, Error
**errp
)
1134 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
1136 if (!qemu_chr_fe_backend_connected(&s
->server_chr
)) {
1137 error_setg(errp
, "You must specify a 'chardev'");
1141 ivshmem_common_realize(dev
, errp
);
1144 static void ivshmem_doorbell_class_init(ObjectClass
*klass
, void *data
)
1146 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1147 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1149 k
->realize
= ivshmem_doorbell_realize
;
1150 dc
->props
= ivshmem_doorbell_properties
;
1151 dc
->vmsd
= &ivshmem_doorbell_vmsd
;
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
,
1162 static void ivshmem_register_types(void)
1164 type_register_static(&ivshmem_common_info
);
1165 type_register_static(&ivshmem_plain_info
);
1166 type_register_static(&ivshmem_doorbell_info
);
1169 type_init(ivshmem_register_types
)