vfio_pci_private.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  3. * Author: Alex Williamson <alex.williamson@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Derived from original vfio:
  10. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  11. * Author: Tom Lyon, pugs@cisco.com
  12. */
  13. #include <linux/mutex.h>
  14. #include <linux/pci.h>
  15. #include <linux/irqbypass.h>
  16. #include <linux/types.h>
  17. #ifndef VFIO_PCI_PRIVATE_H
  18. #define VFIO_PCI_PRIVATE_H
  19. #define VFIO_PCI_OFFSET_SHIFT 40
  20. #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
  21. #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
  22. #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
  23. /* Special capability IDs predefined access */
  24. #define PCI_CAP_ID_INVALID 0xFF /* default raw access */
  25. #define PCI_CAP_ID_INVALID_VIRT 0xFE /* default virt access */
  26. /* Cap maximum number of ioeventfds per device (arbitrary) */
  27. #define VFIO_PCI_IOEVENTFD_MAX 1000
  28. struct vfio_pci_ioeventfd {
  29. struct list_head next;
  30. struct virqfd *virqfd;
  31. void __iomem *addr;
  32. uint64_t data;
  33. loff_t pos;
  34. int bar;
  35. int count;
  36. };
  37. struct vfio_pci_irq_ctx {
  38. struct eventfd_ctx *trigger;
  39. struct virqfd *unmask;
  40. struct virqfd *mask;
  41. char *name;
  42. bool masked;
  43. struct irq_bypass_producer producer;
  44. };
  45. struct vfio_pci_device;
  46. struct vfio_pci_region;
  47. struct vfio_pci_regops {
  48. size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
  49. size_t count, loff_t *ppos, bool iswrite);
  50. void (*release)(struct vfio_pci_device *vdev,
  51. struct vfio_pci_region *region);
  52. };
  53. struct vfio_pci_region {
  54. u32 type;
  55. u32 subtype;
  56. const struct vfio_pci_regops *ops;
  57. void *data;
  58. size_t size;
  59. u32 flags;
  60. };
  61. struct vfio_pci_dummy_resource {
  62. struct resource resource;
  63. int index;
  64. struct list_head res_next;
  65. };
  66. struct vfio_pci_mmap_vma {
  67. struct vm_area_struct *vma;
  68. struct list_head vma_next;
  69. };
  70. struct vfio_pci_device {
  71. struct pci_dev *pdev;
  72. void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
  73. bool bar_mmap_supported[PCI_STD_RESOURCE_END + 1];
  74. u8 *pci_config_map;
  75. u8 *vconfig;
  76. struct perm_bits *msi_perm;
  77. spinlock_t irqlock;
  78. struct mutex igate;
  79. struct vfio_pci_irq_ctx *ctx;
  80. int num_ctx;
  81. int irq_type;
  82. int num_regions;
  83. struct vfio_pci_region *region;
  84. u8 msi_qmax;
  85. u8 msix_bar;
  86. u16 msix_size;
  87. u32 msix_offset;
  88. u32 rbar[7];
  89. bool pci_2_3;
  90. bool virq_disabled;
  91. bool reset_works;
  92. bool extended_caps;
  93. bool bardirty;
  94. bool has_vga;
  95. bool needs_reset;
  96. bool nointx;
  97. struct pci_saved_state *pci_saved_state;
  98. int refcnt;
  99. int ioeventfds_nr;
  100. struct eventfd_ctx *err_trigger;
  101. struct eventfd_ctx *req_trigger;
  102. struct list_head dummy_resources_list;
  103. struct mutex ioeventfds_lock;
  104. struct list_head ioeventfds_list;
  105. struct mutex vma_lock;
  106. struct list_head vma_list;
  107. struct rw_semaphore memory_lock;
  108. };
  109. #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
  110. #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
  111. #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
  112. #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
  113. #define irq_is(vdev, type) (vdev->irq_type == type)
  114. extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
  115. extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
  116. extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
  117. uint32_t flags, unsigned index,
  118. unsigned start, unsigned count, void *data);
  119. extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
  120. char __user *buf, size_t count,
  121. loff_t *ppos, bool iswrite);
  122. extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
  123. size_t count, loff_t *ppos, bool iswrite);
  124. extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
  125. size_t count, loff_t *ppos, bool iswrite);
  126. extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
  127. uint64_t data, int count, int fd);
  128. extern int vfio_pci_init_perm_bits(void);
  129. extern void vfio_pci_uninit_perm_bits(void);
  130. extern int vfio_config_init(struct vfio_pci_device *vdev);
  131. extern void vfio_config_free(struct vfio_pci_device *vdev);
  132. extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
  133. unsigned int type, unsigned int subtype,
  134. const struct vfio_pci_regops *ops,
  135. size_t size, u32 flags, void *data);
  136. extern bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev);
  137. extern void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device
  138. *vdev);
  139. extern u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev);
  140. extern void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev,
  141. u16 cmd);
  142. #ifdef CONFIG_VFIO_PCI_IGD
  143. extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
  144. #else
  145. static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
  146. {
  147. return -ENODEV;
  148. }
  149. #endif
  150. #endif /* VFIO_PCI_PRIVATE_H */