vfio_ap_private.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Private data and functions for adjunct processor VFIO matrix driver.
  4. *
  5. * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  6. * Halil Pasic <pasic@linux.ibm.com>
  7. * Pierre Morel <pmorel@linux.ibm.com>
  8. *
  9. * Copyright IBM Corp. 2018
  10. */
  11. #ifndef _VFIO_AP_PRIVATE_H_
  12. #define _VFIO_AP_PRIVATE_H_
  13. #include <linux/types.h>
  14. #include <linux/mdev.h>
  15. #include <linux/delay.h>
  16. #include <linux/eventfd.h>
  17. #include <linux/mutex.h>
  18. #include <linux/kvm_host.h>
  19. #include <linux/vfio.h>
  20. #include <linux/hashtable.h>
  21. #include "ap_bus.h"
  22. #define VFIO_AP_MODULE_NAME "vfio_ap"
  23. #define VFIO_AP_DRV_NAME "vfio_ap"
  24. /**
  25. * struct ap_matrix_dev - Contains the data for the matrix device.
  26. *
  27. * @device: generic device structure associated with the AP matrix device
  28. * @info: the struct containing the output from the PQAP(QCI) instruction
  29. * @mdev_list: the list of mediated matrix devices created
  30. * @mdevs_lock: mutex for locking the AP matrix device. This lock will be
  31. * taken every time we fiddle with state managed by the vfio_ap
  32. * driver, be it using @mdev_list or writing the state of a
  33. * single ap_matrix_mdev device. It's quite coarse but we don't
  34. * expect much contention.
  35. * @vfio_ap_drv: the vfio_ap device driver
  36. * @guests_lock: mutex for controlling access to a guest that is using AP
  37. * devices passed through by the vfio_ap device driver. This lock
  38. * will be taken when the AP devices are plugged into or unplugged
  39. * from a guest, and when an ap_matrix_mdev device is added to or
  40. * removed from @mdev_list or the list is iterated.
  41. */
  42. struct ap_matrix_dev {
  43. struct device device;
  44. struct ap_config_info info;
  45. struct list_head mdev_list;
  46. struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */
  47. struct ap_driver *vfio_ap_drv;
  48. struct mutex guests_lock; /* serializes access to each KVM guest */
  49. struct mdev_parent parent;
  50. struct mdev_type mdev_type;
  51. struct mdev_type *mdev_types;
  52. };
  53. extern struct ap_matrix_dev *matrix_dev;
  54. /**
  55. * struct ap_matrix - matrix of adapters, domains and control domains
  56. *
  57. * @apm_max: max adapter number in @apm
  58. * @apm: identifies the AP adapters in the matrix
  59. * @aqm_max: max domain number in @aqm
  60. * @aqm: identifies the AP queues (domains) in the matrix
  61. * @adm_max: max domain number in @adm
  62. * @adm: identifies the AP control domains in the matrix
  63. *
  64. * The AP matrix is comprised of three bit masks identifying the adapters,
  65. * queues (domains) and control domains that belong to an AP matrix. The bits in
  66. * each mask, from left to right, correspond to IDs 0 to 255. When a bit is set
  67. * the corresponding ID belongs to the matrix.
  68. */
  69. struct ap_matrix {
  70. unsigned long apm_max;
  71. DECLARE_BITMAP(apm, AP_DEVICES);
  72. unsigned long aqm_max;
  73. DECLARE_BITMAP(aqm, AP_DOMAINS);
  74. unsigned long adm_max;
  75. DECLARE_BITMAP(adm, AP_DOMAINS);
  76. };
  77. /**
  78. * struct ap_queue_table - a table of queue objects.
  79. *
  80. * @queues: a hashtable of queues (struct vfio_ap_queue).
  81. */
  82. struct ap_queue_table {
  83. DECLARE_HASHTABLE(queues, 8);
  84. };
  85. /**
  86. * struct ap_matrix_mdev - Contains the data associated with a matrix mediated
  87. * device.
  88. * @vdev: the vfio device
  89. * @node: allows the ap_matrix_mdev struct to be added to a list
  90. * @matrix: the adapters, usage domains and control domains assigned to the
  91. * mediated matrix device.
  92. * @shadow_apcb: the shadow copy of the APCB field of the KVM guest's CRYCB
  93. * @kvm: the struct holding guest's state
  94. * @pqap_hook: the function pointer to the interception handler for the
  95. * PQAP(AQIC) instruction.
  96. * @mdev: the mediated device
  97. * @qtable: table of queues (struct vfio_ap_queue) assigned to the mdev
  98. * @req_trigger eventfd ctx for signaling userspace to return a device
  99. * @cfg_chg_trigger eventfd ctx to signal AP config changed to userspace
  100. * @apm_add: bitmap of APIDs added to the host's AP configuration
  101. * @aqm_add: bitmap of APQIs added to the host's AP configuration
  102. * @adm_add: bitmap of control domain numbers added to the host's AP
  103. * configuration
  104. */
  105. struct ap_matrix_mdev {
  106. struct vfio_device vdev;
  107. struct list_head node;
  108. struct ap_matrix matrix;
  109. struct ap_matrix shadow_apcb;
  110. struct kvm *kvm;
  111. crypto_hook pqap_hook;
  112. struct mdev_device *mdev;
  113. struct ap_queue_table qtable;
  114. struct eventfd_ctx *req_trigger;
  115. struct eventfd_ctx *cfg_chg_trigger;
  116. DECLARE_BITMAP(apm_add, AP_DEVICES);
  117. DECLARE_BITMAP(aqm_add, AP_DOMAINS);
  118. DECLARE_BITMAP(adm_add, AP_DOMAINS);
  119. };
  120. /**
  121. * struct vfio_ap_queue - contains the data associated with a queue bound to the
  122. * vfio_ap device driver
  123. * @matrix_mdev: the matrix mediated device
  124. * @saved_iova: the notification indicator byte (nib) address
  125. * @apqn: the APQN of the AP queue device
  126. * @saved_isc: the guest ISC registered with the GIB interface
  127. * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable
  128. * @reset_qnode: allows the vfio_ap_queue struct to be added to a list of queues
  129. * that need to be reset
  130. * @reset_status: the status from the last reset of the queue
  131. * @reset_work: work to wait for queue reset to complete
  132. */
  133. struct vfio_ap_queue {
  134. struct ap_matrix_mdev *matrix_mdev;
  135. dma_addr_t saved_iova;
  136. int apqn;
  137. #define VFIO_AP_ISC_INVALID 0xff
  138. unsigned char saved_isc;
  139. struct hlist_node mdev_qnode;
  140. struct list_head reset_qnode;
  141. struct ap_queue_status reset_status;
  142. struct work_struct reset_work;
  143. };
  144. int vfio_ap_mdev_register(void);
  145. void vfio_ap_mdev_unregister(void);
  146. int vfio_ap_mdev_probe_queue(struct ap_device *queue);
  147. void vfio_ap_mdev_remove_queue(struct ap_device *queue);
  148. int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm);
  149. void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info,
  150. struct ap_config_info *old_config_info);
  151. void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
  152. struct ap_config_info *old_config_info);
  153. #endif /* _VFIO_AP_PRIVATE_H_ */