blob: 029694b977f2b968dcef0bd21f8bbbb5629e36c9 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Alex Williamsoncba33452012-07-31 08:16:22 -06002/*
3 * VFIO API definition
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <[email protected]>
Alex Williamsoncba33452012-07-31 08:16:22 -06007 */
8#ifndef VFIO_H
9#define VFIO_H
10
Alex Williamsoncba33452012-07-31 08:16:22 -060011
12#include <linux/iommu.h>
13#include <linux/mm.h>
Antonios Motakis7e992d62015-03-16 14:08:54 -060014#include <linux/workqueue.h>
15#include <linux/poll.h>
David Howells607ca462012-10-13 10:46:48 +010016#include <uapi/linux/vfio.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060017
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
Alex Williamson13060b62015-02-06 15:05:07 -070028 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060029 * @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)
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 Williamson5f3874c2020-03-24 09:28:25 -060045 int (*match)(void *device_data, char *buf);
Alex Williamsoncba33452012-07-31 08:16:22 -060046};
47
Alex Williamson03a76b62015-12-21 15:13:33 -070048extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
49extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
50
Alex Williamsoncba33452012-07-31 08:16:22 -060051extern 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);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060056extern 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);
Alex Williamsoncba33452012-07-31 08:16:22 -060059
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);
Kirti Wankhede21690372016-11-17 02:16:17 +053079 int (*pin_pages)(void *iommu_data, unsigned long *user_pfn,
80 int npage, int prot,
81 unsigned long *phys_pfn);
82 int (*unpin_pages)(void *iommu_data,
83 unsigned long *user_pfn, int npage);
Kirti Wankhedec086de812016-11-17 10:28:26 +053084 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +080085 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +053086 struct notifier_block *nb);
87 int (*unregister_notifier)(void *iommu_data,
88 struct notifier_block *nb);
Alex Williamsoncba33452012-07-31 08:16:22 -060089};
90
91extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
92
93extern void vfio_unregister_iommu_driver(
94 const struct vfio_iommu_driver_ops *ops);
95
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -060096/*
97 * External user API
98 */
99extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
100extern void vfio_group_put_external_user(struct vfio_group *group);
Alex Williamson5d6dee82017-06-28 13:50:05 -0600101extern bool vfio_external_group_match_file(struct vfio_group *group,
102 struct file *filep);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600103extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700104extern long vfio_external_check_extension(struct vfio_group *group,
105 unsigned long arg);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600106
Kirti Wankhede21690372016-11-17 02:16:17 +0530107#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
Jike Song22195cb2016-12-01 13:20:05 +0800114/* each type has independent events */
115enum vfio_notify_type {
116 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800117 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800118};
119
120/* events for VFIO_IOMMU_NOTIFY */
121#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530122
Jike Songccd46db2016-12-01 13:20:06 +0800123/* events for VFIO_GROUP_NOTIFY */
124#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
125
Kirti Wankhedec086de812016-11-17 10:28:26 +0530126extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800127 enum vfio_notify_type type,
128 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530129 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530130extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800131 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530132 struct notifier_block *nb);
133
Jike Songccd46db2016-12-01 13:20:06 +0800134struct kvm;
135extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
136
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700137/*
138 * Sub-module helpers
139 */
140struct vfio_info_cap {
141 struct vfio_info_cap_header *buf;
142 size_t size;
143};
144extern struct vfio_info_cap_header *vfio_info_cap_add(
145 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
146extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
147
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530148extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700149 struct vfio_info_cap_header *cap,
150 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530151
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530152extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
153 int num_irqs, int max_irq_type,
154 size_t *data_size);
155
Gavin Shan92d18a62014-08-08 10:36:20 -0600156struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300157#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600158extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000159extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
160extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
161 unsigned int cmd,
162 unsigned long arg);
163#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600164static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000165{
Gavin Shan1b69be52014-06-10 11:41:57 +1000166}
167
168static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
169{
170}
171
172static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
173 unsigned int cmd,
174 unsigned long arg)
175{
176 return -ENOTTY;
177}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300178#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600179
180/*
181 * IRQfd - generic
182 */
183struct virqfd {
184 void *opaque;
185 struct eventfd_ctx *eventfd;
186 int (*handler)(void *, void *);
187 void (*thread)(void *, void *);
188 void *data;
189 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200190 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600191 poll_table pt;
192 struct work_struct shutdown;
193 struct virqfd **pvirqfd;
194};
195
Antonios Motakis7e992d62015-03-16 14:08:54 -0600196extern int vfio_virqfd_enable(void *opaque,
197 int (*handler)(void *, void *),
198 void (*thread)(void *, void *),
199 void *data, struct virqfd **pvirqfd, int fd);
200extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
201
Alex Williamsoncba33452012-07-31 08:16:22 -0600202#endif /* VFIO_H */