omap-iommu.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * omap iommu: main structures
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef _OMAP_IOMMU_H
  13. #define _OMAP_IOMMU_H
  14. #include <linux/bitops.h>
  15. #include <linux/iommu.h>
  16. #define for_each_iotlb_cr(obj, n, __i, cr) \
  17. for (__i = 0; \
  18. (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
  19. __i++)
  20. struct iotlb_entry {
  21. u32 da;
  22. u32 pa;
  23. u32 pgsz, prsvd, valid;
  24. u32 endian, elsz, mixed;
  25. };
  26. /**
  27. * struct omap_iommu_device - omap iommu device data
  28. * @pgtable: page table used by an omap iommu attached to a domain
  29. * @iommu_dev: pointer to store an omap iommu instance attached to a domain
  30. */
  31. struct omap_iommu_device {
  32. u32 *pgtable;
  33. struct omap_iommu *iommu_dev;
  34. };
  35. /**
  36. * struct omap_iommu_domain - omap iommu domain
  37. * @num_iommus: number of iommus in this domain
  38. * @iommus: omap iommu device data for all iommus in this domain
  39. * @dev: Device using this domain.
  40. * @lock: domain lock, should be taken when attaching/detaching
  41. * @domain: generic domain handle used by iommu core code
  42. */
  43. struct omap_iommu_domain {
  44. u32 num_iommus;
  45. struct omap_iommu_device *iommus;
  46. struct device *dev;
  47. spinlock_t lock;
  48. struct iommu_domain domain;
  49. };
  50. struct omap_iommu {
  51. const char *name;
  52. void __iomem *regbase;
  53. struct regmap *syscfg;
  54. struct device *dev;
  55. struct iommu_domain *domain;
  56. struct dentry *debug_dir;
  57. spinlock_t iommu_lock; /* global for this whole object */
  58. /*
  59. * We don't change iopgd for a situation like pgd for a task,
  60. * but share it globally for each iommu.
  61. */
  62. u32 *iopgd;
  63. spinlock_t page_table_lock; /* protect iopgd */
  64. dma_addr_t pd_dma;
  65. int nr_tlb_entries;
  66. void *ctx; /* iommu context: registres saved area */
  67. int has_bus_err_back;
  68. u32 id;
  69. struct iommu_device iommu;
  70. struct iommu_group *group;
  71. };
  72. /**
  73. * struct omap_iommu_arch_data - omap iommu private data
  74. * @iommu_dev: handle of the iommu device
  75. *
  76. * This is an omap iommu private data object, which binds an iommu user
  77. * to its iommu device. This object should be placed at the iommu user's
  78. * dev_archdata so generic IOMMU API can be used without having to
  79. * utilize omap-specific plumbing anymore.
  80. */
  81. struct omap_iommu_arch_data {
  82. struct omap_iommu *iommu_dev;
  83. };
  84. struct cr_regs {
  85. u32 cam;
  86. u32 ram;
  87. };
  88. struct iotlb_lock {
  89. short base;
  90. short vict;
  91. };
  92. /*
  93. * MMU Register offsets
  94. */
  95. #define MMU_REVISION 0x00
  96. #define MMU_IRQSTATUS 0x18
  97. #define MMU_IRQENABLE 0x1c
  98. #define MMU_WALKING_ST 0x40
  99. #define MMU_CNTL 0x44
  100. #define MMU_FAULT_AD 0x48
  101. #define MMU_TTB 0x4c
  102. #define MMU_LOCK 0x50
  103. #define MMU_LD_TLB 0x54
  104. #define MMU_CAM 0x58
  105. #define MMU_RAM 0x5c
  106. #define MMU_GFLUSH 0x60
  107. #define MMU_FLUSH_ENTRY 0x64
  108. #define MMU_READ_CAM 0x68
  109. #define MMU_READ_RAM 0x6c
  110. #define MMU_EMU_FAULT_AD 0x70
  111. #define MMU_GP_REG 0x88
  112. #define MMU_REG_SIZE 256
  113. /*
  114. * MMU Register bit definitions
  115. */
  116. /* IRQSTATUS & IRQENABLE */
  117. #define MMU_IRQ_MULTIHITFAULT BIT(4)
  118. #define MMU_IRQ_TABLEWALKFAULT BIT(3)
  119. #define MMU_IRQ_EMUMISS BIT(2)
  120. #define MMU_IRQ_TRANSLATIONFAULT BIT(1)
  121. #define MMU_IRQ_TLBMISS BIT(0)
  122. #define __MMU_IRQ_FAULT \
  123. (MMU_IRQ_MULTIHITFAULT | MMU_IRQ_EMUMISS | MMU_IRQ_TRANSLATIONFAULT)
  124. #define MMU_IRQ_MASK \
  125. (__MMU_IRQ_FAULT | MMU_IRQ_TABLEWALKFAULT | MMU_IRQ_TLBMISS)
  126. #define MMU_IRQ_TWL_MASK (__MMU_IRQ_FAULT | MMU_IRQ_TABLEWALKFAULT)
  127. #define MMU_IRQ_TLB_MISS_MASK (__MMU_IRQ_FAULT | MMU_IRQ_TLBMISS)
  128. /* MMU_CNTL */
  129. #define MMU_CNTL_SHIFT 1
  130. #define MMU_CNTL_MASK (7 << MMU_CNTL_SHIFT)
  131. #define MMU_CNTL_EML_TLB BIT(3)
  132. #define MMU_CNTL_TWL_EN BIT(2)
  133. #define MMU_CNTL_MMU_EN BIT(1)
  134. /* CAM */
  135. #define MMU_CAM_VATAG_SHIFT 12
  136. #define MMU_CAM_VATAG_MASK \
  137. ((~0UL >> MMU_CAM_VATAG_SHIFT) << MMU_CAM_VATAG_SHIFT)
  138. #define MMU_CAM_P BIT(3)
  139. #define MMU_CAM_V BIT(2)
  140. #define MMU_CAM_PGSZ_MASK 3
  141. #define MMU_CAM_PGSZ_1M (0 << 0)
  142. #define MMU_CAM_PGSZ_64K (1 << 0)
  143. #define MMU_CAM_PGSZ_4K (2 << 0)
  144. #define MMU_CAM_PGSZ_16M (3 << 0)
  145. /* RAM */
  146. #define MMU_RAM_PADDR_SHIFT 12
  147. #define MMU_RAM_PADDR_MASK \
  148. ((~0UL >> MMU_RAM_PADDR_SHIFT) << MMU_RAM_PADDR_SHIFT)
  149. #define MMU_RAM_ENDIAN_SHIFT 9
  150. #define MMU_RAM_ENDIAN_MASK BIT(MMU_RAM_ENDIAN_SHIFT)
  151. #define MMU_RAM_ENDIAN_LITTLE (0 << MMU_RAM_ENDIAN_SHIFT)
  152. #define MMU_RAM_ENDIAN_BIG BIT(MMU_RAM_ENDIAN_SHIFT)
  153. #define MMU_RAM_ELSZ_SHIFT 7
  154. #define MMU_RAM_ELSZ_MASK (3 << MMU_RAM_ELSZ_SHIFT)
  155. #define MMU_RAM_ELSZ_8 (0 << MMU_RAM_ELSZ_SHIFT)
  156. #define MMU_RAM_ELSZ_16 (1 << MMU_RAM_ELSZ_SHIFT)
  157. #define MMU_RAM_ELSZ_32 (2 << MMU_RAM_ELSZ_SHIFT)
  158. #define MMU_RAM_ELSZ_NONE (3 << MMU_RAM_ELSZ_SHIFT)
  159. #define MMU_RAM_MIXED_SHIFT 6
  160. #define MMU_RAM_MIXED_MASK BIT(MMU_RAM_MIXED_SHIFT)
  161. #define MMU_RAM_MIXED MMU_RAM_MIXED_MASK
  162. #define MMU_GP_REG_BUS_ERR_BACK_EN 0x1
  163. #define get_cam_va_mask(pgsz) \
  164. (((pgsz) == MMU_CAM_PGSZ_16M) ? 0xff000000 : \
  165. ((pgsz) == MMU_CAM_PGSZ_1M) ? 0xfff00000 : \
  166. ((pgsz) == MMU_CAM_PGSZ_64K) ? 0xffff0000 : \
  167. ((pgsz) == MMU_CAM_PGSZ_4K) ? 0xfffff000 : 0)
  168. /*
  169. * DSP_SYSTEM registers and bit definitions (applicable only for DRA7xx DSP)
  170. */
  171. #define DSP_SYS_REVISION 0x00
  172. #define DSP_SYS_MMU_CONFIG 0x18
  173. #define DSP_SYS_MMU_CONFIG_EN_SHIFT 4
  174. /*
  175. * utilities for super page(16MB, 1MB, 64KB and 4KB)
  176. */
  177. #define iopgsz_max(bytes) \
  178. (((bytes) >= SZ_16M) ? SZ_16M : \
  179. ((bytes) >= SZ_1M) ? SZ_1M : \
  180. ((bytes) >= SZ_64K) ? SZ_64K : \
  181. ((bytes) >= SZ_4K) ? SZ_4K : 0)
  182. #define bytes_to_iopgsz(bytes) \
  183. (((bytes) == SZ_16M) ? MMU_CAM_PGSZ_16M : \
  184. ((bytes) == SZ_1M) ? MMU_CAM_PGSZ_1M : \
  185. ((bytes) == SZ_64K) ? MMU_CAM_PGSZ_64K : \
  186. ((bytes) == SZ_4K) ? MMU_CAM_PGSZ_4K : -1)
  187. #define iopgsz_to_bytes(iopgsz) \
  188. (((iopgsz) == MMU_CAM_PGSZ_16M) ? SZ_16M : \
  189. ((iopgsz) == MMU_CAM_PGSZ_1M) ? SZ_1M : \
  190. ((iopgsz) == MMU_CAM_PGSZ_64K) ? SZ_64K : \
  191. ((iopgsz) == MMU_CAM_PGSZ_4K) ? SZ_4K : 0)
  192. #define iopgsz_ok(bytes) (bytes_to_iopgsz(bytes) >= 0)
  193. /*
  194. * global functions
  195. */
  196. struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n);
  197. void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l);
  198. void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l);
  199. #ifdef CONFIG_OMAP_IOMMU_DEBUG
  200. void omap_iommu_debugfs_init(void);
  201. void omap_iommu_debugfs_exit(void);
  202. void omap_iommu_debugfs_add(struct omap_iommu *obj);
  203. void omap_iommu_debugfs_remove(struct omap_iommu *obj);
  204. #else
  205. static inline void omap_iommu_debugfs_init(void) { }
  206. static inline void omap_iommu_debugfs_exit(void) { }
  207. static inline void omap_iommu_debugfs_add(struct omap_iommu *obj) { }
  208. static inline void omap_iommu_debugfs_remove(struct omap_iommu *obj) { }
  209. #endif
  210. /*
  211. * register accessors
  212. */
  213. static inline u32 iommu_read_reg(struct omap_iommu *obj, size_t offs)
  214. {
  215. return __raw_readl(obj->regbase + offs);
  216. }
  217. static inline void iommu_write_reg(struct omap_iommu *obj, u32 val, size_t offs)
  218. {
  219. __raw_writel(val, obj->regbase + offs);
  220. }
  221. static inline int iotlb_cr_valid(struct cr_regs *cr)
  222. {
  223. if (!cr)
  224. return -EINVAL;
  225. return cr->cam & MMU_CAM_V;
  226. }
  227. #endif /* _OMAP_IOMMU_H */