Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * VFIO API definition
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
7 */
8#ifndef VFIO_H
9#define VFIO_H
10
11
12#include <linux/iommu.h>
13#include <linux/mm.h>
14#include <linux/workqueue.h>
15#include <linux/poll.h>
16#include <uapi/linux/vfio.h>
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
28 * @request: Request for the bus driver to release the device
29 * @match: Optional device name match callback (return: 0 for no-match, >0 for
30 * match, -errno for abort (ex. match with insufficient or incorrect
31 * additional args)
32 */
33struct 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);
44 void (*request)(void *device_data, unsigned int count);
45 int (*match)(void *device_data, char *buf);
46};
47
48extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
49extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
50
51extern int vfio_add_group_dev(struct device *dev,
52 const struct vfio_device_ops *ops,
53 void *device_data);
54
55extern void *vfio_del_group_dev(struct device *dev);
56extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
57extern void vfio_device_put(struct vfio_device *device);
58extern void *vfio_device_data(struct vfio_device *device);
59
60/**
61 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
62 */
63struct vfio_iommu_driver_ops {
64 char *name;
65 struct module *owner;
66 void *(*open)(unsigned long arg);
67 void (*release)(void *iommu_data);
68 ssize_t (*read)(void *iommu_data, char __user *buf,
69 size_t count, loff_t *ppos);
70 ssize_t (*write)(void *iommu_data, const char __user *buf,
71 size_t count, loff_t *size);
72 long (*ioctl)(void *iommu_data, unsigned int cmd,
73 unsigned long arg);
74 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
75 int (*attach_group)(void *iommu_data,
76 struct iommu_group *group);
77 void (*detach_group)(void *iommu_data,
78 struct iommu_group *group);
79 int (*pin_pages)(void *iommu_data,
80 struct iommu_group *group,
81 unsigned long *user_pfn,
82 int npage, int prot,
83 unsigned long *phys_pfn);
84 int (*unpin_pages)(void *iommu_data,
85 unsigned long *user_pfn, int npage);
86 int (*register_notifier)(void *iommu_data,
87 unsigned long *events,
88 struct notifier_block *nb);
89 int (*unregister_notifier)(void *iommu_data,
90 struct notifier_block *nb);
91 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
92 void *data, size_t count, bool write);
93 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
94 struct iommu_group *group);
95};
96
97extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
98
99extern void vfio_unregister_iommu_driver(
100 const struct vfio_iommu_driver_ops *ops);
101
102/*
103 * External user API
104 */
105extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
106extern void vfio_group_put_external_user(struct vfio_group *group);
107extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
108 *dev);
109extern bool vfio_external_group_match_file(struct vfio_group *group,
110 struct file *filep);
111extern int vfio_external_user_iommu_id(struct vfio_group *group);
112extern long vfio_external_check_extension(struct vfio_group *group,
113 unsigned long arg);
114
115#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
116
117extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
118 int npage, int prot, unsigned long *phys_pfn);
119extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
120 int npage);
121
122extern int vfio_group_pin_pages(struct vfio_group *group,
123 unsigned long *user_iova_pfn, int npage,
124 int prot, unsigned long *phys_pfn);
125extern int vfio_group_unpin_pages(struct vfio_group *group,
126 unsigned long *user_iova_pfn, int npage);
127
128extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
129 void *data, size_t len, bool write);
130
131extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
132
133/* each type has independent events */
134enum vfio_notify_type {
135 VFIO_IOMMU_NOTIFY = 0,
136 VFIO_GROUP_NOTIFY = 1,
137};
138
139/* events for VFIO_IOMMU_NOTIFY */
140#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
141
142/* events for VFIO_GROUP_NOTIFY */
143#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
144
145extern int vfio_register_notifier(struct device *dev,
146 enum vfio_notify_type type,
147 unsigned long *required_events,
148 struct notifier_block *nb);
149extern int vfio_unregister_notifier(struct device *dev,
150 enum vfio_notify_type type,
151 struct notifier_block *nb);
152
153struct kvm;
154extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
155
156/*
157 * Sub-module helpers
158 */
159struct vfio_info_cap {
160 struct vfio_info_cap_header *buf;
161 size_t size;
162};
163extern struct vfio_info_cap_header *vfio_info_cap_add(
164 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
165extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
166
167extern int vfio_info_add_capability(struct vfio_info_cap *caps,
168 struct vfio_info_cap_header *cap,
169 size_t size);
170
171extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
172 int num_irqs, int max_irq_type,
173 size_t *data_size);
174
175struct pci_dev;
176#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
177extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
178extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
179extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
180 unsigned int cmd,
181 unsigned long arg);
182#else
183static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
184{
185}
186
187static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
188{
189}
190
191static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
192 unsigned int cmd,
193 unsigned long arg)
194{
195 return -ENOTTY;
196}
197#endif /* CONFIG_VFIO_SPAPR_EEH */
198
199/*
200 * IRQfd - generic
201 */
202struct virqfd {
203 void *opaque;
204 struct eventfd_ctx *eventfd;
205 int (*handler)(void *, void *);
206 void (*thread)(void *, void *);
207 void *data;
208 struct work_struct inject;
209 wait_queue_entry_t wait;
210 poll_table pt;
211 struct work_struct shutdown;
212 struct virqfd **pvirqfd;
213};
214
215extern int vfio_virqfd_enable(void *opaque,
216 int (*handler)(void *, void *),
217 void (*thread)(void *, void *),
218 void *data, struct virqfd **pvirqfd, int fd);
219extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
220
221#endif /* VFIO_H */