xref: /qemu/hw/i386/xen/xen_pvdevice.c (revision b6a0aa053711e27e1a7825c1fca662beb05bee6f)
18fbab3b6SPaul Durrant /* Copyright (c) Citrix Systems Inc.
28fbab3b6SPaul Durrant  * All rights reserved.
38fbab3b6SPaul Durrant  *
48fbab3b6SPaul Durrant  * Redistribution and use in source and binary forms,
58fbab3b6SPaul Durrant  * with or without modification, are permitted provided
68fbab3b6SPaul Durrant  * that the following conditions are met:
78fbab3b6SPaul Durrant  *
88fbab3b6SPaul Durrant  * *   Redistributions of source code must retain the above
98fbab3b6SPaul Durrant  *     copyright notice, this list of conditions and the
108fbab3b6SPaul Durrant  *     following disclaimer.
118fbab3b6SPaul Durrant  * *   Redistributions in binary form must reproduce the above
128fbab3b6SPaul Durrant  *     copyright notice, this list of conditions and the
138fbab3b6SPaul Durrant  *     following disclaimer in the documentation and/or other
148fbab3b6SPaul Durrant  *     materials provided with the distribution.
158fbab3b6SPaul Durrant  *
168fbab3b6SPaul Durrant  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
178fbab3b6SPaul Durrant  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
188fbab3b6SPaul Durrant  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
198fbab3b6SPaul Durrant  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
208fbab3b6SPaul Durrant  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
218fbab3b6SPaul Durrant  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
228fbab3b6SPaul Durrant  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
238fbab3b6SPaul Durrant  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
248fbab3b6SPaul Durrant  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
258fbab3b6SPaul Durrant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
268fbab3b6SPaul Durrant  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
278fbab3b6SPaul Durrant  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288fbab3b6SPaul Durrant  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fbab3b6SPaul Durrant  * SUCH DAMAGE.
308fbab3b6SPaul Durrant  */
318fbab3b6SPaul Durrant 
32*b6a0aa05SPeter Maydell #include "qemu/osdep.h"
338fbab3b6SPaul Durrant #include "hw/hw.h"
348fbab3b6SPaul Durrant #include "hw/pci/pci.h"
358fbab3b6SPaul Durrant #include "trace.h"
368fbab3b6SPaul Durrant 
378fbab3b6SPaul Durrant #define TYPE_XEN_PV_DEVICE  "xen-pvdevice"
388fbab3b6SPaul Durrant 
398fbab3b6SPaul Durrant #define XEN_PV_DEVICE(obj) \
408fbab3b6SPaul Durrant      OBJECT_CHECK(XenPVDevice, (obj), TYPE_XEN_PV_DEVICE)
418fbab3b6SPaul Durrant 
428fbab3b6SPaul Durrant typedef struct XenPVDevice {
438fbab3b6SPaul Durrant     /*< private >*/
448fbab3b6SPaul Durrant     PCIDevice       parent_obj;
458fbab3b6SPaul Durrant     /*< public >*/
468fbab3b6SPaul Durrant     uint16_t        vendor_id;
478fbab3b6SPaul Durrant     uint16_t        device_id;
488fbab3b6SPaul Durrant     uint8_t         revision;
498fbab3b6SPaul Durrant     uint32_t        size;
508fbab3b6SPaul Durrant     MemoryRegion    mmio;
518fbab3b6SPaul Durrant } XenPVDevice;
528fbab3b6SPaul Durrant 
538fbab3b6SPaul Durrant static uint64_t xen_pv_mmio_read(void *opaque, hwaddr addr,
548fbab3b6SPaul Durrant                                  unsigned size)
558fbab3b6SPaul Durrant {
568fbab3b6SPaul Durrant     trace_xen_pv_mmio_read(addr);
578fbab3b6SPaul Durrant 
588fbab3b6SPaul Durrant     return ~(uint64_t)0;
598fbab3b6SPaul Durrant }
608fbab3b6SPaul Durrant 
618fbab3b6SPaul Durrant static void xen_pv_mmio_write(void *opaque, hwaddr addr,
628fbab3b6SPaul Durrant                               uint64_t val, unsigned size)
638fbab3b6SPaul Durrant {
648fbab3b6SPaul Durrant     trace_xen_pv_mmio_write(addr);
658fbab3b6SPaul Durrant }
668fbab3b6SPaul Durrant 
678fbab3b6SPaul Durrant static const MemoryRegionOps xen_pv_mmio_ops = {
688fbab3b6SPaul Durrant     .read = &xen_pv_mmio_read,
698fbab3b6SPaul Durrant     .write = &xen_pv_mmio_write,
708fbab3b6SPaul Durrant     .endianness = DEVICE_LITTLE_ENDIAN,
718fbab3b6SPaul Durrant };
728fbab3b6SPaul Durrant 
73c6b14aedSCao jin static void xen_pv_realize(PCIDevice *pci_dev, Error **errp)
748fbab3b6SPaul Durrant {
758fbab3b6SPaul Durrant     XenPVDevice *d = XEN_PV_DEVICE(pci_dev);
768fbab3b6SPaul Durrant     uint8_t *pci_conf;
778fbab3b6SPaul Durrant 
78539891a8SPaul Durrant     /* device-id property must always be supplied */
79c6b14aedSCao jin     if (d->device_id == 0xffff) {
80c6b14aedSCao jin         error_setg(errp, "Device ID invalid, it must always be supplied");
81c6b14aedSCao jin         return;
82c6b14aedSCao jin     }
83539891a8SPaul Durrant 
848fbab3b6SPaul Durrant     pci_conf = pci_dev->config;
858fbab3b6SPaul Durrant 
868fbab3b6SPaul Durrant     pci_set_word(pci_conf + PCI_VENDOR_ID, d->vendor_id);
878fbab3b6SPaul Durrant     pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, d->vendor_id);
888fbab3b6SPaul Durrant     pci_set_word(pci_conf + PCI_DEVICE_ID, d->device_id);
898fbab3b6SPaul Durrant     pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, d->device_id);
908fbab3b6SPaul Durrant     pci_set_byte(pci_conf + PCI_REVISION_ID, d->revision);
918fbab3b6SPaul Durrant 
928fbab3b6SPaul Durrant     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_MEMORY);
938fbab3b6SPaul Durrant 
948fbab3b6SPaul Durrant     pci_config_set_prog_interface(pci_conf, 0);
958fbab3b6SPaul Durrant 
968fbab3b6SPaul Durrant     pci_conf[PCI_INTERRUPT_PIN] = 1;
978fbab3b6SPaul Durrant 
988fbab3b6SPaul Durrant     memory_region_init_io(&d->mmio, NULL, &xen_pv_mmio_ops, d,
998fbab3b6SPaul Durrant                           "mmio", d->size);
1008fbab3b6SPaul Durrant 
1018fbab3b6SPaul Durrant     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
1028fbab3b6SPaul Durrant                      &d->mmio);
1038fbab3b6SPaul Durrant }
1048fbab3b6SPaul Durrant 
1058fbab3b6SPaul Durrant static Property xen_pv_props[] = {
1068fbab3b6SPaul Durrant     DEFINE_PROP_UINT16("vendor-id", XenPVDevice, vendor_id, PCI_VENDOR_ID_XEN),
107539891a8SPaul Durrant     DEFINE_PROP_UINT16("device-id", XenPVDevice, device_id, 0xffff),
1088fbab3b6SPaul Durrant     DEFINE_PROP_UINT8("revision", XenPVDevice, revision, 0x01),
1098fbab3b6SPaul Durrant     DEFINE_PROP_UINT32("size", XenPVDevice, size, 0x400000),
1108fbab3b6SPaul Durrant     DEFINE_PROP_END_OF_LIST()
1118fbab3b6SPaul Durrant };
1128fbab3b6SPaul Durrant 
1138fbab3b6SPaul Durrant static void xen_pv_class_init(ObjectClass *klass, void *data)
1148fbab3b6SPaul Durrant {
1158fbab3b6SPaul Durrant     DeviceClass *dc = DEVICE_CLASS(klass);
1168fbab3b6SPaul Durrant     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1178fbab3b6SPaul Durrant 
118c6b14aedSCao jin     k->realize = xen_pv_realize;
1198fbab3b6SPaul Durrant     k->class_id = PCI_CLASS_SYSTEM_OTHER;
1208fbab3b6SPaul Durrant     dc->desc = "Xen PV Device";
1218fbab3b6SPaul Durrant     dc->props = xen_pv_props;
1228fbab3b6SPaul Durrant }
1238fbab3b6SPaul Durrant 
1248fbab3b6SPaul Durrant static const TypeInfo xen_pv_type_info = {
1258fbab3b6SPaul Durrant     .name          = TYPE_XEN_PV_DEVICE,
1268fbab3b6SPaul Durrant     .parent        = TYPE_PCI_DEVICE,
1278fbab3b6SPaul Durrant     .instance_size = sizeof(XenPVDevice),
1288fbab3b6SPaul Durrant     .class_init    = xen_pv_class_init,
1298fbab3b6SPaul Durrant };
1308fbab3b6SPaul Durrant 
1318fbab3b6SPaul Durrant static void xen_pv_register_types(void)
1328fbab3b6SPaul Durrant {
1338fbab3b6SPaul Durrant     type_register_static(&xen_pv_type_info);
1348fbab3b6SPaul Durrant }
1358fbab3b6SPaul Durrant 
1368fbab3b6SPaul Durrant type_init(xen_pv_register_types)
137