virtio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Virtio Support
  3. *
  4. * Copyright IBM, Corp. 2007
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. #ifndef _QEMU_VIRTIO_H
  14. #define _QEMU_VIRTIO_H
  15. #include "hw.h"
  16. #include "net/net.h"
  17. #include "qdev.h"
  18. #include "sysemu/sysemu.h"
  19. #include "qemu/event_notifier.h"
  20. #ifdef CONFIG_LINUX
  21. #include "9p.h"
  22. #endif
  23. /* from Linux's linux/virtio_config.h */
  24. /* Status byte for guest to report progress, and synchronize features. */
  25. /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
  26. #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
  27. /* We have found a driver for the device. */
  28. #define VIRTIO_CONFIG_S_DRIVER 2
  29. /* Driver has used its parts of the config, and is happy */
  30. #define VIRTIO_CONFIG_S_DRIVER_OK 4
  31. /* We've given up on this device. */
  32. #define VIRTIO_CONFIG_S_FAILED 0x80
  33. /* Some virtio feature bits (currently bits 28 through 31) are reserved for the
  34. * transport being used (eg. virtio_ring), the rest are per-device feature bits. */
  35. #define VIRTIO_TRANSPORT_F_START 28
  36. #define VIRTIO_TRANSPORT_F_END 32
  37. /* We notify when the ring is completely used, even if the guest is suppressing
  38. * callbacks */
  39. #define VIRTIO_F_NOTIFY_ON_EMPTY 24
  40. /* We support indirect buffer descriptors */
  41. #define VIRTIO_RING_F_INDIRECT_DESC 28
  42. /* The Guest publishes the used index for which it expects an interrupt
  43. * at the end of the avail ring. Host should ignore the avail->flags field. */
  44. /* The Host publishes the avail index for which it expects a kick
  45. * at the end of the used ring. Guest should ignore the used->flags field. */
  46. #define VIRTIO_RING_F_EVENT_IDX 29
  47. /* A guest should never accept this. It implies negotiation is broken. */
  48. #define VIRTIO_F_BAD_FEATURE 30
  49. /* from Linux's linux/virtio_ring.h */
  50. /* This marks a buffer as continuing via the next field. */
  51. #define VRING_DESC_F_NEXT 1
  52. /* This marks a buffer as write-only (otherwise read-only). */
  53. #define VRING_DESC_F_WRITE 2
  54. /* This means the buffer contains a list of buffer descriptors. */
  55. #define VRING_DESC_F_INDIRECT 4
  56. /* This means don't notify other side when buffer added. */
  57. #define VRING_USED_F_NO_NOTIFY 1
  58. /* This means don't interrupt guest when buffer consumed. */
  59. #define VRING_AVAIL_F_NO_INTERRUPT 1
  60. struct VirtQueue;
  61. static inline hwaddr vring_align(hwaddr addr,
  62. unsigned long align)
  63. {
  64. return (addr + align - 1) & ~(align - 1);
  65. }
  66. typedef struct VirtQueue VirtQueue;
  67. #define VIRTQUEUE_MAX_SIZE 1024
  68. typedef struct VirtQueueElement
  69. {
  70. unsigned int index;
  71. unsigned int out_num;
  72. unsigned int in_num;
  73. hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
  74. hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
  75. struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
  76. struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
  77. } VirtQueueElement;
  78. typedef struct {
  79. void (*notify)(DeviceState *d, uint16_t vector);
  80. void (*save_config)(DeviceState *d, QEMUFile *f);
  81. void (*save_queue)(DeviceState *d, int n, QEMUFile *f);
  82. int (*load_config)(DeviceState *d, QEMUFile *f);
  83. int (*load_queue)(DeviceState *d, int n, QEMUFile *f);
  84. int (*load_done)(DeviceState *d, QEMUFile *f);
  85. unsigned (*get_features)(DeviceState *d);
  86. bool (*query_guest_notifiers)(DeviceState *d);
  87. int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assigned);
  88. int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
  89. void (*vmstate_change)(DeviceState *d, bool running);
  90. } VirtIOBindings;
  91. #define VIRTIO_PCI_QUEUE_MAX 64
  92. #define VIRTIO_NO_VECTOR 0xffff
  93. #define TYPE_VIRTIO_DEVICE "virtio-device"
  94. #define VIRTIO_DEVICE_GET_CLASS(obj) \
  95. OBJECT_GET_CLASS(VirtioDeviceClass, obj, TYPE_VIRTIO_DEVICE)
  96. #define VIRTIO_DEVICE_CLASS(klass) \
  97. OBJECT_CLASS_CHECK(VirtioDeviceClass, klass, TYPE_VIRTIO_DEVICE)
  98. #define VIRTIO_DEVICE(obj) \
  99. OBJECT_CHECK(VirtIODevice, (obj), TYPE_VIRTIO_DEVICE)
  100. struct VirtIODevice
  101. {
  102. DeviceState parent_obj;
  103. const char *name;
  104. uint8_t status;
  105. uint8_t isr;
  106. uint16_t queue_sel;
  107. uint32_t guest_features;
  108. size_t config_len;
  109. void *config;
  110. uint16_t config_vector;
  111. int nvectors;
  112. /*
  113. * Function pointers will be removed at the end of the series as they are in
  114. * VirtioDeviceClass.
  115. */
  116. uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
  117. uint32_t (*bad_features)(VirtIODevice *vdev);
  118. void (*set_features)(VirtIODevice *vdev, uint32_t val);
  119. void (*get_config)(VirtIODevice *vdev, uint8_t *config);
  120. void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
  121. void (*reset)(VirtIODevice *vdev);
  122. void (*set_status)(VirtIODevice *vdev, uint8_t val);
  123. /* Test and clear event pending status.
  124. * Should be called after unmask to avoid losing events.
  125. * If backend does not support masking,
  126. * must check in frontend instead.
  127. */
  128. bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
  129. /* Mask/unmask events from this vq. Any events reported
  130. * while masked will become pending.
  131. * If backend does not support masking,
  132. * must mask in frontend instead.
  133. */
  134. void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
  135. VirtQueue *vq;
  136. const VirtIOBindings *binding;
  137. DeviceState *binding_opaque;
  138. uint16_t device_id;
  139. bool vm_running;
  140. VMChangeStateEntry *vmstate;
  141. };
  142. typedef struct VirtioDeviceClass {
  143. /* This is what a VirtioDevice must implement */
  144. DeviceClass parent;
  145. int (*init)(VirtIODevice *vdev);
  146. uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
  147. uint32_t (*bad_features)(VirtIODevice *vdev);
  148. void (*set_features)(VirtIODevice *vdev, uint32_t val);
  149. void (*get_config)(VirtIODevice *vdev, uint8_t *config);
  150. void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
  151. void (*reset)(VirtIODevice *vdev);
  152. void (*set_status)(VirtIODevice *vdev, uint8_t val);
  153. } VirtioDeviceClass;
  154. void virtio_init(VirtIODevice *vdev, const char *name,
  155. uint16_t device_id, size_t config_size);
  156. void virtio_common_cleanup(VirtIODevice *vdev);
  157. VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
  158. void (*handle_output)(VirtIODevice *,
  159. VirtQueue *));
  160. void virtio_del_queue(VirtIODevice *vdev, int n);
  161. void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
  162. unsigned int len);
  163. void virtqueue_flush(VirtQueue *vq, unsigned int count);
  164. void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
  165. unsigned int len, unsigned int idx);
  166. void virtqueue_map_sg(struct iovec *sg, hwaddr *addr,
  167. size_t num_sg, int is_write);
  168. int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
  169. int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
  170. unsigned int out_bytes);
  171. void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
  172. unsigned int *out_bytes,
  173. unsigned max_in_bytes, unsigned max_out_bytes);
  174. void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
  175. void virtio_save(VirtIODevice *vdev, QEMUFile *f);
  176. int virtio_load(VirtIODevice *vdev, QEMUFile *f);
  177. void virtio_cleanup(VirtIODevice *vdev);
  178. void virtio_notify_config(VirtIODevice *vdev);
  179. void virtio_queue_set_notification(VirtQueue *vq, int enable);
  180. int virtio_queue_ready(VirtQueue *vq);
  181. int virtio_queue_empty(VirtQueue *vq);
  182. /* Host binding interface. */
  183. VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
  184. size_t config_size, size_t struct_size);
  185. uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
  186. uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
  187. uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
  188. void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  189. void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  190. void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  191. void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
  192. hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
  193. int virtio_queue_get_num(VirtIODevice *vdev, int n);
  194. void virtio_queue_notify(VirtIODevice *vdev, int n);
  195. uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
  196. void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
  197. void virtio_set_status(VirtIODevice *vdev, uint8_t val);
  198. void virtio_reset(void *opaque);
  199. void virtio_update_irq(VirtIODevice *vdev);
  200. int virtio_set_features(VirtIODevice *vdev, uint32_t val);
  201. void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
  202. DeviceState *opaque);
  203. /* Base devices. */
  204. typedef struct VirtIOBlkConf VirtIOBlkConf;
  205. VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk);
  206. struct virtio_net_conf;
  207. VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
  208. struct virtio_net_conf *net,
  209. uint32_t host_features);
  210. typedef struct virtio_serial_conf virtio_serial_conf;
  211. VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
  212. VirtIODevice *virtio_balloon_init(DeviceState *dev);
  213. typedef struct VirtIOSCSIConf VirtIOSCSIConf;
  214. VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *conf);
  215. typedef struct VirtIORNGConf VirtIORNGConf;
  216. VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf);
  217. #ifdef CONFIG_LINUX
  218. VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
  219. #endif
  220. void virtio_net_exit(VirtIODevice *vdev);
  221. void virtio_blk_exit(VirtIODevice *vdev);
  222. void virtio_serial_exit(VirtIODevice *vdev);
  223. void virtio_balloon_exit(VirtIODevice *vdev);
  224. void virtio_scsi_exit(VirtIODevice *vdev);
  225. void virtio_rng_exit(VirtIODevice *vdev);
  226. #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
  227. DEFINE_PROP_BIT("indirect_desc", _state, _field, \
  228. VIRTIO_RING_F_INDIRECT_DESC, true), \
  229. DEFINE_PROP_BIT("event_idx", _state, _field, \
  230. VIRTIO_RING_F_EVENT_IDX, true)
  231. hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
  232. hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
  233. hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
  234. hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n);
  235. hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
  236. hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
  237. hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
  238. hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n);
  239. uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
  240. void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
  241. VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
  242. uint16_t virtio_get_queue_index(VirtQueue *vq);
  243. int virtio_queue_get_id(VirtQueue *vq);
  244. EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
  245. void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
  246. bool with_irqfd);
  247. EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
  248. void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
  249. bool set_handler);
  250. void virtio_queue_notify_vq(VirtQueue *vq);
  251. void virtio_irq(VirtQueue *vq);
  252. #endif