virtio-bus.h 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * VirtioBus
 *
 *  Copyright (C) 2012 : GreenSocs Ltd
 *      http://www.greensocs.com/ , email: info@greensocs.com
 *
 *  Developed by :
 *  Frederic Konrad   <fred.konrad@greensocs.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef VIRTIO_BUS_H
#define VIRTIO_BUS_H

28
#include "hw/qdev.h"
29
#include "sysemu/sysemu.h"
P
Paolo Bonzini 已提交
30
#include "hw/virtio/virtio.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#define TYPE_VIRTIO_BUS "virtio-bus"
#define VIRTIO_BUS_GET_CLASS(obj) \
        OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS)
#define VIRTIO_BUS_CLASS(klass) \
        OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS)
#define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS)

typedef struct VirtioBusState VirtioBusState;

typedef struct VirtioBusClass {
    /* This is what a VirtioBus must implement */
    BusClass parent;
    void (*notify)(DeviceState *d, uint16_t vector);
    void (*save_config)(DeviceState *d, QEMUFile *f);
    void (*save_queue)(DeviceState *d, int n, QEMUFile *f);
47
    void (*save_extra_state)(DeviceState *d, QEMUFile *f);
48 49 50
    int (*load_config)(DeviceState *d, QEMUFile *f);
    int (*load_queue)(DeviceState *d, int n, QEMUFile *f);
    int (*load_done)(DeviceState *d, QEMUFile *f);
51 52
    int (*load_extra_state)(DeviceState *d, QEMUFile *f);
    bool (*has_extra_state)(DeviceState *d);
53 54 55
    bool (*query_guest_notifiers)(DeviceState *d);
    int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
    void (*vmstate_change)(DeviceState *d, bool running);
56 57 58 59 60
    /*
     * Expose the features the transport layer supports before
     * the negotiation takes place.
     */
    void (*pre_plugged)(DeviceState *d, Error **errp);
61 62 63 64
    /*
     * transport independent init function.
     * This is called by virtio-bus just after the device is plugged.
     */
J
Jason Wang 已提交
65
    void (*device_plugged)(DeviceState *d, Error **errp);
66 67 68 69
    /*
     * transport independent exit function.
     * This is called by virtio-bus just before the device is unplugged.
     */
70
    void (*device_unplugged)(DeviceState *d);
71
    int (*query_nvectors)(DeviceState *d);
72
    /*
73
     * ioeventfd handling: if the transport implements ioeventfd_assign,
74
     * it must implement ioeventfd_enabled as well.
75
     */
76 77
    /* Returns true if the ioeventfd is enabled for the device. */
    bool (*ioeventfd_enabled)(DeviceState *d);
78 79 80 81 82 83 84
    /*
     * Assigns/deassigns the ioeventfd backing for the transport on
     * the device for queue number n. Returns an error value on
     * failure.
     */
    int (*ioeventfd_assign)(DeviceState *d, EventNotifier *notifier,
                            int n, bool assign);
85 86 87 88 89 90
    /*
     * Does the transport have variable vring alignment?
     * (ie can it ever call virtio_queue_set_align()?)
     * Note that changing this will break migration for this transport.
     */
    bool has_variable_vring_alignment;
91 92 93 94
} VirtioBusClass;

struct VirtioBusState {
    BusState parent_obj;
95 96 97 98 99 100

    /*
     * Set if the default ioeventfd handlers are disabled by vhost
     * or dataplane.
     */
    bool ioeventfd_disabled;
101 102 103 104 105

    /*
     * Set if ioeventfd has been started.
     */
    bool ioeventfd_started;
106 107
};

J
Jason Wang 已提交
108
void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp);
109
void virtio_bus_reset(VirtioBusState *bus);
110
void virtio_bus_device_unplugged(VirtIODevice *bus);
111 112 113 114
/* Get the device id of the plugged device. */
uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus);
/* Get the config_len field of the plugged device. */
size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus);
115 116 117 118
/* Get bad features of the plugged device. */
uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus);
/* Get config of the plugged device. */
void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config);
K
KONRAD Frederic 已提交
119 120
/* Set config of the plugged device. */
void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config);
121

P
Paolo Bonzini 已提交
122 123 124 125 126 127 128 129 130 131 132 133
static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus)
{
    BusState *qbus = &bus->parent_obj;
    BusChild *kid = QTAILQ_FIRST(&qbus->children);
    DeviceState *qdev = kid ? kid->child : NULL;

    /* This is used on the data path, the cast is guaranteed
     * to succeed by the qdev machinery.
     */
    return (VirtIODevice *)qdev;
}

134 135
/* Return whether the proxy allows ioeventfd.  */
bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus);
136
/* Start the ioeventfd. */
137
int virtio_bus_start_ioeventfd(VirtioBusState *bus);
138 139 140 141 142
/* Stop the ioeventfd. */
void virtio_bus_stop_ioeventfd(VirtioBusState *bus);
/* Switch from/to the generic ioeventfd handler */
int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign);

143 144 145 146 147
/* This is temporary.  It is only needed because virtio_bus_set_host_notifier
 * sets ioeventfd_disabled but we will shortly get rid of it.  */
int set_host_notifier_internal(DeviceState *proxy, VirtioBusState *bus,
                               int n, bool assign, bool set_handler);

148
#endif /* VIRTIO_BUS_H */