vfio iommu: Add blocking notifier to notify DMA_UNMAP
[linux-block.git] / include / linux / vfio.h
CommitLineData
cba3345c
AW
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
cba3345c
AW
14
15#include <linux/iommu.h>
16#include <linux/mm.h>
7e992d69
AM
17#include <linux/workqueue.h>
18#include <linux/poll.h>
607ca46e 19#include <uapi/linux/vfio.h>
cba3345c
AW
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
13060b64 31 * @request: Request for the bus driver to release the device
cba3345c
AW
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);
13060b64 44 void (*request)(void *device_data, unsigned int count);
cba3345c
AW
45};
46
03a76b60
AW
47extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
48extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
49
cba3345c
AW
50extern int vfio_add_group_dev(struct device *dev,
51 const struct vfio_device_ops *ops,
52 void *device_data);
53
54extern void *vfio_del_group_dev(struct device *dev);
44f50716
VMP
55extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
56extern void vfio_device_put(struct vfio_device *device);
57extern void *vfio_device_data(struct vfio_device *device);
cba3345c
AW
58
59/**
60 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
61 */
62struct 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);
2169037d
KW
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);
c086de81
KW
83 int (*register_notifier)(void *iommu_data,
84 struct notifier_block *nb);
85 int (*unregister_notifier)(void *iommu_data,
86 struct notifier_block *nb);
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);
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
c086de81
KW
110#define VFIO_IOMMU_NOTIFY_DMA_UNMAP (1)
111
112extern int vfio_register_notifier(struct device *dev,
113 struct notifier_block *nb);
114
115extern int vfio_unregister_notifier(struct device *dev,
116 struct notifier_block *nb);
117
d7a8d5ed
AW
118/*
119 * Sub-module helpers
120 */
121struct vfio_info_cap {
122 struct vfio_info_cap_header *buf;
123 size_t size;
124};
125extern struct vfio_info_cap_header *vfio_info_cap_add(
126 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
127extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
128
92d18a68 129struct pci_dev;
1b69be5e 130#ifdef CONFIG_EEH
9b936c96 131extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
1b69be5e
GS
132extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
133extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
134 unsigned int cmd,
135 unsigned long arg);
136#else
9b936c96 137static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
1b69be5e 138{
1b69be5e
GS
139}
140
141static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
142{
143}
144
145static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
146 unsigned int cmd,
147 unsigned long arg)
148{
149 return -ENOTTY;
150}
151#endif /* CONFIG_EEH */
7e992d69
AM
152
153/*
154 * IRQfd - generic
155 */
156struct virqfd {
157 void *opaque;
158 struct eventfd_ctx *eventfd;
159 int (*handler)(void *, void *);
160 void (*thread)(void *, void *);
161 void *data;
162 struct work_struct inject;
163 wait_queue_t wait;
164 poll_table pt;
165 struct work_struct shutdown;
166 struct virqfd **pvirqfd;
167};
168
7e992d69
AM
169extern int vfio_virqfd_enable(void *opaque,
170 int (*handler)(void *, void *),
171 void (*thread)(void *, void *),
172 void *data, struct virqfd **pvirqfd, int fd);
173extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
174
cba3345c 175#endif /* VFIO_H */