msm_smmu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. if (flags & MSM_BO_KEEPATTRS)
  198. attrs |= DMA_ATTR_IOMMU_USE_LLC_NWA;
  199. /*
  200. * For import buffer type, dma_map_sg_attrs is called during
  201. * dma_buf_map_attachment and is not required to call again
  202. */
  203. if (!(flags & MSM_BO_EXTBUF)) {
  204. ret = dma_map_sg_attrs(client->dev, sgt->sgl, sgt->nents, dir,
  205. attrs);
  206. if (!ret) {
  207. DRM_ERROR("dma map sg failed\n");
  208. return -ENOMEM;
  209. }
  210. }
  211. if (sgt && sgt->sgl) {
  212. DRM_DEBUG("%pad/0x%x/0x%x/0x%lx\n",
  213. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  214. dir, attrs);
  215. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  216. dir, attrs, client->secure, flags);
  217. }
  218. return 0;
  219. }
  220. static void msm_smmu_unmap_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  221. int dir, u32 flags)
  222. {
  223. struct msm_smmu *smmu = to_msm_smmu(mmu);
  224. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  225. if (!sgt || !client) {
  226. DRM_ERROR("sg table is invalid\n");
  227. return;
  228. }
  229. if (sgt->sgl) {
  230. DRM_DEBUG("%pad/0x%x/0x%x\n",
  231. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  232. dir);
  233. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  234. dir, client->secure, flags);
  235. }
  236. if (!(flags & MSM_BO_EXTBUF))
  237. dma_unmap_sg(client->dev, sgt->sgl, sgt->nents, dir);
  238. }
  239. static bool msm_smmu_is_domain_secure(struct msm_mmu *mmu)
  240. {
  241. struct msm_smmu *smmu = to_msm_smmu(mmu);
  242. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  243. return client->secure;
  244. }
  245. static const struct msm_mmu_funcs funcs = {
  246. .attach = msm_smmu_attach,
  247. .detach = msm_smmu_detach,
  248. .map = msm_smmu_map,
  249. .unmap = msm_smmu_unmap,
  250. .map_dma_buf = msm_smmu_map_dma_buf,
  251. .unmap_dma_buf = msm_smmu_unmap_dma_buf,
  252. .destroy = msm_smmu_destroy,
  253. .is_domain_secure = msm_smmu_is_domain_secure,
  254. .set_attribute = msm_smmu_set_attribute,
  255. .one_to_one_map = msm_smmu_one_to_one_map,
  256. .one_to_one_unmap = msm_smmu_one_to_one_unmap,
  257. .get_dev = msm_smmu_get_dev,
  258. };
  259. static struct msm_smmu_domain msm_smmu_domains[MSM_SMMU_DOMAIN_MAX] = {
  260. [MSM_SMMU_DOMAIN_UNSECURE] = {
  261. .label = "mdp_ns",
  262. .secure = false,
  263. },
  264. [MSM_SMMU_DOMAIN_SECURE] = {
  265. .label = "mdp_s",
  266. .secure = true,
  267. },
  268. [MSM_SMMU_DOMAIN_NRT_UNSECURE] = {
  269. .label = "rot_ns",
  270. .secure = false,
  271. },
  272. [MSM_SMMU_DOMAIN_NRT_SECURE] = {
  273. .label = "rot_s",
  274. .secure = true,
  275. },
  276. };
  277. static const struct of_device_id msm_smmu_dt_match[] = {
  278. { .compatible = "qcom,smmu_sde_unsec",
  279. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_UNSECURE] },
  280. { .compatible = "qcom,smmu_sde_sec",
  281. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_SECURE] },
  282. { .compatible = "qcom,smmu_sde_nrt_unsec",
  283. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_UNSECURE] },
  284. { .compatible = "qcom,smmu_sde_nrt_sec",
  285. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_SECURE] },
  286. {}
  287. };
  288. MODULE_DEVICE_TABLE(of, msm_smmu_dt_match);
  289. static struct msm_smmu_client *msm_smmu_get_smmu(const char *compat)
  290. {
  291. struct msm_smmu_client *curr = NULL;
  292. bool found = false;
  293. if (!compat) {
  294. pr_err("invalid param\n");
  295. return ERR_PTR(-EINVAL);
  296. }
  297. mutex_lock(&smmu_list_lock);
  298. list_for_each_entry(curr, &sde_smmu_list, smmu_list) {
  299. if (of_compat_cmp(compat, curr->compat, strlen(compat)) == 0) {
  300. DRM_DEBUG("found msm_smmu_client for %s\n", compat);
  301. found = true;
  302. break;
  303. }
  304. }
  305. mutex_unlock(&smmu_list_lock);
  306. if (!found)
  307. return ERR_PTR(-ENODEV);
  308. return curr;
  309. }
  310. static struct device *msm_smmu_device_add(struct device *dev,
  311. enum msm_mmu_domain_type domain,
  312. struct msm_smmu *smmu)
  313. {
  314. int i;
  315. const char *compat = NULL;
  316. for (i = 0; i < ARRAY_SIZE(msm_smmu_dt_match); i++) {
  317. if (msm_smmu_dt_match[i].data == &msm_smmu_domains[domain]) {
  318. compat = msm_smmu_dt_match[i].compatible;
  319. break;
  320. }
  321. }
  322. if (!compat) {
  323. DRM_DEBUG("unable to find matching domain for %d\n", domain);
  324. return ERR_PTR(-ENOENT);
  325. }
  326. DRM_DEBUG("found domain %d compat: %s\n", domain, compat);
  327. smmu->client = msm_smmu_get_smmu(compat);
  328. if (IS_ERR_OR_NULL(smmu->client)) {
  329. DRM_ERROR("unable to find domain %d compat: %s\n", domain,
  330. compat);
  331. return ERR_PTR(-ENODEV);
  332. }
  333. return smmu->client->dev;
  334. }
  335. struct msm_mmu *msm_smmu_new(struct device *dev,
  336. enum msm_mmu_domain_type domain)
  337. {
  338. struct msm_smmu *smmu;
  339. struct device *client_dev;
  340. smmu = kzalloc(sizeof(*smmu), GFP_KERNEL);
  341. if (!smmu)
  342. return ERR_PTR(-ENOMEM);
  343. client_dev = msm_smmu_device_add(dev, domain, smmu);
  344. if (IS_ERR_OR_NULL(client_dev)) {
  345. kfree(smmu);
  346. return (void *)client_dev ? : ERR_PTR(-ENODEV);
  347. }
  348. smmu->client_dev = client_dev;
  349. msm_mmu_init(&smmu->base, dev, &funcs);
  350. return &smmu->base;
  351. }
  352. static int msm_smmu_fault_handler(struct iommu_domain *domain,
  353. struct device *dev, unsigned long iova,
  354. int flags, void *token)
  355. {
  356. struct msm_smmu_client *client;
  357. if (!token) {
  358. DRM_ERROR("Error: token is NULL\n");
  359. return -ENOSYS;
  360. }
  361. client = (struct msm_smmu_client *)token;
  362. /* see iommu.h for fault flags definition */
  363. SDE_EVT32(iova, flags);
  364. DRM_ERROR("trigger dump, iova=0x%08lx, flags=0x%x\n", iova, flags);
  365. DRM_ERROR("SMMU device:%s", client->dev ? client->dev->kobj.name : "");
  366. /*
  367. * return -ENOSYS to allow smmu driver to dump out useful
  368. * debug info.
  369. */
  370. return -ENOSYS;
  371. }
  372. /**
  373. * msm_smmu_bind - bind smmu device with controlling device
  374. * @dev: Pointer to base of platform device
  375. * @master: Pointer to container of drm device
  376. * @data: Pointer to private data
  377. * Returns: Zero on success
  378. */
  379. static int msm_smmu_bind(struct device *dev, struct device *master, void *data)
  380. {
  381. return 0;
  382. }
  383. /**
  384. * msm_smmu_unbind - unbind msm_smmu from controlling device
  385. * @dev: Pointer to base of platform device
  386. * @master: Pointer to container of drm device
  387. * @data: Pointer to private data
  388. */
  389. static void msm_smmu_unbind(struct device *dev,
  390. struct device *master, void *data)
  391. {
  392. }
  393. static const struct component_ops msm_smmu_comp_ops = {
  394. .bind = msm_smmu_bind,
  395. .unbind = msm_smmu_unbind,
  396. };
  397. /**
  398. * msm_smmu_probe()
  399. * @pdev: platform device
  400. *
  401. * Each smmu context acts as a separate device and the context banks are
  402. * configured with a VA range.
  403. * Registers the clks as each context bank has its own clks, for which voting
  404. * has to be done everytime before using that context bank.
  405. */
  406. static int msm_smmu_probe(struct platform_device *pdev)
  407. {
  408. const struct of_device_id *match;
  409. struct msm_smmu_client *client;
  410. const struct msm_smmu_domain *domain;
  411. int ret;
  412. match = of_match_device(msm_smmu_dt_match, &pdev->dev);
  413. if (!match || !match->data) {
  414. dev_err(&pdev->dev, "probe failed as match data is invalid\n");
  415. return -EINVAL;
  416. }
  417. domain = match->data;
  418. if (!domain) {
  419. dev_err(&pdev->dev, "no matching device found\n");
  420. return -EINVAL;
  421. }
  422. DRM_INFO("probing device %s\n", match->compatible);
  423. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  424. if (!client)
  425. return -ENOMEM;
  426. client->dev = &pdev->dev;
  427. client->domain = iommu_get_domain_for_dev(client->dev);
  428. if (!client->domain) {
  429. dev_err(&pdev->dev, "iommu get domain for dev failed\n");
  430. return -EINVAL;
  431. }
  432. client->compat = match->compatible;
  433. client->secure = domain->secure;
  434. client->domain_attached = true;
  435. if (!client->dev->dma_parms)
  436. client->dev->dma_parms = devm_kzalloc(client->dev,
  437. sizeof(*client->dev->dma_parms), GFP_KERNEL);
  438. dma_set_max_seg_size(client->dev, DMA_BIT_MASK(32));
  439. dma_set_seg_boundary(client->dev, (unsigned long)DMA_BIT_MASK(64));
  440. iommu_set_fault_handler(client->domain,
  441. msm_smmu_fault_handler, (void *)client);
  442. DRM_INFO("Created domain %s, secure=%d\n",
  443. domain->label, domain->secure);
  444. platform_set_drvdata(pdev, client);
  445. mutex_lock(&smmu_list_lock);
  446. list_add(&client->smmu_list, &sde_smmu_list);
  447. mutex_unlock(&smmu_list_lock);
  448. ret = component_add(&pdev->dev, &msm_smmu_comp_ops);
  449. if (ret)
  450. pr_err("component add failed\n");
  451. return ret;
  452. }
  453. static int msm_smmu_remove(struct platform_device *pdev)
  454. {
  455. struct msm_smmu_client *client;
  456. struct msm_smmu_client *curr, *next;
  457. client = platform_get_drvdata(pdev);
  458. client->domain_attached = false;
  459. mutex_lock(&smmu_list_lock);
  460. list_for_each_entry_safe(curr, next, &sde_smmu_list, smmu_list) {
  461. if (curr == client) {
  462. list_del(&client->smmu_list);
  463. break;
  464. }
  465. }
  466. mutex_unlock(&smmu_list_lock);
  467. return 0;
  468. }
  469. static struct platform_driver msm_smmu_driver = {
  470. .probe = msm_smmu_probe,
  471. .remove = msm_smmu_remove,
  472. .driver = {
  473. .name = "msmdrm_smmu",
  474. .of_match_table = msm_smmu_dt_match,
  475. .suppress_bind_attrs = true,
  476. },
  477. };
  478. int __init msm_smmu_driver_init(void)
  479. {
  480. int ret;
  481. ret = platform_driver_register(&msm_smmu_driver);
  482. if (ret)
  483. pr_err("mdss_smmu_register_driver() failed!\n");
  484. return ret;
  485. }
  486. void __exit msm_smmu_driver_cleanup(void)
  487. {
  488. platform_driver_unregister(&msm_smmu_driver);
  489. }
  490. MODULE_LICENSE("GPL v2");
  491. MODULE_DESCRIPTION("MSM SMMU driver");