tegra-gart.c 12 KB

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