Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1 | /* |
| 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 Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 14 | |
| 15 | #include <linux/iommu.h> |
| 16 | #include <linux/mm.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 17 | #include <uapi/linux/vfio.h> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 18 | |
| 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 |
| 29 | */ |
| 30 | struct vfio_device_ops { |
| 31 | char *name; |
| 32 | int (*open)(void *device_data); |
| 33 | void (*release)(void *device_data); |
| 34 | ssize_t (*read)(void *device_data, char __user *buf, |
| 35 | size_t count, loff_t *ppos); |
| 36 | ssize_t (*write)(void *device_data, const char __user *buf, |
| 37 | size_t count, loff_t *size); |
| 38 | long (*ioctl)(void *device_data, unsigned int cmd, |
| 39 | unsigned long arg); |
| 40 | int (*mmap)(void *device_data, struct vm_area_struct *vma); |
| 41 | }; |
| 42 | |
| 43 | extern int vfio_add_group_dev(struct device *dev, |
| 44 | const struct vfio_device_ops *ops, |
| 45 | void *device_data); |
| 46 | |
| 47 | extern void *vfio_del_group_dev(struct device *dev); |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 48 | extern struct vfio_device *vfio_device_get_from_dev(struct device *dev); |
| 49 | extern void vfio_device_put(struct vfio_device *device); |
| 50 | extern void *vfio_device_data(struct vfio_device *device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks |
| 54 | */ |
| 55 | struct vfio_iommu_driver_ops { |
| 56 | char *name; |
| 57 | struct module *owner; |
| 58 | void *(*open)(unsigned long arg); |
| 59 | void (*release)(void *iommu_data); |
| 60 | ssize_t (*read)(void *iommu_data, char __user *buf, |
| 61 | size_t count, loff_t *ppos); |
| 62 | ssize_t (*write)(void *iommu_data, const char __user *buf, |
| 63 | size_t count, loff_t *size); |
| 64 | long (*ioctl)(void *iommu_data, unsigned int cmd, |
| 65 | unsigned long arg); |
| 66 | int (*mmap)(void *iommu_data, struct vm_area_struct *vma); |
| 67 | int (*attach_group)(void *iommu_data, |
| 68 | struct iommu_group *group); |
| 69 | void (*detach_group)(void *iommu_data, |
| 70 | struct iommu_group *group); |
| 71 | |
| 72 | }; |
| 73 | |
| 74 | extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); |
| 75 | |
| 76 | extern void vfio_unregister_iommu_driver( |
| 77 | const struct vfio_iommu_driver_ops *ops); |
| 78 | |
| 79 | /** |
| 80 | * offsetofend(TYPE, MEMBER) |
| 81 | * |
| 82 | * @TYPE: The type of the structure |
| 83 | * @MEMBER: The member within the structure to get the end offset of |
| 84 | * |
| 85 | * Simple helper macro for dealing with variable sized structures passed |
| 86 | * from user space. This allows us to easily determine if the provided |
| 87 | * structure is sized to include various fields. |
| 88 | */ |
Gavin Shan | b13460b | 2014-05-30 11:35:54 -0600 | [diff] [blame] | 89 | #define offsetofend(TYPE, MEMBER) \ |
| 90 | (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER)) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 91 | |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 92 | /* |
| 93 | * External user API |
| 94 | */ |
| 95 | extern struct vfio_group *vfio_group_get_external_user(struct file *filep); |
| 96 | extern void vfio_group_put_external_user(struct vfio_group *group); |
| 97 | extern int vfio_external_user_iommu_id(struct vfio_group *group); |
Alex Williamson | 88d7ab8 | 2014-02-26 11:38:39 -0700 | [diff] [blame] | 98 | extern long vfio_external_check_extension(struct vfio_group *group, |
| 99 | unsigned long arg); |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 100 | |
Gavin Shan | 92d18a6 | 2014-08-08 10:36:20 -0600 | [diff] [blame] | 101 | struct pci_dev; |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 102 | #ifdef CONFIG_EEH |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 103 | extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev); |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 104 | extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev); |
| 105 | extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 106 | unsigned int cmd, |
| 107 | unsigned long arg); |
| 108 | #else |
Alexey Kardashevskiy | 9b936c9 | 2014-08-08 10:39:16 -0600 | [diff] [blame] | 109 | static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev) |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 110 | { |
Gavin Shan | 1b69be5 | 2014-06-10 11:41:57 +1000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, |
| 118 | unsigned int cmd, |
| 119 | unsigned long arg) |
| 120 | { |
| 121 | return -ENOTTY; |
| 122 | } |
| 123 | #endif /* CONFIG_EEH */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 124 | #endif /* VFIO_H */ |