iommu_dev.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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, int ncb)
  73. {
  74. int ctx;
  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. 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 clk *iommu_pclk;
  115. struct msm_iommu_drvdata *drvdata;
  116. struct msm_iommu_dev *iommu_dev = pdev->dev.platform_data;
  117. void __iomem *regs_base;
  118. resource_size_t len;
  119. int ret, irq, par;
  120. if (pdev->id == -1) {
  121. msm_iommu_root_dev = pdev;
  122. return 0;
  123. }
  124. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  125. if (!drvdata) {
  126. ret = -ENOMEM;
  127. goto fail;
  128. }
  129. if (!iommu_dev) {
  130. ret = -ENODEV;
  131. goto fail;
  132. }
  133. iommu_pclk = clk_get(NULL, "smmu_pclk");
  134. if (IS_ERR(iommu_pclk)) {
  135. ret = -ENODEV;
  136. goto fail;
  137. }
  138. ret = clk_enable(iommu_pclk);
  139. if (ret)
  140. goto fail_enable;
  141. iommu_clk = clk_get(&pdev->dev, "iommu_clk");
  142. if (!IS_ERR(iommu_clk)) {
  143. if (clk_get_rate(iommu_clk) == 0)
  144. clk_set_min_rate(iommu_clk, 1);
  145. ret = clk_enable(iommu_clk);
  146. if (ret) {
  147. clk_put(iommu_clk);
  148. goto fail_pclk;
  149. }
  150. } else
  151. iommu_clk = NULL;
  152. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "physbase");
  153. if (!r) {
  154. ret = -ENODEV;
  155. goto fail_clk;
  156. }
  157. len = resource_size(r);
  158. r2 = request_mem_region(r->start, len, r->name);
  159. if (!r2) {
  160. pr_err("Could not request memory region: start=%p, len=%d\n",
  161. (void *) r->start, len);
  162. ret = -EBUSY;
  163. goto fail_clk;
  164. }
  165. regs_base = ioremap(r2->start, len);
  166. if (!regs_base) {
  167. pr_err("Could not ioremap: start=%p, len=%d\n",
  168. (void *) r2->start, len);
  169. ret = -EBUSY;
  170. goto fail_mem;
  171. }
  172. irq = platform_get_irq_byname(pdev, "secure_irq");
  173. if (irq < 0) {
  174. ret = -ENODEV;
  175. goto fail_io;
  176. }
  177. msm_iommu_reset(regs_base, iommu_dev->ncb);
  178. SET_M(regs_base, 0, 1);
  179. SET_PAR(regs_base, 0, 0);
  180. SET_V2PCFG(regs_base, 0, 1);
  181. SET_V2PPR(regs_base, 0, 0);
  182. par = GET_PAR(regs_base, 0);
  183. SET_V2PCFG(regs_base, 0, 0);
  184. SET_M(regs_base, 0, 0);
  185. if (!par) {
  186. pr_err("%s: Invalid PAR value detected\n", iommu_dev->name);
  187. ret = -ENODEV;
  188. goto fail_io;
  189. }
  190. ret = request_irq(irq, msm_iommu_fault_handler, 0,
  191. "msm_iommu_secure_irpt_handler", drvdata);
  192. if (ret) {
  193. pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
  194. goto fail_io;
  195. }
  196. drvdata->pclk = iommu_pclk;
  197. drvdata->clk = iommu_clk;
  198. drvdata->base = regs_base;
  199. drvdata->irq = irq;
  200. drvdata->ncb = iommu_dev->ncb;
  201. pr_info("device %s mapped at %p, irq %d with %d ctx banks\n",
  202. iommu_dev->name, regs_base, irq, iommu_dev->ncb);
  203. platform_set_drvdata(pdev, drvdata);
  204. if (iommu_clk)
  205. clk_disable(iommu_clk);
  206. clk_disable(iommu_pclk);
  207. return 0;
  208. fail_io:
  209. iounmap(regs_base);
  210. fail_mem:
  211. release_mem_region(r->start, len);
  212. fail_clk:
  213. if (iommu_clk) {
  214. clk_disable(iommu_clk);
  215. clk_put(iommu_clk);
  216. }
  217. fail_pclk:
  218. clk_disable(iommu_pclk);
  219. fail_enable:
  220. clk_put(iommu_pclk);
  221. fail:
  222. kfree(drvdata);
  223. return ret;
  224. }
  225. static int msm_iommu_remove(struct platform_device *pdev)
  226. {
  227. struct msm_iommu_drvdata *drv = NULL;
  228. drv = platform_get_drvdata(pdev);
  229. if (drv) {
  230. if (drv->clk)
  231. clk_put(drv->clk);
  232. clk_put(drv->pclk);
  233. memset(drv, 0, sizeof(*drv));
  234. kfree(drv);
  235. platform_set_drvdata(pdev, NULL);
  236. }
  237. return 0;
  238. }
  239. static int msm_iommu_ctx_probe(struct platform_device *pdev)
  240. {
  241. struct msm_iommu_ctx_dev *c = pdev->dev.platform_data;
  242. struct msm_iommu_drvdata *drvdata;
  243. struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
  244. int i, ret;
  245. if (!c || !pdev->dev.parent) {
  246. ret = -EINVAL;
  247. goto fail;
  248. }
  249. drvdata = dev_get_drvdata(pdev->dev.parent);
  250. if (!drvdata) {
  251. ret = -ENODEV;
  252. goto fail;
  253. }
  254. ctx_drvdata = kzalloc(sizeof(*ctx_drvdata), GFP_KERNEL);
  255. if (!ctx_drvdata) {
  256. ret = -ENOMEM;
  257. goto fail;
  258. }
  259. ctx_drvdata->num = c->num;
  260. ctx_drvdata->pdev = pdev;
  261. INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
  262. platform_set_drvdata(pdev, ctx_drvdata);
  263. ret = clk_enable(drvdata->pclk);
  264. if (ret)
  265. goto fail;
  266. if (drvdata->clk) {
  267. ret = clk_enable(drvdata->clk);
  268. if (ret) {
  269. clk_disable(drvdata->pclk);
  270. goto fail;
  271. }
  272. }
  273. /* Program the M2V tables for this context */
  274. for (i = 0; i < MAX_NUM_MIDS; i++) {
  275. int mid = c->mids[i];
  276. if (mid == -1)
  277. break;
  278. SET_M2VCBR_N(drvdata->base, mid, 0);
  279. SET_CBACR_N(drvdata->base, c->num, 0);
  280. /* Set VMID = 0 */
  281. SET_VMID(drvdata->base, mid, 0);
  282. /* Set the context number for that MID to this context */
  283. SET_CBNDX(drvdata->base, mid, c->num);
  284. /* Set MID associated with this context bank to 0*/
  285. SET_CBVMID(drvdata->base, c->num, 0);
  286. /* Set the ASID for TLB tagging for this context */
  287. SET_CONTEXTIDR_ASID(drvdata->base, c->num, c->num);
  288. /* Set security bit override to be Non-secure */
  289. SET_NSCFG(drvdata->base, mid, 3);
  290. }
  291. if (drvdata->clk)
  292. clk_disable(drvdata->clk);
  293. clk_disable(drvdata->pclk);
  294. dev_info(&pdev->dev, "context %s using bank %d\n", c->name, c->num);
  295. return 0;
  296. fail:
  297. kfree(ctx_drvdata);
  298. return ret;
  299. }
  300. static int msm_iommu_ctx_remove(struct platform_device *pdev)
  301. {
  302. struct msm_iommu_ctx_drvdata *drv = NULL;
  303. drv = platform_get_drvdata(pdev);
  304. if (drv) {
  305. memset(drv, 0, sizeof(struct msm_iommu_ctx_drvdata));
  306. kfree(drv);
  307. platform_set_drvdata(pdev, NULL);
  308. }
  309. return 0;
  310. }
  311. static struct platform_driver msm_iommu_driver = {
  312. .driver = {
  313. .name = "msm_iommu",
  314. },
  315. .probe = msm_iommu_probe,
  316. .remove = msm_iommu_remove,
  317. };
  318. static struct platform_driver msm_iommu_ctx_driver = {
  319. .driver = {
  320. .name = "msm_iommu_ctx",
  321. },
  322. .probe = msm_iommu_ctx_probe,
  323. .remove = msm_iommu_ctx_remove,
  324. };
  325. static int __init msm_iommu_driver_init(void)
  326. {
  327. int ret;
  328. ret = platform_driver_register(&msm_iommu_driver);
  329. if (ret != 0) {
  330. pr_err("Failed to register IOMMU driver\n");
  331. goto error;
  332. }
  333. ret = platform_driver_register(&msm_iommu_ctx_driver);
  334. if (ret != 0) {
  335. pr_err("Failed to register IOMMU context driver\n");
  336. goto error;
  337. }
  338. error:
  339. return ret;
  340. }
  341. static void __exit msm_iommu_driver_exit(void)
  342. {
  343. platform_driver_unregister(&msm_iommu_ctx_driver);
  344. platform_driver_unregister(&msm_iommu_driver);
  345. }
  346. subsys_initcall(msm_iommu_driver_init);
  347. module_exit(msm_iommu_driver_exit);
  348. MODULE_LICENSE("GPL v2");
  349. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");