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#include "hw/hw.h"
26#include "qemu/error-report.h"
27#include "hw/qdev.h"
28#include "hw/virtio/virtio-bus.h"
29#include "hw/virtio/virtio.h"
30
31
32
33#ifdef DEBUG_VIRTIO_BUS
34#define DPRINTF(fmt, ...) \
35do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
36#else
37#define DPRINTF(fmt, ...) do { } while (0)
38#endif
39
40
41int virtio_bus_plug_device(VirtIODevice *vdev)
42{
43 DeviceState *qdev = DEVICE(vdev);
44 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
45 VirtioBusState *bus = VIRTIO_BUS(qbus);
46 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
47 DPRINTF("%s: plug device.\n", qbus->name);
48
49 bus->vdev = vdev;
50
51 if (klass->device_plugged != NULL) {
52 klass->device_plugged(qbus->parent);
53 }
54
55 return 0;
56}
57
58
59void virtio_bus_reset(VirtioBusState *bus)
60{
61 DPRINTF("%s: reset device.\n", qbus->name);
62 if (bus->vdev != NULL) {
63 virtio_reset(bus->vdev);
64 }
65}
66
67
68void virtio_bus_destroy_device(VirtioBusState *bus)
69{
70 DeviceState *qdev;
71 BusState *qbus = BUS(bus);
72 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
73 DPRINTF("%s: remove device.\n", qbus->name);
74
75 if (bus->vdev != NULL) {
76 if (klass->device_unplug != NULL) {
77 klass->device_unplug(qbus->parent);
78 }
79 qdev = DEVICE(bus->vdev);
80 qdev_free(qdev);
81 bus->vdev = NULL;
82 }
83}
84
85
86uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
87{
88 assert(bus->vdev != NULL);
89 return bus->vdev->device_id;
90}
91
92
93size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
94{
95 assert(bus->vdev != NULL);
96 return bus->vdev->config_len;
97}
98
99
100uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
101 uint32_t requested_features)
102{
103 VirtioDeviceClass *k;
104 assert(bus->vdev != NULL);
105 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
106 assert(k->get_features != NULL);
107 return k->get_features(bus->vdev, requested_features);
108}
109
110
111void virtio_bus_set_vdev_features(VirtioBusState *bus,
112 uint32_t requested_features)
113{
114 VirtioDeviceClass *k;
115 assert(bus->vdev != NULL);
116 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
117 if (k->set_features != NULL) {
118 k->set_features(bus->vdev, requested_features);
119 }
120}
121
122
123uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
124{
125 VirtioDeviceClass *k;
126 assert(bus->vdev != NULL);
127 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
128 if (k->bad_features != NULL) {
129 return k->bad_features(bus->vdev);
130 } else {
131 return 0;
132 }
133}
134
135
136void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
137{
138 VirtioDeviceClass *k;
139 assert(bus->vdev != NULL);
140 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
141 if (k->get_config != NULL) {
142 k->get_config(bus->vdev, config);
143 }
144}
145
146
147void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
148{
149 VirtioDeviceClass *k;
150 assert(bus->vdev != NULL);
151 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
152 if (k->set_config != NULL) {
153 k->set_config(bus->vdev, config);
154 }
155}
156
157static char *virtio_bus_get_dev_path(DeviceState *dev)
158{
159 BusState *bus = qdev_get_parent_bus(dev);
160 DeviceState *proxy = DEVICE(bus->parent);
161 return qdev_get_dev_path(proxy);
162}
163
164static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
165{
166 return NULL;
167}
168
169static void virtio_bus_class_init(ObjectClass *klass, void *data)
170{
171 BusClass *bus_class = BUS_CLASS(klass);
172 bus_class->get_dev_path = virtio_bus_get_dev_path;
173 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
174}
175
176static const TypeInfo virtio_bus_info = {
177 .name = TYPE_VIRTIO_BUS,
178 .parent = TYPE_BUS,
179 .instance_size = sizeof(VirtioBusState),
180 .abstract = true,
181 .class_size = sizeof(VirtioBusClass),
182 .class_init = virtio_bus_class_init
183};
184
185static void virtio_register_types(void)
186{
187 type_register_static(&virtio_bus_info);
188}
189
190type_init(virtio_register_types)
191