1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include "qemu/osdep.h"
33#include "qapi/error.h"
34#include "hw/hw.h"
35#include "hw/pci/pci.h"
36#include "trace.h"
37
38#define TYPE_XEN_PV_DEVICE "xen-pvdevice"
39
40#define XEN_PV_DEVICE(obj) \
41 OBJECT_CHECK(XenPVDevice, (obj), TYPE_XEN_PV_DEVICE)
42
43typedef struct XenPVDevice {
44
45 PCIDevice parent_obj;
46
47 uint16_t vendor_id;
48 uint16_t device_id;
49 uint8_t revision;
50 uint32_t size;
51 MemoryRegion mmio;
52} XenPVDevice;
53
54static uint64_t xen_pv_mmio_read(void *opaque, hwaddr addr,
55 unsigned size)
56{
57 trace_xen_pv_mmio_read(addr);
58
59 return ~(uint64_t)0;
60}
61
62static void xen_pv_mmio_write(void *opaque, hwaddr addr,
63 uint64_t val, unsigned size)
64{
65 trace_xen_pv_mmio_write(addr);
66}
67
68static const MemoryRegionOps xen_pv_mmio_ops = {
69 .read = &xen_pv_mmio_read,
70 .write = &xen_pv_mmio_write,
71 .endianness = DEVICE_LITTLE_ENDIAN,
72};
73
74static void xen_pv_realize(PCIDevice *pci_dev, Error **errp)
75{
76 XenPVDevice *d = XEN_PV_DEVICE(pci_dev);
77 uint8_t *pci_conf;
78
79
80 if (d->device_id == 0xffff) {
81 error_setg(errp, "Device ID invalid, it must always be supplied");
82 return;
83 }
84
85 pci_conf = pci_dev->config;
86
87 pci_set_word(pci_conf + PCI_VENDOR_ID, d->vendor_id);
88 pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, d->vendor_id);
89 pci_set_word(pci_conf + PCI_DEVICE_ID, d->device_id);
90 pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, d->device_id);
91 pci_set_byte(pci_conf + PCI_REVISION_ID, d->revision);
92
93 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_MEMORY);
94
95 pci_config_set_prog_interface(pci_conf, 0);
96
97 pci_conf[PCI_INTERRUPT_PIN] = 1;
98
99 memory_region_init_io(&d->mmio, NULL, &xen_pv_mmio_ops, d,
100 "mmio", d->size);
101
102 pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
103 &d->mmio);
104}
105
106static Property xen_pv_props[] = {
107 DEFINE_PROP_UINT16("vendor-id", XenPVDevice, vendor_id, PCI_VENDOR_ID_XEN),
108 DEFINE_PROP_UINT16("device-id", XenPVDevice, device_id, 0xffff),
109 DEFINE_PROP_UINT8("revision", XenPVDevice, revision, 0x01),
110 DEFINE_PROP_UINT32("size", XenPVDevice, size, 0x400000),
111 DEFINE_PROP_END_OF_LIST()
112};
113
114static void xen_pv_class_init(ObjectClass *klass, void *data)
115{
116 DeviceClass *dc = DEVICE_CLASS(klass);
117 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
118
119 k->realize = xen_pv_realize;
120 k->class_id = PCI_CLASS_SYSTEM_OTHER;
121 dc->desc = "Xen PV Device";
122 dc->props = xen_pv_props;
123}
124
125static const TypeInfo xen_pv_type_info = {
126 .name = TYPE_XEN_PV_DEVICE,
127 .parent = TYPE_PCI_DEVICE,
128 .instance_size = sizeof(XenPVDevice),
129 .class_init = xen_pv_class_init,
130 .interfaces = (InterfaceInfo[]) {
131 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
132 { },
133 },
134};
135
136static void xen_pv_register_types(void)
137{
138 type_register_static(&xen_pv_type_info);
139}
140
141type_init(xen_pv_register_types)
142