iommu.h 7.1 KB

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