msm_smmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. if (flags & MSM_BO_KEEPATTRS)
  187. attrs |= DMA_ATTR_IOMMU_USE_LLC_NWA;
  188. /*
  189. * For import buffer type, dma_map_sg_attrs is called during
  190. * dma_buf_map_attachment and is not required to call again
  191. */
  192. if (!(flags & MSM_BO_EXTBUF)) {
  193. ret = dma_map_sg_attrs(client->dev, sgt->sgl, sgt->nents, dir,
  194. attrs);
  195. if (!ret) {
  196. DRM_ERROR("dma map sg failed\n");
  197. return -ENOMEM;
  198. }
  199. }
  200. if (sgt && sgt->sgl) {
  201. DRM_DEBUG("%pad/0x%x/0x%x/0x%lx\n",
  202. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  203. dir, attrs);
  204. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  205. dir, attrs, client->secure);
  206. }
  207. return 0;
  208. }
  209. static void msm_smmu_unmap_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  210. int dir, u32 flags)
  211. {
  212. struct msm_smmu *smmu = to_msm_smmu(mmu);
  213. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  214. if (!sgt || !client) {
  215. DRM_ERROR("sg table is invalid\n");
  216. return;
  217. }
  218. if (sgt->sgl) {
  219. DRM_DEBUG("%pad/0x%x/0x%x\n",
  220. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  221. dir);
  222. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  223. dir, client->secure);
  224. }
  225. if (!(flags & MSM_BO_EXTBUF))
  226. dma_unmap_sg(client->dev, sgt->sgl, sgt->nents, dir);
  227. }
  228. static bool msm_smmu_is_domain_secure(struct msm_mmu *mmu)
  229. {
  230. struct msm_smmu *smmu = to_msm_smmu(mmu);
  231. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  232. return client->secure;
  233. }
  234. static const struct msm_mmu_funcs funcs = {
  235. .attach = msm_smmu_attach,
  236. .detach = msm_smmu_detach,
  237. .map = msm_smmu_map,
  238. .unmap = msm_smmu_unmap,
  239. .map_dma_buf = msm_smmu_map_dma_buf,
  240. .unmap_dma_buf = msm_smmu_unmap_dma_buf,
  241. .destroy = msm_smmu_destroy,
  242. .is_domain_secure = msm_smmu_is_domain_secure,
  243. .set_attribute = msm_smmu_set_attribute,
  244. .one_to_one_map = msm_smmu_one_to_one_map,
  245. .one_to_one_unmap = msm_smmu_one_to_one_unmap,
  246. .get_dev = msm_smmu_get_dev,
  247. };
  248. static struct msm_smmu_domain msm_smmu_domains[MSM_SMMU_DOMAIN_MAX] = {
  249. [MSM_SMMU_DOMAIN_UNSECURE] = {
  250. .label = "mdp_ns",
  251. .secure = false,
  252. },
  253. [MSM_SMMU_DOMAIN_SECURE] = {
  254. .label = "mdp_s",
  255. .secure = true,
  256. },
  257. [MSM_SMMU_DOMAIN_NRT_UNSECURE] = {
  258. .label = "rot_ns",
  259. .secure = false,
  260. },
  261. [MSM_SMMU_DOMAIN_NRT_SECURE] = {
  262. .label = "rot_s",
  263. .secure = true,
  264. },
  265. };
  266. static const struct of_device_id msm_smmu_dt_match[] = {
  267. { .compatible = "qcom,smmu_sde_unsec",
  268. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_UNSECURE] },
  269. { .compatible = "qcom,smmu_sde_sec",
  270. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_SECURE] },
  271. { .compatible = "qcom,smmu_sde_nrt_unsec",
  272. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_UNSECURE] },
  273. { .compatible = "qcom,smmu_sde_nrt_sec",
  274. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_SECURE] },
  275. {}
  276. };
  277. MODULE_DEVICE_TABLE(of, msm_smmu_dt_match);
  278. static struct device *msm_smmu_device_create(struct device *dev,
  279. enum msm_mmu_domain_type domain,
  280. struct msm_smmu *smmu)
  281. {
  282. struct device_node *child;
  283. struct platform_device *pdev;
  284. int i;
  285. const char *compat = NULL;
  286. for (i = 0; i < ARRAY_SIZE(msm_smmu_dt_match); i++) {
  287. if (msm_smmu_dt_match[i].data == &msm_smmu_domains[domain]) {
  288. compat = msm_smmu_dt_match[i].compatible;
  289. break;
  290. }
  291. }
  292. if (!compat) {
  293. DRM_DEBUG("unable to find matching domain for %d\n", domain);
  294. return ERR_PTR(-ENOENT);
  295. }
  296. DRM_DEBUG("found domain %d compat: %s\n", domain, compat);
  297. child = of_find_compatible_node(dev->of_node, NULL, compat);
  298. if (!child) {
  299. DRM_DEBUG("unable to find compatible node for %s\n", compat);
  300. return ERR_PTR(-ENODEV);
  301. }
  302. pdev = of_platform_device_create(child, NULL, dev);
  303. if (!pdev) {
  304. DRM_ERROR("unable to create smmu platform dev for domain %d\n",
  305. domain);
  306. return ERR_PTR(-ENODEV);
  307. }
  308. smmu->client = platform_get_drvdata(pdev);
  309. return &pdev->dev;
  310. }
  311. struct msm_mmu *msm_smmu_new(struct device *dev,
  312. enum msm_mmu_domain_type domain)
  313. {
  314. struct msm_smmu *smmu;
  315. struct device *client_dev;
  316. smmu = kzalloc(sizeof(*smmu), GFP_KERNEL);
  317. if (!smmu)
  318. return ERR_PTR(-ENOMEM);
  319. client_dev = msm_smmu_device_create(dev, domain, smmu);
  320. if (IS_ERR(client_dev)) {
  321. kfree(smmu);
  322. return (void *)client_dev ? : ERR_PTR(-ENODEV);
  323. }
  324. smmu->client_dev = client_dev;
  325. msm_mmu_init(&smmu->base, dev, &funcs);
  326. return &smmu->base;
  327. }
  328. static int msm_smmu_fault_handler(struct iommu_domain *domain,
  329. struct device *dev, unsigned long iova,
  330. int flags, void *token)
  331. {
  332. struct msm_smmu_client *client;
  333. int rc = -EINVAL;
  334. if (!token) {
  335. DRM_ERROR("Error: token is NULL\n");
  336. return -EINVAL;
  337. }
  338. client = (struct msm_smmu_client *)token;
  339. /* see iommu.h for fault flags definition */
  340. SDE_EVT32(iova, flags);
  341. DRM_ERROR("trigger dump, iova=0x%08lx, flags=0x%x\n", iova, flags);
  342. DRM_ERROR("SMMU device:%s", client->dev ? client->dev->kobj.name : "");
  343. /*
  344. * return -ENOSYS to allow smmu driver to dump out useful
  345. * debug info.
  346. */
  347. return rc;
  348. }
  349. /**
  350. * msm_smmu_probe()
  351. * @pdev: platform device
  352. *
  353. * Each smmu context acts as a separate device and the context banks are
  354. * configured with a VA range.
  355. * Registers the clks as each context bank has its own clks, for which voting
  356. * has to be done everytime before using that context bank.
  357. */
  358. static int msm_smmu_probe(struct platform_device *pdev)
  359. {
  360. const struct of_device_id *match;
  361. struct msm_smmu_client *client;
  362. const struct msm_smmu_domain *domain;
  363. match = of_match_device(msm_smmu_dt_match, &pdev->dev);
  364. if (!match || !match->data) {
  365. dev_err(&pdev->dev, "probe failed as match data is invalid\n");
  366. return -EINVAL;
  367. }
  368. domain = match->data;
  369. if (!domain) {
  370. dev_err(&pdev->dev, "no matching device found\n");
  371. return -EINVAL;
  372. }
  373. DRM_INFO("probing device %s\n", match->compatible);
  374. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  375. if (!client)
  376. return -ENOMEM;
  377. client->dev = &pdev->dev;
  378. client->domain = iommu_get_domain_for_dev(client->dev);
  379. if (!client->domain) {
  380. dev_err(&pdev->dev, "iommu get domain for dev failed\n");
  381. return -EINVAL;
  382. }
  383. client->secure = domain->secure;
  384. client->domain_attached = true;
  385. if (!client->dev->dma_parms)
  386. client->dev->dma_parms = devm_kzalloc(client->dev,
  387. sizeof(*client->dev->dma_parms), GFP_KERNEL);
  388. dma_set_max_seg_size(client->dev, DMA_BIT_MASK(32));
  389. dma_set_seg_boundary(client->dev, (unsigned long)DMA_BIT_MASK(64));
  390. iommu_set_fault_handler(client->domain,
  391. msm_smmu_fault_handler, (void *)client);
  392. DRM_INFO("Created domain %s, secure=%d\n",
  393. domain->label, domain->secure);
  394. platform_set_drvdata(pdev, client);
  395. return 0;
  396. }
  397. static int msm_smmu_remove(struct platform_device *pdev)
  398. {
  399. struct msm_smmu_client *client;
  400. client = platform_get_drvdata(pdev);
  401. client->domain_attached = false;
  402. return 0;
  403. }
  404. static struct platform_driver msm_smmu_driver = {
  405. .probe = msm_smmu_probe,
  406. .remove = msm_smmu_remove,
  407. .driver = {
  408. .name = "msmdrm_smmu",
  409. .of_match_table = msm_smmu_dt_match,
  410. .suppress_bind_attrs = true,
  411. },
  412. };
  413. int __init msm_smmu_driver_init(void)
  414. {
  415. int ret;
  416. ret = platform_driver_register(&msm_smmu_driver);
  417. if (ret)
  418. pr_err("mdss_smmu_register_driver() failed!\n");
  419. return ret;
  420. }
  421. void __exit msm_smmu_driver_cleanup(void)
  422. {
  423. platform_driver_unregister(&msm_smmu_driver);
  424. }
  425. MODULE_LICENSE("GPL v2");
  426. MODULE_DESCRIPTION("MSM SMMU driver");