blob: f0e6a72875e471995cca5eadd4de431cf6208f10 [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
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030018/*
19 * VFIO devices can be placed in a set, this allows all devices to share this
20 * structure and the VFIO core will provide a lock that is held around
21 * open_device()/close_device() for all devices in the set.
22 */
23struct vfio_device_set {
24 void *set_id;
25 struct mutex lock;
26 struct list_head device_list;
27 unsigned int device_count;
28};
29
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060030struct vfio_device {
31 struct device *dev;
32 const struct vfio_device_ops *ops;
33 struct vfio_group *group;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030034 struct vfio_device_set *dev_set;
35 struct list_head dev_set_list;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060036
37 /* Members below here are private, not for driver use */
38 refcount_t refcount;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030039 unsigned int open_count;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060040 struct completion comp;
41 struct list_head group_next;
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060042};
43
Alex Williamsoncba33452012-07-31 08:16:22 -060044/**
45 * struct vfio_device_ops - VFIO bus driver device callbacks
46 *
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030047 * @open_device: Called when the first file descriptor is opened for this device
48 * @close_device: Opposite of open_device
Alex Williamsoncba33452012-07-31 08:16:22 -060049 * @open: Called when userspace creates new file descriptor for device
50 * @release: Called when userspace releases file descriptor for device
51 * @read: Perform read(2) on device file descriptor
52 * @write: Perform write(2) on device file descriptor
53 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
54 * operations documented below
55 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070056 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060057 * @match: Optional device name match callback (return: 0 for no-match, >0 for
58 * match, -errno for abort (ex. match with insufficient or incorrect
59 * additional args)
Alex Williamsoncba33452012-07-31 08:16:22 -060060 */
61struct vfio_device_ops {
62 char *name;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030063 int (*open_device)(struct vfio_device *vdev);
64 void (*close_device)(struct vfio_device *vdev);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060065 int (*open)(struct vfio_device *vdev);
66 void (*release)(struct vfio_device *vdev);
67 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060068 size_t count, loff_t *ppos);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060069 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060070 size_t count, loff_t *size);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060071 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
Alex Williamsoncba33452012-07-31 08:16:22 -060072 unsigned long arg);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060073 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
74 void (*request)(struct vfio_device *vdev, unsigned int count);
75 int (*match)(struct vfio_device *vdev, char *buf);
Alex Williamsoncba33452012-07-31 08:16:22 -060076};
77
Alex Williamson03a76b62015-12-21 15:13:33 -070078extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
79extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
80
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060081void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
Jason Gunthorpe1e04ec12021-03-30 09:53:08 -060082 const struct vfio_device_ops *ops);
Max Gurtovoyae03c372021-08-05 22:18:59 -030083void vfio_uninit_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060084int vfio_register_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -060085void vfio_unregister_group_dev(struct vfio_device *device);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060086extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
87extern void vfio_device_put(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -060088
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030089int vfio_assign_device_set(struct vfio_device *device, void *set_id);
90
Steve Sistareec5e32942021-01-29 08:54:10 -080091/* events for the backend driver notify callback */
92enum vfio_iommu_notify_type {
93 VFIO_IOMMU_CONTAINER_CLOSE = 0,
94};
95
Alex Williamsoncba33452012-07-31 08:16:22 -060096/**
97 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
98 */
99struct vfio_iommu_driver_ops {
100 char *name;
101 struct module *owner;
102 void *(*open)(unsigned long arg);
103 void (*release)(void *iommu_data);
104 ssize_t (*read)(void *iommu_data, char __user *buf,
105 size_t count, loff_t *ppos);
106 ssize_t (*write)(void *iommu_data, const char __user *buf,
107 size_t count, loff_t *size);
108 long (*ioctl)(void *iommu_data, unsigned int cmd,
109 unsigned long arg);
110 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
111 int (*attach_group)(void *iommu_data,
112 struct iommu_group *group);
113 void (*detach_group)(void *iommu_data,
114 struct iommu_group *group);
Kirti Wankhede95fc87b4412020-05-29 02:00:54 +0530115 int (*pin_pages)(void *iommu_data,
116 struct iommu_group *group,
117 unsigned long *user_pfn,
Kirti Wankhede21690372016-11-17 02:16:17 +0530118 int npage, int prot,
119 unsigned long *phys_pfn);
120 int (*unpin_pages)(void *iommu_data,
121 unsigned long *user_pfn, int npage);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530122 int (*register_notifier)(void *iommu_data,
Jike Song22195cb2016-12-01 13:20:05 +0800123 unsigned long *events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530124 struct notifier_block *nb);
125 int (*unregister_notifier)(void *iommu_data,
126 struct notifier_block *nb);
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600127 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
128 void *data, size_t count, bool write);
Lu Baolubdfae1c2020-12-09 09:44:44 +0800129 struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
130 struct iommu_group *group);
Steve Sistareec5e32942021-01-29 08:54:10 -0800131 void (*notify)(void *iommu_data,
132 enum vfio_iommu_notify_type event);
Alex Williamsoncba33452012-07-31 08:16:22 -0600133};
134
135extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
136
137extern void vfio_unregister_iommu_driver(
138 const struct vfio_iommu_driver_ops *ops);
139
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600140/*
141 * External user API
142 */
143extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
144extern void vfio_group_put_external_user(struct vfio_group *group);
Yan Zhaoc0560f52020-03-24 09:27:56 -0600145extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
146 *dev);
Alex Williamson5d6dee82017-06-28 13:50:05 -0600147extern bool vfio_external_group_match_file(struct vfio_group *group,
148 struct file *filep);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600149extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700150extern long vfio_external_check_extension(struct vfio_group *group,
151 unsigned long arg);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600152
Kirti Wankhede21690372016-11-17 02:16:17 +0530153#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
154
155extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
156 int npage, int prot, unsigned long *phys_pfn);
157extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
158 int npage);
159
Yan Zhao40280cf2020-03-24 09:27:57 -0600160extern int vfio_group_pin_pages(struct vfio_group *group,
161 unsigned long *user_iova_pfn, int npage,
162 int prot, unsigned long *phys_pfn);
163extern int vfio_group_unpin_pages(struct vfio_group *group,
164 unsigned long *user_iova_pfn, int npage);
165
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600166extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
167 void *data, size_t len, bool write);
168
Lu Baolubdfae1c2020-12-09 09:44:44 +0800169extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
170
Jike Song22195cb2016-12-01 13:20:05 +0800171/* each type has independent events */
172enum vfio_notify_type {
173 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800174 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800175};
176
177/* events for VFIO_IOMMU_NOTIFY */
178#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530179
Jike Songccd46db2016-12-01 13:20:06 +0800180/* events for VFIO_GROUP_NOTIFY */
181#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
182
Kirti Wankhedec086de812016-11-17 10:28:26 +0530183extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800184 enum vfio_notify_type type,
185 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530186 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530187extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800188 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530189 struct notifier_block *nb);
190
Jike Songccd46db2016-12-01 13:20:06 +0800191struct kvm;
192extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
193
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700194/*
195 * Sub-module helpers
196 */
197struct vfio_info_cap {
198 struct vfio_info_cap_header *buf;
199 size_t size;
200};
201extern struct vfio_info_cap_header *vfio_info_cap_add(
202 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
203extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
204
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530205extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700206 struct vfio_info_cap_header *cap,
207 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530208
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530209extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
210 int num_irqs, int max_irq_type,
211 size_t *data_size);
212
Gavin Shan92d18a62014-08-08 10:36:20 -0600213struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300214#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600215extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000216extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
217extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
218 unsigned int cmd,
219 unsigned long arg);
220#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600221static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000222{
Gavin Shan1b69be52014-06-10 11:41:57 +1000223}
224
225static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
226{
227}
228
229static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
230 unsigned int cmd,
231 unsigned long arg)
232{
233 return -ENOTTY;
234}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300235#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600236
237/*
238 * IRQfd - generic
239 */
240struct virqfd {
241 void *opaque;
242 struct eventfd_ctx *eventfd;
243 int (*handler)(void *, void *);
244 void (*thread)(void *, void *);
245 void *data;
246 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200247 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600248 poll_table pt;
249 struct work_struct shutdown;
250 struct virqfd **pvirqfd;
251};
252
Antonios Motakis7e992d62015-03-16 14:08:54 -0600253extern int vfio_virqfd_enable(void *opaque,
254 int (*handler)(void *, void *),
255 void (*thread)(void *, void *),
256 void *data, struct virqfd **pvirqfd, int fd);
257extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
258
Alex Williamsoncba33452012-07-31 08:16:22 -0600259#endif /* VFIO_H */