iommu.h 12 KB

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