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/msm_dma_iommu_mapping.h>
  22. #include <linux/dma-mapping.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. const char *compat;
  31. struct iommu_domain *domain;
  32. const struct dma_map_ops *dma_ops;
  33. bool domain_attached;
  34. bool secure;
  35. struct list_head smmu_list;
  36. };
  37. struct msm_smmu {
  38. struct msm_mmu base;
  39. struct device *client_dev;
  40. struct msm_smmu_client *client;
  41. };
  42. struct msm_smmu_domain {
  43. const char *label;
  44. bool secure;
  45. };
  46. #define to_msm_smmu(x) container_of(x, struct msm_smmu, base)
  47. #define msm_smmu_to_client(smmu) (smmu->client)
  48. /* Serialization lock for smmu_list */
  49. static DEFINE_MUTEX(smmu_list_lock);
  50. /* List of all smmu devices installed */
  51. static LIST_HEAD(sde_smmu_list);
  52. static int msm_smmu_attach(struct msm_mmu *mmu, const char * const *names,
  53. int cnt)
  54. {
  55. struct msm_smmu *smmu = to_msm_smmu(mmu);
  56. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  57. int rc = 0;
  58. if (!client) {
  59. pr_err("undefined smmu client\n");
  60. return -EINVAL;
  61. }
  62. /* domain attach only once */
  63. if (client->domain_attached)
  64. return 0;
  65. if (client->dma_ops) {
  66. set_dma_ops(client->dev, client->dma_ops);
  67. client->dma_ops = NULL;
  68. dev_dbg(client->dev, "iommu domain ops restored\n");
  69. }
  70. rc = iommu_attach_device(client->domain, client->dev);
  71. if (rc) {
  72. dev_err(client->dev, "iommu attach dev failed (%d)\n", rc);
  73. return rc;
  74. }
  75. client->domain_attached = true;
  76. dev_dbg(client->dev, "iommu domain attached\n");
  77. return 0;
  78. }
  79. static void msm_smmu_detach(struct msm_mmu *mmu, const char * const *names,
  80. int cnt)
  81. {
  82. struct msm_smmu *smmu = to_msm_smmu(mmu);
  83. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  84. if (!client) {
  85. pr_err("undefined smmu client\n");
  86. return;
  87. }
  88. if (!client->domain_attached)
  89. return;
  90. pm_runtime_get_sync(mmu->dev);
  91. msm_dma_unmap_all_for_dev(client->dev);
  92. iommu_detach_device(client->domain, client->dev);
  93. client->dma_ops = get_dma_ops(client->dev);
  94. if (client->dma_ops) {
  95. set_dma_ops(client->dev, NULL);
  96. dev_dbg(client->dev, "iommu domain ops removed\n");
  97. }
  98. pm_runtime_put_sync(mmu->dev);
  99. client->domain_attached = false;
  100. dev_dbg(client->dev, "iommu domain detached\n");
  101. }
  102. static int msm_smmu_set_attribute(struct msm_mmu *mmu,
  103. enum iommu_attr attr, void *data)
  104. {
  105. struct msm_smmu *smmu = to_msm_smmu(mmu);
  106. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  107. int ret = 0;
  108. if (!client || !client->domain)
  109. return -ENODEV;
  110. ret = iommu_domain_set_attr(client->domain, attr, data);
  111. if (ret)
  112. DRM_ERROR("set domain attribute failed:%d\n", ret);
  113. return ret;
  114. }
  115. static int msm_smmu_one_to_one_unmap(struct msm_mmu *mmu,
  116. uint32_t dest_address, uint32_t size)
  117. {
  118. struct msm_smmu *smmu = to_msm_smmu(mmu);
  119. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  120. int ret = 0;
  121. if (!client || !client->domain)
  122. return -ENODEV;
  123. ret = iommu_unmap(client->domain, dest_address, size);
  124. if (ret != size)
  125. pr_err("smmu unmap failed\n");
  126. return 0;
  127. }
  128. static int msm_smmu_one_to_one_map(struct msm_mmu *mmu, uint32_t iova,
  129. uint32_t dest_address, uint32_t size, int prot)
  130. {
  131. struct msm_smmu *smmu = to_msm_smmu(mmu);
  132. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  133. int ret = 0;
  134. if (!client || !client->domain)
  135. return -ENODEV;
  136. ret = iommu_map(client->domain, dest_address, dest_address,
  137. size, prot);
  138. if (ret)
  139. pr_err("smmu map failed\n");
  140. return ret;
  141. }
  142. static int msm_smmu_map(struct msm_mmu *mmu, uint64_t iova,
  143. struct sg_table *sgt, unsigned int len, int prot)
  144. {
  145. struct msm_smmu *smmu = to_msm_smmu(mmu);
  146. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  147. size_t ret = 0;
  148. if (sgt && sgt->sgl) {
  149. ret = iommu_map_sg(client->domain, iova, sgt->sgl,
  150. sgt->orig_nents, prot);
  151. WARN_ON((int)ret < 0);
  152. DRM_DEBUG("%pad/0x%x/0x%x/\n", &sgt->sgl->dma_address,
  153. sgt->sgl->dma_length, prot);
  154. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length, prot);
  155. }
  156. return (ret == len) ? 0 : -EINVAL;
  157. }
  158. static int msm_smmu_unmap(struct msm_mmu *mmu, uint64_t iova,
  159. struct sg_table *sgt, unsigned int len)
  160. {
  161. struct msm_smmu *smmu = to_msm_smmu(mmu);
  162. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  163. pm_runtime_get_sync(mmu->dev);
  164. iommu_unmap(client->domain, iova, len);
  165. pm_runtime_put_sync(mmu->dev);
  166. return 0;
  167. }
  168. static void msm_smmu_destroy(struct msm_mmu *mmu)
  169. {
  170. struct msm_smmu *smmu = to_msm_smmu(mmu);
  171. struct platform_device *pdev = to_platform_device(smmu->client_dev);
  172. if (smmu->client_dev)
  173. platform_device_unregister(pdev);
  174. kfree(smmu);
  175. }
  176. struct device *msm_smmu_get_dev(struct msm_mmu *mmu)
  177. {
  178. struct msm_smmu *smmu = to_msm_smmu(mmu);
  179. return smmu->client_dev;
  180. }
  181. static int msm_smmu_map_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  182. int dir, u32 flags)
  183. {
  184. struct msm_smmu *smmu = to_msm_smmu(mmu);
  185. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  186. unsigned long attrs = 0x0;
  187. int ret;
  188. if (!sgt || !client) {
  189. DRM_ERROR("sg table is invalid\n");
  190. return -ENOMEM;
  191. }
  192. if (flags & MSM_BO_KEEPATTRS)
  193. attrs |= DMA_ATTR_IOMMU_USE_LLC_NWA;
  194. /*
  195. * For import buffer type, dma_map_sg_attrs is called during
  196. * dma_buf_map_attachment and is not required to call again
  197. */
  198. if (!(flags & MSM_BO_EXTBUF)) {
  199. ret = dma_map_sg_attrs(client->dev, sgt->sgl, sgt->nents, dir,
  200. attrs);
  201. if (!ret) {
  202. DRM_ERROR("dma map sg failed\n");
  203. return -ENOMEM;
  204. }
  205. }
  206. if (sgt && sgt->sgl) {
  207. DRM_DEBUG("%pad/0x%x/0x%x/0x%lx\n",
  208. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  209. dir, attrs);
  210. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  211. dir, attrs, client->secure, flags);
  212. }
  213. return 0;
  214. }
  215. static void msm_smmu_unmap_dma_buf(struct msm_mmu *mmu, struct sg_table *sgt,
  216. int dir, u32 flags)
  217. {
  218. struct msm_smmu *smmu = to_msm_smmu(mmu);
  219. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  220. if (!sgt || !client) {
  221. DRM_ERROR("sg table is invalid\n");
  222. return;
  223. }
  224. if (sgt->sgl) {
  225. DRM_DEBUG("%pad/0x%x/0x%x\n",
  226. &sgt->sgl->dma_address, sgt->sgl->dma_length,
  227. dir);
  228. SDE_EVT32(sgt->sgl->dma_address, sgt->sgl->dma_length,
  229. dir, client->secure, flags);
  230. }
  231. if (!(flags & MSM_BO_EXTBUF))
  232. dma_unmap_sg(client->dev, sgt->sgl, sgt->nents, dir);
  233. }
  234. static bool msm_smmu_is_domain_secure(struct msm_mmu *mmu)
  235. {
  236. struct msm_smmu *smmu = to_msm_smmu(mmu);
  237. struct msm_smmu_client *client = msm_smmu_to_client(smmu);
  238. return client->secure;
  239. }
  240. static const struct msm_mmu_funcs funcs = {
  241. .attach = msm_smmu_attach,
  242. .detach = msm_smmu_detach,
  243. .map = msm_smmu_map,
  244. .unmap = msm_smmu_unmap,
  245. .map_dma_buf = msm_smmu_map_dma_buf,
  246. .unmap_dma_buf = msm_smmu_unmap_dma_buf,
  247. .destroy = msm_smmu_destroy,
  248. .is_domain_secure = msm_smmu_is_domain_secure,
  249. .set_attribute = msm_smmu_set_attribute,
  250. .one_to_one_map = msm_smmu_one_to_one_map,
  251. .one_to_one_unmap = msm_smmu_one_to_one_unmap,
  252. .get_dev = msm_smmu_get_dev,
  253. };
  254. static struct msm_smmu_domain msm_smmu_domains[MSM_SMMU_DOMAIN_MAX] = {
  255. [MSM_SMMU_DOMAIN_UNSECURE] = {
  256. .label = "mdp_ns",
  257. .secure = false,
  258. },
  259. [MSM_SMMU_DOMAIN_SECURE] = {
  260. .label = "mdp_s",
  261. .secure = true,
  262. },
  263. [MSM_SMMU_DOMAIN_NRT_UNSECURE] = {
  264. .label = "rot_ns",
  265. .secure = false,
  266. },
  267. [MSM_SMMU_DOMAIN_NRT_SECURE] = {
  268. .label = "rot_s",
  269. .secure = true,
  270. },
  271. };
  272. static const struct of_device_id msm_smmu_dt_match[] = {
  273. { .compatible = "qcom,smmu_sde_unsec",
  274. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_UNSECURE] },
  275. { .compatible = "qcom,smmu_sde_sec",
  276. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_SECURE] },
  277. { .compatible = "qcom,smmu_sde_nrt_unsec",
  278. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_UNSECURE] },
  279. { .compatible = "qcom,smmu_sde_nrt_sec",
  280. .data = &msm_smmu_domains[MSM_SMMU_DOMAIN_NRT_SECURE] },
  281. {}
  282. };
  283. MODULE_DEVICE_TABLE(of, msm_smmu_dt_match);
  284. static struct msm_smmu_client *msm_smmu_get_smmu(const char *compat)
  285. {
  286. struct msm_smmu_client *curr = NULL;
  287. bool found = false;
  288. if (!compat) {
  289. pr_err("invalid param\n");
  290. return ERR_PTR(-EINVAL);
  291. }
  292. mutex_lock(&smmu_list_lock);
  293. list_for_each_entry(curr, &sde_smmu_list, smmu_list) {
  294. if (of_compat_cmp(compat, curr->compat, strlen(compat)) == 0) {
  295. DRM_DEBUG("found msm_smmu_client for %s\n", compat);
  296. found = true;
  297. break;
  298. }
  299. }
  300. mutex_unlock(&smmu_list_lock);
  301. if (!found)
  302. return ERR_PTR(-ENODEV);
  303. return curr;
  304. }
  305. static struct device *msm_smmu_device_add(struct device *dev,
  306. enum msm_mmu_domain_type domain,
  307. struct msm_smmu *smmu)
  308. {
  309. int i;
  310. const char *compat = NULL;
  311. for (i = 0; i < ARRAY_SIZE(msm_smmu_dt_match); i++) {
  312. if (msm_smmu_dt_match[i].data == &msm_smmu_domains[domain]) {
  313. compat = msm_smmu_dt_match[i].compatible;
  314. break;
  315. }
  316. }
  317. if (!compat) {
  318. DRM_DEBUG("unable to find matching domain for %d\n", domain);
  319. return ERR_PTR(-ENOENT);
  320. }
  321. DRM_DEBUG("found domain %d compat: %s\n", domain, compat);
  322. smmu->client = msm_smmu_get_smmu(compat);
  323. if (IS_ERR_OR_NULL(smmu->client)) {
  324. DRM_ERROR("unable to find domain %d compat: %s\n", domain,
  325. compat);
  326. return ERR_PTR(-ENODEV);
  327. }
  328. return smmu->client->dev;
  329. }
  330. struct msm_mmu *msm_smmu_new(struct device *dev,
  331. enum msm_mmu_domain_type domain)
  332. {
  333. struct msm_smmu *smmu;
  334. struct device *client_dev;
  335. smmu = kzalloc(sizeof(*smmu), GFP_KERNEL);
  336. if (!smmu)
  337. return ERR_PTR(-ENOMEM);
  338. client_dev = msm_smmu_device_add(dev, domain, smmu);
  339. if (IS_ERR_OR_NULL(client_dev)) {
  340. kfree(smmu);
  341. return (void *)client_dev ? : ERR_PTR(-ENODEV);
  342. }
  343. smmu->client_dev = client_dev;
  344. msm_mmu_init(&smmu->base, dev, &funcs);
  345. return &smmu->base;
  346. }
  347. static int msm_smmu_fault_handler(struct iommu_domain *domain,
  348. struct device *dev, unsigned long iova,
  349. int flags, void *token)
  350. {
  351. struct msm_smmu_client *client;
  352. int rc = -EINVAL;
  353. if (!token) {
  354. DRM_ERROR("Error: token is NULL\n");
  355. return -EINVAL;
  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 rc;
  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");