iommu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/err.h>
  24. #define IOMMU_READ (1)
  25. #define IOMMU_WRITE (2)
  26. #define IOMMU_CACHE (4) /* DMA cache coherency */
  27. struct iommu_ops;
  28. struct iommu_group;
  29. struct bus_type;
  30. struct device;
  31. struct iommu_domain;
  32. /* iommu fault flags */
  33. #define IOMMU_FAULT_READ 0x0
  34. #define IOMMU_FAULT_WRITE 0x1
  35. typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
  36. struct device *, unsigned long, int, void *);
  37. struct iommu_domain {
  38. struct iommu_ops *ops;
  39. void *priv;
  40. iommu_fault_handler_t handler;
  41. void *handler_token;
  42. };
  43. #define IOMMU_CAP_CACHE_COHERENCY 0x1
  44. #define IOMMU_CAP_INTR_REMAP 0x2 /* isolates device intrs */
  45. #ifdef CONFIG_IOMMU_API
  46. /**
  47. * struct iommu_ops - iommu ops and capabilities
  48. * @domain_init: init iommu domain
  49. * @domain_destroy: destroy iommu domain
  50. * @attach_dev: attach device to an iommu domain
  51. * @detach_dev: detach device from an iommu domain
  52. * @map: map a physically contiguous memory region to an iommu domain
  53. * @unmap: unmap a physically contiguous memory region from an iommu domain
  54. * @iova_to_phys: translate iova to physical address
  55. * @domain_has_cap: domain capabilities query
  56. * @commit: commit iommu domain
  57. * @add_device: add device to iommu grouping
  58. * @remove_device: remove device from iommu grouping
  59. * @pgsize_bitmap: bitmap of supported page sizes
  60. */
  61. struct iommu_ops {
  62. int (*domain_init)(struct iommu_domain *domain, int flags);
  63. void (*domain_destroy)(struct iommu_domain *domain);
  64. int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
  65. void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
  66. int (*map)(struct iommu_domain *domain, unsigned long iova,
  67. phys_addr_t paddr, size_t size, int prot);
  68. size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
  69. size_t size);
  70. int (*map_range)(struct iommu_domain *domain, unsigned int iova,
  71. struct scatterlist *sg, unsigned int len, int prot);
  72. int (*unmap_range)(struct iommu_domain *domain, unsigned int iova,
  73. unsigned int len);
  74. phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
  75. unsigned long iova);
  76. int (*domain_has_cap)(struct iommu_domain *domain,
  77. unsigned long cap);
  78. phys_addr_t (*get_pt_base_addr)(struct iommu_domain *domain);
  79. int (*add_device)(struct device *dev);
  80. void (*remove_device)(struct device *dev);
  81. unsigned long pgsize_bitmap;
  82. };
  83. #define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
  84. #define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
  85. #define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
  86. #define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
  87. #define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
  88. #define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
  89. extern int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops);
  90. extern bool iommu_present(struct bus_type *bus);
  91. extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags);
  92. extern void iommu_domain_free(struct iommu_domain *domain);
  93. extern int iommu_attach_device(struct iommu_domain *domain,
  94. struct device *dev);
  95. extern void iommu_detach_device(struct iommu_domain *domain,
  96. struct device *dev);
  97. extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
  98. phys_addr_t paddr, size_t size, int prot);
  99. extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  100. size_t size);
  101. extern int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
  102. struct scatterlist *sg, unsigned int len, int prot);
  103. extern int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
  104. unsigned int len);
  105. extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  106. unsigned long iova);
  107. extern int iommu_domain_has_cap(struct iommu_domain *domain,
  108. unsigned long cap);
  109. extern phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain);
  110. extern void iommu_set_fault_handler(struct iommu_domain *domain,
  111. iommu_fault_handler_t handler, void *token);
  112. extern int iommu_attach_group(struct iommu_domain *domain,
  113. struct iommu_group *group);
  114. extern void iommu_detach_group(struct iommu_domain *domain,
  115. struct iommu_group *group);
  116. extern struct iommu_group *iommu_group_alloc(void);
  117. extern void *iommu_group_get_iommudata(struct iommu_group *group);
  118. extern void iommu_group_set_iommudata(struct iommu_group *group,
  119. void *iommu_data,
  120. void (*release)(void *iommu_data));
  121. extern int iommu_group_set_name(struct iommu_group *group, const char *name);
  122. extern int iommu_group_add_device(struct iommu_group *group,
  123. struct device *dev);
  124. extern void iommu_group_remove_device(struct device *dev);
  125. extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  126. int (*fn)(struct device *, void *));
  127. extern struct iommu_group *iommu_group_get(struct device *dev);
  128. extern struct iommu_group *iommu_group_find(const char *name);
  129. extern void iommu_group_put(struct iommu_group *group);
  130. extern int iommu_group_register_notifier(struct iommu_group *group,
  131. struct notifier_block *nb);
  132. extern int iommu_group_unregister_notifier(struct iommu_group *group,
  133. struct notifier_block *nb);
  134. extern int iommu_group_id(struct iommu_group *group);
  135. /**
  136. * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
  137. * @domain: the iommu domain where the fault has happened
  138. * @dev: the device where the fault has happened
  139. * @iova: the faulting address
  140. * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
  141. *
  142. * This function should be called by the low-level IOMMU implementations
  143. * whenever IOMMU faults happen, to allow high-level users, that are
  144. * interested in such events, to know about them.
  145. *
  146. * This event may be useful for several possible use cases:
  147. * - mere logging of the event
  148. * - dynamic TLB/PTE loading
  149. * - if restarting of the faulting device is required
  150. *
  151. * Returns 0 on success and an appropriate error code otherwise (if dynamic
  152. * PTE/TLB loading will one day be supported, implementations will be able
  153. * to tell whether it succeeded or not according to this return value).
  154. *
  155. * Specifically, -ENOSYS is returned if a fault handler isn't installed
  156. * (though fault handlers can also return -ENOSYS, in case they want to
  157. * elicit the default behavior of the IOMMU drivers).
  158. */
  159. static inline int report_iommu_fault(struct iommu_domain *domain,
  160. struct device *dev, unsigned long iova, int flags)
  161. {
  162. int ret = -ENOSYS;
  163. /*
  164. * if upper layers showed interest and installed a fault handler,
  165. * invoke it.
  166. */
  167. if (domain->handler)
  168. ret = domain->handler(domain, dev, iova, flags,
  169. domain->handler_token);
  170. return ret;
  171. }
  172. #else /* CONFIG_IOMMU_API */
  173. struct iommu_ops {};
  174. struct iommu_group {};
  175. static inline bool iommu_present(struct bus_type *bus)
  176. {
  177. return false;
  178. }
  179. static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags)
  180. {
  181. return NULL;
  182. }
  183. static inline void iommu_domain_free(struct iommu_domain *domain)
  184. {
  185. }
  186. static inline int iommu_attach_device(struct iommu_domain *domain,
  187. struct device *dev)
  188. {
  189. return -ENODEV;
  190. }
  191. static inline void iommu_detach_device(struct iommu_domain *domain,
  192. struct device *dev)
  193. {
  194. }
  195. static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
  196. phys_addr_t paddr, int gfp_order, int prot)
  197. {
  198. return -ENODEV;
  199. }
  200. static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  201. int gfp_order)
  202. {
  203. return -ENODEV;
  204. }
  205. static inline int iommu_map_range(struct iommu_domain *domain,
  206. unsigned int iova, struct scatterlist *sg,
  207. unsigned int len, int prot)
  208. {
  209. return -ENODEV;
  210. }
  211. static inline int iommu_unmap_range(struct iommu_domain *domain,
  212. unsigned int iova, unsigned int len)
  213. {
  214. return -ENODEV;
  215. }
  216. static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  217. unsigned long iova)
  218. {
  219. return 0;
  220. }
  221. static inline int domain_has_cap(struct iommu_domain *domain,
  222. unsigned long cap)
  223. {
  224. return 0;
  225. }
  226. static inline phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain)
  227. {
  228. return 0;
  229. }
  230. static inline void iommu_set_fault_handler(struct iommu_domain *domain,
  231. iommu_fault_handler_t handler, void *token)
  232. {
  233. }
  234. static inline int iommu_attach_group(struct iommu_domain *domain,
  235. struct iommu_group *group)
  236. {
  237. return -ENODEV;
  238. }
  239. static inline void iommu_detach_group(struct iommu_domain *domain,
  240. struct iommu_group *group)
  241. {
  242. }
  243. static inline struct iommu_group *iommu_group_alloc(void)
  244. {
  245. return ERR_PTR(-ENODEV);
  246. }
  247. static inline void *iommu_group_get_iommudata(struct iommu_group *group)
  248. {
  249. return NULL;
  250. }
  251. static inline void iommu_group_set_iommudata(struct iommu_group *group,
  252. void *iommu_data,
  253. void (*release)(void *iommu_data))
  254. {
  255. }
  256. static inline int iommu_group_set_name(struct iommu_group *group,
  257. const char *name)
  258. {
  259. return -ENODEV;
  260. }
  261. static inline int iommu_group_add_device(struct iommu_group *group,
  262. struct device *dev)
  263. {
  264. return -ENODEV;
  265. }
  266. static inline void iommu_group_remove_device(struct device *dev)
  267. {
  268. }
  269. static inline int iommu_group_for_each_dev(struct iommu_group *group,
  270. void *data,
  271. int (*fn)(struct device *, void *))
  272. {
  273. return -ENODEV;
  274. }
  275. static inline struct iommu_group *iommu_group_get(struct device *dev)
  276. {
  277. return NULL;
  278. }
  279. static inline struct iommu_group *iommu_group_find(const char *name)
  280. {
  281. return NULL;
  282. }
  283. static inline void iommu_group_put(struct iommu_group *group)
  284. {
  285. }
  286. static inline int iommu_group_register_notifier(struct iommu_group *group,
  287. struct notifier_block *nb)
  288. {
  289. return -ENODEV;
  290. }
  291. static inline int iommu_group_unregister_notifier(struct iommu_group *group,
  292. struct notifier_block *nb)
  293. {
  294. return 0;
  295. }
  296. static inline int iommu_group_id(struct iommu_group *group)
  297. {
  298. return -ENODEV;
  299. }
  300. #endif /* CONFIG_IOMMU_API */
  301. #endif /* __LINUX_IOMMU_H */