blob: 38d3c6a8dc7e05ec6e94fc17cfcaf973b7fb9367 [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 Williamson5f3874c2020-03-24 09:28:25 -060029 * @match: Optional device name match callback (return: 0 for no-match, >0 for
30 * match, -errno for abort (ex. match with insufficient or incorrect
31 * additional args)
Alex Williamsoncba33452012-07-31 08:16:22 -060032 */
33struct vfio_device_ops {
34 char *name;
35 int (*open)(void *device_data);
36 void (*release)(void *device_data);
37 ssize_t (*read)(void *device_data, char __user *buf,
38 size_t count, loff_t *ppos);
39 ssize_t (*write)(void *device_data, const char __user *buf,
40 size_t count, loff_t *size);
41 long (*ioctl)(void *device_data, unsigned int cmd,
42 unsigned long arg);
43 int (*mmap)(void *device_data, struct vm_area_struct *vma);
Alex Williamson13060b62015-02-06 15:05:07 -070044 void (*request)(void *device_data, unsigned int count);
Alex Williamson5f3874c2020-03-24 09:28:25 -060045 int (*match)(void *device_data, char *buf);
Alex Williamsoncba33452012-07-31 08:16:22 -060046};
47
Alex Williamson03a76b62015-12-21 15:13:33 -070048extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
49extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
50
Alex Williamsoncba33452012-07-31 08:16:22 -060051extern int vfio_add_group_dev(struct device *dev,
52 const struct vfio_device_ops *ops,
53 void *device_data);
54
55extern void *vfio_del_group_dev(struct device *dev);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060056extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
57extern void vfio_device_put(struct vfio_device *device);
58extern void *vfio_device_data(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -060059
60/**
61 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
62 */
63struct vfio_iommu_driver_ops {
64 char *name;
65 struct module *owner;
66 void *(*open)(unsigned long arg);
67 void (*release)(void *iommu_data);
68 ssize_t (*read)(void *iommu_data, char __user *buf,
69 size_t count, loff_t *ppos);
70 ssize_t (*write)(void *iommu_data, const char __user *buf,
71 size_t count, loff_t *size);
72 long (*ioctl)(void *iommu_data, unsigned int cmd,
73 unsigned long arg);
74 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
75 int (*attach_group)(void *iommu_data,
76 struct iommu_group *group);
77 void (*detach_group)(void *iommu_data,
78 struct iommu_group *group);
Kirti Wankhede95fc87b2020-05-29 02:00:54 +053079 int (*pin_pages)(void *iommu_data,
80 struct iommu_group *group,
81 unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +053082 int npage, int prot,
83 unsigned long *phys_pfn);
84 int (*unpin_pages)(void *iommu_data,
85 unsigned long *user_pfn, int npage);
Kirti Wankhedec086de812016-11-17 10:28:26 +053086 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +080087 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +053088 struct notifier_block *nb);
89 int (*unregister_notifier)(void *iommu_data,
90 struct notifier_block *nb);
Yan Zhao8d46c0c2020-03-24 09:27:57 -060091 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
92 void *data, size_t count, bool write);
Alex Williamsoncba33452012-07-31 08:16:22 -060093};
94
95extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
96
97extern void vfio_unregister_iommu_driver(
98 const struct vfio_iommu_driver_ops *ops);
99
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600100/*
101 * External user API
102 */
103extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
104extern void vfio_group_put_external_user(struct vfio_group *group);
Yan Zhaoc0560f52020-03-24 09:27:56 -0600105extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
106 *dev);
Alex Williamson5d6dee82017-06-28 13:50:05 -0600107extern bool vfio_external_group_match_file(struct vfio_group *group,
108 struct file *filep);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600109extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700110extern long vfio_external_check_extension(struct vfio_group *group,
111 unsigned long arg);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600112
Kirti Wankhede21690372016-11-17 02:16:17 +0530113#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
114
115extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
116 int npage, int prot, unsigned long *phys_pfn);
117extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
118 int npage);
119
Yan Zhao40280cf2020-03-24 09:27:57 -0600120extern int vfio_group_pin_pages(struct vfio_group *group,
121 unsigned long *user_iova_pfn, int npage,
122 int prot, unsigned long *phys_pfn);
123extern int vfio_group_unpin_pages(struct vfio_group *group,
124 unsigned long *user_iova_pfn, int npage);
125
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600126extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
127 void *data, size_t len, bool write);
128
Jike Song22195cb2016-12-01 13:20:05 +0800129/* each type has independent events */
130enum vfio_notify_type {
131 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800132 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800133};
134
135/* events for VFIO_IOMMU_NOTIFY */
136#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530137
Jike Songccd46db2016-12-01 13:20:06 +0800138/* events for VFIO_GROUP_NOTIFY */
139#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
140
Kirti Wankhedec086de812016-11-17 10:28:26 +0530141extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800142 enum vfio_notify_type type,
143 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530144 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530145extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800146 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530147 struct notifier_block *nb);
148
Jike Songccd46db2016-12-01 13:20:06 +0800149struct kvm;
150extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
151
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700152/*
153 * Sub-module helpers
154 */
155struct vfio_info_cap {
156 struct vfio_info_cap_header *buf;
157 size_t size;
158};
159extern struct vfio_info_cap_header *vfio_info_cap_add(
160 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
161extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
162
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530163extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700164 struct vfio_info_cap_header *cap,
165 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530166
Kirti Wankhedec747f082016-11-17 02:16:27 +0530167extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
168 int num_irqs, int max_irq_type,
169 size_t *data_size);
170
Gavin Shan92d18a62014-08-08 10:36:20 -0600171struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300172#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600173extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000174extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
175extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
176 unsigned int cmd,
177 unsigned long arg);
178#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600179static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000180{
Gavin Shan1b69be52014-06-10 11:41:57 +1000181}
182
183static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
184{
185}
186
187static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
188 unsigned int cmd,
189 unsigned long arg)
190{
191 return -ENOTTY;
192}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300193#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600194
195/*
196 * IRQfd - generic
197 */
198struct virqfd {
199 void *opaque;
200 struct eventfd_ctx *eventfd;
201 int (*handler)(void *, void *);
202 void (*thread)(void *, void *);
203 void *data;
204 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200205 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600206 poll_table pt;
207 struct work_struct shutdown;
208 struct virqfd **pvirqfd;
209};
210
Antonios Motakis7e992d62015-03-16 14:08:54 -0600211extern int vfio_virqfd_enable(void *opaque,
212 int (*handler)(void *, void *),
213 void (*thread)(void *, void *),
214 void *data, struct virqfd **pvirqfd, int fd);
215extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
216
Alex Williamsoncba33452012-07-31 08:16:22 -0600217#endif /* VFIO_H */