msm_smmu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (c) 2015-2021, 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/qcom-dma-mapping.h>
  22. #include <linux/msm_dma_iommu_mapping.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/qcom-iommu-util.h>
  25. #include <soc/qcom/secure_buffer.h>
  26. #include "msm_drv.h"
  27. #include "msm_gem.h"
  28. #include "msm_mmu.h"
  29. #include "sde_dbg.h"
  30. struct msm_smmu_client {
  31. struct device *dev;
  32. const char *compat;
  33. struct iommu_domain *domain;
  34. const struct dma_map_ops *dma_ops;
  35. bool domain_attached;
  36. bool secure;
  37. struct list_head smmu_list;
  38. };
  39. struct msm_smmu {
  40. struct msm_mmu base;
  41. struct device *client_dev;
  42. struct msm_smmu_client *client;
  43. };
  44. struct msm_smmu_domain {
  45. const char *label;
  46. bool secure;
  47. };
  48. #define to_msm_smmu(x) container_of(x, struct msm_smmu, base)
  49. #define msm_smmu_to_client(smmu) (smmu->client)
  50. /* Serialization lock for smmu_list */
  51. static DEFINE_MUTEX(smmu_list_lock);
  52. /* List of all smmu devices installed */
  53. static LIST_HEAD(sde_smmu_list);
  54. static int msm_smmu_attach(struct msm_mmu *mmu, const char * const *names,
  55. int cnt)
  56. {
  57. struct msm_smmu *smmu = to_msm_smmu(mmu);
  58. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  59. int rc = 0;
  60. if (!client) {
  61. pr_err("undefined smmu client\n");
  62. return -EINVAL;
  63. }
  64. /* domain attach only once */
  65. if (client->domain_attached)
  66. return 0;
  67. if (client->dma_ops) {
  68. set_dma_ops(client->dev, client->dma_ops);
  69. client->dma_ops = NULL;
  70. dev_dbg(client->dev, "iommu domain ops restored\n");
  71. }
  72. rc = qcom_iommu_sid_switch(client->dev, SID_ACQUIRE);
  73. if (rc) {
  74. dev_err(client->dev, "iommu sid switch failed (%d)\n", rc);
  75. return rc;
  76. }
  77. client->domain_attached = true;
  78. dev_dbg(client->dev, "iommu domain attached\n");
  79. return 0;
  80. }
  81. static void msm_smmu_detach(struct msm_mmu *mmu, const char * const *names,
  82. int cnt)
  83. {
  84. struct msm_smmu *smmu = to_msm_smmu(mmu);
  85. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  86. int rc;
  87. if (!client) {
  88. pr_err("undefined smmu client\n");
  89. return;
  90. }
  91. if (!client->domain_attached)
  92. return;
  93. pm_runtime_get_sync(mmu->dev);
  94. msm_dma_unmap_all_for_dev(client->dev);
  95. rc = qcom_iommu_sid_switch(client->dev, SID_RELEASE);
  96. if (rc)
  97. DRM_ERROR("iommu sid switch failed (%d)\n", rc);
  98. client->dma_ops = get_dma_ops(client->dev);
  99. if (client->dma_ops) {
  100. set_dma_ops(client->dev, NULL);
  101. dev_dbg(client->dev, "iommu domain ops removed\n");
  102. }
  103. pm_runtime_put_sync(mmu->dev);
  104. client->domain_attached = false;
  105. dev_dbg(client->dev, "iommu domain detached\n");
  106. }
  107. static int msm_smmu_set_attribute(struct msm_mmu *mmu,
  108. enum iommu_attr attr, void *data)
  109. {
  110. struct msm_smmu *smmu = to_msm_smmu(mmu);
  111. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  112. int ret = 0;
  113. if (!client || !client->domain)
  114. return -ENODEV;
  115. ret = iommu_domain_set_attr(client->domain, attr, data);
  116. if (ret)
  117. DRM_ERROR("set domain attribute failed:%d\n", ret);
  118. return ret;
  119. }
  120. static int msm_smmu_one_to_one_unmap(struct msm_mmu *mmu,
  121. uint32_t dest_address, uint32_t size)
  122. {
  123. struct msm_smmu *smmu = to_msm_smmu(mmu);
  124. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  125. int ret = 0;
  126. if (!client || !client->domain)
  127. return -ENODEV;
  128. ret = iommu_unmap(client->domain, dest_address, size);
  129. if (ret != size)
  130. pr_err("smmu unmap failed\n");
  131. return 0;
  132. }
  133. static int msm_smmu_one_to_one_map(struct msm_mmu *mmu, uint32_t iova,
  134. uint32_t dest_address, uint32_t size, int prot)
  135. {
  136. struct msm_smmu *smmu = to_msm_smmu(mmu);
  137. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  138. int ret = 0;
  139. if (!client || !client->domain)
  140. return -ENODEV;
  141. ret = iommu_map(client->domain, dest_address, dest_address,
  142. size, prot);
  143. if (ret)
  144. pr_err("smmu map failed\n");
  145. return ret;
  146. }
  147. static int msm_smmu_map(struct msm_mmu *mmu, uint64_t iova,
  148. struct sg_table *sgt, unsigned int len, int prot)
  149. {
  150. struct msm_smmu *smmu = to_msm_smmu(mmu);
  151. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  152. size_t ret = 0;
  153. if (sgt && sgt->sgl) {
  154. ret = iommu_map_sg(client->domain, iova, sgt->sgl,
  155. sgt->orig_nents, prot);
  156. WARN_ON((int)ret < 0);
  157. DRM_DEBUG("%pad/0x%x/0x%x/\n", &sgt->sgl->dma_address,
  158. sgt->sgl->dma_length, prot);
  159. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length, prot);
  160. }
  161. return (ret == len) ? 0 : -EINVAL;
  162. }
  163. static int msm_smmu_unmap(struct msm_mmu *mmu, uint64_t iova,
  164. struct sg_table *sgt, unsigned int len)
  165. {
  166. struct msm_smmu *smmu = to_msm_smmu(mmu);
  167. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  168. pm_runtime_get_sync(mmu->dev);
  169. iommu_unmap(client->domain, iova, len);
  170. pm_runtime_put_sync(mmu->dev);
  171. return 0;
  172. }
  173. static void msm_smmu_destroy(struct msm_mmu *mmu)
  174. {
  175. struct msm_smmu *smmu = to_msm_smmu(mmu);
  176. struct platform_device *pdev = to_platform_device(smmu->client_dev);
  177. if (smmu->client_dev)
  178. platform_device_unregister(pdev);
  179. kfree(smmu);
  180. }
  181. struct device *msm_smmu_get_dev(struct msm_mmu *mmu)
  182. {
  183. struct msm_smmu *smmu = to_msm_smmu(mmu);
  184. return smmu->client_dev;
  185. }
  186. static int msm_smmu_map_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  187. int dir, u32 flags)
  188. {
  189. struct msm_smmu *smmu = to_msm_smmu(mmu);
  190. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  191. unsigned long attrs = 0x0;
  192. int ret;
  193. if (!sgt || !client) {
  194. DRM_ERROR("sg table is invalid\n");
  195. return -ENOMEM;
  196. }
  197. /*
  198. * For import buffer type, dma_map_sg_attrs is called during
  199. * dma_buf_map_attachment and is not required to call again
  200. */
  201. if (!(flags & MSM_BO_EXTBUF)) {
  202. ret = dma_map_sg_attrs(client->dev, sgt->sgl, sgt->nents, dir,
  203. attrs);
  204. if (!ret) {
  205. DRM_ERROR("dma map sg failed\n");
  206. return -ENOMEM;
  207. }
  208. }
  209. if (sgt && sgt->sgl) {
  210. DRM_DEBUG("%pad/0x%x/0x%x/0x%lx\n",
  211. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  212. dir, attrs);
  213. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  214. dir, attrs, client->secure, flags);
  215. }
  216. return 0;
  217. }
  218. static void msm_smmu_unmap_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  219. int dir, u32 flags)
  220. {
  221. struct msm_smmu *smmu = to_msm_smmu(mmu);
  222. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  223. if (!sgt || !client) {
  224. DRM_ERROR("sg table is invalid\n");
  225. return;
  226. }
  227. if (sgt->sgl) {
  228. DRM_DEBUG("%pad/0x%x/0x%x\n",
  229. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  230. dir);
  231. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  232. dir, client->secure, flags);
  233. }
  234. if (!(flags & MSM_BO_EXTBUF))
  235. dma_unmap_sg(client->dev, sgt->sgl, sgt->nents, dir);
  236. }
  237. static bool msm_smmu_is_domain_secure(struct msm_mmu *mmu)
  238. {
  239. struct msm_smmu *smmu = to_msm_smmu(mmu);
  240. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  241. return client->secure;
  242. }
  243. static const struct msm_mmu_funcs funcs = {
  244. .attach = msm_smmu_attach,
  245. .detach = msm_smmu_detach,
  246. .map = msm_smmu_map,
  247. .unmap = msm_smmu_unmap,
  248. .map_dma_buf = msm_smmu_map_dma_buf,
  249. .unmap_dma_buf = msm_smmu_unmap_dma_buf,
  250. .destroy = msm_smmu_destroy,
  251. .is_domain_secure = msm_smmu_is_domain_secure,
  252. .set_attribute = msm_smmu_set_attribute,
  253. .one_to_one_map = msm_smmu_one_to_one_map,
  254. .one_to_one_unmap = msm_smmu_one_to_one_unmap,
  255. .get_dev = msm_smmu_get_dev,
  256. };
  257. static struct msm_smmu_domain msm_smmu_domains[MSM_SMMU_DOMAIN_MAX] = {
  258. [MSM_SMMU_DOMAIN_UNSECURE] = {
  259. .label = "mdp_ns",
  260. .secure = false,
  261. },
  262. [MSM_SMMU_DOMAIN_SECURE] = {
  263. .label = "mdp_s",
  264. .secure = true,
  265. },
  266. [MSM_SMMU_DOMAIN_NRT_UNSECURE] = {
  267. .label = "rot_ns",
  268. .secure = false,
  269. },
  270. [MSM_SMMU_DOMAIN_NRT_SECURE] = {
  271. .label = "rot_s",
  272. .secure = true,
  273. },
  274. };
  275. static const struct of_device_id msm_smmu_dt_match[] = {
  276. { .compatible = "qcom,smmu_sde_unsec",
  277. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_UNSECURE] },
  278. { .compatible = "qcom,smmu_sde_sec",
  279. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_SECURE] },
  280. { .compatible = "qcom,smmu_sde_nrt_unsec",
  281. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_UNSECURE] },
  282. { .compatible = "qcom,smmu_sde_nrt_sec",
  283. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_SECURE] },
  284. {}
  285. };
  286. MODULE_DEVICE_TABLE(of, msm_smmu_dt_match);
  287. static struct msm_smmu_client *msm_smmu_get_smmu(const char *compat)
  288. {
  289. struct msm_smmu_client *curr = NULL;
  290. bool found = false;
  291. if (!compat) {
  292. pr_err("invalid param\n");
  293. return ERR_PTR(-EINVAL);
  294. }
  295. mutex_lock(&smmu_list_lock);
  296. list_for_each_entry(curr, &sde_smmu_list, smmu_list) {
  297. if (of_compat_cmp(compat, curr->compat, strlen(compat)) == 0) {
  298. DRM_DEBUG("found msm_smmu_client for %s\n", compat);
  299. found = true;
  300. break;
  301. }
  302. }
  303. mutex_unlock(&smmu_list_lock);
  304. if (!found)
  305. return ERR_PTR(-ENODEV);
  306. return curr;
  307. }
  308. static struct device *msm_smmu_device_add(struct device *dev,
  309. enum msm_mmu_domain_type domain,
  310. struct msm_smmu *smmu)
  311. {
  312. int i;
  313. const char *compat = NULL;
  314. for (i = 0; i < ARRAY_SIZE(msm_smmu_dt_match); i++) {
  315. if (msm_smmu_dt_match[i].data == &msm_smmu_domains[domain]) {
  316. compat = msm_smmu_dt_match[i].compatible;
  317. break;
  318. }
  319. }
  320. if (!compat) {
  321. DRM_DEBUG("unable to find matching domain for %d\n", domain);
  322. return ERR_PTR(-ENOENT);
  323. }
  324. DRM_DEBUG("found domain %d compat: %s\n", domain, compat);
  325. smmu->client = msm_smmu_get_smmu(compat);
  326. if (IS_ERR_OR_NULL(smmu->client)) {
  327. DRM_ERROR("unable to find domain %d compat: %s\n", domain,
  328. compat);
  329. return ERR_PTR(-ENODEV);
  330. }
  331. return smmu->client->dev;
  332. }
  333. struct msm_mmu *msm_smmu_new(struct device *dev,
  334. enum msm_mmu_domain_type domain)
  335. {
  336. struct msm_smmu *smmu;
  337. struct device *client_dev;
  338. smmu = kzalloc(sizeof(*smmu), GFP_KERNEL);
  339. if (!smmu)
  340. return ERR_PTR(-ENOMEM);
  341. client_dev = msm_smmu_device_add(dev, domain, smmu);
  342. if (IS_ERR_OR_NULL(client_dev)) {
  343. kfree(smmu);
  344. return (void *)client_dev ? : ERR_PTR(-ENODEV);
  345. }
  346. smmu->client_dev = client_dev;
  347. msm_mmu_init(&smmu->base, dev, &funcs);
  348. return &smmu->base;
  349. }
  350. static int msm_smmu_fault_handler(struct iommu_domain *domain,
  351. struct device *dev, unsigned long iova,
  352. int flags, void *token)
  353. {
  354. struct msm_smmu_client *client;
  355. int rc = -EINVAL;
  356. if (!token) {
  357. DRM_ERROR("Error: token is NULL\n");
  358. return -EINVAL;
  359. }
  360. client = (struct msm_smmu_client *)token;
  361. /* see iommu.h for fault flags definition */
  362. SDE_EVT32(iova, flags);
  363. DRM_ERROR("trigger dump, iova=0x%08lx, flags=0x%x\n", iova, flags);
  364. DRM_ERROR("SMMU device:%s", client->dev ? client->dev->kobj.name : "");
  365. /*
  366. * return -ENOSYS to allow smmu driver to dump out useful
  367. * debug info.
  368. */
  369. return rc;
  370. }
  371. /**
  372. * msm_smmu_bind - bind smmu device with controlling device
  373. * @dev: Pointer to base of platform device
  374. * @master: Pointer to container of drm device
  375. * @data: Pointer to private data
  376. * Returns: Zero on success
  377. */
  378. static int msm_smmu_bind(struct device *dev, struct device *master, void *data)
  379. {
  380. return 0;
  381. }
  382. /**
  383. * msm_smmu_unbind - unbind msm_smmu from controlling device
  384. * @dev: Pointer to base of platform device
  385. * @master: Pointer to container of drm device
  386. * @data: Pointer to private data
  387. */
  388. static void msm_smmu_unbind(struct device *dev,
  389. struct device *master, void *data)
  390. {
  391. }
  392. static const struct component_ops msm_smmu_comp_ops = {
  393. .bind = msm_smmu_bind,
  394. .unbind = msm_smmu_unbind,
  395. };
  396. /**
  397. * msm_smmu_probe()
  398. * @pdev: platform device
  399. *
  400. * Each smmu context acts as a separate device and the context banks are
  401. * configured with a VA range.
  402. * Registers the clks as each context bank has its own clks, for which voting
  403. * has to be done everytime before using that context bank.
  404. */
  405. static int msm_smmu_probe(struct platform_device *pdev)
  406. {
  407. const struct of_device_id *match;
  408. struct msm_smmu_client *client;
  409. const struct msm_smmu_domain *domain;
  410. int ret;
  411. match = of_match_device(msm_smmu_dt_match, &pdev->dev);
  412. if (!match || !match->data) {
  413. dev_err(&pdev->dev, "probe failed as match data is invalid\n");
  414. return -EINVAL;
  415. }
  416. domain = match->data;
  417. if (!domain) {
  418. dev_err(&pdev->dev, "no matching device found\n");
  419. return -EINVAL;
  420. }
  421. DRM_INFO("probing device %s\n", match->compatible);
  422. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  423. if (!client)
  424. return -ENOMEM;
  425. client->dev = &pdev->dev;
  426. client->domain = iommu_get_domain_for_dev(client->dev);
  427. if (!client->domain) {
  428. dev_err(&pdev->dev, "iommu get domain for dev failed\n");
  429. return -EINVAL;
  430. }
  431. client->compat = match->compatible;
  432. client->secure = domain->secure;
  433. client->domain_attached = true;
  434. if (!client->dev->dma_parms)
  435. client->dev->dma_parms = devm_kzalloc(client->dev,
  436. sizeof(*client->dev->dma_parms), GFP_KERNEL);
  437. dma_set_max_seg_size(client->dev, DMA_BIT_MASK(32));
  438. dma_set_seg_boundary(client->dev, (unsigned long)DMA_BIT_MASK(64));
  439. iommu_set_fault_handler(client->domain,
  440. msm_smmu_fault_handler, (void *)client);
  441. DRM_INFO("Created domain %s, secure=%d\n",
  442. domain->label, domain->secure);
  443. platform_set_drvdata(pdev, client);
  444. mutex_lock(&smmu_list_lock);
  445. list_add(&client->smmu_list, &sde_smmu_list);
  446. mutex_unlock(&smmu_list_lock);
  447. ret = component_add(&pdev->dev, &msm_smmu_comp_ops);
  448. if (ret)
  449. pr_err("component add failed\n");
  450. return ret;
  451. }
  452. static int msm_smmu_remove(struct platform_device *pdev)
  453. {
  454. struct msm_smmu_client *client;
  455. struct msm_smmu_client *curr, *next;
  456. client = platform_get_drvdata(pdev);
  457. client->domain_attached = false;
  458. mutex_lock(&smmu_list_lock);
  459. list_for_each_entry_safe(curr, next, &sde_smmu_list, smmu_list) {
  460. if (curr == client) {
  461. list_del(&client->smmu_list);
  462. break;
  463. }
  464. }
  465. mutex_unlock(&smmu_list_lock);
  466. return 0;
  467. }
  468. static struct platform_driver msm_smmu_driver = {
  469. .probe = msm_smmu_probe,
  470. .remove = msm_smmu_remove,
  471. .driver = {
  472. .name = "msmdrm_smmu",
  473. .of_match_table = msm_smmu_dt_match,
  474. .suppress_bind_attrs = true,
  475. },
  476. };
  477. int __init msm_smmu_driver_init(void)
  478. {
  479. int ret;
  480. ret = platform_driver_register(&msm_smmu_driver);
  481. if (ret)
  482. pr_err("mdss_smmu_register_driver() failed!\n");
  483. return ret;
  484. }
  485. void __exit msm_smmu_driver_cleanup(void)
  486. {
  487. platform_driver_unregister(&msm_smmu_driver);
  488. }
  489. MODULE_LICENSE("GPL v2");
  490. MODULE_DESCRIPTION("MSM SMMU driver");