msm_smmu.c 15 KB

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