2 * IOMMU API for GART in Tegra20
4 * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.
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.
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
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.
20 #define pr_fmt(fmt) "%s(): " fmt, __func__
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
28 #include <linux/list.h>
29 #include <linux/device.h>
31 #include <linux/iommu.h>
34 #include <asm/cacheflush.h>
36 /* bitmap of the page sizes currently supported */
37 #define GART_IOMMU_PGSIZES (SZ_4K)
39 #define GART_REG_BASE 0x24
40 #define GART_CONFIG (0x24 - GART_REG_BASE)
41 #define GART_ENTRY_ADDR (0x28 - GART_REG_BASE)
42 #define GART_ENTRY_DATA (0x2c - GART_REG_BASE)
43 #define GART_ENTRY_PHYS_ADDR_VALID (1 << 31)
45 #define GART_PAGE_SHIFT 12
46 #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT)
47 #define GART_PAGE_MASK \
48 (~(GART_PAGE_SIZE - 1) & ~GART_ENTRY_PHYS_ADDR_VALID)
52 struct list_head list;
58 u32 page_count; /* total remappable size */
59 dma_addr_t iovmm_base; /* offset to vmm_area */
60 spinlock_t pte_lock; /* for pagetable */
61 struct list_head client;
62 spinlock_t client_lock; /* for client list */
67 struct iommu_domain domain; /* generic domain handle */
68 struct gart_device *gart; /* link to gart device */
71 static struct gart_device *gart_handle; /* unique for a system */
73 #define GART_PTE(_pfn) \
74 (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT))
76 static struct gart_domain *to_gart_domain(struct iommu_domain *dom)
78 return container_of(dom, struct gart_domain, domain);
82 * Any interaction between any block on PPSB and a block on APB or AHB
83 * must have these read-back to ensure the APB/AHB bus transaction is
84 * complete before initiating activity on the PPSB block.
86 #define FLUSH_GART_REGS(gart) ((void)readl((gart)->regs + GART_CONFIG))
88 #define for_each_gart_pte(gart, iova) \
89 for (iova = gart->iovmm_base; \
90 iova < gart->iovmm_base + GART_PAGE_SIZE * gart->page_count; \
91 iova += GART_PAGE_SIZE)
93 static inline void gart_set_pte(struct gart_device *gart,
94 unsigned long offs, u32 pte)
96 writel(offs, gart->regs + GART_ENTRY_ADDR);
97 writel(pte, gart->regs + GART_ENTRY_DATA);
99 dev_dbg(gart->dev, "%s %08lx:%08x\n",
100 pte ? "map" : "unmap", offs, pte & GART_PAGE_MASK);
103 static inline unsigned long gart_read_pte(struct gart_device *gart,
108 writel(offs, gart->regs + GART_ENTRY_ADDR);
109 pte = readl(gart->regs + GART_ENTRY_DATA);
114 static void do_gart_setup(struct gart_device *gart, const u32 *data)
118 for_each_gart_pte(gart, iova)
119 gart_set_pte(gart, iova, data ? *(data++) : 0);
121 writel(1, gart->regs + GART_CONFIG);
122 FLUSH_GART_REGS(gart);
126 static void gart_dump_table(struct gart_device *gart)
131 spin_lock_irqsave(&gart->pte_lock, flags);
132 for_each_gart_pte(gart, iova) {
135 pte = gart_read_pte(gart, iova);
137 dev_dbg(gart->dev, "%s %08lx:%08lx\n",
138 (GART_ENTRY_PHYS_ADDR_VALID & pte) ? "v" : " ",
139 iova, pte & GART_PAGE_MASK);
141 spin_unlock_irqrestore(&gart->pte_lock, flags);
144 static inline void gart_dump_table(struct gart_device *gart)
149 static inline bool gart_iova_range_valid(struct gart_device *gart,
150 unsigned long iova, size_t bytes)
152 unsigned long iova_start, iova_end, gart_start, gart_end;
155 iova_end = iova_start + bytes - 1;
156 gart_start = gart->iovmm_base;
157 gart_end = gart_start + gart->page_count * GART_PAGE_SIZE - 1;
159 if (iova_start < gart_start)
161 if (iova_end > gart_end)
166 static int gart_iommu_attach_dev(struct iommu_domain *domain,
169 struct gart_domain *gart_domain = to_gart_domain(domain);
170 struct gart_device *gart;
171 struct gart_client *client, *c;
177 gart_domain->gart = gart;
179 domain->geometry.aperture_start = gart->iovmm_base;
180 domain->geometry.aperture_end = gart->iovmm_base +
181 gart->page_count * GART_PAGE_SIZE - 1;
182 domain->geometry.force_aperture = true;
184 client = devm_kzalloc(gart->dev, sizeof(*c), GFP_KERNEL);
189 spin_lock(&gart->client_lock);
190 list_for_each_entry(c, &gart->client, list) {
193 "%s is already attached\n", dev_name(dev));
198 list_add(&client->list, &gart->client);
199 spin_unlock(&gart->client_lock);
200 dev_dbg(gart->dev, "Attached %s\n", dev_name(dev));
204 devm_kfree(gart->dev, client);
205 spin_unlock(&gart->client_lock);
209 static void gart_iommu_detach_dev(struct iommu_domain *domain,
212 struct gart_domain *gart_domain = to_gart_domain(domain);
213 struct gart_device *gart = gart_domain->gart;
214 struct gart_client *c;
216 spin_lock(&gart->client_lock);
218 list_for_each_entry(c, &gart->client, list) {
221 devm_kfree(gart->dev, c);
222 dev_dbg(gart->dev, "Detached %s\n", dev_name(dev));
226 dev_err(gart->dev, "Couldn't find\n");
228 spin_unlock(&gart->client_lock);
231 static struct iommu_domain *gart_iommu_domain_alloc(unsigned type)
233 struct gart_domain *gart_domain;
235 if (type != IOMMU_DOMAIN_UNMANAGED)
238 gart_domain = kzalloc(sizeof(*gart_domain), GFP_KERNEL);
242 return &gart_domain->domain;
245 static void gart_iommu_domain_free(struct iommu_domain *domain)
247 struct gart_domain *gart_domain = to_gart_domain(domain);
248 struct gart_device *gart = gart_domain->gart;
251 spin_lock(&gart->client_lock);
252 if (!list_empty(&gart->client)) {
253 struct gart_client *c;
255 list_for_each_entry(c, &gart->client, list)
256 gart_iommu_detach_dev(domain, c->dev);
258 spin_unlock(&gart->client_lock);
264 static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
265 phys_addr_t pa, size_t bytes, int prot)
267 struct gart_domain *gart_domain = to_gart_domain(domain);
268 struct gart_device *gart = gart_domain->gart;
272 if (!gart_iova_range_valid(gart, iova, bytes))
275 spin_lock_irqsave(&gart->pte_lock, flags);
276 pfn = __phys_to_pfn(pa);
277 if (!pfn_valid(pfn)) {
278 dev_err(gart->dev, "Invalid page: %pa\n", &pa);
279 spin_unlock_irqrestore(&gart->pte_lock, flags);
282 gart_set_pte(gart, iova, GART_PTE(pfn));
283 FLUSH_GART_REGS(gart);
284 spin_unlock_irqrestore(&gart->pte_lock, flags);
288 static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
291 struct gart_domain *gart_domain = to_gart_domain(domain);
292 struct gart_device *gart = gart_domain->gart;
295 if (!gart_iova_range_valid(gart, iova, bytes))
298 spin_lock_irqsave(&gart->pte_lock, flags);
299 gart_set_pte(gart, iova, 0);
300 FLUSH_GART_REGS(gart);
301 spin_unlock_irqrestore(&gart->pte_lock, flags);
305 static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
308 struct gart_domain *gart_domain = to_gart_domain(domain);
309 struct gart_device *gart = gart_domain->gart;
314 if (!gart_iova_range_valid(gart, iova, 0))
317 spin_lock_irqsave(&gart->pte_lock, flags);
318 pte = gart_read_pte(gart, iova);
319 spin_unlock_irqrestore(&gart->pte_lock, flags);
321 pa = (pte & GART_PAGE_MASK);
322 if (!pfn_valid(__phys_to_pfn(pa))) {
323 dev_err(gart->dev, "No entry for %08llx:%pa\n",
324 (unsigned long long)iova, &pa);
325 gart_dump_table(gart);
331 static bool gart_iommu_capable(enum iommu_cap cap)
336 static const struct iommu_ops gart_iommu_ops = {
337 .capable = gart_iommu_capable,
338 .domain_alloc = gart_iommu_domain_alloc,
339 .domain_free = gart_iommu_domain_free,
340 .attach_dev = gart_iommu_attach_dev,
341 .detach_dev = gart_iommu_detach_dev,
342 .map = gart_iommu_map,
343 .map_sg = default_iommu_map_sg,
344 .unmap = gart_iommu_unmap,
345 .iova_to_phys = gart_iommu_iova_to_phys,
346 .pgsize_bitmap = GART_IOMMU_PGSIZES,
349 static int tegra_gart_suspend(struct device *dev)
351 struct gart_device *gart = dev_get_drvdata(dev);
353 u32 *data = gart->savedata;
356 spin_lock_irqsave(&gart->pte_lock, flags);
357 for_each_gart_pte(gart, iova)
358 *(data++) = gart_read_pte(gart, iova);
359 spin_unlock_irqrestore(&gart->pte_lock, flags);
363 static int tegra_gart_resume(struct device *dev)
365 struct gart_device *gart = dev_get_drvdata(dev);
368 spin_lock_irqsave(&gart->pte_lock, flags);
369 do_gart_setup(gart, gart->savedata);
370 spin_unlock_irqrestore(&gart->pte_lock, flags);
374 static int tegra_gart_probe(struct platform_device *pdev)
376 struct gart_device *gart;
377 struct resource *res, *res_remap;
378 void __iomem *gart_regs;
379 struct device *dev = &pdev->dev;
384 BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
386 /* the GART memory aperture is required */
387 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
388 res_remap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
389 if (!res || !res_remap) {
390 dev_err(dev, "GART memory aperture expected\n");
394 gart = devm_kzalloc(dev, sizeof(*gart), GFP_KERNEL);
396 dev_err(dev, "failed to allocate gart_device\n");
400 gart_regs = devm_ioremap(dev, res->start, resource_size(res));
402 dev_err(dev, "failed to remap GART registers\n");
406 gart->dev = &pdev->dev;
407 spin_lock_init(&gart->pte_lock);
408 spin_lock_init(&gart->client_lock);
409 INIT_LIST_HEAD(&gart->client);
410 gart->regs = gart_regs;
411 gart->iovmm_base = (dma_addr_t)res_remap->start;
412 gart->page_count = (resource_size(res_remap) >> GART_PAGE_SHIFT);
414 gart->savedata = vmalloc(sizeof(u32) * gart->page_count);
415 if (!gart->savedata) {
416 dev_err(dev, "failed to allocate context save area\n");
420 platform_set_drvdata(pdev, gart);
421 do_gart_setup(gart, NULL);
428 static int tegra_gart_remove(struct platform_device *pdev)
430 struct gart_device *gart = platform_get_drvdata(pdev);
432 writel(0, gart->regs + GART_CONFIG);
434 vfree(gart->savedata);
439 static const struct dev_pm_ops tegra_gart_pm_ops = {
440 .suspend = tegra_gart_suspend,
441 .resume = tegra_gart_resume,
444 static const struct of_device_id tegra_gart_of_match[] = {
445 { .compatible = "nvidia,tegra20-gart", },
448 MODULE_DEVICE_TABLE(of, tegra_gart_of_match);
450 static struct platform_driver tegra_gart_driver = {
451 .probe = tegra_gart_probe,
452 .remove = tegra_gart_remove,
454 .name = "tegra-gart",
455 .pm = &tegra_gart_pm_ops,
456 .of_match_table = tegra_gart_of_match,
460 static int tegra_gart_init(void)
462 return platform_driver_register(&tegra_gart_driver);
465 static void __exit tegra_gart_exit(void)
467 platform_driver_unregister(&tegra_gart_driver);
470 subsys_initcall(tegra_gart_init);
471 module_exit(tegra_gart_exit);
473 MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");
474 MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
475 MODULE_ALIAS("platform:tegra-gart");
476 MODULE_LICENSE("GPL v2");