tegra-gart.c 11 KB

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