iommu2.c 8.3 KB

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