]>
Commit | Line | Data |
---|---|---|
ff8eca55 FK |
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 FK |
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 | ||
5e96f5d2 PB |
40 | /* A VirtIODevice is being plugged */ |
41 | int virtio_bus_device_plugged(VirtIODevice *vdev) | |
ff8eca55 FK |
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 | ||
ff8eca55 FK |
49 | if (klass->device_plugged != NULL) { |
50 | klass->device_plugged(qbus->parent); | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | /* Reset the virtio_bus */ | |
57 | void virtio_bus_reset(VirtioBusState *bus) | |
58 | { | |
06d3dff0 PB |
59 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
60 | ||
2c80ab15 | 61 | DPRINTF("%s: reset device.\n", BUS(bus)->name); |
06d3dff0 PB |
62 | if (vdev != NULL) { |
63 | virtio_reset(vdev); | |
ff8eca55 FK |
64 | } |
65 | } | |
66 | ||
5e96f5d2 PB |
67 | /* A VirtIODevice is being unplugged */ |
68 | void virtio_bus_device_unplugged(VirtIODevice *vdev) | |
ff8eca55 | 69 | { |
5e96f5d2 PB |
70 | DeviceState *qdev = DEVICE(vdev); |
71 | BusState *qbus = BUS(qdev_get_parent_bus(qdev)); | |
72 | VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus); | |
06d3dff0 | 73 | |
ff8eca55 FK |
74 | DPRINTF("%s: remove device.\n", qbus->name); |
75 | ||
06d3dff0 | 76 | if (vdev != NULL) { |
5e96f5d2 PB |
77 | if (klass->device_unplugged != NULL) { |
78 | klass->device_unplugged(qbus->parent); | |
ff8eca55 | 79 | } |
ff8eca55 FK |
80 | } |
81 | } | |
82 | ||
83 | /* Get the device id of the plugged device. */ | |
84 | uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus) | |
85 | { | |
06d3dff0 PB |
86 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
87 | assert(vdev != NULL); | |
88 | return vdev->device_id; | |
ff8eca55 FK |
89 | } |
90 | ||
91 | /* Get the config_len field of the plugged device. */ | |
92 | size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus) | |
93 | { | |
06d3dff0 PB |
94 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
95 | assert(vdev != NULL); | |
96 | return vdev->config_len; | |
ff8eca55 FK |
97 | } |
98 | ||
8e05db92 FK |
99 | /* Get the features of the plugged device. */ |
100 | uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus, | |
101 | uint32_t requested_features) | |
102 | { | |
06d3dff0 | 103 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
8e05db92 | 104 | VirtioDeviceClass *k; |
06d3dff0 PB |
105 | |
106 | assert(vdev != NULL); | |
107 | k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
8e05db92 | 108 | assert(k->get_features != NULL); |
06d3dff0 | 109 | return k->get_features(vdev, requested_features); |
8e05db92 FK |
110 | } |
111 | ||
112 | /* Get bad features of the plugged device. */ | |
113 | uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus) | |
114 | { | |
06d3dff0 | 115 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
8e05db92 | 116 | VirtioDeviceClass *k; |
06d3dff0 PB |
117 | |
118 | assert(vdev != NULL); | |
119 | k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
8e05db92 | 120 | if (k->bad_features != NULL) { |
06d3dff0 | 121 | return k->bad_features(vdev); |
8e05db92 FK |
122 | } else { |
123 | return 0; | |
124 | } | |
125 | } | |
126 | ||
127 | /* Get config of the plugged device. */ | |
128 | void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config) | |
129 | { | |
06d3dff0 | 130 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
8e05db92 | 131 | VirtioDeviceClass *k; |
06d3dff0 PB |
132 | |
133 | assert(vdev != NULL); | |
134 | k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
8e05db92 | 135 | if (k->get_config != NULL) { |
06d3dff0 | 136 | k->get_config(vdev, config); |
8e05db92 FK |
137 | } |
138 | } | |
139 | ||
5d448f9d FK |
140 | /* Set config of the plugged device. */ |
141 | void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config) | |
142 | { | |
06d3dff0 | 143 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
5d448f9d | 144 | VirtioDeviceClass *k; |
06d3dff0 PB |
145 | |
146 | assert(vdev != NULL); | |
147 | k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
5d448f9d | 148 | if (k->set_config != NULL) { |
06d3dff0 | 149 | k->set_config(vdev, config); |
5d448f9d FK |
150 | } |
151 | } | |
152 | ||
6d46895b FK |
153 | static char *virtio_bus_get_dev_path(DeviceState *dev) |
154 | { | |
155 | BusState *bus = qdev_get_parent_bus(dev); | |
156 | DeviceState *proxy = DEVICE(bus->parent); | |
157 | return qdev_get_dev_path(proxy); | |
158 | } | |
159 | ||
bbfa18fc AK |
160 | static char *virtio_bus_get_fw_dev_path(DeviceState *dev) |
161 | { | |
162 | return NULL; | |
163 | } | |
164 | ||
6d46895b FK |
165 | static void virtio_bus_class_init(ObjectClass *klass, void *data) |
166 | { | |
167 | BusClass *bus_class = BUS_CLASS(klass); | |
168 | bus_class->get_dev_path = virtio_bus_get_dev_path; | |
bbfa18fc | 169 | bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path; |
6d46895b FK |
170 | } |
171 | ||
ff8eca55 FK |
172 | static const TypeInfo virtio_bus_info = { |
173 | .name = TYPE_VIRTIO_BUS, | |
174 | .parent = TYPE_BUS, | |
175 | .instance_size = sizeof(VirtioBusState), | |
176 | .abstract = true, | |
177 | .class_size = sizeof(VirtioBusClass), | |
6d46895b | 178 | .class_init = virtio_bus_class_init |
ff8eca55 FK |
179 | }; |
180 | ||
181 | static void virtio_register_types(void) | |
182 | { | |
183 | type_register_static(&virtio_bus_info); | |
184 | } | |
185 | ||
186 | type_init(virtio_register_types) |