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