blob: 683b5146022e10e1623287f68bb8709f4af2db55 [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
47extern int vfio_add_group_dev(struct device *dev,
48 const struct vfio_device_ops *ops,
49 void *device_data);
50
51extern void *vfio_del_group_dev(struct device *dev);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060052extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
53extern void vfio_device_put(struct vfio_device *device);
54extern void *vfio_device_data(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -060055
56/**
57 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
58 */
59struct vfio_iommu_driver_ops {
60 char *name;
61 struct module *owner;
62 void *(*open)(unsigned long arg);
63 void (*release)(void *iommu_data);
64 ssize_t (*read)(void *iommu_data, char __user *buf,
65 size_t count, loff_t *ppos);
66 ssize_t (*write)(void *iommu_data, const char __user *buf,
67 size_t count, loff_t *size);
68 long (*ioctl)(void *iommu_data, unsigned int cmd,
69 unsigned long arg);
70 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
71 int (*attach_group)(void *iommu_data,
72 struct iommu_group *group);
73 void (*detach_group)(void *iommu_data,
74 struct iommu_group *group);
75
76};
77
78extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
79
80extern void vfio_unregister_iommu_driver(
81 const struct vfio_iommu_driver_ops *ops);
82
83/**
84 * offsetofend(TYPE, MEMBER)
85 *
86 * @TYPE: The type of the structure
87 * @MEMBER: The member within the structure to get the end offset of
88 *
89 * Simple helper macro for dealing with variable sized structures passed
90 * from user space. This allows us to easily determine if the provided
91 * structure is sized to include various fields.
92 */
Gavin Shanb13460b2014-05-30 11:35:54 -060093#define offsetofend(TYPE, MEMBER) \
94 (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))
Alex Williamsoncba33452012-07-31 08:16:22 -060095
Alexey Kardashevskiy6cdd9782013-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);
101extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700102extern long vfio_external_check_extension(struct vfio_group *group,
103 unsigned long arg);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600104
Gavin Shan92d18a62014-08-08 10:36:20 -0600105struct pci_dev;
Gavin Shan1b69be52014-06-10 11:41:57 +1000106#ifdef CONFIG_EEH
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600107extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000108extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
109extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
110 unsigned int cmd,
111 unsigned long arg);
112#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600113static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000114{
Gavin Shan1b69be52014-06-10 11:41:57 +1000115}
116
117static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
118{
119}
120
121static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
122 unsigned int cmd,
123 unsigned long arg)
124{
125 return -ENOTTY;
126}
127#endif /* CONFIG_EEH */
Antonios Motakis7e992d62015-03-16 14:08:54 -0600128
129/*
130 * IRQfd - generic
131 */
132struct virqfd {
133 void *opaque;
134 struct eventfd_ctx *eventfd;
135 int (*handler)(void *, void *);
136 void (*thread)(void *, void *);
137 void *data;
138 struct work_struct inject;
139 wait_queue_t wait;
140 poll_table pt;
141 struct work_struct shutdown;
142 struct virqfd **pvirqfd;
143};
144
145extern int vfio_virqfd_init(void);
146extern void vfio_virqfd_exit(void);
147extern int vfio_virqfd_enable(void *opaque,
148 int (*handler)(void *, void *),
149 void (*thread)(void *, void *),
150 void *data, struct virqfd **pvirqfd, int fd);
151extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
152
Alex Williamsoncba33452012-07-31 08:16:22 -0600153#endif /* VFIO_H */