162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Private data and functions for adjunct processor VFIO matrix driver.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
662306a36Sopenharmony_ci *	      Halil Pasic <pasic@linux.ibm.com>
762306a36Sopenharmony_ci *	      Pierre Morel <pmorel@linux.ibm.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright IBM Corp. 2018
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifndef _VFIO_AP_PRIVATE_H_
1362306a36Sopenharmony_ci#define _VFIO_AP_PRIVATE_H_
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/types.h>
1662306a36Sopenharmony_ci#include <linux/mdev.h>
1762306a36Sopenharmony_ci#include <linux/delay.h>
1862306a36Sopenharmony_ci#include <linux/eventfd.h>
1962306a36Sopenharmony_ci#include <linux/mutex.h>
2062306a36Sopenharmony_ci#include <linux/kvm_host.h>
2162306a36Sopenharmony_ci#include <linux/vfio.h>
2262306a36Sopenharmony_ci#include <linux/hashtable.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "ap_bus.h"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define VFIO_AP_MODULE_NAME "vfio_ap"
2762306a36Sopenharmony_ci#define VFIO_AP_DRV_NAME "vfio_ap"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/**
3062306a36Sopenharmony_ci * struct ap_matrix_dev - Contains the data for the matrix device.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * @device:	generic device structure associated with the AP matrix device
3362306a36Sopenharmony_ci * @info:	the struct containing the output from the PQAP(QCI) instruction
3462306a36Sopenharmony_ci * @mdev_list:	the list of mediated matrix devices created
3562306a36Sopenharmony_ci * @mdevs_lock: mutex for locking the AP matrix device. This lock will be
3662306a36Sopenharmony_ci *		taken every time we fiddle with state managed by the vfio_ap
3762306a36Sopenharmony_ci *		driver, be it using @mdev_list or writing the state of a
3862306a36Sopenharmony_ci *		single ap_matrix_mdev device. It's quite coarse but we don't
3962306a36Sopenharmony_ci *		expect much contention.
4062306a36Sopenharmony_ci * @vfio_ap_drv: the vfio_ap device driver
4162306a36Sopenharmony_ci * @guests_lock: mutex for controlling access to a guest that is using AP
4262306a36Sopenharmony_ci *		 devices passed through by the vfio_ap device driver. This lock
4362306a36Sopenharmony_ci *		 will be taken when the AP devices are plugged into or unplugged
4462306a36Sopenharmony_ci *		 from a guest, and when an ap_matrix_mdev device is added to or
4562306a36Sopenharmony_ci *		 removed from @mdev_list or the list is iterated.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cistruct ap_matrix_dev {
4862306a36Sopenharmony_ci	struct device device;
4962306a36Sopenharmony_ci	struct ap_config_info info;
5062306a36Sopenharmony_ci	struct list_head mdev_list;
5162306a36Sopenharmony_ci	struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */
5262306a36Sopenharmony_ci	struct ap_driver  *vfio_ap_drv;
5362306a36Sopenharmony_ci	struct mutex guests_lock; /* serializes access to each KVM guest */
5462306a36Sopenharmony_ci	struct mdev_parent parent;
5562306a36Sopenharmony_ci	struct mdev_type mdev_type;
5662306a36Sopenharmony_ci	struct mdev_type *mdev_types[1];
5762306a36Sopenharmony_ci};
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciextern struct ap_matrix_dev *matrix_dev;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/**
6262306a36Sopenharmony_ci * struct ap_matrix - matrix of adapters, domains and control domains
6362306a36Sopenharmony_ci *
6462306a36Sopenharmony_ci * @apm_max: max adapter number in @apm
6562306a36Sopenharmony_ci * @apm: identifies the AP adapters in the matrix
6662306a36Sopenharmony_ci * @aqm_max: max domain number in @aqm
6762306a36Sopenharmony_ci * @aqm: identifies the AP queues (domains) in the matrix
6862306a36Sopenharmony_ci * @adm_max: max domain number in @adm
6962306a36Sopenharmony_ci * @adm: identifies the AP control domains in the matrix
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * The AP matrix is comprised of three bit masks identifying the adapters,
7262306a36Sopenharmony_ci * queues (domains) and control domains that belong to an AP matrix. The bits in
7362306a36Sopenharmony_ci * each mask, from left to right, correspond to IDs 0 to 255. When a bit is set
7462306a36Sopenharmony_ci * the corresponding ID belongs to the matrix.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_cistruct ap_matrix {
7762306a36Sopenharmony_ci	unsigned long apm_max;
7862306a36Sopenharmony_ci	DECLARE_BITMAP(apm, 256);
7962306a36Sopenharmony_ci	unsigned long aqm_max;
8062306a36Sopenharmony_ci	DECLARE_BITMAP(aqm, 256);
8162306a36Sopenharmony_ci	unsigned long adm_max;
8262306a36Sopenharmony_ci	DECLARE_BITMAP(adm, 256);
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/**
8662306a36Sopenharmony_ci * struct ap_queue_table - a table of queue objects.
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * @queues: a hashtable of queues (struct vfio_ap_queue).
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_cistruct ap_queue_table {
9162306a36Sopenharmony_ci	DECLARE_HASHTABLE(queues, 8);
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/**
9562306a36Sopenharmony_ci * struct ap_matrix_mdev - Contains the data associated with a matrix mediated
9662306a36Sopenharmony_ci *			   device.
9762306a36Sopenharmony_ci * @vdev:	the vfio device
9862306a36Sopenharmony_ci * @node:	allows the ap_matrix_mdev struct to be added to a list
9962306a36Sopenharmony_ci * @matrix:	the adapters, usage domains and control domains assigned to the
10062306a36Sopenharmony_ci *		mediated matrix device.
10162306a36Sopenharmony_ci * @shadow_apcb:    the shadow copy of the APCB field of the KVM guest's CRYCB
10262306a36Sopenharmony_ci * @kvm:	the struct holding guest's state
10362306a36Sopenharmony_ci * @pqap_hook:	the function pointer to the interception handler for the
10462306a36Sopenharmony_ci *		PQAP(AQIC) instruction.
10562306a36Sopenharmony_ci * @mdev:	the mediated device
10662306a36Sopenharmony_ci * @qtable:	table of queues (struct vfio_ap_queue) assigned to the mdev
10762306a36Sopenharmony_ci * @req_trigger eventfd ctx for signaling userspace to return a device
10862306a36Sopenharmony_ci * @apm_add:	bitmap of APIDs added to the host's AP configuration
10962306a36Sopenharmony_ci * @aqm_add:	bitmap of APQIs added to the host's AP configuration
11062306a36Sopenharmony_ci * @adm_add:	bitmap of control domain numbers added to the host's AP
11162306a36Sopenharmony_ci *		configuration
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_cistruct ap_matrix_mdev {
11462306a36Sopenharmony_ci	struct vfio_device vdev;
11562306a36Sopenharmony_ci	struct list_head node;
11662306a36Sopenharmony_ci	struct ap_matrix matrix;
11762306a36Sopenharmony_ci	struct ap_matrix shadow_apcb;
11862306a36Sopenharmony_ci	struct kvm *kvm;
11962306a36Sopenharmony_ci	crypto_hook pqap_hook;
12062306a36Sopenharmony_ci	struct mdev_device *mdev;
12162306a36Sopenharmony_ci	struct ap_queue_table qtable;
12262306a36Sopenharmony_ci	struct eventfd_ctx *req_trigger;
12362306a36Sopenharmony_ci	DECLARE_BITMAP(apm_add, AP_DEVICES);
12462306a36Sopenharmony_ci	DECLARE_BITMAP(aqm_add, AP_DOMAINS);
12562306a36Sopenharmony_ci	DECLARE_BITMAP(adm_add, AP_DOMAINS);
12662306a36Sopenharmony_ci};
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci/**
12962306a36Sopenharmony_ci * struct vfio_ap_queue - contains the data associated with a queue bound to the
13062306a36Sopenharmony_ci *			  vfio_ap device driver
13162306a36Sopenharmony_ci * @matrix_mdev: the matrix mediated device
13262306a36Sopenharmony_ci * @saved_iova: the notification indicator byte (nib) address
13362306a36Sopenharmony_ci * @apqn: the APQN of the AP queue device
13462306a36Sopenharmony_ci * @saved_isc: the guest ISC registered with the GIB interface
13562306a36Sopenharmony_ci * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable
13662306a36Sopenharmony_ci * @reset_qnode: allows the vfio_ap_queue struct to be added to a list of queues
13762306a36Sopenharmony_ci *		 that need to be reset
13862306a36Sopenharmony_ci * @reset_status: the status from the last reset of the queue
13962306a36Sopenharmony_ci * @reset_work: work to wait for queue reset to complete
14062306a36Sopenharmony_ci */
14162306a36Sopenharmony_cistruct vfio_ap_queue {
14262306a36Sopenharmony_ci	struct ap_matrix_mdev *matrix_mdev;
14362306a36Sopenharmony_ci	dma_addr_t saved_iova;
14462306a36Sopenharmony_ci	int	apqn;
14562306a36Sopenharmony_ci#define VFIO_AP_ISC_INVALID 0xff
14662306a36Sopenharmony_ci	unsigned char saved_isc;
14762306a36Sopenharmony_ci	struct hlist_node mdev_qnode;
14862306a36Sopenharmony_ci	struct list_head reset_qnode;
14962306a36Sopenharmony_ci	struct ap_queue_status reset_status;
15062306a36Sopenharmony_ci	struct work_struct reset_work;
15162306a36Sopenharmony_ci};
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ciint vfio_ap_mdev_register(void);
15462306a36Sopenharmony_civoid vfio_ap_mdev_unregister(void);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciint vfio_ap_mdev_probe_queue(struct ap_device *queue);
15762306a36Sopenharmony_civoid vfio_ap_mdev_remove_queue(struct ap_device *queue);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciint vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_civoid vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info,
16262306a36Sopenharmony_ci			    struct ap_config_info *old_config_info);
16362306a36Sopenharmony_civoid vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
16462306a36Sopenharmony_ci			      struct ap_config_info *old_config_info);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#endif /* _VFIO_AP_PRIVATE_H_ */
167