18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Private data and functions for adjunct processor VFIO matrix driver.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
68c2ecf20Sopenharmony_ci *	      Halil Pasic <pasic@linux.ibm.com>
78c2ecf20Sopenharmony_ci *	      Pierre Morel <pmorel@linux.ibm.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2018
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef _VFIO_AP_PRIVATE_H_
138c2ecf20Sopenharmony_ci#define _VFIO_AP_PRIVATE_H_
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/mdev.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci#include <linux/mutex.h>
208c2ecf20Sopenharmony_ci#include <linux/kvm_host.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "ap_bus.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define VFIO_AP_MODULE_NAME "vfio_ap"
258c2ecf20Sopenharmony_ci#define VFIO_AP_DRV_NAME "vfio_ap"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/**
288c2ecf20Sopenharmony_ci * ap_matrix_dev - the AP matrix device structure
298c2ecf20Sopenharmony_ci * @device:	generic device structure associated with the AP matrix device
308c2ecf20Sopenharmony_ci * @available_instances: number of mediated matrix devices that can be created
318c2ecf20Sopenharmony_ci * @info:	the struct containing the output from the PQAP(QCI) instruction
328c2ecf20Sopenharmony_ci * mdev_list:	the list of mediated matrix devices created
338c2ecf20Sopenharmony_ci * lock:	mutex for locking the AP matrix device. This lock will be
348c2ecf20Sopenharmony_ci *		taken every time we fiddle with state managed by the vfio_ap
358c2ecf20Sopenharmony_ci *		driver, be it using @mdev_list or writing the state of a
368c2ecf20Sopenharmony_ci *		single ap_matrix_mdev device. It's quite coarse but we don't
378c2ecf20Sopenharmony_ci *		expect much contention.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_cistruct ap_matrix_dev {
408c2ecf20Sopenharmony_ci	struct device device;
418c2ecf20Sopenharmony_ci	atomic_t available_instances;
428c2ecf20Sopenharmony_ci	struct ap_config_info info;
438c2ecf20Sopenharmony_ci	struct list_head mdev_list;
448c2ecf20Sopenharmony_ci	struct mutex lock;
458c2ecf20Sopenharmony_ci	struct ap_driver  *vfio_ap_drv;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciextern struct ap_matrix_dev *matrix_dev;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/**
518c2ecf20Sopenharmony_ci * The AP matrix is comprised of three bit masks identifying the adapters,
528c2ecf20Sopenharmony_ci * queues (domains) and control domains that belong to an AP matrix. The bits i
538c2ecf20Sopenharmony_ci * each mask, from least significant to most significant bit, correspond to IDs
548c2ecf20Sopenharmony_ci * 0 to 255. When a bit is set, the corresponding ID belongs to the matrix.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * @apm_max: max adapter number in @apm
578c2ecf20Sopenharmony_ci * @apm identifies the AP adapters in the matrix
588c2ecf20Sopenharmony_ci * @aqm_max: max domain number in @aqm
598c2ecf20Sopenharmony_ci * @aqm identifies the AP queues (domains) in the matrix
608c2ecf20Sopenharmony_ci * @adm_max: max domain number in @adm
618c2ecf20Sopenharmony_ci * @adm identifies the AP control domains in the matrix
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_cistruct ap_matrix {
648c2ecf20Sopenharmony_ci	unsigned long apm_max;
658c2ecf20Sopenharmony_ci	DECLARE_BITMAP(apm, 256);
668c2ecf20Sopenharmony_ci	unsigned long aqm_max;
678c2ecf20Sopenharmony_ci	DECLARE_BITMAP(aqm, 256);
688c2ecf20Sopenharmony_ci	unsigned long adm_max;
698c2ecf20Sopenharmony_ci	DECLARE_BITMAP(adm, 256);
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/**
738c2ecf20Sopenharmony_ci * struct ap_matrix_mdev - the mediated matrix device structure
748c2ecf20Sopenharmony_ci * @list:	allows the ap_matrix_mdev struct to be added to a list
758c2ecf20Sopenharmony_ci * @matrix:	the adapters, usage domains and control domains assigned to the
768c2ecf20Sopenharmony_ci *		mediated matrix device.
778c2ecf20Sopenharmony_ci * @group_notifier: notifier block used for specifying callback function for
788c2ecf20Sopenharmony_ci *		    handling the VFIO_GROUP_NOTIFY_SET_KVM event
798c2ecf20Sopenharmony_ci * @kvm:	the struct holding guest's state
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_cistruct ap_matrix_mdev {
828c2ecf20Sopenharmony_ci	struct list_head node;
838c2ecf20Sopenharmony_ci	struct ap_matrix matrix;
848c2ecf20Sopenharmony_ci	struct notifier_block group_notifier;
858c2ecf20Sopenharmony_ci	struct notifier_block iommu_notifier;
868c2ecf20Sopenharmony_ci	struct kvm *kvm;
878c2ecf20Sopenharmony_ci	struct kvm_s390_module_hook pqap_hook;
888c2ecf20Sopenharmony_ci	struct mdev_device *mdev;
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistruct vfio_ap_queue {
928c2ecf20Sopenharmony_ci	struct ap_matrix_mdev *matrix_mdev;
938c2ecf20Sopenharmony_ci	unsigned long saved_pfn;
948c2ecf20Sopenharmony_ci	int	apqn;
958c2ecf20Sopenharmony_ci#define VFIO_AP_ISC_INVALID 0xff
968c2ecf20Sopenharmony_ci	unsigned char saved_isc;
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciint vfio_ap_mdev_register(void);
1008c2ecf20Sopenharmony_civoid vfio_ap_mdev_unregister(void);
1018c2ecf20Sopenharmony_ciint vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1028c2ecf20Sopenharmony_ci			     unsigned int retry);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#endif /* _VFIO_AP_PRIVATE_H_ */
105