tegra-gart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * IOMMU API for GART in Tegra20
  3. *
  4. * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope 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, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #define pr_fmt(fmt) "%s(): " fmt, __func__
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mm.h>
  26. #include <linux/list.h>
  27. #include <linux/device.h>
  28. #include <linux/io.h>
  29. #include <linux/iommu.h>
  30. #include <linux/of.h>
  31. #include <asm/cacheflush.h>
  32. /* bitmap of the page sizes currently supported */
  33. #define GART_IOMMU_PGSIZES (SZ_4K)
  34. #define GART_REG_BASE 0x24
  35. #define GART_CONFIG (0x24 - GART_REG_BASE)
  36. #define GART_ENTRY_ADDR (0x28 - GART_REG_BASE)
  37. #define GART_ENTRY_DATA (0x2c - GART_REG_BASE)
  38. #define GART_ENTRY_PHYS_ADDR_VALID (1 << 31)
  39. #define GART_PAGE_SHIFT 12
  40. #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT)
  41. #define GART_PAGE_MASK \
  42. (~(GART_PAGE_SIZE - 1) & ~GART_ENTRY_PHYS_ADDR_VALID)
  43. struct gart_client {
  44. struct device *dev;
  45. struct list_head list;
  46. };
  47. struct gart_device {
  48. void __iomem *regs;
  49. u32 *savedata;
  50. u32 page_count; /* total remappable size */
  51. dma_addr_t iovmm_base; /* offset to vmm_area */
  52. spinlock_t pte_lock; /* for pagetable */
  53. struct list_head client;
  54. spinlock_t client_lock; /* for client list */
  55. struct device *dev;
  56. struct iommu_device iommu; /* IOMMU Core handle */
  57. };
  58. struct gart_domain {
  59. struct iommu_domain domain; /* generic domain handle */
  60. struct gart_device *gart; /* link to gart device */
  61. };
  62. static struct gart_device *gart_handle; /* unique for a system */
  63. static bool gart_debug;
  64. #define GART_PTE(_pfn) \
  65. (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT))
  66. static struct gart_domain *to_gart_domain(struct iommu_domain *dom)
  67. {
  68. return container_of(dom, struct gart_domain, domain);
  69. }
  70. /*
  71. * Any interaction between any block on PPSB and a block on APB or AHB
  72. * must have these read-back to ensure the APB/AHB bus transaction is
  73. * complete before initiating activity on the PPSB block.
  74. */
  75. #define FLUSH_GART_REGS(gart) ((void)readl((gart)->regs + GART_CONFIG))
  76. #define for_each_gart_pte(gart, iova) \
  77. for (iova = gart->iovmm_base; \
  78. iova < gart->iovmm_base + GART_PAGE_SIZE * gart->page_count; \
  79. iova += GART_PAGE_SIZE)
  80. static inline void gart_set_pte(struct gart_device *gart,
  81. unsigned long offs, u32 pte)
  82. {
  83. writel(offs, gart->regs + GART_ENTRY_ADDR);
  84. writel(pte, gart->regs + GART_ENTRY_DATA);
  85. dev_dbg(gart->dev, "%s %08lx:%08x\n",
  86. pte ? "map" : "unmap", offs, pte & GART_PAGE_MASK);
  87. }
  88. static inline unsigned long gart_read_pte(struct gart_device *gart,
  89. unsigned long offs)
  90. {
  91. unsigned long pte;
  92. writel(offs, gart->regs + GART_ENTRY_ADDR);
  93. pte = readl(gart->regs + GART_ENTRY_DATA);
  94. return pte;
  95. }
  96. static void do_gart_setup(struct gart_device *gart, const u32 *data)
  97. {
  98. unsigned long iova;
  99. for_each_gart_pte(gart, iova)
  100. gart_set_pte(gart, iova, data ? *(data++) : 0);
  101. writel(1, gart->regs + GART_CONFIG);
  102. FLUSH_GART_REGS(gart);
  103. }
  104. #ifdef DEBUG
  105. static void gart_dump_table(struct gart_device *gart)
  106. {
  107. unsigned long iova;
  108. unsigned long flags;
  109. spin_lock_irqsave(&gart->pte_lock, flags);
  110. for_each_gart_pte(gart, iova) {
  111. unsigned long pte;
  112. pte = gart_read_pte(gart, iova);
  113. dev_dbg(gart->dev, "%s %08lx:%08lx\n",
  114. (GART_ENTRY_PHYS_ADDR_VALID & pte) ? "v" : " ",
  115. iova, pte & GART_PAGE_MASK);
  116. }
  117. spin_unlock_irqrestore(&gart->pte_lock, flags);
  118. }
  119. #else
  120. static inline void gart_dump_table(struct gart_device *gart)
  121. {
  122. }
  123. #endif
  124. static inline bool gart_iova_range_valid(struct gart_device *gart,
  125. unsigned long iova, size_t bytes)
  126. {
  127. unsigned long iova_start, iova_end, gart_start, gart_end;
  128. iova_start = iova;
  129. iova_end = iova_start + bytes - 1;
  130. gart_start = gart->iovmm_base;
  131. gart_end = gart_start + gart->page_count * GART_PAGE_SIZE - 1;
  132. if (iova_start < gart_start)
  133. return false;
  134. if (iova_end > gart_end)
  135. return false;
  136. return true;
  137. }
  138. static int gart_iommu_attach_dev(struct iommu_domain *domain,
  139. struct device *dev)
  140. {
  141. struct gart_domain *gart_domain = to_gart_domain(domain);
  142. struct gart_device *gart = gart_domain->gart;
  143. struct gart_client *client, *c;
  144. int err = 0;
  145. client = devm_kzalloc(gart->dev, sizeof(*c), GFP_KERNEL);
  146. if (!client)
  147. return -ENOMEM;
  148. client->dev = dev;
  149. spin_lock(&gart->client_lock);
  150. list_for_each_entry(c, &gart->client, list) {
  151. if (c->dev == dev) {
  152. dev_err(gart->dev,
  153. "%s is already attached\n", dev_name(dev));
  154. err = -EINVAL;
  155. goto fail;
  156. }
  157. }
  158. list_add(&client->list, &gart->client);
  159. spin_unlock(&gart->client_lock);
  160. dev_dbg(gart->dev, "Attached %s\n", dev_name(dev));
  161. return 0;
  162. fail:
  163. devm_kfree(gart->dev, client);
  164. spin_unlock(&gart->client_lock);
  165. return err;
  166. }
  167. static void gart_iommu_detach_dev(struct iommu_domain *domain,
  168. struct device *dev)
  169. {
  170. struct gart_domain *gart_domain = to_gart_domain(domain);
  171. struct gart_device *gart = gart_domain->gart;
  172. struct gart_client *c;
  173. spin_lock(&gart->client_lock);
  174. list_for_each_entry(c, &gart->client, list) {
  175. if (c->dev == dev) {
  176. list_del(&c->list);
  177. devm_kfree(gart->dev, c);
  178. dev_dbg(gart->dev, "Detached %s\n", dev_name(dev));
  179. goto out;
  180. }
  181. }
  182. dev_err(gart->dev, "Couldn't find\n");
  183. out:
  184. spin_unlock(&gart->client_lock);
  185. }
  186. static struct iommu_domain *gart_iommu_domain_alloc(unsigned type)
  187. {
  188. struct gart_domain *gart_domain;
  189. struct gart_device *gart;
  190. if (type != IOMMU_DOMAIN_UNMANAGED)
  191. return NULL;
  192. gart = gart_handle;
  193. if (!gart)
  194. return NULL;
  195. gart_domain = kzalloc(sizeof(*gart_domain), GFP_KERNEL);
  196. if (!gart_domain)
  197. return NULL;
  198. gart_domain->gart = gart;
  199. gart_domain->domain.geometry.aperture_start = gart->iovmm_base;
  200. gart_domain->domain.geometry.aperture_end = gart->iovmm_base +
  201. gart->page_count * GART_PAGE_SIZE - 1;
  202. gart_domain->domain.geometry.force_aperture = true;
  203. return &gart_domain->domain;
  204. }
  205. static void gart_iommu_domain_free(struct iommu_domain *domain)
  206. {
  207. struct gart_domain *gart_domain = to_gart_domain(domain);
  208. struct gart_device *gart = gart_domain->gart;
  209. if (gart) {
  210. spin_lock(&gart->client_lock);
  211. if (!list_empty(&gart->client)) {
  212. struct gart_client *c;
  213. list_for_each_entry(c, &gart->client, list)
  214. gart_iommu_detach_dev(domain, c->dev);
  215. }
  216. spin_unlock(&gart->client_lock);
  217. }
  218. kfree(gart_domain);
  219. }
  220. static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
  221. phys_addr_t pa, size_t bytes, int prot)
  222. {
  223. struct gart_domain *gart_domain = to_gart_domain(domain);
  224. struct gart_device *gart = gart_domain->gart;
  225. unsigned long flags;
  226. unsigned long pfn;
  227. unsigned long pte;
  228. if (!gart_iova_range_valid(gart, iova, bytes))
  229. return -EINVAL;
  230. spin_lock_irqsave(&gart->pte_lock, flags);
  231. pfn = __phys_to_pfn(pa);
  232. if (!pfn_valid(pfn)) {
  233. dev_err(gart->dev, "Invalid page: %pa\n", &pa);
  234. spin_unlock_irqrestore(&gart->pte_lock, flags);
  235. return -EINVAL;
  236. }
  237. if (gart_debug) {
  238. pte = gart_read_pte(gart, iova);
  239. if (pte & GART_ENTRY_PHYS_ADDR_VALID) {
  240. spin_unlock_irqrestore(&gart->pte_lock, flags);
  241. dev_err(gart->dev, "Page entry is in-use\n");
  242. return -EBUSY;
  243. }
  244. }
  245. gart_set_pte(gart, iova, GART_PTE(pfn));
  246. FLUSH_GART_REGS(gart);
  247. spin_unlock_irqrestore(&gart->pte_lock, flags);
  248. return 0;
  249. }
  250. static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  251. size_t bytes)
  252. {
  253. struct gart_domain *gart_domain = to_gart_domain(domain);
  254. struct gart_device *gart = gart_domain->gart;
  255. unsigned long flags;
  256. if (!gart_iova_range_valid(gart, iova, bytes))
  257. return 0;
  258. spin_lock_irqsave(&gart->pte_lock, flags);
  259. gart_set_pte(gart, iova, 0);
  260. FLUSH_GART_REGS(gart);
  261. spin_unlock_irqrestore(&gart->pte_lock, flags);
  262. return bytes;
  263. }
  264. static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
  265. dma_addr_t iova)
  266. {
  267. struct gart_domain *gart_domain = to_gart_domain(domain);
  268. struct gart_device *gart = gart_domain->gart;
  269. unsigned long pte;
  270. phys_addr_t pa;
  271. unsigned long flags;
  272. if (!gart_iova_range_valid(gart, iova, 0))
  273. return -EINVAL;
  274. spin_lock_irqsave(&gart->pte_lock, flags);
  275. pte = gart_read_pte(gart, iova);
  276. spin_unlock_irqrestore(&gart->pte_lock, flags);
  277. pa = (pte & GART_PAGE_MASK);
  278. if (!pfn_valid(__phys_to_pfn(pa))) {
  279. dev_err(gart->dev, "No entry for %08llx:%pa\n",
  280. (unsigned long long)iova, &pa);
  281. gart_dump_table(gart);
  282. return -EINVAL;
  283. }
  284. return pa;
  285. }
  286. static bool gart_iommu_capable(enum iommu_cap cap)
  287. {
  288. return false;
  289. }
  290. static int gart_iommu_add_device(struct device *dev)
  291. {
  292. struct iommu_group *group = iommu_group_get_for_dev(dev);
  293. if (IS_ERR(group))
  294. return PTR_ERR(group);
  295. iommu_group_put(group);
  296. iommu_device_link(&gart_handle->iommu, dev);
  297. return 0;
  298. }
  299. static void gart_iommu_remove_device(struct device *dev)
  300. {
  301. iommu_group_remove_device(dev);
  302. iommu_device_unlink(&gart_handle->iommu, dev);
  303. }
  304. static const struct iommu_ops gart_iommu_ops = {
  305. .capable = gart_iommu_capable,
  306. .domain_alloc = gart_iommu_domain_alloc,
  307. .domain_free = gart_iommu_domain_free,
  308. .attach_dev = gart_iommu_attach_dev,
  309. .detach_dev = gart_iommu_detach_dev,
  310. .add_device = gart_iommu_add_device,
  311. .remove_device = gart_iommu_remove_device,
  312. .device_group = generic_device_group,
  313. .map = gart_iommu_map,
  314. .unmap = gart_iommu_unmap,
  315. .iova_to_phys = gart_iommu_iova_to_phys,
  316. .pgsize_bitmap = GART_IOMMU_PGSIZES,
  317. };
  318. static int tegra_gart_suspend(struct device *dev)
  319. {
  320. struct gart_device *gart = dev_get_drvdata(dev);
  321. unsigned long iova;
  322. u32 *data = gart->savedata;
  323. unsigned long flags;
  324. spin_lock_irqsave(&gart->pte_lock, flags);
  325. for_each_gart_pte(gart, iova)
  326. *(data++) = gart_read_pte(gart, iova);
  327. spin_unlock_irqrestore(&gart->pte_lock, flags);
  328. return 0;
  329. }
  330. static int tegra_gart_resume(struct device *dev)
  331. {
  332. struct gart_device *gart = dev_get_drvdata(dev);
  333. unsigned long flags;
  334. spin_lock_irqsave(&gart->pte_lock, flags);
  335. do_gart_setup(gart, gart->savedata);
  336. spin_unlock_irqrestore(&gart->pte_lock, flags);
  337. return 0;
  338. }
  339. static int tegra_gart_probe(struct platform_device *pdev)
  340. {
  341. struct gart_device *gart;
  342. struct resource *res, *res_remap;
  343. void __iomem *gart_regs;
  344. struct device *dev = &pdev->dev;
  345. int ret;
  346. if (gart_handle)
  347. return -EIO;
  348. BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
  349. /* the GART memory aperture is required */
  350. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  351. res_remap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  352. if (!res || !res_remap) {
  353. dev_err(dev, "GART memory aperture expected\n");
  354. return -ENXIO;
  355. }
  356. gart = devm_kzalloc(dev, sizeof(*gart), GFP_KERNEL);
  357. if (!gart) {
  358. dev_err(dev, "failed to allocate gart_device\n");
  359. return -ENOMEM;
  360. }
  361. gart_regs = devm_ioremap(dev, res->start, resource_size(res));
  362. if (!gart_regs) {
  363. dev_err(dev, "failed to remap GART registers\n");
  364. return -ENXIO;
  365. }
  366. ret = iommu_device_sysfs_add(&gart->iommu, &pdev->dev, NULL,
  367. dev_name(&pdev->dev));
  368. if (ret) {
  369. dev_err(dev, "Failed to register IOMMU in sysfs\n");
  370. return ret;
  371. }
  372. iommu_device_set_ops(&gart->iommu, &gart_iommu_ops);
  373. ret = iommu_device_register(&gart->iommu);
  374. if (ret) {
  375. dev_err(dev, "Failed to register IOMMU\n");
  376. iommu_device_sysfs_remove(&gart->iommu);
  377. return ret;
  378. }
  379. gart->dev = &pdev->dev;
  380. spin_lock_init(&gart->pte_lock);
  381. spin_lock_init(&gart->client_lock);
  382. INIT_LIST_HEAD(&gart->client);
  383. gart->regs = gart_regs;
  384. gart->iovmm_base = (dma_addr_t)res_remap->start;
  385. gart->page_count = (resource_size(res_remap) >> GART_PAGE_SHIFT);
  386. gart->savedata = vmalloc(array_size(sizeof(u32), gart->page_count));
  387. if (!gart->savedata) {
  388. dev_err(dev, "failed to allocate context save area\n");
  389. return -ENOMEM;
  390. }
  391. platform_set_drvdata(pdev, gart);
  392. do_gart_setup(gart, NULL);
  393. gart_handle = gart;
  394. return 0;
  395. }
  396. static int tegra_gart_remove(struct platform_device *pdev)
  397. {
  398. struct gart_device *gart = platform_get_drvdata(pdev);
  399. iommu_device_unregister(&gart->iommu);
  400. iommu_device_sysfs_remove(&gart->iommu);
  401. writel(0, gart->regs + GART_CONFIG);
  402. if (gart->savedata)
  403. vfree(gart->savedata);
  404. gart_handle = NULL;
  405. return 0;
  406. }
  407. static const struct dev_pm_ops tegra_gart_pm_ops = {
  408. .suspend = tegra_gart_suspend,
  409. .resume = tegra_gart_resume,
  410. };
  411. static const struct of_device_id tegra_gart_of_match[] = {
  412. { .compatible = "nvidia,tegra20-gart", },
  413. { },
  414. };
  415. MODULE_DEVICE_TABLE(of, tegra_gart_of_match);
  416. static struct platform_driver tegra_gart_driver = {
  417. .probe = tegra_gart_probe,
  418. .remove = tegra_gart_remove,
  419. .driver = {
  420. .name = "tegra-gart",
  421. .pm = &tegra_gart_pm_ops,
  422. .of_match_table = tegra_gart_of_match,
  423. },
  424. };
  425. static int tegra_gart_init(void)
  426. {
  427. return platform_driver_register(&tegra_gart_driver);
  428. }
  429. static void __exit tegra_gart_exit(void)
  430. {
  431. platform_driver_unregister(&tegra_gart_driver);
  432. }
  433. subsys_initcall(tegra_gart_init);
  434. module_exit(tegra_gart_exit);
  435. module_param(gart_debug, bool, 0644);
  436. MODULE_PARM_DESC(gart_debug, "Enable GART debugging");
  437. MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");
  438. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  439. MODULE_ALIAS("platform:tegra-gart");
  440. MODULE_LICENSE("GPL v2");