blob: b7e18bde5aa8b3c6977aa0e822312181a463904a [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 <[email protected]>
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
Steve Sistareec5e32942021-01-29 08:54:10 -080060/* events for the backend driver notify callback */
61enum vfio_iommu_notify_type {
62 VFIO_IOMMU_CONTAINER_CLOSE = 0,
63};
64
Alex Williamsoncba33452012-07-31 08:16:22 -060065/**
66 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
67 */
68struct vfio_iommu_driver_ops {
69 char *name;
70 struct module *owner;
71 void *(*open)(unsigned long arg);
72 void (*release)(void *iommu_data);
73 ssize_t (*read)(void *iommu_data, char __user *buf,
74 size_t count, loff_t *ppos);
75 ssize_t (*write)(void *iommu_data, const char __user *buf,
76 size_t count, loff_t *size);
77 long (*ioctl)(void *iommu_data, unsigned int cmd,
78 unsigned long arg);
79 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
80 int (*attach_group)(void *iommu_data,
81 struct iommu_group *group);
82 void (*detach_group)(void *iommu_data,
83 struct iommu_group *group);
Kirti Wankhede95fc87b4412020-05-29 02:00:54 +053084 int (*pin_pages)(void *iommu_data,
85 struct iommu_group *group,
86 unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +053087 int npage, int prot,
88 unsigned long *phys_pfn);
89 int (*unpin_pages)(void *iommu_data,
90 unsigned long *user_pfn, int npage);
Kirti Wankhedec086de812016-11-17 10:28:26 +053091 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +080092 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +053093 struct notifier_block *nb);
94 int (*unregister_notifier)(void *iommu_data,
95 struct notifier_block *nb);
Yan Zhao8d46c0c2020-03-24 09:27:57 -060096 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
97 void *data, size_t count, bool write);
Lu Baolubdfae1c2020-12-09 09:44:44 +080098 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
99 struct iommu_group *group);
Steve Sistareec5e32942021-01-29 08:54:10 -0800100 void (*notify)(void *iommu_data,
101 enum vfio_iommu_notify_type event);
Alex Williamsoncba33452012-07-31 08:16:22 -0600102};
103
104extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
105
106extern void vfio_unregister_iommu_driver(
107 const struct vfio_iommu_driver_ops *ops);
108
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600109/*
110 * External user API
111 */
112extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
113extern void vfio_group_put_external_user(struct vfio_group *group);
Yan Zhaoc0560f52020-03-24 09:27:56 -0600114extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
115 *dev);
Alex Williamson5d6dee82017-06-28 13:50:05 -0600116extern bool vfio_external_group_match_file(struct vfio_group *group,
117 struct file *filep);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600118extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700119extern long vfio_external_check_extension(struct vfio_group *group,
120 unsigned long arg);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600121
Kirti Wankhede21690372016-11-17 02:16:17 +0530122#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
123
124extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
125 int npage, int prot, unsigned long *phys_pfn);
126extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
127 int npage);
128
Yan Zhao40280cf2020-03-24 09:27:57 -0600129extern int vfio_group_pin_pages(struct vfio_group *group,
130 unsigned long *user_iova_pfn, int npage,
131 int prot, unsigned long *phys_pfn);
132extern int vfio_group_unpin_pages(struct vfio_group *group,
133 unsigned long *user_iova_pfn, int npage);
134
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600135extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
136 void *data, size_t len, bool write);
137
Lu Baolubdfae1c2020-12-09 09:44:44 +0800138extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
139
Jike Song22195cb2016-12-01 13:20:05 +0800140/* each type has independent events */
141enum vfio_notify_type {
142 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800143 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800144};
145
146/* events for VFIO_IOMMU_NOTIFY */
147#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530148
Jike Songccd46db2016-12-01 13:20:06 +0800149/* events for VFIO_GROUP_NOTIFY */
150#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
151
Kirti Wankhedec086de812016-11-17 10:28:26 +0530152extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800153 enum vfio_notify_type type,
154 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530155 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530156extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800157 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530158 struct notifier_block *nb);
159
Jike Songccd46db2016-12-01 13:20:06 +0800160struct kvm;
161extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
162
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700163/*
164 * Sub-module helpers
165 */
166struct vfio_info_cap {
167 struct vfio_info_cap_header *buf;
168 size_t size;
169};
170extern struct vfio_info_cap_header *vfio_info_cap_add(
171 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
172extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
173
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530174extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700175 struct vfio_info_cap_header *cap,
176 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530177
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530178extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
179 int num_irqs, int max_irq_type,
180 size_t *data_size);
181
Gavin Shan92d18a62014-08-08 10:36:20 -0600182struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300183#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600184extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000185extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
186extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
187 unsigned int cmd,
188 unsigned long arg);
189#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600190static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000191{
Gavin Shan1b69be52014-06-10 11:41:57 +1000192}
193
194static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
195{
196}
197
198static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
199 unsigned int cmd,
200 unsigned long arg)
201{
202 return -ENOTTY;
203}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300204#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600205
206/*
207 * IRQfd - generic
208 */
209struct virqfd {
210 void *opaque;
211 struct eventfd_ctx *eventfd;
212 int (*handler)(void *, void *);
213 void (*thread)(void *, void *);
214 void *data;
215 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200216 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600217 poll_table pt;
218 struct work_struct shutdown;
219 struct virqfd **pvirqfd;
220};
221
Antonios Motakis7e992d62015-03-16 14:08:54 -0600222extern int vfio_virqfd_enable(void *opaque,
223 int (*handler)(void *, void *),
224 void (*thread)(void *, void *),
225 void *data, struct virqfd **pvirqfd, int fd);
226extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
227
Alex Williamsoncba33452012-07-31 08:16:22 -0600228#endif /* VFIO_H */