msm_iommu_dev.c 8.5 KB

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