blob: 7cc374da4839da40ea25c1da6368f15683abbb6b [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 Gunthorpe2fd585f2021-08-05 22:19:00 -030018/*
19 * VFIO devices can be placed in a set, this allows all devices to share this
20 * structure and the VFIO core will provide a lock that is held around
21 * open_device()/close_device() for all devices in the set.
22 */
23struct vfio_device_set {
24 void *set_id;
25 struct mutex lock;
26 struct list_head device_list;
27 unsigned int device_count;
28};
29
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060030struct vfio_device {
31 struct device *dev;
32 const struct vfio_device_ops *ops;
33 struct vfio_group *group;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030034 struct vfio_device_set *dev_set;
35 struct list_head dev_set_list;
Jason Gunthorpe8cb3d832022-02-24 16:20:19 +020036 unsigned int migration_flags;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060037
38 /* Members below here are private, not for driver use */
39 refcount_t refcount;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030040 unsigned int open_count;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060041 struct completion comp;
42 struct list_head group_next;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060043};
44
Alex Williamsoncba33452012-07-31 08:16:22 -060045/**
46 * struct vfio_device_ops - VFIO bus driver device callbacks
47 *
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030048 * @open_device: Called when the first file descriptor is opened for this device
49 * @close_device: Opposite of open_device
Alex Williamsoncba33452012-07-31 08:16:22 -060050 * @read: Perform read(2) on device file descriptor
51 * @write: Perform write(2) on device file descriptor
52 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
53 * operations documented below
54 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070055 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060056 * @match: Optional device name match callback (return: 0 for no-match, >0 for
57 * match, -errno for abort (ex. match with insufficient or incorrect
58 * additional args)
Jason Gunthorpe445ad492022-02-24 16:20:17 +020059 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
Jason Gunthorpe115dcec2022-02-24 16:20:18 +020060 * @migration_set_state: Optional callback to change the migration state for
61 * devices that support migration. It's mandatory for
62 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
63 * The returned FD is used for data transfer according to the FSM
64 * definition. The driver is responsible to ensure that FD reaches end
65 * of stream or error whenever the migration FSM leaves a data transfer
66 * state or before close_device() returns.
67 * @migration_get_state: Optional callback to get the migration state for
68 * devices that support migration. It's mandatory for
69 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
Alex Williamsoncba33452012-07-31 08:16:22 -060070 */
71struct vfio_device_ops {
72 char *name;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030073 int (*open_device)(struct vfio_device *vdev);
74 void (*close_device)(struct vfio_device *vdev);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060075 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060076 size_t count, loff_t *ppos);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060077 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060078 size_t count, loff_t *size);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060079 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
Alex Williamsoncba33452012-07-31 08:16:22 -060080 unsigned long arg);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060081 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
82 void (*request)(struct vfio_device *vdev, unsigned int count);
83 int (*match)(struct vfio_device *vdev, char *buf);
Jason Gunthorpe445ad492022-02-24 16:20:17 +020084 int (*device_feature)(struct vfio_device *device, u32 flags,
85 void __user *arg, size_t argsz);
Jason Gunthorpe115dcec2022-02-24 16:20:18 +020086 struct file *(*migration_set_state)(
87 struct vfio_device *device,
88 enum vfio_device_mig_state new_state);
89 int (*migration_get_state)(struct vfio_device *device,
90 enum vfio_device_mig_state *curr_state);
Alex Williamsoncba33452012-07-31 08:16:22 -060091};
92
Jason Gunthorpe445ad492022-02-24 16:20:17 +020093/**
94 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
95 * @flags: Arg from the device_feature op
96 * @argsz: Arg from the device_feature op
97 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
98 * supports
99 * @minsz: Minimum data size the driver accepts
100 *
101 * For use in a driver's device_feature op. Checks that the inputs to the
102 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
103 * the driver should execute the get or set, otherwise the relevant
104 * value should be returned.
105 */
106static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
107 size_t minsz)
108{
109 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
110 ~supported_ops)
111 return -EINVAL;
112 if (flags & VFIO_DEVICE_FEATURE_PROBE)
113 return 0;
114 /* Without PROBE one of GET or SET must be requested */
115 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
116 return -EINVAL;
117 if (argsz < minsz)
118 return -EINVAL;
119 return 1;
120}
121
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600122void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
Jason Gunthorpe1e04ec12021-03-30 09:53:08 -0600123 const struct vfio_device_ops *ops);
Max Gurtovoyae03c372021-08-05 22:18:59 -0300124void vfio_uninit_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600125int vfio_register_group_dev(struct vfio_device *device);
Christoph Hellwigc68ea0d2021-09-24 17:56:57 +0200126int vfio_register_emulated_iommu_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600127void vfio_unregister_group_dev(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600128
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -0300129int vfio_assign_device_set(struct vfio_device *device, void *set_id);
130
Jason Gunthorpe115dcec2022-02-24 16:20:18 +0200131int vfio_mig_get_next_state(struct vfio_device *device,
132 enum vfio_device_mig_state cur_fsm,
133 enum vfio_device_mig_state new_fsm,
134 enum vfio_device_mig_state *next_fsm);
135
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600136/*
137 * External user API
138 */
139extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
140extern void vfio_group_put_external_user(struct vfio_group *group);
Jason Gunthorpe50d63b52022-05-04 16:14:41 -0300141extern struct iommu_group *vfio_file_iommu_group(struct file *file);
Jason Gunthorpea905ad02022-05-04 16:14:43 -0300142extern bool vfio_file_enforced_coherent(struct file *file);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600143
Kirti Wankhede21690372016-11-17 02:16:17 +0530144#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
145
Jason Gunthorpe8e432bb2022-05-11 13:12:59 -0600146extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +0530147 int npage, int prot, unsigned long *phys_pfn);
Jason Gunthorpe8e432bb2022-05-11 13:12:59 -0600148extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +0530149 int npage);
Jason Gunthorpec6250ff2022-05-11 13:12:59 -0600150extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600151 void *data, size_t len, bool write);
152
Jike Song22195cb2016-12-01 13:20:05 +0800153/* each type has independent events */
154enum vfio_notify_type {
155 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800156 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800157};
158
159/* events for VFIO_IOMMU_NOTIFY */
160#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530161
Jike Songccd46db2016-12-01 13:20:06 +0800162/* events for VFIO_GROUP_NOTIFY */
163#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
164
Jason Gunthorpe09ea48e2022-05-11 13:12:58 -0600165extern int vfio_register_notifier(struct vfio_device *device,
Jike Song22195cb2016-12-01 13:20:05 +0800166 enum vfio_notify_type type,
167 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530168 struct notifier_block *nb);
Jason Gunthorpe09ea48e2022-05-11 13:12:58 -0600169extern int vfio_unregister_notifier(struct vfio_device *device,
Jike Song22195cb2016-12-01 13:20:05 +0800170 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530171 struct notifier_block *nb);
172
Jike Songccd46db2016-12-01 13:20:06 +0800173struct kvm;
174extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
175
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700176/*
177 * Sub-module helpers
178 */
179struct vfio_info_cap {
180 struct vfio_info_cap_header *buf;
181 size_t size;
182};
183extern struct vfio_info_cap_header *vfio_info_cap_add(
184 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
185extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
186
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530187extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700188 struct vfio_info_cap_header *cap,
189 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530190
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530191extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
192 int num_irqs, int max_irq_type,
193 size_t *data_size);
194
Gavin Shan92d18a62014-08-08 10:36:20 -0600195struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300196#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600197extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000198extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
199extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
200 unsigned int cmd,
201 unsigned long arg);
202#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600203static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000204{
Gavin Shan1b69be52014-06-10 11:41:57 +1000205}
206
207static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
208{
209}
210
211static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
212 unsigned int cmd,
213 unsigned long arg)
214{
215 return -ENOTTY;
216}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300217#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600218
219/*
220 * IRQfd - generic
221 */
222struct virqfd {
223 void *opaque;
224 struct eventfd_ctx *eventfd;
225 int (*handler)(void *, void *);
226 void (*thread)(void *, void *);
227 void *data;
228 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200229 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600230 poll_table pt;
231 struct work_struct shutdown;
232 struct virqfd **pvirqfd;
233};
234
Antonios Motakis7e992d62015-03-16 14:08:54 -0600235extern int vfio_virqfd_enable(void *opaque,
236 int (*handler)(void *, void *),
237 void (*thread)(void *, void *),
238 void *data, struct virqfd **pvirqfd, int fd);
239extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
240
Alex Williamsoncba33452012-07-31 08:16:22 -0600241#endif /* VFIO_H */