xref: /qemu/include/hw/virtio/virtio-bus.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
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 #ifndef VIRTIO_BUS_H
26 #define VIRTIO_BUS_H
27 
28 #include "hw/qdev-core.h"
29 #include "hw/virtio/virtio.h"
30 #include "qom/object.h"
31 
32 #define TYPE_VIRTIO_BUS "virtio-bus"
33 typedef struct VirtioBusClass VirtioBusClass;
34 typedef struct VirtioBusState VirtioBusState;
35 #define VIRTIO_BUS_GET_CLASS(obj) \
36         OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS)
37 #define VIRTIO_BUS_CLASS(klass) \
38         OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS)
39 #define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS)
40 
41 
42 struct VirtioBusClass {
43     /* This is what a VirtioBus must implement */
44     BusClass parent;
45     void (*notify)(DeviceState *d, uint16_t vector);
46     void (*save_config)(DeviceState *d, QEMUFile *f);
47     void (*save_queue)(DeviceState *d, int n, QEMUFile *f);
48     void (*save_extra_state)(DeviceState *d, QEMUFile *f);
49     int (*load_config)(DeviceState *d, QEMUFile *f);
50     int (*load_queue)(DeviceState *d, int n, QEMUFile *f);
51     int (*load_done)(DeviceState *d, QEMUFile *f);
52     int (*load_extra_state)(DeviceState *d, QEMUFile *f);
53     bool (*has_extra_state)(DeviceState *d);
54     bool (*query_guest_notifiers)(DeviceState *d);
55     int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
56     int (*set_host_notifier_mr)(DeviceState *d, int n,
57                                 MemoryRegion *mr, bool assign);
58     void (*vmstate_change)(DeviceState *d, bool running);
59     /*
60      * Expose the features the transport layer supports before
61      * the negotiation takes place.
62      */
63     void (*pre_plugged)(DeviceState *d, Error **errp);
64     /*
65      * transport independent init function.
66      * This is called by virtio-bus just after the device is plugged.
67      */
68     void (*device_plugged)(DeviceState *d, Error **errp);
69     /*
70      * transport independent exit function.
71      * This is called by virtio-bus just before the device is unplugged.
72      */
73     void (*device_unplugged)(DeviceState *d);
74     int (*query_nvectors)(DeviceState *d);
75     /*
76      * ioeventfd handling: if the transport implements ioeventfd_assign,
77      * it must implement ioeventfd_enabled as well.
78      */
79     /* Returns true if the ioeventfd is enabled for the device. */
80     bool (*ioeventfd_enabled)(DeviceState *d);
81     /*
82      * Assigns/deassigns the ioeventfd backing for the transport on
83      * the device for queue number n. Returns an error value on
84      * failure.
85      */
86     int (*ioeventfd_assign)(DeviceState *d, EventNotifier *notifier,
87                             int n, bool assign);
88     /*
89      * Whether queue number n is enabled.
90      */
91     bool (*queue_enabled)(DeviceState *d, int n);
92     /*
93      * Does the transport have variable vring alignment?
94      * (ie can it ever call virtio_queue_set_align()?)
95      * Note that changing this will break migration for this transport.
96      */
97     bool has_variable_vring_alignment;
98     AddressSpace *(*get_dma_as)(DeviceState *d);
99 };
100 
101 struct VirtioBusState {
102     BusState parent_obj;
103 
104     /*
105      * Set if ioeventfd has been started.
106      */
107     bool ioeventfd_started;
108 
109     /*
110      * Set if ioeventfd has been grabbed by vhost.  When ioeventfd
111      * is grabbed by vhost, we track its started/stopped state (which
112      * depends in turn on the virtio status register), but do not
113      * register a handler for the ioeventfd.  When ioeventfd is
114      * released, if ioeventfd_started is true we finally register
115      * the handler so that QEMU's device model can use ioeventfd.
116      */
117     int ioeventfd_grabbed;
118 };
119 
120 void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp);
121 void virtio_bus_reset(VirtioBusState *bus);
122 void virtio_bus_device_unplugged(VirtIODevice *bus);
123 /* Get the device id of the plugged device. */
124 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus);
125 /* Get the config_len field of the plugged device. */
126 size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus);
127 /* Get bad features of the plugged device. */
128 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus);
129 /* Get config of the plugged device. */
130 void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config);
131 /* Set config of the plugged device. */
132 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config);
133 
134 static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus)
135 {
136     BusState *qbus = &bus->parent_obj;
137     BusChild *kid = QTAILQ_FIRST(&qbus->children);
138     DeviceState *qdev = kid ? kid->child : NULL;
139 
140     /* This is used on the data path, the cast is guaranteed
141      * to succeed by the qdev machinery.
142      */
143     return (VirtIODevice *)qdev;
144 }
145 
146 /* Return whether the proxy allows ioeventfd.  */
147 bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus);
148 /* Start the ioeventfd. */
149 int virtio_bus_start_ioeventfd(VirtioBusState *bus);
150 /* Stop the ioeventfd. */
151 void virtio_bus_stop_ioeventfd(VirtioBusState *bus);
152 /* Tell the bus that vhost is grabbing the ioeventfd. */
153 int virtio_bus_grab_ioeventfd(VirtioBusState *bus);
154 /* bus that vhost is not using the ioeventfd anymore. */
155 void virtio_bus_release_ioeventfd(VirtioBusState *bus);
156 /* Switch from/to the generic ioeventfd handler */
157 int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign);
158 /* Tell the bus that the ioeventfd handler is no longer required. */
159 void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n);
160 
161 #endif /* VIRTIO_BUS_H */
162