blob: 29bafc6e82ae08f30ccbc7e01ee1b1d0ee256e39 [file] [log] [blame]
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +02001/*
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
20#define pr_fmt(fmt) "%s(): " fmt, __func__
21
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>
27#include <linux/mm.h>
28#include <linux/list.h>
29#include <linux/device.h>
30#include <linux/io.h>
31#include <linux/iommu.h>
Thierry Reding7cffae42012-04-13 15:08:08 +020032#include <linux/of.h>
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +020033
34#include <asm/cacheflush.h>
35
36/* bitmap of the page sizes currently supported */
37#define GART_IOMMU_PGSIZES (SZ_4K)
38
Hiroshi DOYU774dfc92012-05-10 10:45:32 +030039#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)
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +020043#define GART_ENTRY_PHYS_ADDR_VALID (1 << 31)
44
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)
49
50struct gart_client {
51 struct device *dev;
52 struct list_head list;
53};
54
55struct gart_device {
56 void __iomem *regs;
57 u32 *savedata;
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 */
63 struct device *dev;
64};
65
Joerg Roedelb5cbb382015-03-26 13:43:13 +010066struct gart_domain {
67 struct iommu_domain domain; /* generic domain handle */
68 struct gart_device *gart; /* link to gart device */
69};
70
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +020071static struct gart_device *gart_handle; /* unique for a system */
72
73#define GART_PTE(_pfn) \
74 (GART_ENTRY_PHYS_ADDR_VALID | ((_pfn) << PAGE_SHIFT))
75
Joerg Roedelb5cbb382015-03-26 13:43:13 +010076static struct gart_domain *to_gart_domain(struct iommu_domain *dom)
77{
78 return container_of(dom, struct gart_domain, domain);
79}
80
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +020081/*
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.
85 */
86#define FLUSH_GART_REGS(gart) ((void)readl((gart)->regs + GART_CONFIG))
87
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)
92
93static inline void gart_set_pte(struct gart_device *gart,
94 unsigned long offs, u32 pte)
95{
96 writel(offs, gart->regs + GART_ENTRY_ADDR);
97 writel(pte, gart->regs + GART_ENTRY_DATA);
98
99 dev_dbg(gart->dev, "%s %08lx:%08x\n",
100 pte ? "map" : "unmap", offs, pte & GART_PAGE_MASK);
101}
102
103static inline unsigned long gart_read_pte(struct gart_device *gart,
104 unsigned long offs)
105{
106 unsigned long pte;
107
108 writel(offs, gart->regs + GART_ENTRY_ADDR);
109 pte = readl(gart->regs + GART_ENTRY_DATA);
110
111 return pte;
112}
113
114static void do_gart_setup(struct gart_device *gart, const u32 *data)
115{
116 unsigned long iova;
117
118 for_each_gart_pte(gart, iova)
119 gart_set_pte(gart, iova, data ? *(data++) : 0);
120
121 writel(1, gart->regs + GART_CONFIG);
122 FLUSH_GART_REGS(gart);
123}
124
125#ifdef DEBUG
126static void gart_dump_table(struct gart_device *gart)
127{
128 unsigned long iova;
129 unsigned long flags;
130
131 spin_lock_irqsave(&gart->pte_lock, flags);
132 for_each_gart_pte(gart, iova) {
133 unsigned long pte;
134
135 pte = gart_read_pte(gart, iova);
136
137 dev_dbg(gart->dev, "%s %08lx:%08lx\n",
138 (GART_ENTRY_PHYS_ADDR_VALID & pte) ? "v" : " ",
139 iova, pte & GART_PAGE_MASK);
140 }
141 spin_unlock_irqrestore(&gart->pte_lock, flags);
142}
143#else
144static inline void gart_dump_table(struct gart_device *gart)
145{
146}
147#endif
148
149static inline bool gart_iova_range_valid(struct gart_device *gart,
150 unsigned long iova, size_t bytes)
151{
152 unsigned long iova_start, iova_end, gart_start, gart_end;
153
154 iova_start = iova;
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;
158
159 if (iova_start < gart_start)
160 return false;
161 if (iova_end > gart_end)
162 return false;
163 return true;
164}
165
166static int gart_iommu_attach_dev(struct iommu_domain *domain,
167 struct device *dev)
168{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100169 struct gart_domain *gart_domain = to_gart_domain(domain);
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200170 struct gart_device *gart = gart_domain->gart;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200171 struct gart_client *client, *c;
172 int err = 0;
173
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200174 client = devm_kzalloc(gart->dev, sizeof(*c), GFP_KERNEL);
175 if (!client)
176 return -ENOMEM;
177 client->dev = dev;
178
179 spin_lock(&gart->client_lock);
180 list_for_each_entry(c, &gart->client, list) {
181 if (c->dev == dev) {
182 dev_err(gart->dev,
183 "%s is already attached\n", dev_name(dev));
184 err = -EINVAL;
185 goto fail;
186 }
187 }
188 list_add(&client->list, &gart->client);
189 spin_unlock(&gart->client_lock);
190 dev_dbg(gart->dev, "Attached %s\n", dev_name(dev));
191 return 0;
192
193fail:
194 devm_kfree(gart->dev, client);
195 spin_unlock(&gart->client_lock);
196 return err;
197}
198
199static void gart_iommu_detach_dev(struct iommu_domain *domain,
200 struct device *dev)
201{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100202 struct gart_domain *gart_domain = to_gart_domain(domain);
203 struct gart_device *gart = gart_domain->gart;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200204 struct gart_client *c;
205
206 spin_lock(&gart->client_lock);
207
208 list_for_each_entry(c, &gart->client, list) {
209 if (c->dev == dev) {
210 list_del(&c->list);
211 devm_kfree(gart->dev, c);
212 dev_dbg(gart->dev, "Detached %s\n", dev_name(dev));
213 goto out;
214 }
215 }
216 dev_err(gart->dev, "Couldn't find\n");
217out:
218 spin_unlock(&gart->client_lock);
219}
220
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100221static struct iommu_domain *gart_iommu_domain_alloc(unsigned type)
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200222{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100223 struct gart_domain *gart_domain;
Thierry Reding836a8ac2015-03-27 11:07:26 +0100224 struct gart_device *gart;
225
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100226 if (type != IOMMU_DOMAIN_UNMANAGED)
227 return NULL;
228
Thierry Reding836a8ac2015-03-27 11:07:26 +0100229 gart = gart_handle;
230 if (!gart)
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200231 return NULL;
Thierry Reding836a8ac2015-03-27 11:07:26 +0100232
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100233 gart_domain = kzalloc(sizeof(*gart_domain), GFP_KERNEL);
234 if (!gart_domain)
235 return NULL;
Thierry Reding836a8ac2015-03-27 11:07:26 +0100236
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200237 gart_domain->gart = gart;
238 gart_domain->domain.geometry.aperture_start = gart->iovmm_base;
239 gart_domain->domain.geometry.aperture_end = gart->iovmm_base +
Thierry Reding836a8ac2015-03-27 11:07:26 +0100240 gart->page_count * GART_PAGE_SIZE - 1;
Joerg Roedel7f65ef02015-04-02 13:33:19 +0200241 gart_domain->domain.geometry.force_aperture = true;
Thierry Reding836a8ac2015-03-27 11:07:26 +0100242
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100243 return &gart_domain->domain;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200244}
245
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100246static void gart_iommu_domain_free(struct iommu_domain *domain)
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200247{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100248 struct gart_domain *gart_domain = to_gart_domain(domain);
249 struct gart_device *gart = gart_domain->gart;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200250
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100251 if (gart) {
252 spin_lock(&gart->client_lock);
253 if (!list_empty(&gart->client)) {
254 struct gart_client *c;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200255
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100256 list_for_each_entry(c, &gart->client, list)
257 gart_iommu_detach_dev(domain, c->dev);
258 }
259 spin_unlock(&gart->client_lock);
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200260 }
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100261
262 kfree(gart_domain);
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200263}
264
265static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
266 phys_addr_t pa, size_t bytes, int prot)
267{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100268 struct gart_domain *gart_domain = to_gart_domain(domain);
269 struct gart_device *gart = gart_domain->gart;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200270 unsigned long flags;
271 unsigned long pfn;
272
273 if (!gart_iova_range_valid(gart, iova, bytes))
274 return -EINVAL;
275
276 spin_lock_irqsave(&gart->pte_lock, flags);
277 pfn = __phys_to_pfn(pa);
278 if (!pfn_valid(pfn)) {
Thierry Redinge56b3da2013-09-17 10:19:31 +0200279 dev_err(gart->dev, "Invalid page: %pa\n", &pa);
Lucas Stach09c32532012-03-12 20:15:01 +0100280 spin_unlock_irqrestore(&gart->pte_lock, flags);
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200281 return -EINVAL;
282 }
283 gart_set_pte(gart, iova, GART_PTE(pfn));
284 FLUSH_GART_REGS(gart);
285 spin_unlock_irqrestore(&gart->pte_lock, flags);
286 return 0;
287}
288
289static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
290 size_t bytes)
291{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100292 struct gart_domain *gart_domain = to_gart_domain(domain);
293 struct gart_device *gart = gart_domain->gart;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200294 unsigned long flags;
295
296 if (!gart_iova_range_valid(gart, iova, bytes))
297 return 0;
298
299 spin_lock_irqsave(&gart->pte_lock, flags);
300 gart_set_pte(gart, iova, 0);
301 FLUSH_GART_REGS(gart);
302 spin_unlock_irqrestore(&gart->pte_lock, flags);
303 return 0;
304}
305
306static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +0530307 dma_addr_t iova)
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200308{
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100309 struct gart_domain *gart_domain = to_gart_domain(domain);
310 struct gart_device *gart = gart_domain->gart;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200311 unsigned long pte;
312 phys_addr_t pa;
313 unsigned long flags;
314
315 if (!gart_iova_range_valid(gart, iova, 0))
316 return -EINVAL;
317
318 spin_lock_irqsave(&gart->pte_lock, flags);
319 pte = gart_read_pte(gart, iova);
320 spin_unlock_irqrestore(&gart->pte_lock, flags);
321
322 pa = (pte & GART_PAGE_MASK);
323 if (!pfn_valid(__phys_to_pfn(pa))) {
Thierry Redinge56b3da2013-09-17 10:19:31 +0200324 dev_err(gart->dev, "No entry for %08llx:%pa\n",
325 (unsigned long long)iova, &pa);
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200326 gart_dump_table(gart);
327 return -EINVAL;
328 }
329 return pa;
330}
331
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200332static bool gart_iommu_capable(enum iommu_cap cap)
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200333{
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200334 return false;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200335}
336
Robin Murphy15f9a312017-07-21 13:12:37 +0100337static int gart_iommu_add_device(struct device *dev)
338{
339 struct iommu_group *group = iommu_group_get_for_dev(dev);
340
341 if (IS_ERR(group))
342 return PTR_ERR(group);
343
344 iommu_group_put(group);
345 return 0;
346}
347
348static void gart_iommu_remove_device(struct device *dev)
349{
350 iommu_group_remove_device(dev);
351}
352
Thierry Redingb22f6432014-06-27 09:03:12 +0200353static const struct iommu_ops gart_iommu_ops = {
Joerg Roedel7c2aa642014-09-05 10:51:37 +0200354 .capable = gart_iommu_capable,
Joerg Roedelb5cbb382015-03-26 13:43:13 +0100355 .domain_alloc = gart_iommu_domain_alloc,
356 .domain_free = gart_iommu_domain_free,
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200357 .attach_dev = gart_iommu_attach_dev,
358 .detach_dev = gart_iommu_detach_dev,
Robin Murphy15f9a312017-07-21 13:12:37 +0100359 .add_device = gart_iommu_add_device,
360 .remove_device = gart_iommu_remove_device,
361 .device_group = generic_device_group,
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200362 .map = gart_iommu_map,
Thierry Reding35577072015-01-23 16:37:52 +0100363 .map_sg = default_iommu_map_sg,
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200364 .unmap = gart_iommu_unmap,
365 .iova_to_phys = gart_iommu_iova_to_phys,
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200366 .pgsize_bitmap = GART_IOMMU_PGSIZES,
367};
368
369static int tegra_gart_suspend(struct device *dev)
370{
371 struct gart_device *gart = dev_get_drvdata(dev);
372 unsigned long iova;
373 u32 *data = gart->savedata;
374 unsigned long flags;
375
376 spin_lock_irqsave(&gart->pte_lock, flags);
377 for_each_gart_pte(gart, iova)
378 *(data++) = gart_read_pte(gart, iova);
379 spin_unlock_irqrestore(&gart->pte_lock, flags);
380 return 0;
381}
382
383static int tegra_gart_resume(struct device *dev)
384{
385 struct gart_device *gart = dev_get_drvdata(dev);
386 unsigned long flags;
387
388 spin_lock_irqsave(&gart->pte_lock, flags);
389 do_gart_setup(gart, gart->savedata);
390 spin_unlock_irqrestore(&gart->pte_lock, flags);
391 return 0;
392}
393
394static int tegra_gart_probe(struct platform_device *pdev)
395{
396 struct gart_device *gart;
397 struct resource *res, *res_remap;
398 void __iomem *gart_regs;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200399 struct device *dev = &pdev->dev;
400
401 if (gart_handle)
402 return -EIO;
403
404 BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
405
406 /* the GART memory aperture is required */
407 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
408 res_remap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
409 if (!res || !res_remap) {
410 dev_err(dev, "GART memory aperture expected\n");
411 return -ENXIO;
412 }
413
414 gart = devm_kzalloc(dev, sizeof(*gart), GFP_KERNEL);
415 if (!gart) {
416 dev_err(dev, "failed to allocate gart_device\n");
417 return -ENOMEM;
418 }
419
420 gart_regs = devm_ioremap(dev, res->start, resource_size(res));
421 if (!gart_regs) {
422 dev_err(dev, "failed to remap GART registers\n");
Wei Yongjund0c5b252013-09-24 11:40:24 +0800423 return -ENXIO;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200424 }
425
426 gart->dev = &pdev->dev;
427 spin_lock_init(&gart->pte_lock);
428 spin_lock_init(&gart->client_lock);
429 INIT_LIST_HEAD(&gart->client);
430 gart->regs = gart_regs;
431 gart->iovmm_base = (dma_addr_t)res_remap->start;
432 gart->page_count = (resource_size(res_remap) >> GART_PAGE_SHIFT);
433
434 gart->savedata = vmalloc(sizeof(u32) * gart->page_count);
435 if (!gart->savedata) {
436 dev_err(dev, "failed to allocate context save area\n");
Wei Yongjund0c5b252013-09-24 11:40:24 +0800437 return -ENOMEM;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200438 }
439
440 platform_set_drvdata(pdev, gart);
441 do_gart_setup(gart, NULL);
442
443 gart_handle = gart;
Thierry Redingc7e3ca52015-01-23 16:37:51 +0100444
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200445 return 0;
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200446}
447
448static int tegra_gart_remove(struct platform_device *pdev)
449{
450 struct gart_device *gart = platform_get_drvdata(pdev);
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200451
452 writel(0, gart->regs + GART_CONFIG);
453 if (gart->savedata)
454 vfree(gart->savedata);
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200455 gart_handle = NULL;
456 return 0;
457}
458
Sachin Kamat8a788652013-10-08 16:21:03 +0530459static const struct dev_pm_ops tegra_gart_pm_ops = {
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200460 .suspend = tegra_gart_suspend,
461 .resume = tegra_gart_resume,
462};
463
Kiran Padwald943b0f2014-09-11 19:07:36 +0530464static const struct of_device_id tegra_gart_of_match[] = {
Thierry Reding7cffae42012-04-13 15:08:08 +0200465 { .compatible = "nvidia,tegra20-gart", },
466 { },
467};
468MODULE_DEVICE_TABLE(of, tegra_gart_of_match);
Thierry Reding7cffae42012-04-13 15:08:08 +0200469
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200470static struct platform_driver tegra_gart_driver = {
471 .probe = tegra_gart_probe,
472 .remove = tegra_gart_remove,
473 .driver = {
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200474 .name = "tegra-gart",
475 .pm = &tegra_gart_pm_ops,
Stephen Warrene664e8c2013-02-15 15:01:06 -0700476 .of_match_table = tegra_gart_of_match,
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200477 },
478};
479
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -0800480static int tegra_gart_init(void)
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200481{
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200482 return platform_driver_register(&tegra_gart_driver);
483}
484
485static void __exit tegra_gart_exit(void)
486{
487 platform_driver_unregister(&tegra_gart_driver);
488}
489
490subsys_initcall(tegra_gart_init);
491module_exit(tegra_gart_exit);
492
493MODULE_DESCRIPTION("IOMMU API for GART in Tegra20");
494MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
Thierry Reding7cffae42012-04-13 15:08:08 +0200495MODULE_ALIAS("platform:tegra-gart");
Hiroshi DOYUd53e54b2011-11-16 17:36:37 +0200496MODULE_LICENSE("GPL v2");