11fde5734STony Krowiak /* SPDX-License-Identifier: GPL-2.0 */ 21fde5734STony Krowiak /* 31fde5734STony Krowiak * Private data and functions for adjunct processor VFIO matrix driver. 41fde5734STony Krowiak * 51fde5734STony Krowiak * Author(s): Tony Krowiak <akrowiak@linux.ibm.com> 665f06713STony Krowiak * Halil Pasic <pasic@linux.ibm.com> 7ec89b55eSPierre Morel * Pierre Morel <pmorel@linux.ibm.com> 81fde5734STony Krowiak * 91fde5734STony Krowiak * Copyright IBM Corp. 2018 101fde5734STony Krowiak */ 111fde5734STony Krowiak 121fde5734STony Krowiak #ifndef _VFIO_AP_PRIVATE_H_ 131fde5734STony Krowiak #define _VFIO_AP_PRIVATE_H_ 141fde5734STony Krowiak 151fde5734STony Krowiak #include <linux/types.h> 161fde5734STony Krowiak #include <linux/device.h> 171fde5734STony Krowiak #include <linux/mdev.h> 181fde5734STony Krowiak #include <linux/delay.h> 191fde5734STony Krowiak #include <linux/mutex.h> 20e5282de9SPierre Morel #include <linux/kvm_host.h> 21eb0feefdSJason Gunthorpe #include <linux/vfio.h> 2211cb2419STony Krowiak #include <linux/hashtable.h> 231fde5734STony Krowiak 241fde5734STony Krowiak #include "ap_bus.h" 251fde5734STony Krowiak 261fde5734STony Krowiak #define VFIO_AP_MODULE_NAME "vfio_ap" 271fde5734STony Krowiak #define VFIO_AP_DRV_NAME "vfio_ap" 281fde5734STony Krowiak 291fde5734STony Krowiak /** 305ef4f710STony Krowiak * struct ap_matrix_dev - Contains the data for the matrix device. 315ef4f710STony Krowiak * 321fde5734STony Krowiak * @device: generic device structure associated with the AP matrix device 3365f06713STony Krowiak * @available_instances: number of mediated matrix devices that can be created 3465f06713STony Krowiak * @info: the struct containing the output from the PQAP(QCI) instruction 355ef4f710STony Krowiak * @mdev_list: the list of mediated matrix devices created 36d0786556STony Krowiak * @mdevs_lock: mutex for locking the AP matrix device. This lock will be 3765f06713STony Krowiak * taken every time we fiddle with state managed by the vfio_ap 3865f06713STony Krowiak * driver, be it using @mdev_list or writing the state of a 3965f06713STony Krowiak * single ap_matrix_mdev device. It's quite coarse but we don't 4065f06713STony Krowiak * expect much contention. 415ef4f710STony Krowiak * @vfio_ap_drv: the vfio_ap device driver 4221195eb0STony Krowiak * @guests_lock: mutex for controlling access to a guest that is using AP 4321195eb0STony Krowiak * devices passed through by the vfio_ap device driver. This lock 4421195eb0STony Krowiak * will be taken when the AP devices are plugged into or unplugged 4521195eb0STony Krowiak * from a guest, and when an ap_matrix_mdev device is added to or 4621195eb0STony Krowiak * removed from @mdev_list or the list is iterated. 471fde5734STony Krowiak */ 481fde5734STony Krowiak struct ap_matrix_dev { 491fde5734STony Krowiak struct device device; 5065f06713STony Krowiak atomic_t available_instances; 5165f06713STony Krowiak struct ap_config_info info; 5265f06713STony Krowiak struct list_head mdev_list; 53d0786556STony Krowiak struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */ 5436360658SPierre Morel struct ap_driver *vfio_ap_drv; 5521195eb0STony Krowiak struct mutex guests_lock; /* serializes access to each KVM guest */ 561fde5734STony Krowiak }; 571fde5734STony Krowiak 581fde5734STony Krowiak extern struct ap_matrix_dev *matrix_dev; 591fde5734STony Krowiak 6065f06713STony Krowiak /** 615ef4f710STony Krowiak * struct ap_matrix - matrix of adapters, domains and control domains 6265f06713STony Krowiak * 6365f06713STony Krowiak * @apm_max: max adapter number in @apm 645ef4f710STony Krowiak * @apm: identifies the AP adapters in the matrix 6565f06713STony Krowiak * @aqm_max: max domain number in @aqm 665ef4f710STony Krowiak * @aqm: identifies the AP queues (domains) in the matrix 6765f06713STony Krowiak * @adm_max: max domain number in @adm 685ef4f710STony Krowiak * @adm: identifies the AP control domains in the matrix 695ef4f710STony Krowiak * 705ef4f710STony Krowiak * The AP matrix is comprised of three bit masks identifying the adapters, 715ef4f710STony Krowiak * queues (domains) and control domains that belong to an AP matrix. The bits in 725ef4f710STony Krowiak * each mask, from left to right, correspond to IDs 0 to 255. When a bit is set 735ef4f710STony Krowiak * the corresponding ID belongs to the matrix. 7465f06713STony Krowiak */ 7565f06713STony Krowiak struct ap_matrix { 7665f06713STony Krowiak unsigned long apm_max; 7765f06713STony Krowiak DECLARE_BITMAP(apm, 256); 7865f06713STony Krowiak unsigned long aqm_max; 7965f06713STony Krowiak DECLARE_BITMAP(aqm, 256); 8065f06713STony Krowiak unsigned long adm_max; 8165f06713STony Krowiak DECLARE_BITMAP(adm, 256); 8265f06713STony Krowiak }; 8365f06713STony Krowiak 8465f06713STony Krowiak /** 8511cb2419STony Krowiak * struct ap_queue_table - a table of queue objects. 8611cb2419STony Krowiak * 8711cb2419STony Krowiak * @queues: a hashtable of queues (struct vfio_ap_queue). 8811cb2419STony Krowiak */ 8911cb2419STony Krowiak struct ap_queue_table { 9011cb2419STony Krowiak DECLARE_HASHTABLE(queues, 8); 9111cb2419STony Krowiak }; 9211cb2419STony Krowiak 9311cb2419STony Krowiak /** 945ef4f710STony Krowiak * struct ap_matrix_mdev - Contains the data associated with a matrix mediated 955ef4f710STony Krowiak * device. 965ef4f710STony Krowiak * @vdev: the vfio device 975ef4f710STony Krowiak * @node: allows the ap_matrix_mdev struct to be added to a list 9865f06713STony Krowiak * @matrix: the adapters, usage domains and control domains assigned to the 9965f06713STony Krowiak * mediated matrix device. 10049b0109fSTony Krowiak * @shadow_apcb: the shadow copy of the APCB field of the KVM guest's CRYCB 1015ef4f710STony Krowiak * @iommu_notifier: notifier block used for specifying callback function for 1025ef4f710STony Krowiak * handling the VFIO_IOMMU_NOTIFY_DMA_UNMAP even 103258287c9STony Krowiak * @kvm: the struct holding guest's state 1045ef4f710STony Krowiak * @pqap_hook: the function pointer to the interception handler for the 1055ef4f710STony Krowiak * PQAP(AQIC) instruction. 1065ef4f710STony Krowiak * @mdev: the mediated device 10711cb2419STony Krowiak * @qtable: table of queues (struct vfio_ap_queue) assigned to the mdev 108*eeb386aeSTony Krowiak * @apm_add: bitmap of APIDs added to the host's AP configuration 109*eeb386aeSTony Krowiak * @aqm_add: bitmap of APQIs added to the host's AP configuration 110*eeb386aeSTony Krowiak * @adm_add: bitmap of control domain numbers added to the host's AP 111*eeb386aeSTony Krowiak * configuration 11265f06713STony Krowiak */ 11365f06713STony Krowiak struct ap_matrix_mdev { 114eb0feefdSJason Gunthorpe struct vfio_device vdev; 11565f06713STony Krowiak struct list_head node; 11665f06713STony Krowiak struct ap_matrix matrix; 11749b0109fSTony Krowiak struct ap_matrix shadow_apcb; 11862e358ceSPierre Morel struct notifier_block iommu_notifier; 119258287c9STony Krowiak struct kvm *kvm; 1201e753732STony Krowiak crypto_hook pqap_hook; 12162e358ceSPierre Morel struct mdev_device *mdev; 12211cb2419STony Krowiak struct ap_queue_table qtable; 123*eeb386aeSTony Krowiak DECLARE_BITMAP(apm_add, AP_DEVICES); 124*eeb386aeSTony Krowiak DECLARE_BITMAP(aqm_add, AP_DOMAINS); 125*eeb386aeSTony Krowiak DECLARE_BITMAP(adm_add, AP_DOMAINS); 12665f06713STony Krowiak }; 12765f06713STony Krowiak 1285ef4f710STony Krowiak /** 1295ef4f710STony Krowiak * struct vfio_ap_queue - contains the data associated with a queue bound to the 1305ef4f710STony Krowiak * vfio_ap device driver 1315ef4f710STony Krowiak * @matrix_mdev: the matrix mediated device 1325ef4f710STony Krowiak * @saved_pfn: the guest PFN pinned for the guest 1335ef4f710STony Krowiak * @apqn: the APQN of the AP queue device 1345ef4f710STony Krowiak * @saved_isc: the guest ISC registered with the GIB interface 13511cb2419STony Krowiak * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable 13670aeefe5STony Krowiak * @reset_rc: the status response code from the last reset of the queue 1375ef4f710STony Krowiak */ 138ec89b55eSPierre Morel struct vfio_ap_queue { 139ec89b55eSPierre Morel struct ap_matrix_mdev *matrix_mdev; 140ec89b55eSPierre Morel unsigned long saved_pfn; 141ec89b55eSPierre Morel int apqn; 142ec89b55eSPierre Morel #define VFIO_AP_ISC_INVALID 0xff 143ec89b55eSPierre Morel unsigned char saved_isc; 14411cb2419STony Krowiak struct hlist_node mdev_qnode; 14570aeefe5STony Krowiak unsigned int reset_rc; 146ec89b55eSPierre Morel }; 1476c12a638STony Krowiak 1486c12a638STony Krowiak int vfio_ap_mdev_register(void); 1496c12a638STony Krowiak void vfio_ap_mdev_unregister(void); 150260f3ea1STony Krowiak 151260f3ea1STony Krowiak int vfio_ap_mdev_probe_queue(struct ap_device *queue); 152260f3ea1STony Krowiak void vfio_ap_mdev_remove_queue(struct ap_device *queue); 1536c12a638STony Krowiak 1543f85d1dfSTony Krowiak int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm); 1553f85d1dfSTony Krowiak 156*eeb386aeSTony Krowiak void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info, 157*eeb386aeSTony Krowiak struct ap_config_info *old_config_info); 158*eeb386aeSTony Krowiak void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info, 159*eeb386aeSTony Krowiak struct ap_config_info *old_config_info); 160*eeb386aeSTony Krowiak 1611fde5734STony Krowiak #endif /* _VFIO_AP_PRIVATE_H_ */ 162