msm_smmu.c 14 KB

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