blob: 2d67b8998fd8b49d877d65b0b94a022be47d4e28 [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>
David Howells607ca462012-10-13 10:46:48 +010017#include <uapi/linux/vfio.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060018
19/**
20 * struct vfio_device_ops - VFIO bus driver device callbacks
21 *
22 * @open: Called when userspace creates new file descriptor for device
23 * @release: Called when userspace releases file descriptor for device
24 * @read: Perform read(2) on device file descriptor
25 * @write: Perform write(2) on device file descriptor
26 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
27 * operations documented below
28 * @mmap: Perform mmap(2) on a region of the device file descriptor
Alex Williamson13060b62015-02-06 15:05:07 -070029 * @request: Request for the bus driver to release the device
Alex Williamsoncba33452012-07-31 08:16:22 -060030 */
31struct vfio_device_ops {
32 char *name;
33 int (*open)(void *device_data);
34 void (*release)(void *device_data);
35 ssize_t (*read)(void *device_data, char __user *buf,
36 size_t count, loff_t *ppos);
37 ssize_t (*write)(void *device_data, const char __user *buf,
38 size_t count, loff_t *size);
39 long (*ioctl)(void *device_data, unsigned int cmd,
40 unsigned long arg);
41 int (*mmap)(void *device_data, struct vm_area_struct *vma);
Alex Williamson13060b62015-02-06 15:05:07 -070042 void (*request)(void *device_data, unsigned int count);
Alex Williamsoncba33452012-07-31 08:16:22 -060043};
44
45extern int vfio_add_group_dev(struct device *dev,
46 const struct vfio_device_ops *ops,
47 void *device_data);
48
49extern void *vfio_del_group_dev(struct device *dev);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -060050extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
51extern void vfio_device_put(struct vfio_device *device);
52extern void *vfio_device_data(struct vfio_device *device);
Alex Williamsoncba33452012-07-31 08:16:22 -060053
54/**
55 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
56 */
57struct vfio_iommu_driver_ops {
58 char *name;
59 struct module *owner;
60 void *(*open)(unsigned long arg);
61 void (*release)(void *iommu_data);
62 ssize_t (*read)(void *iommu_data, char __user *buf,
63 size_t count, loff_t *ppos);
64 ssize_t (*write)(void *iommu_data, const char __user *buf,
65 size_t count, loff_t *size);
66 long (*ioctl)(void *iommu_data, unsigned int cmd,
67 unsigned long arg);
68 int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
69 int (*attach_group)(void *iommu_data,
70 struct iommu_group *group);
71 void (*detach_group)(void *iommu_data,
72 struct iommu_group *group);
73
74};
75
76extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
77
78extern void vfio_unregister_iommu_driver(
79 const struct vfio_iommu_driver_ops *ops);
80
81/**
82 * offsetofend(TYPE, MEMBER)
83 *
84 * @TYPE: The type of the structure
85 * @MEMBER: The member within the structure to get the end offset of
86 *
87 * Simple helper macro for dealing with variable sized structures passed
88 * from user space. This allows us to easily determine if the provided
89 * structure is sized to include various fields.
90 */
Gavin Shanb13460b2014-05-30 11:35:54 -060091#define offsetofend(TYPE, MEMBER) \
92 (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))
Alex Williamsoncba33452012-07-31 08:16:22 -060093
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -060094/*
95 * External user API
96 */
97extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
98extern void vfio_group_put_external_user(struct vfio_group *group);
99extern int vfio_external_user_iommu_id(struct vfio_group *group);
Alex Williamson88d7ab82014-02-26 11:38:39 -0700100extern long vfio_external_check_extension(struct vfio_group *group,
101 unsigned long arg);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -0600102
Gavin Shan92d18a62014-08-08 10:36:20 -0600103struct pci_dev;
Gavin Shan1b69be52014-06-10 11:41:57 +1000104#ifdef CONFIG_EEH
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600105extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000106extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
107extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
108 unsigned int cmd,
109 unsigned long arg);
110#else
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600111static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
Gavin Shan1b69be52014-06-10 11:41:57 +1000112{
Gavin Shan1b69be52014-06-10 11:41:57 +1000113}
114
115static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
116{
117}
118
119static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
120 unsigned int cmd,
121 unsigned long arg)
122{
123 return -ENOTTY;
124}
125#endif /* CONFIG_EEH */
Alex Williamsoncba33452012-07-31 08:16:22 -0600126#endif /* VFIO_H */