From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42035) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agxmS-0006pE-Hg for qemu-devel@nongnu.org; Fri, 18 Mar 2016 13:01:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1agxmN-0008BW-Mw for qemu-devel@nongnu.org; Fri, 18 Mar 2016 13:01:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49177) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agxmN-0008B9-Cf for qemu-devel@nongnu.org; Fri, 18 Mar 2016 13:01:39 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id EFC63C003592 for ; Fri, 18 Mar 2016 17:01:38 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-34.ams2.redhat.com [10.36.116.34]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2IH1ZVf010122 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Fri, 18 Mar 2016 13:01:37 -0400 From: Markus Armbruster Date: Fri, 18 Mar 2016 18:01:24 +0100 Message-Id: <1458320487-19603-38-git-send-email-armbru@redhat.com> In-Reply-To: <1458320487-19603-1-git-send-email-armbru@redhat.com> References: <1458320487-19603-1-git-send-email-armbru@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 37/40] ivshmem: Clean up after the previous commit List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Move code to more sensible places. Use the opportunity to reorder and document IVShmemState members. Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau Message-Id: <1458066895-20632-38-git-send-email-armbru@redhat.com> --- hw/misc/ivshmem.c | 400 +++++++++++++++++++++++++++---------------------= ------ 1 file changed, 203 insertions(+), 197 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 3829155..49e363e 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -85,26 +85,31 @@ typedef struct IVShmemState { PCIDevice parent_obj; /*< public >*/ =20 - HostMemoryBackend *hostmem; + uint32_t features; + + /* exactly one of these two may be set */ + HostMemoryBackend *hostmem; /* with interrupts */ + CharDriverState *server_chr; /* without interrupts */ + + /* registers */ uint32_t intrmask; uint32_t intrstatus; + int vm_id; =20 - CharDriverState *server_chr; - MemoryRegion ivshmem_mmio; - + /* BARs */ + MemoryRegion ivshmem_mmio; /* BAR 0 (registers) */ MemoryRegion *ivshmem_bar2; /* BAR 2 (shared memory) */ MemoryRegion server_bar2; /* used with server_chr */ =20 + /* interrupt support */ Peer *peers; int nb_peers; /* space in @peers[] */ - - int vm_id; uint32_t vectors; - uint32_t features; MSIVector *msi_vectors; uint64_t msg_buf; /* buffer for receiving server messages = */ int msg_buffered_bytes; /* #bytes in @msg_buf */ =20 + /* migration stuff */ OnOffAuto master; Error *migration_blocker; =20 @@ -817,23 +822,6 @@ static void ivshmem_write_config(PCIDevice *pdev, ui= nt32_t address, } } =20 -static void desugar_shm(IVShmemState *s) -{ - Object *obj; - char *path; - - obj =3D object_new("memory-backend-file"); - path =3D g_strdup_printf("/dev/shm/%s", s->shmobj); - object_property_set_str(obj, path, "mem-path", &error_abort); - g_free(path); - object_property_set_int(obj, s->legacy_size, "size", &error_abort); - object_property_set_bool(obj, true, "share", &error_abort); - object_property_add_child(OBJECT(s), "internal-shm-backend", obj, - &error_abort); - user_creatable_complete(obj, &error_abort); - s->hostmem =3D MEMORY_BACKEND(obj); -} - static void ivshmem_common_realize(PCIDevice *dev, Error **errp) { IVShmemState *s =3D IVSHMEM_COMMON(dev); @@ -909,65 +897,6 @@ static void ivshmem_common_realize(PCIDevice *dev, E= rror **errp) } } =20 -static void ivshmem_realize(PCIDevice *dev, Error **errp) -{ - IVShmemState *s =3D IVSHMEM_COMMON(dev); - - if (!qtest_enabled()) { - error_report("ivshmem is deprecated, please use ivshmem-plain" - " or ivshmem-doorbell instead"); - } - - if (!!s->server_chr + !!s->shmobj + !!s->hostmem !=3D 1) { - error_setg(errp, - "You must specify either 'shm', 'chardev' or 'x-memde= v'"); - return; - } - - if (s->hostmem) { - if (s->sizearg) { - g_warning("size argument ignored with hostmem"); - } - } else if (s->sizearg =3D=3D NULL) { - s->legacy_size =3D 4 << 20; /* 4 MB default */ - } else { - char *end; - int64_t size =3D qemu_strtosz(s->sizearg, &end); - if (size < 0 || (size_t)size !=3D size || *end !=3D '\0' - || !is_power_of_2(size)) { - error_setg(errp, "Invalid size %s", s->sizearg); - return; - } - s->legacy_size =3D size; - } - - /* check that role is reasonable */ - if (s->role) { - if (strncmp(s->role, "peer", 5) =3D=3D 0) { - s->master =3D ON_OFF_AUTO_OFF; - } else if (strncmp(s->role, "master", 7) =3D=3D 0) { - s->master =3D ON_OFF_AUTO_ON; - } else { - error_setg(errp, "'role' must be 'peer' or 'master'"); - return; - } - } else { - s->master =3D ON_OFF_AUTO_AUTO; - } - - if (s->shmobj) { - desugar_shm(s); - } - - /* - * Note: we don't use INTx with IVSHMEM_MSI at all, so this is a - * bald-faced lie then. But it's a backwards compatible lie. - */ - pci_config_set_interrupt_pin(dev->config, 1); - - ivshmem_common_realize(dev, errp); -} - static void ivshmem_exit(PCIDevice *dev) { IVShmemState *s =3D IVSHMEM_COMMON(dev); @@ -1009,18 +938,6 @@ static void ivshmem_exit(PCIDevice *dev) g_free(s->msi_vectors); } =20 -static bool test_msix(void *opaque, int version_id) -{ - IVShmemState *s =3D opaque; - - return ivshmem_has_feature(s, IVSHMEM_MSI); -} - -static bool test_no_msix(void *opaque, int version_id) -{ - return !test_msix(opaque, version_id); -} - static int ivshmem_pre_load(void *opaque) { IVShmemState *s =3D opaque; @@ -1043,70 +960,6 @@ static int ivshmem_post_load(void *opaque, int vers= ion_id) return 0; } =20 -static int ivshmem_load_old(QEMUFile *f, void *opaque, int version_id) -{ - IVShmemState *s =3D opaque; - PCIDevice *pdev =3D PCI_DEVICE(s); - int ret; - - IVSHMEM_DPRINTF("ivshmem_load_old\n"); - - if (version_id !=3D 0) { - return -EINVAL; - } - - ret =3D ivshmem_pre_load(s); - if (ret) { - return ret; - } - - ret =3D pci_device_load(pdev, f); - if (ret) { - return ret; - } - - if (ivshmem_has_feature(s, IVSHMEM_MSI)) { - msix_load(pdev, f); - ivshmem_msix_vector_use(s); - } else { - s->intrstatus =3D qemu_get_be32(f); - s->intrmask =3D qemu_get_be32(f); - } - - return 0; -} - -static const VMStateDescription ivshmem_vmsd =3D { - .name =3D "ivshmem", - .version_id =3D 1, - .minimum_version_id =3D 1, - .pre_load =3D ivshmem_pre_load, - .post_load =3D ivshmem_post_load, - .fields =3D (VMStateField[]) { - VMSTATE_PCI_DEVICE(parent_obj, IVShmemState), - - VMSTATE_MSIX_TEST(parent_obj, IVShmemState, test_msix), - VMSTATE_UINT32_TEST(intrstatus, IVShmemState, test_no_msix), - VMSTATE_UINT32_TEST(intrmask, IVShmemState, test_no_msix), - - VMSTATE_END_OF_LIST() - }, - .load_state_old =3D ivshmem_load_old, - .minimum_version_id_old =3D 0 -}; - -static Property ivshmem_properties[] =3D { - DEFINE_PROP_CHR("chardev", IVShmemState, server_chr), - DEFINE_PROP_STRING("size", IVShmemState, sizearg), - DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1), - DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENT= FD, false), - DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true), - DEFINE_PROP_STRING("shm", IVShmemState, shmobj), - DEFINE_PROP_STRING("role", IVShmemState, role), - DEFINE_PROP_UINT32("use64", IVShmemState, not_legacy_32bit, 1), - DEFINE_PROP_END_OF_LIST(), -}; - static void ivshmem_common_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); @@ -1124,17 +977,13 @@ static void ivshmem_common_class_init(ObjectClass = *klass, void *data) dc->desc =3D "Inter-VM shared memory"; } =20 -static void ivshmem_class_init(ObjectClass *klass, void *data) -{ - DeviceClass *dc =3D DEVICE_CLASS(klass); - PCIDeviceClass *k =3D PCI_DEVICE_CLASS(klass); - - k->realize =3D ivshmem_realize; - k->revision =3D 0; - dc->desc =3D "Inter-VM shared memory (legacy)"; - dc->props =3D ivshmem_properties; - dc->vmsd =3D &ivshmem_vmsd; -} +static const TypeInfo ivshmem_common_info =3D { + .name =3D TYPE_IVSHMEM_COMMON, + .parent =3D TYPE_PCI_DEVICE, + .instance_size =3D sizeof(IVShmemState), + .abstract =3D true, + .class_init =3D ivshmem_common_class_init, +}; =20 static void ivshmem_check_memdev_is_busy(Object *obj, const char *name, Object *val, Error **errp) @@ -1151,33 +1000,6 @@ static void ivshmem_check_memdev_is_busy(Object *o= bj, const char *name, } } =20 -static void ivshmem_init(Object *obj) -{ - IVShmemState *s =3D IVSHMEM(obj); - - object_property_add_link(obj, "x-memdev", TYPE_MEMORY_BACKEND, - (Object **)&s->hostmem, - ivshmem_check_memdev_is_busy, - OBJ_PROP_LINK_UNREF_ON_RELEASE, - &error_abort); -} - -static const TypeInfo ivshmem_common_info =3D { - .name =3D TYPE_IVSHMEM_COMMON, - .parent =3D TYPE_PCI_DEVICE, - .instance_size =3D sizeof(IVShmemState), - .abstract =3D true, - .class_init =3D ivshmem_common_class_init, -}; - -static const TypeInfo ivshmem_info =3D { - .name =3D TYPE_IVSHMEM, - .parent =3D TYPE_IVSHMEM_COMMON, - .instance_size =3D sizeof(IVShmemState), - .instance_init =3D ivshmem_init, - .class_init =3D ivshmem_class_init, -}; - static const VMStateDescription ivshmem_plain_vmsd =3D { .name =3D TYPE_IVSHMEM_PLAIN, .version_id =3D 0, @@ -1272,6 +1094,190 @@ static const TypeInfo ivshmem_doorbell_info =3D { .class_init =3D ivshmem_doorbell_class_init, }; =20 +static int ivshmem_load_old(QEMUFile *f, void *opaque, int version_id) +{ + IVShmemState *s =3D opaque; + PCIDevice *pdev =3D PCI_DEVICE(s); + int ret; + + IVSHMEM_DPRINTF("ivshmem_load_old\n"); + + if (version_id !=3D 0) { + return -EINVAL; + } + + ret =3D ivshmem_pre_load(s); + if (ret) { + return ret; + } + + ret =3D pci_device_load(pdev, f); + if (ret) { + return ret; + } + + if (ivshmem_has_feature(s, IVSHMEM_MSI)) { + msix_load(pdev, f); + ivshmem_msix_vector_use(s); + } else { + s->intrstatus =3D qemu_get_be32(f); + s->intrmask =3D qemu_get_be32(f); + } + + return 0; +} + +static bool test_msix(void *opaque, int version_id) +{ + IVShmemState *s =3D opaque; + + return ivshmem_has_feature(s, IVSHMEM_MSI); +} + +static bool test_no_msix(void *opaque, int version_id) +{ + return !test_msix(opaque, version_id); +} + +static const VMStateDescription ivshmem_vmsd =3D { + .name =3D "ivshmem", + .version_id =3D 1, + .minimum_version_id =3D 1, + .pre_load =3D ivshmem_pre_load, + .post_load =3D ivshmem_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_PCI_DEVICE(parent_obj, IVShmemState), + + VMSTATE_MSIX_TEST(parent_obj, IVShmemState, test_msix), + VMSTATE_UINT32_TEST(intrstatus, IVShmemState, test_no_msix), + VMSTATE_UINT32_TEST(intrmask, IVShmemState, test_no_msix), + + VMSTATE_END_OF_LIST() + }, + .load_state_old =3D ivshmem_load_old, + .minimum_version_id_old =3D 0 +}; + +static Property ivshmem_properties[] =3D { + DEFINE_PROP_CHR("chardev", IVShmemState, server_chr), + DEFINE_PROP_STRING("size", IVShmemState, sizearg), + DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1), + DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENT= FD, + false), + DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true), + DEFINE_PROP_STRING("shm", IVShmemState, shmobj), + DEFINE_PROP_STRING("role", IVShmemState, role), + DEFINE_PROP_UINT32("use64", IVShmemState, not_legacy_32bit, 1), + DEFINE_PROP_END_OF_LIST(), +}; + +static void desugar_shm(IVShmemState *s) +{ + Object *obj; + char *path; + + obj =3D object_new("memory-backend-file"); + path =3D g_strdup_printf("/dev/shm/%s", s->shmobj); + object_property_set_str(obj, path, "mem-path", &error_abort); + g_free(path); + object_property_set_int(obj, s->legacy_size, "size", &error_abort); + object_property_set_bool(obj, true, "share", &error_abort); + object_property_add_child(OBJECT(s), "internal-shm-backend", obj, + &error_abort); + user_creatable_complete(obj, &error_abort); + s->hostmem =3D MEMORY_BACKEND(obj); +} + +static void ivshmem_realize(PCIDevice *dev, Error **errp) +{ + IVShmemState *s =3D IVSHMEM_COMMON(dev); + + if (!qtest_enabled()) { + error_report("ivshmem is deprecated, please use ivshmem-plain" + " or ivshmem-doorbell instead"); + } + + if (!!s->server_chr + !!s->shmobj + !!s->hostmem !=3D 1) { + error_setg(errp, + "You must specify either 'shm', 'chardev' or 'x-memde= v'"); + return; + } + + if (s->hostmem) { + if (s->sizearg) { + g_warning("size argument ignored with hostmem"); + } + } else if (s->sizearg =3D=3D NULL) { + s->legacy_size =3D 4 << 20; /* 4 MB default */ + } else { + char *end; + int64_t size =3D qemu_strtosz(s->sizearg, &end); + if (size < 0 || (size_t)size !=3D size || *end !=3D '\0' + || !is_power_of_2(size)) { + error_setg(errp, "Invalid size %s", s->sizearg); + return; + } + s->legacy_size =3D size; + } + + /* check that role is reasonable */ + if (s->role) { + if (strncmp(s->role, "peer", 5) =3D=3D 0) { + s->master =3D ON_OFF_AUTO_OFF; + } else if (strncmp(s->role, "master", 7) =3D=3D 0) { + s->master =3D ON_OFF_AUTO_ON; + } else { + error_setg(errp, "'role' must be 'peer' or 'master'"); + return; + } + } else { + s->master =3D ON_OFF_AUTO_AUTO; + } + + if (s->shmobj) { + desugar_shm(s); + } + + /* + * Note: we don't use INTx with IVSHMEM_MSI at all, so this is a + * bald-faced lie then. But it's a backwards compatible lie. + */ + pci_config_set_interrupt_pin(dev->config, 1); + + ivshmem_common_realize(dev, errp); +} + +static void ivshmem_init(Object *obj) +{ + IVShmemState *s =3D IVSHMEM(obj); + + object_property_add_link(obj, "x-memdev", TYPE_MEMORY_BACKEND, + (Object **)&s->hostmem, + ivshmem_check_memdev_is_busy, + OBJ_PROP_LINK_UNREF_ON_RELEASE, + &error_abort); +} + +static void ivshmem_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + PCIDeviceClass *k =3D PCI_DEVICE_CLASS(klass); + + k->realize =3D ivshmem_realize; + k->revision =3D 0; + dc->desc =3D "Inter-VM shared memory (legacy)"; + dc->props =3D ivshmem_properties; + dc->vmsd =3D &ivshmem_vmsd; +} + +static const TypeInfo ivshmem_info =3D { + .name =3D TYPE_IVSHMEM, + .parent =3D TYPE_IVSHMEM_COMMON, + .instance_size =3D sizeof(IVShmemState), + .instance_init =3D ivshmem_init, + .class_init =3D ivshmem_class_init, +}; + static void ivshmem_register_types(void) { type_register_static(&ivshmem_common_info); --=20 2.4.3