virtio.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.h"
  17. #include "qdev.h"
  18. #include "sysemu.h"
  19. #include "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 target_phys_addr_t vring_align(target_phys_addr_t 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. target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE];
  74. target_phys_addr_t 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)(void * opaque, uint16_t vector);
  80. void (*save_config)(void * opaque, QEMUFile *f);
  81. void (*save_queue)(void * opaque, int n, QEMUFile *f);
  82. int (*load_config)(void * opaque, QEMUFile *f);
  83. int (*load_queue)(void * opaque, int n, QEMUFile *f);
  84. int (*load_done)(void * opaque, QEMUFile *f);
  85. unsigned (*get_features)(void * opaque);
  86. bool (*query_guest_notifiers)(void * opaque);
  87. int (*set_guest_notifiers)(void * opaque, bool assigned);
  88. int (*set_host_notifier)(void * opaque, int n, bool assigned);
  89. void (*vmstate_change)(void * opaque, bool running);
  90. } VirtIOBindings;
  91. #define VIRTIO_PCI_QUEUE_MAX 64
  92. #define VIRTIO_NO_VECTOR 0xffff
  93. struct VirtIODevice
  94. {
  95. const char *name;
  96. uint8_t status;
  97. uint8_t isr;
  98. uint16_t queue_sel;
  99. uint32_t guest_features;
  100. size_t config_len;
  101. void *config;
  102. uint16_t config_vector;
  103. int nvectors;
  104. uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
  105. uint32_t (*bad_features)(VirtIODevice *vdev);
  106. void (*set_features)(VirtIODevice *vdev, uint32_t val);
  107. void (*get_config)(VirtIODevice *vdev, uint8_t *config);
  108. void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
  109. void (*reset)(VirtIODevice *vdev);
  110. void (*set_status)(VirtIODevice *vdev, uint8_t val);
  111. VirtQueue *vq;
  112. const VirtIOBindings *binding;
  113. void *binding_opaque;
  114. uint16_t device_id;
  115. bool vm_running;
  116. VMChangeStateEntry *vmstate;
  117. };
  118. VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
  119. void (*handle_output)(VirtIODevice *,
  120. VirtQueue *));
  121. void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
  122. unsigned int len);
  123. void virtqueue_flush(VirtQueue *vq, unsigned int count);
  124. void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
  125. unsigned int len, unsigned int idx);
  126. void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr,
  127. size_t num_sg, int is_write);
  128. int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
  129. int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes);
  130. void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
  131. void virtio_save(VirtIODevice *vdev, QEMUFile *f);
  132. int virtio_load(VirtIODevice *vdev, QEMUFile *f);
  133. void virtio_cleanup(VirtIODevice *vdev);
  134. void virtio_notify_config(VirtIODevice *vdev);
  135. void virtio_queue_set_notification(VirtQueue *vq, int enable);
  136. int virtio_queue_ready(VirtQueue *vq);
  137. int virtio_queue_empty(VirtQueue *vq);
  138. /* Host binding interface. */
  139. VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
  140. size_t config_size, size_t struct_size);
  141. uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
  142. uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
  143. uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
  144. void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  145. void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  146. void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  147. void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr);
  148. target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n);
  149. int virtio_queue_get_num(VirtIODevice *vdev, int n);
  150. void virtio_queue_notify(VirtIODevice *vdev, int n);
  151. uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
  152. void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
  153. void virtio_set_status(VirtIODevice *vdev, uint8_t val);
  154. void virtio_reset(void *opaque);
  155. void virtio_update_irq(VirtIODevice *vdev);
  156. int virtio_set_features(VirtIODevice *vdev, uint32_t val);
  157. void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
  158. void *opaque);
  159. /* Base devices. */
  160. typedef struct VirtIOBlkConf VirtIOBlkConf;
  161. VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk);
  162. struct virtio_net_conf;
  163. VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
  164. struct virtio_net_conf *net);
  165. typedef struct virtio_serial_conf virtio_serial_conf;
  166. VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
  167. VirtIODevice *virtio_balloon_init(DeviceState *dev);
  168. typedef struct VirtIOSCSIConf VirtIOSCSIConf;
  169. VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *conf);
  170. #ifdef CONFIG_LINUX
  171. VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
  172. #endif
  173. void virtio_net_exit(VirtIODevice *vdev);
  174. void virtio_blk_exit(VirtIODevice *vdev);
  175. void virtio_serial_exit(VirtIODevice *vdev);
  176. void virtio_balloon_exit(VirtIODevice *vdev);
  177. void virtio_scsi_exit(VirtIODevice *vdev);
  178. #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
  179. DEFINE_PROP_BIT("indirect_desc", _state, _field, \
  180. VIRTIO_RING_F_INDIRECT_DESC, true), \
  181. DEFINE_PROP_BIT("event_idx", _state, _field, \
  182. VIRTIO_RING_F_EVENT_IDX, true)
  183. target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
  184. target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
  185. target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
  186. target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n);
  187. target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
  188. target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
  189. target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n);
  190. target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n);
  191. uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
  192. void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
  193. VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
  194. int virtio_queue_get_id(VirtQueue *vq);
  195. EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
  196. void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
  197. bool with_irqfd);
  198. EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
  199. void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
  200. bool set_handler);
  201. void virtio_queue_notify_vq(VirtQueue *vq);
  202. void virtio_irq(VirtQueue *vq);
  203. #endif