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/msix.h"
23 #include "sysemu/kvm.h"
24 #include "migration/migration.h"
25 #include "qemu/error-report.h"
26 #include "qemu/event_notifier.h"
27 #include "qemu/fifo8.h"
28 #include "sysemu/char.h"
31 #include <sys/types.h>
34 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
35 #define PCI_DEVICE_ID_IVSHMEM 0x1110
37 #define IVSHMEM_MAX_PEERS G_MAXUINT16
38 #define IVSHMEM_IOEVENTFD 0
41 #define IVSHMEM_PEER 0
42 #define IVSHMEM_MASTER 1
44 #define IVSHMEM_REG_BAR_SIZE 0x100
46 //#define DEBUG_IVSHMEM
48 #define IVSHMEM_DPRINTF(fmt, ...) \
49 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
51 #define IVSHMEM_DPRINTF(fmt, ...)
54 #define TYPE_IVSHMEM "ivshmem"
55 #define IVSHMEM(obj) \
56 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
60 EventNotifier
*eventfds
;
63 typedef struct EventfdEntry
{
68 typedef struct IVShmemState
{
76 CharDriverState
**eventfd_chr
;
77 CharDriverState
*server_chr
;
79 MemoryRegion ivshmem_mmio
;
81 /* We might need to register the BAR before we actually have the memory.
82 * So prepare a container MemoryRegion for the BAR immediately and
83 * add a subregion when we have the memory.
87 uint64_t ivshmem_size
; /* size of shared memory region */
88 uint32_t ivshmem_64bit
;
89 int shm_fd
; /* shared memory file descriptor */
92 int nb_peers
; /* how many guests we have space for */
97 EventfdEntry
*eventfd_table
;
99 Error
*migration_blocker
;
104 int role_val
; /* scalar to avoid multiple string comparisons */
107 /* registers for the Inter-VM shared memory device */
108 enum ivshmem_registers
{
115 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
116 unsigned int feature
) {
117 return (ivs
->features
& (1 << feature
));
120 /* accessing registers - based on rtl8139 */
121 static void ivshmem_update_irq(IVShmemState
*s
)
123 PCIDevice
*d
= PCI_DEVICE(s
);
125 isr
= (s
->intrstatus
& s
->intrmask
) & 0xffffffff;
127 /* don't print ISR resets */
129 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
130 isr
? 1 : 0, s
->intrstatus
, s
->intrmask
);
133 pci_set_irq(d
, (isr
!= 0));
136 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
138 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
142 ivshmem_update_irq(s
);
145 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
147 uint32_t ret
= s
->intrmask
;
149 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
);
160 ivshmem_update_irq(s
);
163 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
165 uint32_t ret
= s
->intrstatus
;
167 /* reading ISR clears all interrupts */
170 ivshmem_update_irq(s
);
175 static void ivshmem_io_write(void *opaque
, hwaddr addr
,
176 uint64_t val
, unsigned size
)
178 IVShmemState
*s
= opaque
;
180 uint16_t dest
= val
>> 16;
181 uint16_t vector
= val
& 0xff;
185 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
189 ivshmem_IntrMask_write(s
, val
);
193 ivshmem_IntrStatus_write(s
, val
);
197 /* check that dest VM ID is reasonable */
198 if (dest
>= s
->nb_peers
) {
199 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
203 /* check doorbell range */
204 if (vector
< s
->peers
[dest
].nb_eventfds
) {
205 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
206 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
208 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
213 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx
"\n", addr
);
217 static uint64_t ivshmem_io_read(void *opaque
, hwaddr addr
,
221 IVShmemState
*s
= opaque
;
227 ret
= ivshmem_IntrMask_read(s
);
231 ret
= ivshmem_IntrStatus_read(s
);
235 /* return my VM ID if the memory is mapped */
236 if (s
->shm_fd
>= 0) {
244 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
251 static const MemoryRegionOps ivshmem_mmio_ops
= {
252 .read
= ivshmem_io_read
,
253 .write
= ivshmem_io_write
,
254 .endianness
= DEVICE_NATIVE_ENDIAN
,
256 .min_access_size
= 4,
257 .max_access_size
= 4,
261 static void ivshmem_receive(void *opaque
, const uint8_t *buf
, int size
)
263 IVShmemState
*s
= opaque
;
265 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x size: %d\n", *buf
, size
);
267 ivshmem_IntrStatus_write(s
, *buf
);
270 static int ivshmem_can_receive(void * opaque
)
275 static void ivshmem_event(void *opaque
, int event
)
277 IVSHMEM_DPRINTF("ivshmem_event %d\n", event
);
280 static void fake_irqfd(void *opaque
, const uint8_t *buf
, int size
) {
282 EventfdEntry
*entry
= opaque
;
283 PCIDevice
*pdev
= entry
->pdev
;
285 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, entry
->vector
);
286 msix_notify(pdev
, entry
->vector
);
289 static CharDriverState
* create_eventfd_chr_device(void * opaque
, EventNotifier
*n
,
292 /* create a event character device based on the passed eventfd */
293 IVShmemState
*s
= opaque
;
294 CharDriverState
* chr
;
295 int eventfd
= event_notifier_get_fd(n
);
297 chr
= qemu_chr_open_eventfd(eventfd
);
300 error_report("creating chardriver for eventfd %d failed", eventfd
);
303 qemu_chr_fe_claim_no_fail(chr
);
305 /* if MSI is supported we need multiple interrupts */
306 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
307 s
->eventfd_table
[vector
].pdev
= PCI_DEVICE(s
);
308 s
->eventfd_table
[vector
].vector
= vector
;
310 qemu_chr_add_handlers(chr
, ivshmem_can_receive
, fake_irqfd
,
311 ivshmem_event
, &s
->eventfd_table
[vector
]);
313 qemu_chr_add_handlers(chr
, ivshmem_can_receive
, ivshmem_receive
,
321 static int check_shm_size(IVShmemState
*s
, int fd
, Error
**errp
)
323 /* check that the guest isn't going to try and map more memory than the
324 * the object has allocated return -1 to indicate error */
328 if (fstat(fd
, &buf
) < 0) {
329 error_setg(errp
, "exiting: fstat on fd %d failed: %s",
330 fd
, strerror(errno
));
334 if (s
->ivshmem_size
> buf
.st_size
) {
335 error_setg(errp
, "Requested memory size greater"
336 " than shared object size (%" PRIu64
" > %" PRIu64
")",
337 s
->ivshmem_size
, (uint64_t)buf
.st_size
);
344 /* create the shared memory BAR when we are not using the server, so we can
345 * create the BAR and map the memory immediately */
346 static int create_shared_memory_BAR(IVShmemState
*s
, int fd
, uint8_t attr
,
351 ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
352 if (ptr
== MAP_FAILED
) {
353 error_setg_errno(errp
, errno
, "Failed to mmap shared memory");
359 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
), "ivshmem.bar2",
360 s
->ivshmem_size
, ptr
);
361 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
362 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
364 /* region for shared memory */
365 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
370 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
372 memory_region_add_eventfd(&s
->ivshmem_mmio
,
377 &s
->peers
[posn
].eventfds
[i
]);
380 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
382 memory_region_del_eventfd(&s
->ivshmem_mmio
,
387 &s
->peers
[posn
].eventfds
[i
]);
390 static void close_guest_eventfds(IVShmemState
*s
, int posn
)
392 int i
, guest_curr_max
;
394 if (!ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
397 if (posn
< 0 || posn
>= s
->nb_peers
) {
398 error_report("invalid peer %d", posn
);
402 guest_curr_max
= s
->peers
[posn
].nb_eventfds
;
404 memory_region_transaction_begin();
405 for (i
= 0; i
< guest_curr_max
; i
++) {
406 ivshmem_del_eventfd(s
, posn
, i
);
408 memory_region_transaction_commit();
409 for (i
= 0; i
< guest_curr_max
; i
++) {
410 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
413 g_free(s
->peers
[posn
].eventfds
);
414 s
->peers
[posn
].nb_eventfds
= 0;
417 /* this function increase the dynamic storage need to store data about other
419 static int resize_peers(IVShmemState
*s
, int new_min_size
)
424 /* limit number of max peers */
425 if (new_min_size
<= 0 || new_min_size
> IVSHMEM_MAX_PEERS
) {
428 if (new_min_size
<= s
->nb_peers
) {
432 old_size
= s
->nb_peers
;
433 s
->nb_peers
= new_min_size
;
435 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s
->nb_peers
);
437 s
->peers
= g_realloc(s
->peers
, s
->nb_peers
* sizeof(Peer
));
439 for (j
= old_size
; j
< s
->nb_peers
; j
++) {
440 s
->peers
[j
].eventfds
= g_new0(EventNotifier
, s
->vectors
);
441 s
->peers
[j
].nb_eventfds
= 0;
447 static bool fifo_update_and_get(IVShmemState
*s
, const uint8_t *buf
, int size
,
448 void *data
, size_t len
)
453 assert(len
<= sizeof(long)); /* limitation of the fifo */
454 if (fifo8_is_empty(&s
->incoming_fifo
) && size
== len
) {
455 memcpy(data
, buf
, size
);
459 IVSHMEM_DPRINTF("short read of %d bytes\n", size
);
461 num
= MIN(size
, sizeof(long) - fifo8_num_used(&s
->incoming_fifo
));
462 fifo8_push_all(&s
->incoming_fifo
, buf
, num
);
464 if (fifo8_num_used(&s
->incoming_fifo
) < len
) {
471 p
= fifo8_pop_buf(&s
->incoming_fifo
, len
, &num
);
474 memcpy(data
, p
, len
);
477 fifo8_push_all(&s
->incoming_fifo
, buf
, size
);
483 static void ivshmem_read(void *opaque
, const uint8_t *buf
, int size
)
485 IVShmemState
*s
= opaque
;
492 if (!fifo_update_and_get(s
, buf
, size
,
493 &incoming_posn
, sizeof(incoming_posn
))) {
497 if (incoming_posn
< -1) {
498 IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn
);
502 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
503 incoming_fd
= qemu_chr_fe_get_msgfd(s
->server_chr
);
504 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn
, incoming_fd
);
506 /* make sure we have enough space for this guest */
507 if (incoming_posn
>= s
->nb_peers
) {
508 if (resize_peers(s
, incoming_posn
+ 1) < 0) {
509 error_report("failed to resize peers array");
510 if (incoming_fd
!= -1) {
517 peer
= &s
->peers
[incoming_posn
];
519 if (incoming_fd
== -1) {
520 /* if posn is positive and unseen before then this is our posn*/
521 if (incoming_posn
>= 0 && s
->vm_id
== -1) {
522 /* receive our posn */
523 s
->vm_id
= incoming_posn
;
525 /* otherwise an fd == -1 means an existing guest has gone away */
526 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn
);
527 close_guest_eventfds(s
, incoming_posn
);
532 /* if the position is -1, then it's shared memory region fd */
533 if (incoming_posn
== -1) {
536 if (s
->shm_fd
>= 0) {
537 error_report("shm already initialized");
542 if (check_shm_size(s
, incoming_fd
, &err
) == -1) {
543 error_report_err(err
);
548 /* mmap the region and map into the BAR2 */
549 map_ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
551 if (map_ptr
== MAP_FAILED
) {
552 error_report("Failed to mmap shared memory %s", strerror(errno
));
556 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
),
557 "ivshmem.bar2", s
->ivshmem_size
, map_ptr
);
558 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
560 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64
"\n",
561 map_ptr
, s
->ivshmem_size
);
563 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
565 /* only store the fd if it is successfully mapped */
566 s
->shm_fd
= incoming_fd
;
571 /* each peer has an associated array of eventfds, and we keep
572 * track of how many eventfds received so far */
573 /* get a new eventfd: */
574 new_eventfd
= peer
->nb_eventfds
++;
576 /* this is an eventfd for a particular guest VM */
577 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn
,
578 new_eventfd
, incoming_fd
);
579 event_notifier_init_fd(&peer
->eventfds
[new_eventfd
], incoming_fd
);
581 if (incoming_posn
== s
->vm_id
) {
582 s
->eventfd_chr
[new_eventfd
] = create_eventfd_chr_device(s
,
583 &s
->peers
[s
->vm_id
].eventfds
[new_eventfd
],
587 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
588 ivshmem_add_eventfd(s
, incoming_posn
, new_eventfd
);
592 /* Select the MSI-X vectors used by device.
593 * ivshmem maps events to vectors statically, so
594 * we just enable all vectors on init and after reset. */
595 static void ivshmem_use_msix(IVShmemState
* s
)
597 PCIDevice
*d
= PCI_DEVICE(s
);
600 IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__
, msix_present(d
));
601 if (!msix_present(d
)) {
605 for (i
= 0; i
< s
->vectors
; i
++) {
606 msix_vector_use(d
, i
);
610 static void ivshmem_reset(DeviceState
*d
)
612 IVShmemState
*s
= IVSHMEM(d
);
618 static uint64_t ivshmem_get_size(IVShmemState
* s
, Error
**errp
) {
623 value
= strtoull(s
->sizearg
, &ptr
, 10);
625 case 0: case 'M': case 'm':
632 error_setg(errp
, "invalid ram size: %s", s
->sizearg
);
636 /* BARs must be a power of 2 */
637 if (!is_power_of_2(value
)) {
638 error_setg(errp
, "size must be power of 2");
645 static int ivshmem_setup_msi(IVShmemState
* s
)
647 if (msix_init_exclusive_bar(PCI_DEVICE(s
), s
->vectors
, 1)) {
651 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
653 /* allocate QEMU char devices for receiving interrupts */
654 s
->eventfd_table
= g_malloc0(s
->vectors
* sizeof(EventfdEntry
));
660 static void ivshmem_write_config(PCIDevice
*pci_dev
, uint32_t address
,
661 uint32_t val
, int len
)
663 pci_default_write_config(pci_dev
, address
, val
, len
);
666 static void pci_ivshmem_realize(PCIDevice
*dev
, Error
**errp
)
668 IVShmemState
*s
= IVSHMEM(dev
);
670 uint8_t attr
= PCI_BASE_ADDRESS_SPACE_MEMORY
|
671 PCI_BASE_ADDRESS_MEM_PREFETCH
;
672 Error
*local_err
= NULL
;
676 if (s
->sizearg
== NULL
) {
677 s
->ivshmem_size
= 4 << 20; /* 4 MB default */
679 s
->ivshmem_size
= ivshmem_get_size(s
, &local_err
);
681 error_propagate(errp
, local_err
);
686 fifo8_create(&s
->incoming_fifo
, sizeof(long));
688 /* IRQFD requires MSI */
689 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
) &&
690 !ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
691 error_setg(errp
, "ioeventfd/irqfd requires MSI");
695 /* check that role is reasonable */
697 if (strncmp(s
->role
, "peer", 5) == 0) {
698 s
->role_val
= IVSHMEM_PEER
;
699 } else if (strncmp(s
->role
, "master", 7) == 0) {
700 s
->role_val
= IVSHMEM_MASTER
;
702 error_setg(errp
, "'role' must be 'peer' or 'master'");
706 s
->role_val
= IVSHMEM_MASTER
; /* default */
709 if (s
->role_val
== IVSHMEM_PEER
) {
710 error_setg(&s
->migration_blocker
,
711 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
712 migrate_add_blocker(s
->migration_blocker
);
715 pci_conf
= dev
->config
;
716 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
718 pci_config_set_interrupt_pin(pci_conf
, 1);
720 memory_region_init_io(&s
->ivshmem_mmio
, OBJECT(s
), &ivshmem_mmio_ops
, s
,
721 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
723 /* region for registers*/
724 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
727 memory_region_init(&s
->bar
, OBJECT(s
), "ivshmem-bar2-container", s
->ivshmem_size
);
728 if (s
->ivshmem_64bit
) {
729 attr
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
732 if (s
->server_chr
!= NULL
) {
733 if (strncmp(s
->server_chr
->filename
, "unix:", 5)) {
734 error_setg(errp
, "chardev is not a unix client socket");
738 /* if we get a UNIX socket as the parameter we will talk
739 * to the ivshmem server to receive the memory region */
741 if (s
->shmobj
!= NULL
) {
742 error_setg(errp
, "do not specify both 'chardev' "
743 "and 'shm' with ivshmem");
747 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
748 s
->server_chr
->filename
);
750 if (ivshmem_has_feature(s
, IVSHMEM_MSI
) &&
751 ivshmem_setup_msi(s
)) {
752 error_setg(errp
, "msix initialization failed");
756 /* we allocate enough space for 16 guests and grow as needed */
760 pci_register_bar(dev
, 2, attr
, &s
->bar
);
762 s
->eventfd_chr
= g_malloc0(s
->vectors
* sizeof(CharDriverState
*));
764 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
, ivshmem_read
,
767 /* just map the file immediately, we're not using a server */
770 if (s
->shmobj
== NULL
) {
771 error_setg(errp
, "Must specify 'chardev' or 'shm' to ivshmem");
775 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s
->shmobj
);
777 /* try opening with O_EXCL and if it succeeds zero the memory
778 * by truncating to 0 */
779 if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
|O_EXCL
,
780 S_IRWXU
|S_IRWXG
|S_IRWXO
)) > 0) {
781 /* truncate file to length PCI device's memory */
782 if (ftruncate(fd
, s
->ivshmem_size
) != 0) {
783 error_report("could not truncate shared file");
786 } else if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
,
787 S_IRWXU
|S_IRWXG
|S_IRWXO
)) < 0) {
788 error_setg(errp
, "could not open shared file");
792 if (check_shm_size(s
, fd
, errp
) == -1) {
796 create_shared_memory_BAR(s
, fd
, attr
, errp
);
800 static void pci_ivshmem_exit(PCIDevice
*dev
)
802 IVShmemState
*s
= IVSHMEM(dev
);
805 fifo8_destroy(&s
->incoming_fifo
);
807 if (s
->migration_blocker
) {
808 migrate_del_blocker(s
->migration_blocker
);
809 error_free(s
->migration_blocker
);
812 if (s
->shm_fd
>= 0) {
813 void *addr
= memory_region_get_ram_ptr(&s
->ivshmem
);
815 vmstate_unregister_ram(&s
->ivshmem
, DEVICE(dev
));
816 memory_region_del_subregion(&s
->bar
, &s
->ivshmem
);
818 if (munmap(addr
, s
->ivshmem_size
) == -1) {
819 error_report("Failed to munmap shared memory %s", strerror(errno
));
823 if (s
->eventfd_chr
) {
824 for (i
= 0; i
< s
->vectors
; i
++) {
825 if (s
->eventfd_chr
[i
]) {
826 qemu_chr_free(s
->eventfd_chr
[i
]);
829 g_free(s
->eventfd_chr
);
833 for (i
= 0; i
< s
->nb_peers
; i
++) {
834 close_guest_eventfds(s
, i
);
839 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
840 msix_uninit_exclusive_bar(dev
);
843 g_free(s
->eventfd_table
);
846 static bool test_msix(void *opaque
, int version_id
)
848 IVShmemState
*s
= opaque
;
850 return ivshmem_has_feature(s
, IVSHMEM_MSI
);
853 static bool test_no_msix(void *opaque
, int version_id
)
855 return !test_msix(opaque
, version_id
);
858 static int ivshmem_pre_load(void *opaque
)
860 IVShmemState
*s
= opaque
;
862 if (s
->role_val
== IVSHMEM_PEER
) {
863 error_report("'peer' devices are not migratable");
870 static int ivshmem_post_load(void *opaque
, int version_id
)
872 IVShmemState
*s
= opaque
;
874 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
881 static int ivshmem_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
883 IVShmemState
*s
= opaque
;
884 PCIDevice
*pdev
= PCI_DEVICE(s
);
887 IVSHMEM_DPRINTF("ivshmem_load_old\n");
889 if (version_id
!= 0) {
893 if (s
->role_val
== IVSHMEM_PEER
) {
894 error_report("'peer' devices are not migratable");
898 ret
= pci_device_load(pdev
, f
);
903 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
907 s
->intrstatus
= qemu_get_be32(f
);
908 s
->intrmask
= qemu_get_be32(f
);
914 static const VMStateDescription ivshmem_vmsd
= {
917 .minimum_version_id
= 1,
918 .pre_load
= ivshmem_pre_load
,
919 .post_load
= ivshmem_post_load
,
920 .fields
= (VMStateField
[]) {
921 VMSTATE_PCI_DEVICE(parent_obj
, IVShmemState
),
923 VMSTATE_MSIX_TEST(parent_obj
, IVShmemState
, test_msix
),
924 VMSTATE_UINT32_TEST(intrstatus
, IVShmemState
, test_no_msix
),
925 VMSTATE_UINT32_TEST(intrmask
, IVShmemState
, test_no_msix
),
927 VMSTATE_END_OF_LIST()
929 .load_state_old
= ivshmem_load_old
,
930 .minimum_version_id_old
= 0
933 static Property ivshmem_properties
[] = {
934 DEFINE_PROP_CHR("chardev", IVShmemState
, server_chr
),
935 DEFINE_PROP_STRING("size", IVShmemState
, sizearg
),
936 DEFINE_PROP_UINT32("vectors", IVShmemState
, vectors
, 1),
937 DEFINE_PROP_BIT("ioeventfd", IVShmemState
, features
, IVSHMEM_IOEVENTFD
, false),
938 DEFINE_PROP_BIT("msi", IVShmemState
, features
, IVSHMEM_MSI
, true),
939 DEFINE_PROP_STRING("shm", IVShmemState
, shmobj
),
940 DEFINE_PROP_STRING("role", IVShmemState
, role
),
941 DEFINE_PROP_UINT32("use64", IVShmemState
, ivshmem_64bit
, 1),
942 DEFINE_PROP_END_OF_LIST(),
945 static void ivshmem_class_init(ObjectClass
*klass
, void *data
)
947 DeviceClass
*dc
= DEVICE_CLASS(klass
);
948 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
950 k
->realize
= pci_ivshmem_realize
;
951 k
->exit
= pci_ivshmem_exit
;
952 k
->config_write
= ivshmem_write_config
;
953 k
->vendor_id
= PCI_VENDOR_ID_IVSHMEM
;
954 k
->device_id
= PCI_DEVICE_ID_IVSHMEM
;
955 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
956 dc
->reset
= ivshmem_reset
;
957 dc
->props
= ivshmem_properties
;
958 dc
->vmsd
= &ivshmem_vmsd
;
959 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
960 dc
->desc
= "Inter-VM shared memory";
963 static const TypeInfo ivshmem_info
= {
964 .name
= TYPE_IVSHMEM
,
965 .parent
= TYPE_PCI_DEVICE
,
966 .instance_size
= sizeof(IVShmemState
),
967 .class_init
= ivshmem_class_init
,
970 static void ivshmem_register_types(void)
972 type_register_static(&ivshmem_info
);
975 type_init(ivshmem_register_types
)