vfio: introduce vfio_dma_rw to read/write a range of IOVAs
[linux-block.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);
8d46c0cc
YZ
85 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
86 void *data, size_t count, bool write);
cba3345c
AW
87};
88
89extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
90
91extern void vfio_unregister_iommu_driver(
92 const struct vfio_iommu_driver_ops *ops);
93
6cdd9782
AK
94/*
95 * External user API
96 */
97extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
98extern void vfio_group_put_external_user(struct vfio_group *group);
c0560f51
YZ
99extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
100 *dev);
5d6dee80
AW
101extern bool vfio_external_group_match_file(struct vfio_group *group,
102 struct file *filep);
6cdd9782 103extern int vfio_external_user_iommu_id(struct vfio_group *group);
88d7ab89
AW
104extern long vfio_external_check_extension(struct vfio_group *group,
105 unsigned long arg);
6cdd9782 106
2169037d
KW
107#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
108
109extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
110 int npage, int prot, unsigned long *phys_pfn);
111extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
112 int npage);
113
8d46c0cc
YZ
114extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
115 void *data, size_t len, bool write);
116
22195cbd
JS
117/* each type has independent events */
118enum vfio_notify_type {
119 VFIO_IOMMU_NOTIFY = 0,
ccd46dba 120 VFIO_GROUP_NOTIFY = 1,
22195cbd
JS
121};
122
123/* events for VFIO_IOMMU_NOTIFY */
124#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
c086de81 125
ccd46dba
JS
126/* events for VFIO_GROUP_NOTIFY */
127#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
128
c086de81 129extern int vfio_register_notifier(struct device *dev,
22195cbd
JS
130 enum vfio_notify_type type,
131 unsigned long *required_events,
c086de81 132 struct notifier_block *nb);
c086de81 133extern int vfio_unregister_notifier(struct device *dev,
22195cbd 134 enum vfio_notify_type type,
c086de81
KW
135 struct notifier_block *nb);
136
ccd46dba
JS
137struct kvm;
138extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
139
d7a8d5ed
AW
140/*
141 * Sub-module helpers
142 */
143struct vfio_info_cap {
144 struct vfio_info_cap_header *buf;
145 size_t size;
146};
147extern struct vfio_info_cap_header *vfio_info_cap_add(
148 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
149extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
150
b3c0a866 151extern int vfio_info_add_capability(struct vfio_info_cap *caps,
dda01f78
AW
152 struct vfio_info_cap_header *cap,
153 size_t size);
b3c0a866 154
c747f08a
KW
155extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
156 int num_irqs, int max_irq_type,
157 size_t *data_size);
158
92d18a68 159struct pci_dev;
bb67b496 160#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
9b936c96 161extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
1b69be5e
GS
162extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
163extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
164 unsigned int cmd,
165 unsigned long arg);
166#else
9b936c96 167static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
1b69be5e 168{
1b69be5e
GS
169}
170
171static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
172{
173}
174
175static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
176 unsigned int cmd,
177 unsigned long arg)
178{
179 return -ENOTTY;
180}
bb67b496 181#endif /* CONFIG_VFIO_SPAPR_EEH */
7e992d69
AM
182
183/*
184 * IRQfd - generic
185 */
186struct virqfd {
187 void *opaque;
188 struct eventfd_ctx *eventfd;
189 int (*handler)(void *, void *);
190 void (*thread)(void *, void *);
191 void *data;
192 struct work_struct inject;
ac6424b9 193 wait_queue_entry_t wait;
7e992d69
AM
194 poll_table pt;
195 struct work_struct shutdown;
196 struct virqfd **pvirqfd;
197};
198
7e992d69
AM
199extern int vfio_virqfd_enable(void *opaque,
200 int (*handler)(void *, void *),
201 void (*thread)(void *, void *),
202 void *data, struct virqfd **pvirqfd, int fd);
203extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
204
cba3345c 205#endif /* VFIO_H */