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 "qapi/error.h"
21 #include "qemu/cutils.h"
23 #include "hw/i386/pc.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/migration.h"
29 #include "qemu/error-report.h"
30 #include "qemu/event_notifier.h"
31 #include "qemu/fifo8.h"
32 #include "sysemu/char.h"
33 #include "sysemu/hostmem.h"
34 #include "qapi/visitor.h"
35 #include "exec/ram_addr.h"
37 #include "hw/misc/ivshmem.h"
41 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
42 #define PCI_DEVICE_ID_IVSHMEM 0x1110
44 #define IVSHMEM_MAX_PEERS G_MAXUINT16
45 #define IVSHMEM_IOEVENTFD 0
48 #define IVSHMEM_PEER 0
49 #define IVSHMEM_MASTER 1
51 #define IVSHMEM_REG_BAR_SIZE 0x100
53 //#define DEBUG_IVSHMEM
55 #define IVSHMEM_DPRINTF(fmt, ...) \
56 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
58 #define IVSHMEM_DPRINTF(fmt, ...)
61 #define TYPE_IVSHMEM "ivshmem"
62 #define IVSHMEM(obj) \
63 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
67 EventNotifier
*eventfds
;
70 typedef struct MSIVector
{
75 typedef struct IVShmemState
{
80 HostMemoryBackend
*hostmem
;
84 CharDriverState
**eventfd_chr
;
85 CharDriverState
*server_chr
;
87 MemoryRegion ivshmem_mmio
;
89 /* We might need to register the BAR before we actually have the memory.
90 * So prepare a container MemoryRegion for the BAR immediately and
91 * add a subregion when we have the memory.
95 uint64_t ivshmem_size
; /* size of shared memory region */
96 uint32_t ivshmem_64bit
;
99 int nb_peers
; /* how many peers we have space for */
104 MSIVector
*msi_vectors
;
106 Error
*migration_blocker
;
111 int role_val
; /* scalar to avoid multiple string comparisons */
114 /* registers for the Inter-VM shared memory device */
115 enum ivshmem_registers
{
122 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
123 unsigned int feature
) {
124 return (ivs
->features
& (1 << feature
));
127 /* accessing registers - based on rtl8139 */
128 static void ivshmem_update_irq(IVShmemState
*s
)
130 PCIDevice
*d
= PCI_DEVICE(s
);
132 isr
= (s
->intrstatus
& s
->intrmask
) & 0xffffffff;
134 /* don't print ISR resets */
136 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
137 isr
? 1 : 0, s
->intrstatus
, s
->intrmask
);
140 pci_set_irq(d
, (isr
!= 0));
143 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
145 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
149 ivshmem_update_irq(s
);
152 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
154 uint32_t ret
= s
->intrmask
;
156 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret
);
161 static void ivshmem_IntrStatus_write(IVShmemState
*s
, uint32_t val
)
163 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val
);
167 ivshmem_update_irq(s
);
170 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
172 uint32_t ret
= s
->intrstatus
;
174 /* reading ISR clears all interrupts */
177 ivshmem_update_irq(s
);
182 static void ivshmem_io_write(void *opaque
, hwaddr addr
,
183 uint64_t val
, unsigned size
)
185 IVShmemState
*s
= opaque
;
187 uint16_t dest
= val
>> 16;
188 uint16_t vector
= val
& 0xff;
192 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
196 ivshmem_IntrMask_write(s
, val
);
200 ivshmem_IntrStatus_write(s
, val
);
204 /* check that dest VM ID is reasonable */
205 if (dest
>= s
->nb_peers
) {
206 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
210 /* check doorbell range */
211 if (vector
< s
->peers
[dest
].nb_eventfds
) {
212 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
213 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
215 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
220 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx
"\n", addr
);
224 static uint64_t ivshmem_io_read(void *opaque
, hwaddr addr
,
228 IVShmemState
*s
= opaque
;
234 ret
= ivshmem_IntrMask_read(s
);
238 ret
= ivshmem_IntrStatus_read(s
);
242 /* return my VM ID if the memory is mapped */
243 if (memory_region_is_mapped(&s
->ivshmem
)) {
251 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
258 static const MemoryRegionOps ivshmem_mmio_ops
= {
259 .read
= ivshmem_io_read
,
260 .write
= ivshmem_io_write
,
261 .endianness
= DEVICE_NATIVE_ENDIAN
,
263 .min_access_size
= 4,
264 .max_access_size
= 4,
268 static int ivshmem_can_receive(void * opaque
)
270 return sizeof(int64_t);
273 static void ivshmem_event(void *opaque
, int event
)
275 IVSHMEM_DPRINTF("ivshmem_event %d\n", event
);
278 static void ivshmem_vector_notify(void *opaque
)
280 MSIVector
*entry
= opaque
;
281 PCIDevice
*pdev
= entry
->pdev
;
282 IVShmemState
*s
= IVSHMEM(pdev
);
283 int vector
= entry
- s
->msi_vectors
;
284 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
286 if (!event_notifier_test_and_clear(n
)) {
290 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, vector
);
291 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
292 msix_notify(pdev
, vector
);
294 ivshmem_IntrStatus_write(s
, 1);
298 static int ivshmem_vector_unmask(PCIDevice
*dev
, unsigned vector
,
301 IVShmemState
*s
= IVSHMEM(dev
);
302 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
303 MSIVector
*v
= &s
->msi_vectors
[vector
];
306 IVSHMEM_DPRINTF("vector unmask %p %d\n", dev
, vector
);
308 ret
= kvm_irqchip_update_msi_route(kvm_state
, v
->virq
, msg
, dev
);
313 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, v
->virq
);
316 static void ivshmem_vector_mask(PCIDevice
*dev
, unsigned vector
)
318 IVShmemState
*s
= IVSHMEM(dev
);
319 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
322 IVSHMEM_DPRINTF("vector mask %p %d\n", dev
, vector
);
324 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
,
325 s
->msi_vectors
[vector
].virq
);
327 error_report("remove_irqfd_notifier_gsi failed");
331 static void ivshmem_vector_poll(PCIDevice
*dev
,
332 unsigned int vector_start
,
333 unsigned int vector_end
)
335 IVShmemState
*s
= IVSHMEM(dev
);
338 IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev
, vector_start
, vector_end
);
340 vector_end
= MIN(vector_end
, s
->vectors
);
342 for (vector
= vector_start
; vector
< vector_end
; vector
++) {
343 EventNotifier
*notifier
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
345 if (!msix_is_masked(dev
, vector
)) {
349 if (event_notifier_test_and_clear(notifier
)) {
350 msix_set_pending(dev
, vector
);
355 static void watch_vector_notifier(IVShmemState
*s
, EventNotifier
*n
,
358 int eventfd
= event_notifier_get_fd(n
);
360 /* if MSI is supported we need multiple interrupts */
361 s
->msi_vectors
[vector
].pdev
= PCI_DEVICE(s
);
363 qemu_set_fd_handler(eventfd
, ivshmem_vector_notify
,
364 NULL
, &s
->msi_vectors
[vector
]);
367 static int check_shm_size(IVShmemState
*s
, int fd
, Error
**errp
)
369 /* check that the guest isn't going to try and map more memory than the
370 * the object has allocated return -1 to indicate error */
374 if (fstat(fd
, &buf
) < 0) {
375 error_setg(errp
, "exiting: fstat on fd %d failed: %s",
376 fd
, strerror(errno
));
380 if (s
->ivshmem_size
> buf
.st_size
) {
381 error_setg(errp
, "Requested memory size greater"
382 " than shared object size (%" PRIu64
" > %" PRIu64
")",
383 s
->ivshmem_size
, (uint64_t)buf
.st_size
);
390 /* create the shared memory BAR when we are not using the server, so we can
391 * create the BAR and map the memory immediately */
392 static int create_shared_memory_BAR(IVShmemState
*s
, int fd
, uint8_t attr
,
397 ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
398 if (ptr
== MAP_FAILED
) {
399 error_setg_errno(errp
, errno
, "Failed to mmap shared memory");
403 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
), "ivshmem.bar2",
404 s
->ivshmem_size
, ptr
);
405 qemu_set_ram_fd(memory_region_get_ram_addr(&s
->ivshmem
), fd
);
406 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
407 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
409 /* region for shared memory */
410 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
415 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
417 memory_region_add_eventfd(&s
->ivshmem_mmio
,
422 &s
->peers
[posn
].eventfds
[i
]);
425 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
427 memory_region_del_eventfd(&s
->ivshmem_mmio
,
432 &s
->peers
[posn
].eventfds
[i
]);
435 static void close_peer_eventfds(IVShmemState
*s
, int posn
)
439 if (!ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
442 if (posn
< 0 || posn
>= s
->nb_peers
) {
443 error_report("invalid peer %d", posn
);
447 n
= s
->peers
[posn
].nb_eventfds
;
449 memory_region_transaction_begin();
450 for (i
= 0; i
< n
; i
++) {
451 ivshmem_del_eventfd(s
, posn
, i
);
453 memory_region_transaction_commit();
454 for (i
= 0; i
< n
; i
++) {
455 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
458 g_free(s
->peers
[posn
].eventfds
);
459 s
->peers
[posn
].nb_eventfds
= 0;
462 /* this function increase the dynamic storage need to store data about other
464 static int resize_peers(IVShmemState
*s
, int new_min_size
)
469 /* limit number of max peers */
470 if (new_min_size
<= 0 || new_min_size
> IVSHMEM_MAX_PEERS
) {
473 if (new_min_size
<= s
->nb_peers
) {
477 old_size
= s
->nb_peers
;
478 s
->nb_peers
= new_min_size
;
480 IVSHMEM_DPRINTF("bumping storage to %d peers\n", s
->nb_peers
);
482 s
->peers
= g_realloc(s
->peers
, s
->nb_peers
* sizeof(Peer
));
484 for (j
= old_size
; j
< s
->nb_peers
; j
++) {
485 s
->peers
[j
].eventfds
= g_new0(EventNotifier
, s
->vectors
);
486 s
->peers
[j
].nb_eventfds
= 0;
492 static bool fifo_update_and_get(IVShmemState
*s
, const uint8_t *buf
, int size
,
493 void *data
, size_t len
)
498 assert(len
<= sizeof(int64_t)); /* limitation of the fifo */
499 if (fifo8_is_empty(&s
->incoming_fifo
) && size
== len
) {
500 memcpy(data
, buf
, size
);
504 IVSHMEM_DPRINTF("short read of %d bytes\n", size
);
506 num
= MIN(size
, sizeof(int64_t) - fifo8_num_used(&s
->incoming_fifo
));
507 fifo8_push_all(&s
->incoming_fifo
, buf
, num
);
509 if (fifo8_num_used(&s
->incoming_fifo
) < len
) {
516 p
= fifo8_pop_buf(&s
->incoming_fifo
, len
, &num
);
519 memcpy(data
, p
, len
);
522 fifo8_push_all(&s
->incoming_fifo
, buf
, size
);
528 static bool fifo_update_and_get_i64(IVShmemState
*s
,
529 const uint8_t *buf
, int size
, int64_t *i64
)
531 if (fifo_update_and_get(s
, buf
, size
, i64
, sizeof(*i64
))) {
532 *i64
= GINT64_FROM_LE(*i64
);
539 static int ivshmem_add_kvm_msi_virq(IVShmemState
*s
, int vector
)
541 PCIDevice
*pdev
= PCI_DEVICE(s
);
542 MSIMessage msg
= msix_get_message(pdev
, vector
);
545 IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector
);
547 if (s
->msi_vectors
[vector
].pdev
!= NULL
) {
551 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
, pdev
);
553 error_report("ivshmem: kvm_irqchip_add_msi_route failed");
557 s
->msi_vectors
[vector
].virq
= ret
;
558 s
->msi_vectors
[vector
].pdev
= pdev
;
563 static void setup_interrupt(IVShmemState
*s
, int vector
)
565 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
566 bool with_irqfd
= kvm_msi_via_irqfd_enabled() &&
567 ivshmem_has_feature(s
, IVSHMEM_MSI
);
568 PCIDevice
*pdev
= PCI_DEVICE(s
);
570 IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector
);
573 IVSHMEM_DPRINTF("with eventfd");
574 watch_vector_notifier(s
, n
, vector
);
575 } else if (msix_enabled(pdev
)) {
576 IVSHMEM_DPRINTF("with irqfd");
577 if (ivshmem_add_kvm_msi_virq(s
, vector
) < 0) {
581 if (!msix_is_masked(pdev
, vector
)) {
582 kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
,
583 s
->msi_vectors
[vector
].virq
);
586 /* it will be delayed until msix is enabled, in write_config */
587 IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled");
591 static void ivshmem_read(void *opaque
, const uint8_t *buf
, int size
)
593 IVShmemState
*s
= opaque
;
596 int64_t incoming_posn
;
600 if (!fifo_update_and_get_i64(s
, buf
, size
, &incoming_posn
)) {
604 if (incoming_posn
< -1) {
605 IVSHMEM_DPRINTF("invalid incoming_posn %" PRId64
"\n", incoming_posn
);
609 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
610 incoming_fd
= qemu_chr_fe_get_msgfd(s
->server_chr
);
611 IVSHMEM_DPRINTF("posn is %" PRId64
", fd is %d\n",
612 incoming_posn
, incoming_fd
);
614 /* make sure we have enough space for this peer */
615 if (incoming_posn
>= s
->nb_peers
) {
616 if (resize_peers(s
, incoming_posn
+ 1) < 0) {
617 error_report("failed to resize peers array");
618 if (incoming_fd
!= -1) {
625 peer
= &s
->peers
[incoming_posn
];
627 if (incoming_fd
== -1) {
628 /* if posn is positive and unseen before then this is our posn*/
629 if (incoming_posn
>= 0 && s
->vm_id
== -1) {
630 /* receive our posn */
631 s
->vm_id
= incoming_posn
;
633 /* otherwise an fd == -1 means an existing peer has gone away */
634 IVSHMEM_DPRINTF("posn %" PRId64
" has gone away\n", incoming_posn
);
635 close_peer_eventfds(s
, incoming_posn
);
640 /* if the position is -1, then it's shared memory region fd */
641 if (incoming_posn
== -1) {
644 if (memory_region_is_mapped(&s
->ivshmem
)) {
645 error_report("shm already initialized");
650 if (check_shm_size(s
, incoming_fd
, &err
) == -1) {
651 error_report_err(err
);
656 /* mmap the region and map into the BAR2 */
657 map_ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
659 if (map_ptr
== MAP_FAILED
) {
660 error_report("Failed to mmap shared memory %s", strerror(errno
));
664 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
),
665 "ivshmem.bar2", s
->ivshmem_size
, map_ptr
);
666 qemu_set_ram_fd(memory_region_get_ram_addr(&s
->ivshmem
),
668 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
670 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64
"\n",
671 map_ptr
, s
->ivshmem_size
);
673 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
678 /* each peer has an associated array of eventfds, and we keep
679 * track of how many eventfds received so far */
680 /* get a new eventfd: */
681 if (peer
->nb_eventfds
>= s
->vectors
) {
682 error_report("Too many eventfd received, device has %d vectors",
688 new_eventfd
= peer
->nb_eventfds
++;
690 /* this is an eventfd for a particular peer VM */
691 IVSHMEM_DPRINTF("eventfds[%" PRId64
"][%d] = %d\n", incoming_posn
,
692 new_eventfd
, incoming_fd
);
693 event_notifier_init_fd(&peer
->eventfds
[new_eventfd
], incoming_fd
);
694 fcntl_setfl(incoming_fd
, O_NONBLOCK
); /* msix/irqfd poll non block */
696 if (incoming_posn
== s
->vm_id
) {
697 setup_interrupt(s
, new_eventfd
);
700 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
701 ivshmem_add_eventfd(s
, incoming_posn
, new_eventfd
);
705 static void ivshmem_check_version(void *opaque
, const uint8_t * buf
, int size
)
707 IVShmemState
*s
= opaque
;
711 if (!fifo_update_and_get_i64(s
, buf
, size
, &version
)) {
715 tmp
= qemu_chr_fe_get_msgfd(s
->server_chr
);
716 if (tmp
!= -1 || version
!= IVSHMEM_PROTOCOL_VERSION
) {
717 fprintf(stderr
, "incompatible version, you are connecting to a ivshmem-"
718 "server using a different protocol please check your setup\n");
719 qemu_chr_delete(s
->server_chr
);
720 s
->server_chr
= NULL
;
724 IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
725 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
, ivshmem_read
,
729 /* Select the MSI-X vectors used by device.
730 * ivshmem maps events to vectors statically, so
731 * we just enable all vectors on init and after reset. */
732 static void ivshmem_use_msix(IVShmemState
* s
)
734 PCIDevice
*d
= PCI_DEVICE(s
);
737 IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__
, msix_present(d
));
738 if (!msix_present(d
)) {
742 for (i
= 0; i
< s
->vectors
; i
++) {
743 msix_vector_use(d
, i
);
747 static void ivshmem_reset(DeviceState
*d
)
749 IVShmemState
*s
= IVSHMEM(d
);
756 static int ivshmem_setup_interrupts(IVShmemState
*s
)
758 /* allocate QEMU callback data for receiving interrupts */
759 s
->msi_vectors
= g_malloc0(s
->vectors
* sizeof(MSIVector
));
761 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
762 if (msix_init_exclusive_bar(PCI_DEVICE(s
), s
->vectors
, 1)) {
766 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
773 static void ivshmem_enable_irqfd(IVShmemState
*s
)
775 PCIDevice
*pdev
= PCI_DEVICE(s
);
778 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
779 ivshmem_add_kvm_msi_virq(s
, i
);
782 if (msix_set_vector_notifiers(pdev
,
783 ivshmem_vector_unmask
,
785 ivshmem_vector_poll
)) {
786 error_report("ivshmem: msix_set_vector_notifiers failed");
790 static void ivshmem_remove_kvm_msi_virq(IVShmemState
*s
, int vector
)
792 IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector
);
794 if (s
->msi_vectors
[vector
].pdev
== NULL
) {
798 /* it was cleaned when masked in the frontend. */
799 kvm_irqchip_release_virq(kvm_state
, s
->msi_vectors
[vector
].virq
);
801 s
->msi_vectors
[vector
].pdev
= NULL
;
804 static void ivshmem_disable_irqfd(IVShmemState
*s
)
806 PCIDevice
*pdev
= PCI_DEVICE(s
);
809 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
810 ivshmem_remove_kvm_msi_virq(s
, i
);
813 msix_unset_vector_notifiers(pdev
);
816 static void ivshmem_write_config(PCIDevice
*pdev
, uint32_t address
,
817 uint32_t val
, int len
)
819 IVShmemState
*s
= IVSHMEM(pdev
);
820 int is_enabled
, was_enabled
= msix_enabled(pdev
);
822 pci_default_write_config(pdev
, address
, val
, len
);
823 is_enabled
= msix_enabled(pdev
);
825 if (kvm_msi_via_irqfd_enabled() && s
->vm_id
!= -1) {
826 if (!was_enabled
&& is_enabled
) {
827 ivshmem_enable_irqfd(s
);
828 } else if (was_enabled
&& !is_enabled
) {
829 ivshmem_disable_irqfd(s
);
834 static void pci_ivshmem_realize(PCIDevice
*dev
, Error
**errp
)
836 IVShmemState
*s
= IVSHMEM(dev
);
838 uint8_t attr
= PCI_BASE_ADDRESS_SPACE_MEMORY
|
839 PCI_BASE_ADDRESS_MEM_PREFETCH
;
841 if (!!s
->server_chr
+ !!s
->shmobj
+ !!s
->hostmem
!= 1) {
843 "You must specify either 'shm', 'chardev' or 'x-memdev'");
851 g_warning("size argument ignored with hostmem");
854 mr
= host_memory_backend_get_memory(s
->hostmem
, errp
);
855 s
->ivshmem_size
= memory_region_size(mr
);
856 } else if (s
->sizearg
== NULL
) {
857 s
->ivshmem_size
= 4 << 20; /* 4 MB default */
860 int64_t size
= qemu_strtosz(s
->sizearg
, &end
);
861 if (size
< 0 || *end
!= '\0' || !is_power_of_2(size
)) {
862 error_setg(errp
, "Invalid size %s", s
->sizearg
);
865 s
->ivshmem_size
= size
;
868 fifo8_create(&s
->incoming_fifo
, sizeof(int64_t));
870 /* IRQFD requires MSI */
871 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
) &&
872 !ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
873 error_setg(errp
, "ioeventfd/irqfd requires MSI");
877 /* check that role is reasonable */
879 if (strncmp(s
->role
, "peer", 5) == 0) {
880 s
->role_val
= IVSHMEM_PEER
;
881 } else if (strncmp(s
->role
, "master", 7) == 0) {
882 s
->role_val
= IVSHMEM_MASTER
;
884 error_setg(errp
, "'role' must be 'peer' or 'master'");
888 s
->role_val
= IVSHMEM_MASTER
; /* default */
891 if (s
->role_val
== IVSHMEM_PEER
) {
892 error_setg(&s
->migration_blocker
,
893 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
894 migrate_add_blocker(s
->migration_blocker
);
897 pci_conf
= dev
->config
;
898 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
900 pci_config_set_interrupt_pin(pci_conf
, 1);
902 memory_region_init_io(&s
->ivshmem_mmio
, OBJECT(s
), &ivshmem_mmio_ops
, s
,
903 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
905 /* region for registers*/
906 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
909 memory_region_init(&s
->bar
, OBJECT(s
), "ivshmem-bar2-container", s
->ivshmem_size
);
910 if (s
->ivshmem_64bit
) {
911 attr
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
914 if (s
->hostmem
!= NULL
) {
917 IVSHMEM_DPRINTF("using hostmem\n");
919 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(s
->hostmem
), errp
);
920 vmstate_register_ram(mr
, DEVICE(s
));
921 memory_region_add_subregion(&s
->bar
, 0, mr
);
922 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
923 } else if (s
->server_chr
!= NULL
) {
924 /* FIXME do not rely on what chr drivers put into filename */
925 if (strncmp(s
->server_chr
->filename
, "unix:", 5)) {
926 error_setg(errp
, "chardev is not a unix client socket");
930 /* if we get a UNIX socket as the parameter we will talk
931 * to the ivshmem server to receive the memory region */
933 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
934 s
->server_chr
->filename
);
936 if (ivshmem_setup_interrupts(s
) < 0) {
937 error_setg(errp
, "failed to initialize interrupts");
941 /* we allocate enough space for 16 peers and grow as needed */
945 pci_register_bar(dev
, 2, attr
, &s
->bar
);
947 s
->eventfd_chr
= g_malloc0(s
->vectors
* sizeof(CharDriverState
*));
949 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
,
950 ivshmem_check_version
, ivshmem_event
, s
);
952 /* just map the file immediately, we're not using a server */
955 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s
->shmobj
);
957 /* try opening with O_EXCL and if it succeeds zero the memory
958 * by truncating to 0 */
959 if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
|O_EXCL
,
960 S_IRWXU
|S_IRWXG
|S_IRWXO
)) > 0) {
961 /* truncate file to length PCI device's memory */
962 if (ftruncate(fd
, s
->ivshmem_size
) != 0) {
963 error_report("could not truncate shared file");
966 } else if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
,
967 S_IRWXU
|S_IRWXG
|S_IRWXO
)) < 0) {
968 error_setg(errp
, "could not open shared file");
972 if (check_shm_size(s
, fd
, errp
) == -1) {
976 create_shared_memory_BAR(s
, fd
, attr
, errp
);
980 static void pci_ivshmem_exit(PCIDevice
*dev
)
982 IVShmemState
*s
= IVSHMEM(dev
);
985 fifo8_destroy(&s
->incoming_fifo
);
987 if (s
->migration_blocker
) {
988 migrate_del_blocker(s
->migration_blocker
);
989 error_free(s
->migration_blocker
);
992 if (memory_region_is_mapped(&s
->ivshmem
)) {
994 void *addr
= memory_region_get_ram_ptr(&s
->ivshmem
);
997 if (munmap(addr
, s
->ivshmem_size
) == -1) {
998 error_report("Failed to munmap shared memory %s",
1002 fd
= qemu_get_ram_fd(memory_region_get_ram_addr(&s
->ivshmem
));
1008 vmstate_unregister_ram(&s
->ivshmem
, DEVICE(dev
));
1009 memory_region_del_subregion(&s
->bar
, &s
->ivshmem
);
1012 if (s
->eventfd_chr
) {
1013 for (i
= 0; i
< s
->vectors
; i
++) {
1014 if (s
->eventfd_chr
[i
]) {
1015 qemu_chr_free(s
->eventfd_chr
[i
]);
1018 g_free(s
->eventfd_chr
);
1022 for (i
= 0; i
< s
->nb_peers
; i
++) {
1023 close_peer_eventfds(s
, i
);
1028 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1029 msix_uninit_exclusive_bar(dev
);
1032 g_free(s
->msi_vectors
);
1035 static bool test_msix(void *opaque
, int version_id
)
1037 IVShmemState
*s
= opaque
;
1039 return ivshmem_has_feature(s
, IVSHMEM_MSI
);
1042 static bool test_no_msix(void *opaque
, int version_id
)
1044 return !test_msix(opaque
, version_id
);
1047 static int ivshmem_pre_load(void *opaque
)
1049 IVShmemState
*s
= opaque
;
1051 if (s
->role_val
== IVSHMEM_PEER
) {
1052 error_report("'peer' devices are not migratable");
1059 static int ivshmem_post_load(void *opaque
, int version_id
)
1061 IVShmemState
*s
= opaque
;
1063 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1064 ivshmem_use_msix(s
);
1070 static int ivshmem_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
1072 IVShmemState
*s
= opaque
;
1073 PCIDevice
*pdev
= PCI_DEVICE(s
);
1076 IVSHMEM_DPRINTF("ivshmem_load_old\n");
1078 if (version_id
!= 0) {
1082 if (s
->role_val
== IVSHMEM_PEER
) {
1083 error_report("'peer' devices are not migratable");
1087 ret
= pci_device_load(pdev
, f
);
1092 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1094 ivshmem_use_msix(s
);
1096 s
->intrstatus
= qemu_get_be32(f
);
1097 s
->intrmask
= qemu_get_be32(f
);
1103 static const VMStateDescription ivshmem_vmsd
= {
1106 .minimum_version_id
= 1,
1107 .pre_load
= ivshmem_pre_load
,
1108 .post_load
= ivshmem_post_load
,
1109 .fields
= (VMStateField
[]) {
1110 VMSTATE_PCI_DEVICE(parent_obj
, IVShmemState
),
1112 VMSTATE_MSIX_TEST(parent_obj
, IVShmemState
, test_msix
),
1113 VMSTATE_UINT32_TEST(intrstatus
, IVShmemState
, test_no_msix
),
1114 VMSTATE_UINT32_TEST(intrmask
, IVShmemState
, test_no_msix
),
1116 VMSTATE_END_OF_LIST()
1118 .load_state_old
= ivshmem_load_old
,
1119 .minimum_version_id_old
= 0
1122 static Property ivshmem_properties
[] = {
1123 DEFINE_PROP_CHR("chardev", IVShmemState
, server_chr
),
1124 DEFINE_PROP_STRING("size", IVShmemState
, sizearg
),
1125 DEFINE_PROP_UINT32("vectors", IVShmemState
, vectors
, 1),
1126 DEFINE_PROP_BIT("ioeventfd", IVShmemState
, features
, IVSHMEM_IOEVENTFD
, false),
1127 DEFINE_PROP_BIT("msi", IVShmemState
, features
, IVSHMEM_MSI
, true),
1128 DEFINE_PROP_STRING("shm", IVShmemState
, shmobj
),
1129 DEFINE_PROP_STRING("role", IVShmemState
, role
),
1130 DEFINE_PROP_UINT32("use64", IVShmemState
, ivshmem_64bit
, 1),
1131 DEFINE_PROP_END_OF_LIST(),
1134 static void ivshmem_class_init(ObjectClass
*klass
, void *data
)
1136 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1137 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1139 k
->realize
= pci_ivshmem_realize
;
1140 k
->exit
= pci_ivshmem_exit
;
1141 k
->config_write
= ivshmem_write_config
;
1142 k
->vendor_id
= PCI_VENDOR_ID_IVSHMEM
;
1143 k
->device_id
= PCI_DEVICE_ID_IVSHMEM
;
1144 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
1145 dc
->reset
= ivshmem_reset
;
1146 dc
->props
= ivshmem_properties
;
1147 dc
->vmsd
= &ivshmem_vmsd
;
1148 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1149 dc
->desc
= "Inter-VM shared memory";
1152 static void ivshmem_check_memdev_is_busy(Object
*obj
, const char *name
,
1153 Object
*val
, Error
**errp
)
1157 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(val
), errp
);
1158 if (memory_region_is_mapped(mr
)) {
1159 char *path
= object_get_canonical_path_component(val
);
1160 error_setg(errp
, "can't use already busy memdev: %s", path
);
1163 qdev_prop_allow_set_link_before_realize(obj
, name
, val
, errp
);
1167 static void ivshmem_init(Object
*obj
)
1169 IVShmemState
*s
= IVSHMEM(obj
);
1171 object_property_add_link(obj
, "x-memdev", TYPE_MEMORY_BACKEND
,
1172 (Object
**)&s
->hostmem
,
1173 ivshmem_check_memdev_is_busy
,
1174 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1178 static const TypeInfo ivshmem_info
= {
1179 .name
= TYPE_IVSHMEM
,
1180 .parent
= TYPE_PCI_DEVICE
,
1181 .instance_size
= sizeof(IVShmemState
),
1182 .instance_init
= ivshmem_init
,
1183 .class_init
= ivshmem_class_init
,
1186 static void ivshmem_register_types(void)
1188 type_register_static(&ivshmem_info
);
1191 type_init(ivshmem_register_types
)