vfio_pci_private.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  3. * Author: Alex Williamson <[email protected]>
  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, [email protected]
  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. struct vfio_pci_irq_ctx {
  27. struct eventfd_ctx *trigger;
  28. struct virqfd *unmask;
  29. struct virqfd *mask;
  30. char *name;
  31. bool masked;
  32. struct irq_bypass_producer producer;
  33. };
  34. struct vfio_pci_device;
  35. struct vfio_pci_region;
  36. struct vfio_pci_regops {
  37. size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
  38. size_t count, loff_t *ppos, bool iswrite);
  39. void (*release)(struct vfio_pci_device *vdev,
  40. struct vfio_pci_region *region);
  41. };
  42. struct vfio_pci_region {
  43. u32 type;
  44. u32 subtype;
  45. const struct vfio_pci_regops *ops;
  46. void *data;
  47. size_t size;
  48. u32 flags;
  49. };
  50. struct vfio_pci_dummy_resource {
  51. struct resource resource;
  52. int index;
  53. struct list_head res_next;
  54. };
  55. struct vfio_pci_device {
  56. struct pci_dev *pdev;
  57. void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
  58. bool bar_mmap_supported[PCI_STD_RESOURCE_END + 1];
  59. u8 *pci_config_map;
  60. u8 *vconfig;
  61. struct perm_bits *msi_perm;
  62. spinlock_t irqlock;
  63. struct mutex igate;
  64. struct vfio_pci_irq_ctx *ctx;
  65. int num_ctx;
  66. int irq_type;
  67. int num_regions;
  68. struct vfio_pci_region *region;
  69. u8 msi_qmax;
  70. u8 msix_bar;
  71. u16 msix_size;
  72. u32 msix_offset;
  73. u32 rbar[7];
  74. bool pci_2_3;
  75. bool virq_disabled;
  76. bool reset_works;
  77. bool extended_caps;
  78. bool bardirty;
  79. bool has_vga;
  80. bool needs_reset;
  81. bool nointx;
  82. struct pci_saved_state *pci_saved_state;
  83. int refcnt;
  84. struct eventfd_ctx *err_trigger;
  85. struct eventfd_ctx *req_trigger;
  86. struct list_head dummy_resources_list;
  87. };
  88. #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
  89. #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
  90. #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
  91. #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
  92. #define irq_is(vdev, type) (vdev->irq_type == type)
  93. extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
  94. extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
  95. extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
  96. uint32_t flags, unsigned index,
  97. unsigned start, unsigned count, void *data);
  98. extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
  99. char __user *buf, size_t count,
  100. loff_t *ppos, bool iswrite);
  101. extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
  102. size_t count, loff_t *ppos, bool iswrite);
  103. extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
  104. size_t count, loff_t *ppos, bool iswrite);
  105. extern int vfio_pci_init_perm_bits(void);
  106. extern void vfio_pci_uninit_perm_bits(void);
  107. extern int vfio_config_init(struct vfio_pci_device *vdev);
  108. extern void vfio_config_free(struct vfio_pci_device *vdev);
  109. extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
  110. unsigned int type, unsigned int subtype,
  111. const struct vfio_pci_regops *ops,
  112. size_t size, u32 flags, void *data);
  113. #ifdef CONFIG_VFIO_PCI_IGD
  114. extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
  115. #else
  116. static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
  117. {
  118. return -ENODEV;
  119. }
  120. #endif
  121. #endif /* VFIO_PCI_PRIVATE_H */