iommu.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/of.h>
  23. #include <linux/types.h>
  24. #include <linux/scatterlist.h>
  25. #include <trace/events/iommu.h>
  26. #define IOMMU_READ (1 << 0)
  27. #define IOMMU_WRITE (1 << 1)
  28. #define IOMMU_CACHE (1 << 2) /* DMA cache coherency */
  29. #define IOMMU_NOEXEC (1 << 3)
  30. struct iommu_ops;
  31. struct iommu_group;
  32. struct bus_type;
  33. struct device;
  34. struct iommu_domain;
  35. struct notifier_block;
  36. /* iommu fault flags */
  37. #define IOMMU_FAULT_READ 0x0
  38. #define IOMMU_FAULT_WRITE 0x1
  39. typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
  40. struct device *, unsigned long, int, void *);
  41. struct iommu_domain_geometry {
  42. dma_addr_t aperture_start; /* First address that can be mapped */
  43. dma_addr_t aperture_end; /* Last address that can be mapped */
  44. bool force_aperture; /* DMA only allowed in mappable range? */
  45. };
  46. /* Domain feature flags */
  47. #define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
  48. #define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
  49. implementation */
  50. #define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */
  51. /*
  52. * This are the possible domain-types
  53. *
  54. * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate
  55. * devices
  56. * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses
  57. * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used
  58. * for VMs
  59. * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations.
  60. * This flag allows IOMMU drivers to implement
  61. * certain optimizations for these domains
  62. */
  63. #define IOMMU_DOMAIN_BLOCKED (0U)
  64. #define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
  65. #define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
  66. #define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
  67. __IOMMU_DOMAIN_DMA_API)
  68. struct iommu_domain {
  69. unsigned type;
  70. const struct iommu_ops *ops;
  71. iommu_fault_handler_t handler;
  72. void *handler_token;
  73. struct iommu_domain_geometry geometry;
  74. void *iova_cookie;
  75. };
  76. enum iommu_cap {
  77. IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA
  78. transactions */
  79. IOMMU_CAP_INTR_REMAP, /* IOMMU supports interrupt isolation */
  80. IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */
  81. };
  82. /*
  83. * Following constraints are specifc to FSL_PAMUV1:
  84. * -aperture must be power of 2, and naturally aligned
  85. * -number of windows must be power of 2, and address space size
  86. * of each window is determined by aperture size / # of windows
  87. * -the actual size of the mapped region of a window must be power
  88. * of 2 starting with 4KB and physical address must be naturally
  89. * aligned.
  90. * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
  91. * The caller can invoke iommu_domain_get_attr to check if the underlying
  92. * iommu implementation supports these constraints.
  93. */
  94. enum iommu_attr {
  95. DOMAIN_ATTR_GEOMETRY,
  96. DOMAIN_ATTR_PAGING,
  97. DOMAIN_ATTR_WINDOWS,
  98. DOMAIN_ATTR_FSL_PAMU_STASH,
  99. DOMAIN_ATTR_FSL_PAMU_ENABLE,
  100. DOMAIN_ATTR_FSL_PAMUV1,
  101. DOMAIN_ATTR_NESTING, /* two stages of translation */
  102. DOMAIN_ATTR_MAX,
  103. };
  104. /**
  105. * struct iommu_dm_region - descriptor for a direct mapped memory region
  106. * @list: Linked list pointers
  107. * @start: System physical start address of the region
  108. * @length: Length of the region in bytes
  109. * @prot: IOMMU Protection flags (READ/WRITE/...)
  110. */
  111. struct iommu_dm_region {
  112. struct list_head list;
  113. phys_addr_t start;
  114. size_t length;
  115. int prot;
  116. };
  117. #ifdef CONFIG_IOMMU_API
  118. /**
  119. * struct iommu_ops - iommu ops and capabilities
  120. * @domain_init: init iommu domain
  121. * @domain_destroy: destroy iommu domain
  122. * @attach_dev: attach device to an iommu domain
  123. * @detach_dev: detach device from an iommu domain
  124. * @map: map a physically contiguous memory region to an iommu domain
  125. * @unmap: unmap a physically contiguous memory region from an iommu domain
  126. * @map_sg: map a scatter-gather list of physically contiguous memory chunks
  127. * to an iommu domain
  128. * @iova_to_phys: translate iova to physical address
  129. * @add_device: add device to iommu grouping
  130. * @remove_device: remove device from iommu grouping
  131. * @domain_get_attr: Query domain attributes
  132. * @domain_set_attr: Change domain attributes
  133. * @of_xlate: add OF master IDs to iommu grouping
  134. * @pgsize_bitmap: bitmap of supported page sizes
  135. * @priv: per-instance data private to the iommu driver
  136. */
  137. struct iommu_ops {
  138. bool (*capable)(enum iommu_cap);
  139. /* Domain allocation and freeing by the iommu driver */
  140. struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
  141. void (*domain_free)(struct iommu_domain *);
  142. int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
  143. void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
  144. int (*map)(struct iommu_domain *domain, unsigned long iova,
  145. phys_addr_t paddr, size_t size, int prot);
  146. size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
  147. size_t size);
  148. size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova,
  149. struct scatterlist *sg, unsigned int nents, int prot);
  150. phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
  151. int (*add_device)(struct device *dev);
  152. void (*remove_device)(struct device *dev);
  153. struct iommu_group *(*device_group)(struct device *dev);
  154. int (*domain_get_attr)(struct iommu_domain *domain,
  155. enum iommu_attr attr, void *data);
  156. int (*domain_set_attr)(struct iommu_domain *domain,
  157. enum iommu_attr attr, void *data);
  158. /* Request/Free a list of direct mapping requirements for a device */
  159. void (*get_dm_regions)(struct device *dev, struct list_head *list);
  160. void (*put_dm_regions)(struct device *dev, struct list_head *list);
  161. /* Window handling functions */
  162. int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
  163. phys_addr_t paddr, u64 size, int prot);
  164. void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
  165. /* Set the numer of window per domain */
  166. int (*domain_set_windows)(struct iommu_domain *domain, u32 w_count);
  167. /* Get the numer of window per domain */
  168. u32 (*domain_get_windows)(struct iommu_domain *domain);
  169. #ifdef CONFIG_OF_IOMMU
  170. int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
  171. #endif
  172. unsigned long pgsize_bitmap;
  173. void *priv;
  174. };
  175. #define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */
  176. #define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */
  177. #define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */
  178. #define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4 /* Post Driver bind */
  179. #define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5 /* Pre Driver unbind */
  180. #define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6 /* Post Driver unbind */
  181. extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
  182. extern bool iommu_present(struct bus_type *bus);
  183. extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
  184. extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
  185. extern struct iommu_group *iommu_group_get_by_id(int id);
  186. extern void iommu_domain_free(struct iommu_domain *domain);
  187. extern int iommu_attach_device(struct iommu_domain *domain,
  188. struct device *dev);
  189. extern void iommu_detach_device(struct iommu_domain *domain,
  190. struct device *dev);
  191. extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
  192. extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
  193. phys_addr_t paddr, size_t size, int prot);
  194. extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  195. size_t size);
  196. extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  197. struct scatterlist *sg,unsigned int nents,
  198. int prot);
  199. extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
  200. extern void iommu_set_fault_handler(struct iommu_domain *domain,
  201. iommu_fault_handler_t handler, void *token);
  202. extern void iommu_get_dm_regions(struct device *dev, struct list_head *list);
  203. extern void iommu_put_dm_regions(struct device *dev, struct list_head *list);
  204. extern int iommu_request_dm_for_dev(struct device *dev);
  205. extern int iommu_attach_group(struct iommu_domain *domain,
  206. struct iommu_group *group);
  207. extern void iommu_detach_group(struct iommu_domain *domain,
  208. struct iommu_group *group);
  209. extern struct iommu_group *iommu_group_alloc(void);
  210. extern void *iommu_group_get_iommudata(struct iommu_group *group);
  211. extern void iommu_group_set_iommudata(struct iommu_group *group,
  212. void *iommu_data,
  213. void (*release)(void *iommu_data));
  214. extern int iommu_group_set_name(struct iommu_group *group, const char *name);
  215. extern int iommu_group_add_device(struct iommu_group *group,
  216. struct device *dev);
  217. extern void iommu_group_remove_device(struct device *dev);
  218. extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  219. int (*fn)(struct device *, void *));
  220. extern struct iommu_group *iommu_group_get(struct device *dev);
  221. extern void iommu_group_put(struct iommu_group *group);
  222. extern int iommu_group_register_notifier(struct iommu_group *group,
  223. struct notifier_block *nb);
  224. extern int iommu_group_unregister_notifier(struct iommu_group *group,
  225. struct notifier_block *nb);
  226. extern int iommu_group_id(struct iommu_group *group);
  227. extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
  228. extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
  229. extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
  230. void *data);
  231. extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
  232. void *data);
  233. struct device *iommu_device_create(struct device *parent, void *drvdata,
  234. const struct attribute_group **groups,
  235. const char *fmt, ...) __printf(4, 5);
  236. void iommu_device_destroy(struct device *dev);
  237. int iommu_device_link(struct device *dev, struct device *link);
  238. void iommu_device_unlink(struct device *dev, struct device *link);
  239. /* Window handling function prototypes */
  240. extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  241. phys_addr_t offset, u64 size,
  242. int prot);
  243. extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
  244. /**
  245. * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
  246. * @domain: the iommu domain where the fault has happened
  247. * @dev: the device where the fault has happened
  248. * @iova: the faulting address
  249. * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
  250. *
  251. * This function should be called by the low-level IOMMU implementations
  252. * whenever IOMMU faults happen, to allow high-level users, that are
  253. * interested in such events, to know about them.
  254. *
  255. * This event may be useful for several possible use cases:
  256. * - mere logging of the event
  257. * - dynamic TLB/PTE loading
  258. * - if restarting of the faulting device is required
  259. *
  260. * Returns 0 on success and an appropriate error code otherwise (if dynamic
  261. * PTE/TLB loading will one day be supported, implementations will be able
  262. * to tell whether it succeeded or not according to this return value).
  263. *
  264. * Specifically, -ENOSYS is returned if a fault handler isn't installed
  265. * (though fault handlers can also return -ENOSYS, in case they want to
  266. * elicit the default behavior of the IOMMU drivers).
  267. */
  268. static inline int report_iommu_fault(struct iommu_domain *domain,
  269. struct device *dev, unsigned long iova, int flags)
  270. {
  271. int ret = -ENOSYS;
  272. /*
  273. * if upper layers showed interest and installed a fault handler,
  274. * invoke it.
  275. */
  276. if (domain->handler)
  277. ret = domain->handler(domain, dev, iova, flags,
  278. domain->handler_token);
  279. trace_io_page_fault(dev, iova, flags);
  280. return ret;
  281. }
  282. static inline size_t iommu_map_sg(struct iommu_domain *domain,
  283. unsigned long iova, struct scatterlist *sg,
  284. unsigned int nents, int prot)
  285. {
  286. return domain->ops->map_sg(domain, iova, sg, nents, prot);
  287. }
  288. /* PCI device grouping function */
  289. extern struct iommu_group *pci_device_group(struct device *dev);
  290. /* Generic device grouping function */
  291. extern struct iommu_group *generic_device_group(struct device *dev);
  292. #else /* CONFIG_IOMMU_API */
  293. struct iommu_ops {};
  294. struct iommu_group {};
  295. static inline bool iommu_present(struct bus_type *bus)
  296. {
  297. return false;
  298. }
  299. static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
  300. {
  301. return false;
  302. }
  303. static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  304. {
  305. return NULL;
  306. }
  307. static inline struct iommu_group *iommu_group_get_by_id(int id)
  308. {
  309. return NULL;
  310. }
  311. static inline void iommu_domain_free(struct iommu_domain *domain)
  312. {
  313. }
  314. static inline int iommu_attach_device(struct iommu_domain *domain,
  315. struct device *dev)
  316. {
  317. return -ENODEV;
  318. }
  319. static inline void iommu_detach_device(struct iommu_domain *domain,
  320. struct device *dev)
  321. {
  322. }
  323. static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
  324. {
  325. return NULL;
  326. }
  327. static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
  328. phys_addr_t paddr, int gfp_order, int prot)
  329. {
  330. return -ENODEV;
  331. }
  332. static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  333. int gfp_order)
  334. {
  335. return -ENODEV;
  336. }
  337. static inline size_t iommu_map_sg(struct iommu_domain *domain,
  338. unsigned long iova, struct scatterlist *sg,
  339. unsigned int nents, int prot)
  340. {
  341. return -ENODEV;
  342. }
  343. static inline int iommu_domain_window_enable(struct iommu_domain *domain,
  344. u32 wnd_nr, phys_addr_t paddr,
  345. u64 size, int prot)
  346. {
  347. return -ENODEV;
  348. }
  349. static inline void iommu_domain_window_disable(struct iommu_domain *domain,
  350. u32 wnd_nr)
  351. {
  352. }
  353. static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  354. {
  355. return 0;
  356. }
  357. static inline void iommu_set_fault_handler(struct iommu_domain *domain,
  358. iommu_fault_handler_t handler, void *token)
  359. {
  360. }
  361. static inline void iommu_get_dm_regions(struct device *dev,
  362. struct list_head *list)
  363. {
  364. }
  365. static inline void iommu_put_dm_regions(struct device *dev,
  366. struct list_head *list)
  367. {
  368. }
  369. static inline int iommu_request_dm_for_dev(struct device *dev)
  370. {
  371. return -ENODEV;
  372. }
  373. static inline int iommu_attach_group(struct iommu_domain *domain,
  374. struct iommu_group *group)
  375. {
  376. return -ENODEV;
  377. }
  378. static inline void iommu_detach_group(struct iommu_domain *domain,
  379. struct iommu_group *group)
  380. {
  381. }
  382. static inline struct iommu_group *iommu_group_alloc(void)
  383. {
  384. return ERR_PTR(-ENODEV);
  385. }
  386. static inline void *iommu_group_get_iommudata(struct iommu_group *group)
  387. {
  388. return NULL;
  389. }
  390. static inline void iommu_group_set_iommudata(struct iommu_group *group,
  391. void *iommu_data,
  392. void (*release)(void *iommu_data))
  393. {
  394. }
  395. static inline int iommu_group_set_name(struct iommu_group *group,
  396. const char *name)
  397. {
  398. return -ENODEV;
  399. }
  400. static inline int iommu_group_add_device(struct iommu_group *group,
  401. struct device *dev)
  402. {
  403. return -ENODEV;
  404. }
  405. static inline void iommu_group_remove_device(struct device *dev)
  406. {
  407. }
  408. static inline int iommu_group_for_each_dev(struct iommu_group *group,
  409. void *data,
  410. int (*fn)(struct device *, void *))
  411. {
  412. return -ENODEV;
  413. }
  414. static inline struct iommu_group *iommu_group_get(struct device *dev)
  415. {
  416. return NULL;
  417. }
  418. static inline void iommu_group_put(struct iommu_group *group)
  419. {
  420. }
  421. static inline int iommu_group_register_notifier(struct iommu_group *group,
  422. struct notifier_block *nb)
  423. {
  424. return -ENODEV;
  425. }
  426. static inline int iommu_group_unregister_notifier(struct iommu_group *group,
  427. struct notifier_block *nb)
  428. {
  429. return 0;
  430. }
  431. static inline int iommu_group_id(struct iommu_group *group)
  432. {
  433. return -ENODEV;
  434. }
  435. static inline int iommu_domain_get_attr(struct iommu_domain *domain,
  436. enum iommu_attr attr, void *data)
  437. {
  438. return -EINVAL;
  439. }
  440. static inline int iommu_domain_set_attr(struct iommu_domain *domain,
  441. enum iommu_attr attr, void *data)
  442. {
  443. return -EINVAL;
  444. }
  445. static inline struct device *iommu_device_create(struct device *parent,
  446. void *drvdata,
  447. const struct attribute_group **groups,
  448. const char *fmt, ...)
  449. {
  450. return ERR_PTR(-ENODEV);
  451. }
  452. static inline void iommu_device_destroy(struct device *dev)
  453. {
  454. }
  455. static inline int iommu_device_link(struct device *dev, struct device *link)
  456. {
  457. return -EINVAL;
  458. }
  459. static inline void iommu_device_unlink(struct device *dev, struct device *link)
  460. {
  461. }
  462. #endif /* CONFIG_IOMMU_API */
  463. #endif /* __LINUX_IOMMU_H */