iommu.h 10 KB

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