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 | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 18 | /* |
| 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 | */ |
| 23 | struct 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 Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 30 | struct vfio_device { |
| 31 | struct device *dev; |
| 32 | const struct vfio_device_ops *ops; |
| 33 | struct vfio_group *group; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 34 | struct vfio_device_set *dev_set; |
| 35 | struct list_head dev_set_list; |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 36 | |
| 37 | /* Members below here are private, not for driver use */ |
| 38 | refcount_t refcount; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 39 | unsigned int open_count; |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 40 | struct completion comp; |
| 41 | struct list_head group_next; |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 42 | }; |
| 43 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 44 | /** |
| 45 | * struct vfio_device_ops - VFIO bus driver device callbacks |
| 46 | * |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 47 | * @open_device: Called when the first file descriptor is opened for this device |
| 48 | * @close_device: Opposite of open_device |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 49 | * @read: Perform read(2) on device file descriptor |
| 50 | * @write: Perform write(2) on device file descriptor |
| 51 | * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_* |
| 52 | * operations documented below |
| 53 | * @mmap: Perform mmap(2) on a region of the device file descriptor |
Alex Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 54 | * @request: Request for the bus driver to release the device |
Alex Williamson | 5f3874c | 2020-03-24 09:28:25 -0600 | [diff] [blame] | 55 | * @match: Optional device name match callback (return: 0 for no-match, >0 for |
| 56 | * match, -errno for abort (ex. match with insufficient or incorrect |
| 57 | * additional args) |
Jason Gunthorpe | 445ad49 | 2022-02-24 16:20:17 +0200 | [diff] [blame^] | 58 | * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 59 | */ |
| 60 | struct vfio_device_ops { |
| 61 | char *name; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 62 | int (*open_device)(struct vfio_device *vdev); |
| 63 | void (*close_device)(struct vfio_device *vdev); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 64 | ssize_t (*read)(struct vfio_device *vdev, char __user *buf, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 65 | size_t count, loff_t *ppos); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 66 | ssize_t (*write)(struct vfio_device *vdev, const char __user *buf, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 67 | size_t count, loff_t *size); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 68 | long (*ioctl)(struct vfio_device *vdev, unsigned int cmd, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 69 | unsigned long arg); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 70 | int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma); |
| 71 | void (*request)(struct vfio_device *vdev, unsigned int count); |
| 72 | int (*match)(struct vfio_device *vdev, char *buf); |
Jason Gunthorpe | 445ad49 | 2022-02-24 16:20:17 +0200 | [diff] [blame^] | 73 | int (*device_feature)(struct vfio_device *device, u32 flags, |
| 74 | void __user *arg, size_t argsz); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 75 | }; |
| 76 | |
Jason Gunthorpe | 445ad49 | 2022-02-24 16:20:17 +0200 | [diff] [blame^] | 77 | /** |
| 78 | * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl |
| 79 | * @flags: Arg from the device_feature op |
| 80 | * @argsz: Arg from the device_feature op |
| 81 | * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver |
| 82 | * supports |
| 83 | * @minsz: Minimum data size the driver accepts |
| 84 | * |
| 85 | * For use in a driver's device_feature op. Checks that the inputs to the |
| 86 | * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if |
| 87 | * the driver should execute the get or set, otherwise the relevant |
| 88 | * value should be returned. |
| 89 | */ |
| 90 | static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops, |
| 91 | size_t minsz) |
| 92 | { |
| 93 | if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) & |
| 94 | ~supported_ops) |
| 95 | return -EINVAL; |
| 96 | if (flags & VFIO_DEVICE_FEATURE_PROBE) |
| 97 | return 0; |
| 98 | /* Without PROBE one of GET or SET must be requested */ |
| 99 | if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET))) |
| 100 | return -EINVAL; |
| 101 | if (argsz < minsz) |
| 102 | return -EINVAL; |
| 103 | return 1; |
| 104 | } |
| 105 | |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 106 | void vfio_init_group_dev(struct vfio_device *device, struct device *dev, |
Jason Gunthorpe | 1e04ec1 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 107 | const struct vfio_device_ops *ops); |
Max Gurtovoy | ae03c37 | 2021-08-05 22:18:59 -0300 | [diff] [blame] | 108 | void vfio_uninit_group_dev(struct vfio_device *device); |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 109 | int vfio_register_group_dev(struct vfio_device *device); |
Christoph Hellwig | c68ea0d | 2021-09-24 17:56:57 +0200 | [diff] [blame] | 110 | int vfio_register_emulated_iommu_dev(struct vfio_device *device); |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 111 | void vfio_unregister_group_dev(struct vfio_device *device); |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 112 | extern struct vfio_device *vfio_device_get_from_dev(struct device *dev); |
| 113 | extern void vfio_device_put(struct vfio_device *device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 114 | |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame] | 115 | int vfio_assign_device_set(struct vfio_device *device, void *set_id); |
| 116 | |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 117 | /* |
| 118 | * External user API |
| 119 | */ |
| 120 | extern struct vfio_group *vfio_group_get_external_user(struct file *filep); |
| 121 | extern void vfio_group_put_external_user(struct vfio_group *group); |
Yan Zhao | c0560f5 | 2020-03-24 09:27:56 -0600 | [diff] [blame] | 122 | extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device |
| 123 | *dev); |
Alex Williamson | 5d6dee8 | 2017-06-28 13:50:05 -0600 | [diff] [blame] | 124 | extern bool vfio_external_group_match_file(struct vfio_group *group, |
| 125 | struct file *filep); |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 126 | extern int vfio_external_user_iommu_id(struct vfio_group *group); |
Alex Williamson | 88d7ab8 | 2014-02-26 11:38:39 -0700 | [diff] [blame] | 127 | extern long vfio_external_check_extension(struct vfio_group *group, |
| 128 | unsigned long arg); |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 129 | |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 130 | #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) |
| 131 | |
| 132 | extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, |
| 133 | int npage, int prot, unsigned long *phys_pfn); |
| 134 | extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, |
| 135 | int npage); |
| 136 | |
Yan Zhao | 40280cf | 2020-03-24 09:27:57 -0600 | [diff] [blame] | 137 | extern int vfio_group_pin_pages(struct vfio_group *group, |
| 138 | unsigned long *user_iova_pfn, int npage, |
| 139 | int prot, unsigned long *phys_pfn); |
| 140 | extern int vfio_group_unpin_pages(struct vfio_group *group, |
| 141 | unsigned long *user_iova_pfn, int npage); |
| 142 | |
Yan Zhao | 8d46c0c | 2020-03-24 09:27:57 -0600 | [diff] [blame] | 143 | extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova, |
| 144 | void *data, size_t len, bool write); |
| 145 | |
Lu Baolu | bdfae1c | 2020-12-09 09:44:44 +0800 | [diff] [blame] | 146 | extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group); |
| 147 | |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 148 | /* each type has independent events */ |
| 149 | enum vfio_notify_type { |
| 150 | VFIO_IOMMU_NOTIFY = 0, |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 151 | VFIO_GROUP_NOTIFY = 1, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /* events for VFIO_IOMMU_NOTIFY */ |
| 155 | #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0) |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 156 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 157 | /* events for VFIO_GROUP_NOTIFY */ |
| 158 | #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0) |
| 159 | |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 160 | extern int vfio_register_notifier(struct device *dev, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 161 | enum vfio_notify_type type, |
| 162 | unsigned long *required_events, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 163 | struct notifier_block *nb); |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 164 | extern int vfio_unregister_notifier(struct device *dev, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 165 | enum vfio_notify_type type, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 166 | struct notifier_block *nb); |
| 167 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 168 | struct kvm; |
| 169 | extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); |
| 170 | |
Alex Williamson | d7a8d5e | 2016-02-22 16:02:33 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Sub-module helpers |
| 173 | */ |
| 174 | struct vfio_info_cap { |
| 175 | struct vfio_info_cap_header *buf; |
| 176 | size_t size; |
| 177 | }; |
| 178 | extern struct vfio_info_cap_header *vfio_info_cap_add( |
| 179 | struct vfio_info_cap *caps, size_t size, u16 id, u16 version); |
| 180 | extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); |
| 181 | |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 182 | extern int vfio_info_add_capability(struct vfio_info_cap *caps, |
Alex Williamson | dda01f7 | 2017-12-12 12:59:39 -0700 | [diff] [blame] | 183 | struct vfio_info_cap_header *cap, |
| 184 | size_t size); |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 185 | |
Kirti Wankhede | c747f08a | 2016-11-17 02:16:27 +0530 | [diff] [blame] | 186 | extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, |
| 187 | int num_irqs, int max_irq_type, |
| 188 | size_t *data_size); |
| 189 | |
Gavin Shan | 92d18a6 | 2014-08-08 10:36:20 -0600 | [diff] [blame] | 190 | struct pci_dev; |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 191 | #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH) |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 192 | extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 193 | extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev); |
| 194 | extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 195 | unsigned int cmd, |
| 196 | unsigned long arg); |
| 197 | #else |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 198 | static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev) |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 199 | { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 207 | unsigned int cmd, |
| 208 | unsigned long arg) |
| 209 | { |
| 210 | return -ENOTTY; |
| 211 | } |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 212 | #endif /* CONFIG_VFIO_SPAPR_EEH */ |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * IRQfd - generic |
| 216 | */ |
| 217 | struct virqfd { |
| 218 | void *opaque; |
| 219 | struct eventfd_ctx *eventfd; |
| 220 | int (*handler)(void *, void *); |
| 221 | void (*thread)(void *, void *); |
| 222 | void *data; |
| 223 | struct work_struct inject; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 224 | wait_queue_entry_t wait; |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 225 | poll_table pt; |
| 226 | struct work_struct shutdown; |
| 227 | struct virqfd **pvirqfd; |
| 228 | }; |
| 229 | |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 230 | extern int vfio_virqfd_enable(void *opaque, |
| 231 | int (*handler)(void *, void *), |
| 232 | void (*thread)(void *, void *), |
| 233 | void *data, struct virqfd **pvirqfd, int fd); |
| 234 | extern void vfio_virqfd_disable(struct virqfd **pvirqfd); |
| 235 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 236 | #endif /* VFIO_H */ |