| 1 | /* |
| 2 | * Virtio PMEM PCI device |
| 3 | * |
| 4 | * Copyright (C) 2018-2019 Red Hat, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Pankaj Gupta <pagupta@redhat.com> |
| 8 | * David Hildenbrand <david@redhat.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2. |
| 11 | * See the COPYING file in the top-level directory. |
| 12 | */ |
| 13 | |
| 14 | #include "qemu/osdep.h" |
| 15 | |
| 16 | #include "virtio-pmem-pci.h" |
| 17 | #include "hw/mem/memory-device.h" |
| 18 | #include "qapi/error.h" |
| 19 | |
| 20 | static void virtio_pmem_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) |
| 21 | { |
| 22 | VirtIOPMEMPCI *pmem_pci = VIRTIO_PMEM_PCI(vpci_dev); |
| 23 | DeviceState *vdev = DEVICE(&pmem_pci->vdev); |
| 24 | |
| 25 | virtio_pci_force_virtio_1(vpci_dev); |
| 26 | qdev_realize(vdev, BUS(&vpci_dev->bus), errp); |
| 27 | } |
| 28 | |
| 29 | static void virtio_pmem_pci_set_addr(MemoryDeviceState *md, uint64_t addr, |
| 30 | Error **errp) |
| 31 | { |
| 32 | object_property_set_uint(OBJECT(md), VIRTIO_PMEM_ADDR_PROP, addr, errp); |
| 33 | } |
| 34 | |
| 35 | static uint64_t virtio_pmem_pci_get_addr(const MemoryDeviceState *md) |
| 36 | { |
| 37 | return object_property_get_uint(OBJECT(md), VIRTIO_PMEM_ADDR_PROP, |
| 38 | &error_abort); |
| 39 | } |
| 40 | |
| 41 | static MemoryRegion *virtio_pmem_pci_get_memory_region(MemoryDeviceState *md, |
| 42 | Error **errp) |
| 43 | { |
| 44 | VirtIOPMEMPCI *pci_pmem = VIRTIO_PMEM_PCI(md); |
| 45 | VirtIOPMEM *pmem = &pci_pmem->vdev; |
| 46 | VirtIOPMEMClass *vpc = VIRTIO_PMEM_GET_CLASS(pmem); |
| 47 | |
| 48 | return vpc->get_memory_region(pmem, errp); |
| 49 | } |
| 50 | |
| 51 | static uint64_t virtio_pmem_pci_get_plugged_size(const MemoryDeviceState *md, |
| 52 | Error **errp) |
| 53 | { |
| 54 | VirtIOPMEMPCI *pci_pmem = VIRTIO_PMEM_PCI(md); |
| 55 | VirtIOPMEM *pmem = &pci_pmem->vdev; |
| 56 | VirtIOPMEMClass *vpc = VIRTIO_PMEM_GET_CLASS(pmem); |
| 57 | MemoryRegion *mr = vpc->get_memory_region(pmem, errp); |
| 58 | |
| 59 | /* the plugged size corresponds to the region size */ |
| 60 | return mr ? memory_region_size(mr) : 0; |
| 61 | } |
| 62 | |
| 63 | static void virtio_pmem_pci_fill_device_info(const MemoryDeviceState *md, |
| 64 | MemoryDeviceInfo *info) |
| 65 | { |
| 66 | VirtioPMEMDeviceInfo *vi = g_new0(VirtioPMEMDeviceInfo, 1); |
| 67 | VirtIOPMEMPCI *pci_pmem = VIRTIO_PMEM_PCI(md); |
| 68 | VirtIOPMEM *pmem = &pci_pmem->vdev; |
| 69 | VirtIOPMEMClass *vpc = VIRTIO_PMEM_GET_CLASS(pmem); |
| 70 | DeviceState *dev = DEVICE(md); |
| 71 | |
| 72 | if (dev->id) { |
| 73 | vi->id = g_strdup(dev->id); |
| 74 | } |
| 75 | |
| 76 | /* let the real device handle everything else */ |
| 77 | vpc->fill_device_info(pmem, vi); |
| 78 | |
| 79 | info->u.virtio_pmem.data = vi; |
| 80 | info->type = MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM; |
| 81 | } |
| 82 | |
| 83 | static void virtio_pmem_pci_class_init(ObjectClass *klass, const void *data) |
| 84 | { |
| 85 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 86 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); |
| 87 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
| 88 | MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(klass); |
| 89 | |
| 90 | k->realize = virtio_pmem_pci_realize; |
| 91 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 92 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; |
| 93 | pcidev_k->class_id = PCI_CLASS_OTHERS; |
| 94 | |
| 95 | mdc->get_addr = virtio_pmem_pci_get_addr; |
| 96 | mdc->set_addr = virtio_pmem_pci_set_addr; |
| 97 | mdc->get_plugged_size = virtio_pmem_pci_get_plugged_size; |
| 98 | mdc->get_memory_region = virtio_pmem_pci_get_memory_region; |
| 99 | mdc->fill_device_info = virtio_pmem_pci_fill_device_info; |
| 100 | } |
| 101 | |
| 102 | static void virtio_pmem_pci_instance_init(Object *obj) |
| 103 | { |
| 104 | VirtIOPMEMPCI *dev = VIRTIO_PMEM_PCI(obj); |
| 105 | |
| 106 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), |
| 107 | TYPE_VIRTIO_PMEM); |
| 108 | } |
| 109 | |
| 110 | static const VirtioPCIDeviceTypeInfo virtio_pmem_pci_info = { |
| 111 | .base_name = TYPE_VIRTIO_PMEM_PCI, |
| 112 | .generic_name = "virtio-pmem-pci", |
| 113 | .parent = TYPE_VIRTIO_MD_PCI, |
| 114 | .instance_size = sizeof(VirtIOPMEMPCI), |
| 115 | .instance_init = virtio_pmem_pci_instance_init, |
| 116 | .class_init = virtio_pmem_pci_class_init, |
| 117 | }; |
| 118 | |
| 119 | static void virtio_pmem_pci_register_types(void) |
| 120 | { |
| 121 | virtio_pci_types_register(&virtio_pmem_pci_info); |
| 122 | } |
| 123 | type_init(virtio_pmem_pci_register_types) |