msm_iommu_dev.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. 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, int ncb)
  72. {
  73. int ctx;
  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. for (ctx = 0; ctx < ncb; ctx++) {
  86. SET_BPRCOSH(base, ctx, 0);
  87. SET_BPRCISH(base, ctx, 0);
  88. SET_BPRCNSH(base, ctx, 0);
  89. SET_BPSHCFG(base, ctx, 0);
  90. SET_BPMTCFG(base, ctx, 0);
  91. SET_ACTLR(base, ctx, 0);
  92. SET_SCTLR(base, ctx, 0);
  93. SET_FSRRESTORE(base, ctx, 0);
  94. SET_TTBR0(base, ctx, 0);
  95. SET_TTBR1(base, ctx, 0);
  96. SET_TTBCR(base, ctx, 0);
  97. SET_BFBCR(base, ctx, 0);
  98. SET_PAR(base, ctx, 0);
  99. SET_FAR(base, ctx, 0);
  100. SET_CTX_TLBIALL(base, ctx, 0);
  101. SET_TLBFLPTER(base, ctx, 0);
  102. SET_TLBSLPTER(base, ctx, 0);
  103. SET_TLBLKCR(base, ctx, 0);
  104. SET_PRRR(base, ctx, 0);
  105. SET_NMRR(base, ctx, 0);
  106. SET_CONTEXTIDR(base, ctx, 0);
  107. }
  108. }
  109. static int msm_iommu_probe(struct platform_device *pdev)
  110. {
  111. struct resource *r, *r2;
  112. struct clk *iommu_clk;
  113. struct clk *iommu_pclk;
  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, irq, par;
  119. if (pdev->id == -1) {
  120. msm_iommu_root_dev = pdev;
  121. return 0;
  122. }
  123. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  124. if (!drvdata) {
  125. ret = -ENOMEM;
  126. goto fail;
  127. }
  128. if (!iommu_dev) {
  129. ret = -ENODEV;
  130. goto fail;
  131. }
  132. iommu_pclk = clk_get(NULL, "smmu_pclk");
  133. if (IS_ERR(iommu_pclk)) {
  134. ret = -ENODEV;
  135. goto fail;
  136. }
  137. ret = clk_prepare_enable(iommu_pclk);
  138. if (ret)
  139. goto fail_enable;
  140. iommu_clk = clk_get(&pdev->dev, "iommu_clk");
  141. if (!IS_ERR(iommu_clk)) {
  142. if (clk_get_rate(iommu_clk) == 0)
  143. clk_set_rate(iommu_clk, 1);
  144. ret = clk_prepare_enable(iommu_clk);
  145. if (ret) {
  146. clk_put(iommu_clk);
  147. goto fail_pclk;
  148. }
  149. } else
  150. iommu_clk = NULL;
  151. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
  152. if (!r) {
  153. ret = -ENODEV;
  154. goto fail_clk;
  155. }
  156. len = resource_size(r);
  157. r2 = request_mem_region(r->start, len, r->name);
  158. if (!r2) {
  159. pr_err("Could not request memory region: start=%p, len=%d\n",
  160. (void *) r->start, len);
  161. ret = -EBUSY;
  162. goto fail_clk;
  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. msm_iommu_reset(regs_base, iommu_dev->ncb);
  177. SET_M(regs_base, 0, 1);
  178. SET_PAR(regs_base, 0, 0);
  179. SET_V2PCFG(regs_base, 0, 1);
  180. SET_V2PPR(regs_base, 0, 0);
  181. par = GET_PAR(regs_base, 0);
  182. SET_V2PCFG(regs_base, 0, 0);
  183. SET_M(regs_base, 0, 0);
  184. if (!par) {
  185. pr_err("%s: Invalid PAR value detected\n", iommu_dev->name);
  186. ret = -ENODEV;
  187. goto fail_io;
  188. }
  189. ret = request_irq(irq, msm_iommu_fault_handler, 0,
  190. "msm_iommu_secure_irpt_handler", drvdata);
  191. if (ret) {
  192. pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
  193. goto fail_io;
  194. }
  195. drvdata->pclk = iommu_pclk;
  196. drvdata->clk = iommu_clk;
  197. drvdata->base = regs_base;
  198. drvdata->irq = irq;
  199. drvdata->ncb = iommu_dev->ncb;
  200. pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
  201. iommu_dev->name, regs_base, irq, iommu_dev->ncb);
  202. platform_set_drvdata(pdev, drvdata);
  203. if (iommu_clk)
  204. clk_disable(iommu_clk);
  205. clk_disable(iommu_pclk);
  206. return 0;
  207. fail_io:
  208. iounmap(regs_base);
  209. fail_mem:
  210. release_mem_region(r->start, len);
  211. fail_clk:
  212. if (iommu_clk) {
  213. clk_disable(iommu_clk);
  214. clk_put(iommu_clk);
  215. }
  216. fail_pclk:
  217. clk_disable_unprepare(iommu_pclk);
  218. fail_enable:
  219. clk_put(iommu_pclk);
  220. fail:
  221. kfree(drvdata);
  222. return ret;
  223. }
  224. static int msm_iommu_remove(struct platform_device *pdev)
  225. {
  226. struct msm_iommu_drvdata *drv = NULL;
  227. drv = platform_get_drvdata(pdev);
  228. if (drv) {
  229. if (drv->clk) {
  230. clk_unprepare(drv->clk);
  231. clk_put(drv->clk);
  232. }
  233. clk_unprepare(drv->pclk);
  234. clk_put(drv->pclk);
  235. memset(drv, 0, sizeof(*drv));
  236. kfree(drv);
  237. platform_set_drvdata(pdev, NULL);
  238. }
  239. return 0;
  240. }
  241. static int msm_iommu_ctx_probe(struct platform_device *pdev)
  242. {
  243. struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
  244. struct msm_iommu_drvdata *drvdata;
  245. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  246. int i, ret;
  247. if (!c || !pdev->dev.parent)
  248. return -EINVAL;
  249. drvdata = dev_get_drvdata(pdev->dev.parent);
  250. if (!drvdata)
  251. return -ENODEV;
  252. ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
  253. if (!ctx_drvdata)
  254. return -ENOMEM;
  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_prepare_enable(drvdata->pclk);
  260. if (ret)
  261. goto fail;
  262. if (drvdata->clk) {
  263. ret = clk_prepare_enable(drvdata->clk);
  264. if (ret) {
  265. clk_disable_unprepare(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 = 0 */
  277. SET_VMID(drvdata->base, mid, 0);
  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 to 0*/
  281. SET_CBVMID(drvdata->base, c->num, 0);
  282. /* Set the ASID for TLB tagging for this context */
  283. SET_CONTEXTIDR_ASID(drvdata->base, c->num, c->num);
  284. /* Set security bit override to be Non-secure */
  285. SET_NSCFG(drvdata->base, mid, 3);
  286. }
  287. if (drvdata->clk)
  288. clk_disable(drvdata->clk);
  289. clk_disable(drvdata->pclk);
  290. dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
  291. return 0;
  292. fail:
  293. kfree(ctx_drvdata);
  294. return ret;
  295. }
  296. static int msm_iommu_ctx_remove(struct platform_device *pdev)
  297. {
  298. struct msm_iommu_ctx_drvdata *drv = NULL;
  299. drv = platform_get_drvdata(pdev);
  300. if (drv) {
  301. memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
  302. kfree(drv);
  303. platform_set_drvdata(pdev, NULL);
  304. }
  305. return 0;
  306. }
  307. static struct platform_driver msm_iommu_driver = {
  308. .driver = {
  309. .name = "msm_iommu",
  310. },
  311. .probe = msm_iommu_probe,
  312. .remove = msm_iommu_remove,
  313. };
  314. static struct platform_driver msm_iommu_ctx_driver = {
  315. .driver = {
  316. .name = "msm_iommu_ctx",
  317. },
  318. .probe = msm_iommu_ctx_probe,
  319. .remove = msm_iommu_ctx_remove,
  320. };
  321. static int __init msm_iommu_driver_init(void)
  322. {
  323. int ret;
  324. ret = platform_driver_register(&msm_iommu_driver);
  325. if (ret != 0) {
  326. pr_err("Failed to register IOMMU driver\n");
  327. goto error;
  328. }
  329. ret = platform_driver_register(&msm_iommu_ctx_driver);
  330. if (ret != 0) {
  331. platform_driver_unregister(&msm_iommu_driver);
  332. pr_err("Failed to register IOMMU context driver\n");
  333. goto error;
  334. }
  335. error:
  336. return ret;
  337. }
  338. static void __exit msm_iommu_driver_exit(void)
  339. {
  340. platform_driver_unregister(&msm_iommu_ctx_driver);
  341. platform_driver_unregister(&msm_iommu_driver);
  342. }
  343. subsys_initcall(msm_iommu_driver_init);
  344. module_exit(msm_iommu_driver_exit);
  345. MODULE_LICENSE("GPL v2");
  346. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");