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 2883c9f4caSPaolo Bonzini #include "hw/qdev.h" 29ff8eca55SKONRAD Frederic #include "sysemu/sysemu.h" 300d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 31ff8eca55SKONRAD Frederic 32ff8eca55SKONRAD Frederic #define TYPE_VIRTIO_BUS "virtio-bus" 33ff8eca55SKONRAD Frederic #define VIRTIO_BUS_GET_CLASS(obj) \ 34ff8eca55SKONRAD Frederic OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS) 35ff8eca55SKONRAD Frederic #define VIRTIO_BUS_CLASS(klass) \ 36ff8eca55SKONRAD Frederic OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS) 37ff8eca55SKONRAD Frederic #define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS) 38ff8eca55SKONRAD Frederic 39ff8eca55SKONRAD Frederic typedef struct VirtioBusState VirtioBusState; 40ff8eca55SKONRAD Frederic 41ff8eca55SKONRAD Frederic typedef struct VirtioBusClass { 42ff8eca55SKONRAD Frederic /* This is what a VirtioBus must implement */ 43ff8eca55SKONRAD Frederic BusClass parent; 44ff8eca55SKONRAD Frederic void (*notify)(DeviceState *d, uint16_t vector); 45ff8eca55SKONRAD Frederic void (*save_config)(DeviceState *d, QEMUFile *f); 46ff8eca55SKONRAD Frederic void (*save_queue)(DeviceState *d, int n, QEMUFile *f); 47a6df8adfSJason Wang void (*save_extra_state)(DeviceState *d, QEMUFile *f); 48ff8eca55SKONRAD Frederic int (*load_config)(DeviceState *d, QEMUFile *f); 49ff8eca55SKONRAD Frederic int (*load_queue)(DeviceState *d, int n, QEMUFile *f); 50ff8eca55SKONRAD Frederic int (*load_done)(DeviceState *d, QEMUFile *f); 51a6df8adfSJason Wang int (*load_extra_state)(DeviceState *d, QEMUFile *f); 52a6df8adfSJason Wang bool (*has_extra_state)(DeviceState *d); 53ff8eca55SKONRAD Frederic bool (*query_guest_notifiers)(DeviceState *d); 54ff8eca55SKONRAD Frederic int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign); 55ff8eca55SKONRAD Frederic void (*vmstate_change)(DeviceState *d, bool running); 56ff8eca55SKONRAD Frederic /* 57d1b4259fSMaxime Coquelin * Expose the features the transport layer supports before 58d1b4259fSMaxime Coquelin * the negotiation takes place. 59d1b4259fSMaxime Coquelin */ 60d1b4259fSMaxime Coquelin void (*pre_plugged)(DeviceState *d, Error **errp); 61d1b4259fSMaxime Coquelin /* 62ff8eca55SKONRAD Frederic * transport independent init function. 63ff8eca55SKONRAD Frederic * This is called by virtio-bus just after the device is plugged. 64ff8eca55SKONRAD Frederic */ 65e8398045SJason Wang void (*device_plugged)(DeviceState *d, Error **errp); 66ff8eca55SKONRAD Frederic /* 67ff8eca55SKONRAD Frederic * transport independent exit function. 68ff8eca55SKONRAD Frederic * This is called by virtio-bus just before the device is unplugged. 69ff8eca55SKONRAD Frederic */ 705e96f5d2SPaolo Bonzini void (*device_unplugged)(DeviceState *d); 71e0d686bfSJason Wang int (*query_nvectors)(DeviceState *d); 726ce69d1cSPeter Maydell /* 736798e245SCornelia Huck * ioeventfd handling: if the transport implements ioeventfd_started, 746798e245SCornelia Huck * it must implement the other ioeventfd callbacks as well 756798e245SCornelia Huck */ 766798e245SCornelia Huck /* Returns true if the ioeventfd has been started for the device. */ 776798e245SCornelia Huck bool (*ioeventfd_started)(DeviceState *d); 786798e245SCornelia Huck /* 796798e245SCornelia Huck * Sets the 'ioeventfd started' state after the ioeventfd has been 806798e245SCornelia Huck * started/stopped for the device. err signifies whether an error 816798e245SCornelia Huck * had occurred. 826798e245SCornelia Huck */ 836798e245SCornelia Huck void (*ioeventfd_set_started)(DeviceState *d, bool started, bool err); 846798e245SCornelia Huck /* Returns true if the ioeventfd has been disabled for the device. */ 856798e245SCornelia Huck bool (*ioeventfd_disabled)(DeviceState *d); 866798e245SCornelia Huck /* 876798e245SCornelia Huck * Assigns/deassigns the ioeventfd backing for the transport on 886798e245SCornelia Huck * the device for queue number n. Returns an error value on 896798e245SCornelia Huck * failure. 906798e245SCornelia Huck */ 916798e245SCornelia Huck int (*ioeventfd_assign)(DeviceState *d, EventNotifier *notifier, 926798e245SCornelia Huck int n, bool assign); 936798e245SCornelia Huck /* 946ce69d1cSPeter Maydell * Does the transport have variable vring alignment? 956ce69d1cSPeter Maydell * (ie can it ever call virtio_queue_set_align()?) 966ce69d1cSPeter Maydell * Note that changing this will break migration for this transport. 976ce69d1cSPeter Maydell */ 986ce69d1cSPeter Maydell bool has_variable_vring_alignment; 99ff8eca55SKONRAD Frederic } VirtioBusClass; 100ff8eca55SKONRAD Frederic 101ff8eca55SKONRAD Frederic struct VirtioBusState { 102ff8eca55SKONRAD Frederic BusState parent_obj; 103*4ddcc2d5SPaolo Bonzini 104*4ddcc2d5SPaolo Bonzini /* 105*4ddcc2d5SPaolo Bonzini * Set if the default ioeventfd handlers are disabled by vhost 106*4ddcc2d5SPaolo Bonzini * or dataplane. 107*4ddcc2d5SPaolo Bonzini */ 108*4ddcc2d5SPaolo Bonzini bool ioeventfd_disabled; 109ff8eca55SKONRAD Frederic }; 110ff8eca55SKONRAD Frederic 111e8398045SJason Wang void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp); 112ff8eca55SKONRAD Frederic void virtio_bus_reset(VirtioBusState *bus); 1135e96f5d2SPaolo Bonzini void virtio_bus_device_unplugged(VirtIODevice *bus); 114ff8eca55SKONRAD Frederic /* Get the device id of the plugged device. */ 115ff8eca55SKONRAD Frederic uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus); 116ff8eca55SKONRAD Frederic /* Get the config_len field of the plugged device. */ 117ff8eca55SKONRAD Frederic size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus); 1188e05db92SKONRAD Frederic /* Get bad features of the plugged device. */ 1198e05db92SKONRAD Frederic uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus); 1208e05db92SKONRAD Frederic /* Get config of the plugged device. */ 1218e05db92SKONRAD Frederic void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config); 1225d448f9dSKONRAD Frederic /* Set config of the plugged device. */ 1235d448f9dSKONRAD Frederic void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config); 124ff8eca55SKONRAD Frederic 12506d3dff0SPaolo Bonzini static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus) 12606d3dff0SPaolo Bonzini { 12706d3dff0SPaolo Bonzini BusState *qbus = &bus->parent_obj; 12806d3dff0SPaolo Bonzini BusChild *kid = QTAILQ_FIRST(&qbus->children); 12906d3dff0SPaolo Bonzini DeviceState *qdev = kid ? kid->child : NULL; 13006d3dff0SPaolo Bonzini 13106d3dff0SPaolo Bonzini /* This is used on the data path, the cast is guaranteed 13206d3dff0SPaolo Bonzini * to succeed by the qdev machinery. 13306d3dff0SPaolo Bonzini */ 13406d3dff0SPaolo Bonzini return (VirtIODevice *)qdev; 13506d3dff0SPaolo Bonzini } 13606d3dff0SPaolo Bonzini 1376798e245SCornelia Huck /* Start the ioeventfd. */ 1386798e245SCornelia Huck void virtio_bus_start_ioeventfd(VirtioBusState *bus); 1396798e245SCornelia Huck /* Stop the ioeventfd. */ 1406798e245SCornelia Huck void virtio_bus_stop_ioeventfd(VirtioBusState *bus); 1416798e245SCornelia Huck /* Switch from/to the generic ioeventfd handler */ 1426798e245SCornelia Huck int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign); 1436798e245SCornelia Huck 144ff8eca55SKONRAD Frederic #endif /* VIRTIO_BUS_H */ 145