blob: 550c28f2ef604dbb4e109972f40eade455e98d4d [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 * @read: Perform read(2) on device file descriptor
50 * @write: Perform write(2) on device file descriptor
51 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
52 * operations documented below
53 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070054 * @request: Request for the bus driver to release the device
Alex Williamson5f3874c2020-03-24 09:28:25 -060055 * @match: Optional device name match callback (return: 0 for no-match, >0 for
56 * match, -errno for abort (ex. match with insufficient or incorrect
57 * additional args)
Jason Gunthorpe445ad492022-02-24 16:20:17 +020058 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
Alex Williamsoncba33452012-07-31 08:16:22 -060059 */
60struct vfio_device_ops {
61 char *name;
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -030062 int (*open_device)(struct vfio_device *vdev);
63 void (*close_device)(struct vfio_device *vdev);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060064 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060065 size_t count, loff_t *ppos);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060066 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
Alex Williamsoncba33452012-07-31 08:16:22 -060067 size_t count, loff_t *size);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060068 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
Alex Williamsoncba33452012-07-31 08:16:22 -060069 unsigned long arg);
Jason Gunthorpe6df62c52021-03-30 09:53:08 -060070 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
71 void (*request)(struct vfio_device *vdev, unsigned int count);
72 int (*match)(struct vfio_device *vdev, char *buf);
Jason Gunthorpe445ad492022-02-24 16:20:17 +020073 int (*device_feature)(struct vfio_device *device, u32 flags,
74 void __user *arg, size_t argsz);
Alex Williamsoncba33452012-07-31 08:16:22 -060075};
76
Jason Gunthorpe445ad492022-02-24 16:20:17 +020077/**
78 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
79 * @flags: Arg from the device_feature op
80 * @argsz: Arg from the device_feature op
81 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
82 * supports
83 * @minsz: Minimum data size the driver accepts
84 *
85 * For use in a driver's device_feature op. Checks that the inputs to the
86 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
87 * the driver should execute the get or set, otherwise the relevant
88 * value should be returned.
89 */
90static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
91 size_t minsz)
92{
93 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
94 ~supported_ops)
95 return -EINVAL;
96 if (flags & VFIO_DEVICE_FEATURE_PROBE)
97 return 0;
98 /* Without PROBE one of GET or SET must be requested */
99 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
100 return -EINVAL;
101 if (argsz < minsz)
102 return -EINVAL;
103 return 1;
104}
105
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600106void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
Jason Gunthorpe1e04ec12021-03-30 09:53:08 -0600107 const struct vfio_device_ops *ops);
Max Gurtovoyae03c372021-08-05 22:18:59 -0300108void vfio_uninit_group_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600109int vfio_register_group_dev(struct vfio_device *device);
Christoph Hellwigc68ea0d2021-09-24 17:56:57 +0200110int vfio_register_emulated_iommu_dev(struct vfio_device *device);
Jason Gunthorpe0bfc6a42021-03-30 09:53:05 -0600111void vfio_unregister_group_dev(struct vfio_device *device);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600112extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
113extern void vfio_device_put(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600114
Jason Gunthorpe2fd585f2021-08-05 22:19:00 -0300115int vfio_assign_device_set(struct vfio_device *device, void *set_id);
116
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600117/*
118 * External user API
119 */
120extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
121extern void vfio_group_put_external_user(struct vfio_group *group);
Yan Zhaoc0560f52020-03-24 09:27:56 -0600122extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
123 *dev);
Alex Williamson5d6dee82017-06-28 13:50:05 -0600124extern bool vfio_external_group_match_file(struct vfio_group *group,
125 struct file *filep);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600126extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700127extern long vfio_external_check_extension(struct vfio_group *group,
128 unsigned long arg);
Alexey Kardashevskiy6cdd97822013-08-05 10:52:36 -0600129
Kirti Wankhede21690372016-11-17 02:16:17 +0530130#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
131
132extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
133 int npage, int prot, unsigned long *phys_pfn);
134extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
135 int npage);
136
Yan Zhao40280cf2020-03-24 09:27:57 -0600137extern int vfio_group_pin_pages(struct vfio_group *group,
138 unsigned long *user_iova_pfn, int npage,
139 int prot, unsigned long *phys_pfn);
140extern int vfio_group_unpin_pages(struct vfio_group *group,
141 unsigned long *user_iova_pfn, int npage);
142
Yan Zhao8d46c0c2020-03-24 09:27:57 -0600143extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
144 void *data, size_t len, bool write);
145
Lu Baolubdfae1c2020-12-09 09:44:44 +0800146extern struct iommu_domain *vfio_group_iommu_domain(struct vfio_group *group);
147
Jike Song22195cb2016-12-01 13:20:05 +0800148/* each type has independent events */
149enum vfio_notify_type {
150 VFIO_IOMMU_NOTIFY = 0,
Jike Songccd46db2016-12-01 13:20:06 +0800151 VFIO_GROUP_NOTIFY = 1,
Jike Song22195cb2016-12-01 13:20:05 +0800152};
153
154/* events for VFIO_IOMMU_NOTIFY */
155#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
Kirti Wankhedec086de812016-11-17 10:28:26 +0530156
Jike Songccd46db2016-12-01 13:20:06 +0800157/* events for VFIO_GROUP_NOTIFY */
158#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
159
Kirti Wankhedec086de812016-11-17 10:28:26 +0530160extern int vfio_register_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800161 enum vfio_notify_type type,
162 unsigned long *required_events,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530163 struct notifier_block *nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +0530164extern int vfio_unregister_notifier(struct device *dev,
Jike Song22195cb2016-12-01 13:20:05 +0800165 enum vfio_notify_type type,
Kirti Wankhedec086de812016-11-17 10:28:26 +0530166 struct notifier_block *nb);
167
Jike Songccd46db2016-12-01 13:20:06 +0800168struct kvm;
169extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
170
Alex Williamsond7a8d5e2016-02-22 16:02:33 -0700171/*
172 * Sub-module helpers
173 */
174struct vfio_info_cap {
175 struct vfio_info_cap_header *buf;
176 size_t size;
177};
178extern struct vfio_info_cap_header *vfio_info_cap_add(
179 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
180extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
181
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530182extern int vfio_info_add_capability(struct vfio_info_cap *caps,
Alex Williamsondda01f72017-12-12 12:59:39 -0700183 struct vfio_info_cap_header *cap,
184 size_t size);
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +0530185
Kirti Wankhedec747f08a2016-11-17 02:16:27 +0530186extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
187 int num_irqs, int max_irq_type,
188 size_t *data_size);
189
Gavin Shan92d18a62014-08-08 10:36:20 -0600190struct pci_dev;
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300191#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600192extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000193extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
194extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
195 unsigned int cmd,
196 unsigned long arg);
197#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600198static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000199{
Gavin Shan1b69be52014-06-10 11:41:57 +1000200}
201
202static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
203{
204}
205
206static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
207 unsigned int cmd,
208 unsigned long arg)
209{
210 return -ENOTTY;
211}
Murilo Opsfelder Araujobb67b492017-07-18 14:22:20 -0300212#endif /* CONFIG_VFIO_SPAPR_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600213
214/*
215 * IRQfd - generic
216 */
217struct virqfd {
218 void *opaque;
219 struct eventfd_ctx *eventfd;
220 int (*handler)(void *, void *);
221 void (*thread)(void *, void *);
222 void *data;
223 struct work_struct inject;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200224 wait_queue_entry_t wait;
Antonios Motakis7e992d62015-03-16 14:08:54 -0600225 poll_table pt;
226 struct work_struct shutdown;
227 struct virqfd **pvirqfd;
228};
229
Antonios Motakis7e992d62015-03-16 14:08:54 -0600230extern int vfio_virqfd_enable(void *opaque,
231 int (*handler)(void *, void *),
232 void (*thread)(void *, void *),
233 void *data, struct virqfd **pvirqfd, int fd);
234extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
235
Alex Williamsoncba33452012-07-31 08:16:22 -0600236#endif /* VFIO_H */