blob: 0e282655909164a6798fd2e8eb54ddf3f3a01533 [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>
Yishai Hadas80c4b922022-09-08 21:34:43 +030017#include <linux/iova_bitmap.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060018
Jason Gunthorpeba70a892022-05-04 16:14:44 -030019struct kvm;
20
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030021/*
22 * VFIO devices can be placed in a set, this allows all devices to share this
23 * structure and the VFIO core will provide a lock that is held around
24 * open_device()/close_device() for all devices in the set.
25 */
26struct vfio_device_set {
27 void *set_id;
28 struct mutex lock;
29 struct list_head device_list;
30 unsigned int device_count;
31};
32
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060033struct vfio_device {
34 struct device *dev;
35 const struct vfio_device_ops *ops;
Yishai Hadas6e97eba2022-06-28 18:59:10 +030036 /*
Yishai Hadas80c4b922022-09-08 21:34:43 +030037 * mig_ops/log_ops is a static property of the vfio_device which must
38 * be set prior to registering the vfio_device.
Yishai Hadas6e97eba2022-06-28 18:59:10 +030039 */
40 const struct vfio_migration_ops *mig_ops;
Yishai Hadas80c4b922022-09-08 21:34:43 +030041 const struct vfio_log_ops *log_ops;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060042 struct vfio_group *group;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030043 struct vfio_device_set *dev_set;
44 struct list_head dev_set_list;
Jason Gunthorpe8cb3d832022-02-24 16:20:19 +020045 unsigned int migration_flags;
Matthew Rosato421cfe62022-05-19 14:33:11 -040046 /* Driver must reference the kvm during open_device or never touch it */
47 struct kvm *kvm;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060048
49 /* Members below here are private, not for driver use */
50 refcount_t refcount;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030051 unsigned int open_count;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060052 struct completion comp;
53 struct list_head group_next;
Jason Gunthorpe8cfc5b62022-07-19 21:02:49 -030054 struct list_head iommu_entry;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060055};
56
Alex Williamsoncba33452012-07-31 08:16:22 -060057/**
58 * struct vfio_device_ops - VFIO bus driver device callbacks
59 *
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030060 * @open_device: Called when the first file descriptor is opened for this device
61 * @close_device: Opposite of open_device
Alex Williamsoncba33452012-07-31 08:16:22 -060062 * @read: Perform read(2) on device file descriptor
63 * @write: Perform write(2) on device file descriptor
64 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
65 * operations documented below
66 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070067 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060068 * @match: Optional device name match callback (return: 0 for no-match, >0 for
69 * match, -errno for abort (ex. match with insufficient or incorrect
70 * additional args)
Jason Gunthorpece4b46572022-07-19 21:02:48 -030071 * @dma_unmap: Called when userspace unmaps IOVA from the container
72 * this device is attached to.
Jason Gunthorpe445ad492022-02-24 16:20:17 +020073 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
Alex Williamsoncba33452012-07-31 08:16:22 -060074 */
75struct vfio_device_ops {
76 char *name;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030077 int (*open_device)(struct vfio_device *vdev);
78 void (*close_device)(struct vfio_device *vdev);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060079 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060080 size_t count, loff_t *ppos);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060081 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060082 size_t count, loff_t *size);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060083 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
Alex Williamsoncba33452012-07-31 08:16:22 -060084 unsigned long arg);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060085 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
86 void (*request)(struct vfio_device *vdev, unsigned int count);
87 int (*match)(struct vfio_device *vdev, char *buf);
Jason Gunthorpece4b46572022-07-19 21:02:48 -030088 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
Jason Gunthorpe445ad492022-02-24 16:20:17 +020089 int (*device_feature)(struct vfio_device *device, u32 flags,
90 void __user *arg, size_t argsz);
Yishai Hadas6e97eba2022-06-28 18:59:10 +030091};
92
93/**
94 * @migration_set_state: Optional callback to change the migration state for
95 * devices that support migration. It's mandatory for
96 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
97 * The returned FD is used for data transfer according to the FSM
98 * definition. The driver is responsible to ensure that FD reaches end
99 * of stream or error whenever the migration FSM leaves a data transfer
100 * state or before close_device() returns.
101 * @migration_get_state: Optional callback to get the migration state for
102 * devices that support migration. It's mandatory for
103 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
104 */
105struct vfio_migration_ops {
Jason Gunthorpe115dcec2022-02-24 16:20:18 +0200106 struct file *(*migration_set_state)(
107 struct vfio_device *device,
108 enum vfio_device_mig_state new_state);
109 int (*migration_get_state)(struct vfio_device *device,
110 enum vfio_device_mig_state *curr_state);
Alex Williamsoncba33452012-07-31 08:16:22 -0600111};
112
Jason Gunthorpe445ad492022-02-24 16:20:17 +0200113/**
Yishai Hadas80c4b922022-09-08 21:34:43 +0300114 * @log_start: Optional callback to ask the device start DMA logging.
115 * @log_stop: Optional callback to ask the device stop DMA logging.
116 * @log_read_and_clear: Optional callback to ask the device read
117 * and clear the dirty DMAs in some given range.
118 *
119 * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set
120 * of features does not track logging state relative to the device,
121 * therefore the device implementation of vfio_log_ops must handle
122 * arbitrary user requests. This includes rejecting subsequent calls
123 * to log_start without an intervening log_stop, as well as graceful
124 * handling of log_stop and log_read_and_clear from invalid states.
125 */
126struct vfio_log_ops {
127 int (*log_start)(struct vfio_device *device,
128 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
129 int (*log_stop)(struct vfio_device *device);
130 int (*log_read_and_clear)(struct vfio_device *device,
131 unsigned long iova, unsigned long length,
132 struct iova_bitmap *dirty);
133};
134
135/**
Jason Gunthorpe445ad492022-02-24 16:20:17 +0200136 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
137 * @flags: Arg from the device_feature op
138 * @argsz: Arg from the device_feature op
139 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
140 * supports
141 * @minsz: Minimum data size the driver accepts
142 *
143 * For use in a driver's device_feature op. Checks that the inputs to the
144 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
145 * the driver should execute the get or set, otherwise the relevant
146 * value should be returned.
147 */
148static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
149 size_t minsz)
150{
151 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
152 ~supported_ops)
153 return -EINVAL;
154 if (flags & VFIO_DEVICE_FEATURE_PROBE)
155 return 0;
156 /* Without PROBE one of GET or SET must be requested */
157 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
158 return -EINVAL;
159 if (argsz < minsz)
160 return -EINVAL;
161 return 1;
162}
163
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600164void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
Jason Gunthorpe1e04ec12021-03-30 09:53:08 -0600165 const struct vfio_device_ops *ops);
Max Gurtovoyae03c372021-08-05 22:18:59 -0300166void vfio_uninit_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600167int vfio_register_group_dev(struct vfio_device *device);
Christoph Hellwigc68ea0d2021-09-24 17:56:57 +0200168int vfio_register_emulated_iommu_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600169void vfio_unregister_group_dev(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600170
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -0300171int vfio_assign_device_set(struct vfio_device *device, void *set_id);
172
Jason Gunthorpe115dcec2022-02-24 16:20:18 +0200173int vfio_mig_get_next_state(struct vfio_device *device,
174 enum vfio_device_mig_state cur_fsm,
175 enum vfio_device_mig_state new_fsm,
176 enum vfio_device_mig_state *next_fsm);
177
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600178/*
179 * External user API
180 */
Alex Williamsond1877e62022-06-08 12:55:13 -0600181struct iommu_group *vfio_file_iommu_group(struct file *file);
182bool vfio_file_enforced_coherent(struct file *file);
183void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
184bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600185
Kirti Wankhede21690372016-11-17 02:16:17 +0530186#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
187
Nicolin Chen44abdd12022-07-22 19:02:51 -0700188int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
Nicolin Chen34a255e62022-07-22 19:02:56 -0700189 int npage, int prot, struct page **pages);
Nicolin Chen44abdd12022-07-22 19:02:51 -0700190void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
Nicolin Chen8561aa42022-07-22 19:02:54 -0700191int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
Alex Williamsond1877e62022-06-08 12:55:13 -0600192 void *data, size_t len, bool write);
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600193
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700194/*
195 * Sub-module helpers
196 */
197struct vfio_info_cap {
198 struct vfio_info_cap_header *buf;
199 size_t size;
200};
Alex Williamsond1877e62022-06-08 12:55:13 -0600201struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
202 size_t size, u16 id,
203 u16 version);
204void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700205
Alex Williamsond1877e62022-06-08 12:55:13 -0600206int vfio_info_add_capability(struct vfio_info_cap *caps,
207 struct vfio_info_cap_header *cap, size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530208
Alex Williamsond1877e62022-06-08 12:55:13 -0600209int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
210 int num_irqs, int max_irq_type,
211 size_t *data_size);
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530212
Gavin Shan92d18a62014-08-08 10:36:20 -0600213struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300214#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alex Williamsond1877e62022-06-08 12:55:13 -0600215void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
216void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
217long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, unsigned int cmd,
218 unsigned long arg);
Gavin Shan1b69be52014-06-10 11:41:57 +1000219#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600220static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000221{
Gavin Shan1b69be52014-06-10 11:41:57 +1000222}
223
224static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
225{
226}
227
228static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
229 unsigned int cmd,
230 unsigned long arg)
231{
232 return -ENOTTY;
233}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300234#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600235
236/*
237 * IRQfd - generic
238 */
239struct virqfd {
240 void *opaque;
241 struct eventfd_ctx *eventfd;
242 int (*handler)(void *, void *);
243 void (*thread)(void *, void *);
244 void *data;
245 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200246 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600247 poll_table pt;
248 struct work_struct shutdown;
249 struct virqfd **pvirqfd;
250};
251
Alex Williamsond1877e62022-06-08 12:55:13 -0600252int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
253 void (*thread)(void *, void *), void *data,
254 struct virqfd **pvirqfd, int fd);
255void vfio_virqfd_disable(struct virqfd **pvirqfd);
Antonios Motakis7e992d62015-03-16 14:08:54 -0600256
Alex Williamsoncba33452012-07-31 08:16:22 -0600257#endif /* VFIO_H */