blob: edf9b2cad277d95d5b4b2ffd4b3202d2abd14f70 [file] [log] [blame]
Alex Williamsoncba33452012-07-31 08:16:22 -06001/*
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
Alex Williamsoncba33452012-07-31 08:16:22 -060014
15#include <linux/iommu.h>
16#include <linux/mm.h>
Antonios Motakis7e992d62015-03-16 14:08:54 -060017#include <linux/workqueue.h>
18#include <linux/poll.h>
David Howells607ca462012-10-13 10:46:48 +010019#include <uapi/linux/vfio.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060020
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
Alex Williamson13060b62015-02-06 15:05:07 -070031 * @request: Request for the bus driver to release the device
Alex Williamsoncba33452012-07-31 08:16:22 -060032 */
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);
Alex Williamson13060b62015-02-06 15:05:07 -070044 void (*request)(void *device_data, unsigned int count);
Alex Williamsoncba33452012-07-31 08:16:22 -060045};
46
Alex Williamson03a76b62015-12-21 15:13:33 -070047extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
48extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
49
Alex Williamsoncba33452012-07-31 08:16:22 -060050extern 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);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060055extern 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);
Alex Williamsoncba33452012-07-31 08:16:22 -060058
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);
Kirti Wankhede21690372016-11-17 02:16:17 +053078 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);
Kirti Wankhedec086de812016-11-17 10:28:26 +053083 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +080084 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +053085 struct notifier_block *nb);
86 int (*unregister_notifier)(void *iommu_data,
87 struct notifier_block *nb);
Alex Williamsoncba33452012-07-31 08:16:22 -060088};
89
90extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
91
92extern void vfio_unregister_iommu_driver(
93 const struct vfio_iommu_driver_ops *ops);
94
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -060095/*
96 * External user API
97 */
98extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
99extern void vfio_group_put_external_user(struct vfio_group *group);
100extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700101extern long vfio_external_check_extension(struct vfio_group *group,
102 unsigned long arg);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600103
Kirti Wankhede21690372016-11-17 02:16:17 +0530104#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
105
106extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
107 int npage, int prot, unsigned long *phys_pfn);
108extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
109 int npage);
110
Jike Song22195cb2016-12-01 13:20:05 +0800111/* each type has independent events */
112enum vfio_notify_type {
113 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800114 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800115};
116
117/* events for VFIO_IOMMU_NOTIFY */
118#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530119
Jike Songccd46db2016-12-01 13:20:06 +0800120/* events for VFIO_GROUP_NOTIFY */
121#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
122
Kirti Wankhedec086de812016-11-17 10:28:26 +0530123extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800124 enum vfio_notify_type type,
125 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530126 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530127extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800128 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530129 struct notifier_block *nb);
130
Jike Songccd46db2016-12-01 13:20:06 +0800131struct kvm;
132extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
133
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700134/*
135 * Sub-module helpers
136 */
137struct vfio_info_cap {
138 struct vfio_info_cap_header *buf;
139 size_t size;
140};
141extern struct vfio_info_cap_header *vfio_info_cap_add(
142 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
143extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
144
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530145extern int vfio_info_add_capability(struct vfio_info_cap *caps,
146 int cap_type_id, void *cap_type);
147
Kirti Wankhedec747f082016-11-17 02:16:27 +0530148extern 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
Gavin Shan92d18a62014-08-08 10:36:20 -0600152struct pci_dev;
Gavin Shan1b69be52014-06-10 11:41:57 +1000153#ifdef CONFIG_EEH
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600154extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000155extern 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
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600160static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000161{
Gavin Shan1b69be52014-06-10 11:41:57 +1000162}
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}
174#endif /* CONFIG_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600175
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;
186 wait_queue_t wait;
187 poll_table pt;
188 struct work_struct shutdown;
189 struct virqfd **pvirqfd;
190};
191
Antonios Motakis7e992d62015-03-16 14:08:54 -0600192extern 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
Alex Williamsoncba33452012-07-31 08:16:22 -0600198#endif /* VFIO_H */