omap-iommu.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * omap iommu: main structures
  4. *
  5. * Copyright (C) 2008-2009 Nokia Corporation
  6. *
  7. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  8. */
  9. #ifndef _OMAP_IOMMU_H
  10. #define _OMAP_IOMMU_H
  11. #include <linux/bitops.h>
  12. #include <linux/iommu.h>
  13. #define for_each_iotlb_cr(obj, n, __i, cr) \
  14. for (__i = 0; \
  15. (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
  16. __i++)
  17. struct iotlb_entry {
  18. u32 da;
  19. u32 pa;
  20. u32 pgsz, prsvd, valid;
  21. u32 endian, elsz, mixed;
  22. };
  23. /**
  24. * struct omap_iommu_device - omap iommu device data
  25. * @pgtable: page table used by an omap iommu attached to a domain
  26. * @iommu_dev: pointer to store an omap iommu instance attached to a domain
  27. */
  28. struct omap_iommu_device {
  29. u32 *pgtable;
  30. struct omap_iommu *iommu_dev;
  31. };
  32. /**
  33. * struct omap_iommu_domain - omap iommu domain
  34. * @num_iommus: number of iommus in this domain
  35. * @iommus: omap iommu device data for all iommus in this domain
  36. * @dev: Device using this domain.
  37. * @lock: domain lock, should be taken when attaching/detaching
  38. * @domain: generic domain handle used by iommu core code
  39. */
  40. struct omap_iommu_domain {
  41. u32 num_iommus;
  42. struct omap_iommu_device *iommus;
  43. struct device *dev;
  44. spinlock_t lock;
  45. struct iommu_domain domain;
  46. };
  47. struct omap_iommu {
  48. const char *name;
  49. void __iomem *regbase;
  50. struct regmap *syscfg;
  51. struct device *dev;
  52. struct iommu_domain *domain;
  53. struct dentry *debug_dir;
  54. spinlock_t iommu_lock; /* global for this whole object */
  55. /*
  56. * We don't change iopgd for a situation like pgd for a task,
  57. * but share it globally for each iommu.
  58. */
  59. u32 *iopgd;
  60. spinlock_t page_table_lock; /* protect iopgd */
  61. dma_addr_t pd_dma;
  62. int nr_tlb_entries;
  63. void *ctx; /* iommu context: registres saved area */
  64. struct cr_regs *cr_ctx;
  65. u32 num_cr_ctx;
  66. int has_bus_err_back;
  67. u32 id;
  68. struct iommu_device iommu;
  69. bool has_iommu_driver;
  70. u8 pwrst;
  71. };
  72. /**
  73. * struct omap_iommu_arch_data - omap iommu private data
  74. * @iommu_dev: handle of the OMAP 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 */