vfio.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  4. * Author: Alex Williamson <[email protected]>
  5. */
  6. #ifndef __VFIO_VFIO_H__
  7. #define __VFIO_VFIO_H__
  8. #include <linux/device.h>
  9. #include <linux/cdev.h>
  10. #include <linux/module.h>
  11. struct iommu_group;
  12. struct vfio_device;
  13. struct vfio_container;
  14. enum vfio_group_type {
  15. /*
  16. * Physical device with IOMMU backing.
  17. */
  18. VFIO_IOMMU,
  19. /*
  20. * Virtual device without IOMMU backing. The VFIO core fakes up an
  21. * iommu_group as the iommu_group sysfs interface is part of the
  22. * userspace ABI. The user of these devices must not be able to
  23. * directly trigger unmediated DMA.
  24. */
  25. VFIO_EMULATED_IOMMU,
  26. /*
  27. * Physical device without IOMMU backing. The VFIO core fakes up an
  28. * iommu_group as the iommu_group sysfs interface is part of the
  29. * userspace ABI. Users can trigger unmediated DMA by the device,
  30. * usage is highly dangerous, requires an explicit opt-in and will
  31. * taint the kernel.
  32. */
  33. VFIO_NO_IOMMU,
  34. };
  35. struct vfio_group {
  36. struct device dev;
  37. struct cdev cdev;
  38. /*
  39. * When drivers is non-zero a driver is attached to the struct device
  40. * that provided the iommu_group and thus the iommu_group is a valid
  41. * pointer. When drivers is 0 the driver is being detached. Once users
  42. * reaches 0 then the iommu_group is invalid.
  43. */
  44. refcount_t drivers;
  45. unsigned int container_users;
  46. struct iommu_group *iommu_group;
  47. struct vfio_container *container;
  48. struct list_head device_list;
  49. struct mutex device_lock;
  50. struct list_head vfio_next;
  51. struct list_head container_next;
  52. enum vfio_group_type type;
  53. struct mutex group_lock;
  54. struct kvm *kvm;
  55. struct file *opened_file;
  56. struct blocking_notifier_head notifier;
  57. };
  58. /* events for the backend driver notify callback */
  59. enum vfio_iommu_notify_type {
  60. VFIO_IOMMU_CONTAINER_CLOSE = 0,
  61. };
  62. /**
  63. * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
  64. */
  65. struct vfio_iommu_driver_ops {
  66. char *name;
  67. struct module *owner;
  68. void *(*open)(unsigned long arg);
  69. void (*release)(void *iommu_data);
  70. long (*ioctl)(void *iommu_data, unsigned int cmd,
  71. unsigned long arg);
  72. int (*attach_group)(void *iommu_data,
  73. struct iommu_group *group,
  74. enum vfio_group_type);
  75. void (*detach_group)(void *iommu_data,
  76. struct iommu_group *group);
  77. int (*pin_pages)(void *iommu_data,
  78. struct iommu_group *group,
  79. dma_addr_t user_iova,
  80. int npage, int prot,
  81. struct page **pages);
  82. void (*unpin_pages)(void *iommu_data,
  83. dma_addr_t user_iova, int npage);
  84. void (*register_device)(void *iommu_data,
  85. struct vfio_device *vdev);
  86. void (*unregister_device)(void *iommu_data,
  87. struct vfio_device *vdev);
  88. int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
  89. void *data, size_t count, bool write);
  90. struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
  91. struct iommu_group *group);
  92. void (*notify)(void *iommu_data,
  93. enum vfio_iommu_notify_type event);
  94. };
  95. struct vfio_iommu_driver {
  96. const struct vfio_iommu_driver_ops *ops;
  97. struct list_head vfio_next;
  98. };
  99. int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
  100. void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
  101. bool vfio_assert_device_open(struct vfio_device *device);
  102. struct vfio_container *vfio_container_from_file(struct file *filep);
  103. int vfio_device_assign_container(struct vfio_device *device);
  104. void vfio_device_unassign_container(struct vfio_device *device);
  105. int vfio_container_attach_group(struct vfio_container *container,
  106. struct vfio_group *group);
  107. void vfio_group_detach_container(struct vfio_group *group);
  108. void vfio_device_container_register(struct vfio_device *device);
  109. void vfio_device_container_unregister(struct vfio_device *device);
  110. long vfio_container_ioctl_check_extension(struct vfio_container *container,
  111. unsigned long arg);
  112. int __init vfio_container_init(void);
  113. void vfio_container_cleanup(void);
  114. #ifdef CONFIG_VFIO_NOIOMMU
  115. extern bool vfio_noiommu __read_mostly;
  116. #else
  117. enum { vfio_noiommu = false };
  118. #endif
  119. #endif