iommu_dev.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <linux/clk.h>
  23. #include <linux/iommu.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <mach/iommu_hw-8xxx.h>
  28. #include <mach/iommu.h>
  29. struct iommu_ctx_iter_data {
  30. /* input */
  31. const char *name;
  32. /* output */
  33. struct device *dev;
  34. };
  35. static struct platform_device *msm_iommu_root_dev;
  36. static int each_iommu_ctx(struct device *dev, void *data)
  37. {
  38. struct iommu_ctx_iter_data *res = data;
  39. struct msm_iommu_ctx_dev *c = dev->platform_data;
  40. if (!res || !c || !c->name || !res->name)
  41. return -EINVAL;
  42. if (!strcmp(res->name, c->name)) {
  43. res->dev = dev;
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. static int each_iommu(struct device *dev, void *data)
  49. {
  50. return device_for_each_child(dev, data, each_iommu_ctx);
  51. }
  52. struct device *msm_iommu_get_ctx(const char *ctx_name)
  53. {
  54. struct iommu_ctx_iter_data r;
  55. int found;
  56. if (!msm_iommu_root_dev) {
  57. pr_err("No root IOMMU device.\n");
  58. goto fail;
  59. }
  60. r.name = ctx_name;
  61. found = device_for_each_child(&msm_iommu_root_dev->dev, &r, each_iommu);
  62. if (!found) {
  63. pr_err("Could not find context <%s>\n", ctx_name);
  64. goto fail;
  65. }
  66. return r.dev;
  67. fail:
  68. return NULL;
  69. }
  70. EXPORT_SYMBOL(msm_iommu_get_ctx);
  71. static void msm_iommu_reset(void __iomem *base)
  72. {
  73. int ctx, ncb;
  74. SET_RPUE(base, 0);
  75. SET_RPUEIE(base, 0);
  76. SET_ESRRESTORE(base, 0);
  77. SET_TBE(base, 0);
  78. SET_CR(base, 0);
  79. SET_SPDMBE(base, 0);
  80. SET_TESTBUSCR(base, 0);
  81. SET_TLBRSW(base, 0);
  82. SET_GLOBAL_TLBIALL(base, 0);
  83. SET_RPU_ACR(base, 0);
  84. SET_TLBLKCRWE(base, 1);
  85. ncb = GET_NCB(base)+1;
  86. for (ctx = 0; ctx < ncb; ctx++) {
  87. SET_BPRCOSH(base, ctx, 0);
  88. SET_BPRCISH(base, ctx, 0);
  89. SET_BPRCNSH(base, ctx, 0);
  90. SET_BPSHCFG(base, ctx, 0);
  91. SET_BPMTCFG(base, ctx, 0);
  92. SET_ACTLR(base, ctx, 0);
  93. SET_SCTLR(base, ctx, 0);
  94. SET_FSRRESTORE(base, ctx, 0);
  95. SET_TTBR0(base, ctx, 0);
  96. SET_TTBR1(base, ctx, 0);
  97. SET_TTBCR(base, ctx, 0);
  98. SET_BFBCR(base, ctx, 0);
  99. SET_PAR(base, ctx, 0);
  100. SET_FAR(base, ctx, 0);
  101. SET_CTX_TLBIALL(base, ctx, 0);
  102. SET_TLBFLPTER(base, ctx, 0);
  103. SET_TLBSLPTER(base, ctx, 0);
  104. SET_TLBLKCR(base, ctx, 0);
  105. SET_PRRR(base, ctx, 0);
  106. SET_NMRR(base, ctx, 0);
  107. SET_CONTEXTIDR(base, ctx, 0);
  108. }
  109. }
  110. static int msm_iommu_probe(struct platform_device *pdev)
  111. {
  112. struct resource *r, *r2;
  113. struct clk *iommu_clk;
  114. struct msm_iommu_drvdata *drvdata;
  115. struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
  116. void __iomem *regs_base;
  117. resource_size_t len;
  118. int ret = 0, ncb, nm2v, irq;
  119. if (pdev->id != -1) {
  120. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  121. if (!drvdata) {
  122. ret = -ENOMEM;
  123. goto fail;
  124. }
  125. if (!iommu_dev) {
  126. ret = -ENODEV;
  127. goto fail;
  128. }
  129. if (iommu_dev->clk_rate != 0) {
  130. iommu_clk = clk_get(&pdev->dev, "iommu_clk");
  131. if (IS_ERR(iommu_clk)) {
  132. ret = -ENODEV;
  133. goto fail;
  134. }
  135. if (iommu_dev->clk_rate > 0) {
  136. ret = clk_set_rate(iommu_clk,
  137. iommu_dev->clk_rate);
  138. if (ret) {
  139. clk_put(iommu_clk);
  140. goto fail;
  141. }
  142. }
  143. ret = clk_enable(iommu_clk);
  144. if (ret) {
  145. clk_put(iommu_clk);
  146. goto fail;
  147. }
  148. clk_put(iommu_clk);
  149. }
  150. r = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  151. "physbase");
  152. if (!r) {
  153. ret = -ENODEV;
  154. goto fail;
  155. }
  156. len = r->end - r->start + 1;
  157. r2 = request_mem_region(r->start, len, r->name);
  158. if (!r2) {
  159. pr_err("Could not request memory region: "
  160. "start=%p, len=%d\n", (void *) r->start, len);
  161. ret = -EBUSY;
  162. goto fail;
  163. }
  164. regs_base = ioremap(r2->start, len);
  165. if (!regs_base) {
  166. pr_err("Could not ioremap: start=%p, len=%d\n",
  167. (void *) r2->start, len);
  168. ret = -EBUSY;
  169. goto fail_mem;
  170. }
  171. irq = platform_get_irq_byname(pdev, "secure_irq");
  172. if (irq < 0) {
  173. ret = -ENODEV;
  174. goto fail_io;
  175. }
  176. mb();
  177. if (GET_IDR(regs_base) == 0) {
  178. pr_err("Invalid IDR value detected\n");
  179. ret = -ENODEV;
  180. goto fail_io;
  181. }
  182. ret = request_irq(irq, msm_iommu_fault_handler, 0,
  183. "msm_iommu_secure_irpt_handler", drvdata);
  184. if (ret) {
  185. pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
  186. goto fail_io;
  187. }
  188. msm_iommu_reset(regs_base);
  189. drvdata->base = regs_base;
  190. drvdata->irq = irq;
  191. nm2v = GET_NM2VCBMT((unsigned long) regs_base);
  192. ncb = GET_NCB((unsigned long) regs_base);
  193. pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
  194. iommu_dev->name, regs_base, irq, ncb+1);
  195. platform_set_drvdata(pdev, drvdata);
  196. } else
  197. msm_iommu_root_dev = pdev;
  198. return 0;
  199. fail_io:
  200. iounmap(regs_base);
  201. fail_mem:
  202. release_mem_region(r->start, len);
  203. fail:
  204. kfree(drvdata);
  205. return ret;
  206. }
  207. static int msm_iommu_remove(struct platform_device *pdev)
  208. {
  209. struct msm_iommu_drvdata *drv = NULL;
  210. drv = platform_get_drvdata(pdev);
  211. if (drv) {
  212. memset(drv, 0, sizeof(struct msm_iommu_drvdata));
  213. kfree(drv);
  214. platform_set_drvdata(pdev, NULL);
  215. }
  216. return 0;
  217. }
  218. static int msm_iommu_ctx_probe(struct platform_device *pdev)
  219. {
  220. struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
  221. struct msm_iommu_drvdata *drvdata;
  222. struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
  223. int i, ret = 0;
  224. if (!c || !pdev->dev.parent) {
  225. ret = -EINVAL;
  226. goto fail;
  227. }
  228. drvdata = dev_get_drvdata(pdev->dev.parent);
  229. if (!drvdata) {
  230. ret = -ENODEV;
  231. goto fail;
  232. }
  233. ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
  234. if (!ctx_drvdata) {
  235. ret = -ENOMEM;
  236. goto fail;
  237. }
  238. ctx_drvdata->num = c->num;
  239. ctx_drvdata->pdev = pdev;
  240. INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
  241. platform_set_drvdata(pdev, ctx_drvdata);
  242. /* Program the M2V tables for this context */
  243. for (i = 0; i < MAX_NUM_MIDS; i++) {
  244. int mid = c->mids[i];
  245. if (mid == -1)
  246. break;
  247. SET_M2VCBR_N(drvdata->base, mid, 0);
  248. SET_CBACR_N(drvdata->base, c->num, 0);
  249. /* Set VMID = MID */
  250. SET_VMID(drvdata->base, mid, mid);
  251. /* Set the context number for that MID to this context */
  252. SET_CBNDX(drvdata->base, mid, c->num);
  253. /* Set MID associated with this context bank */
  254. SET_CBVMID(drvdata->base, c->num, mid);
  255. /* Set security bit override to be Non-secure */
  256. SET_NSCFG(drvdata->base, mid, 3);
  257. }
  258. pr_info("context device %s with bank index %d\n", c->name, c->num);
  259. return 0;
  260. fail:
  261. kfree(ctx_drvdata);
  262. return ret;
  263. }
  264. static int msm_iommu_ctx_remove(struct platform_device *pdev)
  265. {
  266. struct msm_iommu_ctx_drvdata *drv = NULL;
  267. drv = platform_get_drvdata(pdev);
  268. if (drv) {
  269. memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
  270. kfree(drv);
  271. platform_set_drvdata(pdev, NULL);
  272. }
  273. return 0;
  274. }
  275. static struct platform_driver msm_iommu_driver = {
  276. .driver = {
  277. .name = "msm_iommu",
  278. },
  279. .probe = msm_iommu_probe,
  280. .remove = msm_iommu_remove,
  281. };
  282. static struct platform_driver msm_iommu_ctx_driver = {
  283. .driver = {
  284. .name = "msm_iommu_ctx",
  285. },
  286. .probe = msm_iommu_ctx_probe,
  287. .remove = msm_iommu_ctx_remove,
  288. };
  289. static int __init msm_iommu_driver_init(void)
  290. {
  291. int ret;
  292. ret = platform_driver_register(&msm_iommu_driver);
  293. if (ret != 0) {
  294. pr_err("Failed to register IOMMU driver\n");
  295. goto error;
  296. }
  297. ret = platform_driver_register(&msm_iommu_ctx_driver);
  298. if (ret != 0) {
  299. pr_err("Failed to register IOMMU context driver\n");
  300. goto error;
  301. }
  302. error:
  303. return ret;
  304. }
  305. static void __exit msm_iommu_driver_exit(void)
  306. {
  307. platform_driver_unregister(&msm_iommu_ctx_driver);
  308. platform_driver_unregister(&msm_iommu_driver);
  309. }
  310. subsys_initcall(msm_iommu_driver_init);
  311. module_exit(msm_iommu_driver_exit);
  312. MODULE_LICENSE("GPL v2");
  313. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");