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 | * @open: Called when userspace creates new file descriptor for device |
| 50 | * @release: Called when userspace releases file descriptor for device |
| 51 | * @read: Perform read(2) on device file descriptor |
| 52 | * @write: Perform write(2) on device file descriptor |
| 53 | * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_* |
| 54 | * operations documented below |
| 55 | * @mmap: Perform mmap(2) on a region of the device file descriptor |
Alex Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 56 | * @request: Request for the bus driver to release the device |
Alex Williamson | 5f3874c | 2020-03-24 09:28:25 -0600 | [diff] [blame] | 57 | * @match: Optional device name match callback (return: 0 for no-match, >0 for |
| 58 | * match, -errno for abort (ex. match with insufficient or incorrect |
| 59 | * additional args) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 60 | */ |
| 61 | struct vfio_device_ops { |
| 62 | char *name; |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame^] | 63 | int (*open_device)(struct vfio_device *vdev); |
| 64 | void (*close_device)(struct vfio_device *vdev); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 65 | int (*open)(struct vfio_device *vdev); |
| 66 | void (*release)(struct vfio_device *vdev); |
| 67 | ssize_t (*read)(struct vfio_device *vdev, char __user *buf, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 68 | size_t count, loff_t *ppos); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 69 | ssize_t (*write)(struct vfio_device *vdev, const char __user *buf, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 70 | size_t count, loff_t *size); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 71 | long (*ioctl)(struct vfio_device *vdev, unsigned int cmd, |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 72 | unsigned long arg); |
Jason Gunthorpe | 6df62c5 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 73 | int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma); |
| 74 | void (*request)(struct vfio_device *vdev, unsigned int count); |
| 75 | int (*match)(struct vfio_device *vdev, char *buf); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 76 | }; |
| 77 | |
Alex Williamson | 03a76b6 | 2015-12-21 15:13:33 -0700 | [diff] [blame] | 78 | extern struct iommu_group *vfio_iommu_group_get(struct device *dev); |
| 79 | extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev); |
| 80 | |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 81 | void vfio_init_group_dev(struct vfio_device *device, struct device *dev, |
Jason Gunthorpe | 1e04ec1 | 2021-03-30 09:53:08 -0600 | [diff] [blame] | 82 | const struct vfio_device_ops *ops); |
Max Gurtovoy | ae03c37 | 2021-08-05 22:18:59 -0300 | [diff] [blame] | 83 | void vfio_uninit_group_dev(struct vfio_device *device); |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 84 | int vfio_register_group_dev(struct vfio_device *device); |
Jason Gunthorpe | 0bfc6a4 | 2021-03-30 09:53:05 -0600 | [diff] [blame] | 85 | void vfio_unregister_group_dev(struct vfio_device *device); |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 86 | extern struct vfio_device *vfio_device_get_from_dev(struct device *dev); |
| 87 | extern void vfio_device_put(struct vfio_device *device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 88 | |
Jason Gunthorpe | 2fd585f | 2021-08-05 22:19:00 -0300 | [diff] [blame^] | 89 | int vfio_assign_device_set(struct vfio_device *device, void *set_id); |
| 90 | |
Steve Sistare | ec5e3294 | 2021-01-29 08:54:10 -0800 | [diff] [blame] | 91 | /* events for the backend driver notify callback */ |
| 92 | enum vfio_iommu_notify_type { |
| 93 | VFIO_IOMMU_CONTAINER_CLOSE = 0, |
| 94 | }; |
| 95 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 96 | /** |
| 97 | * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks |
| 98 | */ |
| 99 | struct vfio_iommu_driver_ops { |
| 100 | char *name; |
| 101 | struct module *owner; |
| 102 | void *(*open)(unsigned long arg); |
| 103 | void (*release)(void *iommu_data); |
| 104 | ssize_t (*read)(void *iommu_data, char __user *buf, |
| 105 | size_t count, loff_t *ppos); |
| 106 | ssize_t (*write)(void *iommu_data, const char __user *buf, |
| 107 | size_t count, loff_t *size); |
| 108 | long (*ioctl)(void *iommu_data, unsigned int cmd, |
| 109 | unsigned long arg); |
| 110 | int (*mmap)(void *iommu_data, struct vm_area_struct *vma); |
| 111 | int (*attach_group)(void *iommu_data, |
| 112 | struct iommu_group *group); |
| 113 | void (*detach_group)(void *iommu_data, |
| 114 | struct iommu_group *group); |
Kirti Wankhede | 95fc87b441 | 2020-05-29 02:00:54 +0530 | [diff] [blame] | 115 | int (*pin_pages)(void *iommu_data, |
| 116 | struct iommu_group *group, |
| 117 | unsigned long *user_pfn, |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 118 | int npage, int prot, |
| 119 | unsigned long *phys_pfn); |
| 120 | int (*unpin_pages)(void *iommu_data, |
| 121 | unsigned long *user_pfn, int npage); |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 122 | int (*register_notifier)(void *iommu_data, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 123 | unsigned long *events, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 124 | struct notifier_block *nb); |
| 125 | int (*unregister_notifier)(void *iommu_data, |
| 126 | struct notifier_block *nb); |
Yan Zhao | 8d46c0c | 2020-03-24 09:27:57 -0600 | [diff] [blame] | 127 | int (*dma_rw)(void *iommu_data, dma_addr_t user_iova, |
| 128 | void *data, size_t count, bool write); |
Lu Baolu | bdfae1c | 2020-12-09 09:44:44 +0800 | [diff] [blame] | 129 | struct iommu_domain *(*group_iommu_domain)(void *iommu_data, |
| 130 | struct iommu_group *group); |
Steve Sistare | ec5e3294 | 2021-01-29 08:54:10 -0800 | [diff] [blame] | 131 | void (*notify)(void *iommu_data, |
| 132 | enum vfio_iommu_notify_type event); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); |
| 136 | |
| 137 | extern void vfio_unregister_iommu_driver( |
| 138 | const struct vfio_iommu_driver_ops *ops); |
| 139 | |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 140 | /* |
| 141 | * External user API |
| 142 | */ |
| 143 | extern struct vfio_group *vfio_group_get_external_user(struct file *filep); |
| 144 | extern void vfio_group_put_external_user(struct vfio_group *group); |
Yan Zhao | c0560f5 | 2020-03-24 09:27:56 -0600 | [diff] [blame] | 145 | extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device |
| 146 | *dev); |
Alex Williamson | 5d6dee8 | 2017-06-28 13:50:05 -0600 | [diff] [blame] | 147 | extern bool vfio_external_group_match_file(struct vfio_group *group, |
| 148 | struct file *filep); |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 149 | extern int vfio_external_user_iommu_id(struct vfio_group *group); |
Alex Williamson | 88d7ab8 | 2014-02-26 11:38:39 -0700 | [diff] [blame] | 150 | extern long vfio_external_check_extension(struct vfio_group *group, |
| 151 | unsigned long arg); |
Alexey Kardashevskiy | 6cdd9782 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 152 | |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 153 | #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) |
| 154 | |
| 155 | extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, |
| 156 | int npage, int prot, unsigned long *phys_pfn); |
| 157 | extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, |
| 158 | int npage); |
| 159 | |
Yan Zhao | 40280cf | 2020-03-24 09:27:57 -0600 | [diff] [blame] | 160 | extern int vfio_group_pin_pages(struct vfio_group *group, |
| 161 | unsigned long *user_iova_pfn, int npage, |
| 162 | int prot, unsigned long *phys_pfn); |
| 163 | extern int vfio_group_unpin_pages(struct vfio_group *group, |
| 164 | unsigned long *user_iova_pfn, int npage); |
| 165 | |
Yan Zhao | 8d46c0c | 2020-03-24 09:27:57 -0600 | [diff] [blame] | 166 | extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova, |
| 167 | void *data, size_t len, bool write); |
| 168 | |
Lu Baolu | bdfae1c | 2020-12-09 09:44:44 +0800 | [diff] [blame] | 169 | extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group); |
| 170 | |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 171 | /* each type has independent events */ |
| 172 | enum vfio_notify_type { |
| 173 | VFIO_IOMMU_NOTIFY = 0, |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 174 | VFIO_GROUP_NOTIFY = 1, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | /* events for VFIO_IOMMU_NOTIFY */ |
| 178 | #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0) |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 179 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 180 | /* events for VFIO_GROUP_NOTIFY */ |
| 181 | #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0) |
| 182 | |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 183 | extern int vfio_register_notifier(struct device *dev, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 184 | enum vfio_notify_type type, |
| 185 | unsigned long *required_events, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 186 | struct notifier_block *nb); |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 187 | extern int vfio_unregister_notifier(struct device *dev, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 188 | enum vfio_notify_type type, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 189 | struct notifier_block *nb); |
| 190 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 191 | struct kvm; |
| 192 | extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); |
| 193 | |
Alex Williamson | d7a8d5e | 2016-02-22 16:02:33 -0700 | [diff] [blame] | 194 | /* |
| 195 | * Sub-module helpers |
| 196 | */ |
| 197 | struct vfio_info_cap { |
| 198 | struct vfio_info_cap_header *buf; |
| 199 | size_t size; |
| 200 | }; |
| 201 | extern struct vfio_info_cap_header *vfio_info_cap_add( |
| 202 | struct vfio_info_cap *caps, size_t size, u16 id, u16 version); |
| 203 | extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); |
| 204 | |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 205 | extern int vfio_info_add_capability(struct vfio_info_cap *caps, |
Alex Williamson | dda01f7 | 2017-12-12 12:59:39 -0700 | [diff] [blame] | 206 | struct vfio_info_cap_header *cap, |
| 207 | size_t size); |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 208 | |
Kirti Wankhede | c747f08a | 2016-11-17 02:16:27 +0530 | [diff] [blame] | 209 | extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, |
| 210 | int num_irqs, int max_irq_type, |
| 211 | size_t *data_size); |
| 212 | |
Gavin Shan | 92d18a6 | 2014-08-08 10:36:20 -0600 | [diff] [blame] | 213 | struct pci_dev; |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 214 | #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH) |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 215 | extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 216 | extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev); |
| 217 | extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 218 | unsigned int cmd, |
| 219 | unsigned long arg); |
| 220 | #else |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 221 | static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev) |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 222 | { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev) |
| 226 | { |
| 227 | } |
| 228 | |
| 229 | static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 230 | unsigned int cmd, |
| 231 | unsigned long arg) |
| 232 | { |
| 233 | return -ENOTTY; |
| 234 | } |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 235 | #endif /* CONFIG_VFIO_SPAPR_EEH */ |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * IRQfd - generic |
| 239 | */ |
| 240 | struct virqfd { |
| 241 | void *opaque; |
| 242 | struct eventfd_ctx *eventfd; |
| 243 | int (*handler)(void *, void *); |
| 244 | void (*thread)(void *, void *); |
| 245 | void *data; |
| 246 | struct work_struct inject; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 247 | wait_queue_entry_t wait; |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 248 | poll_table pt; |
| 249 | struct work_struct shutdown; |
| 250 | struct virqfd **pvirqfd; |
| 251 | }; |
| 252 | |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 253 | extern int vfio_virqfd_enable(void *opaque, |
| 254 | int (*handler)(void *, void *), |
| 255 | void (*thread)(void *, void *), |
| 256 | void *data, struct virqfd **pvirqfd, int fd); |
| 257 | extern void vfio_virqfd_disable(struct virqfd **pvirqfd); |
| 258 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 259 | #endif /* VFIO_H */ |