4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
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.
15 #include <linux/iommu.h>
17 #include <linux/workqueue.h>
18 #include <linux/poll.h>
19 #include <uapi/linux/vfio.h>
22 * struct vfio_device_ops - VFIO bus driver device callbacks
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
31 * @request: Request for the bus driver to release the device
33 struct vfio_device_ops
{
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
,
43 int (*mmap
)(void *device_data
, struct vm_area_struct
*vma
);
44 void (*request
)(void *device_data
, unsigned int count
);
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
);
50 extern int vfio_add_group_dev(struct device
*dev
,
51 const struct vfio_device_ops
*ops
,
54 extern void *vfio_del_group_dev(struct device
*dev
);
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
);
60 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
62 struct vfio_iommu_driver_ops
{
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
,
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
);
78 int (*pin_pages
)(void *iommu_data
, unsigned long *user_pfn
,
80 unsigned long *phys_pfn
);
81 int (*unpin_pages
)(void *iommu_data
,
82 unsigned long *user_pfn
, int npage
);
83 int (*register_notifier
)(void *iommu_data
,
84 unsigned long *events
,
85 struct notifier_block
*nb
);
86 int (*unregister_notifier
)(void *iommu_data
,
87 struct notifier_block
*nb
);
90 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops
*ops
);
92 extern void vfio_unregister_iommu_driver(
93 const struct vfio_iommu_driver_ops
*ops
);
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
);
100 extern bool vfio_external_group_match_file(struct vfio_group
*group
,
102 extern int vfio_external_user_iommu_id(struct vfio_group
*group
);
103 extern long vfio_external_check_extension(struct vfio_group
*group
,
106 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
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
,
113 /* each type has independent events */
114 enum vfio_notify_type
{
115 VFIO_IOMMU_NOTIFY
= 0,
116 VFIO_GROUP_NOTIFY
= 1,
119 /* events for VFIO_IOMMU_NOTIFY */
120 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
122 /* events for VFIO_GROUP_NOTIFY */
123 #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
125 extern int vfio_register_notifier(struct device
*dev
,
126 enum vfio_notify_type type
,
127 unsigned long *required_events
,
128 struct notifier_block
*nb
);
129 extern int vfio_unregister_notifier(struct device
*dev
,
130 enum vfio_notify_type type
,
131 struct notifier_block
*nb
);
134 extern void vfio_group_set_kvm(struct vfio_group
*group
, struct kvm
*kvm
);
139 struct vfio_info_cap
{
140 struct vfio_info_cap_header
*buf
;
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
);
147 extern int vfio_info_add_capability(struct vfio_info_cap
*caps
,
148 struct vfio_info_cap_header
*cap
,
151 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set
*hdr
,
152 int num_irqs
, int max_irq_type
,
156 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
157 extern void vfio_spapr_pci_eeh_open(struct pci_dev
*pdev
);
158 extern void vfio_spapr_pci_eeh_release(struct pci_dev
*pdev
);
159 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group
*group
,
163 static inline void vfio_spapr_pci_eeh_open(struct pci_dev
*pdev
)
167 static inline void vfio_spapr_pci_eeh_release(struct pci_dev
*pdev
)
171 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group
*group
,
177 #endif /* CONFIG_VFIO_SPAPR_EEH */
184 struct eventfd_ctx
*eventfd
;
185 int (*handler
)(void *, void *);
186 void (*thread
)(void *, void *);
188 struct work_struct inject
;
189 wait_queue_entry_t wait
;
191 struct work_struct shutdown
;
192 struct virqfd
**pvirqfd
;
195 extern int vfio_virqfd_enable(void *opaque
,
196 int (*handler
)(void *, void *),
197 void (*thread
)(void *, void *),
198 void *data
, struct virqfd
**pvirqfd
, int fd
);
199 extern void vfio_virqfd_disable(struct virqfd
**pvirqfd
);