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> 22*11cb2419STony 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 365ef4f710STony Krowiak * @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 421fde5734STony Krowiak */ 431fde5734STony Krowiak struct ap_matrix_dev { 441fde5734STony Krowiak struct device device; 4565f06713STony Krowiak atomic_t available_instances; 4665f06713STony Krowiak struct ap_config_info info; 4765f06713STony Krowiak struct list_head mdev_list; 4865f06713STony Krowiak struct mutex lock; 4936360658SPierre Morel struct ap_driver *vfio_ap_drv; 501fde5734STony Krowiak }; 511fde5734STony Krowiak 521fde5734STony Krowiak extern struct ap_matrix_dev *matrix_dev; 531fde5734STony Krowiak 5465f06713STony Krowiak /** 555ef4f710STony Krowiak * struct ap_matrix - matrix of adapters, domains and control domains 5665f06713STony Krowiak * 5765f06713STony Krowiak * @apm_max: max adapter number in @apm 585ef4f710STony Krowiak * @apm: identifies the AP adapters in the matrix 5965f06713STony Krowiak * @aqm_max: max domain number in @aqm 605ef4f710STony Krowiak * @aqm: identifies the AP queues (domains) in the matrix 6165f06713STony Krowiak * @adm_max: max domain number in @adm 625ef4f710STony Krowiak * @adm: identifies the AP control domains in the matrix 635ef4f710STony Krowiak * 645ef4f710STony Krowiak * The AP matrix is comprised of three bit masks identifying the adapters, 655ef4f710STony Krowiak * queues (domains) and control domains that belong to an AP matrix. The bits in 665ef4f710STony Krowiak * each mask, from left to right, correspond to IDs 0 to 255. When a bit is set 675ef4f710STony Krowiak * the corresponding ID belongs to the matrix. 6865f06713STony Krowiak */ 6965f06713STony Krowiak struct ap_matrix { 7065f06713STony Krowiak unsigned long apm_max; 7165f06713STony Krowiak DECLARE_BITMAP(apm, 256); 7265f06713STony Krowiak unsigned long aqm_max; 7365f06713STony Krowiak DECLARE_BITMAP(aqm, 256); 7465f06713STony Krowiak unsigned long adm_max; 7565f06713STony Krowiak DECLARE_BITMAP(adm, 256); 7665f06713STony Krowiak }; 7765f06713STony Krowiak 7865f06713STony Krowiak /** 79*11cb2419STony Krowiak * struct ap_queue_table - a table of queue objects. 80*11cb2419STony Krowiak * 81*11cb2419STony Krowiak * @queues: a hashtable of queues (struct vfio_ap_queue). 82*11cb2419STony Krowiak */ 83*11cb2419STony Krowiak struct ap_queue_table { 84*11cb2419STony Krowiak DECLARE_HASHTABLE(queues, 8); 85*11cb2419STony Krowiak }; 86*11cb2419STony Krowiak 87*11cb2419STony Krowiak /** 885ef4f710STony Krowiak * struct ap_matrix_mdev - Contains the data associated with a matrix mediated 895ef4f710STony Krowiak * device. 905ef4f710STony Krowiak * @vdev: the vfio device 915ef4f710STony Krowiak * @node: allows the ap_matrix_mdev struct to be added to a list 9265f06713STony Krowiak * @matrix: the adapters, usage domains and control domains assigned to the 9365f06713STony Krowiak * mediated matrix device. 945ef4f710STony Krowiak * @iommu_notifier: notifier block used for specifying callback function for 955ef4f710STony Krowiak * handling the VFIO_IOMMU_NOTIFY_DMA_UNMAP even 96258287c9STony Krowiak * @kvm: the struct holding guest's state 975ef4f710STony Krowiak * @pqap_hook: the function pointer to the interception handler for the 985ef4f710STony Krowiak * PQAP(AQIC) instruction. 995ef4f710STony Krowiak * @mdev: the mediated device 100*11cb2419STony Krowiak * @qtable: table of queues (struct vfio_ap_queue) assigned to the mdev 10165f06713STony Krowiak */ 10265f06713STony Krowiak struct ap_matrix_mdev { 103eb0feefdSJason Gunthorpe struct vfio_device vdev; 10465f06713STony Krowiak struct list_head node; 10565f06713STony Krowiak struct ap_matrix matrix; 10662e358ceSPierre Morel struct notifier_block iommu_notifier; 107258287c9STony Krowiak struct kvm *kvm; 1081e753732STony Krowiak crypto_hook pqap_hook; 10962e358ceSPierre Morel struct mdev_device *mdev; 110*11cb2419STony Krowiak struct ap_queue_table qtable; 11165f06713STony Krowiak }; 11265f06713STony Krowiak 1135ef4f710STony Krowiak /** 1145ef4f710STony Krowiak * struct vfio_ap_queue - contains the data associated with a queue bound to the 1155ef4f710STony Krowiak * vfio_ap device driver 1165ef4f710STony Krowiak * @matrix_mdev: the matrix mediated device 1175ef4f710STony Krowiak * @saved_pfn: the guest PFN pinned for the guest 1185ef4f710STony Krowiak * @apqn: the APQN of the AP queue device 1195ef4f710STony Krowiak * @saved_isc: the guest ISC registered with the GIB interface 120*11cb2419STony Krowiak * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable 1215ef4f710STony Krowiak */ 122ec89b55eSPierre Morel struct vfio_ap_queue { 123ec89b55eSPierre Morel struct ap_matrix_mdev *matrix_mdev; 124ec89b55eSPierre Morel unsigned long saved_pfn; 125ec89b55eSPierre Morel int apqn; 126ec89b55eSPierre Morel #define VFIO_AP_ISC_INVALID 0xff 127ec89b55eSPierre Morel unsigned char saved_isc; 128*11cb2419STony Krowiak struct hlist_node mdev_qnode; 129ec89b55eSPierre Morel }; 1306c12a638STony Krowiak 1316c12a638STony Krowiak int vfio_ap_mdev_register(void); 1326c12a638STony Krowiak void vfio_ap_mdev_unregister(void); 133260f3ea1STony Krowiak 134260f3ea1STony Krowiak int vfio_ap_mdev_probe_queue(struct ap_device *queue); 135260f3ea1STony Krowiak void vfio_ap_mdev_remove_queue(struct ap_device *queue); 1366c12a638STony Krowiak 1371fde5734STony Krowiak #endif /* _VFIO_AP_PRIVATE_H_ */ 138