omap-iommu2.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * omap iommu: omap2/3 architecture specific functions
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
  7. * Paul Mundt and Toshihiro Kobayashi
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. #include <linux/io.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/module.h>
  18. #include <linux/omap-iommu.h>
  19. #include <linux/slab.h>
  20. #include <linux/stringify.h>
  21. #include <linux/platform_data/iommu-omap.h>
  22. #include "omap-iommu.h"
  23. /*
  24. * omap2 architecture specific register bit definitions
  25. */
  26. #define IOMMU_ARCH_VERSION 0x00000011
  27. /* IRQSTATUS & IRQENABLE */
  28. #define MMU_IRQ_MULTIHITFAULT (1 << 4)
  29. #define MMU_IRQ_TABLEWALKFAULT (1 << 3)
  30. #define MMU_IRQ_EMUMISS (1 << 2)
  31. #define MMU_IRQ_TRANSLATIONFAULT (1 << 1)
  32. #define MMU_IRQ_TLBMISS (1 << 0)
  33. #define __MMU_IRQ_FAULT \
  34. (MMU_IRQ_MULTIHITFAULT | MMU_IRQ_EMUMISS | MMU_IRQ_TRANSLATIONFAULT)
  35. #define MMU_IRQ_MASK \
  36. (__MMU_IRQ_FAULT | MMU_IRQ_TABLEWALKFAULT | MMU_IRQ_TLBMISS)
  37. #define MMU_IRQ_TWL_MASK (__MMU_IRQ_FAULT | MMU_IRQ_TABLEWALKFAULT)
  38. #define MMU_IRQ_TLB_MISS_MASK (__MMU_IRQ_FAULT | MMU_IRQ_TLBMISS)
  39. /* MMU_CNTL */
  40. #define MMU_CNTL_SHIFT 1
  41. #define MMU_CNTL_MASK (7 << MMU_CNTL_SHIFT)
  42. #define MMU_CNTL_EML_TLB (1 << 3)
  43. #define MMU_CNTL_TWL_EN (1 << 2)
  44. #define MMU_CNTL_MMU_EN (1 << 1)
  45. #define get_cam_va_mask(pgsz) \
  46. (((pgsz) == MMU_CAM_PGSZ_16M) ? 0xff000000 : \
  47. ((pgsz) == MMU_CAM_PGSZ_1M) ? 0xfff00000 : \
  48. ((pgsz) == MMU_CAM_PGSZ_64K) ? 0xffff0000 : \
  49. ((pgsz) == MMU_CAM_PGSZ_4K) ? 0xfffff000 : 0)
  50. /* IOMMU errors */
  51. #define OMAP_IOMMU_ERR_TLB_MISS (1 << 0)
  52. #define OMAP_IOMMU_ERR_TRANS_FAULT (1 << 1)
  53. #define OMAP_IOMMU_ERR_EMU_MISS (1 << 2)
  54. #define OMAP_IOMMU_ERR_TBLWALK_FAULT (1 << 3)
  55. #define OMAP_IOMMU_ERR_MULTIHIT_FAULT (1 << 4)
  56. static void __iommu_set_twl(struct omap_iommu *obj, bool on)
  57. {
  58. u32 l = iommu_read_reg(obj, MMU_CNTL);
  59. if (on)
  60. iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
  61. else
  62. iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
  63. l &= ~MMU_CNTL_MASK;
  64. if (on)
  65. l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
  66. else
  67. l |= (MMU_CNTL_MMU_EN);
  68. iommu_write_reg(obj, l, MMU_CNTL);
  69. }
  70. static int omap2_iommu_enable(struct omap_iommu *obj)
  71. {
  72. u32 l, pa;
  73. if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
  74. return -EINVAL;
  75. pa = virt_to_phys(obj->iopgd);
  76. if (!IS_ALIGNED(pa, SZ_16K))
  77. return -EINVAL;
  78. l = iommu_read_reg(obj, MMU_REVISION);
  79. dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
  80. (l >> 4) & 0xf, l & 0xf);
  81. iommu_write_reg(obj, pa, MMU_TTB);
  82. __iommu_set_twl(obj, true);
  83. return 0;
  84. }
  85. static void omap2_iommu_disable(struct omap_iommu *obj)
  86. {
  87. u32 l = iommu_read_reg(obj, MMU_CNTL);
  88. l &= ~MMU_CNTL_MASK;
  89. iommu_write_reg(obj, l, MMU_CNTL);
  90. dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
  91. }
  92. static void omap2_iommu_set_twl(struct omap_iommu *obj, bool on)
  93. {
  94. __iommu_set_twl(obj, false);
  95. }
  96. static u32 omap2_iommu_fault_isr(struct omap_iommu *obj, u32 *ra)
  97. {
  98. u32 stat, da;
  99. u32 errs = 0;
  100. stat = iommu_read_reg(obj, MMU_IRQSTATUS);
  101. stat &= MMU_IRQ_MASK;
  102. if (!stat) {
  103. *ra = 0;
  104. return 0;
  105. }
  106. da = iommu_read_reg(obj, MMU_FAULT_AD);
  107. *ra = da;
  108. if (stat & MMU_IRQ_TLBMISS)
  109. errs |= OMAP_IOMMU_ERR_TLB_MISS;
  110. if (stat & MMU_IRQ_TRANSLATIONFAULT)
  111. errs |= OMAP_IOMMU_ERR_TRANS_FAULT;
  112. if (stat & MMU_IRQ_EMUMISS)
  113. errs |= OMAP_IOMMU_ERR_EMU_MISS;
  114. if (stat & MMU_IRQ_TABLEWALKFAULT)
  115. errs |= OMAP_IOMMU_ERR_TBLWALK_FAULT;
  116. if (stat & MMU_IRQ_MULTIHITFAULT)
  117. errs |= OMAP_IOMMU_ERR_MULTIHIT_FAULT;
  118. iommu_write_reg(obj, stat, MMU_IRQSTATUS);
  119. return errs;
  120. }
  121. static void omap2_tlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  122. {
  123. cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
  124. cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
  125. }
  126. static void omap2_tlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  127. {
  128. iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
  129. iommu_write_reg(obj, cr->ram, MMU_RAM);
  130. }
  131. static u32 omap2_cr_to_virt(struct cr_regs *cr)
  132. {
  133. u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
  134. u32 mask = get_cam_va_mask(cr->cam & page_size);
  135. return cr->cam & mask;
  136. }
  137. static struct cr_regs *omap2_alloc_cr(struct omap_iommu *obj,
  138. struct iotlb_entry *e)
  139. {
  140. struct cr_regs *cr;
  141. if (e->da & ~(get_cam_va_mask(e->pgsz))) {
  142. dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
  143. e->da);
  144. return ERR_PTR(-EINVAL);
  145. }
  146. cr = kmalloc(sizeof(*cr), GFP_KERNEL);
  147. if (!cr)
  148. return ERR_PTR(-ENOMEM);
  149. cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
  150. cr->ram = e->pa | e->endian | e->elsz | e->mixed;
  151. return cr;
  152. }
  153. static inline int omap2_cr_valid(struct cr_regs *cr)
  154. {
  155. return cr->cam & MMU_CAM_V;
  156. }
  157. static u32 omap2_get_pte_attr(struct iotlb_entry *e)
  158. {
  159. u32 attr;
  160. attr = e->mixed << 5;
  161. attr |= e->endian;
  162. attr |= e->elsz >> 3;
  163. attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
  164. (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
  165. return attr;
  166. }
  167. static ssize_t
  168. omap2_dump_cr(struct omap_iommu *obj, struct cr_regs *cr, char *buf)
  169. {
  170. char *p = buf;
  171. /* FIXME: Need more detail analysis of cam/ram */
  172. p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
  173. (cr->cam & MMU_CAM_P) ? 1 : 0);
  174. return p - buf;
  175. }
  176. #define pr_reg(name) \
  177. do { \
  178. ssize_t bytes; \
  179. const char *str = "%20s: %08x\n"; \
  180. const int maxcol = 32; \
  181. bytes = snprintf(p, maxcol, str, __stringify(name), \
  182. iommu_read_reg(obj, MMU_##name)); \
  183. p += bytes; \
  184. len -= bytes; \
  185. if (len < maxcol) \
  186. goto out; \
  187. } while (0)
  188. static ssize_t
  189. omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
  190. {
  191. char *p = buf;
  192. pr_reg(REVISION);
  193. pr_reg(IRQSTATUS);
  194. pr_reg(IRQENABLE);
  195. pr_reg(WALKING_ST);
  196. pr_reg(CNTL);
  197. pr_reg(FAULT_AD);
  198. pr_reg(TTB);
  199. pr_reg(LOCK);
  200. pr_reg(LD_TLB);
  201. pr_reg(CAM);
  202. pr_reg(RAM);
  203. pr_reg(GFLUSH);
  204. pr_reg(FLUSH_ENTRY);
  205. pr_reg(READ_CAM);
  206. pr_reg(READ_RAM);
  207. pr_reg(EMU_FAULT_AD);
  208. out:
  209. return p - buf;
  210. }
  211. static void omap2_iommu_save_ctx(struct omap_iommu *obj)
  212. {
  213. int i;
  214. u32 *p = obj->ctx;
  215. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  216. p[i] = iommu_read_reg(obj, i * sizeof(u32));
  217. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
  218. }
  219. BUG_ON(p[0] != IOMMU_ARCH_VERSION);
  220. }
  221. static void omap2_iommu_restore_ctx(struct omap_iommu *obj)
  222. {
  223. int i;
  224. u32 *p = obj->ctx;
  225. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  226. iommu_write_reg(obj, p[i], i * sizeof(u32));
  227. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
  228. }
  229. BUG_ON(p[0] != IOMMU_ARCH_VERSION);
  230. }
  231. static void omap2_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
  232. {
  233. e->da = cr->cam & MMU_CAM_VATAG_MASK;
  234. e->pa = cr->ram & MMU_RAM_PADDR_MASK;
  235. e->valid = cr->cam & MMU_CAM_V;
  236. e->pgsz = cr->cam & MMU_CAM_PGSZ_MASK;
  237. e->endian = cr->ram & MMU_RAM_ENDIAN_MASK;
  238. e->elsz = cr->ram & MMU_RAM_ELSZ_MASK;
  239. e->mixed = cr->ram & MMU_RAM_MIXED;
  240. }
  241. static const struct iommu_functions omap2_iommu_ops = {
  242. .version = IOMMU_ARCH_VERSION,
  243. .enable = omap2_iommu_enable,
  244. .disable = omap2_iommu_disable,
  245. .set_twl = omap2_iommu_set_twl,
  246. .fault_isr = omap2_iommu_fault_isr,
  247. .tlb_read_cr = omap2_tlb_read_cr,
  248. .tlb_load_cr = omap2_tlb_load_cr,
  249. .cr_to_e = omap2_cr_to_e,
  250. .cr_to_virt = omap2_cr_to_virt,
  251. .alloc_cr = omap2_alloc_cr,
  252. .cr_valid = omap2_cr_valid,
  253. .dump_cr = omap2_dump_cr,
  254. .get_pte_attr = omap2_get_pte_attr,
  255. .save_ctx = omap2_iommu_save_ctx,
  256. .restore_ctx = omap2_iommu_restore_ctx,
  257. .dump_ctx = omap2_iommu_dump_ctx,
  258. };
  259. static int __init omap2_iommu_init(void)
  260. {
  261. return omap_install_iommu_arch(&omap2_iommu_ops);
  262. }
  263. module_init(omap2_iommu_init);
  264. static void __exit omap2_iommu_exit(void)
  265. {
  266. omap_uninstall_iommu_arch(&omap2_iommu_ops);
  267. }
  268. module_exit(omap2_iommu_exit);
  269. MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
  270. MODULE_DESCRIPTION("omap iommu: omap2/3 architecture specific functions");
  271. MODULE_LICENSE("GPL v2");