tegra-gart.c 11 KB

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