]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/linux/vfio.h
Merge tag '5.2-rc6-smb3-fix' of git://git.samba.org/sfrench/cifs-2.6
[mirror_ubuntu-eoan-kernel.git] / include / linux / vfio.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
cba3345c
AW
2/*
3 * VFIO API definition
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
cba3345c
AW
7 */
8#ifndef VFIO_H
9#define VFIO_H
10
cba3345c
AW
11
12#include <linux/iommu.h>
13#include <linux/mm.h>
7e992d69
AM
14#include <linux/workqueue.h>
15#include <linux/poll.h>
607ca46e 16#include <uapi/linux/vfio.h>
cba3345c
AW
17
18/**
19 * struct vfio_device_ops - VFIO bus driver device callbacks
20 *
21 * @open: Called when userspace creates new file descriptor for device
22 * @release: Called when userspace releases file descriptor for device
23 * @read: Perform read(2) on device file descriptor
24 * @write: Perform write(2) on device file descriptor
25 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
26 * operations documented below
27 * @mmap: Perform mmap(2) on a region of the device file descriptor
13060b64 28 * @request: Request for the bus driver to release the device
cba3345c
AW
29 */
30struct vfio_device_ops {
31 char *name;
32 int (*open)(void *device_data);
33 void (*release)(void *device_data);
34 ssize_t (*read)(void *device_data, char __user *buf,
35 size_t count, loff_t *ppos);
36 ssize_t (*write)(void *device_data, const char __user *buf,
37 size_t count, loff_t *size);
38 long (*ioctl)(void *device_data, unsigned int cmd,
39 unsigned long arg);
40 int (*mmap)(void *device_data, struct vm_area_struct *vma);
13060b64 41 void (*request)(void *device_data, unsigned int count);
cba3345c
AW
42};
43
03a76b60
AW
44extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
45extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
46
cba3345c
AW
47extern int vfio_add_group_dev(struct device *dev,
48 const struct vfio_device_ops *ops,
49 void *device_data);
50
51extern void *vfio_del_group_dev(struct device *dev);
44f50716
VMP
52extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
53extern void vfio_device_put(struct vfio_device *device);
54extern void *vfio_device_data(struct vfio_device *device);
cba3345c
AW
55
56/**
57 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
58 */
59struct vfio_iommu_driver_ops {
60 char *name;
61 struct module *owner;
62 void *(*open)(unsigned long arg);
63 void (*release)(void *iommu_data);
64 ssize_t (*read)(void *iommu_data, char __user *buf,
65 size_t count, loff_t *ppos);
66 ssize_t (*write)(void *iommu_data, const char __user *buf,
67 size_t count, loff_t *size);
68 long (*ioctl)(void *iommu_data, unsigned int cmd,
69 unsigned long arg);
70 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
71 int (*attach_group)(void *iommu_data,
72 struct iommu_group *group);
73 void (*detach_group)(void *iommu_data,
74 struct iommu_group *group);
2169037d
KW
75 int (*pin_pages)(void *iommu_data, unsigned long *user_pfn,
76 int npage, int prot,
77 unsigned long *phys_pfn);
78 int (*unpin_pages)(void *iommu_data,
79 unsigned long *user_pfn, int npage);
c086de81 80 int (*register_notifier)(void *iommu_data,
22195cbd 81 unsigned long *events,
c086de81
KW
82 struct notifier_block *nb);
83 int (*unregister_notifier)(void *iommu_data,
84 struct notifier_block *nb);
cba3345c
AW
85};
86
87extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
88
89extern void vfio_unregister_iommu_driver(
90 const struct vfio_iommu_driver_ops *ops);
91
6cdd9782
AK
92/*
93 * External user API
94 */
95extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
96extern void vfio_group_put_external_user(struct vfio_group *group);
5d6dee80
AW
97extern bool vfio_external_group_match_file(struct vfio_group *group,
98 struct file *filep);
6cdd9782 99extern int vfio_external_user_iommu_id(struct vfio_group *group);
88d7ab89
AW
100extern long vfio_external_check_extension(struct vfio_group *group,
101 unsigned long arg);
6cdd9782 102
2169037d
KW
103#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
104
105extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
106 int npage, int prot, unsigned long *phys_pfn);
107extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
108 int npage);
109
22195cbd
JS
110/* each type has independent events */
111enum vfio_notify_type {
112 VFIO_IOMMU_NOTIFY = 0,
ccd46dba 113 VFIO_GROUP_NOTIFY = 1,
22195cbd
JS
114};
115
116/* events for VFIO_IOMMU_NOTIFY */
117#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
c086de81 118
ccd46dba
JS
119/* events for VFIO_GROUP_NOTIFY */
120#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
121
c086de81 122extern int vfio_register_notifier(struct device *dev,
22195cbd
JS
123 enum vfio_notify_type type,
124 unsigned long *required_events,
c086de81 125 struct notifier_block *nb);
c086de81 126extern int vfio_unregister_notifier(struct device *dev,
22195cbd 127 enum vfio_notify_type type,
c086de81
KW
128 struct notifier_block *nb);
129
ccd46dba
JS
130struct kvm;
131extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
132
d7a8d5ed
AW
133/*
134 * Sub-module helpers
135 */
136struct vfio_info_cap {
137 struct vfio_info_cap_header *buf;
138 size_t size;
139};
140extern struct vfio_info_cap_header *vfio_info_cap_add(
141 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
142extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
143
b3c0a866 144extern int vfio_info_add_capability(struct vfio_info_cap *caps,
dda01f78
AW
145 struct vfio_info_cap_header *cap,
146 size_t size);
b3c0a866 147
c747f08a
KW
148extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
149 int num_irqs, int max_irq_type,
150 size_t *data_size);
151
92d18a68 152struct pci_dev;
bb67b496 153#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
9b936c96 154extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
1b69be5e
GS
155extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
156extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
157 unsigned int cmd,
158 unsigned long arg);
159#else
9b936c96 160static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
1b69be5e 161{
1b69be5e
GS
162}
163
164static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
165{
166}
167
168static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
169 unsigned int cmd,
170 unsigned long arg)
171{
172 return -ENOTTY;
173}
bb67b496 174#endif /* CONFIG_VFIO_SPAPR_EEH */
7e992d69
AM
175
176/*
177 * IRQfd - generic
178 */
179struct virqfd {
180 void *opaque;
181 struct eventfd_ctx *eventfd;
182 int (*handler)(void *, void *);
183 void (*thread)(void *, void *);
184 void *data;
185 struct work_struct inject;
ac6424b9 186 wait_queue_entry_t wait;
7e992d69
AM
187 poll_table pt;
188 struct work_struct shutdown;
189 struct virqfd **pvirqfd;
190};
191
7e992d69
AM
192extern int vfio_virqfd_enable(void *opaque,
193 int (*handler)(void *, void *),
194 void (*thread)(void *, void *),
195 void *data, struct virqfd **pvirqfd, int fd);
196extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
197
cba3345c 198#endif /* VFIO_H */