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 "hw/i386/pc.h"
21 #include "hw/pci/pci.h"
22 #include "hw/pci/msi.h"
23 #include "hw/pci/msix.h"
24 #include "sysemu/kvm.h"
25 #include "migration/migration.h"
26 #include "qemu/error-report.h"
27 #include "qemu/event_notifier.h"
28 #include "qemu/fifo8.h"
29 #include "sysemu/char.h"
30 #include "sysemu/hostmem.h"
31 #include "qapi/visitor.h"
33 #include "hw/misc/ivshmem.h"
36 #include <sys/types.h>
39 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
40 #define PCI_DEVICE_ID_IVSHMEM 0x1110
42 #define IVSHMEM_MAX_PEERS G_MAXUINT16
43 #define IVSHMEM_IOEVENTFD 0
46 #define IVSHMEM_PEER 0
47 #define IVSHMEM_MASTER 1
49 #define IVSHMEM_REG_BAR_SIZE 0x100
51 //#define DEBUG_IVSHMEM
53 #define IVSHMEM_DPRINTF(fmt, ...) \
54 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
56 #define IVSHMEM_DPRINTF(fmt, ...)
59 #define TYPE_IVSHMEM "ivshmem"
60 #define IVSHMEM(obj) \
61 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
65 EventNotifier
*eventfds
;
68 typedef struct MSIVector
{
73 typedef struct IVShmemState
{
78 HostMemoryBackend
*hostmem
;
82 CharDriverState
**eventfd_chr
;
83 CharDriverState
*server_chr
;
85 MemoryRegion ivshmem_mmio
;
87 /* We might need to register the BAR before we actually have the memory.
88 * So prepare a container MemoryRegion for the BAR immediately and
89 * add a subregion when we have the memory.
93 uint64_t ivshmem_size
; /* size of shared memory region */
94 uint32_t ivshmem_64bit
;
97 int nb_peers
; /* how many peers we have space for */
102 MSIVector
*msi_vectors
;
104 Error
*migration_blocker
;
109 int role_val
; /* scalar to avoid multiple string comparisons */
112 /* registers for the Inter-VM shared memory device */
113 enum ivshmem_registers
{
120 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
121 unsigned int feature
) {
122 return (ivs
->features
& (1 << feature
));
125 /* accessing registers - based on rtl8139 */
126 static void ivshmem_update_irq(IVShmemState
*s
)
128 PCIDevice
*d
= PCI_DEVICE(s
);
130 isr
= (s
->intrstatus
& s
->intrmask
) & 0xffffffff;
132 /* don't print ISR resets */
134 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
135 isr
? 1 : 0, s
->intrstatus
, s
->intrmask
);
138 pci_set_irq(d
, (isr
!= 0));
141 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
143 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
147 ivshmem_update_irq(s
);
150 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
152 uint32_t ret
= s
->intrmask
;
154 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret
);
159 static void ivshmem_IntrStatus_write(IVShmemState
*s
, uint32_t val
)
161 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val
);
165 ivshmem_update_irq(s
);
168 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
170 uint32_t ret
= s
->intrstatus
;
172 /* reading ISR clears all interrupts */
175 ivshmem_update_irq(s
);
180 static void ivshmem_io_write(void *opaque
, hwaddr addr
,
181 uint64_t val
, unsigned size
)
183 IVShmemState
*s
= opaque
;
185 uint16_t dest
= val
>> 16;
186 uint16_t vector
= val
& 0xff;
190 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
194 ivshmem_IntrMask_write(s
, val
);
198 ivshmem_IntrStatus_write(s
, val
);
202 /* check that dest VM ID is reasonable */
203 if (dest
>= s
->nb_peers
) {
204 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
208 /* check doorbell range */
209 if (vector
< s
->peers
[dest
].nb_eventfds
) {
210 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
211 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
213 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
218 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx
"\n", addr
);
222 static uint64_t ivshmem_io_read(void *opaque
, hwaddr addr
,
226 IVShmemState
*s
= opaque
;
232 ret
= ivshmem_IntrMask_read(s
);
236 ret
= ivshmem_IntrStatus_read(s
);
240 /* return my VM ID if the memory is mapped */
241 if (memory_region_is_mapped(&s
->ivshmem
)) {
249 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
256 static const MemoryRegionOps ivshmem_mmio_ops
= {
257 .read
= ivshmem_io_read
,
258 .write
= ivshmem_io_write
,
259 .endianness
= DEVICE_NATIVE_ENDIAN
,
261 .min_access_size
= 4,
262 .max_access_size
= 4,
266 static void ivshmem_receive(void *opaque
, const uint8_t *buf
, int size
)
268 IVShmemState
*s
= opaque
;
270 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x size: %d\n", *buf
, size
);
272 ivshmem_IntrStatus_write(s
, *buf
);
275 static int ivshmem_can_receive(void * opaque
)
277 return sizeof(int64_t);
280 static void ivshmem_event(void *opaque
, int event
)
282 IVSHMEM_DPRINTF("ivshmem_event %d\n", event
);
285 static void fake_irqfd(void *opaque
, const uint8_t *buf
, int size
) {
287 MSIVector
*entry
= opaque
;
288 PCIDevice
*pdev
= entry
->pdev
;
289 IVShmemState
*s
= IVSHMEM(pdev
);
290 int vector
= entry
- s
->msi_vectors
;
292 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, vector
);
293 msix_notify(pdev
, vector
);
296 static int ivshmem_vector_unmask(PCIDevice
*dev
, unsigned vector
,
299 IVShmemState
*s
= IVSHMEM(dev
);
300 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
301 MSIVector
*v
= &s
->msi_vectors
[vector
];
304 IVSHMEM_DPRINTF("vector unmask %p %d\n", dev
, vector
);
306 ret
= kvm_irqchip_update_msi_route(kvm_state
, v
->virq
, msg
, dev
);
311 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, v
->virq
);
314 static void ivshmem_vector_mask(PCIDevice
*dev
, unsigned vector
)
316 IVShmemState
*s
= IVSHMEM(dev
);
317 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
320 IVSHMEM_DPRINTF("vector mask %p %d\n", dev
, vector
);
322 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
,
323 s
->msi_vectors
[vector
].virq
);
325 error_report("remove_irqfd_notifier_gsi failed");
329 static void ivshmem_vector_poll(PCIDevice
*dev
,
330 unsigned int vector_start
,
331 unsigned int vector_end
)
333 IVShmemState
*s
= IVSHMEM(dev
);
336 IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev
, vector_start
, vector_end
);
338 vector_end
= MIN(vector_end
, s
->vectors
);
340 for (vector
= vector_start
; vector
< vector_end
; vector
++) {
341 EventNotifier
*notifier
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
343 if (!msix_is_masked(dev
, vector
)) {
347 if (event_notifier_test_and_clear(notifier
)) {
348 msix_set_pending(dev
, vector
);
353 static CharDriverState
* create_eventfd_chr_device(void * opaque
, EventNotifier
*n
,
356 /* create a event character device based on the passed eventfd */
357 IVShmemState
*s
= opaque
;
358 PCIDevice
*pdev
= PCI_DEVICE(s
);
359 int eventfd
= event_notifier_get_fd(n
);
360 CharDriverState
*chr
;
362 s
->msi_vectors
[vector
].pdev
= pdev
;
364 chr
= qemu_chr_open_eventfd(eventfd
);
367 error_report("creating chardriver for eventfd %d failed", eventfd
);
370 qemu_chr_fe_claim_no_fail(chr
);
372 /* if MSI is supported we need multiple interrupts */
373 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
374 s
->msi_vectors
[vector
].pdev
= PCI_DEVICE(s
);
376 qemu_chr_add_handlers(chr
, ivshmem_can_receive
, fake_irqfd
,
377 ivshmem_event
, &s
->msi_vectors
[vector
]);
379 qemu_chr_add_handlers(chr
, ivshmem_can_receive
, ivshmem_receive
,
387 static int check_shm_size(IVShmemState
*s
, int fd
, Error
**errp
)
389 /* check that the guest isn't going to try and map more memory than the
390 * the object has allocated return -1 to indicate error */
394 if (fstat(fd
, &buf
) < 0) {
395 error_setg(errp
, "exiting: fstat on fd %d failed: %s",
396 fd
, strerror(errno
));
400 if (s
->ivshmem_size
> buf
.st_size
) {
401 error_setg(errp
, "Requested memory size greater"
402 " than shared object size (%" PRIu64
" > %" PRIu64
")",
403 s
->ivshmem_size
, (uint64_t)buf
.st_size
);
410 /* create the shared memory BAR when we are not using the server, so we can
411 * create the BAR and map the memory immediately */
412 static int create_shared_memory_BAR(IVShmemState
*s
, int fd
, uint8_t attr
,
417 ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
418 if (ptr
== MAP_FAILED
) {
419 error_setg_errno(errp
, errno
, "Failed to mmap shared memory");
423 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
), "ivshmem.bar2",
424 s
->ivshmem_size
, ptr
);
425 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
426 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
428 /* region for shared memory */
429 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
434 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
436 memory_region_add_eventfd(&s
->ivshmem_mmio
,
441 &s
->peers
[posn
].eventfds
[i
]);
444 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
446 memory_region_del_eventfd(&s
->ivshmem_mmio
,
451 &s
->peers
[posn
].eventfds
[i
]);
454 static void close_peer_eventfds(IVShmemState
*s
, int posn
)
458 if (!ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
461 if (posn
< 0 || posn
>= s
->nb_peers
) {
462 error_report("invalid peer %d", posn
);
466 n
= s
->peers
[posn
].nb_eventfds
;
468 memory_region_transaction_begin();
469 for (i
= 0; i
< n
; i
++) {
470 ivshmem_del_eventfd(s
, posn
, i
);
472 memory_region_transaction_commit();
473 for (i
= 0; i
< n
; i
++) {
474 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
477 g_free(s
->peers
[posn
].eventfds
);
478 s
->peers
[posn
].nb_eventfds
= 0;
481 /* this function increase the dynamic storage need to store data about other
483 static int resize_peers(IVShmemState
*s
, int new_min_size
)
488 /* limit number of max peers */
489 if (new_min_size
<= 0 || new_min_size
> IVSHMEM_MAX_PEERS
) {
492 if (new_min_size
<= s
->nb_peers
) {
496 old_size
= s
->nb_peers
;
497 s
->nb_peers
= new_min_size
;
499 IVSHMEM_DPRINTF("bumping storage to %d peers\n", s
->nb_peers
);
501 s
->peers
= g_realloc(s
->peers
, s
->nb_peers
* sizeof(Peer
));
503 for (j
= old_size
; j
< s
->nb_peers
; j
++) {
504 s
->peers
[j
].eventfds
= g_new0(EventNotifier
, s
->vectors
);
505 s
->peers
[j
].nb_eventfds
= 0;
511 static bool fifo_update_and_get(IVShmemState
*s
, const uint8_t *buf
, int size
,
512 void *data
, size_t len
)
517 assert(len
<= sizeof(int64_t)); /* limitation of the fifo */
518 if (fifo8_is_empty(&s
->incoming_fifo
) && size
== len
) {
519 memcpy(data
, buf
, size
);
523 IVSHMEM_DPRINTF("short read of %d bytes\n", size
);
525 num
= MIN(size
, sizeof(int64_t) - fifo8_num_used(&s
->incoming_fifo
));
526 fifo8_push_all(&s
->incoming_fifo
, buf
, num
);
528 if (fifo8_num_used(&s
->incoming_fifo
) < len
) {
535 p
= fifo8_pop_buf(&s
->incoming_fifo
, len
, &num
);
538 memcpy(data
, p
, len
);
541 fifo8_push_all(&s
->incoming_fifo
, buf
, size
);
547 static bool fifo_update_and_get_i64(IVShmemState
*s
,
548 const uint8_t *buf
, int size
, int64_t *i64
)
550 if (fifo_update_and_get(s
, buf
, size
, i64
, sizeof(*i64
))) {
551 *i64
= GINT64_FROM_LE(*i64
);
558 static int ivshmem_add_kvm_msi_virq(IVShmemState
*s
, int vector
)
560 PCIDevice
*pdev
= PCI_DEVICE(s
);
561 MSIMessage msg
= msix_get_message(pdev
, vector
);
564 IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector
);
566 if (s
->msi_vectors
[vector
].pdev
!= NULL
) {
570 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
, pdev
);
572 error_report("ivshmem: kvm_irqchip_add_msi_route failed");
576 s
->msi_vectors
[vector
].virq
= ret
;
577 s
->msi_vectors
[vector
].pdev
= pdev
;
582 static void setup_interrupt(IVShmemState
*s
, int vector
)
584 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
585 bool with_irqfd
= kvm_msi_via_irqfd_enabled() &&
586 ivshmem_has_feature(s
, IVSHMEM_MSI
);
587 PCIDevice
*pdev
= PCI_DEVICE(s
);
589 IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector
);
592 IVSHMEM_DPRINTF("with eventfd");
593 s
->eventfd_chr
[vector
] = create_eventfd_chr_device(s
, n
, vector
);
594 } else if (msix_enabled(pdev
)) {
595 IVSHMEM_DPRINTF("with irqfd");
596 if (ivshmem_add_kvm_msi_virq(s
, vector
) < 0) {
600 if (!msix_is_masked(pdev
, vector
)) {
601 kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
,
602 s
->msi_vectors
[vector
].virq
);
605 /* it will be delayed until msix is enabled, in write_config */
606 IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled");
610 static void ivshmem_read(void *opaque
, const uint8_t *buf
, int size
)
612 IVShmemState
*s
= opaque
;
615 int64_t incoming_posn
;
619 if (!fifo_update_and_get_i64(s
, buf
, size
, &incoming_posn
)) {
623 if (incoming_posn
< -1) {
624 IVSHMEM_DPRINTF("invalid incoming_posn %" PRId64
"\n", incoming_posn
);
628 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
629 incoming_fd
= qemu_chr_fe_get_msgfd(s
->server_chr
);
630 IVSHMEM_DPRINTF("posn is %" PRId64
", fd is %d\n",
631 incoming_posn
, incoming_fd
);
633 /* make sure we have enough space for this peer */
634 if (incoming_posn
>= s
->nb_peers
) {
635 if (resize_peers(s
, incoming_posn
+ 1) < 0) {
636 error_report("failed to resize peers array");
637 if (incoming_fd
!= -1) {
644 peer
= &s
->peers
[incoming_posn
];
646 if (incoming_fd
== -1) {
647 /* if posn is positive and unseen before then this is our posn*/
648 if (incoming_posn
>= 0 && s
->vm_id
== -1) {
649 /* receive our posn */
650 s
->vm_id
= incoming_posn
;
652 /* otherwise an fd == -1 means an existing peer has gone away */
653 IVSHMEM_DPRINTF("posn %" PRId64
" has gone away\n", incoming_posn
);
654 close_peer_eventfds(s
, incoming_posn
);
659 /* if the position is -1, then it's shared memory region fd */
660 if (incoming_posn
== -1) {
663 if (memory_region_is_mapped(&s
->ivshmem
)) {
664 error_report("shm already initialized");
669 if (check_shm_size(s
, incoming_fd
, &err
) == -1) {
670 error_report_err(err
);
675 /* mmap the region and map into the BAR2 */
676 map_ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
678 if (map_ptr
== MAP_FAILED
) {
679 error_report("Failed to mmap shared memory %s", strerror(errno
));
683 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
),
684 "ivshmem.bar2", s
->ivshmem_size
, map_ptr
);
685 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
687 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64
"\n",
688 map_ptr
, s
->ivshmem_size
);
690 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
696 /* each peer has an associated array of eventfds, and we keep
697 * track of how many eventfds received so far */
698 /* get a new eventfd: */
699 if (peer
->nb_eventfds
>= s
->vectors
) {
700 error_report("Too many eventfd received, device has %d vectors",
706 new_eventfd
= peer
->nb_eventfds
++;
708 /* this is an eventfd for a particular peer VM */
709 IVSHMEM_DPRINTF("eventfds[%" PRId64
"][%d] = %d\n", incoming_posn
,
710 new_eventfd
, incoming_fd
);
711 event_notifier_init_fd(&peer
->eventfds
[new_eventfd
], incoming_fd
);
712 fcntl_setfl(incoming_fd
, O_NONBLOCK
); /* msix/irqfd poll non block */
714 if (incoming_posn
== s
->vm_id
) {
715 setup_interrupt(s
, new_eventfd
);
718 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
719 ivshmem_add_eventfd(s
, incoming_posn
, new_eventfd
);
723 static void ivshmem_check_version(void *opaque
, const uint8_t * buf
, int size
)
725 IVShmemState
*s
= opaque
;
729 if (!fifo_update_and_get_i64(s
, buf
, size
, &version
)) {
733 tmp
= qemu_chr_fe_get_msgfd(s
->server_chr
);
734 if (tmp
!= -1 || version
!= IVSHMEM_PROTOCOL_VERSION
) {
735 fprintf(stderr
, "incompatible version, you are connecting to a ivshmem-"
736 "server using a different protocol please check your setup\n");
737 qemu_chr_delete(s
->server_chr
);
738 s
->server_chr
= NULL
;
742 IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
743 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
, ivshmem_read
,
747 /* Select the MSI-X vectors used by device.
748 * ivshmem maps events to vectors statically, so
749 * we just enable all vectors on init and after reset. */
750 static void ivshmem_use_msix(IVShmemState
* s
)
752 PCIDevice
*d
= PCI_DEVICE(s
);
755 IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__
, msix_present(d
));
756 if (!msix_present(d
)) {
760 for (i
= 0; i
< s
->vectors
; i
++) {
761 msix_vector_use(d
, i
);
765 static void ivshmem_reset(DeviceState
*d
)
767 IVShmemState
*s
= IVSHMEM(d
);
774 static int ivshmem_setup_msi(IVShmemState
* s
)
776 if (msix_init_exclusive_bar(PCI_DEVICE(s
), s
->vectors
, 1)) {
780 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
782 /* allocate QEMU char devices for receiving interrupts */
783 s
->msi_vectors
= g_malloc0(s
->vectors
* sizeof(MSIVector
));
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
++) {
795 ivshmem_add_kvm_msi_virq(s
, i
);
798 if (msix_set_vector_notifiers(pdev
,
799 ivshmem_vector_unmask
,
801 ivshmem_vector_poll
)) {
802 error_report("ivshmem: msix_set_vector_notifiers failed");
806 static void ivshmem_remove_kvm_msi_virq(IVShmemState
*s
, int vector
)
808 IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector
);
810 if (s
->msi_vectors
[vector
].pdev
== NULL
) {
814 /* it was cleaned when masked in the frontend. */
815 kvm_irqchip_release_virq(kvm_state
, s
->msi_vectors
[vector
].virq
);
817 s
->msi_vectors
[vector
].pdev
= NULL
;
820 static void ivshmem_disable_irqfd(IVShmemState
*s
)
822 PCIDevice
*pdev
= PCI_DEVICE(s
);
825 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
826 ivshmem_remove_kvm_msi_virq(s
, i
);
829 msix_unset_vector_notifiers(pdev
);
832 static void ivshmem_write_config(PCIDevice
*pdev
, uint32_t address
,
833 uint32_t val
, int len
)
835 IVShmemState
*s
= IVSHMEM(pdev
);
836 int is_enabled
, was_enabled
= msix_enabled(pdev
);
838 pci_default_write_config(pdev
, address
, val
, len
);
839 is_enabled
= msix_enabled(pdev
);
841 if (kvm_msi_via_irqfd_enabled() && s
->vm_id
!= -1) {
842 if (!was_enabled
&& is_enabled
) {
843 ivshmem_enable_irqfd(s
);
844 } else if (was_enabled
&& !is_enabled
) {
845 ivshmem_disable_irqfd(s
);
850 static void pci_ivshmem_realize(PCIDevice
*dev
, Error
**errp
)
852 IVShmemState
*s
= IVSHMEM(dev
);
854 uint8_t attr
= PCI_BASE_ADDRESS_SPACE_MEMORY
|
855 PCI_BASE_ADDRESS_MEM_PREFETCH
;
857 if (!!s
->server_chr
+ !!s
->shmobj
+ !!s
->hostmem
!= 1) {
859 "You must specify either 'shm', 'chardev' or 'x-memdev'");
867 g_warning("size argument ignored with hostmem");
870 mr
= host_memory_backend_get_memory(s
->hostmem
, errp
);
871 s
->ivshmem_size
= memory_region_size(mr
);
872 } else if (s
->sizearg
== NULL
) {
873 s
->ivshmem_size
= 4 << 20; /* 4 MB default */
876 int64_t size
= qemu_strtosz(s
->sizearg
, &end
);
877 if (size
< 0 || *end
!= '\0' || !is_power_of_2(size
)) {
878 error_setg(errp
, "Invalid size %s", s
->sizearg
);
881 s
->ivshmem_size
= size
;
884 fifo8_create(&s
->incoming_fifo
, sizeof(int64_t));
886 /* IRQFD requires MSI */
887 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
) &&
888 !ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
889 error_setg(errp
, "ioeventfd/irqfd requires MSI");
893 /* check that role is reasonable */
895 if (strncmp(s
->role
, "peer", 5) == 0) {
896 s
->role_val
= IVSHMEM_PEER
;
897 } else if (strncmp(s
->role
, "master", 7) == 0) {
898 s
->role_val
= IVSHMEM_MASTER
;
900 error_setg(errp
, "'role' must be 'peer' or 'master'");
904 s
->role_val
= IVSHMEM_MASTER
; /* default */
907 if (s
->role_val
== IVSHMEM_PEER
) {
908 error_setg(&s
->migration_blocker
,
909 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
910 migrate_add_blocker(s
->migration_blocker
);
913 pci_conf
= dev
->config
;
914 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
916 pci_config_set_interrupt_pin(pci_conf
, 1);
918 memory_region_init_io(&s
->ivshmem_mmio
, OBJECT(s
), &ivshmem_mmio_ops
, s
,
919 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
921 /* region for registers*/
922 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
925 memory_region_init(&s
->bar
, OBJECT(s
), "ivshmem-bar2-container", s
->ivshmem_size
);
926 if (s
->ivshmem_64bit
) {
927 attr
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
930 if (s
->hostmem
!= NULL
) {
933 IVSHMEM_DPRINTF("using hostmem\n");
935 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(s
->hostmem
), errp
);
936 vmstate_register_ram(mr
, DEVICE(s
));
937 memory_region_add_subregion(&s
->bar
, 0, mr
);
938 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
939 } else if (s
->server_chr
!= NULL
) {
940 /* FIXME do not rely on what chr drivers put into filename */
941 if (strncmp(s
->server_chr
->filename
, "unix:", 5)) {
942 error_setg(errp
, "chardev is not a unix client socket");
946 /* if we get a UNIX socket as the parameter we will talk
947 * to the ivshmem server to receive the memory region */
949 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
950 s
->server_chr
->filename
);
952 if (ivshmem_has_feature(s
, IVSHMEM_MSI
) &&
953 ivshmem_setup_msi(s
)) {
954 error_setg(errp
, "msix initialization failed");
958 /* we allocate enough space for 16 peers and grow as needed */
962 pci_register_bar(dev
, 2, attr
, &s
->bar
);
964 s
->eventfd_chr
= g_malloc0(s
->vectors
* sizeof(CharDriverState
*));
966 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
,
967 ivshmem_check_version
, ivshmem_event
, s
);
969 /* just map the file immediately, we're not using a server */
972 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s
->shmobj
);
974 /* try opening with O_EXCL and if it succeeds zero the memory
975 * by truncating to 0 */
976 if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
|O_EXCL
,
977 S_IRWXU
|S_IRWXG
|S_IRWXO
)) > 0) {
978 /* truncate file to length PCI device's memory */
979 if (ftruncate(fd
, s
->ivshmem_size
) != 0) {
980 error_report("could not truncate shared file");
983 } else if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
,
984 S_IRWXU
|S_IRWXG
|S_IRWXO
)) < 0) {
985 error_setg(errp
, "could not open shared file");
989 if (check_shm_size(s
, fd
, errp
) == -1) {
993 create_shared_memory_BAR(s
, fd
, attr
, errp
);
998 static void pci_ivshmem_exit(PCIDevice
*dev
)
1000 IVShmemState
*s
= IVSHMEM(dev
);
1003 fifo8_destroy(&s
->incoming_fifo
);
1005 if (s
->migration_blocker
) {
1006 migrate_del_blocker(s
->migration_blocker
);
1007 error_free(s
->migration_blocker
);
1010 if (memory_region_is_mapped(&s
->ivshmem
)) {
1012 void *addr
= memory_region_get_ram_ptr(&s
->ivshmem
);
1014 if (munmap(addr
, s
->ivshmem_size
) == -1) {
1015 error_report("Failed to munmap shared memory %s",
1020 vmstate_unregister_ram(&s
->ivshmem
, DEVICE(dev
));
1021 memory_region_del_subregion(&s
->bar
, &s
->ivshmem
);
1024 if (s
->eventfd_chr
) {
1025 for (i
= 0; i
< s
->vectors
; i
++) {
1026 if (s
->eventfd_chr
[i
]) {
1027 qemu_chr_free(s
->eventfd_chr
[i
]);
1030 g_free(s
->eventfd_chr
);
1034 for (i
= 0; i
< s
->nb_peers
; i
++) {
1035 close_peer_eventfds(s
, i
);
1040 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1041 msix_uninit_exclusive_bar(dev
);
1044 g_free(s
->msi_vectors
);
1047 static bool test_msix(void *opaque
, int version_id
)
1049 IVShmemState
*s
= opaque
;
1051 return ivshmem_has_feature(s
, IVSHMEM_MSI
);
1054 static bool test_no_msix(void *opaque
, int version_id
)
1056 return !test_msix(opaque
, version_id
);
1059 static int ivshmem_pre_load(void *opaque
)
1061 IVShmemState
*s
= opaque
;
1063 if (s
->role_val
== IVSHMEM_PEER
) {
1064 error_report("'peer' devices are not migratable");
1071 static int ivshmem_post_load(void *opaque
, int version_id
)
1073 IVShmemState
*s
= opaque
;
1075 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1076 ivshmem_use_msix(s
);
1082 static int ivshmem_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
1084 IVShmemState
*s
= opaque
;
1085 PCIDevice
*pdev
= PCI_DEVICE(s
);
1088 IVSHMEM_DPRINTF("ivshmem_load_old\n");
1090 if (version_id
!= 0) {
1094 if (s
->role_val
== IVSHMEM_PEER
) {
1095 error_report("'peer' devices are not migratable");
1099 ret
= pci_device_load(pdev
, f
);
1104 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1106 ivshmem_use_msix(s
);
1108 s
->intrstatus
= qemu_get_be32(f
);
1109 s
->intrmask
= qemu_get_be32(f
);
1115 static const VMStateDescription ivshmem_vmsd
= {
1118 .minimum_version_id
= 1,
1119 .pre_load
= ivshmem_pre_load
,
1120 .post_load
= ivshmem_post_load
,
1121 .fields
= (VMStateField
[]) {
1122 VMSTATE_PCI_DEVICE(parent_obj
, IVShmemState
),
1124 VMSTATE_MSIX_TEST(parent_obj
, IVShmemState
, test_msix
),
1125 VMSTATE_UINT32_TEST(intrstatus
, IVShmemState
, test_no_msix
),
1126 VMSTATE_UINT32_TEST(intrmask
, IVShmemState
, test_no_msix
),
1128 VMSTATE_END_OF_LIST()
1130 .load_state_old
= ivshmem_load_old
,
1131 .minimum_version_id_old
= 0
1134 static Property ivshmem_properties
[] = {
1135 DEFINE_PROP_CHR("chardev", IVShmemState
, server_chr
),
1136 DEFINE_PROP_STRING("size", IVShmemState
, sizearg
),
1137 DEFINE_PROP_UINT32("vectors", IVShmemState
, vectors
, 1),
1138 DEFINE_PROP_BIT("ioeventfd", IVShmemState
, features
, IVSHMEM_IOEVENTFD
, false),
1139 DEFINE_PROP_BIT("msi", IVShmemState
, features
, IVSHMEM_MSI
, true),
1140 DEFINE_PROP_STRING("shm", IVShmemState
, shmobj
),
1141 DEFINE_PROP_STRING("role", IVShmemState
, role
),
1142 DEFINE_PROP_UINT32("use64", IVShmemState
, ivshmem_64bit
, 1),
1143 DEFINE_PROP_END_OF_LIST(),
1146 static void ivshmem_class_init(ObjectClass
*klass
, void *data
)
1148 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1149 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1151 k
->realize
= pci_ivshmem_realize
;
1152 k
->exit
= pci_ivshmem_exit
;
1153 k
->config_write
= ivshmem_write_config
;
1154 k
->vendor_id
= PCI_VENDOR_ID_IVSHMEM
;
1155 k
->device_id
= PCI_DEVICE_ID_IVSHMEM
;
1156 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
1157 dc
->reset
= ivshmem_reset
;
1158 dc
->props
= ivshmem_properties
;
1159 dc
->vmsd
= &ivshmem_vmsd
;
1160 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1161 dc
->desc
= "Inter-VM shared memory";
1164 static void ivshmem_check_memdev_is_busy(Object
*obj
, const char *name
,
1165 Object
*val
, Error
**errp
)
1169 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(val
), errp
);
1170 if (memory_region_is_mapped(mr
)) {
1171 char *path
= object_get_canonical_path_component(val
);
1172 error_setg(errp
, "can't use already busy memdev: %s", path
);
1175 qdev_prop_allow_set_link_before_realize(obj
, name
, val
, errp
);
1179 static void ivshmem_init(Object
*obj
)
1181 IVShmemState
*s
= IVSHMEM(obj
);
1183 object_property_add_link(obj
, "x-memdev", TYPE_MEMORY_BACKEND
,
1184 (Object
**)&s
->hostmem
,
1185 ivshmem_check_memdev_is_busy
,
1186 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1190 static const TypeInfo ivshmem_info
= {
1191 .name
= TYPE_IVSHMEM
,
1192 .parent
= TYPE_PCI_DEVICE
,
1193 .instance_size
= sizeof(IVShmemState
),
1194 .instance_init
= ivshmem_init
,
1195 .class_init
= ivshmem_class_init
,
1198 static void ivshmem_register_types(void)
1200 type_register_static(&ivshmem_info
);
1203 type_init(ivshmem_register_types
)