xen_pvdevice.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Copyright (c) Citrix Systems Inc.
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms,
  5. * with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the
  10. * following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the
  13. * following disclaimer in the documentation and/or other
  14. * materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  17. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "qemu/osdep.h"
  32. #include "qapi/error.h"
  33. #include "qemu/module.h"
  34. #include "hw/pci/pci_device.h"
  35. #include "hw/qdev-properties.h"
  36. #include "migration/vmstate.h"
  37. #include "trace.h"
  38. #include "qom/object.h"
  39. #define TYPE_XEN_PV_DEVICE "xen-pvdevice"
  40. OBJECT_DECLARE_SIMPLE_TYPE(XenPVDevice, XEN_PV_DEVICE)
  41. struct XenPVDevice {
  42. /*< private >*/
  43. PCIDevice parent_obj;
  44. /*< public >*/
  45. uint16_t vendor_id;
  46. uint16_t device_id;
  47. uint8_t revision;
  48. uint32_t size;
  49. MemoryRegion mmio;
  50. };
  51. static uint64_t xen_pv_mmio_read(void *opaque, hwaddr addr,
  52. unsigned size)
  53. {
  54. trace_xen_pv_mmio_read(addr);
  55. return ~(uint64_t)0;
  56. }
  57. static void xen_pv_mmio_write(void *opaque, hwaddr addr,
  58. uint64_t val, unsigned size)
  59. {
  60. trace_xen_pv_mmio_write(addr);
  61. }
  62. static const MemoryRegionOps xen_pv_mmio_ops = {
  63. .read = &xen_pv_mmio_read,
  64. .write = &xen_pv_mmio_write,
  65. .endianness = DEVICE_LITTLE_ENDIAN,
  66. };
  67. static const VMStateDescription vmstate_xen_pvdevice = {
  68. .name = "xen-pvdevice",
  69. .version_id = 1,
  70. .minimum_version_id = 1,
  71. .fields = (const VMStateField[]) {
  72. VMSTATE_PCI_DEVICE(parent_obj, XenPVDevice),
  73. VMSTATE_END_OF_LIST()
  74. }
  75. };
  76. static void xen_pv_realize(PCIDevice *pci_dev, Error **errp)
  77. {
  78. XenPVDevice *d = XEN_PV_DEVICE(pci_dev);
  79. uint8_t *pci_conf;
  80. /* device-id property must always be supplied */
  81. if (d->device_id == 0xffff) {
  82. error_setg(errp, "Device ID invalid, it must always be supplied");
  83. return;
  84. }
  85. pci_conf = pci_dev->config;
  86. pci_set_word(pci_conf + PCI_VENDOR_ID, d->vendor_id);
  87. pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, d->vendor_id);
  88. pci_set_word(pci_conf + PCI_DEVICE_ID, d->device_id);
  89. pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, d->device_id);
  90. pci_set_byte(pci_conf + PCI_REVISION_ID, d->revision);
  91. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_MEMORY);
  92. pci_config_set_prog_interface(pci_conf, 0);
  93. pci_conf[PCI_INTERRUPT_PIN] = 1;
  94. memory_region_init_io(&d->mmio, NULL, &xen_pv_mmio_ops, d,
  95. "mmio", d->size);
  96. pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
  97. &d->mmio);
  98. }
  99. static const Property xen_pv_props[] = {
  100. DEFINE_PROP_UINT16("vendor-id", XenPVDevice, vendor_id, PCI_VENDOR_ID_XEN),
  101. DEFINE_PROP_UINT16("device-id", XenPVDevice, device_id, 0xffff),
  102. DEFINE_PROP_UINT8("revision", XenPVDevice, revision, 0x01),
  103. DEFINE_PROP_UINT32("size", XenPVDevice, size, 0x400000),
  104. };
  105. static void xen_pv_class_init(ObjectClass *klass, void *data)
  106. {
  107. DeviceClass *dc = DEVICE_CLASS(klass);
  108. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  109. k->realize = xen_pv_realize;
  110. k->class_id = PCI_CLASS_SYSTEM_OTHER;
  111. dc->desc = "Xen PV Device";
  112. device_class_set_props(dc, xen_pv_props);
  113. dc->vmsd = &vmstate_xen_pvdevice;
  114. }
  115. static const TypeInfo xen_pv_type_info = {
  116. .name = TYPE_XEN_PV_DEVICE,
  117. .parent = TYPE_PCI_DEVICE,
  118. .instance_size = sizeof(XenPVDevice),
  119. .class_init = xen_pv_class_init,
  120. .interfaces = (InterfaceInfo[]) {
  121. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  122. { },
  123. },
  124. };
  125. static void xen_pv_register_types(void)
  126. {
  127. type_register_static(&xen_pv_type_info);
  128. }
  129. type_init(xen_pv_register_types)