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.
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
22 #include "qapi/error.h"
23 #include "qemu/cutils.h"
24 #include "hw/pci/pci.h"
25 #include "hw/qdev-properties.h"
26 #include "hw/pci/msi.h"
27 #include "hw/pci/msix.h"
28 #include "sysemu/kvm.h"
29 #include "migration/blocker.h"
30 #include "migration/vmstate.h"
31 #include "qemu/error-report.h"
32 #include "qemu/event_notifier.h"
33 #include "qemu/module.h"
34 #include "qom/object_interfaces.h"
35 #include "chardev/char-fe.h"
36 #include "sysemu/hostmem.h"
37 #include "sysemu/qtest.h"
38 #include "qapi/visitor.h"
40 #include "hw/misc/ivshmem.h"
42 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
43 #define PCI_DEVICE_ID_IVSHMEM 0x1110
45 #define IVSHMEM_MAX_PEERS UINT16_MAX
46 #define IVSHMEM_IOEVENTFD 0
49 #define IVSHMEM_REG_BAR_SIZE 0x100
51 #define IVSHMEM_DEBUG 0
52 #define IVSHMEM_DPRINTF(fmt, ...) \
54 if (IVSHMEM_DEBUG) { \
55 printf("IVSHMEM: " fmt, ## __VA_ARGS__); \
59 #define TYPE_IVSHMEM_COMMON "ivshmem-common"
60 #define IVSHMEM_COMMON(obj) \
61 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_COMMON)
63 #define TYPE_IVSHMEM_PLAIN "ivshmem-plain"
64 #define IVSHMEM_PLAIN(obj) \
65 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_PLAIN)
67 #define TYPE_IVSHMEM_DOORBELL "ivshmem-doorbell"
68 #define IVSHMEM_DOORBELL(obj) \
69 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM_DOORBELL)
71 #define TYPE_IVSHMEM "ivshmem"
72 #define IVSHMEM(obj) \
73 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
77 EventNotifier
*eventfds
;
80 typedef struct MSIVector
{
86 typedef struct IVShmemState
{
93 /* exactly one of these two may be set */
94 HostMemoryBackend
*hostmem
; /* with interrupts */
95 CharBackend server_chr
; /* without interrupts */
103 MemoryRegion ivshmem_mmio
; /* BAR 0 (registers) */
104 MemoryRegion
*ivshmem_bar2
; /* BAR 2 (shared memory) */
105 MemoryRegion server_bar2
; /* used with server_chr */
107 /* interrupt support */
109 int nb_peers
; /* space in @peers[] */
111 MSIVector
*msi_vectors
;
112 uint64_t msg_buf
; /* buffer for receiving server messages */
113 int msg_buffered_bytes
; /* #bytes in @msg_buf */
115 /* migration stuff */
117 Error
*migration_blocker
;
120 /* registers for the Inter-VM shared memory device */
121 enum ivshmem_registers
{
128 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
129 unsigned int feature
) {
130 return (ivs
->features
& (1 << feature
));
133 static inline bool ivshmem_is_master(IVShmemState
*s
)
135 assert(s
->master
!= ON_OFF_AUTO_AUTO
);
136 return s
->master
== ON_OFF_AUTO_ON
;
139 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
141 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
146 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
148 uint32_t ret
= s
->intrmask
;
150 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret
);
154 static void ivshmem_IntrStatus_write(IVShmemState
*s
, uint32_t val
)
156 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val
);
161 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
163 uint32_t ret
= s
->intrstatus
;
165 /* reading ISR clears all interrupts */
170 static void ivshmem_io_write(void *opaque
, hwaddr addr
,
171 uint64_t val
, unsigned size
)
173 IVShmemState
*s
= opaque
;
175 uint16_t dest
= val
>> 16;
176 uint16_t vector
= val
& 0xff;
180 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
184 ivshmem_IntrMask_write(s
, val
);
188 ivshmem_IntrStatus_write(s
, val
);
192 /* check that dest VM ID is reasonable */
193 if (dest
>= s
->nb_peers
) {
194 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
198 /* check doorbell range */
199 if (vector
< s
->peers
[dest
].nb_eventfds
) {
200 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
201 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
203 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
208 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx
"\n", addr
);
212 static uint64_t ivshmem_io_read(void *opaque
, hwaddr addr
,
216 IVShmemState
*s
= opaque
;
222 ret
= ivshmem_IntrMask_read(s
);
226 ret
= ivshmem_IntrStatus_read(s
);
234 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
241 static const MemoryRegionOps ivshmem_mmio_ops
= {
242 .read
= ivshmem_io_read
,
243 .write
= ivshmem_io_write
,
244 .endianness
= DEVICE_NATIVE_ENDIAN
,
246 .min_access_size
= 4,
247 .max_access_size
= 4,
251 static void ivshmem_vector_notify(void *opaque
)
253 MSIVector
*entry
= opaque
;
254 PCIDevice
*pdev
= entry
->pdev
;
255 IVShmemState
*s
= IVSHMEM_COMMON(pdev
);
256 int vector
= entry
- s
->msi_vectors
;
257 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
259 if (!event_notifier_test_and_clear(n
)) {
263 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, vector
);
264 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
265 if (msix_enabled(pdev
)) {
266 msix_notify(pdev
, vector
);
269 ivshmem_IntrStatus_write(s
, 1);
273 static int ivshmem_vector_unmask(PCIDevice
*dev
, unsigned vector
,
276 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
277 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
278 MSIVector
*v
= &s
->msi_vectors
[vector
];
281 IVSHMEM_DPRINTF("vector unmask %p %d\n", dev
, vector
);
283 error_report("ivshmem: vector %d route does not exist", vector
);
286 assert(!v
->unmasked
);
288 ret
= kvm_irqchip_update_msi_route(kvm_state
, v
->virq
, msg
, dev
);
292 kvm_irqchip_commit_routes(kvm_state
);
294 ret
= kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, v
->virq
);
303 static void ivshmem_vector_mask(PCIDevice
*dev
, unsigned vector
)
305 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
306 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
307 MSIVector
*v
= &s
->msi_vectors
[vector
];
310 IVSHMEM_DPRINTF("vector mask %p %d\n", dev
, vector
);
312 error_report("ivshmem: vector %d route does not exist", vector
);
317 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, v
->virq
);
319 error_report("remove_irqfd_notifier_gsi failed");
325 static void ivshmem_vector_poll(PCIDevice
*dev
,
326 unsigned int vector_start
,
327 unsigned int vector_end
)
329 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
332 IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev
, vector_start
, vector_end
);
334 vector_end
= MIN(vector_end
, s
->vectors
);
336 for (vector
= vector_start
; vector
< vector_end
; vector
++) {
337 EventNotifier
*notifier
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
339 if (!msix_is_masked(dev
, vector
)) {
343 if (event_notifier_test_and_clear(notifier
)) {
344 msix_set_pending(dev
, vector
);
349 static void watch_vector_notifier(IVShmemState
*s
, EventNotifier
*n
,
352 int eventfd
= event_notifier_get_fd(n
);
354 assert(!s
->msi_vectors
[vector
].pdev
);
355 s
->msi_vectors
[vector
].pdev
= PCI_DEVICE(s
);
357 qemu_set_fd_handler(eventfd
, ivshmem_vector_notify
,
358 NULL
, &s
->msi_vectors
[vector
]);
361 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
363 memory_region_add_eventfd(&s
->ivshmem_mmio
,
368 &s
->peers
[posn
].eventfds
[i
]);
371 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
373 memory_region_del_eventfd(&s
->ivshmem_mmio
,
378 &s
->peers
[posn
].eventfds
[i
]);
381 static void close_peer_eventfds(IVShmemState
*s
, int posn
)
385 assert(posn
>= 0 && posn
< s
->nb_peers
);
386 n
= s
->peers
[posn
].nb_eventfds
;
388 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
389 memory_region_transaction_begin();
390 for (i
= 0; i
< n
; i
++) {
391 ivshmem_del_eventfd(s
, posn
, i
);
393 memory_region_transaction_commit();
396 for (i
= 0; i
< n
; i
++) {
397 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
400 g_free(s
->peers
[posn
].eventfds
);
401 s
->peers
[posn
].nb_eventfds
= 0;
404 static void resize_peers(IVShmemState
*s
, int nb_peers
)
406 int old_nb_peers
= s
->nb_peers
;
409 assert(nb_peers
> old_nb_peers
);
410 IVSHMEM_DPRINTF("bumping storage to %d peers\n", nb_peers
);
412 s
->peers
= g_realloc(s
->peers
, nb_peers
* sizeof(Peer
));
413 s
->nb_peers
= nb_peers
;
415 for (i
= old_nb_peers
; i
< nb_peers
; i
++) {
416 s
->peers
[i
].eventfds
= g_new0(EventNotifier
, s
->vectors
);
417 s
->peers
[i
].nb_eventfds
= 0;
421 static void ivshmem_add_kvm_msi_virq(IVShmemState
*s
, int vector
,
424 PCIDevice
*pdev
= PCI_DEVICE(s
);
427 IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector
);
428 assert(!s
->msi_vectors
[vector
].pdev
);
430 ret
= kvm_irqchip_add_msi_route(kvm_state
, vector
, pdev
);
432 error_setg(errp
, "kvm_irqchip_add_msi_route failed");
436 s
->msi_vectors
[vector
].virq
= ret
;
437 s
->msi_vectors
[vector
].pdev
= pdev
;
440 static void setup_interrupt(IVShmemState
*s
, int vector
, Error
**errp
)
442 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
443 bool with_irqfd
= kvm_msi_via_irqfd_enabled() &&
444 ivshmem_has_feature(s
, IVSHMEM_MSI
);
445 PCIDevice
*pdev
= PCI_DEVICE(s
);
448 IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector
);
451 IVSHMEM_DPRINTF("with eventfd\n");
452 watch_vector_notifier(s
, n
, vector
);
453 } else if (msix_enabled(pdev
)) {
454 IVSHMEM_DPRINTF("with irqfd\n");
455 ivshmem_add_kvm_msi_virq(s
, vector
, &err
);
457 error_propagate(errp
, err
);
461 if (!msix_is_masked(pdev
, vector
)) {
462 kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
,
463 s
->msi_vectors
[vector
].virq
);
464 /* TODO handle error */
467 /* it will be delayed until msix is enabled, in write_config */
468 IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled\n");
472 static void process_msg_shmem(IVShmemState
*s
, int fd
, Error
**errp
)
474 Error
*local_err
= NULL
;
478 if (s
->ivshmem_bar2
) {
479 error_setg(errp
, "server sent unexpected shared memory message");
484 if (fstat(fd
, &buf
) < 0) {
485 error_setg_errno(errp
, errno
,
486 "can't determine size of shared memory sent by server");
493 /* mmap the region and map into the BAR2 */
494 memory_region_init_ram_from_fd(&s
->server_bar2
, OBJECT(s
),
495 "ivshmem.bar2", size
, true, fd
, &local_err
);
497 error_propagate(errp
, local_err
);
501 s
->ivshmem_bar2
= &s
->server_bar2
;
504 static void process_msg_disconnect(IVShmemState
*s
, uint16_t posn
,
507 IVSHMEM_DPRINTF("posn %d has gone away\n", posn
);
508 if (posn
>= s
->nb_peers
|| posn
== s
->vm_id
) {
509 error_setg(errp
, "invalid peer %d", posn
);
512 close_peer_eventfds(s
, posn
);
515 static void process_msg_connect(IVShmemState
*s
, uint16_t posn
, int fd
,
518 Peer
*peer
= &s
->peers
[posn
];
522 * The N-th connect message for this peer comes with the file
523 * descriptor for vector N-1. Count messages to find the vector.
525 if (peer
->nb_eventfds
>= s
->vectors
) {
526 error_setg(errp
, "Too many eventfd received, device has %d vectors",
531 vector
= peer
->nb_eventfds
++;
533 IVSHMEM_DPRINTF("eventfds[%d][%d] = %d\n", posn
, vector
, fd
);
534 event_notifier_init_fd(&peer
->eventfds
[vector
], fd
);
535 fcntl_setfl(fd
, O_NONBLOCK
); /* msix/irqfd poll non block */
537 if (posn
== s
->vm_id
) {
538 setup_interrupt(s
, vector
, errp
);
539 /* TODO do we need to handle the error? */
542 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
543 ivshmem_add_eventfd(s
, posn
, vector
);
547 static void process_msg(IVShmemState
*s
, int64_t msg
, int fd
, Error
**errp
)
549 IVSHMEM_DPRINTF("posn is %" PRId64
", fd is %d\n", msg
, fd
);
551 if (msg
< -1 || msg
> IVSHMEM_MAX_PEERS
) {
552 error_setg(errp
, "server sent invalid message %" PRId64
, msg
);
558 process_msg_shmem(s
, fd
, errp
);
562 if (msg
>= s
->nb_peers
) {
563 resize_peers(s
, msg
+ 1);
567 process_msg_connect(s
, msg
, fd
, errp
);
569 process_msg_disconnect(s
, msg
, errp
);
573 static int ivshmem_can_receive(void *opaque
)
575 IVShmemState
*s
= opaque
;
577 assert(s
->msg_buffered_bytes
< sizeof(s
->msg_buf
));
578 return sizeof(s
->msg_buf
) - s
->msg_buffered_bytes
;
581 static void ivshmem_read(void *opaque
, const uint8_t *buf
, int size
)
583 IVShmemState
*s
= opaque
;
588 assert(size
>= 0 && s
->msg_buffered_bytes
+ size
<= sizeof(s
->msg_buf
));
589 memcpy((unsigned char *)&s
->msg_buf
+ s
->msg_buffered_bytes
, buf
, size
);
590 s
->msg_buffered_bytes
+= size
;
591 if (s
->msg_buffered_bytes
< sizeof(s
->msg_buf
)) {
594 msg
= le64_to_cpu(s
->msg_buf
);
595 s
->msg_buffered_bytes
= 0;
597 fd
= qemu_chr_fe_get_msgfd(&s
->server_chr
);
599 process_msg(s
, msg
, fd
, &err
);
601 error_report_err(err
);
605 static int64_t ivshmem_recv_msg(IVShmemState
*s
, int *pfd
, Error
**errp
)
612 ret
= qemu_chr_fe_read_all(&s
->server_chr
, (uint8_t *)&msg
+ n
,
618 error_setg_errno(errp
, -ret
, "read from server failed");
622 } while (n
< sizeof(msg
));
624 *pfd
= qemu_chr_fe_get_msgfd(&s
->server_chr
);
625 return le64_to_cpu(msg
);
628 static void ivshmem_recv_setup(IVShmemState
*s
, Error
**errp
)
634 msg
= ivshmem_recv_msg(s
, &fd
, &err
);
636 error_propagate(errp
, err
);
639 if (msg
!= IVSHMEM_PROTOCOL_VERSION
) {
640 error_setg(errp
, "server sent version %" PRId64
", expecting %d",
641 msg
, IVSHMEM_PROTOCOL_VERSION
);
645 error_setg(errp
, "server sent invalid version message");
650 * ivshmem-server sends the remaining initial messages in a fixed
651 * order, but the device has always accepted them in any order.
652 * Stay as compatible as practical, just in case people use
653 * servers that behave differently.
657 * ivshmem_device_spec.txt has always required the ID message
658 * right here, and ivshmem-server has always complied. However,
659 * older versions of the device accepted it out of order, but
660 * broke when an interrupt setup message arrived before it.
662 msg
= ivshmem_recv_msg(s
, &fd
, &err
);
664 error_propagate(errp
, err
);
667 if (fd
!= -1 || msg
< 0 || msg
> IVSHMEM_MAX_PEERS
) {
668 error_setg(errp
, "server sent invalid ID message");
674 * Receive more messages until we got shared memory.
677 msg
= ivshmem_recv_msg(s
, &fd
, &err
);
679 error_propagate(errp
, err
);
682 process_msg(s
, msg
, fd
, &err
);
684 error_propagate(errp
, err
);
690 * This function must either map the shared memory or fail. The
691 * loop above ensures that: it terminates normally only after it
692 * successfully processed the server's shared memory message.
693 * Assert that actually mapped the shared memory:
695 assert(s
->ivshmem_bar2
);
698 /* Select the MSI-X vectors used by device.
699 * ivshmem maps events to vectors statically, so
700 * we just enable all vectors on init and after reset. */
701 static void ivshmem_msix_vector_use(IVShmemState
*s
)
703 PCIDevice
*d
= PCI_DEVICE(s
);
706 for (i
= 0; i
< s
->vectors
; i
++) {
707 msix_vector_use(d
, i
);
711 static void ivshmem_disable_irqfd(IVShmemState
*s
);
713 static void ivshmem_reset(DeviceState
*d
)
715 IVShmemState
*s
= IVSHMEM_COMMON(d
);
717 ivshmem_disable_irqfd(s
);
721 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
722 ivshmem_msix_vector_use(s
);
726 static int ivshmem_setup_interrupts(IVShmemState
*s
, Error
**errp
)
728 /* allocate QEMU callback data for receiving interrupts */
729 s
->msi_vectors
= g_malloc0(s
->vectors
* sizeof(MSIVector
));
731 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
732 if (msix_init_exclusive_bar(PCI_DEVICE(s
), s
->vectors
, 1, errp
)) {
736 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
737 ivshmem_msix_vector_use(s
);
743 static void ivshmem_remove_kvm_msi_virq(IVShmemState
*s
, int vector
)
745 IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector
);
747 if (s
->msi_vectors
[vector
].pdev
== NULL
) {
751 /* it was cleaned when masked in the frontend. */
752 kvm_irqchip_release_virq(kvm_state
, s
->msi_vectors
[vector
].virq
);
754 s
->msi_vectors
[vector
].pdev
= NULL
;
757 static void ivshmem_enable_irqfd(IVShmemState
*s
)
759 PCIDevice
*pdev
= PCI_DEVICE(s
);
762 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
765 ivshmem_add_kvm_msi_virq(s
, i
, &err
);
767 error_report_err(err
);
772 if (msix_set_vector_notifiers(pdev
,
773 ivshmem_vector_unmask
,
775 ivshmem_vector_poll
)) {
776 error_report("ivshmem: msix_set_vector_notifiers failed");
783 ivshmem_remove_kvm_msi_virq(s
, i
);
787 static void ivshmem_disable_irqfd(IVShmemState
*s
)
789 PCIDevice
*pdev
= PCI_DEVICE(s
);
792 if (!pdev
->msix_vector_use_notifier
) {
796 msix_unset_vector_notifiers(pdev
);
798 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
800 * MSI-X is already disabled here so msix_unset_vector_notifiers()
801 * didn't call our release notifier. Do it now to keep our masks and
804 if (s
->msi_vectors
[i
].unmasked
) {
805 ivshmem_vector_mask(pdev
, i
);
807 ivshmem_remove_kvm_msi_virq(s
, i
);
812 static void ivshmem_write_config(PCIDevice
*pdev
, uint32_t address
,
813 uint32_t val
, int len
)
815 IVShmemState
*s
= IVSHMEM_COMMON(pdev
);
816 int is_enabled
, was_enabled
= msix_enabled(pdev
);
818 pci_default_write_config(pdev
, address
, val
, len
);
819 is_enabled
= msix_enabled(pdev
);
821 if (kvm_msi_via_irqfd_enabled()) {
822 if (!was_enabled
&& is_enabled
) {
823 ivshmem_enable_irqfd(s
);
824 } else if (was_enabled
&& !is_enabled
) {
825 ivshmem_disable_irqfd(s
);
830 static void ivshmem_common_realize(PCIDevice
*dev
, Error
**errp
)
832 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
836 /* IRQFD requires MSI */
837 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
) &&
838 !ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
839 error_setg(errp
, "ioeventfd/irqfd requires MSI");
843 pci_conf
= dev
->config
;
844 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
846 memory_region_init_io(&s
->ivshmem_mmio
, OBJECT(s
), &ivshmem_mmio_ops
, s
,
847 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
849 /* region for registers*/
850 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
853 if (s
->hostmem
!= NULL
) {
854 IVSHMEM_DPRINTF("using hostmem\n");
856 s
->ivshmem_bar2
= host_memory_backend_get_memory(s
->hostmem
);
857 host_memory_backend_set_mapped(s
->hostmem
, true);
859 Chardev
*chr
= qemu_chr_fe_get_driver(&s
->server_chr
);
862 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
865 /* we allocate enough space for 16 peers and grow as needed */
869 * Receive setup messages from server synchronously.
870 * Older versions did it asynchronously, but that creates a
871 * number of entertaining race conditions.
873 ivshmem_recv_setup(s
, &err
);
875 error_propagate(errp
, err
);
879 if (s
->master
== ON_OFF_AUTO_ON
&& s
->vm_id
!= 0) {
881 "master must connect to the server before any peers");
885 qemu_chr_fe_set_handlers(&s
->server_chr
, ivshmem_can_receive
,
886 ivshmem_read
, NULL
, NULL
, s
, NULL
, true);
888 if (ivshmem_setup_interrupts(s
, errp
) < 0) {
889 error_prepend(errp
, "Failed to initialize interrupts: ");
894 if (s
->master
== ON_OFF_AUTO_AUTO
) {
895 s
->master
= s
->vm_id
== 0 ? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
898 if (!ivshmem_is_master(s
)) {
899 error_setg(&s
->migration_blocker
,
900 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
901 if (migrate_add_blocker(s
->migration_blocker
, errp
) < 0) {
902 error_free(s
->migration_blocker
);
907 vmstate_register_ram(s
->ivshmem_bar2
, DEVICE(s
));
908 pci_register_bar(PCI_DEVICE(s
), 2,
909 PCI_BASE_ADDRESS_SPACE_MEMORY
|
910 PCI_BASE_ADDRESS_MEM_PREFETCH
|
911 PCI_BASE_ADDRESS_MEM_TYPE_64
,
915 static void ivshmem_exit(PCIDevice
*dev
)
917 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
920 if (s
->migration_blocker
) {
921 migrate_del_blocker(s
->migration_blocker
);
922 error_free(s
->migration_blocker
);
925 if (memory_region_is_mapped(s
->ivshmem_bar2
)) {
927 void *addr
= memory_region_get_ram_ptr(s
->ivshmem_bar2
);
930 if (munmap(addr
, memory_region_size(s
->ivshmem_bar2
) == -1)) {
931 error_report("Failed to munmap shared memory %s",
935 fd
= memory_region_get_fd(s
->ivshmem_bar2
);
939 vmstate_unregister_ram(s
->ivshmem_bar2
, DEVICE(dev
));
943 host_memory_backend_set_mapped(s
->hostmem
, false);
947 for (i
= 0; i
< s
->nb_peers
; i
++) {
948 close_peer_eventfds(s
, i
);
953 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
954 msix_uninit_exclusive_bar(dev
);
957 g_free(s
->msi_vectors
);
960 static int ivshmem_pre_load(void *opaque
)
962 IVShmemState
*s
= opaque
;
964 if (!ivshmem_is_master(s
)) {
965 error_report("'peer' devices are not migratable");
972 static int ivshmem_post_load(void *opaque
, int version_id
)
974 IVShmemState
*s
= opaque
;
976 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
977 ivshmem_msix_vector_use(s
);
982 static void ivshmem_common_class_init(ObjectClass
*klass
, void *data
)
984 DeviceClass
*dc
= DEVICE_CLASS(klass
);
985 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
987 k
->realize
= ivshmem_common_realize
;
988 k
->exit
= ivshmem_exit
;
989 k
->config_write
= ivshmem_write_config
;
990 k
->vendor_id
= PCI_VENDOR_ID_IVSHMEM
;
991 k
->device_id
= PCI_DEVICE_ID_IVSHMEM
;
992 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
994 dc
->reset
= ivshmem_reset
;
995 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
996 dc
->desc
= "Inter-VM shared memory";
999 static const TypeInfo ivshmem_common_info
= {
1000 .name
= TYPE_IVSHMEM_COMMON
,
1001 .parent
= TYPE_PCI_DEVICE
,
1002 .instance_size
= sizeof(IVShmemState
),
1004 .class_init
= ivshmem_common_class_init
,
1005 .interfaces
= (InterfaceInfo
[]) {
1006 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1011 static const VMStateDescription ivshmem_plain_vmsd
= {
1012 .name
= TYPE_IVSHMEM_PLAIN
,
1014 .minimum_version_id
= 0,
1015 .pre_load
= ivshmem_pre_load
,
1016 .post_load
= ivshmem_post_load
,
1017 .fields
= (VMStateField
[]) {
1018 VMSTATE_PCI_DEVICE(parent_obj
, IVShmemState
),
1019 VMSTATE_UINT32(intrstatus
, IVShmemState
),
1020 VMSTATE_UINT32(intrmask
, IVShmemState
),
1021 VMSTATE_END_OF_LIST()
1025 static Property ivshmem_plain_properties
[] = {
1026 DEFINE_PROP_ON_OFF_AUTO("master", IVShmemState
, master
, ON_OFF_AUTO_OFF
),
1027 DEFINE_PROP_LINK("memdev", IVShmemState
, hostmem
, TYPE_MEMORY_BACKEND
,
1028 HostMemoryBackend
*),
1029 DEFINE_PROP_END_OF_LIST(),
1032 static void ivshmem_plain_realize(PCIDevice
*dev
, Error
**errp
)
1034 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
1037 error_setg(errp
, "You must specify a 'memdev'");
1039 } else if (host_memory_backend_is_mapped(s
->hostmem
)) {
1040 error_setg(errp
, "can't use already busy memdev: %s",
1041 object_get_canonical_path_component(OBJECT(s
->hostmem
)));
1045 ivshmem_common_realize(dev
, errp
);
1048 static void ivshmem_plain_class_init(ObjectClass
*klass
, void *data
)
1050 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1051 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1053 k
->realize
= ivshmem_plain_realize
;
1054 device_class_set_props(dc
, ivshmem_plain_properties
);
1055 dc
->vmsd
= &ivshmem_plain_vmsd
;
1058 static const TypeInfo ivshmem_plain_info
= {
1059 .name
= TYPE_IVSHMEM_PLAIN
,
1060 .parent
= TYPE_IVSHMEM_COMMON
,
1061 .instance_size
= sizeof(IVShmemState
),
1062 .class_init
= ivshmem_plain_class_init
,
1065 static const VMStateDescription ivshmem_doorbell_vmsd
= {
1066 .name
= TYPE_IVSHMEM_DOORBELL
,
1068 .minimum_version_id
= 0,
1069 .pre_load
= ivshmem_pre_load
,
1070 .post_load
= ivshmem_post_load
,
1071 .fields
= (VMStateField
[]) {
1072 VMSTATE_PCI_DEVICE(parent_obj
, IVShmemState
),
1073 VMSTATE_MSIX(parent_obj
, IVShmemState
),
1074 VMSTATE_UINT32(intrstatus
, IVShmemState
),
1075 VMSTATE_UINT32(intrmask
, IVShmemState
),
1076 VMSTATE_END_OF_LIST()
1080 static Property ivshmem_doorbell_properties
[] = {
1081 DEFINE_PROP_CHR("chardev", IVShmemState
, server_chr
),
1082 DEFINE_PROP_UINT32("vectors", IVShmemState
, vectors
, 1),
1083 DEFINE_PROP_BIT("ioeventfd", IVShmemState
, features
, IVSHMEM_IOEVENTFD
,
1085 DEFINE_PROP_ON_OFF_AUTO("master", IVShmemState
, master
, ON_OFF_AUTO_OFF
),
1086 DEFINE_PROP_END_OF_LIST(),
1089 static void ivshmem_doorbell_init(Object
*obj
)
1091 IVShmemState
*s
= IVSHMEM_DOORBELL(obj
);
1093 s
->features
|= (1 << IVSHMEM_MSI
);
1096 static void ivshmem_doorbell_realize(PCIDevice
*dev
, Error
**errp
)
1098 IVShmemState
*s
= IVSHMEM_COMMON(dev
);
1100 if (!qemu_chr_fe_backend_connected(&s
->server_chr
)) {
1101 error_setg(errp
, "You must specify a 'chardev'");
1105 ivshmem_common_realize(dev
, errp
);
1108 static void ivshmem_doorbell_class_init(ObjectClass
*klass
, void *data
)
1110 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1111 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1113 k
->realize
= ivshmem_doorbell_realize
;
1114 device_class_set_props(dc
, ivshmem_doorbell_properties
);
1115 dc
->vmsd
= &ivshmem_doorbell_vmsd
;
1118 static const TypeInfo ivshmem_doorbell_info
= {
1119 .name
= TYPE_IVSHMEM_DOORBELL
,
1120 .parent
= TYPE_IVSHMEM_COMMON
,
1121 .instance_size
= sizeof(IVShmemState
),
1122 .instance_init
= ivshmem_doorbell_init
,
1123 .class_init
= ivshmem_doorbell_class_init
,
1126 static void ivshmem_register_types(void)
1128 type_register_static(&ivshmem_common_info
);
1129 type_register_static(&ivshmem_plain_info
);
1130 type_register_static(&ivshmem_doorbell_info
);
1133 type_init(ivshmem_register_types
)