iommu.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __LINUX_IOMMU_H
  19. #define __LINUX_IOMMU_H
  20. #include <linux/errno.h>
  21. #define IOMMU_READ (1)
  22. #define IOMMU_WRITE (2)
  23. #define IOMMU_CACHE (4) /* DMA cache coherency */
  24. struct iommu_ops;
  25. struct bus_type;
  26. struct device;
  27. struct iommu_domain;
  28. /* iommu fault flags */
  29. #define IOMMU_FAULT_READ 0x0
  30. #define IOMMU_FAULT_WRITE 0x1
  31. typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
  32. struct device *, unsigned long, int);
  33. struct iommu_domain {
  34. struct iommu_ops *ops;
  35. void *priv;
  36. iommu_fault_handler_t handler;
  37. };
  38. #define IOMMU_CAP_CACHE_COHERENCY 0x1
  39. #define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */
  40. #ifdef CONFIG_IOMMU_API
  41. /**
  42. * struct iommu_ops - iommu ops and capabilities
  43. * @domain_init: init iommu domain
  44. * @domain_destroy: destroy iommu domain
  45. * @attach_dev: attach device to an iommu domain
  46. * @detach_dev: detach device from an iommu domain
  47. * @map: map a physically contiguous memory region to an iommu domain
  48. * @unmap: unmap a physically contiguous memory region from an iommu domain
  49. * @iova_to_phys: translate iova to physical address
  50. * @domain_has_cap: domain capabilities query
  51. * @commit: commit iommu domain
  52. * @pgsize_bitmap: bitmap of supported page sizes
  53. */
  54. struct iommu_ops {
  55. int (*domain_init)(struct iommu_domain *domain);
  56. void (*domain_destroy)(struct iommu_domain *domain);
  57. int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
  58. void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
  59. int (*map)(struct iommu_domain *domain, unsigned long iova,
  60. phys_addr_t paddr, size_t size, int prot);
  61. size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
  62. size_t size);
  63. phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
  64. unsigned long iova);
  65. int (*domain_has_cap)(struct iommu_domain *domain,
  66. unsigned long cap);
  67. int (*device_group)(struct device *dev, unsigned int *groupid);
  68. unsigned long pgsize_bitmap;
  69. };
  70. extern int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops);
  71. extern bool iommu_present(struct bus_type *bus);
  72. extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
  73. extern void iommu_domain_free(struct iommu_domain *domain);
  74. extern int iommu_attach_device(struct iommu_domain *domain,
  75. struct device *dev);
  76. extern void iommu_detach_device(struct iommu_domain *domain,
  77. struct device *dev);
  78. extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
  79. phys_addr_t paddr, size_t size, int prot);
  80. extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  81. size_t size);
  82. extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  83. unsigned long iova);
  84. extern int iommu_domain_has_cap(struct iommu_domain *domain,
  85. unsigned long cap);
  86. extern void iommu_set_fault_handler(struct iommu_domain *domain,
  87. iommu_fault_handler_t handler);
  88. extern int iommu_device_group(struct device *dev, unsigned int *groupid);
  89. /**
  90. * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
  91. * @domain: the iommu domain where the fault has happened
  92. * @dev: the device where the fault has happened
  93. * @iova: the faulting address
  94. * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
  95. *
  96. * This function should be called by the low-level IOMMU implementations
  97. * whenever IOMMU faults happen, to allow high-level users, that are
  98. * interested in such events, to know about them.
  99. *
  100. * This event may be useful for several possible use cases:
  101. * - mere logging of the event
  102. * - dynamic TLB/PTE loading
  103. * - if restarting of the faulting device is required
  104. *
  105. * Returns 0 on success and an appropriate error code otherwise (if dynamic
  106. * PTE/TLB loading will one day be supported, implementations will be able
  107. * to tell whether it succeeded or not according to this return value).
  108. *
  109. * Specifically, -ENOSYS is returned if a fault handler isn't installed
  110. * (though fault handlers can also return -ENOSYS, in case they want to
  111. * elicit the default behavior of the IOMMU drivers).
  112. */
  113. static inline int report_iommu_fault(struct iommu_domain *domain,
  114. struct device *dev, unsigned long iova, int flags)
  115. {
  116. int ret = -ENOSYS;
  117. /*
  118. * if upper layers showed interest and installed a fault handler,
  119. * invoke it.
  120. */
  121. if (domain->handler)
  122. ret = domain->handler(domain, dev, iova, flags);
  123. return ret;
  124. }
  125. #else /* CONFIG_IOMMU_API */
  126. struct iommu_ops {};
  127. static inline bool iommu_present(struct bus_type *bus)
  128. {
  129. return false;
  130. }
  131. static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  132. {
  133. return NULL;
  134. }
  135. static inline void iommu_domain_free(struct iommu_domain *domain)
  136. {
  137. }
  138. static inline int iommu_attach_device(struct iommu_domain *domain,
  139. struct device *dev)
  140. {
  141. return -ENODEV;
  142. }
  143. static inline void iommu_detach_device(struct iommu_domain *domain,
  144. struct device *dev)
  145. {
  146. }
  147. static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
  148. phys_addr_t paddr, int gfp_order, int prot)
  149. {
  150. return -ENODEV;
  151. }
  152. static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  153. int gfp_order)
  154. {
  155. return -ENODEV;
  156. }
  157. static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  158. unsigned long iova)
  159. {
  160. return 0;
  161. }
  162. static inline int domain_has_cap(struct iommu_domain *domain,
  163. unsigned long cap)
  164. {
  165. return 0;
  166. }
  167. static inline void iommu_set_fault_handler(struct iommu_domain *domain,
  168. iommu_fault_handler_t handler)
  169. {
  170. }
  171. static inline int iommu_device_group(struct device *dev, unsigned int *groupid)
  172. {
  173. return -ENODEV;
  174. }
  175. #endif /* CONFIG_IOMMU_API */
  176. #endif /* __LINUX_IOMMU_H */