iommu.h 13 KB

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