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