xref: /qemu/include/hw/virtio/virtio-bus.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1ff8eca55SKONRAD Frederic /*
2ff8eca55SKONRAD Frederic  * VirtioBus
3ff8eca55SKONRAD Frederic  *
4ff8eca55SKONRAD Frederic  *  Copyright (C) 2012 : GreenSocs Ltd
5ff8eca55SKONRAD Frederic  *      http://www.greensocs.com/ , email: info@greensocs.com
6ff8eca55SKONRAD Frederic  *
7ff8eca55SKONRAD Frederic  *  Developed by :
8ff8eca55SKONRAD Frederic  *  Frederic Konrad   <fred.konrad@greensocs.com>
9ff8eca55SKONRAD Frederic  *
10ff8eca55SKONRAD Frederic  * This program is free software; you can redistribute it and/or modify
11ff8eca55SKONRAD Frederic  * it under the terms of the GNU General Public License as published by
12ff8eca55SKONRAD Frederic  * the Free Software Foundation, either version 2 of the License, or
13ff8eca55SKONRAD Frederic  * (at your option) any later version.
14ff8eca55SKONRAD Frederic  *
15ff8eca55SKONRAD Frederic  * This program is distributed in the hope that it will be useful,
16ff8eca55SKONRAD Frederic  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ff8eca55SKONRAD Frederic  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ff8eca55SKONRAD Frederic  * GNU General Public License for more details.
19ff8eca55SKONRAD Frederic  *
20ff8eca55SKONRAD Frederic  * You should have received a copy of the GNU General Public License along
21ff8eca55SKONRAD Frederic  * with this program; if not, see <http://www.gnu.org/licenses/>.
22ff8eca55SKONRAD Frederic  *
23ff8eca55SKONRAD Frederic  */
24ff8eca55SKONRAD Frederic 
25ff8eca55SKONRAD Frederic #ifndef VIRTIO_BUS_H
26ff8eca55SKONRAD Frederic #define VIRTIO_BUS_H
27ff8eca55SKONRAD Frederic 
28a27bd6c7SMarkus Armbruster #include "hw/qdev-core.h"
290d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
30*db1015e9SEduardo Habkost #include "qom/object.h"
31ff8eca55SKONRAD Frederic 
32ff8eca55SKONRAD Frederic #define TYPE_VIRTIO_BUS "virtio-bus"
33*db1015e9SEduardo Habkost typedef struct VirtioBusClass VirtioBusClass;
34*db1015e9SEduardo Habkost typedef struct VirtioBusState VirtioBusState;
35ff8eca55SKONRAD Frederic #define VIRTIO_BUS_GET_CLASS(obj) \
36ff8eca55SKONRAD Frederic         OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS)
37ff8eca55SKONRAD Frederic #define VIRTIO_BUS_CLASS(klass) \
38ff8eca55SKONRAD Frederic         OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS)
39ff8eca55SKONRAD Frederic #define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS)
40ff8eca55SKONRAD Frederic 
41ff8eca55SKONRAD Frederic 
42*db1015e9SEduardo Habkost struct VirtioBusClass {
43ff8eca55SKONRAD Frederic     /* This is what a VirtioBus must implement */
44ff8eca55SKONRAD Frederic     BusClass parent;
45ff8eca55SKONRAD Frederic     void (*notify)(DeviceState *d, uint16_t vector);
46ff8eca55SKONRAD Frederic     void (*save_config)(DeviceState *d, QEMUFile *f);
47ff8eca55SKONRAD Frederic     void (*save_queue)(DeviceState *d, int n, QEMUFile *f);
48a6df8adfSJason Wang     void (*save_extra_state)(DeviceState *d, QEMUFile *f);
49ff8eca55SKONRAD Frederic     int (*load_config)(DeviceState *d, QEMUFile *f);
50ff8eca55SKONRAD Frederic     int (*load_queue)(DeviceState *d, int n, QEMUFile *f);
51ff8eca55SKONRAD Frederic     int (*load_done)(DeviceState *d, QEMUFile *f);
52a6df8adfSJason Wang     int (*load_extra_state)(DeviceState *d, QEMUFile *f);
53a6df8adfSJason Wang     bool (*has_extra_state)(DeviceState *d);
54ff8eca55SKONRAD Frederic     bool (*query_guest_notifiers)(DeviceState *d);
55ff8eca55SKONRAD Frederic     int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
566f80e617STiwei Bie     int (*set_host_notifier_mr)(DeviceState *d, int n,
576f80e617STiwei Bie                                 MemoryRegion *mr, bool assign);
58ff8eca55SKONRAD Frederic     void (*vmstate_change)(DeviceState *d, bool running);
59ff8eca55SKONRAD Frederic     /*
60d1b4259fSMaxime Coquelin      * Expose the features the transport layer supports before
61d1b4259fSMaxime Coquelin      * the negotiation takes place.
62d1b4259fSMaxime Coquelin      */
63d1b4259fSMaxime Coquelin     void (*pre_plugged)(DeviceState *d, Error **errp);
64d1b4259fSMaxime Coquelin     /*
65ff8eca55SKONRAD Frederic      * transport independent init function.
66ff8eca55SKONRAD Frederic      * This is called by virtio-bus just after the device is plugged.
67ff8eca55SKONRAD Frederic      */
68e8398045SJason Wang     void (*device_plugged)(DeviceState *d, Error **errp);
69ff8eca55SKONRAD Frederic     /*
70ff8eca55SKONRAD Frederic      * transport independent exit function.
71ff8eca55SKONRAD Frederic      * This is called by virtio-bus just before the device is unplugged.
72ff8eca55SKONRAD Frederic      */
735e96f5d2SPaolo Bonzini     void (*device_unplugged)(DeviceState *d);
74e0d686bfSJason Wang     int (*query_nvectors)(DeviceState *d);
756ce69d1cSPeter Maydell     /*
76b13d3962SPaolo Bonzini      * ioeventfd handling: if the transport implements ioeventfd_assign,
778e93cef1SPaolo Bonzini      * it must implement ioeventfd_enabled as well.
786798e245SCornelia Huck      */
798e93cef1SPaolo Bonzini     /* Returns true if the ioeventfd is enabled for the device. */
808e93cef1SPaolo Bonzini     bool (*ioeventfd_enabled)(DeviceState *d);
816798e245SCornelia Huck     /*
826798e245SCornelia Huck      * Assigns/deassigns the ioeventfd backing for the transport on
836798e245SCornelia Huck      * the device for queue number n. Returns an error value on
846798e245SCornelia Huck      * failure.
856798e245SCornelia Huck      */
866798e245SCornelia Huck     int (*ioeventfd_assign)(DeviceState *d, EventNotifier *notifier,
876798e245SCornelia Huck                             int n, bool assign);
886798e245SCornelia Huck     /*
89b2a5f62aSJason Wang      * Whether queue number n is enabled.
90b2a5f62aSJason Wang      */
91b2a5f62aSJason Wang     bool (*queue_enabled)(DeviceState *d, int n);
92b2a5f62aSJason Wang     /*
936ce69d1cSPeter Maydell      * Does the transport have variable vring alignment?
946ce69d1cSPeter Maydell      * (ie can it ever call virtio_queue_set_align()?)
956ce69d1cSPeter Maydell      * Note that changing this will break migration for this transport.
966ce69d1cSPeter Maydell      */
976ce69d1cSPeter Maydell     bool has_variable_vring_alignment;
988607f5c3SJason Wang     AddressSpace *(*get_dma_as)(DeviceState *d);
99*db1015e9SEduardo Habkost };
100ff8eca55SKONRAD Frederic 
101ff8eca55SKONRAD Frederic struct VirtioBusState {
102ff8eca55SKONRAD Frederic     BusState parent_obj;
1034ddcc2d5SPaolo Bonzini 
1044ddcc2d5SPaolo Bonzini     /*
105b13d3962SPaolo Bonzini      * Set if ioeventfd has been started.
106b13d3962SPaolo Bonzini      */
107b13d3962SPaolo Bonzini     bool ioeventfd_started;
108310837deSPaolo Bonzini 
109310837deSPaolo Bonzini     /*
110310837deSPaolo Bonzini      * Set if ioeventfd has been grabbed by vhost.  When ioeventfd
111310837deSPaolo Bonzini      * is grabbed by vhost, we track its started/stopped state (which
112310837deSPaolo Bonzini      * depends in turn on the virtio status register), but do not
113310837deSPaolo Bonzini      * register a handler for the ioeventfd.  When ioeventfd is
114310837deSPaolo Bonzini      * released, if ioeventfd_started is true we finally register
115310837deSPaolo Bonzini      * the handler so that QEMU's device model can use ioeventfd.
116310837deSPaolo Bonzini      */
117310837deSPaolo Bonzini     int ioeventfd_grabbed;
118ff8eca55SKONRAD Frederic };
119ff8eca55SKONRAD Frederic 
120e8398045SJason Wang void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp);
121ff8eca55SKONRAD Frederic void virtio_bus_reset(VirtioBusState *bus);
1225e96f5d2SPaolo Bonzini void virtio_bus_device_unplugged(VirtIODevice *bus);
123ff8eca55SKONRAD Frederic /* Get the device id of the plugged device. */
124ff8eca55SKONRAD Frederic uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus);
125ff8eca55SKONRAD Frederic /* Get the config_len field of the plugged device. */
126ff8eca55SKONRAD Frederic size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus);
1278e05db92SKONRAD Frederic /* Get bad features of the plugged device. */
1288e05db92SKONRAD Frederic uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus);
1298e05db92SKONRAD Frederic /* Get config of the plugged device. */
1308e05db92SKONRAD Frederic void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config);
1315d448f9dSKONRAD Frederic /* Set config of the plugged device. */
1325d448f9dSKONRAD Frederic void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config);
133ff8eca55SKONRAD Frederic 
13406d3dff0SPaolo Bonzini static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus)
13506d3dff0SPaolo Bonzini {
13606d3dff0SPaolo Bonzini     BusState *qbus = &bus->parent_obj;
13706d3dff0SPaolo Bonzini     BusChild *kid = QTAILQ_FIRST(&qbus->children);
13806d3dff0SPaolo Bonzini     DeviceState *qdev = kid ? kid->child : NULL;
13906d3dff0SPaolo Bonzini 
14006d3dff0SPaolo Bonzini     /* This is used on the data path, the cast is guaranteed
14106d3dff0SPaolo Bonzini      * to succeed by the qdev machinery.
14206d3dff0SPaolo Bonzini      */
14306d3dff0SPaolo Bonzini     return (VirtIODevice *)qdev;
14406d3dff0SPaolo Bonzini }
14506d3dff0SPaolo Bonzini 
1468e93cef1SPaolo Bonzini /* Return whether the proxy allows ioeventfd.  */
1478e93cef1SPaolo Bonzini bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus);
1486798e245SCornelia Huck /* Start the ioeventfd. */
149ff4c07dfSPaolo Bonzini int virtio_bus_start_ioeventfd(VirtioBusState *bus);
1506798e245SCornelia Huck /* Stop the ioeventfd. */
1516798e245SCornelia Huck void virtio_bus_stop_ioeventfd(VirtioBusState *bus);
152310837deSPaolo Bonzini /* Tell the bus that vhost is grabbing the ioeventfd. */
153310837deSPaolo Bonzini int virtio_bus_grab_ioeventfd(VirtioBusState *bus);
154310837deSPaolo Bonzini /* bus that vhost is not using the ioeventfd anymore. */
155310837deSPaolo Bonzini void virtio_bus_release_ioeventfd(VirtioBusState *bus);
1566798e245SCornelia Huck /* Switch from/to the generic ioeventfd handler */
1576798e245SCornelia Huck int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign);
15876143618SGal Hammer /* Tell the bus that the ioeventfd handler is no longer required. */
15976143618SGal Hammer void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n);
1606798e245SCornelia Huck 
161ff8eca55SKONRAD Frederic #endif /* VIRTIO_BUS_H */
162