msm_smmu.c 12 KB

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