iommu.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 __MACH_IOMMU_H
  13. #define __MACH_IOMMU_H
  14. #include <linux/io.h>
  15. #if defined(CONFIG_ARCH_OMAP1)
  16. #error "iommu for this processor not implemented yet"
  17. #endif
  18. struct iotlb_entry {
  19. u32 da;
  20. u32 pa;
  21. u32 pgsz, prsvd, valid;
  22. union {
  23. u16 ap;
  24. struct {
  25. u32 endian, elsz, mixed;
  26. };
  27. };
  28. };
  29. struct omap_iommu {
  30. const char *name;
  31. struct module *owner;
  32. struct clk *clk;
  33. void __iomem *regbase;
  34. struct device *dev;
  35. void *isr_priv;
  36. struct iommu_domain *domain;
  37. unsigned int refcount;
  38. spinlock_t iommu_lock; /* global for this whole object */
  39. /*
  40. * We don't change iopgd for a situation like pgd for a task,
  41. * but share it globally for each iommu.
  42. */
  43. u32 *iopgd;
  44. spinlock_t page_table_lock; /* protect iopgd */
  45. int nr_tlb_entries;
  46. struct list_head mmap;
  47. struct mutex mmap_lock; /* protect mmap */
  48. void *ctx; /* iommu context: registres saved area */
  49. u32 da_start;
  50. u32 da_end;
  51. };
  52. struct cr_regs {
  53. union {
  54. struct {
  55. u16 cam_l;
  56. u16 cam_h;
  57. };
  58. u32 cam;
  59. };
  60. union {
  61. struct {
  62. u16 ram_l;
  63. u16 ram_h;
  64. };
  65. u32 ram;
  66. };
  67. };
  68. struct iotlb_lock {
  69. short base;
  70. short vict;
  71. };
  72. /* architecture specific functions */
  73. struct iommu_functions {
  74. unsigned long version;
  75. int (*enable)(struct omap_iommu *obj);
  76. void (*disable)(struct omap_iommu *obj);
  77. void (*set_twl)(struct omap_iommu *obj, bool on);
  78. u32 (*fault_isr)(struct omap_iommu *obj, u32 *ra);
  79. void (*tlb_read_cr)(struct omap_iommu *obj, struct cr_regs *cr);
  80. void (*tlb_load_cr)(struct omap_iommu *obj, struct cr_regs *cr);
  81. struct cr_regs *(*alloc_cr)(struct omap_iommu *obj,
  82. struct iotlb_entry *e);
  83. int (*cr_valid)(struct cr_regs *cr);
  84. u32 (*cr_to_virt)(struct cr_regs *cr);
  85. void (*cr_to_e)(struct cr_regs *cr, struct iotlb_entry *e);
  86. ssize_t (*dump_cr)(struct omap_iommu *obj, struct cr_regs *cr,
  87. char *buf);
  88. u32 (*get_pte_attr)(struct iotlb_entry *e);
  89. void (*save_ctx)(struct omap_iommu *obj);
  90. void (*restore_ctx)(struct omap_iommu *obj);
  91. ssize_t (*dump_ctx)(struct omap_iommu *obj, char *buf, ssize_t len);
  92. };
  93. /**
  94. * struct omap_mmu_dev_attr - OMAP mmu device attributes for omap_hwmod
  95. * @da_start: device address where the va space starts.
  96. * @da_end: device address where the va space ends.
  97. * @nr_tlb_entries: number of entries supported by the translation
  98. * look-aside buffer (TLB).
  99. */
  100. struct omap_mmu_dev_attr {
  101. u32 da_start;
  102. u32 da_end;
  103. int nr_tlb_entries;
  104. };
  105. struct iommu_platform_data {
  106. const char *name;
  107. const char *clk_name;
  108. const int nr_tlb_entries;
  109. u32 da_start;
  110. u32 da_end;
  111. };
  112. /**
  113. * struct iommu_arch_data - omap iommu private data
  114. * @name: name of the iommu device
  115. * @iommu_dev: handle of the iommu device
  116. *
  117. * This is an omap iommu private data object, which binds an iommu user
  118. * to its iommu device. This object should be placed at the iommu user's
  119. * dev_archdata so generic IOMMU API can be used without having to
  120. * utilize omap-specific plumbing anymore.
  121. */
  122. struct omap_iommu_arch_data {
  123. const char *name;
  124. struct omap_iommu *iommu_dev;
  125. };
  126. #ifdef CONFIG_IOMMU_API
  127. /**
  128. * dev_to_omap_iommu() - retrieves an omap iommu object from a user device
  129. * @dev: iommu client device
  130. */
  131. static inline struct omap_iommu *dev_to_omap_iommu(struct device *dev)
  132. {
  133. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  134. return arch_data->iommu_dev;
  135. }
  136. #endif
  137. /* IOMMU errors */
  138. #define OMAP_IOMMU_ERR_TLB_MISS (1 << 0)
  139. #define OMAP_IOMMU_ERR_TRANS_FAULT (1 << 1)
  140. #define OMAP_IOMMU_ERR_EMU_MISS (1 << 2)
  141. #define OMAP_IOMMU_ERR_TBLWALK_FAULT (1 << 3)
  142. #define OMAP_IOMMU_ERR_MULTIHIT_FAULT (1 << 4)
  143. /*
  144. * MMU Register offsets
  145. */
  146. #define MMU_REVISION 0x00
  147. #define MMU_SYSCONFIG 0x10
  148. #define MMU_SYSSTATUS 0x14
  149. #define MMU_IRQSTATUS 0x18
  150. #define MMU_IRQENABLE 0x1c
  151. #define MMU_WALKING_ST 0x40
  152. #define MMU_CNTL 0x44
  153. #define MMU_FAULT_AD 0x48
  154. #define MMU_TTB 0x4c
  155. #define MMU_LOCK 0x50
  156. #define MMU_LD_TLB 0x54
  157. #define MMU_CAM 0x58
  158. #define MMU_RAM 0x5c
  159. #define MMU_GFLUSH 0x60
  160. #define MMU_FLUSH_ENTRY 0x64
  161. #define MMU_READ_CAM 0x68
  162. #define MMU_READ_RAM 0x6c
  163. #define MMU_EMU_FAULT_AD 0x70
  164. #define MMU_REG_SIZE 256
  165. /*
  166. * MMU Register bit definitions
  167. */
  168. #define MMU_LOCK_BASE_SHIFT 10
  169. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  170. #define MMU_LOCK_BASE(x) \
  171. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  172. #define MMU_LOCK_VICT_SHIFT 4
  173. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  174. #define MMU_LOCK_VICT(x) \
  175. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  176. #define MMU_CAM_VATAG_SHIFT 12
  177. #define MMU_CAM_VATAG_MASK \
  178. ((~0UL >> MMU_CAM_VATAG_SHIFT) << MMU_CAM_VATAG_SHIFT)
  179. #define MMU_CAM_P (1 << 3)
  180. #define MMU_CAM_V (1 << 2)
  181. #define MMU_CAM_PGSZ_MASK 3
  182. #define MMU_CAM_PGSZ_1M (0 << 0)
  183. #define MMU_CAM_PGSZ_64K (1 << 0)
  184. #define MMU_CAM_PGSZ_4K (2 << 0)
  185. #define MMU_CAM_PGSZ_16M (3 << 0)
  186. #define MMU_RAM_PADDR_SHIFT 12
  187. #define MMU_RAM_PADDR_MASK \
  188. ((~0UL >> MMU_RAM_PADDR_SHIFT) << MMU_RAM_PADDR_SHIFT)
  189. #define MMU_RAM_ENDIAN_MASK (1 << MMU_RAM_ENDIAN_SHIFT)
  190. #define MMU_RAM_ELSZ_MASK (3 << MMU_RAM_ELSZ_SHIFT)
  191. #define MMU_RAM_ELSZ_16 (1 << MMU_RAM_ELSZ_SHIFT)
  192. #define MMU_RAM_ELSZ_32 (2 << MMU_RAM_ELSZ_SHIFT)
  193. #define MMU_RAM_ELSZ_NONE (3 << MMU_RAM_ELSZ_SHIFT)
  194. #define MMU_RAM_MIXED_SHIFT 6
  195. #define MMU_RAM_MIXED_MASK (1 << MMU_RAM_MIXED_SHIFT)
  196. #define MMU_RAM_MIXED MMU_RAM_MIXED_MASK
  197. /*
  198. * utilities for super page(16MB, 1MB, 64KB and 4KB)
  199. */
  200. #define iopgsz_max(bytes) \
  201. (((bytes) >= SZ_16M) ? SZ_16M : \
  202. ((bytes) >= SZ_1M) ? SZ_1M : \
  203. ((bytes) >= SZ_64K) ? SZ_64K : \
  204. ((bytes) >= SZ_4K) ? SZ_4K : 0)
  205. #define bytes_to_iopgsz(bytes) \
  206. (((bytes) == SZ_16M) ? MMU_CAM_PGSZ_16M : \
  207. ((bytes) == SZ_1M) ? MMU_CAM_PGSZ_1M : \
  208. ((bytes) == SZ_64K) ? MMU_CAM_PGSZ_64K : \
  209. ((bytes) == SZ_4K) ? MMU_CAM_PGSZ_4K : -1)
  210. #define iopgsz_to_bytes(iopgsz) \
  211. (((iopgsz) == MMU_CAM_PGSZ_16M) ? SZ_16M : \
  212. ((iopgsz) == MMU_CAM_PGSZ_1M) ? SZ_1M : \
  213. ((iopgsz) == MMU_CAM_PGSZ_64K) ? SZ_64K : \
  214. ((iopgsz) == MMU_CAM_PGSZ_4K) ? SZ_4K : 0)
  215. #define iopgsz_ok(bytes) (bytes_to_iopgsz(bytes) >= 0)
  216. /*
  217. * global functions
  218. */
  219. extern u32 omap_iommu_arch_version(void);
  220. extern void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e);
  221. extern int
  222. omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e);
  223. extern int omap_iommu_set_isr(const char *name,
  224. int (*isr)(struct omap_iommu *obj, u32 da, u32 iommu_errs,
  225. void *priv),
  226. void *isr_priv);
  227. extern int omap_install_iommu_arch(const struct iommu_functions *ops);
  228. extern void omap_uninstall_iommu_arch(const struct iommu_functions *ops);
  229. extern int omap_foreach_iommu_device(void *data,
  230. int (*fn)(struct device *, void *));
  231. extern ssize_t
  232. omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len);
  233. extern size_t
  234. omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t len);
  235. /*
  236. * register accessors
  237. */
  238. static inline u32 iommu_read_reg(struct omap_iommu *obj, size_t offs)
  239. {
  240. return __raw_readl(obj->regbase + offs);
  241. }
  242. static inline void iommu_write_reg(struct omap_iommu *obj, u32 val, size_t offs)
  243. {
  244. __raw_writel(val, obj->regbase + offs);
  245. }
  246. #endif /* __MACH_IOMMU_H */