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 */
66 static struct gart_device *gart_handle; /* unique for a system */
68 #define GART_PTE(_pfn) \
69 (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT))
72 * Any interaction between any block on PPSB and a block on APB or AHB
73 * must have these read-back to ensure the APB/AHB bus transaction is
74 * complete before initiating activity on the PPSB block.
76 #define FLUSH_GART_REGS(gart) ((void)readl((gart)->regs + GART_CONFIG))
78 #define for_each_gart_pte(gart, iova) \
79 for (iova = gart->iovmm_base; \
80 iova < gart->iovmm_base + GART_PAGE_SIZE * gart->page_count; \
81 iova += GART_PAGE_SIZE)
83 static inline void gart_set_pte(struct gart_device *gart,
84 unsigned long offs, u32 pte)
86 writel(offs, gart->regs + GART_ENTRY_ADDR);
87 writel(pte, gart->regs + GART_ENTRY_DATA);
89 dev_dbg(gart->dev, "%s %08lx:%08x\n",
90 pte ? "map" : "unmap", offs, pte & GART_PAGE_MASK);
93 static inline unsigned long gart_read_pte(struct gart_device *gart,
98 writel(offs, gart->regs + GART_ENTRY_ADDR);
99 pte = readl(gart->regs + GART_ENTRY_DATA);
104 static void do_gart_setup(struct gart_device *gart, const u32 *data)
108 for_each_gart_pte(gart, iova)
109 gart_set_pte(gart, iova, data ? *(data++) : 0);
111 writel(1, gart->regs + GART_CONFIG);
112 FLUSH_GART_REGS(gart);
116 static void gart_dump_table(struct gart_device *gart)
121 spin_lock_irqsave(&gart->pte_lock, flags);
122 for_each_gart_pte(gart, iova) {
125 pte = gart_read_pte(gart, iova);
127 dev_dbg(gart->dev, "%s %08lx:%08lx\n",
128 (GART_ENTRY_PHYS_ADDR_VALID & pte) ? "v" : " ",
129 iova, pte & GART_PAGE_MASK);
131 spin_unlock_irqrestore(&gart->pte_lock, flags);
134 static inline void gart_dump_table(struct gart_device *gart)
139 static inline bool gart_iova_range_valid(struct gart_device *gart,
140 unsigned long iova, size_t bytes)
142 unsigned long iova_start, iova_end, gart_start, gart_end;
145 iova_end = iova_start + bytes - 1;
146 gart_start = gart->iovmm_base;
147 gart_end = gart_start + gart->page_count * GART_PAGE_SIZE - 1;
149 if (iova_start < gart_start)
151 if (iova_end > gart_end)
156 static int gart_iommu_attach_dev(struct iommu_domain *domain,
159 struct gart_device *gart;
160 struct gart_client *client, *c;
168 domain->geometry.aperture_start = gart->iovmm_base;
169 domain->geometry.aperture_end = gart->iovmm_base +
170 gart->page_count * GART_PAGE_SIZE - 1;
171 domain->geometry.force_aperture = true;
173 client = devm_kzalloc(gart->dev, sizeof(*c), GFP_KERNEL);
178 spin_lock(&gart->client_lock);
179 list_for_each_entry(c, &gart->client, list) {
182 "%s is already attached\n", dev_name(dev));
187 list_add(&client->list, &gart->client);
188 spin_unlock(&gart->client_lock);
189 dev_dbg(gart->dev, "Attached %s\n", dev_name(dev));
193 devm_kfree(gart->dev, client);
194 spin_unlock(&gart->client_lock);
198 static void gart_iommu_detach_dev(struct iommu_domain *domain,
201 struct gart_device *gart = domain->priv;
202 struct gart_client *c;
204 spin_lock(&gart->client_lock);
206 list_for_each_entry(c, &gart->client, list) {
209 devm_kfree(gart->dev, c);
210 dev_dbg(gart->dev, "Detached %s\n", dev_name(dev));
214 dev_err(gart->dev, "Couldn't find\n");
216 spin_unlock(&gart->client_lock);
219 static int gart_iommu_domain_init(struct iommu_domain *domain)
224 static void gart_iommu_domain_destroy(struct iommu_domain *domain)
226 struct gart_device *gart = domain->priv;
231 spin_lock(&gart->client_lock);
232 if (!list_empty(&gart->client)) {
233 struct gart_client *c;
235 list_for_each_entry(c, &gart->client, list)
236 gart_iommu_detach_dev(domain, c->dev);
238 spin_unlock(&gart->client_lock);
242 static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
243 phys_addr_t pa, size_t bytes, int prot)
245 struct gart_device *gart = domain->priv;
249 if (!gart_iova_range_valid(gart, iova, bytes))
252 spin_lock_irqsave(&gart->pte_lock, flags);
253 pfn = __phys_to_pfn(pa);
254 if (!pfn_valid(pfn)) {
255 dev_err(gart->dev, "Invalid page: %pa\n", &pa);
256 spin_unlock_irqrestore(&gart->pte_lock, flags);
259 gart_set_pte(gart, iova, GART_PTE(pfn));
260 FLUSH_GART_REGS(gart);
261 spin_unlock_irqrestore(&gart->pte_lock, flags);
265 static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
268 struct gart_device *gart = domain->priv;
271 if (!gart_iova_range_valid(gart, iova, bytes))
274 spin_lock_irqsave(&gart->pte_lock, flags);
275 gart_set_pte(gart, iova, 0);
276 FLUSH_GART_REGS(gart);
277 spin_unlock_irqrestore(&gart->pte_lock, flags);
281 static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
284 struct gart_device *gart = domain->priv;
289 if (!gart_iova_range_valid(gart, iova, 0))
292 spin_lock_irqsave(&gart->pte_lock, flags);
293 pte = gart_read_pte(gart, iova);
294 spin_unlock_irqrestore(&gart->pte_lock, flags);
296 pa = (pte & GART_PAGE_MASK);
297 if (!pfn_valid(__phys_to_pfn(pa))) {
298 dev_err(gart->dev, "No entry for %08llx:%pa\n",
299 (unsigned long long)iova, &pa);
300 gart_dump_table(gart);
306 static int gart_iommu_domain_has_cap(struct iommu_domain *domain,
312 static struct iommu_ops gart_iommu_ops = {
313 .domain_init = gart_iommu_domain_init,
314 .domain_destroy = gart_iommu_domain_destroy,
315 .attach_dev = gart_iommu_attach_dev,
316 .detach_dev = gart_iommu_detach_dev,
317 .map = gart_iommu_map,
318 .unmap = gart_iommu_unmap,
319 .iova_to_phys = gart_iommu_iova_to_phys,
320 .domain_has_cap = gart_iommu_domain_has_cap,
321 .pgsize_bitmap = GART_IOMMU_PGSIZES,
324 static int tegra_gart_suspend(struct device *dev)
326 struct gart_device *gart = dev_get_drvdata(dev);
328 u32 *data = gart->savedata;
331 spin_lock_irqsave(&gart->pte_lock, flags);
332 for_each_gart_pte(gart, iova)
333 *(data++) = gart_read_pte(gart, iova);
334 spin_unlock_irqrestore(&gart->pte_lock, flags);
338 static int tegra_gart_resume(struct device *dev)
340 struct gart_device *gart = dev_get_drvdata(dev);
343 spin_lock_irqsave(&gart->pte_lock, flags);
344 do_gart_setup(gart, gart->savedata);
345 spin_unlock_irqrestore(&gart->pte_lock, flags);
349 static int tegra_gart_probe(struct platform_device *pdev)
351 struct gart_device *gart;
352 struct resource *res, *res_remap;
353 void __iomem *gart_regs;
354 struct device *dev = &pdev->dev;
359 BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
361 /* the GART memory aperture is required */
362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 res_remap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
364 if (!res || !res_remap) {
365 dev_err(dev, "GART memory aperture expected\n");
369 gart = devm_kzalloc(dev, sizeof(*gart), GFP_KERNEL);
371 dev_err(dev, "failed to allocate gart_device\n");
375 gart_regs = devm_ioremap(dev, res->start, resource_size(res));
377 dev_err(dev, "failed to remap GART registers\n");
381 gart->dev = &pdev->dev;
382 spin_lock_init(&gart->pte_lock);
383 spin_lock_init(&gart->client_lock);
384 INIT_LIST_HEAD(&gart->client);
385 gart->regs = gart_regs;
386 gart->iovmm_base = (dma_addr_t)res_remap->start;
387 gart->page_count = (resource_size(res_remap) >> GART_PAGE_SHIFT);
389 gart->savedata = vmalloc(sizeof(u32) * gart->page_count);
390 if (!gart->savedata) {
391 dev_err(dev, "failed to allocate context save area\n");
395 platform_set_drvdata(pdev, gart);
396 do_gart_setup(gart, NULL);
399 bus_set_iommu(&platform_bus_type, &gart_iommu_ops);
403 static int tegra_gart_remove(struct platform_device *pdev)
405 struct gart_device *gart = platform_get_drvdata(pdev);
407 writel(0, gart->regs + GART_CONFIG);
409 vfree(gart->savedata);
414 static const struct dev_pm_ops tegra_gart_pm_ops = {
415 .suspend = tegra_gart_suspend,
416 .resume = tegra_gart_resume,
419 static struct of_device_id tegra_gart_of_match[] = {
420 { .compatible = "nvidia,tegra20-gart", },
423 MODULE_DEVICE_TABLE(of, tegra_gart_of_match);
425 static struct platform_driver tegra_gart_driver = {
426 .probe = tegra_gart_probe,
427 .remove = tegra_gart_remove,
429 .owner = THIS_MODULE,
430 .name = "tegra-gart",
431 .pm = &tegra_gart_pm_ops,
432 .of_match_table = tegra_gart_of_match,
436 static int tegra_gart_init(void)
438 return platform_driver_register(&tegra_gart_driver);
441 static void __exit tegra_gart_exit(void)
443 platform_driver_unregister(&tegra_gart_driver);
446 subsys_initcall(tegra_gart_init);
447 module_exit(tegra_gart_exit);
449 MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");
450 MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
451 MODULE_ALIAS("platform:tegra-gart");
452 MODULE_LICENSE("GPL v2");