blob: 784c34c0a28763d58eb91706fd9d1b15db2bbc04 [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
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060018struct vfio_device {
19 struct device *dev;
20 const struct vfio_device_ops *ops;
21 struct vfio_group *group;
22
23 /* Members below here are private, not for driver use */
24 refcount_t refcount;
25 struct completion comp;
26 struct list_head group_next;
27 void *device_data;
28};
29
Alex Williamsoncba33452012-07-31 08:16:22 -060030/**
31 * struct vfio_device_ops - VFIO bus driver device callbacks
32 *
33 * @open: Called when userspace creates new file descriptor for device
34 * @release: Called when userspace releases file descriptor for device
35 * @read: Perform read(2) on device file descriptor
36 * @write: Perform write(2) on device file descriptor
37 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
38 * operations documented below
39 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070040 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060041 * @match: Optional device name match callback (return: 0 for no-match, >0 for
42 * match, -errno for abort (ex. match with insufficient or incorrect
43 * additional args)
Alex Williamsoncba33452012-07-31 08:16:22 -060044 */
45struct vfio_device_ops {
46 char *name;
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060047 int (*open)(struct vfio_device *vdev);
48 void (*release)(struct vfio_device *vdev);
49 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060050 size_t count, loff_t *ppos);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060051 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060052 size_t count, loff_t *size);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060053 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
Alex Williamsoncba33452012-07-31 08:16:22 -060054 unsigned long arg);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060055 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
56 void (*request)(struct vfio_device *vdev, unsigned int count);
57 int (*match)(struct vfio_device *vdev, char *buf);
Alex Williamsoncba33452012-07-31 08:16:22 -060058};
59
Alex Williamson03a76b62015-12-21 15:13:33 -070060extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
61extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
62
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060063void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
64 const struct vfio_device_ops *ops, void *device_data);
65int vfio_register_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060066void vfio_unregister_group_dev(struct vfio_device *device);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060067extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
68extern void vfio_device_put(struct vfio_device *device);
69extern void *vfio_device_data(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -060070
Steve Sistareec5e32942021-01-29 08:54:10 -080071/* events for the backend driver notify callback */
72enum vfio_iommu_notify_type {
73 VFIO_IOMMU_CONTAINER_CLOSE = 0,
74};
75
Alex Williamsoncba33452012-07-31 08:16:22 -060076/**
77 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
78 */
79struct vfio_iommu_driver_ops {
80 char *name;
81 struct module *owner;
82 void *(*open)(unsigned long arg);
83 void (*release)(void *iommu_data);
84 ssize_t (*read)(void *iommu_data, char __user *buf,
85 size_t count, loff_t *ppos);
86 ssize_t (*write)(void *iommu_data, const char __user *buf,
87 size_t count, loff_t *size);
88 long (*ioctl)(void *iommu_data, unsigned int cmd,
89 unsigned long arg);
90 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
91 int (*attach_group)(void *iommu_data,
92 struct iommu_group *group);
93 void (*detach_group)(void *iommu_data,
94 struct iommu_group *group);
Kirti Wankhede95fc87b4412020-05-29 02:00:54 +053095 int (*pin_pages)(void *iommu_data,
96 struct iommu_group *group,
97 unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +053098 int npage, int prot,
99 unsigned long *phys_pfn);
100 int (*unpin_pages)(void *iommu_data,
101 unsigned long *user_pfn, int npage);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530102 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +0800103 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530104 struct notifier_block *nb);
105 int (*unregister_notifier)(void *iommu_data,
106 struct notifier_block *nb);
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600107 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
108 void *data, size_t count, bool write);
Lu Baolubdfae1c2020-12-09 09:44:44 +0800109 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
110 struct iommu_group *group);
Steve Sistareec5e32942021-01-29 08:54:10 -0800111 void (*notify)(void *iommu_data,
112 enum vfio_iommu_notify_type event);
Alex Williamsoncba33452012-07-31 08:16:22 -0600113};
114
115extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
116
117extern void vfio_unregister_iommu_driver(
118 const struct vfio_iommu_driver_ops *ops);
119
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600120/*
121 * External user API
122 */
123extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
124extern void vfio_group_put_external_user(struct vfio_group *group);
Yan Zhaoc0560f52020-03-24 09:27:56 -0600125extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
126 *dev);
Alex Williamson5d6dee82017-06-28 13:50:05 -0600127extern bool vfio_external_group_match_file(struct vfio_group *group,
128 struct file *filep);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600129extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700130extern long vfio_external_check_extension(struct vfio_group *group,
131 unsigned long arg);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600132
Kirti Wankhede21690372016-11-17 02:16:17 +0530133#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
134
135extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
136 int npage, int prot, unsigned long *phys_pfn);
137extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
138 int npage);
139
Yan Zhao40280cf2020-03-24 09:27:57 -0600140extern int vfio_group_pin_pages(struct vfio_group *group,
141 unsigned long *user_iova_pfn, int npage,
142 int prot, unsigned long *phys_pfn);
143extern int vfio_group_unpin_pages(struct vfio_group *group,
144 unsigned long *user_iova_pfn, int npage);
145
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600146extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
147 void *data, size_t len, bool write);
148
Lu Baolubdfae1c2020-12-09 09:44:44 +0800149extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
150
Jike Song22195cb2016-12-01 13:20:05 +0800151/* each type has independent events */
152enum vfio_notify_type {
153 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800154 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800155};
156
157/* events for VFIO_IOMMU_NOTIFY */
158#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530159
Jike Songccd46db2016-12-01 13:20:06 +0800160/* events for VFIO_GROUP_NOTIFY */
161#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
162
Kirti Wankhedec086de812016-11-17 10:28:26 +0530163extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800164 enum vfio_notify_type type,
165 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530166 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530167extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800168 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530169 struct notifier_block *nb);
170
Jike Songccd46db2016-12-01 13:20:06 +0800171struct kvm;
172extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
173
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700174/*
175 * Sub-module helpers
176 */
177struct vfio_info_cap {
178 struct vfio_info_cap_header *buf;
179 size_t size;
180};
181extern struct vfio_info_cap_header *vfio_info_cap_add(
182 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
183extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
184
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530185extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700186 struct vfio_info_cap_header *cap,
187 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530188
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530189extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
190 int num_irqs, int max_irq_type,
191 size_t *data_size);
192
Gavin Shan92d18a62014-08-08 10:36:20 -0600193struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300194#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600195extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000196extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
197extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
198 unsigned int cmd,
199 unsigned long arg);
200#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600201static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000202{
Gavin Shan1b69be52014-06-10 11:41:57 +1000203}
204
205static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
206{
207}
208
209static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
210 unsigned int cmd,
211 unsigned long arg)
212{
213 return -ENOTTY;
214}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300215#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600216
217/*
218 * IRQfd - generic
219 */
220struct virqfd {
221 void *opaque;
222 struct eventfd_ctx *eventfd;
223 int (*handler)(void *, void *);
224 void (*thread)(void *, void *);
225 void *data;
226 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200227 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600228 poll_table pt;
229 struct work_struct shutdown;
230 struct virqfd **pvirqfd;
231};
232
Antonios Motakis7e992d62015-03-16 14:08:54 -0600233extern int vfio_virqfd_enable(void *opaque,
234 int (*handler)(void *, void *),
235 void (*thread)(void *, void *),
236 void *data, struct virqfd **pvirqfd, int fd);
237extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
238
Alex Williamsoncba33452012-07-31 08:16:22 -0600239#endif /* VFIO_H */