pciback.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * PCI Backend Common Data Structures & Function Declarations
  4. *
  5. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  6. */
  7. #ifndef __XEN_PCIBACK_H__
  8. #define __XEN_PCIBACK_H__
  9. #include <linux/pci.h>
  10. #include <linux/interrupt.h>
  11. #include <xen/xenbus.h>
  12. #include <linux/list.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/atomic.h>
  16. #include <xen/events.h>
  17. #include <xen/interface/io/pciif.h>
  18. #define DRV_NAME "xen-pciback"
  19. struct pci_dev_entry {
  20. struct list_head list;
  21. struct pci_dev *dev;
  22. };
  23. #define _PDEVF_op_active (0)
  24. #define PDEVF_op_active (1<<(_PDEVF_op_active))
  25. #define _PCIB_op_pending (1)
  26. #define PCIB_op_pending (1<<(_PCIB_op_pending))
  27. #define _EOI_pending (2)
  28. #define EOI_pending (1<<(_EOI_pending))
  29. struct xen_pcibk_device {
  30. void *pci_dev_data;
  31. struct mutex dev_lock;
  32. struct xenbus_device *xdev;
  33. struct xenbus_watch be_watch;
  34. u8 be_watching;
  35. int evtchn_irq;
  36. struct xen_pci_sharedinfo *sh_info;
  37. unsigned long flags;
  38. struct work_struct op_work;
  39. struct xen_pci_op op;
  40. };
  41. struct xen_pcibk_dev_data {
  42. struct list_head config_fields;
  43. struct pci_saved_state *pci_saved_state;
  44. unsigned int permissive:1;
  45. unsigned int warned_on_write:1;
  46. unsigned int enable_intx:1;
  47. unsigned int isr_on:1; /* Whether the IRQ handler is installed. */
  48. unsigned int ack_intr:1; /* .. and ACK-ing */
  49. unsigned long handled;
  50. unsigned int irq; /* Saved in case device transitions to MSI/MSI-X */
  51. char irq_name[0]; /* xen-pcibk[000:04:00.0] */
  52. };
  53. /* Used by XenBus and xen_pcibk_ops.c */
  54. extern wait_queue_head_t xen_pcibk_aer_wait_queue;
  55. /* Used by pcistub.c and conf_space_quirks.c */
  56. extern struct list_head xen_pcibk_quirks;
  57. /* Get/Put PCI Devices that are hidden from the PCI Backend Domain */
  58. struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
  59. int domain, int bus,
  60. int slot, int func);
  61. struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
  62. struct pci_dev *dev);
  63. void pcistub_put_pci_dev(struct pci_dev *dev);
  64. /* Ensure a device is turned off or reset */
  65. void xen_pcibk_reset_device(struct pci_dev *pdev);
  66. /* Access a virtual configuration space for a PCI device */
  67. int xen_pcibk_config_init(void);
  68. int xen_pcibk_config_init_dev(struct pci_dev *dev);
  69. void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev);
  70. void xen_pcibk_config_reset_dev(struct pci_dev *dev);
  71. void xen_pcibk_config_free_dev(struct pci_dev *dev);
  72. int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
  73. u32 *ret_val);
  74. int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size,
  75. u32 value);
  76. /* Handle requests for specific devices from the frontend */
  77. typedef int (*publish_pci_dev_cb) (struct xen_pcibk_device *pdev,
  78. unsigned int domain, unsigned int bus,
  79. unsigned int devfn, unsigned int devid);
  80. typedef int (*publish_pci_root_cb) (struct xen_pcibk_device *pdev,
  81. unsigned int domain, unsigned int bus);
  82. /* Backend registration for the two types of BDF representation:
  83. * vpci - BDFs start at 00
  84. * passthrough - BDFs are exactly like in the host.
  85. */
  86. struct xen_pcibk_backend {
  87. const char *name;
  88. int (*init)(struct xen_pcibk_device *pdev);
  89. void (*free)(struct xen_pcibk_device *pdev);
  90. int (*find)(struct pci_dev *pcidev, struct xen_pcibk_device *pdev,
  91. unsigned int *domain, unsigned int *bus,
  92. unsigned int *devfn);
  93. int (*publish)(struct xen_pcibk_device *pdev, publish_pci_root_cb cb);
  94. void (*release)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
  95. bool lock);
  96. int (*add)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
  97. int devid, publish_pci_dev_cb publish_cb);
  98. struct pci_dev *(*get)(struct xen_pcibk_device *pdev,
  99. unsigned int domain, unsigned int bus,
  100. unsigned int devfn);
  101. };
  102. extern const struct xen_pcibk_backend xen_pcibk_vpci_backend;
  103. extern const struct xen_pcibk_backend xen_pcibk_passthrough_backend;
  104. extern const struct xen_pcibk_backend *xen_pcibk_backend;
  105. static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
  106. struct pci_dev *dev,
  107. int devid,
  108. publish_pci_dev_cb publish_cb)
  109. {
  110. if (xen_pcibk_backend && xen_pcibk_backend->add)
  111. return xen_pcibk_backend->add(pdev, dev, devid, publish_cb);
  112. return -1;
  113. }
  114. static inline void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
  115. struct pci_dev *dev, bool lock)
  116. {
  117. if (xen_pcibk_backend && xen_pcibk_backend->release)
  118. return xen_pcibk_backend->release(pdev, dev, lock);
  119. }
  120. static inline struct pci_dev *
  121. xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, unsigned int domain,
  122. unsigned int bus, unsigned int devfn)
  123. {
  124. if (xen_pcibk_backend && xen_pcibk_backend->get)
  125. return xen_pcibk_backend->get(pdev, domain, bus, devfn);
  126. return NULL;
  127. }
  128. /**
  129. * Add for domain0 PCIE-AER handling. Get guest domain/bus/devfn in xen_pcibk
  130. * before sending aer request to pcifront, so that guest could identify
  131. * device, coopearte with xen_pcibk to finish aer recovery job if device driver
  132. * has the capability
  133. */
  134. static inline int xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
  135. struct xen_pcibk_device *pdev,
  136. unsigned int *domain,
  137. unsigned int *bus,
  138. unsigned int *devfn)
  139. {
  140. if (xen_pcibk_backend && xen_pcibk_backend->find)
  141. return xen_pcibk_backend->find(pcidev, pdev, domain, bus,
  142. devfn);
  143. return -1;
  144. }
  145. static inline int xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
  146. {
  147. if (xen_pcibk_backend && xen_pcibk_backend->init)
  148. return xen_pcibk_backend->init(pdev);
  149. return -1;
  150. }
  151. static inline int xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
  152. publish_pci_root_cb cb)
  153. {
  154. if (xen_pcibk_backend && xen_pcibk_backend->publish)
  155. return xen_pcibk_backend->publish(pdev, cb);
  156. return -1;
  157. }
  158. static inline void xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
  159. {
  160. if (xen_pcibk_backend && xen_pcibk_backend->free)
  161. return xen_pcibk_backend->free(pdev);
  162. }
  163. /* Handles events from front-end */
  164. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id);
  165. void xen_pcibk_do_op(struct work_struct *data);
  166. static inline void xen_pcibk_lateeoi(struct xen_pcibk_device *pdev,
  167. unsigned int eoi_flag)
  168. {
  169. if (test_and_clear_bit(_EOI_pending, &pdev->flags))
  170. xen_irq_lateeoi(pdev->evtchn_irq, eoi_flag);
  171. }
  172. int xen_pcibk_xenbus_register(void);
  173. void xen_pcibk_xenbus_unregister(void);
  174. extern int verbose_request;
  175. #endif
  176. /* Handles shared IRQs that can to device domain and control domain. */
  177. void xen_pcibk_irq_handler(struct pci_dev *dev, int reset);