blob: e42a711a2800c0f6cf02535468ba52d2c8f2d74a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Alex Williamsoncba33452012-07-31 08:16:22 -06002/*
3 * VFIO API definition
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
Alex Williamsoncba33452012-07-31 08:16:22 -06007 */
8#ifndef VFIO_H
9#define VFIO_H
10
Alex Williamsoncba33452012-07-31 08:16:22 -060011
12#include <linux/iommu.h>
13#include <linux/mm.h>
Antonios Motakis7e992d62015-03-16 14:08:54 -060014#include <linux/workqueue.h>
15#include <linux/poll.h>
David Howells607ca462012-10-13 10:46:48 +010016#include <uapi/linux/vfio.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060017
18/**
19 * struct vfio_device_ops - VFIO bus driver device callbacks
20 *
21 * @open: Called when userspace creates new file descriptor for device
22 * @release: Called when userspace releases file descriptor for device
23 * @read: Perform read(2) on device file descriptor
24 * @write: Perform write(2) on device file descriptor
25 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
26 * operations documented below
27 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070028 * @request: Request for the bus driver to release the device
Alex Williamsoncba33452012-07-31 08:16:22 -060029 */
30struct vfio_device_ops {
31 char *name;
32 int (*open)(void *device_data);
33 void (*release)(void *device_data);
34 ssize_t (*read)(void *device_data, char __user *buf,
35 size_t count, loff_t *ppos);
36 ssize_t (*write)(void *device_data, const char __user *buf,
37 size_t count, loff_t *size);
38 long (*ioctl)(void *device_data, unsigned int cmd,
39 unsigned long arg);
40 int (*mmap)(void *device_data, struct vm_area_struct *vma);
Alex Williamson13060b62015-02-06 15:05:07 -070041 void (*request)(void *device_data, unsigned int count);
Alex Williamsoncba33452012-07-31 08:16:22 -060042};
43
Alex Williamson03a76b62015-12-21 15:13:33 -070044extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
45extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
46
Alex Williamsoncba33452012-07-31 08:16:22 -060047extern int vfio_add_group_dev(struct device *dev,
48 const struct vfio_device_ops *ops,
49 void *device_data);
50
51extern void *vfio_del_group_dev(struct device *dev);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060052extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
53extern void vfio_device_put(struct vfio_device *device);
54extern void *vfio_device_data(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -060055
56/**
57 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
58 */
59struct vfio_iommu_driver_ops {
60 char *name;
61 struct module *owner;
62 void *(*open)(unsigned long arg);
63 void (*release)(void *iommu_data);
64 ssize_t (*read)(void *iommu_data, char __user *buf,
65 size_t count, loff_t *ppos);
66 ssize_t (*write)(void *iommu_data, const char __user *buf,
67 size_t count, loff_t *size);
68 long (*ioctl)(void *iommu_data, unsigned int cmd,
69 unsigned long arg);
70 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
71 int (*attach_group)(void *iommu_data,
72 struct iommu_group *group);
73 void (*detach_group)(void *iommu_data,
74 struct iommu_group *group);
Kirti Wankhede21690372016-11-17 02:16:17 +053075 int (*pin_pages)(void *iommu_data, unsigned long *user_pfn,
76 int npage, int prot,
77 unsigned long *phys_pfn);
78 int (*unpin_pages)(void *iommu_data,
79 unsigned long *user_pfn, int npage);
Kirti Wankhedec086de812016-11-17 10:28:26 +053080 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +080081 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +053082 struct notifier_block *nb);
83 int (*unregister_notifier)(void *iommu_data,
84 struct notifier_block *nb);
Alex Williamsoncba33452012-07-31 08:16:22 -060085};
86
87extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
88
89extern void vfio_unregister_iommu_driver(
90 const struct vfio_iommu_driver_ops *ops);
91
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -060092/*
93 * External user API
94 */
95extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
96extern void vfio_group_put_external_user(struct vfio_group *group);
Alex Williamson5d6dee82017-06-28 13:50:05 -060097extern bool vfio_external_group_match_file(struct vfio_group *group,
98 struct file *filep);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -060099extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700100extern long vfio_external_check_extension(struct vfio_group *group,
101 unsigned long arg);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600102
Kirti Wankhede21690372016-11-17 02:16:17 +0530103#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
104
105extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
106 int npage, int prot, unsigned long *phys_pfn);
107extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
108 int npage);
109
Jike Song22195cb2016-12-01 13:20:05 +0800110/* each type has independent events */
111enum vfio_notify_type {
112 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800113 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800114};
115
116/* events for VFIO_IOMMU_NOTIFY */
117#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530118
Jike Songccd46db2016-12-01 13:20:06 +0800119/* events for VFIO_GROUP_NOTIFY */
120#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
121
Kirti Wankhedec086de812016-11-17 10:28:26 +0530122extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800123 enum vfio_notify_type type,
124 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530125 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530126extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800127 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530128 struct notifier_block *nb);
129
Jike Songccd46db2016-12-01 13:20:06 +0800130struct kvm;
131extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
132
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700133/*
134 * Sub-module helpers
135 */
136struct vfio_info_cap {
137 struct vfio_info_cap_header *buf;
138 size_t size;
139};
140extern struct vfio_info_cap_header *vfio_info_cap_add(
141 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
142extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
143
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530144extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700145 struct vfio_info_cap_header *cap,
146 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530147
Kirti Wankhedec747f082016-11-17 02:16:27 +0530148extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
149 int num_irqs, int max_irq_type,
150 size_t *data_size);
151
Gavin Shan92d18a62014-08-08 10:36:20 -0600152struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300153#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600154extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000155extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
156extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
157 unsigned int cmd,
158 unsigned long arg);
159#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600160static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000161{
Gavin Shan1b69be52014-06-10 11:41:57 +1000162}
163
164static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
165{
166}
167
168static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
169 unsigned int cmd,
170 unsigned long arg)
171{
172 return -ENOTTY;
173}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300174#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600175
176/*
177 * IRQfd - generic
178 */
179struct virqfd {
180 void *opaque;
181 struct eventfd_ctx *eventfd;
182 int (*handler)(void *, void *);
183 void (*thread)(void *, void *);
184 void *data;
185 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200186 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600187 poll_table pt;
188 struct work_struct shutdown;
189 struct virqfd **pvirqfd;
190};
191
Antonios Motakis7e992d62015-03-16 14:08:54 -0600192extern int vfio_virqfd_enable(void *opaque,
193 int (*handler)(void *, void *),
194 void (*thread)(void *, void *),
195 void *data, struct virqfd **pvirqfd, int fd);
196extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
197
Alex Williamsoncba33452012-07-31 08:16:22 -0600198#endif /* VFIO_H */