pciback.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * PCI Backend Common Data Structures & Function Declarations
  4. *
  5. * Author: Ryan Wilson <[email protected]>
  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 allow_interrupt_control:1;
  46. unsigned int warned_on_write:1;
  47. unsigned int enable_intx:1;
  48. unsigned int isr_on:1; /* Whether the IRQ handler is installed. */
  49. unsigned int ack_intr:1; /* .. and ACK-ing */
  50. unsigned long handled;
  51. unsigned int irq; /* Saved in case device transitions to MSI/MSI-X */
  52. char irq_name[]; /* xen-pcibk[000:04:00.0] */
  53. };
  54. /* Used by XenBus and xen_pcibk_ops.c */
  55. extern wait_queue_head_t xen_pcibk_aer_wait_queue;
  56. /* Used by pcistub.c and conf_space_quirks.c */
  57. extern struct list_head xen_pcibk_quirks;
  58. /* Get/Put PCI Devices that are hidden from the PCI Backend Domain */
  59. struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
  60. int domain, int bus,
  61. int slot, int func);
  62. struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
  63. struct pci_dev *dev);
  64. void pcistub_put_pci_dev(struct pci_dev *dev);
  65. static inline bool xen_pcibk_pv_support(void)
  66. {
  67. return IS_ENABLED(CONFIG_XEN_PCIDEV_BACKEND);
  68. }
  69. /* Ensure a device is turned off or reset */
  70. void xen_pcibk_reset_device(struct pci_dev *pdev);
  71. /* Access a virtual configuration space for a PCI device */
  72. int xen_pcibk_config_init(void);
  73. int xen_pcibk_config_init_dev(struct pci_dev *dev);
  74. void xen_pcibk_config_free_dyn_fields(struct pci_dev *dev);
  75. void xen_pcibk_config_reset_dev(struct pci_dev *dev);
  76. void xen_pcibk_config_free_dev(struct pci_dev *dev);
  77. int xen_pcibk_config_read(struct pci_dev *dev, int offset, int size,
  78. u32 *ret_val);
  79. int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size,
  80. u32 value);
  81. /* Handle requests for specific devices from the frontend */
  82. typedef int (*publish_pci_dev_cb) (struct xen_pcibk_device *pdev,
  83. unsigned int domain, unsigned int bus,
  84. unsigned int devfn, unsigned int devid);
  85. typedef int (*publish_pci_root_cb) (struct xen_pcibk_device *pdev,
  86. unsigned int domain, unsigned int bus);
  87. /* Backend registration for the two types of BDF representation:
  88. * vpci - BDFs start at 00
  89. * passthrough - BDFs are exactly like in the host.
  90. */
  91. struct xen_pcibk_backend {
  92. const char *name;
  93. int (*init)(struct xen_pcibk_device *pdev);
  94. void (*free)(struct xen_pcibk_device *pdev);
  95. int (*find)(struct pci_dev *pcidev, struct xen_pcibk_device *pdev,
  96. unsigned int *domain, unsigned int *bus,
  97. unsigned int *devfn);
  98. int (*publish)(struct xen_pcibk_device *pdev, publish_pci_root_cb cb);
  99. void (*release)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
  100. bool lock);
  101. int (*add)(struct xen_pcibk_device *pdev, struct pci_dev *dev,
  102. int devid, publish_pci_dev_cb publish_cb);
  103. struct pci_dev *(*get)(struct xen_pcibk_device *pdev,
  104. unsigned int domain, unsigned int bus,
  105. unsigned int devfn);
  106. };
  107. extern const struct xen_pcibk_backend xen_pcibk_vpci_backend;
  108. extern const struct xen_pcibk_backend xen_pcibk_passthrough_backend;
  109. extern const struct xen_pcibk_backend *xen_pcibk_backend;
  110. static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
  111. struct pci_dev *dev,
  112. int devid,
  113. publish_pci_dev_cb publish_cb)
  114. {
  115. if (xen_pcibk_backend && xen_pcibk_backend->add)
  116. return xen_pcibk_backend->add(pdev, dev, devid, publish_cb);
  117. return -1;
  118. }
  119. static inline void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
  120. struct pci_dev *dev, bool lock)
  121. {
  122. if (xen_pcibk_backend && xen_pcibk_backend->release)
  123. return xen_pcibk_backend->release(pdev, dev, lock);
  124. }
  125. static inline struct pci_dev *
  126. xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, unsigned int domain,
  127. unsigned int bus, unsigned int devfn)
  128. {
  129. if (xen_pcibk_backend && xen_pcibk_backend->get)
  130. return xen_pcibk_backend->get(pdev, domain, bus, devfn);
  131. return NULL;
  132. }
  133. /**
  134. * Add for domain0 PCIE-AER handling. Get guest domain/bus/devfn in xen_pcibk
  135. * before sending aer request to pcifront, so that guest could identify
  136. * device, coopearte with xen_pcibk to finish aer recovery job if device driver
  137. * has the capability
  138. */
  139. static inline int xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
  140. struct xen_pcibk_device *pdev,
  141. unsigned int *domain,
  142. unsigned int *bus,
  143. unsigned int *devfn)
  144. {
  145. if (xen_pcibk_backend && xen_pcibk_backend->find)
  146. return xen_pcibk_backend->find(pcidev, pdev, domain, bus,
  147. devfn);
  148. return -1;
  149. }
  150. static inline int xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
  151. {
  152. if (xen_pcibk_backend && xen_pcibk_backend->init)
  153. return xen_pcibk_backend->init(pdev);
  154. return -1;
  155. }
  156. static inline int xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
  157. publish_pci_root_cb cb)
  158. {
  159. if (xen_pcibk_backend && xen_pcibk_backend->publish)
  160. return xen_pcibk_backend->publish(pdev, cb);
  161. return -1;
  162. }
  163. static inline void xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
  164. {
  165. if (xen_pcibk_backend && xen_pcibk_backend->free)
  166. return xen_pcibk_backend->free(pdev);
  167. }
  168. /* Handles events from front-end */
  169. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id);
  170. void xen_pcibk_do_op(struct work_struct *data);
  171. static inline void xen_pcibk_lateeoi(struct xen_pcibk_device *pdev,
  172. unsigned int eoi_flag)
  173. {
  174. if (test_and_clear_bit(_EOI_pending, &pdev->flags))
  175. xen_irq_lateeoi(pdev->evtchn_irq, eoi_flag);
  176. }
  177. int xen_pcibk_xenbus_register(void);
  178. void xen_pcibk_xenbus_unregister(void);
  179. #endif
  180. /* Handles shared IRQs that can to device domain and control domain. */
  181. void xen_pcibk_irq_handler(struct pci_dev *dev, int reset);