blob: b298a26c4f073c59b5d1119373c2912b938b4977 [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;
35 struct vfio_group *group;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030036 struct vfio_device_set *dev_set;
37 struct list_head dev_set_list;
Jason Gunthorpe8cb3d832022-02-24 16:20:19 +020038 unsigned int migration_flags;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060039
40 /* Members below here are private, not for driver use */
41 refcount_t refcount;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030042 unsigned int open_count;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060043 struct completion comp;
44 struct list_head group_next;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060045};
46
Alex Williamsoncba33452012-07-31 08:16:22 -060047/**
48 * struct vfio_device_ops - VFIO bus driver device callbacks
49 *
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030050 * @open_device: Called when the first file descriptor is opened for this device
51 * @close_device: Opposite of open_device
Alex Williamsoncba33452012-07-31 08:16:22 -060052 * @read: Perform read(2) on device file descriptor
53 * @write: Perform write(2) on device file descriptor
54 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
55 * operations documented below
56 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070057 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060058 * @match: Optional device name match callback (return: 0 for no-match, >0 for
59 * match, -errno for abort (ex. match with insufficient or incorrect
60 * additional args)
Jason Gunthorpe445ad492022-02-24 16:20:17 +020061 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
Jason Gunthorpe115dcec2022-02-24 16:20:18 +020062 * @migration_set_state: Optional callback to change the migration state for
63 * devices that support migration. It's mandatory for
64 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
65 * The returned FD is used for data transfer according to the FSM
66 * definition. The driver is responsible to ensure that FD reaches end
67 * of stream or error whenever the migration FSM leaves a data transfer
68 * state or before close_device() returns.
69 * @migration_get_state: Optional callback to get the migration state for
70 * devices that support migration. It's mandatory for
71 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
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 Gunthorpe445ad492022-02-24 16:20:17 +020086 int (*device_feature)(struct vfio_device *device, u32 flags,
87 void __user *arg, size_t argsz);
Jason Gunthorpe115dcec2022-02-24 16:20:18 +020088 struct file *(*migration_set_state)(
89 struct vfio_device *device,
90 enum vfio_device_mig_state new_state);
91 int (*migration_get_state)(struct vfio_device *device,
92 enum vfio_device_mig_state *curr_state);
Alex Williamsoncba33452012-07-31 08:16:22 -060093};
94
Jason Gunthorpe445ad492022-02-24 16:20:17 +020095/**
96 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
97 * @flags: Arg from the device_feature op
98 * @argsz: Arg from the device_feature op
99 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
100 * supports
101 * @minsz: Minimum data size the driver accepts
102 *
103 * For use in a driver's device_feature op. Checks that the inputs to the
104 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
105 * the driver should execute the get or set, otherwise the relevant
106 * value should be returned.
107 */
108static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
109 size_t minsz)
110{
111 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
112 ~supported_ops)
113 return -EINVAL;
114 if (flags & VFIO_DEVICE_FEATURE_PROBE)
115 return 0;
116 /* Without PROBE one of GET or SET must be requested */
117 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
118 return -EINVAL;
119 if (argsz < minsz)
120 return -EINVAL;
121 return 1;
122}
123
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600124void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
Jason Gunthorpe1e04ec12021-03-30 09:53:08 -0600125 const struct vfio_device_ops *ops);
Max Gurtovoyae03c372021-08-05 22:18:59 -0300126void vfio_uninit_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600127int vfio_register_group_dev(struct vfio_device *device);
Christoph Hellwigc68ea0d2021-09-24 17:56:57 +0200128int vfio_register_emulated_iommu_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600129void vfio_unregister_group_dev(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600130
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -0300131int vfio_assign_device_set(struct vfio_device *device, void *set_id);
132
Jason Gunthorpe115dcec2022-02-24 16:20:18 +0200133int vfio_mig_get_next_state(struct vfio_device *device,
134 enum vfio_device_mig_state cur_fsm,
135 enum vfio_device_mig_state new_fsm,
136 enum vfio_device_mig_state *next_fsm);
137
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600138/*
139 * External user API
140 */
141extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
142extern void vfio_group_put_external_user(struct vfio_group *group);
Jason Gunthorpe50d63b52022-05-04 16:14:41 -0300143extern struct iommu_group *vfio_file_iommu_group(struct file *file);
Jason Gunthorpea905ad02022-05-04 16:14:43 -0300144extern bool vfio_file_enforced_coherent(struct file *file);
Jason Gunthorpeba70a892022-05-04 16:14:44 -0300145extern void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600146
Kirti Wankhede21690372016-11-17 02:16:17 +0530147#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
148
Jason Gunthorpe8e432bb2022-05-11 13:12:59 -0600149extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +0530150 int npage, int prot, unsigned long *phys_pfn);
Jason Gunthorpe8e432bb2022-05-11 13:12:59 -0600151extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +0530152 int npage);
Jason Gunthorpec6250ff2022-05-11 13:12:59 -0600153extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600154 void *data, size_t len, bool write);
155
Jike Song22195cb2016-12-01 13:20:05 +0800156/* each type has independent events */
157enum vfio_notify_type {
158 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800159 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800160};
161
162/* events for VFIO_IOMMU_NOTIFY */
163#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530164
Jike Songccd46db2016-12-01 13:20:06 +0800165/* events for VFIO_GROUP_NOTIFY */
166#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
167
Jason Gunthorpe09ea48e2022-05-11 13:12:58 -0600168extern int vfio_register_notifier(struct vfio_device *device,
Jike Song22195cb2016-12-01 13:20:05 +0800169 enum vfio_notify_type type,
170 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530171 struct notifier_block *nb);
Jason Gunthorpe09ea48e2022-05-11 13:12:58 -0600172extern int vfio_unregister_notifier(struct vfio_device *device,
Jike Song22195cb2016-12-01 13:20:05 +0800173 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530174 struct notifier_block *nb);
175
Jike Songccd46db2016-12-01 13:20:06 +0800176
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700177/*
178 * Sub-module helpers
179 */
180struct vfio_info_cap {
181 struct vfio_info_cap_header *buf;
182 size_t size;
183};
184extern struct vfio_info_cap_header *vfio_info_cap_add(
185 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
186extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
187
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530188extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700189 struct vfio_info_cap_header *cap,
190 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530191
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530192extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
193 int num_irqs, int max_irq_type,
194 size_t *data_size);
195
Gavin Shan92d18a62014-08-08 10:36:20 -0600196struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300197#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600198extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000199extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
200extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
201 unsigned int cmd,
202 unsigned long arg);
203#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600204static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000205{
Gavin Shan1b69be52014-06-10 11:41:57 +1000206}
207
208static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
209{
210}
211
212static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
213 unsigned int cmd,
214 unsigned long arg)
215{
216 return -ENOTTY;
217}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300218#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600219
220/*
221 * IRQfd - generic
222 */
223struct virqfd {
224 void *opaque;
225 struct eventfd_ctx *eventfd;
226 int (*handler)(void *, void *);
227 void (*thread)(void *, void *);
228 void *data;
229 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200230 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600231 poll_table pt;
232 struct work_struct shutdown;
233 struct virqfd **pvirqfd;
234};
235
Antonios Motakis7e992d62015-03-16 14:08:54 -0600236extern int vfio_virqfd_enable(void *opaque,
237 int (*handler)(void *, void *),
238 void (*thread)(void *, void *),
239 void *data, struct virqfd **pvirqfd, int fd);
240extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
241
Alex Williamsoncba33452012-07-31 08:16:22 -0600242#endif /* VFIO_H */