iommu.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, void *);
  33. struct iommu_domain {
  34. struct iommu_ops *ops;
  35. void *priv;
  36. iommu_fault_handler_t handler;
  37. void *handler_token;
  38. };
  39. #define IOMMU_CAP_CACHE_COHERENCY 0x1
  40. #define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */
  41. #ifdef CONFIG_IOMMU_API
  42. /**
  43. * struct iommu_ops - iommu ops and capabilities
  44. * @domain_init: init iommu domain
  45. * @domain_destroy: destroy iommu domain
  46. * @attach_dev: attach device to an iommu domain
  47. * @detach_dev: detach device from an iommu domain
  48. * @map: map a physically contiguous memory region to an iommu domain
  49. * @unmap: unmap a physically contiguous memory region from an iommu domain
  50. * @iova_to_phys: translate iova to physical address
  51. * @domain_has_cap: domain capabilities query
  52. * @commit: commit iommu domain
  53. * @pgsize_bitmap: bitmap of supported page sizes
  54. */
  55. struct iommu_ops {
  56. int (*domain_init)(struct iommu_domain *domain);
  57. void (*domain_destroy)(struct iommu_domain *domain);
  58. int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
  59. void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
  60. int (*map)(struct iommu_domain *domain, unsigned long iova,
  61. phys_addr_t paddr, size_t size, int prot);
  62. size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
  63. size_t size);
  64. phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
  65. unsigned long iova);
  66. int (*domain_has_cap)(struct iommu_domain *domain,
  67. unsigned long cap);
  68. int (*device_group)(struct device *dev, unsigned int *groupid);
  69. unsigned long pgsize_bitmap;
  70. };
  71. extern int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops);
  72. extern bool iommu_present(struct bus_type *bus);
  73. extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
  74. extern void iommu_domain_free(struct iommu_domain *domain);
  75. extern int iommu_attach_device(struct iommu_domain *domain,
  76. struct device *dev);
  77. extern void iommu_detach_device(struct iommu_domain *domain,
  78. struct device *dev);
  79. extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
  80. phys_addr_t paddr, size_t size, int prot);
  81. extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  82. size_t size);
  83. extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  84. unsigned long iova);
  85. extern int iommu_domain_has_cap(struct iommu_domain *domain,
  86. unsigned long cap);
  87. extern void iommu_set_fault_handler(struct iommu_domain *domain,
  88. iommu_fault_handler_t handler, void *token);
  89. extern int iommu_device_group(struct device *dev, unsigned int *groupid);
  90. /**
  91. * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
  92. * @domain: the iommu domain where the fault has happened
  93. * @dev: the device where the fault has happened
  94. * @iova: the faulting address
  95. * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
  96. *
  97. * This function should be called by the low-level IOMMU implementations
  98. * whenever IOMMU faults happen, to allow high-level users, that are
  99. * interested in such events, to know about them.
  100. *
  101. * This event may be useful for several possible use cases:
  102. * - mere logging of the event
  103. * - dynamic TLB/PTE loading
  104. * - if restarting of the faulting device is required
  105. *
  106. * Returns 0 on success and an appropriate error code otherwise (if dynamic
  107. * PTE/TLB loading will one day be supported, implementations will be able
  108. * to tell whether it succeeded or not according to this return value).
  109. *
  110. * Specifically, -ENOSYS is returned if a fault handler isn't installed
  111. * (though fault handlers can also return -ENOSYS, in case they want to
  112. * elicit the default behavior of the IOMMU drivers).
  113. */
  114. static inline int report_iommu_fault(struct iommu_domain *domain,
  115. struct device *dev, unsigned long iova, int flags)
  116. {
  117. int ret = -ENOSYS;
  118. /*
  119. * if upper layers showed interest and installed a fault handler,
  120. * invoke it.
  121. */
  122. if (domain->handler)
  123. ret = domain->handler(domain, dev, iova, flags,
  124. domain->handler_token);
  125. return ret;
  126. }
  127. #else /* CONFIG_IOMMU_API */
  128. struct iommu_ops {};
  129. static inline bool iommu_present(struct bus_type *bus)
  130. {
  131. return false;
  132. }
  133. static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  134. {
  135. return NULL;
  136. }
  137. static inline void iommu_domain_free(struct iommu_domain *domain)
  138. {
  139. }
  140. static inline int iommu_attach_device(struct iommu_domain *domain,
  141. struct device *dev)
  142. {
  143. return -ENODEV;
  144. }
  145. static inline void iommu_detach_device(struct iommu_domain *domain,
  146. struct device *dev)
  147. {
  148. }
  149. static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
  150. phys_addr_t paddr, int gfp_order, int prot)
  151. {
  152. return -ENODEV;
  153. }
  154. static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  155. int gfp_order)
  156. {
  157. return -ENODEV;
  158. }
  159. static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  160. unsigned long iova)
  161. {
  162. return 0;
  163. }
  164. static inline int domain_has_cap(struct iommu_domain *domain,
  165. unsigned long cap)
  166. {
  167. return 0;
  168. }
  169. static inline void iommu_set_fault_handler(struct iommu_domain *domain,
  170. iommu_fault_handler_t handler, void *token)
  171. {
  172. }
  173. static inline int iommu_device_group(struct device *dev, unsigned int *groupid)
  174. {
  175. return -ENODEV;
  176. }
  177. #endif /* CONFIG_IOMMU_API */
  178. #endif /* __LINUX_IOMMU_H */