iommu_dev.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* Copyright (c) 2010-2011, 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. #include <mach/clk.h>
  30. struct iommu_ctx_iter_data {
  31. /* input */
  32. const char *name;
  33. /* output */
  34. struct device *dev;
  35. };
  36. static struct platform_device *msm_iommu_root_dev;
  37. static int each_iommu_ctx(struct device *dev, void *data)
  38. {
  39. struct iommu_ctx_iter_data *res = data;
  40. struct msm_iommu_ctx_dev *c = dev->platform_data;
  41. if (!res || !c || !c->name || !res->name)
  42. return -EINVAL;
  43. if (!strcmp(res->name, c->name)) {
  44. res->dev = dev;
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. static int each_iommu(struct device *dev, void *data)
  50. {
  51. return device_for_each_child(dev, data, each_iommu_ctx);
  52. }
  53. struct device *msm_iommu_get_ctx(const char *ctx_name)
  54. {
  55. struct iommu_ctx_iter_data r;
  56. int found;
  57. if (!msm_iommu_root_dev) {
  58. pr_err("No root IOMMU device.\n");
  59. goto fail;
  60. }
  61. r.name = ctx_name;
  62. found = device_for_each_child(&msm_iommu_root_dev->dev, &r, each_iommu);
  63. if (!found) {
  64. pr_err("Could not find context <%s>\n", ctx_name);
  65. goto fail;
  66. }
  67. return r.dev;
  68. fail:
  69. return NULL;
  70. }
  71. EXPORT_SYMBOL(msm_iommu_get_ctx);
  72. static void msm_iommu_reset(void __iomem *base)
  73. {
  74. int ctx, ncb;
  75. SET_RPUE(base, 0);
  76. SET_RPUEIE(base, 0);
  77. SET_ESRRESTORE(base, 0);
  78. SET_TBE(base, 0);
  79. SET_CR(base, 0);
  80. SET_SPDMBE(base, 0);
  81. SET_TESTBUSCR(base, 0);
  82. SET_TLBRSW(base, 0);
  83. SET_GLOBAL_TLBIALL(base, 0);
  84. SET_RPU_ACR(base, 0);
  85. SET_TLBLKCRWE(base, 1);
  86. ncb = GET_NCB(base)+1;
  87. for (ctx = 0; ctx < ncb; ctx++) {
  88. SET_BPRCOSH(base, ctx, 0);
  89. SET_BPRCISH(base, ctx, 0);
  90. SET_BPRCNSH(base, ctx, 0);
  91. SET_BPSHCFG(base, ctx, 0);
  92. SET_BPMTCFG(base, ctx, 0);
  93. SET_ACTLR(base, ctx, 0);
  94. SET_SCTLR(base, ctx, 0);
  95. SET_FSRRESTORE(base, ctx, 0);
  96. SET_TTBR0(base, ctx, 0);
  97. SET_TTBR1(base, ctx, 0);
  98. SET_TTBCR(base, ctx, 0);
  99. SET_BFBCR(base, ctx, 0);
  100. SET_PAR(base, ctx, 0);
  101. SET_FAR(base, ctx, 0);
  102. SET_CTX_TLBIALL(base, ctx, 0);
  103. SET_TLBFLPTER(base, ctx, 0);
  104. SET_TLBSLPTER(base, ctx, 0);
  105. SET_TLBLKCR(base, ctx, 0);
  106. SET_PRRR(base, ctx, 0);
  107. SET_NMRR(base, ctx, 0);
  108. SET_CONTEXTIDR(base, ctx, 0);
  109. }
  110. }
  111. static int msm_iommu_probe(struct platform_device *pdev)
  112. {
  113. struct resource *r, *r2;
  114. struct clk *iommu_clk;
  115. struct clk *iommu_pclk;
  116. struct msm_iommu_drvdata *drvdata;
  117. struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
  118. void __iomem *regs_base;
  119. resource_size_t len;
  120. int ret, ncb, nm2v, irq;
  121. if (pdev->id == -1) {
  122. msm_iommu_root_dev = pdev;
  123. return 0;
  124. }
  125. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  126. if (!drvdata) {
  127. ret = -ENOMEM;
  128. goto fail;
  129. }
  130. if (!iommu_dev) {
  131. ret = -ENODEV;
  132. goto fail;
  133. }
  134. iommu_pclk = clk_get(NULL, "smmu_pclk");
  135. if (IS_ERR(iommu_pclk)) {
  136. ret = -ENODEV;
  137. goto fail;
  138. }
  139. ret = clk_enable(iommu_pclk);
  140. if (ret)
  141. goto fail_enable;
  142. iommu_clk = clk_get(&pdev->dev, "iommu_clk");
  143. if (!IS_ERR(iommu_clk)) {
  144. if (clk_get_rate(iommu_clk) == 0)
  145. clk_set_min_rate(iommu_clk, 1);
  146. ret = clk_enable(iommu_clk);
  147. if (ret) {
  148. clk_put(iommu_clk);
  149. goto fail_pclk;
  150. }
  151. } else
  152. iommu_clk = NULL;
  153. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
  154. if (!r) {
  155. ret = -ENODEV;
  156. goto fail_clk;
  157. }
  158. len = resource_size(r);
  159. r2 = request_mem_region(r->start, len, r->name);
  160. if (!r2) {
  161. pr_err("Could not request memory region: start=%p, len=%d\n",
  162. (void *) r->start, len);
  163. ret = -EBUSY;
  164. goto fail_clk;
  165. }
  166. regs_base = ioremap(r2->start, len);
  167. if (!regs_base) {
  168. pr_err("Could not ioremap: start=%p, len=%d\n",
  169. (void *) r2->start, len);
  170. ret = -EBUSY;
  171. goto fail_mem;
  172. }
  173. irq = platform_get_irq_byname(pdev, "secure_irq");
  174. if (irq < 0) {
  175. ret = -ENODEV;
  176. goto fail_io;
  177. }
  178. mb();
  179. if (GET_IDR(regs_base) == 0) {
  180. pr_err("Invalid IDR value detected\n");
  181. ret = -ENODEV;
  182. goto fail_io;
  183. }
  184. ret = request_irq(irq, msm_iommu_fault_handler, 0,
  185. "msm_iommu_secure_irpt_handler", drvdata);
  186. if (ret) {
  187. pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
  188. goto fail_io;
  189. }
  190. msm_iommu_reset(regs_base);
  191. drvdata->pclk = iommu_pclk;
  192. drvdata->clk = iommu_clk;
  193. drvdata->base = regs_base;
  194. drvdata->irq = irq;
  195. nm2v = GET_NM2VCBMT((unsigned long) regs_base);
  196. ncb = GET_NCB((unsigned long) regs_base);
  197. pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
  198. iommu_dev->name, regs_base, irq, ncb+1);
  199. platform_set_drvdata(pdev, drvdata);
  200. if (iommu_clk)
  201. clk_disable(iommu_clk);
  202. clk_disable(iommu_pclk);
  203. return 0;
  204. fail_io:
  205. iounmap(regs_base);
  206. fail_mem:
  207. release_mem_region(r->start, len);
  208. fail_clk:
  209. if (iommu_clk) {
  210. clk_disable(iommu_clk);
  211. clk_put(iommu_clk);
  212. }
  213. fail_pclk:
  214. clk_disable(iommu_pclk);
  215. fail_enable:
  216. clk_put(iommu_pclk);
  217. fail:
  218. kfree(drvdata);
  219. return ret;
  220. }
  221. static int msm_iommu_remove(struct platform_device *pdev)
  222. {
  223. struct msm_iommu_drvdata *drv = NULL;
  224. drv = platform_get_drvdata(pdev);
  225. if (drv) {
  226. if (drv->clk)
  227. clk_put(drv->clk);
  228. clk_put(drv->pclk);
  229. memset(drv, 0, sizeof(*drv));
  230. kfree(drv);
  231. platform_set_drvdata(pdev, NULL);
  232. }
  233. return 0;
  234. }
  235. static int msm_iommu_ctx_probe(struct platform_device *pdev)
  236. {
  237. struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
  238. struct msm_iommu_drvdata *drvdata;
  239. struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
  240. int i, ret;
  241. if (!c || !pdev->dev.parent) {
  242. ret = -EINVAL;
  243. goto fail;
  244. }
  245. drvdata = dev_get_drvdata(pdev->dev.parent);
  246. if (!drvdata) {
  247. ret = -ENODEV;
  248. goto fail;
  249. }
  250. ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
  251. if (!ctx_drvdata) {
  252. ret = -ENOMEM;
  253. goto fail;
  254. }
  255. ctx_drvdata->num = c->num;
  256. ctx_drvdata->pdev = pdev;
  257. INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
  258. platform_set_drvdata(pdev, ctx_drvdata);
  259. ret = clk_enable(drvdata->pclk);
  260. if (ret)
  261. goto fail;
  262. if (drvdata->clk) {
  263. ret = clk_enable(drvdata->clk);
  264. if (ret) {
  265. clk_disable(drvdata->pclk);
  266. goto fail;
  267. }
  268. }
  269. /* Program the M2V tables for this context */
  270. for (i = 0; i < MAX_NUM_MIDS; i++) {
  271. int mid = c->mids[i];
  272. if (mid == -1)
  273. break;
  274. SET_M2VCBR_N(drvdata->base, mid, 0);
  275. SET_CBACR_N(drvdata->base, c->num, 0);
  276. /* Set VMID = MID */
  277. SET_VMID(drvdata->base, mid, mid);
  278. /* Set the context number for that MID to this context */
  279. SET_CBNDX(drvdata->base, mid, c->num);
  280. /* Set MID associated with this context bank */
  281. SET_CBVMID(drvdata->base, c->num, mid);
  282. /* Set security bit override to be Non-secure */
  283. SET_NSCFG(drvdata->base, mid, 3);
  284. }
  285. if (drvdata->clk)
  286. clk_disable(drvdata->clk);
  287. clk_disable(drvdata->pclk);
  288. dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
  289. return 0;
  290. fail:
  291. kfree(ctx_drvdata);
  292. return ret;
  293. }
  294. static int msm_iommu_ctx_remove(struct platform_device *pdev)
  295. {
  296. struct msm_iommu_ctx_drvdata *drv = NULL;
  297. drv = platform_get_drvdata(pdev);
  298. if (drv) {
  299. memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
  300. kfree(drv);
  301. platform_set_drvdata(pdev, NULL);
  302. }
  303. return 0;
  304. }
  305. static struct platform_driver msm_iommu_driver = {
  306. .driver = {
  307. .name = "msm_iommu",
  308. },
  309. .probe = msm_iommu_probe,
  310. .remove = msm_iommu_remove,
  311. };
  312. static struct platform_driver msm_iommu_ctx_driver = {
  313. .driver = {
  314. .name = "msm_iommu_ctx",
  315. },
  316. .probe = msm_iommu_ctx_probe,
  317. .remove = msm_iommu_ctx_remove,
  318. };
  319. static int __init msm_iommu_driver_init(void)
  320. {
  321. int ret;
  322. ret = platform_driver_register(&msm_iommu_driver);
  323. if (ret != 0) {
  324. pr_err("Failed to register IOMMU driver\n");
  325. goto error;
  326. }
  327. ret = platform_driver_register(&msm_iommu_ctx_driver);
  328. if (ret != 0) {
  329. pr_err("Failed to register IOMMU context driver\n");
  330. goto error;
  331. }
  332. error:
  333. return ret;
  334. }
  335. static void __exit msm_iommu_driver_exit(void)
  336. {
  337. platform_driver_unregister(&msm_iommu_ctx_driver);
  338. platform_driver_unregister(&msm_iommu_driver);
  339. }
  340. subsys_initcall(msm_iommu_driver_init);
  341. module_exit(msm_iommu_driver_exit);
  342. MODULE_LICENSE("GPL v2");
  343. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");