tegra-gart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. };
  57. static struct gart_device *gart_handle; /* unique for a system */
  58. #define GART_PTE(_pfn) \
  59. (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT))
  60. /*
  61. * Any interaction between any block on PPSB and a block on APB or AHB
  62. * must have these read-back to ensure the APB/AHB bus transaction is
  63. * complete before initiating activity on the PPSB block.
  64. */
  65. #define FLUSH_GART_REGS(gart) ((void)readl((gart)->regs + GART_CONFIG))
  66. #define for_each_gart_pte(gart, iova) \
  67. for (iova = gart->iovmm_base; \
  68. iova < gart->iovmm_base + GART_PAGE_SIZE * gart->page_count; \
  69. iova += GART_PAGE_SIZE)
  70. static inline void gart_set_pte(struct gart_device *gart,
  71. unsigned long offs, u32 pte)
  72. {
  73. writel(offs, gart->regs + GART_ENTRY_ADDR);
  74. writel(pte, gart->regs + GART_ENTRY_DATA);
  75. dev_dbg(gart->dev, "%s %08lx:%08x\n",
  76. pte ? "map" : "unmap", offs, pte & GART_PAGE_MASK);
  77. }
  78. static inline unsigned long gart_read_pte(struct gart_device *gart,
  79. unsigned long offs)
  80. {
  81. unsigned long pte;
  82. writel(offs, gart->regs + GART_ENTRY_ADDR);
  83. pte = readl(gart->regs + GART_ENTRY_DATA);
  84. return pte;
  85. }
  86. static void do_gart_setup(struct gart_device *gart, const u32 *data)
  87. {
  88. unsigned long iova;
  89. for_each_gart_pte(gart, iova)
  90. gart_set_pte(gart, iova, data ? *(data++) : 0);
  91. writel(1, gart->regs + GART_CONFIG);
  92. FLUSH_GART_REGS(gart);
  93. }
  94. #ifdef DEBUG
  95. static void gart_dump_table(struct gart_device *gart)
  96. {
  97. unsigned long iova;
  98. unsigned long flags;
  99. spin_lock_irqsave(&gart->pte_lock, flags);
  100. for_each_gart_pte(gart, iova) {
  101. unsigned long pte;
  102. pte = gart_read_pte(gart, iova);
  103. dev_dbg(gart->dev, "%s %08lx:%08lx\n",
  104. (GART_ENTRY_PHYS_ADDR_VALID & pte) ? "v" : " ",
  105. iova, pte & GART_PAGE_MASK);
  106. }
  107. spin_unlock_irqrestore(&gart->pte_lock, flags);
  108. }
  109. #else
  110. static inline void gart_dump_table(struct gart_device *gart)
  111. {
  112. }
  113. #endif
  114. static inline bool gart_iova_range_valid(struct gart_device *gart,
  115. unsigned long iova, size_t bytes)
  116. {
  117. unsigned long iova_start, iova_end, gart_start, gart_end;
  118. iova_start = iova;
  119. iova_end = iova_start + bytes - 1;
  120. gart_start = gart->iovmm_base;
  121. gart_end = gart_start + gart->page_count * GART_PAGE_SIZE - 1;
  122. if (iova_start < gart_start)
  123. return false;
  124. if (iova_end > gart_end)
  125. return false;
  126. return true;
  127. }
  128. static int gart_iommu_attach_dev(struct iommu_domain *domain,
  129. struct device *dev)
  130. {
  131. struct gart_device *gart;
  132. struct gart_client *client, *c;
  133. int err = 0;
  134. gart = gart_handle;
  135. if (!gart)
  136. return -EINVAL;
  137. domain->priv = gart;
  138. domain->geometry.aperture_start = gart->iovmm_base;
  139. domain->geometry.aperture_end = gart->iovmm_base +
  140. gart->page_count * GART_PAGE_SIZE - 1;
  141. domain->geometry.force_aperture = true;
  142. client = devm_kzalloc(gart->dev, sizeof(*c), GFP_KERNEL);
  143. if (!client)
  144. return -ENOMEM;
  145. client->dev = dev;
  146. spin_lock(&gart->client_lock);
  147. list_for_each_entry(c, &gart->client, list) {
  148. if (c->dev == dev) {
  149. dev_err(gart->dev,
  150. "%s is already attached\n", dev_name(dev));
  151. err = -EINVAL;
  152. goto fail;
  153. }
  154. }
  155. list_add(&client->list, &gart->client);
  156. spin_unlock(&gart->client_lock);
  157. dev_dbg(gart->dev, "Attached %s\n", dev_name(dev));
  158. return 0;
  159. fail:
  160. devm_kfree(gart->dev, client);
  161. spin_unlock(&gart->client_lock);
  162. return err;
  163. }
  164. static void gart_iommu_detach_dev(struct iommu_domain *domain,
  165. struct device *dev)
  166. {
  167. struct gart_device *gart = domain->priv;
  168. struct gart_client *c;
  169. spin_lock(&gart->client_lock);
  170. list_for_each_entry(c, &gart->client, list) {
  171. if (c->dev == dev) {
  172. list_del(&c->list);
  173. devm_kfree(gart->dev, c);
  174. dev_dbg(gart->dev, "Detached %s\n", dev_name(dev));
  175. goto out;
  176. }
  177. }
  178. dev_err(gart->dev, "Couldn't find\n");
  179. out:
  180. spin_unlock(&gart->client_lock);
  181. }
  182. static int gart_iommu_domain_init(struct iommu_domain *domain)
  183. {
  184. return 0;
  185. }
  186. static void gart_iommu_domain_destroy(struct iommu_domain *domain)
  187. {
  188. struct gart_device *gart = domain->priv;
  189. if (!gart)
  190. return;
  191. spin_lock(&gart->client_lock);
  192. if (!list_empty(&gart->client)) {
  193. struct gart_client *c;
  194. list_for_each_entry(c, &gart->client, list)
  195. gart_iommu_detach_dev(domain, c->dev);
  196. }
  197. spin_unlock(&gart->client_lock);
  198. domain->priv = NULL;
  199. }
  200. static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
  201. phys_addr_t pa, size_t bytes, int prot)
  202. {
  203. struct gart_device *gart = domain->priv;
  204. unsigned long flags;
  205. unsigned long pfn;
  206. if (!gart_iova_range_valid(gart, iova, bytes))
  207. return -EINVAL;
  208. spin_lock_irqsave(&gart->pte_lock, flags);
  209. pfn = __phys_to_pfn(pa);
  210. if (!pfn_valid(pfn)) {
  211. dev_err(gart->dev, "Invalid page: %08x\n", pa);
  212. spin_unlock_irqrestore(&gart->pte_lock, flags);
  213. return -EINVAL;
  214. }
  215. gart_set_pte(gart, iova, GART_PTE(pfn));
  216. FLUSH_GART_REGS(gart);
  217. spin_unlock_irqrestore(&gart->pte_lock, flags);
  218. return 0;
  219. }
  220. static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  221. size_t bytes)
  222. {
  223. struct gart_device *gart = domain->priv;
  224. unsigned long flags;
  225. if (!gart_iova_range_valid(gart, iova, bytes))
  226. return 0;
  227. spin_lock_irqsave(&gart->pte_lock, flags);
  228. gart_set_pte(gart, iova, 0);
  229. FLUSH_GART_REGS(gart);
  230. spin_unlock_irqrestore(&gart->pte_lock, flags);
  231. return 0;
  232. }
  233. static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
  234. unsigned long iova)
  235. {
  236. struct gart_device *gart = domain->priv;
  237. unsigned long pte;
  238. phys_addr_t pa;
  239. unsigned long flags;
  240. if (!gart_iova_range_valid(gart, iova, 0))
  241. return -EINVAL;
  242. spin_lock_irqsave(&gart->pte_lock, flags);
  243. pte = gart_read_pte(gart, iova);
  244. spin_unlock_irqrestore(&gart->pte_lock, flags);
  245. pa = (pte & GART_PAGE_MASK);
  246. if (!pfn_valid(__phys_to_pfn(pa))) {
  247. dev_err(gart->dev, "No entry for %08lx:%08x\n", iova, pa);
  248. gart_dump_table(gart);
  249. return -EINVAL;
  250. }
  251. return pa;
  252. }
  253. static int gart_iommu_domain_has_cap(struct iommu_domain *domain,
  254. unsigned long cap)
  255. {
  256. return 0;
  257. }
  258. static struct iommu_ops gart_iommu_ops = {
  259. .domain_init = gart_iommu_domain_init,
  260. .domain_destroy = gart_iommu_domain_destroy,
  261. .attach_dev = gart_iommu_attach_dev,
  262. .detach_dev = gart_iommu_detach_dev,
  263. .map = gart_iommu_map,
  264. .unmap = gart_iommu_unmap,
  265. .iova_to_phys = gart_iommu_iova_to_phys,
  266. .domain_has_cap = gart_iommu_domain_has_cap,
  267. .pgsize_bitmap = GART_IOMMU_PGSIZES,
  268. };
  269. static int tegra_gart_suspend(struct device *dev)
  270. {
  271. struct gart_device *gart = dev_get_drvdata(dev);
  272. unsigned long iova;
  273. u32 *data = gart->savedata;
  274. unsigned long flags;
  275. spin_lock_irqsave(&gart->pte_lock, flags);
  276. for_each_gart_pte(gart, iova)
  277. *(data++) = gart_read_pte(gart, iova);
  278. spin_unlock_irqrestore(&gart->pte_lock, flags);
  279. return 0;
  280. }
  281. static int tegra_gart_resume(struct device *dev)
  282. {
  283. struct gart_device *gart = dev_get_drvdata(dev);
  284. unsigned long flags;
  285. spin_lock_irqsave(&gart->pte_lock, flags);
  286. do_gart_setup(gart, gart->savedata);
  287. spin_unlock_irqrestore(&gart->pte_lock, flags);
  288. return 0;
  289. }
  290. static int tegra_gart_probe(struct platform_device *pdev)
  291. {
  292. struct gart_device *gart;
  293. struct resource *res, *res_remap;
  294. void __iomem *gart_regs;
  295. int err;
  296. struct device *dev = &pdev->dev;
  297. if (gart_handle)
  298. return -EIO;
  299. BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
  300. /* the GART memory aperture is required */
  301. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  302. res_remap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  303. if (!res || !res_remap) {
  304. dev_err(dev, "GART memory aperture expected\n");
  305. return -ENXIO;
  306. }
  307. gart = devm_kzalloc(dev, sizeof(*gart), GFP_KERNEL);
  308. if (!gart) {
  309. dev_err(dev, "failed to allocate gart_device\n");
  310. return -ENOMEM;
  311. }
  312. gart_regs = devm_ioremap(dev, res->start, resource_size(res));
  313. if (!gart_regs) {
  314. dev_err(dev, "failed to remap GART registers\n");
  315. err = -ENXIO;
  316. goto fail;
  317. }
  318. gart->dev = &pdev->dev;
  319. spin_lock_init(&gart->pte_lock);
  320. spin_lock_init(&gart->client_lock);
  321. INIT_LIST_HEAD(&gart->client);
  322. gart->regs = gart_regs;
  323. gart->iovmm_base = (dma_addr_t)res_remap->start;
  324. gart->page_count = (resource_size(res_remap) >> GART_PAGE_SHIFT);
  325. gart->savedata = vmalloc(sizeof(u32) * gart->page_count);
  326. if (!gart->savedata) {
  327. dev_err(dev, "failed to allocate context save area\n");
  328. err = -ENOMEM;
  329. goto fail;
  330. }
  331. platform_set_drvdata(pdev, gart);
  332. do_gart_setup(gart, NULL);
  333. gart_handle = gart;
  334. bus_set_iommu(&platform_bus_type, &gart_iommu_ops);
  335. return 0;
  336. fail:
  337. if (gart_regs)
  338. devm_iounmap(dev, gart_regs);
  339. if (gart && gart->savedata)
  340. vfree(gart->savedata);
  341. devm_kfree(dev, gart);
  342. return err;
  343. }
  344. static int tegra_gart_remove(struct platform_device *pdev)
  345. {
  346. struct gart_device *gart = platform_get_drvdata(pdev);
  347. struct device *dev = gart->dev;
  348. writel(0, gart->regs + GART_CONFIG);
  349. if (gart->savedata)
  350. vfree(gart->savedata);
  351. if (gart->regs)
  352. devm_iounmap(dev, gart->regs);
  353. devm_kfree(dev, gart);
  354. gart_handle = NULL;
  355. return 0;
  356. }
  357. const struct dev_pm_ops tegra_gart_pm_ops = {
  358. .suspend = tegra_gart_suspend,
  359. .resume = tegra_gart_resume,
  360. };
  361. static struct of_device_id tegra_gart_of_match[] = {
  362. { .compatible = "nvidia,tegra20-gart", },
  363. { },
  364. };
  365. MODULE_DEVICE_TABLE(of, tegra_gart_of_match);
  366. static struct platform_driver tegra_gart_driver = {
  367. .probe = tegra_gart_probe,
  368. .remove = tegra_gart_remove,
  369. .driver = {
  370. .owner = THIS_MODULE,
  371. .name = "tegra-gart",
  372. .pm = &tegra_gart_pm_ops,
  373. .of_match_table = tegra_gart_of_match,
  374. },
  375. };
  376. static int tegra_gart_init(void)
  377. {
  378. return platform_driver_register(&tegra_gart_driver);
  379. }
  380. static void __exit tegra_gart_exit(void)
  381. {
  382. platform_driver_unregister(&tegra_gart_driver);
  383. }
  384. subsys_initcall(tegra_gart_init);
  385. module_exit(tegra_gart_exit);
  386. MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");
  387. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  388. MODULE_ALIAS("platform:tegra-gart");
  389. MODULE_LICENSE("GPL v2");