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/mdev.h> 171fde5734STony Krowiak #include <linux/delay.h> 181fde5734STony Krowiak #include <linux/mutex.h> 19e5282de9SPierre Morel #include <linux/kvm_host.h> 20eb0feefdSJason Gunthorpe #include <linux/vfio.h> 2111cb2419STony Krowiak #include <linux/hashtable.h> 221fde5734STony Krowiak 231fde5734STony Krowiak #include "ap_bus.h" 241fde5734STony Krowiak 251fde5734STony Krowiak #define VFIO_AP_MODULE_NAME "vfio_ap" 261fde5734STony Krowiak #define VFIO_AP_DRV_NAME "vfio_ap" 271fde5734STony Krowiak 281fde5734STony Krowiak /** 295ef4f710STony Krowiak * struct ap_matrix_dev - Contains the data for the matrix device. 305ef4f710STony Krowiak * 311fde5734STony Krowiak * @device: generic device structure associated with the AP matrix device 3265f06713STony Krowiak * @available_instances: number of mediated matrix devices that can be created 3365f06713STony Krowiak * @info: the struct containing the output from the PQAP(QCI) instruction 345ef4f710STony Krowiak * @mdev_list: the list of mediated matrix devices created 35d0786556STony Krowiak * @mdevs_lock: mutex for locking the AP matrix device. This lock will be 3665f06713STony Krowiak * taken every time we fiddle with state managed by the vfio_ap 3765f06713STony Krowiak * driver, be it using @mdev_list or writing the state of a 3865f06713STony Krowiak * single ap_matrix_mdev device. It's quite coarse but we don't 3965f06713STony Krowiak * expect much contention. 405ef4f710STony Krowiak * @vfio_ap_drv: the vfio_ap device driver 4121195eb0STony Krowiak * @guests_lock: mutex for controlling access to a guest that is using AP 4221195eb0STony Krowiak * devices passed through by the vfio_ap device driver. This lock 4321195eb0STony Krowiak * will be taken when the AP devices are plugged into or unplugged 4421195eb0STony Krowiak * from a guest, and when an ap_matrix_mdev device is added to or 4521195eb0STony Krowiak * removed from @mdev_list or the list is iterated. 461fde5734STony Krowiak */ 471fde5734STony Krowiak struct ap_matrix_dev { 481fde5734STony Krowiak struct device device; 4965f06713STony Krowiak atomic_t available_instances; 5065f06713STony Krowiak struct ap_config_info info; 5165f06713STony Krowiak struct list_head mdev_list; 52d0786556STony Krowiak struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */ 5336360658SPierre Morel struct ap_driver *vfio_ap_drv; 5421195eb0STony Krowiak struct mutex guests_lock; /* serializes access to each KVM guest */ 55*89345d51SChristoph Hellwig struct mdev_parent parent; 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 101258287c9STony Krowiak * @kvm: the struct holding guest's state 1025ef4f710STony Krowiak * @pqap_hook: the function pointer to the interception handler for the 1035ef4f710STony Krowiak * PQAP(AQIC) instruction. 1045ef4f710STony Krowiak * @mdev: the mediated device 10511cb2419STony Krowiak * @qtable: table of queues (struct vfio_ap_queue) assigned to the mdev 106eeb386aeSTony Krowiak * @apm_add: bitmap of APIDs added to the host's AP configuration 107eeb386aeSTony Krowiak * @aqm_add: bitmap of APQIs added to the host's AP configuration 108eeb386aeSTony Krowiak * @adm_add: bitmap of control domain numbers added to the host's AP 109eeb386aeSTony Krowiak * configuration 11065f06713STony Krowiak */ 11165f06713STony Krowiak struct ap_matrix_mdev { 112eb0feefdSJason Gunthorpe struct vfio_device vdev; 11365f06713STony Krowiak struct list_head node; 11465f06713STony Krowiak struct ap_matrix matrix; 11549b0109fSTony Krowiak struct ap_matrix shadow_apcb; 116258287c9STony Krowiak struct kvm *kvm; 1171e753732STony Krowiak crypto_hook pqap_hook; 11862e358ceSPierre Morel struct mdev_device *mdev; 11911cb2419STony Krowiak struct ap_queue_table qtable; 120eeb386aeSTony Krowiak DECLARE_BITMAP(apm_add, AP_DEVICES); 121eeb386aeSTony Krowiak DECLARE_BITMAP(aqm_add, AP_DOMAINS); 122eeb386aeSTony Krowiak DECLARE_BITMAP(adm_add, AP_DOMAINS); 12365f06713STony Krowiak }; 12465f06713STony Krowiak 1255ef4f710STony Krowiak /** 1265ef4f710STony Krowiak * struct vfio_ap_queue - contains the data associated with a queue bound to the 1275ef4f710STony Krowiak * vfio_ap device driver 1285ef4f710STony Krowiak * @matrix_mdev: the matrix mediated device 1293fad3a26SNicolin Chen * @saved_iova: the notification indicator byte (nib) address 1305ef4f710STony Krowiak * @apqn: the APQN of the AP queue device 1315ef4f710STony Krowiak * @saved_isc: the guest ISC registered with the GIB interface 13211cb2419STony Krowiak * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable 13370aeefe5STony Krowiak * @reset_rc: the status response code from the last reset of the queue 1345ef4f710STony Krowiak */ 135ec89b55eSPierre Morel struct vfio_ap_queue { 136ec89b55eSPierre Morel struct ap_matrix_mdev *matrix_mdev; 1373fad3a26SNicolin Chen dma_addr_t saved_iova; 138ec89b55eSPierre Morel int apqn; 139ec89b55eSPierre Morel #define VFIO_AP_ISC_INVALID 0xff 140ec89b55eSPierre Morel unsigned char saved_isc; 14111cb2419STony Krowiak struct hlist_node mdev_qnode; 14270aeefe5STony Krowiak unsigned int reset_rc; 143ec89b55eSPierre Morel }; 1446c12a638STony Krowiak 1456c12a638STony Krowiak int vfio_ap_mdev_register(void); 1466c12a638STony Krowiak void vfio_ap_mdev_unregister(void); 147260f3ea1STony Krowiak 148260f3ea1STony Krowiak int vfio_ap_mdev_probe_queue(struct ap_device *queue); 149260f3ea1STony Krowiak void vfio_ap_mdev_remove_queue(struct ap_device *queue); 1506c12a638STony Krowiak 1513f85d1dfSTony Krowiak int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm); 1523f85d1dfSTony Krowiak 153eeb386aeSTony Krowiak void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info, 154eeb386aeSTony Krowiak struct ap_config_info *old_config_info); 155eeb386aeSTony Krowiak void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info, 156eeb386aeSTony Krowiak struct ap_config_info *old_config_info); 157ec89b55eSPierre Morel 1581fde5734STony Krowiak #endif /* _VFIO_AP_PRIVATE_H_ */ 159