Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1 | /* |
| 2 | * VFIO API definition |
| 3 | * |
| 4 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. |
| 5 | * Author: Alex Williamson <alex.williamson@redhat.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #ifndef VFIO_H |
| 12 | #define VFIO_H |
| 13 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 14 | |
| 15 | #include <linux/iommu.h> |
| 16 | #include <linux/mm.h> |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/poll.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 19 | #include <uapi/linux/vfio.h> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * struct vfio_device_ops - VFIO bus driver device callbacks |
| 23 | * |
| 24 | * @open: Called when userspace creates new file descriptor for device |
| 25 | * @release: Called when userspace releases file descriptor for device |
| 26 | * @read: Perform read(2) on device file descriptor |
| 27 | * @write: Perform write(2) on device file descriptor |
| 28 | * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_* |
| 29 | * operations documented below |
| 30 | * @mmap: Perform mmap(2) on a region of the device file descriptor |
Alex Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 31 | * @request: Request for the bus driver to release the device |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 32 | */ |
| 33 | struct vfio_device_ops { |
| 34 | char *name; |
| 35 | int (*open)(void *device_data); |
| 36 | void (*release)(void *device_data); |
| 37 | ssize_t (*read)(void *device_data, char __user *buf, |
| 38 | size_t count, loff_t *ppos); |
| 39 | ssize_t (*write)(void *device_data, const char __user *buf, |
| 40 | size_t count, loff_t *size); |
| 41 | long (*ioctl)(void *device_data, unsigned int cmd, |
| 42 | unsigned long arg); |
| 43 | int (*mmap)(void *device_data, struct vm_area_struct *vma); |
Alex Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 44 | void (*request)(void *device_data, unsigned int count); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
Alex Williamson | 03a76b6 | 2015-12-21 15:13:33 -0700 | [diff] [blame] | 47 | extern struct iommu_group *vfio_iommu_group_get(struct device *dev); |
| 48 | extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev); |
| 49 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 50 | extern int vfio_add_group_dev(struct device *dev, |
| 51 | const struct vfio_device_ops *ops, |
| 52 | void *device_data); |
| 53 | |
| 54 | extern void *vfio_del_group_dev(struct device *dev); |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 55 | extern struct vfio_device *vfio_device_get_from_dev(struct device *dev); |
| 56 | extern void vfio_device_put(struct vfio_device *device); |
| 57 | extern void *vfio_device_data(struct vfio_device *device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks |
| 61 | */ |
| 62 | struct vfio_iommu_driver_ops { |
| 63 | char *name; |
| 64 | struct module *owner; |
| 65 | void *(*open)(unsigned long arg); |
| 66 | void (*release)(void *iommu_data); |
| 67 | ssize_t (*read)(void *iommu_data, char __user *buf, |
| 68 | size_t count, loff_t *ppos); |
| 69 | ssize_t (*write)(void *iommu_data, const char __user *buf, |
| 70 | size_t count, loff_t *size); |
| 71 | long (*ioctl)(void *iommu_data, unsigned int cmd, |
| 72 | unsigned long arg); |
| 73 | int (*mmap)(void *iommu_data, struct vm_area_struct *vma); |
| 74 | int (*attach_group)(void *iommu_data, |
| 75 | struct iommu_group *group); |
| 76 | void (*detach_group)(void *iommu_data, |
| 77 | struct iommu_group *group); |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 78 | int (*pin_pages)(void *iommu_data, unsigned long *user_pfn, |
| 79 | int npage, int prot, |
| 80 | unsigned long *phys_pfn); |
| 81 | int (*unpin_pages)(void *iommu_data, |
| 82 | unsigned long *user_pfn, int npage); |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 83 | int (*register_notifier)(void *iommu_data, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 84 | unsigned long *events, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 85 | struct notifier_block *nb); |
| 86 | int (*unregister_notifier)(void *iommu_data, |
| 87 | struct notifier_block *nb); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); |
| 91 | |
| 92 | extern void vfio_unregister_iommu_driver( |
| 93 | const struct vfio_iommu_driver_ops *ops); |
| 94 | |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 95 | /* |
| 96 | * External user API |
| 97 | */ |
| 98 | extern struct vfio_group *vfio_group_get_external_user(struct file *filep); |
| 99 | extern void vfio_group_put_external_user(struct vfio_group *group); |
Alex Williamson | 5d6dee8 | 2017-06-28 13:50:05 -0600 | [diff] [blame] | 100 | extern bool vfio_external_group_match_file(struct vfio_group *group, |
| 101 | struct file *filep); |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 102 | extern int vfio_external_user_iommu_id(struct vfio_group *group); |
Alex Williamson | 88d7ab8 | 2014-02-26 11:38:39 -0700 | [diff] [blame] | 103 | extern long vfio_external_check_extension(struct vfio_group *group, |
| 104 | unsigned long arg); |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 105 | |
Kirti Wankhede | 2169037 | 2016-11-17 02:16:17 +0530 | [diff] [blame] | 106 | #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) |
| 107 | |
| 108 | extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, |
| 109 | int npage, int prot, unsigned long *phys_pfn); |
| 110 | extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, |
| 111 | int npage); |
| 112 | |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 113 | /* each type has independent events */ |
| 114 | enum vfio_notify_type { |
| 115 | VFIO_IOMMU_NOTIFY = 0, |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 116 | VFIO_GROUP_NOTIFY = 1, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /* events for VFIO_IOMMU_NOTIFY */ |
| 120 | #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0) |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 121 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 122 | /* events for VFIO_GROUP_NOTIFY */ |
| 123 | #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0) |
| 124 | |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 125 | extern int vfio_register_notifier(struct device *dev, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 126 | enum vfio_notify_type type, |
| 127 | unsigned long *required_events, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 128 | struct notifier_block *nb); |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 129 | extern int vfio_unregister_notifier(struct device *dev, |
Jike Song | 22195cb | 2016-12-01 13:20:05 +0800 | [diff] [blame] | 130 | enum vfio_notify_type type, |
Kirti Wankhede | c086de81 | 2016-11-17 10:28:26 +0530 | [diff] [blame] | 131 | struct notifier_block *nb); |
| 132 | |
Jike Song | ccd46db | 2016-12-01 13:20:06 +0800 | [diff] [blame] | 133 | struct kvm; |
| 134 | extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); |
| 135 | |
Alex Williamson | d7a8d5e | 2016-02-22 16:02:33 -0700 | [diff] [blame] | 136 | /* |
| 137 | * Sub-module helpers |
| 138 | */ |
| 139 | struct vfio_info_cap { |
| 140 | struct vfio_info_cap_header *buf; |
| 141 | size_t size; |
| 142 | }; |
| 143 | extern struct vfio_info_cap_header *vfio_info_cap_add( |
| 144 | struct vfio_info_cap *caps, size_t size, u16 id, u16 version); |
| 145 | extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); |
| 146 | |
Kirti Wankhede | b3c0a86 | 2016-11-17 02:16:25 +0530 | [diff] [blame] | 147 | extern int vfio_info_add_capability(struct vfio_info_cap *caps, |
| 148 | int cap_type_id, void *cap_type); |
| 149 | |
Kirti Wankhede | c747f08 | 2016-11-17 02:16:27 +0530 | [diff] [blame] | 150 | extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, |
| 151 | int num_irqs, int max_irq_type, |
| 152 | size_t *data_size); |
| 153 | |
Gavin Shan | 92d18a6 | 2014-08-08 10:36:20 -0600 | [diff] [blame] | 154 | struct pci_dev; |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 155 | #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH) |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 156 | extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 157 | extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev); |
| 158 | extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 159 | unsigned int cmd, |
| 160 | unsigned long arg); |
| 161 | #else |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 162 | static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev) |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 163 | { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 171 | unsigned int cmd, |
| 172 | unsigned long arg) |
| 173 | { |
| 174 | return -ENOTTY; |
| 175 | } |
Murilo Opsfelder Araujo | bb67b49 | 2017-07-18 14:22:20 -0300 | [diff] [blame] | 176 | #endif /* CONFIG_VFIO_SPAPR_EEH */ |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * IRQfd - generic |
| 180 | */ |
| 181 | struct virqfd { |
| 182 | void *opaque; |
| 183 | struct eventfd_ctx *eventfd; |
| 184 | int (*handler)(void *, void *); |
| 185 | void (*thread)(void *, void *); |
| 186 | void *data; |
| 187 | struct work_struct inject; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 188 | wait_queue_entry_t wait; |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 189 | poll_table pt; |
| 190 | struct work_struct shutdown; |
| 191 | struct virqfd **pvirqfd; |
| 192 | }; |
| 193 | |
Antonios Motakis | 7e992d6 | 2015-03-16 14:08:54 -0600 | [diff] [blame] | 194 | extern int vfio_virqfd_enable(void *opaque, |
| 195 | int (*handler)(void *, void *), |
| 196 | void (*thread)(void *, void *), |
| 197 | void *data, struct virqfd **pvirqfd, int fd); |
| 198 | extern void vfio_virqfd_disable(struct virqfd **pvirqfd); |
| 199 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 200 | #endif /* VFIO_H */ |