]> git.proxmox.com Git - qemu.git/blame - hw/virtio/virtio-bus.c
Merge remote-tracking branch 'bonzini/tags/for-anthony' into staging
[qemu.git] / hw / virtio / virtio-bus.c
CommitLineData
ff8eca55
KF
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
83c9f4ca 25#include "hw/hw.h"
ff8eca55 26#include "qemu/error-report.h"
83c9f4ca 27#include "hw/qdev.h"
0d09e41a
PB
28#include "hw/virtio/virtio-bus.h"
29#include "hw/virtio/virtio.h"
ff8eca55
KF
30
31/* #define DEBUG_VIRTIO_BUS */
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/* Plug the VirtIODevice */
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
ff8eca55
KF
51 if (klass->device_plugged != NULL) {
52 klass->device_plugged(qbus->parent);
53 }
54
55 return 0;
56}
57
58/* Reset the virtio_bus */
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/* Destroy the VirtIODevice */
68void virtio_bus_destroy_device(VirtioBusState *bus)
69{
ff8eca55
KF
70 BusState *qbus = BUS(bus);
71 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
72 DPRINTF("%s: remove device.\n", qbus->name);
73
74 if (bus->vdev != NULL) {
75 if (klass->device_unplug != NULL) {
76 klass->device_unplug(qbus->parent);
77 }
02a5c4c9 78 object_unparent(OBJECT(bus->vdev));
ff8eca55
KF
79 bus->vdev = NULL;
80 }
81}
82
83/* Get the device id of the plugged device. */
84uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
85{
86 assert(bus->vdev != NULL);
87 return bus->vdev->device_id;
88}
89
90/* Get the config_len field of the plugged device. */
91size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
92{
93 assert(bus->vdev != NULL);
94 return bus->vdev->config_len;
95}
96
8e05db92
KF
97/* Get the features of the plugged device. */
98uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
99 uint32_t requested_features)
100{
101 VirtioDeviceClass *k;
102 assert(bus->vdev != NULL);
103 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
104 assert(k->get_features != NULL);
105 return k->get_features(bus->vdev, requested_features);
106}
107
5d448f9d
KF
108/* Set the features of the plugged device. */
109void virtio_bus_set_vdev_features(VirtioBusState *bus,
110 uint32_t requested_features)
111{
112 VirtioDeviceClass *k;
113 assert(bus->vdev != NULL);
114 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
115 if (k->set_features != NULL) {
116 k->set_features(bus->vdev, requested_features);
117 }
118}
119
8e05db92
KF
120/* Get bad features of the plugged device. */
121uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
122{
123 VirtioDeviceClass *k;
124 assert(bus->vdev != NULL);
125 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
126 if (k->bad_features != NULL) {
127 return k->bad_features(bus->vdev);
128 } else {
129 return 0;
130 }
131}
132
133/* Get config of the plugged device. */
134void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
135{
136 VirtioDeviceClass *k;
137 assert(bus->vdev != NULL);
138 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
139 if (k->get_config != NULL) {
140 k->get_config(bus->vdev, config);
141 }
142}
143
5d448f9d
KF
144/* Set config of the plugged device. */
145void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
146{
147 VirtioDeviceClass *k;
148 assert(bus->vdev != NULL);
149 k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
150 if (k->set_config != NULL) {
151 k->set_config(bus->vdev, config);
152 }
153}
154
6d46895b
KF
155static char *virtio_bus_get_dev_path(DeviceState *dev)
156{
157 BusState *bus = qdev_get_parent_bus(dev);
158 DeviceState *proxy = DEVICE(bus->parent);
159 return qdev_get_dev_path(proxy);
160}
161
bbfa18fc
AK
162static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
163{
164 return NULL;
165}
166
6d46895b
KF
167static void virtio_bus_class_init(ObjectClass *klass, void *data)
168{
169 BusClass *bus_class = BUS_CLASS(klass);
170 bus_class->get_dev_path = virtio_bus_get_dev_path;
bbfa18fc 171 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
6d46895b
KF
172}
173
ff8eca55
KF
174static const TypeInfo virtio_bus_info = {
175 .name = TYPE_VIRTIO_BUS,
176 .parent = TYPE_BUS,
177 .instance_size = sizeof(VirtioBusState),
178 .abstract = true,
179 .class_size = sizeof(VirtioBusClass),
6d46895b 180 .class_init = virtio_bus_class_init
ff8eca55
KF
181};
182
183static void virtio_register_types(void)
184{
185 type_register_static(&virtio_bus_info);
186}
187
188type_init(virtio_register_types)