2
0

virtio-pmem-pci.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include "qemu/osdep.h"
  14. #include "virtio-pmem-pci.h"
  15. #include "hw/mem/memory-device.h"
  16. #include "qapi/error.h"
  17. static void virtio_pmem_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
  18. {
  19. VirtIOPMEMPCI *pmem_pci = VIRTIO_PMEM_PCI(vpci_dev);
  20. DeviceState *vdev = DEVICE(&pmem_pci->vdev);
  21. qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
  22. object_property_set_bool(OBJECT(vdev), true, "realized", errp);
  23. }
  24. static void virtio_pmem_pci_set_addr(MemoryDeviceState *md, uint64_t addr,
  25. Error **errp)
  26. {
  27. object_property_set_uint(OBJECT(md), addr, VIRTIO_PMEM_ADDR_PROP, errp);
  28. }
  29. static uint64_t virtio_pmem_pci_get_addr(const MemoryDeviceState *md)
  30. {
  31. return object_property_get_uint(OBJECT(md), VIRTIO_PMEM_ADDR_PROP,
  32. &error_abort);
  33. }
  34. static MemoryRegion *virtio_pmem_pci_get_memory_region(MemoryDeviceState *md,
  35. Error **errp)
  36. {
  37. VirtIOPMEMPCI *pci_pmem = VIRTIO_PMEM_PCI(md);
  38. VirtIOPMEM *pmem = VIRTIO_PMEM(&pci_pmem->vdev);
  39. VirtIOPMEMClass *vpc = VIRTIO_PMEM_GET_CLASS(pmem);
  40. return vpc->get_memory_region(pmem, errp);
  41. }
  42. static uint64_t virtio_pmem_pci_get_plugged_size(const MemoryDeviceState *md,
  43. Error **errp)
  44. {
  45. VirtIOPMEMPCI *pci_pmem = VIRTIO_PMEM_PCI(md);
  46. VirtIOPMEM *pmem = VIRTIO_PMEM(&pci_pmem->vdev);
  47. VirtIOPMEMClass *vpc = VIRTIO_PMEM_GET_CLASS(pmem);
  48. MemoryRegion *mr = vpc->get_memory_region(pmem, errp);
  49. /* the plugged size corresponds to the region size */
  50. return mr ? memory_region_size(mr) : 0;
  51. }
  52. static void virtio_pmem_pci_fill_device_info(const MemoryDeviceState *md,
  53. MemoryDeviceInfo *info)
  54. {
  55. VirtioPMEMDeviceInfo *vi = g_new0(VirtioPMEMDeviceInfo, 1);
  56. VirtIOPMEMPCI *pci_pmem = VIRTIO_PMEM_PCI(md);
  57. VirtIOPMEM *pmem = VIRTIO_PMEM(&pci_pmem->vdev);
  58. VirtIOPMEMClass *vpc = VIRTIO_PMEM_GET_CLASS(pmem);
  59. DeviceState *dev = DEVICE(md);
  60. if (dev->id) {
  61. vi->has_id = true;
  62. vi->id = g_strdup(dev->id);
  63. }
  64. /* let the real device handle everything else */
  65. vpc->fill_device_info(pmem, vi);
  66. info->u.virtio_pmem.data = vi;
  67. info->type = MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM;
  68. }
  69. static void virtio_pmem_pci_class_init(ObjectClass *klass, void *data)
  70. {
  71. DeviceClass *dc = DEVICE_CLASS(klass);
  72. VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
  73. PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
  74. MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(klass);
  75. k->realize = virtio_pmem_pci_realize;
  76. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  77. pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  78. pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_PMEM;
  79. pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
  80. pcidev_k->class_id = PCI_CLASS_OTHERS;
  81. mdc->get_addr = virtio_pmem_pci_get_addr;
  82. mdc->set_addr = virtio_pmem_pci_set_addr;
  83. mdc->get_plugged_size = virtio_pmem_pci_get_plugged_size;
  84. mdc->get_memory_region = virtio_pmem_pci_get_memory_region;
  85. mdc->fill_device_info = virtio_pmem_pci_fill_device_info;
  86. }
  87. static void virtio_pmem_pci_instance_init(Object *obj)
  88. {
  89. VirtIOPMEMPCI *dev = VIRTIO_PMEM_PCI(obj);
  90. virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
  91. TYPE_VIRTIO_PMEM);
  92. }
  93. static const VirtioPCIDeviceTypeInfo virtio_pmem_pci_info = {
  94. .base_name = TYPE_VIRTIO_PMEM_PCI,
  95. .generic_name = "virtio-pmem-pci",
  96. .instance_size = sizeof(VirtIOPMEMPCI),
  97. .instance_init = virtio_pmem_pci_instance_init,
  98. .class_init = virtio_pmem_pci_class_init,
  99. .interfaces = (InterfaceInfo[]) {
  100. { TYPE_MEMORY_DEVICE },
  101. { }
  102. },
  103. };
  104. static void virtio_pmem_pci_register_types(void)
  105. {
  106. virtio_pci_types_register(&virtio_pmem_pci_info);
  107. }
  108. type_init(virtio_pmem_pci_register_types)