ivshmem: remove superflous ivshmem_attr field
[qemu/ar7.git] / hw / misc / ivshmem.c
blob7138b8d92350d1e8419052f8e47b5b90b9e77108
1 /*
2 * Inter-VM Shared Memory PCI device.
4 * Author:
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)
11 * and rtl8139.c
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 "hw/hw.h"
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"
30 #include <sys/mman.h>
31 #include <sys/types.h>
32 #include <limits.h>
34 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
35 #define PCI_DEVICE_ID_IVSHMEM 0x1110
37 #define IVSHMEM_IOEVENTFD 0
38 #define IVSHMEM_MSI 1
40 #define IVSHMEM_PEER 0
41 #define IVSHMEM_MASTER 1
43 #define IVSHMEM_REG_BAR_SIZE 0x100
45 //#define DEBUG_IVSHMEM
46 #ifdef DEBUG_IVSHMEM
47 #define IVSHMEM_DPRINTF(fmt, ...) \
48 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
49 #else
50 #define IVSHMEM_DPRINTF(fmt, ...)
51 #endif
53 #define TYPE_IVSHMEM "ivshmem"
54 #define IVSHMEM(obj) \
55 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
57 typedef struct Peer {
58 int nb_eventfds;
59 EventNotifier *eventfds;
60 } Peer;
62 typedef struct EventfdEntry {
63 PCIDevice *pdev;
64 int vector;
65 } EventfdEntry;
67 typedef struct IVShmemState {
68 /*< private >*/
69 PCIDevice parent_obj;
70 /*< public >*/
72 uint32_t intrmask;
73 uint32_t intrstatus;
74 uint32_t doorbell;
76 CharDriverState **eventfd_chr;
77 CharDriverState *server_chr;
78 Fifo8 incoming_fifo;
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.
85 MemoryRegion bar;
86 MemoryRegion ivshmem;
87 uint64_t ivshmem_size; /* size of shared memory region */
88 uint32_t ivshmem_64bit;
89 int shm_fd; /* shared memory file descriptor */
91 Peer *peers;
92 int nb_peers; /* how many guests we have space for */
93 int max_peer; /* maximum numbered peer */
95 int vm_id;
96 uint32_t vectors;
97 uint32_t features;
98 EventfdEntry *eventfd_table;
100 Error *migration_blocker;
102 char * shmobj;
103 char * sizearg;
104 char * role;
105 int role_val; /* scalar to avoid multiple string comparisons */
106 } IVShmemState;
108 /* registers for the Inter-VM shared memory device */
109 enum ivshmem_registers {
110 INTRMASK = 0,
111 INTRSTATUS = 4,
112 IVPOSITION = 8,
113 DOORBELL = 12,
116 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
117 unsigned int feature) {
118 return (ivs->features & (1 << feature));
121 static inline bool is_power_of_two(uint64_t x) {
122 return (x & (x - 1)) == 0;
125 /* accessing registers - based on rtl8139 */
126 static void ivshmem_update_irq(IVShmemState *s, int val)
128 PCIDevice *d = PCI_DEVICE(s);
129 int isr;
130 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
132 /* don't print ISR resets */
133 if (isr) {
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);
145 s->intrmask = val;
147 ivshmem_update_irq(s, val);
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);
156 return ret;
159 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
161 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
163 s->intrstatus = val;
165 ivshmem_update_irq(s, val);
168 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
170 uint32_t ret = s->intrstatus;
172 /* reading ISR clears all interrupts */
173 s->intrstatus = 0;
175 ivshmem_update_irq(s, 0);
177 return ret;
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;
188 addr &= 0xfc;
190 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
191 switch (addr)
193 case INTRMASK:
194 ivshmem_IntrMask_write(s, val);
195 break;
197 case INTRSTATUS:
198 ivshmem_IntrStatus_write(s, val);
199 break;
201 case DOORBELL:
202 /* check that dest VM ID is reasonable */
203 if (dest > s->max_peer) {
204 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
205 break;
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 break;
214 default:
215 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
219 static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
220 unsigned size)
223 IVShmemState *s = opaque;
224 uint32_t ret;
226 switch (addr)
228 case INTRMASK:
229 ret = ivshmem_IntrMask_read(s);
230 break;
232 case INTRSTATUS:
233 ret = ivshmem_IntrStatus_read(s);
234 break;
236 case IVPOSITION:
237 /* return my VM ID if the memory is mapped */
238 if (s->shm_fd > 0) {
239 ret = s->vm_id;
240 } else {
241 ret = -1;
243 break;
245 default:
246 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
247 ret = 0;
250 return ret;
253 static const MemoryRegionOps ivshmem_mmio_ops = {
254 .read = ivshmem_io_read,
255 .write = ivshmem_io_write,
256 .endianness = DEVICE_NATIVE_ENDIAN,
257 .impl = {
258 .min_access_size = 4,
259 .max_access_size = 4,
263 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
265 IVShmemState *s = opaque;
267 ivshmem_IntrStatus_write(s, *buf);
269 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
272 static int ivshmem_can_receive(void * opaque)
274 return sizeof(long);
277 static void ivshmem_event(void *opaque, int event)
279 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
282 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
284 EventfdEntry *entry = opaque;
285 PCIDevice *pdev = entry->pdev;
287 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
288 msix_notify(pdev, entry->vector);
291 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
292 int vector)
294 /* create a event character device based on the passed eventfd */
295 IVShmemState *s = opaque;
296 CharDriverState * chr;
297 int eventfd = event_notifier_get_fd(n);
299 chr = qemu_chr_open_eventfd(eventfd);
301 if (chr == NULL) {
302 error_report("creating eventfd for eventfd %d failed", eventfd);
303 exit(1);
305 qemu_chr_fe_claim_no_fail(chr);
307 /* if MSI is supported we need multiple interrupts */
308 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
309 s->eventfd_table[vector].pdev = PCI_DEVICE(s);
310 s->eventfd_table[vector].vector = vector;
312 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
313 ivshmem_event, &s->eventfd_table[vector]);
314 } else {
315 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
316 ivshmem_event, s);
319 return chr;
323 static int check_shm_size(IVShmemState *s, int fd) {
324 /* check that the guest isn't going to try and map more memory than the
325 * the object has allocated return -1 to indicate error */
327 struct stat buf;
329 if (fstat(fd, &buf) < 0) {
330 error_report("exiting: fstat on fd %d failed: %s",
331 fd, strerror(errno));
332 return -1;
335 if (s->ivshmem_size > buf.st_size) {
336 error_report("Requested memory size greater"
337 " than shared object size (%" PRIu64 " > %" PRIu64")",
338 s->ivshmem_size, (uint64_t)buf.st_size);
339 return -1;
340 } else {
341 return 0;
345 /* create the shared memory BAR when we are not using the server, so we can
346 * create the BAR and map the memory immediately */
347 static void create_shared_memory_BAR(IVShmemState *s, int fd, uint8_t attr) {
349 void * ptr;
351 s->shm_fd = fd;
353 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
355 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2",
356 s->ivshmem_size, ptr);
357 vmstate_register_ram(&s->ivshmem, DEVICE(s));
358 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
360 /* region for shared memory */
361 pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
364 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
366 memory_region_add_eventfd(&s->ivshmem_mmio,
367 DOORBELL,
369 true,
370 (posn << 16) | i,
371 &s->peers[posn].eventfds[i]);
374 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
376 memory_region_del_eventfd(&s->ivshmem_mmio,
377 DOORBELL,
379 true,
380 (posn << 16) | i,
381 &s->peers[posn].eventfds[i]);
384 static void close_guest_eventfds(IVShmemState *s, int posn)
386 int i, guest_curr_max;
388 if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
389 return;
391 if (posn < 0 || posn >= s->nb_peers) {
392 return;
395 guest_curr_max = s->peers[posn].nb_eventfds;
397 memory_region_transaction_begin();
398 for (i = 0; i < guest_curr_max; i++) {
399 ivshmem_del_eventfd(s, posn, i);
401 memory_region_transaction_commit();
402 for (i = 0; i < guest_curr_max; i++) {
403 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
406 g_free(s->peers[posn].eventfds);
407 s->peers[posn].nb_eventfds = 0;
410 /* this function increase the dynamic storage need to store data about other
411 * guests */
412 static int increase_dynamic_storage(IVShmemState *s, int new_min_size)
415 int j, old_nb_alloc;
417 /* check for integer overflow */
418 if (new_min_size >= INT_MAX / sizeof(Peer) - 1 || new_min_size <= 0) {
419 return -1;
422 old_nb_alloc = s->nb_peers;
424 if (new_min_size >= s->nb_peers) {
425 /* +1 because #new_min_size is used as last array index */
426 s->nb_peers = new_min_size + 1;
427 } else {
428 return 0;
431 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
432 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
434 /* zero out new pointers */
435 for (j = old_nb_alloc; j < s->nb_peers; j++) {
436 s->peers[j].eventfds = NULL;
437 s->peers[j].nb_eventfds = 0;
440 return 0;
443 static bool fifo_update_and_get(IVShmemState *s, const uint8_t *buf, int size,
444 void *data, size_t len)
446 const uint8_t *p;
447 uint32_t num;
449 assert(len <= sizeof(long)); /* limitation of the fifo */
450 if (fifo8_is_empty(&s->incoming_fifo) && size == len) {
451 memcpy(data, buf, size);
452 return true;
455 IVSHMEM_DPRINTF("short read of %d bytes\n", size);
457 num = MIN(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo));
458 fifo8_push_all(&s->incoming_fifo, buf, num);
460 if (fifo8_num_used(&s->incoming_fifo) < len) {
461 assert(num == 0);
462 return false;
465 size -= num;
466 buf += num;
467 p = fifo8_pop_buf(&s->incoming_fifo, len, &num);
468 assert(num == len);
470 memcpy(data, p, len);
472 if (size > 0) {
473 fifo8_push_all(&s->incoming_fifo, buf, size);
476 return true;
479 static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
481 IVShmemState *s = opaque;
482 int incoming_fd;
483 int guest_max_eventfd;
484 long incoming_posn;
486 if (!fifo_update_and_get(s, buf, size,
487 &incoming_posn, sizeof(incoming_posn))) {
488 return;
491 if (incoming_posn < -1) {
492 IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn);
493 return;
496 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
497 incoming_fd = qemu_chr_fe_get_msgfd(s->server_chr);
498 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, incoming_fd);
500 /* make sure we have enough space for this guest */
501 if (incoming_posn >= s->nb_peers) {
502 if (increase_dynamic_storage(s, incoming_posn) < 0) {
503 error_report("increase_dynamic_storage() failed");
504 if (incoming_fd != -1) {
505 close(incoming_fd);
507 return;
511 if (incoming_fd == -1) {
512 /* if posn is positive and unseen before then this is our posn*/
513 if ((incoming_posn >= 0) &&
514 (s->peers[incoming_posn].eventfds == NULL)) {
515 /* receive our posn */
516 s->vm_id = incoming_posn;
517 return;
518 } else {
519 /* otherwise an fd == -1 means an existing guest has gone away */
520 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
521 close_guest_eventfds(s, incoming_posn);
522 return;
526 /* if the position is -1, then it's shared memory region fd */
527 if (incoming_posn == -1) {
529 void * map_ptr;
531 s->max_peer = 0;
533 if (check_shm_size(s, incoming_fd) == -1) {
534 exit(1);
537 /* mmap the region and map into the BAR2 */
538 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
539 incoming_fd, 0);
540 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
541 "ivshmem.bar2", s->ivshmem_size, map_ptr);
542 vmstate_register_ram(&s->ivshmem, DEVICE(s));
544 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",
545 map_ptr, s->ivshmem_size);
547 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
549 /* only store the fd if it is successfully mapped */
550 s->shm_fd = incoming_fd;
552 return;
555 /* each guest has an array of eventfds, and we keep track of how many
556 * guests for each VM */
557 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
559 if (guest_max_eventfd == 0) {
560 /* one eventfd per MSI vector */
561 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
564 /* this is an eventfd for a particular guest VM */
565 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
566 guest_max_eventfd, incoming_fd);
567 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
568 incoming_fd);
570 /* increment count for particular guest */
571 s->peers[incoming_posn].nb_eventfds++;
573 /* keep track of the maximum VM ID */
574 if (incoming_posn > s->max_peer) {
575 s->max_peer = incoming_posn;
578 if (incoming_posn == s->vm_id) {
579 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
580 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
581 guest_max_eventfd);
584 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
585 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
589 /* Select the MSI-X vectors used by device.
590 * ivshmem maps events to vectors statically, so
591 * we just enable all vectors on init and after reset. */
592 static void ivshmem_use_msix(IVShmemState * s)
594 PCIDevice *d = PCI_DEVICE(s);
595 int i;
597 if (!msix_present(d)) {
598 return;
601 for (i = 0; i < s->vectors; i++) {
602 msix_vector_use(d, i);
606 static void ivshmem_reset(DeviceState *d)
608 IVShmemState *s = IVSHMEM(d);
610 s->intrstatus = 0;
611 ivshmem_use_msix(s);
614 static uint64_t ivshmem_get_size(IVShmemState * s) {
616 uint64_t value;
617 char *ptr;
619 value = strtoull(s->sizearg, &ptr, 10);
620 switch (*ptr) {
621 case 0: case 'M': case 'm':
622 value <<= 20;
623 break;
624 case 'G': case 'g':
625 value <<= 30;
626 break;
627 default:
628 error_report("invalid ram size: %s", s->sizearg);
629 exit(1);
632 /* BARs must be a power of 2 */
633 if (!is_power_of_two(value)) {
634 error_report("size must be power of 2");
635 exit(1);
638 return value;
641 static void ivshmem_setup_msi(IVShmemState * s)
643 if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
644 IVSHMEM_DPRINTF("msix initialization failed\n");
645 exit(1);
648 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
650 /* allocate QEMU char devices for receiving interrupts */
651 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
653 ivshmem_use_msix(s);
656 static void ivshmem_save(QEMUFile* f, void *opaque)
658 IVShmemState *proxy = opaque;
659 PCIDevice *pci_dev = PCI_DEVICE(proxy);
661 IVSHMEM_DPRINTF("ivshmem_save\n");
662 pci_device_save(pci_dev, f);
664 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
665 msix_save(pci_dev, f);
666 } else {
667 qemu_put_be32(f, proxy->intrstatus);
668 qemu_put_be32(f, proxy->intrmask);
673 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
675 IVSHMEM_DPRINTF("ivshmem_load\n");
677 IVShmemState *proxy = opaque;
678 PCIDevice *pci_dev = PCI_DEVICE(proxy);
679 int ret;
681 if (version_id > 0) {
682 return -EINVAL;
685 if (proxy->role_val == IVSHMEM_PEER) {
686 error_report("'peer' devices are not migratable");
687 return -EINVAL;
690 ret = pci_device_load(pci_dev, f);
691 if (ret) {
692 return ret;
695 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
696 msix_load(pci_dev, f);
697 ivshmem_use_msix(proxy);
698 } else {
699 proxy->intrstatus = qemu_get_be32(f);
700 proxy->intrmask = qemu_get_be32(f);
703 return 0;
706 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
707 uint32_t val, int len)
709 pci_default_write_config(pci_dev, address, val, len);
712 static int pci_ivshmem_init(PCIDevice *dev)
714 IVShmemState *s = IVSHMEM(dev);
715 uint8_t *pci_conf;
716 uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
717 PCI_BASE_ADDRESS_MEM_PREFETCH;
719 if (s->sizearg == NULL)
720 s->ivshmem_size = 4 << 20; /* 4 MB default */
721 else {
722 s->ivshmem_size = ivshmem_get_size(s);
725 fifo8_create(&s->incoming_fifo, sizeof(long));
727 register_savevm(DEVICE(dev), "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
728 dev);
730 /* IRQFD requires MSI */
731 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
732 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
733 error_report("ioeventfd/irqfd requires MSI");
734 exit(1);
737 /* check that role is reasonable */
738 if (s->role) {
739 if (strncmp(s->role, "peer", 5) == 0) {
740 s->role_val = IVSHMEM_PEER;
741 } else if (strncmp(s->role, "master", 7) == 0) {
742 s->role_val = IVSHMEM_MASTER;
743 } else {
744 error_report("'role' must be 'peer' or 'master'");
745 exit(1);
747 } else {
748 s->role_val = IVSHMEM_MASTER; /* default */
751 if (s->role_val == IVSHMEM_PEER) {
752 error_setg(&s->migration_blocker,
753 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
754 migrate_add_blocker(s->migration_blocker);
757 pci_conf = dev->config;
758 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
760 pci_config_set_interrupt_pin(pci_conf, 1);
762 s->shm_fd = 0;
764 memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
765 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
767 /* region for registers*/
768 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
769 &s->ivshmem_mmio);
771 memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
772 if (s->ivshmem_64bit) {
773 attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
776 if ((s->server_chr != NULL) &&
777 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
778 /* if we get a UNIX socket as the parameter we will talk
779 * to the ivshmem server to receive the memory region */
781 if (s->shmobj != NULL) {
782 error_report("WARNING: do not specify both 'chardev' "
783 "and 'shm' with ivshmem");
786 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
787 s->server_chr->filename);
789 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
790 ivshmem_setup_msi(s);
793 /* we allocate enough space for 16 guests and grow as needed */
794 s->nb_peers = 16;
795 s->vm_id = -1;
797 /* allocate/initialize space for interrupt handling */
798 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
800 pci_register_bar(dev, 2, attr, &s->bar);
802 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
804 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
805 ivshmem_event, s);
806 } else {
807 /* just map the file immediately, we're not using a server */
808 int fd;
810 if (s->shmobj == NULL) {
811 error_report("Must specify 'chardev' or 'shm' to ivshmem");
812 exit(1);
815 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
817 /* try opening with O_EXCL and if it succeeds zero the memory
818 * by truncating to 0 */
819 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
820 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
821 /* truncate file to length PCI device's memory */
822 if (ftruncate(fd, s->ivshmem_size) != 0) {
823 error_report("could not truncate shared file");
826 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
827 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
828 error_report("could not open shared file");
829 exit(1);
833 if (check_shm_size(s, fd) == -1) {
834 exit(1);
837 create_shared_memory_BAR(s, fd, attr);
840 dev->config_write = ivshmem_write_config;
842 return 0;
845 static void pci_ivshmem_uninit(PCIDevice *dev)
847 IVShmemState *s = IVSHMEM(dev);
849 if (s->migration_blocker) {
850 migrate_del_blocker(s->migration_blocker);
851 error_free(s->migration_blocker);
854 memory_region_del_subregion(&s->bar, &s->ivshmem);
855 vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
856 unregister_savevm(DEVICE(dev), "ivshmem", s);
857 fifo8_destroy(&s->incoming_fifo);
860 static Property ivshmem_properties[] = {
861 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
862 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
863 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
864 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
865 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
866 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
867 DEFINE_PROP_STRING("role", IVShmemState, role),
868 DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
869 DEFINE_PROP_END_OF_LIST(),
872 static void ivshmem_class_init(ObjectClass *klass, void *data)
874 DeviceClass *dc = DEVICE_CLASS(klass);
875 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
877 k->init = pci_ivshmem_init;
878 k->exit = pci_ivshmem_uninit;
879 k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
880 k->device_id = PCI_DEVICE_ID_IVSHMEM;
881 k->class_id = PCI_CLASS_MEMORY_RAM;
882 dc->reset = ivshmem_reset;
883 dc->props = ivshmem_properties;
884 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
887 static const TypeInfo ivshmem_info = {
888 .name = TYPE_IVSHMEM,
889 .parent = TYPE_PCI_DEVICE,
890 .instance_size = sizeof(IVShmemState),
891 .class_init = ivshmem_class_init,
894 static void ivshmem_register_types(void)
896 type_register_static(&ivshmem_info);
899 type_init(ivshmem_register_types)