blob: 9e3b6abcf890673f1320249d1771377ec0db8044 [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 Gunthorpeba70a892022-05-04 16:14:44 -030018struct kvm;
19
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030020/*
21 * VFIO devices can be placed in a set, this allows all devices to share this
22 * structure and the VFIO core will provide a lock that is held around
23 * open_device()/close_device() for all devices in the set.
24 */
25struct vfio_device_set {
26 void *set_id;
27 struct mutex lock;
28 struct list_head device_list;
29 unsigned int device_count;
30};
31
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060032struct vfio_device {
33 struct device *dev;
34 const struct vfio_device_ops *ops;
Yishai Hadas6e97eba2022-06-28 18:59:10 +030035 /*
36 * mig_ops is a static property of the vfio_device which must be set
37 * prior to registering the vfio_device.
38 */
39 const struct vfio_migration_ops *mig_ops;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060040 struct vfio_group *group;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030041 struct vfio_device_set *dev_set;
42 struct list_head dev_set_list;
Jason Gunthorpe8cb3d832022-02-24 16:20:19 +020043 unsigned int migration_flags;
Matthew Rosato421cfe62022-05-19 14:33:11 -040044 /* Driver must reference the kvm during open_device or never touch it */
45 struct kvm *kvm;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060046
47 /* Members below here are private, not for driver use */
48 refcount_t refcount;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030049 unsigned int open_count;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060050 struct completion comp;
51 struct list_head group_next;
Jason Gunthorpe8cfc5b62022-07-19 21:02:49 -030052 struct list_head iommu_entry;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060053};
54
Alex Williamsoncba33452012-07-31 08:16:22 -060055/**
56 * struct vfio_device_ops - VFIO bus driver device callbacks
57 *
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030058 * @open_device: Called when the first file descriptor is opened for this device
59 * @close_device: Opposite of open_device
Alex Williamsoncba33452012-07-31 08:16:22 -060060 * @read: Perform read(2) on device file descriptor
61 * @write: Perform write(2) on device file descriptor
62 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
63 * operations documented below
64 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070065 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060066 * @match: Optional device name match callback (return: 0 for no-match, >0 for
67 * match, -errno for abort (ex. match with insufficient or incorrect
68 * additional args)
Jason Gunthorpece4b46572022-07-19 21:02:48 -030069 * @dma_unmap: Called when userspace unmaps IOVA from the container
70 * this device is attached to.
Jason Gunthorpe445ad492022-02-24 16:20:17 +020071 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
Alex Williamsoncba33452012-07-31 08:16:22 -060072 */
73struct vfio_device_ops {
74 char *name;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030075 int (*open_device)(struct vfio_device *vdev);
76 void (*close_device)(struct vfio_device *vdev);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060077 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060078 size_t count, loff_t *ppos);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060079 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060080 size_t count, loff_t *size);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060081 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
Alex Williamsoncba33452012-07-31 08:16:22 -060082 unsigned long arg);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060083 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
84 void (*request)(struct vfio_device *vdev, unsigned int count);
85 int (*match)(struct vfio_device *vdev, char *buf);
Jason Gunthorpece4b46572022-07-19 21:02:48 -030086 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
Jason Gunthorpe445ad492022-02-24 16:20:17 +020087 int (*device_feature)(struct vfio_device *device, u32 flags,
88 void __user *arg, size_t argsz);
Yishai Hadas6e97eba2022-06-28 18:59:10 +030089};
90
91/**
92 * @migration_set_state: Optional callback to change the migration state for
93 * devices that support migration. It's mandatory for
94 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
95 * The returned FD is used for data transfer according to the FSM
96 * definition. The driver is responsible to ensure that FD reaches end
97 * of stream or error whenever the migration FSM leaves a data transfer
98 * state or before close_device() returns.
99 * @migration_get_state: Optional callback to get the migration state for
100 * devices that support migration. It's mandatory for
101 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
102 */
103struct vfio_migration_ops {
Jason Gunthorpe115dcec2022-02-24 16:20:18 +0200104 struct file *(*migration_set_state)(
105 struct vfio_device *device,
106 enum vfio_device_mig_state new_state);
107 int (*migration_get_state)(struct vfio_device *device,
108 enum vfio_device_mig_state *curr_state);
Alex Williamsoncba33452012-07-31 08:16:22 -0600109};
110
Jason Gunthorpe445ad492022-02-24 16:20:17 +0200111/**
112 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
113 * @flags: Arg from the device_feature op
114 * @argsz: Arg from the device_feature op
115 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
116 * supports
117 * @minsz: Minimum data size the driver accepts
118 *
119 * For use in a driver's device_feature op. Checks that the inputs to the
120 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
121 * the driver should execute the get or set, otherwise the relevant
122 * value should be returned.
123 */
124static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
125 size_t minsz)
126{
127 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
128 ~supported_ops)
129 return -EINVAL;
130 if (flags & VFIO_DEVICE_FEATURE_PROBE)
131 return 0;
132 /* Without PROBE one of GET or SET must be requested */
133 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
134 return -EINVAL;
135 if (argsz < minsz)
136 return -EINVAL;
137 return 1;
138}
139
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600140void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
Jason Gunthorpe1e04ec12021-03-30 09:53:08 -0600141 const struct vfio_device_ops *ops);
Max Gurtovoyae03c372021-08-05 22:18:59 -0300142void vfio_uninit_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600143int vfio_register_group_dev(struct vfio_device *device);
Christoph Hellwigc68ea0d2021-09-24 17:56:57 +0200144int vfio_register_emulated_iommu_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600145void vfio_unregister_group_dev(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600146
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -0300147int vfio_assign_device_set(struct vfio_device *device, void *set_id);
148
Jason Gunthorpe115dcec2022-02-24 16:20:18 +0200149int vfio_mig_get_next_state(struct vfio_device *device,
150 enum vfio_device_mig_state cur_fsm,
151 enum vfio_device_mig_state new_fsm,
152 enum vfio_device_mig_state *next_fsm);
153
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600154/*
155 * External user API
156 */
Alex Williamsond1877e62022-06-08 12:55:13 -0600157struct iommu_group *vfio_file_iommu_group(struct file *file);
158bool vfio_file_enforced_coherent(struct file *file);
159void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
160bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600161
Kirti Wankhede21690372016-11-17 02:16:17 +0530162#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
163
Nicolin Chen44abdd12022-07-22 19:02:51 -0700164int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
Alex Williamsond1877e62022-06-08 12:55:13 -0600165 int npage, int prot, unsigned long *phys_pfn);
Nicolin Chen44abdd12022-07-22 19:02:51 -0700166void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
Alex Williamsond1877e62022-06-08 12:55:13 -0600167int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
168 void *data, size_t len, bool write);
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600169
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700170/*
171 * Sub-module helpers
172 */
173struct vfio_info_cap {
174 struct vfio_info_cap_header *buf;
175 size_t size;
176};
Alex Williamsond1877e62022-06-08 12:55:13 -0600177struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
178 size_t size, u16 id,
179 u16 version);
180void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700181
Alex Williamsond1877e62022-06-08 12:55:13 -0600182int vfio_info_add_capability(struct vfio_info_cap *caps,
183 struct vfio_info_cap_header *cap, size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530184
Alex Williamsond1877e62022-06-08 12:55:13 -0600185int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
186 int num_irqs, int max_irq_type,
187 size_t *data_size);
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530188
Gavin Shan92d18a62014-08-08 10:36:20 -0600189struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300190#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alex Williamsond1877e62022-06-08 12:55:13 -0600191void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
192void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
193long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, unsigned int cmd,
194 unsigned long arg);
Gavin Shan1b69be52014-06-10 11:41:57 +1000195#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600196static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000197{
Gavin Shan1b69be52014-06-10 11:41:57 +1000198}
199
200static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
201{
202}
203
204static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
205 unsigned int cmd,
206 unsigned long arg)
207{
208 return -ENOTTY;
209}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300210#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600211
212/*
213 * IRQfd - generic
214 */
215struct virqfd {
216 void *opaque;
217 struct eventfd_ctx *eventfd;
218 int (*handler)(void *, void *);
219 void (*thread)(void *, void *);
220 void *data;
221 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200222 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600223 poll_table pt;
224 struct work_struct shutdown;
225 struct virqfd **pvirqfd;
226};
227
Alex Williamsond1877e62022-06-08 12:55:13 -0600228int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
229 void (*thread)(void *, void *), void *data,
230 struct virqfd **pvirqfd, int fd);
231void vfio_virqfd_disable(struct virqfd **pvirqfd);
Antonios Motakis7e992d62015-03-16 14:08:54 -0600232
Alex Williamsoncba33452012-07-31 08:16:22 -0600233#endif /* VFIO_H */