virtio-pmem-pci.c 4.1 KB

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