msm_smmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/msm_dma_iommu_mapping.h>
  22. #include <linux/dma-mapping.h>
  23. #include <soc/qcom/secure_buffer.h>
  24. #include "msm_drv.h"
  25. #include "msm_gem.h"
  26. #include "msm_mmu.h"
  27. #include "sde_dbg.h"
  28. struct msm_smmu_client {
  29. struct device *dev;
  30. struct iommu_domain *domain;
  31. const struct dma_map_ops *dma_ops;
  32. bool domain_attached;
  33. bool secure;
  34. };
  35. struct msm_smmu {
  36. struct msm_mmu base;
  37. struct device *client_dev;
  38. struct msm_smmu_client *client;
  39. };
  40. struct msm_smmu_domain {
  41. const char *label;
  42. bool secure;
  43. };
  44. #define to_msm_smmu(x) container_of(x, struct msm_smmu, base)
  45. #define msm_smmu_to_client(smmu) (smmu->client)
  46. static int msm_smmu_attach(struct msm_mmu *mmu, const char * const *names,
  47. int cnt)
  48. {
  49. struct msm_smmu *smmu = to_msm_smmu(mmu);
  50. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  51. int rc = 0;
  52. if (!client) {
  53. pr_err("undefined smmu client\n");
  54. return -EINVAL;
  55. }
  56. /* domain attach only once */
  57. if (client->domain_attached)
  58. return 0;
  59. if (client->dma_ops) {
  60. set_dma_ops(client->dev, client->dma_ops);
  61. client->dma_ops = NULL;
  62. dev_dbg(client->dev, "iommu domain ops restored\n");
  63. }
  64. rc = iommu_attach_device(client->domain, client->dev);
  65. if (rc) {
  66. dev_err(client->dev, "iommu attach dev failed (%d)\n", rc);
  67. return rc;
  68. }
  69. client->domain_attached = true;
  70. dev_dbg(client->dev, "iommu domain attached\n");
  71. return 0;
  72. }
  73. static void msm_smmu_detach(struct msm_mmu *mmu, const char * const *names,
  74. int cnt)
  75. {
  76. struct msm_smmu *smmu = to_msm_smmu(mmu);
  77. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  78. if (!client) {
  79. pr_err("undefined smmu client\n");
  80. return;
  81. }
  82. if (!client->domain_attached)
  83. return;
  84. pm_runtime_get_sync(mmu->dev);
  85. msm_dma_unmap_all_for_dev(client->dev);
  86. iommu_detach_device(client->domain, client->dev);
  87. client->dma_ops = get_dma_ops(client->dev);
  88. if (client->dma_ops) {
  89. set_dma_ops(client->dev, NULL);
  90. dev_dbg(client->dev, "iommu domain ops removed\n");
  91. }
  92. pm_runtime_put_sync(mmu->dev);
  93. client->domain_attached = false;
  94. dev_dbg(client->dev, "iommu domain detached\n");
  95. }
  96. static int msm_smmu_set_attribute(struct msm_mmu *mmu,
  97. enum iommu_attr attr, void *data)
  98. {
  99. struct msm_smmu *smmu = to_msm_smmu(mmu);
  100. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  101. int ret = 0;
  102. if (!client || !client->domain)
  103. return -ENODEV;
  104. ret = iommu_domain_set_attr(client->domain, attr, data);
  105. if (ret)
  106. DRM_ERROR("set domain attribute failed:%d\n", ret);
  107. return ret;
  108. }
  109. static int msm_smmu_one_to_one_unmap(struct msm_mmu *mmu,
  110. uint32_t dest_address, uint32_t size)
  111. {
  112. struct msm_smmu *smmu = to_msm_smmu(mmu);
  113. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  114. int ret = 0;
  115. if (!client || !client->domain)
  116. return -ENODEV;
  117. ret = iommu_unmap(client->domain, dest_address, size);
  118. if (ret != size)
  119. pr_err("smmu unmap failed\n");
  120. return 0;
  121. }
  122. static int msm_smmu_one_to_one_map(struct msm_mmu *mmu, uint32_t iova,
  123. uint32_t dest_address, uint32_t size, int prot)
  124. {
  125. struct msm_smmu *smmu = to_msm_smmu(mmu);
  126. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  127. int ret = 0;
  128. if (!client || !client->domain)
  129. return -ENODEV;
  130. ret = iommu_map(client->domain, dest_address, dest_address,
  131. size, prot);
  132. if (ret)
  133. pr_err("smmu map failed\n");
  134. return ret;
  135. }
  136. static int msm_smmu_map(struct msm_mmu *mmu, uint64_t iova,
  137. struct sg_table *sgt, unsigned int len, int prot)
  138. {
  139. struct msm_smmu *smmu = to_msm_smmu(mmu);
  140. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  141. size_t ret = 0;
  142. if (sgt && sgt->sgl) {
  143. ret = iommu_map_sg(client->domain, iova, sgt->sgl,
  144. sgt->nents, prot);
  145. WARN_ON((int)ret < 0);
  146. DRM_DEBUG("%pad/0x%x/0x%x/\n", &sgt->sgl->dma_address,
  147. sgt->sgl->dma_length, prot);
  148. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length, prot);
  149. }
  150. return (ret == len) ? 0 : -EINVAL;
  151. }
  152. static int msm_smmu_unmap(struct msm_mmu *mmu, uint64_t iova,
  153. struct sg_table *sgt, unsigned int len)
  154. {
  155. struct msm_smmu *smmu = to_msm_smmu(mmu);
  156. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  157. pm_runtime_get_sync(mmu->dev);
  158. iommu_unmap(client->domain, iova, len);
  159. pm_runtime_put_sync(mmu->dev);
  160. return 0;
  161. }
  162. static void msm_smmu_destroy(struct msm_mmu *mmu)
  163. {
  164. struct msm_smmu *smmu = to_msm_smmu(mmu);
  165. struct platform_device *pdev = to_platform_device(smmu->client_dev);
  166. if (smmu->client_dev)
  167. platform_device_unregister(pdev);
  168. kfree(smmu);
  169. }
  170. struct device *msm_smmu_get_dev(struct msm_mmu *mmu)
  171. {
  172. struct msm_smmu *smmu = to_msm_smmu(mmu);
  173. return smmu->client_dev;
  174. }
  175. static int msm_smmu_map_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  176. int dir, u32 flags)
  177. {
  178. struct msm_smmu *smmu = to_msm_smmu(mmu);
  179. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  180. unsigned long attrs = 0x0;
  181. int ret;
  182. if (!sgt || !client) {
  183. DRM_ERROR("sg table is invalid\n");
  184. return -ENOMEM;
  185. }
  186. /*
  187. * For import buffer type, dma_map_sg_attrs is called during
  188. * dma_buf_map_attachment and is not required to call again
  189. */
  190. if (!(flags & MSM_BO_EXTBUF)) {
  191. ret = dma_map_sg_attrs(client->dev, sgt->sgl, sgt->nents, dir,
  192. attrs);
  193. if (!ret) {
  194. DRM_ERROR("dma map sg failed\n");
  195. return -ENOMEM;
  196. }
  197. }
  198. if (sgt && sgt->sgl) {
  199. DRM_DEBUG("%pad/0x%x/0x%x/0x%lx\n",
  200. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  201. dir, attrs);
  202. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  203. dir, attrs, client->secure);
  204. }
  205. return 0;
  206. }
  207. static void msm_smmu_unmap_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  208. int dir, u32 flags)
  209. {
  210. struct msm_smmu *smmu = to_msm_smmu(mmu);
  211. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  212. if (!sgt || !client) {
  213. DRM_ERROR("sg table is invalid\n");
  214. return;
  215. }
  216. if (sgt->sgl) {
  217. DRM_DEBUG("%pad/0x%x/0x%x\n",
  218. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  219. dir);
  220. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  221. dir, client->secure);
  222. }
  223. if (!(flags & MSM_BO_EXTBUF))
  224. dma_unmap_sg(client->dev, sgt->sgl, sgt->nents, dir);
  225. }
  226. static bool msm_smmu_is_domain_secure(struct msm_mmu *mmu)
  227. {
  228. struct msm_smmu *smmu = to_msm_smmu(mmu);
  229. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  230. return client->secure;
  231. }
  232. static const struct msm_mmu_funcs funcs = {
  233. .attach = msm_smmu_attach,
  234. .detach = msm_smmu_detach,
  235. .map = msm_smmu_map,
  236. .unmap = msm_smmu_unmap,
  237. .map_dma_buf = msm_smmu_map_dma_buf,
  238. .unmap_dma_buf = msm_smmu_unmap_dma_buf,
  239. .destroy = msm_smmu_destroy,
  240. .is_domain_secure = msm_smmu_is_domain_secure,
  241. .set_attribute = msm_smmu_set_attribute,
  242. .one_to_one_map = msm_smmu_one_to_one_map,
  243. .one_to_one_unmap = msm_smmu_one_to_one_unmap,
  244. .get_dev = msm_smmu_get_dev,
  245. };
  246. static struct msm_smmu_domain msm_smmu_domains[MSM_SMMU_DOMAIN_MAX] = {
  247. [MSM_SMMU_DOMAIN_UNSECURE] = {
  248. .label = "mdp_ns",
  249. .secure = false,
  250. },
  251. [MSM_SMMU_DOMAIN_SECURE] = {
  252. .label = "mdp_s",
  253. .secure = true,
  254. },
  255. [MSM_SMMU_DOMAIN_NRT_UNSECURE] = {
  256. .label = "rot_ns",
  257. .secure = false,
  258. },
  259. [MSM_SMMU_DOMAIN_NRT_SECURE] = {
  260. .label = "rot_s",
  261. .secure = true,
  262. },
  263. };
  264. static const struct of_device_id msm_smmu_dt_match[] = {
  265. { .compatible = "qcom,smmu_sde_unsec",
  266. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_UNSECURE] },
  267. { .compatible = "qcom,smmu_sde_sec",
  268. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_SECURE] },
  269. { .compatible = "qcom,smmu_sde_nrt_unsec",
  270. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_UNSECURE] },
  271. { .compatible = "qcom,smmu_sde_nrt_sec",
  272. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_SECURE] },
  273. {}
  274. };
  275. MODULE_DEVICE_TABLE(of, msm_smmu_dt_match);
  276. static struct device *msm_smmu_device_create(struct device *dev,
  277. enum msm_mmu_domain_type domain,
  278. struct msm_smmu *smmu)
  279. {
  280. struct device_node *child;
  281. struct platform_device *pdev;
  282. int i;
  283. const char *compat = NULL;
  284. for (i = 0; i < ARRAY_SIZE(msm_smmu_dt_match); i++) {
  285. if (msm_smmu_dt_match[i].data == &msm_smmu_domains[domain]) {
  286. compat = msm_smmu_dt_match[i].compatible;
  287. break;
  288. }
  289. }
  290. if (!compat) {
  291. DRM_DEBUG("unable to find matching domain for %d\n", domain);
  292. return ERR_PTR(-ENOENT);
  293. }
  294. DRM_DEBUG("found domain %d compat: %s\n", domain, compat);
  295. child = of_find_compatible_node(dev->of_node, NULL, compat);
  296. if (!child) {
  297. DRM_DEBUG("unable to find compatible node for %s\n", compat);
  298. return ERR_PTR(-ENODEV);
  299. }
  300. pdev = of_platform_device_create(child, NULL, dev);
  301. if (!pdev) {
  302. DRM_ERROR("unable to create smmu platform dev for domain %d\n",
  303. domain);
  304. return ERR_PTR(-ENODEV);
  305. }
  306. smmu->client = platform_get_drvdata(pdev);
  307. return &pdev->dev;
  308. }
  309. struct msm_mmu *msm_smmu_new(struct device *dev,
  310. enum msm_mmu_domain_type domain)
  311. {
  312. struct msm_smmu *smmu;
  313. struct device *client_dev;
  314. smmu = kzalloc(sizeof(*smmu), GFP_KERNEL);
  315. if (!smmu)
  316. return ERR_PTR(-ENOMEM);
  317. client_dev = msm_smmu_device_create(dev, domain, smmu);
  318. if (IS_ERR(client_dev)) {
  319. kfree(smmu);
  320. return (void *)client_dev ? : ERR_PTR(-ENODEV);
  321. }
  322. smmu->client_dev = client_dev;
  323. msm_mmu_init(&smmu->base, dev, &funcs);
  324. return &smmu->base;
  325. }
  326. static int msm_smmu_fault_handler(struct iommu_domain *domain,
  327. struct device *dev, unsigned long iova,
  328. int flags, void *token)
  329. {
  330. struct msm_smmu_client *client;
  331. int rc = -EINVAL;
  332. if (!token) {
  333. DRM_ERROR("Error: token is NULL\n");
  334. return -EINVAL;
  335. }
  336. client = (struct msm_smmu_client *)token;
  337. /* see iommu.h for fault flags definition */
  338. SDE_EVT32(iova, flags);
  339. DRM_ERROR("trigger dump, iova=0x%08lx, flags=0x%x\n", iova, flags);
  340. DRM_ERROR("SMMU device:%s", client->dev ? client->dev->kobj.name : "");
  341. /*
  342. * return -ENOSYS to allow smmu driver to dump out useful
  343. * debug info.
  344. */
  345. return rc;
  346. }
  347. /**
  348. * msm_smmu_probe()
  349. * @pdev: platform device
  350. *
  351. * Each smmu context acts as a separate device and the context banks are
  352. * configured with a VA range.
  353. * Registers the clks as each context bank has its own clks, for which voting
  354. * has to be done everytime before using that context bank.
  355. */
  356. static int msm_smmu_probe(struct platform_device *pdev)
  357. {
  358. const struct of_device_id *match;
  359. struct msm_smmu_client *client;
  360. const struct msm_smmu_domain *domain;
  361. match = of_match_device(msm_smmu_dt_match, &pdev->dev);
  362. if (!match || !match->data) {
  363. dev_err(&pdev->dev, "probe failed as match data is invalid\n");
  364. return -EINVAL;
  365. }
  366. domain = match->data;
  367. if (!domain) {
  368. dev_err(&pdev->dev, "no matching device found\n");
  369. return -EINVAL;
  370. }
  371. DRM_INFO("probing device %s\n", match->compatible);
  372. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  373. if (!client)
  374. return -ENOMEM;
  375. client->dev = &pdev->dev;
  376. client->domain = iommu_get_domain_for_dev(client->dev);
  377. if (!client->domain) {
  378. dev_err(&pdev->dev, "iommu get domain for dev failed\n");
  379. return -EINVAL;
  380. }
  381. client->secure = domain->secure;
  382. client->domain_attached = true;
  383. if (!client->dev->dma_parms)
  384. client->dev->dma_parms = devm_kzalloc(client->dev,
  385. sizeof(*client->dev->dma_parms), GFP_KERNEL);
  386. dma_set_max_seg_size(client->dev, DMA_BIT_MASK(32));
  387. dma_set_seg_boundary(client->dev, (unsigned long)DMA_BIT_MASK(64));
  388. iommu_set_fault_handler(client->domain,
  389. msm_smmu_fault_handler, (void *)client);
  390. DRM_INFO("Created domain %s, secure=%d\n",
  391. domain->label, domain->secure);
  392. platform_set_drvdata(pdev, client);
  393. return 0;
  394. }
  395. static int msm_smmu_remove(struct platform_device *pdev)
  396. {
  397. struct msm_smmu_client *client;
  398. client = platform_get_drvdata(pdev);
  399. client->domain_attached = false;
  400. return 0;
  401. }
  402. static struct platform_driver msm_smmu_driver = {
  403. .probe = msm_smmu_probe,
  404. .remove = msm_smmu_remove,
  405. .driver = {
  406. .name = "msmdrm_smmu",
  407. .of_match_table = msm_smmu_dt_match,
  408. .suppress_bind_attrs = true,
  409. },
  410. };
  411. int __init msm_smmu_driver_init(void)
  412. {
  413. int ret;
  414. ret = platform_driver_register(&msm_smmu_driver);
  415. if (ret)
  416. pr_err("mdss_smmu_register_driver() failed!\n");
  417. return ret;
  418. }
  419. void __exit msm_smmu_driver_cleanup(void)
  420. {
  421. platform_driver_unregister(&msm_smmu_driver);
  422. }
  423. MODULE_LICENSE("GPL v2");
  424. MODULE_DESCRIPTION("MSM SMMU driver");