Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 2 | /* |
| 3 | * VFIO API definition |
| 4 | * |
| 5 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. |
| 6 | * Author: Alex Williamson <[email protected]> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 7 | */ |
| 8 | #ifndef VFIO_H |
| 9 | #define VFIO_H |
| 10 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 11 | |
| 12 | #include <linux/iommu.h> |
| 13 | #include <linux/mm.h> |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 14 | #include <linux/workqueue.h> |
| 15 | #include <linux/poll.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 16 | #include <uapi/linux/vfio.h> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 17 | |
Jason Gunthorpe | ba70a89 | 2022-05-04 16:14:44 -0300 | [diff] [blame^] | 18 | struct kvm; |
| 19 | |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 20 | /* |
| 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 | */ |
| 25 | struct 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 Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 32 | struct vfio_device { |
| 33 | struct device *dev; |
| 34 | const struct vfio_device_ops *ops; |
| 35 | struct vfio_group *group; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 36 | struct vfio_device_set *dev_set; |
| 37 | struct list_head dev_set_list; |
Jason Gunthorpe | 8cb3d83 | 2022-02-24 16:20:19 +0200 | [diff] [blame] | 38 | unsigned int migration_flags; |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 39 | |
| 40 | /* Members below here are private, not for driver use */ |
| 41 | refcount_t refcount; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 42 | unsigned int open_count; |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 43 | struct completion comp; |
| 44 | struct list_head group_next; |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 47 | /** |
| 48 | * struct vfio_device_ops - VFIO bus driver device callbacks |
| 49 | * |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 50 | * @open_device: Called when the first file descriptor is opened for this device |
| 51 | * @close_device: Opposite of open_device |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 52 | * @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 Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 57 | * @request: Request for the bus driver to release the device |
Alex Williamson | 5f3874c | 2020-03-24 09:28:25 -0600 | [diff] [blame] | 58 | * @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 Gunthorpe | 445ad49 | 2022-02-24 16:20:17 +0200 | [diff] [blame] | 61 | * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl |
Jason Gunthorpe | 115dcec | 2022-02-24 16:20:18 +0200 | [diff] [blame] | 62 | * @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 Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 72 | */ |
| 73 | struct vfio_device_ops { |
| 74 | char *name; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 75 | int (*open_device)(struct vfio_device *vdev); |
| 76 | void (*close_device)(struct vfio_device *vdev); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 77 | ssize_t (*read)(struct vfio_device *vdev, char __user *buf, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 78 | size_t count, loff_t *ppos); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 79 | ssize_t (*write)(struct vfio_device *vdev, const char __user *buf, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 80 | size_t count, loff_t *size); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 81 | long (*ioctl)(struct vfio_device *vdev, unsigned int cmd, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 82 | unsigned long arg); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 83 | 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 Gunthorpe | 445ad49 | 2022-02-24 16:20:17 +0200 | [diff] [blame] | 86 | int (*device_feature)(struct vfio_device *device, u32 flags, |
| 87 | void __user *arg, size_t argsz); |
Jason Gunthorpe | 115dcec | 2022-02-24 16:20:18 +0200 | [diff] [blame] | 88 | 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 Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 93 | }; |
| 94 | |
Jason Gunthorpe | 445ad49 | 2022-02-24 16:20:17 +0200 | [diff] [blame] | 95 | /** |
| 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 | */ |
| 108 | static 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 Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 124 | void vfio_init_group_dev(struct vfio_device *device, struct device *dev, |
Jason Gunthorpe | 1e04ec1 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 125 | const struct vfio_device_ops *ops); |
Max Gurtovoy | ae03c37 | 2021-08-05 22:18:59 -0300 | [diff] [blame] | 126 | void vfio_uninit_group_dev(struct vfio_device *device); |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 127 | int vfio_register_group_dev(struct vfio_device *device); |
Christoph Hellwig | c68ea0d | 2021-09-24 17:56:57 +0200 | [diff] [blame] | 128 | int vfio_register_emulated_iommu_dev(struct vfio_device *device); |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 129 | void vfio_unregister_group_dev(struct vfio_device *device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 130 | |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 131 | int vfio_assign_device_set(struct vfio_device *device, void *set_id); |
| 132 | |
Jason Gunthorpe | 115dcec | 2022-02-24 16:20:18 +0200 | [diff] [blame] | 133 | int 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 Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 138 | /* |
| 139 | * External user API |
| 140 | */ |
| 141 | extern struct vfio_group *vfio_group_get_external_user(struct file *filep); |
| 142 | extern void vfio_group_put_external_user(struct vfio_group *group); |
Jason Gunthorpe | 50d63b5 | 2022-05-04 16:14:41 -0300 | [diff] [blame] | 143 | extern struct iommu_group *vfio_file_iommu_group(struct file *file); |
Jason Gunthorpe | a905ad0 | 2022-05-04 16:14:43 -0300 | [diff] [blame] | 144 | extern bool vfio_file_enforced_coherent(struct file *file); |
Jason Gunthorpe | ba70a89 | 2022-05-04 16:14:44 -0300 | [diff] [blame^] | 145 | extern void vfio_file_set_kvm(struct file *file, struct kvm *kvm); |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 146 | |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 147 | #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) |
| 148 | |
Jason Gunthorpe | 8e432bb | 2022-05-11 13:12:59 -0600 | [diff] [blame] | 149 | extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn, |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 150 | int npage, int prot, unsigned long *phys_pfn); |
Jason Gunthorpe | 8e432bb | 2022-05-11 13:12:59 -0600 | [diff] [blame] | 151 | extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn, |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 152 | int npage); |
Jason Gunthorpe | c6250ff | 2022-05-11 13:12:59 -0600 | [diff] [blame] | 153 | extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, |
Yan Zhao | 8d46c0c | 2020-03-24 09:27:57 -0600 | [diff] [blame] | 154 | void *data, size_t len, bool write); |
| 155 | |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 156 | /* each type has independent events */ |
| 157 | enum vfio_notify_type { |
| 158 | VFIO_IOMMU_NOTIFY = 0, |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 159 | VFIO_GROUP_NOTIFY = 1, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /* events for VFIO_IOMMU_NOTIFY */ |
| 163 | #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0) |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 164 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 165 | /* events for VFIO_GROUP_NOTIFY */ |
| 166 | #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0) |
| 167 | |
Jason Gunthorpe | 09ea48e | 2022-05-11 13:12:58 -0600 | [diff] [blame] | 168 | extern int vfio_register_notifier(struct vfio_device *device, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 169 | enum vfio_notify_type type, |
| 170 | unsigned long *required_events, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 171 | struct notifier_block *nb); |
Jason Gunthorpe | 09ea48e | 2022-05-11 13:12:58 -0600 | [diff] [blame] | 172 | extern int vfio_unregister_notifier(struct vfio_device *device, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 173 | enum vfio_notify_type type, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 174 | struct notifier_block *nb); |
| 175 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 176 | |
Alex Williamson | d7a8d5e | 2016-02-22 16:02:33 -0700 | [diff] [blame] | 177 | /* |
| 178 | * Sub-module helpers |
| 179 | */ |
| 180 | struct vfio_info_cap { |
| 181 | struct vfio_info_cap_header *buf; |
| 182 | size_t size; |
| 183 | }; |
| 184 | extern struct vfio_info_cap_header *vfio_info_cap_add( |
| 185 | struct vfio_info_cap *caps, size_t size, u16 id, u16 version); |
| 186 | extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); |
| 187 | |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 188 | extern int vfio_info_add_capability(struct vfio_info_cap *caps, |
Alex Williamson | dda01f7 | 2017-12-12 12:59:39 -0700 | [diff] [blame] | 189 | struct vfio_info_cap_header *cap, |
| 190 | size_t size); |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 191 | |
Kirti Wankhede | c747f08a | 2016-11-17 02:16:27 +0530 | [diff] [blame] | 192 | extern 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 Shan | 92d18a6 | 2014-08-08 10:36:20 -0600 | [diff] [blame] | 196 | struct pci_dev; |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 197 | #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH) |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 198 | extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 199 | extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev); |
| 200 | extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 201 | unsigned int cmd, |
| 202 | unsigned long arg); |
| 203 | #else |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 204 | static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev) |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 205 | { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | static 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 Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 218 | #endif /* CONFIG_VFIO_SPAPR_EEH */ |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * IRQfd - generic |
| 222 | */ |
| 223 | struct 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 Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 230 | wait_queue_entry_t wait; |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 231 | poll_table pt; |
| 232 | struct work_struct shutdown; |
| 233 | struct virqfd **pvirqfd; |
| 234 | }; |
| 235 | |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 236 | extern int vfio_virqfd_enable(void *opaque, |
| 237 | int (*handler)(void *, void *), |
| 238 | void (*thread)(void *, void *), |
| 239 | void *data, struct virqfd **pvirqfd, int fd); |
| 240 | extern void vfio_virqfd_disable(struct virqfd **pvirqfd); |
| 241 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 242 | #endif /* VFIO_H */ |