virtio.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "block.h"
  20. #include "event_notifier.h"
  21. #ifdef CONFIG_LINUX
  22. #include "9p.h"
  23. #endif
  24. /* from Linux's linux/virtio_config.h */
  25. /* Status byte for guest to report progress, and synchronize features. */
  26. /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
  27. #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
  28. /* We have found a driver for the device. */
  29. #define VIRTIO_CONFIG_S_DRIVER 2
  30. /* Driver has used its parts of the config, and is happy */
  31. #define VIRTIO_CONFIG_S_DRIVER_OK 4
  32. /* We've given up on this device. */
  33. #define VIRTIO_CONFIG_S_FAILED 0x80
  34. /* Some virtio feature bits (currently bits 28 through 31) are reserved for the
  35. * transport being used (eg. virtio_ring), the rest are per-device feature bits. */
  36. #define VIRTIO_TRANSPORT_F_START 28
  37. #define VIRTIO_TRANSPORT_F_END 32
  38. /* We notify when the ring is completely used, even if the guest is suppressing
  39. * callbacks */
  40. #define VIRTIO_F_NOTIFY_ON_EMPTY 24
  41. /* We support indirect buffer descriptors */
  42. #define VIRTIO_RING_F_INDIRECT_DESC 28
  43. /* The Guest publishes the used index for which it expects an interrupt
  44. * at the end of the avail ring. Host should ignore the avail->flags field. */
  45. /* The Host publishes the avail index for which it expects a kick
  46. * at the end of the used ring. Guest should ignore the used->flags field. */
  47. #define VIRTIO_RING_F_EVENT_IDX 29
  48. /* A guest should never accept this. It implies negotiation is broken. */
  49. #define VIRTIO_F_BAD_FEATURE 30
  50. /* from Linux's linux/virtio_ring.h */
  51. /* This marks a buffer as continuing via the next field. */
  52. #define VRING_DESC_F_NEXT 1
  53. /* This marks a buffer as write-only (otherwise read-only). */
  54. #define VRING_DESC_F_WRITE 2
  55. /* This means the buffer contains a list of buffer descriptors. */
  56. #define VRING_DESC_F_INDIRECT 4
  57. /* This means don't notify other side when buffer added. */
  58. #define VRING_USED_F_NO_NOTIFY 1
  59. /* This means don't interrupt guest when buffer consumed. */
  60. #define VRING_AVAIL_F_NO_INTERRUPT 1
  61. struct VirtQueue;
  62. static inline target_phys_addr_t vring_align(target_phys_addr_t addr,
  63. unsigned long align)
  64. {
  65. return (addr + align - 1) & ~(align - 1);
  66. }
  67. typedef struct VirtQueue VirtQueue;
  68. #define VIRTQUEUE_MAX_SIZE 1024
  69. typedef struct VirtQueueElement
  70. {
  71. unsigned int index;
  72. unsigned int out_num;
  73. unsigned int in_num;
  74. target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE];
  75. target_phys_addr_t out_addr[VIRTQUEUE_MAX_SIZE];
  76. struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
  77. struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
  78. } VirtQueueElement;
  79. typedef struct {
  80. void (*notify)(void * opaque, uint16_t vector);
  81. void (*save_config)(void * opaque, QEMUFile *f);
  82. void (*save_queue)(void * opaque, int n, QEMUFile *f);
  83. int (*load_config)(void * opaque, QEMUFile *f);
  84. int (*load_queue)(void * opaque, int n, QEMUFile *f);
  85. int (*load_done)(void * opaque, QEMUFile *f);
  86. unsigned (*get_features)(void * opaque);
  87. bool (*query_guest_notifiers)(void * opaque);
  88. int (*set_guest_notifiers)(void * opaque, bool assigned);
  89. int (*set_host_notifier)(void * opaque, int n, bool assigned);
  90. void (*vmstate_change)(void * opaque, bool running);
  91. } VirtIOBindings;
  92. #define VIRTIO_PCI_QUEUE_MAX 64
  93. #define VIRTIO_NO_VECTOR 0xffff
  94. struct VirtIODevice
  95. {
  96. const char *name;
  97. uint8_t status;
  98. uint8_t isr;
  99. uint16_t queue_sel;
  100. uint32_t guest_features;
  101. size_t config_len;
  102. void *config;
  103. uint16_t config_vector;
  104. int nvectors;
  105. uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
  106. uint32_t (*bad_features)(VirtIODevice *vdev);
  107. void (*set_features)(VirtIODevice *vdev, uint32_t val);
  108. void (*get_config)(VirtIODevice *vdev, uint8_t *config);
  109. void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
  110. void (*reset)(VirtIODevice *vdev);
  111. void (*set_status)(VirtIODevice *vdev, uint8_t val);
  112. VirtQueue *vq;
  113. const VirtIOBindings *binding;
  114. void *binding_opaque;
  115. uint16_t device_id;
  116. bool vm_running;
  117. VMChangeStateEntry *vmstate;
  118. };
  119. VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
  120. void (*handle_output)(VirtIODevice *,
  121. VirtQueue *));
  122. void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
  123. unsigned int len);
  124. void virtqueue_flush(VirtQueue *vq, unsigned int count);
  125. void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
  126. unsigned int len, unsigned int idx);
  127. void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr,
  128. size_t num_sg, int is_write);
  129. int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
  130. int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes);
  131. void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
  132. void virtio_save(VirtIODevice *vdev, QEMUFile *f);
  133. int virtio_load(VirtIODevice *vdev, QEMUFile *f);
  134. void virtio_cleanup(VirtIODevice *vdev);
  135. void virtio_notify_config(VirtIODevice *vdev);
  136. void virtio_queue_set_notification(VirtQueue *vq, int enable);
  137. int virtio_queue_ready(VirtQueue *vq);
  138. int virtio_queue_empty(VirtQueue *vq);
  139. /* Host binding interface. */
  140. VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
  141. size_t config_size, size_t struct_size);
  142. uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
  143. uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
  144. uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
  145. void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  146. void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  147. void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
  148. void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr);
  149. target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n);
  150. int virtio_queue_get_num(VirtIODevice *vdev, int n);
  151. void virtio_queue_notify(VirtIODevice *vdev, int n);
  152. uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
  153. void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
  154. void virtio_set_status(VirtIODevice *vdev, uint8_t val);
  155. void virtio_reset(void *opaque);
  156. void virtio_update_irq(VirtIODevice *vdev);
  157. int virtio_set_features(VirtIODevice *vdev, uint32_t val);
  158. void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
  159. void *opaque);
  160. /* Base devices. */
  161. VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
  162. char **serial);
  163. struct virtio_net_conf;
  164. VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
  165. struct virtio_net_conf *net);
  166. typedef struct virtio_serial_conf virtio_serial_conf;
  167. VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
  168. VirtIODevice *virtio_balloon_init(DeviceState *dev);
  169. #ifdef CONFIG_LINUX
  170. VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
  171. #endif
  172. void virtio_net_exit(VirtIODevice *vdev);
  173. void virtio_blk_exit(VirtIODevice *vdev);
  174. void virtio_serial_exit(VirtIODevice *vdev);
  175. void virtio_balloon_exit(VirtIODevice *vdev);
  176. #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
  177. DEFINE_PROP_BIT("indirect_desc", _state, _field, \
  178. VIRTIO_RING_F_INDIRECT_DESC, true), \
  179. DEFINE_PROP_BIT("event_idx", _state, _field, \
  180. VIRTIO_RING_F_EVENT_IDX, true)
  181. target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
  182. target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
  183. target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
  184. target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n);
  185. target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
  186. target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
  187. target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n);
  188. target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n);
  189. uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
  190. void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
  191. VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
  192. EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
  193. EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
  194. void virtio_queue_notify_vq(VirtQueue *vq);
  195. void virtio_irq(VirtQueue *vq);
  196. #endif