vfio.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include <linux/iommu.h>
  14. #include <linux/mm.h>
  15. #include <uapi/linux/vfio.h>
  16. /**
  17. * struct vfio_device_ops - VFIO bus driver device callbacks
  18. *
  19. * @open: Called when userspace creates new file descriptor for device
  20. * @release: Called when userspace releases file descriptor for device
  21. * @read: Perform read(2) on device file descriptor
  22. * @write: Perform write(2) on device file descriptor
  23. * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
  24. * operations documented below
  25. * @mmap: Perform mmap(2) on a region of the device file descriptor
  26. */
  27. struct vfio_device_ops {
  28. char *name;
  29. int (*open)(void *device_data);
  30. void (*release)(void *device_data);
  31. ssize_t (*read)(void *device_data, char __user *buf,
  32. size_t count, loff_t *ppos);
  33. ssize_t (*write)(void *device_data, const char __user *buf,
  34. size_t count, loff_t *size);
  35. long (*ioctl)(void *device_data, unsigned int cmd,
  36. unsigned long arg);
  37. int (*mmap)(void *device_data, struct vm_area_struct *vma);
  38. };
  39. extern int vfio_add_group_dev(struct device *dev,
  40. const struct vfio_device_ops *ops,
  41. void *device_data);
  42. extern void *vfio_del_group_dev(struct device *dev);
  43. extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
  44. extern void vfio_device_put(struct vfio_device *device);
  45. extern void *vfio_device_data(struct vfio_device *device);
  46. /**
  47. * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
  48. */
  49. struct vfio_iommu_driver_ops {
  50. char *name;
  51. struct module *owner;
  52. void *(*open)(unsigned long arg);
  53. void (*release)(void *iommu_data);
  54. ssize_t (*read)(void *iommu_data, char __user *buf,
  55. size_t count, loff_t *ppos);
  56. ssize_t (*write)(void *iommu_data, const char __user *buf,
  57. size_t count, loff_t *size);
  58. long (*ioctl)(void *iommu_data, unsigned int cmd,
  59. unsigned long arg);
  60. int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
  61. int (*attach_group)(void *iommu_data,
  62. struct iommu_group *group);
  63. void (*detach_group)(void *iommu_data,
  64. struct iommu_group *group);
  65. };
  66. extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
  67. extern void vfio_unregister_iommu_driver(
  68. const struct vfio_iommu_driver_ops *ops);
  69. /**
  70. * offsetofend(TYPE, MEMBER)
  71. *
  72. * @TYPE: The type of the structure
  73. * @MEMBER: The member within the structure to get the end offset of
  74. *
  75. * Simple helper macro for dealing with variable sized structures passed
  76. * from user space. This allows us to easily determine if the provided
  77. * structure is sized to include various fields.
  78. */
  79. #define offsetofend(TYPE, MEMBER) ({ \
  80. TYPE tmp; \
  81. offsetof(TYPE, MEMBER) + sizeof(tmp.MEMBER); }) \
  82. /*
  83. * External user API
  84. */
  85. extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
  86. extern void vfio_group_put_external_user(struct vfio_group *group);
  87. extern int vfio_external_user_iommu_id(struct vfio_group *group);
  88. #endif /* VFIO_H */