virtio_ring.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_VIRTIO_RING_H
  3. #define _LINUX_VIRTIO_RING_H
  4. #include <asm/barrier.h>
  5. #include <linux/irqreturn.h>
  6. #include <uapi/linux/virtio_ring.h>
  7. /*
  8. * Barriers in virtio are tricky. Non-SMP virtio guests can't assume
  9. * they're not on an SMP host system, so they need to assume real
  10. * barriers. Non-SMP virtio hosts could skip the barriers, but does
  11. * anyone care?
  12. *
  13. * For virtio_pci on SMP, we don't need to order with respect to MMIO
  14. * accesses through relaxed memory I/O windows, so virt_mb() et al are
  15. * sufficient.
  16. *
  17. * For using virtio to talk to real devices (eg. other heterogeneous
  18. * CPUs) we do need real barriers. In theory, we could be using both
  19. * kinds of virtio, so it's a runtime decision, and the branch is
  20. * actually quite cheap.
  21. */
  22. static inline void virtio_mb(bool weak_barriers)
  23. {
  24. if (weak_barriers)
  25. virt_mb();
  26. else
  27. mb();
  28. }
  29. static inline void virtio_rmb(bool weak_barriers)
  30. {
  31. if (weak_barriers)
  32. virt_rmb();
  33. else
  34. dma_rmb();
  35. }
  36. static inline void virtio_wmb(bool weak_barriers)
  37. {
  38. if (weak_barriers)
  39. virt_wmb();
  40. else
  41. dma_wmb();
  42. }
  43. #define virtio_store_mb(weak_barriers, p, v) \
  44. do { \
  45. if (weak_barriers) { \
  46. virt_store_mb(*p, v); \
  47. } else { \
  48. WRITE_ONCE(*p, v); \
  49. mb(); \
  50. } \
  51. } while (0) \
  52. struct virtio_device;
  53. struct virtqueue;
  54. /*
  55. * Creates a virtqueue and allocates the descriptor ring. If
  56. * may_reduce_num is set, then this may allocate a smaller ring than
  57. * expected. The caller should query virtqueue_get_vring_size to learn
  58. * the actual size of the ring.
  59. */
  60. struct virtqueue *vring_create_virtqueue(unsigned int index,
  61. unsigned int num,
  62. unsigned int vring_align,
  63. struct virtio_device *vdev,
  64. bool weak_barriers,
  65. bool may_reduce_num,
  66. bool ctx,
  67. bool (*notify)(struct virtqueue *vq),
  68. void (*callback)(struct virtqueue *vq),
  69. const char *name);
  70. /*
  71. * Creates a virtqueue with a standard layout but a caller-allocated
  72. * ring.
  73. */
  74. struct virtqueue *vring_new_virtqueue(unsigned int index,
  75. unsigned int num,
  76. unsigned int vring_align,
  77. struct virtio_device *vdev,
  78. bool weak_barriers,
  79. bool ctx,
  80. void *pages,
  81. bool (*notify)(struct virtqueue *vq),
  82. void (*callback)(struct virtqueue *vq),
  83. const char *name);
  84. /*
  85. * Destroys a virtqueue. If created with vring_create_virtqueue, this
  86. * also frees the ring.
  87. */
  88. void vring_del_virtqueue(struct virtqueue *vq);
  89. /* Filter out transport-specific feature bits. */
  90. void vring_transport_features(struct virtio_device *vdev);
  91. irqreturn_t vring_interrupt(int irq, void *_vq);
  92. #endif /* _LINUX_VIRTIO_RING_H */