msm_smmu.c 12 KB

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