2019-06-04 11:11:33 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2012-07-31 18:16:22 +04:00
|
|
|
/*
|
|
|
|
* VFIO API definition
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc. All rights reserved.
|
|
|
|
* Author: Alex Williamson <alex.williamson@redhat.com>
|
|
|
|
*/
|
|
|
|
#ifndef VFIO_H
|
|
|
|
#define VFIO_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <linux/iommu.h>
|
|
|
|
#include <linux/mm.h>
|
2015-03-16 23:08:54 +03:00
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/poll.h>
|
2012-10-13 13:46:48 +04:00
|
|
|
#include <uapi/linux/vfio.h>
|
2012-07-31 18:16:22 +04:00
|
|
|
|
2021-08-06 04:19:00 +03:00
|
|
|
/*
|
|
|
|
* VFIO devices can be placed in a set, this allows all devices to share this
|
|
|
|
* structure and the VFIO core will provide a lock that is held around
|
|
|
|
* open_device()/close_device() for all devices in the set.
|
|
|
|
*/
|
|
|
|
struct vfio_device_set {
|
|
|
|
void *set_id;
|
|
|
|
struct mutex lock;
|
|
|
|
struct list_head device_list;
|
|
|
|
unsigned int device_count;
|
|
|
|
};
|
|
|
|
|
2021-03-30 18:53:05 +03:00
|
|
|
struct vfio_device {
|
|
|
|
struct device *dev;
|
|
|
|
const struct vfio_device_ops *ops;
|
|
|
|
struct vfio_group *group;
|
2021-08-06 04:19:00 +03:00
|
|
|
struct vfio_device_set *dev_set;
|
|
|
|
struct list_head dev_set_list;
|
2021-03-30 18:53:05 +03:00
|
|
|
|
|
|
|
/* Members below here are private, not for driver use */
|
|
|
|
refcount_t refcount;
|
2021-08-06 04:19:00 +03:00
|
|
|
unsigned int open_count;
|
2021-03-30 18:53:05 +03:00
|
|
|
struct completion comp;
|
|
|
|
struct list_head group_next;
|
|
|
|
};
|
|
|
|
|
2012-07-31 18:16:22 +04:00
|
|
|
/**
|
|
|
|
* struct vfio_device_ops - VFIO bus driver device callbacks
|
|
|
|
*
|
2021-08-06 04:19:00 +03:00
|
|
|
* @open_device: Called when the first file descriptor is opened for this device
|
|
|
|
* @close_device: Opposite of open_device
|
2012-07-31 18:16:22 +04:00
|
|
|
* @read: Perform read(2) on device file descriptor
|
|
|
|
* @write: Perform write(2) on device file descriptor
|
|
|
|
* @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
|
|
|
|
* operations documented below
|
|
|
|
* @mmap: Perform mmap(2) on a region of the device file descriptor
|
2015-02-07 01:05:07 +03:00
|
|
|
* @request: Request for the bus driver to release the device
|
2020-03-24 18:28:25 +03:00
|
|
|
* @match: Optional device name match callback (return: 0 for no-match, >0 for
|
|
|
|
* match, -errno for abort (ex. match with insufficient or incorrect
|
|
|
|
* additional args)
|
2012-07-31 18:16:22 +04:00
|
|
|
*/
|
|
|
|
struct vfio_device_ops {
|
|
|
|
char *name;
|
2021-08-06 04:19:00 +03:00
|
|
|
int (*open_device)(struct vfio_device *vdev);
|
|
|
|
void (*close_device)(struct vfio_device *vdev);
|
2021-03-30 18:53:08 +03:00
|
|
|
ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
|
2012-07-31 18:16:22 +04:00
|
|
|
size_t count, loff_t *ppos);
|
2021-03-30 18:53:08 +03:00
|
|
|
ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
|
2012-07-31 18:16:22 +04:00
|
|
|
size_t count, loff_t *size);
|
2021-03-30 18:53:08 +03:00
|
|
|
long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
|
2012-07-31 18:16:22 +04:00
|
|
|
unsigned long arg);
|
2021-03-30 18:53:08 +03:00
|
|
|
int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
|
|
|
|
void (*request)(struct vfio_device *vdev, unsigned int count);
|
|
|
|
int (*match)(struct vfio_device *vdev, char *buf);
|
2012-07-31 18:16:22 +04:00
|
|
|
};
|
|
|
|
|
2015-12-22 01:13:33 +03:00
|
|
|
extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
|
|
|
|
extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
|
|
|
|
|
2021-03-30 18:53:05 +03:00
|
|
|
void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
|
2021-03-30 18:53:08 +03:00
|
|
|
const struct vfio_device_ops *ops);
|
2021-08-06 04:18:59 +03:00
|
|
|
void vfio_uninit_group_dev(struct vfio_device *device);
|
2021-03-30 18:53:05 +03:00
|
|
|
int vfio_register_group_dev(struct vfio_device *device);
|
|
|
|
void vfio_unregister_group_dev(struct vfio_device *device);
|
2013-03-11 19:28:44 +04:00
|
|
|
extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
|
|
|
|
extern void vfio_device_put(struct vfio_device *device);
|
2012-07-31 18:16:22 +04:00
|
|
|
|
2021-08-06 04:19:00 +03:00
|
|
|
int vfio_assign_device_set(struct vfio_device *device, void *set_id);
|
|
|
|
|
2021-01-29 19:54:10 +03:00
|
|
|
/* events for the backend driver notify callback */
|
|
|
|
enum vfio_iommu_notify_type {
|
|
|
|
VFIO_IOMMU_CONTAINER_CLOSE = 0,
|
|
|
|
};
|
|
|
|
|
2012-07-31 18:16:22 +04:00
|
|
|
/**
|
|
|
|
* struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
|
|
|
|
*/
|
|
|
|
struct vfio_iommu_driver_ops {
|
|
|
|
char *name;
|
|
|
|
struct module *owner;
|
|
|
|
void *(*open)(unsigned long arg);
|
|
|
|
void (*release)(void *iommu_data);
|
|
|
|
ssize_t (*read)(void *iommu_data, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos);
|
|
|
|
ssize_t (*write)(void *iommu_data, const char __user *buf,
|
|
|
|
size_t count, loff_t *size);
|
|
|
|
long (*ioctl)(void *iommu_data, unsigned int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
|
|
|
|
int (*attach_group)(void *iommu_data,
|
|
|
|
struct iommu_group *group);
|
|
|
|
void (*detach_group)(void *iommu_data,
|
|
|
|
struct iommu_group *group);
|
2020-05-28 23:30:54 +03:00
|
|
|
int (*pin_pages)(void *iommu_data,
|
|
|
|
struct iommu_group *group,
|
|
|
|
unsigned long *user_pfn,
|
2016-11-16 23:46:17 +03:00
|
|
|
int npage, int prot,
|
|
|
|
unsigned long *phys_pfn);
|
|
|
|
int (*unpin_pages)(void *iommu_data,
|
|
|
|
unsigned long *user_pfn, int npage);
|
2016-11-17 07:58:26 +03:00
|
|
|
int (*register_notifier)(void *iommu_data,
|
2016-12-01 08:20:05 +03:00
|
|
|
unsigned long *events,
|
2016-11-17 07:58:26 +03:00
|
|
|
struct notifier_block *nb);
|
|
|
|
int (*unregister_notifier)(void *iommu_data,
|
|
|
|
struct notifier_block *nb);
|
2020-03-24 18:27:57 +03:00
|
|
|
int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
|
|
|
|
void *data, size_t count, bool write);
|
2020-12-09 04:44:44 +03:00
|
|
|
struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
|
|
|
|
struct iommu_group *group);
|
2021-01-29 19:54:10 +03:00
|
|
|
void (*notify)(void *iommu_data,
|
|
|
|
enum vfio_iommu_notify_type event);
|
2012-07-31 18:16:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
|
|
|
|
|
|
|
|
extern void vfio_unregister_iommu_driver(
|
|
|
|
const struct vfio_iommu_driver_ops *ops);
|
|
|
|
|
2013-08-05 20:52:36 +04:00
|
|
|
/*
|
|
|
|
* External user API
|
|
|
|
*/
|
|
|
|
extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
|
|
|
|
extern void vfio_group_put_external_user(struct vfio_group *group);
|
2020-03-24 18:27:56 +03:00
|
|
|
extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
|
|
|
|
*dev);
|
2017-06-28 22:50:05 +03:00
|
|
|
extern bool vfio_external_group_match_file(struct vfio_group *group,
|
|
|
|
struct file *filep);
|
2013-08-05 20:52:36 +04:00
|
|
|
extern int vfio_external_user_iommu_id(struct vfio_group *group);
|
2014-02-26 22:38:39 +04:00
|
|
|
extern long vfio_external_check_extension(struct vfio_group *group,
|
|
|
|
unsigned long arg);
|
2013-08-05 20:52:36 +04:00
|
|
|
|
2016-11-16 23:46:17 +03:00
|
|
|
#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
|
|
|
|
|
|
|
|
extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
|
|
|
|
int npage, int prot, unsigned long *phys_pfn);
|
|
|
|
extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
|
|
|
|
int npage);
|
|
|
|
|
2020-03-24 18:27:57 +03:00
|
|
|
extern int vfio_group_pin_pages(struct vfio_group *group,
|
|
|
|
unsigned long *user_iova_pfn, int npage,
|
|
|
|
int prot, unsigned long *phys_pfn);
|
|
|
|
extern int vfio_group_unpin_pages(struct vfio_group *group,
|
|
|
|
unsigned long *user_iova_pfn, int npage);
|
|
|
|
|
2020-03-24 18:27:57 +03:00
|
|
|
extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
|
|
|
|
void *data, size_t len, bool write);
|
|
|
|
|
2020-12-09 04:44:44 +03:00
|
|
|
extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
|
|
|
|
|
2016-12-01 08:20:05 +03:00
|
|
|
/* each type has independent events */
|
|
|
|
enum vfio_notify_type {
|
|
|
|
VFIO_IOMMU_NOTIFY = 0,
|
2016-12-01 08:20:06 +03:00
|
|
|
VFIO_GROUP_NOTIFY = 1,
|
2016-12-01 08:20:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* events for VFIO_IOMMU_NOTIFY */
|
|
|
|
#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
|
2016-11-17 07:58:26 +03:00
|
|
|
|
2016-12-01 08:20:06 +03:00
|
|
|
/* events for VFIO_GROUP_NOTIFY */
|
|
|
|
#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
|
|
|
|
|
2016-11-17 07:58:26 +03:00
|
|
|
extern int vfio_register_notifier(struct device *dev,
|
2016-12-01 08:20:05 +03:00
|
|
|
enum vfio_notify_type type,
|
|
|
|
unsigned long *required_events,
|
2016-11-17 07:58:26 +03:00
|
|
|
struct notifier_block *nb);
|
|
|
|
extern int vfio_unregister_notifier(struct device *dev,
|
2016-12-01 08:20:05 +03:00
|
|
|
enum vfio_notify_type type,
|
2016-11-17 07:58:26 +03:00
|
|
|
struct notifier_block *nb);
|
|
|
|
|
2016-12-01 08:20:06 +03:00
|
|
|
struct kvm;
|
|
|
|
extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
|
|
|
|
|
2016-02-23 02:02:33 +03:00
|
|
|
/*
|
|
|
|
* Sub-module helpers
|
|
|
|
*/
|
|
|
|
struct vfio_info_cap {
|
|
|
|
struct vfio_info_cap_header *buf;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
extern struct vfio_info_cap_header *vfio_info_cap_add(
|
|
|
|
struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
|
|
|
|
extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
|
|
|
|
|
2016-11-16 23:46:25 +03:00
|
|
|
extern int vfio_info_add_capability(struct vfio_info_cap *caps,
|
2017-12-12 22:59:39 +03:00
|
|
|
struct vfio_info_cap_header *cap,
|
|
|
|
size_t size);
|
2016-11-16 23:46:25 +03:00
|
|
|
|
2016-11-16 23:46:27 +03:00
|
|
|
extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
|
|
|
|
int num_irqs, int max_irq_type,
|
|
|
|
size_t *data_size);
|
|
|
|
|
2014-08-08 20:36:20 +04:00
|
|
|
struct pci_dev;
|
2017-07-18 20:22:20 +03:00
|
|
|
#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
|
2014-08-08 20:39:16 +04:00
|
|
|
extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
|
2014-06-10 05:41:57 +04:00
|
|
|
extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
|
|
|
|
extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
|
|
|
|
unsigned int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
#else
|
2014-08-08 20:39:16 +04:00
|
|
|
static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
|
2014-06-10 05:41:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
|
|
|
|
unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
2017-07-18 20:22:20 +03:00
|
|
|
#endif /* CONFIG_VFIO_SPAPR_EEH */
|
2015-03-16 23:08:54 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IRQfd - generic
|
|
|
|
*/
|
|
|
|
struct virqfd {
|
|
|
|
void *opaque;
|
|
|
|
struct eventfd_ctx *eventfd;
|
|
|
|
int (*handler)(void *, void *);
|
|
|
|
void (*thread)(void *, void *);
|
|
|
|
void *data;
|
|
|
|
struct work_struct inject;
|
2017-06-20 13:06:13 +03:00
|
|
|
wait_queue_entry_t wait;
|
2015-03-16 23:08:54 +03:00
|
|
|
poll_table pt;
|
|
|
|
struct work_struct shutdown;
|
|
|
|
struct virqfd **pvirqfd;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern int vfio_virqfd_enable(void *opaque,
|
|
|
|
int (*handler)(void *, void *),
|
|
|
|
void (*thread)(void *, void *),
|
|
|
|
void *data, struct virqfd **pvirqfd, int fd);
|
|
|
|
extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
|
|
|
|
|
2012-07-31 18:16:22 +04:00
|
|
|
#endif /* VFIO_H */
|