Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / virtio / virtio-bus.c
1 /*
2  * VirtioBus
3  *
4  *  Copyright (C) 2012 : GreenSocs Ltd
5  *      http://www.greensocs.com/ , email: info@greensocs.com
6  *
7  *  Developed by :
8  *  Frederic Konrad   <fred.konrad@greensocs.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, see <http://www.gnu.org/licenses/>.
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 /* #define DEBUG_VIRTIO_BUS */
32
33 #ifdef DEBUG_VIRTIO_BUS
34 #define DPRINTF(fmt, ...) \
35 do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
36 #else
37 #define DPRINTF(fmt, ...) do { } while (0)
38 #endif
39
40 /* A VirtIODevice is being plugged */
41 void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
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     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
48
49     DPRINTF("%s: plug device.\n", qbus->name);
50
51     if (klass->device_plugged != NULL) {
52         klass->device_plugged(qbus->parent, errp);
53     }
54
55     /* Get the features of the plugged device. */
56     assert(vdc->get_features != NULL);
57     vdev->host_features = vdc->get_features(vdev, vdev->host_features,
58                                             errp);
59 }
60
61 /* Reset the virtio_bus */
62 void virtio_bus_reset(VirtioBusState *bus)
63 {
64     VirtIODevice *vdev = virtio_bus_get_device(bus);
65
66     DPRINTF("%s: reset device.\n", BUS(bus)->name);
67     if (vdev != NULL) {
68         virtio_reset(vdev);
69     }
70 }
71
72 /* A VirtIODevice is being unplugged */
73 void virtio_bus_device_unplugged(VirtIODevice *vdev)
74 {
75     DeviceState *qdev = DEVICE(vdev);
76     BusState *qbus = BUS(qdev_get_parent_bus(qdev));
77     VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
78
79     DPRINTF("%s: remove device.\n", qbus->name);
80
81     if (vdev != NULL) {
82         if (klass->device_unplugged != NULL) {
83             klass->device_unplugged(qbus->parent);
84         }
85     }
86 }
87
88 /* Get the device id of the plugged device. */
89 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
90 {
91     VirtIODevice *vdev = virtio_bus_get_device(bus);
92     assert(vdev != NULL);
93     return vdev->device_id;
94 }
95
96 /* Get the config_len field of the plugged device. */
97 size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
98 {
99     VirtIODevice *vdev = virtio_bus_get_device(bus);
100     assert(vdev != NULL);
101     return vdev->config_len;
102 }
103
104 /* Get bad features of the plugged device. */
105 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
106 {
107     VirtIODevice *vdev = virtio_bus_get_device(bus);
108     VirtioDeviceClass *k;
109
110     assert(vdev != NULL);
111     k = VIRTIO_DEVICE_GET_CLASS(vdev);
112     if (k->bad_features != NULL) {
113         return k->bad_features(vdev);
114     } else {
115         return 0;
116     }
117 }
118
119 /* Get config of the plugged device. */
120 void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
121 {
122     VirtIODevice *vdev = virtio_bus_get_device(bus);
123     VirtioDeviceClass *k;
124
125     assert(vdev != NULL);
126     k = VIRTIO_DEVICE_GET_CLASS(vdev);
127     if (k->get_config != NULL) {
128         k->get_config(vdev, config);
129     }
130 }
131
132 /* Set config of the plugged device. */
133 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
134 {
135     VirtIODevice *vdev = virtio_bus_get_device(bus);
136     VirtioDeviceClass *k;
137
138     assert(vdev != NULL);
139     k = VIRTIO_DEVICE_GET_CLASS(vdev);
140     if (k->set_config != NULL) {
141         k->set_config(vdev, config);
142     }
143 }
144
145 static char *virtio_bus_get_dev_path(DeviceState *dev)
146 {
147     BusState *bus = qdev_get_parent_bus(dev);
148     DeviceState *proxy = DEVICE(bus->parent);
149     return qdev_get_dev_path(proxy);
150 }
151
152 static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
153 {
154     return NULL;
155 }
156
157 static void virtio_bus_class_init(ObjectClass *klass, void *data)
158 {
159     BusClass *bus_class = BUS_CLASS(klass);
160     bus_class->get_dev_path = virtio_bus_get_dev_path;
161     bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
162 }
163
164 static const TypeInfo virtio_bus_info = {
165     .name = TYPE_VIRTIO_BUS,
166     .parent = TYPE_BUS,
167     .instance_size = sizeof(VirtioBusState),
168     .abstract = true,
169     .class_size = sizeof(VirtioBusClass),
170     .class_init = virtio_bus_class_init
171 };
172
173 static void virtio_register_types(void)
174 {
175     type_register_static(&virtio_bus_info);
176 }
177
178 type_init(virtio_register_types)