iommu/tegra-smmu: Store struct page pointer for page tables
[linux-2.6-block.git] / drivers / iommu / tegra-smmu.c
1 /*
2  * Copyright (C) 2011-2014 NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/bitops.h>
10 #include <linux/debugfs.h>
11 #include <linux/err.h>
12 #include <linux/iommu.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18
19 #include <soc/tegra/ahb.h>
20 #include <soc/tegra/mc.h>
21
22 struct tegra_smmu {
23         void __iomem *regs;
24         struct device *dev;
25
26         struct tegra_mc *mc;
27         const struct tegra_smmu_soc *soc;
28
29         unsigned long pfn_mask;
30
31         unsigned long *asids;
32         struct mutex lock;
33
34         struct list_head list;
35
36         struct dentry *debugfs;
37 };
38
39 struct tegra_smmu_as {
40         struct iommu_domain domain;
41         struct tegra_smmu *smmu;
42         unsigned int use_count;
43         struct page *count;
44         struct page **pts;
45         struct page *pd;
46         unsigned id;
47         u32 attr;
48 };
49
50 static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
51 {
52         return container_of(dom, struct tegra_smmu_as, domain);
53 }
54
55 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
56                                unsigned long offset)
57 {
58         writel(value, smmu->regs + offset);
59 }
60
61 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
62 {
63         return readl(smmu->regs + offset);
64 }
65
66 #define SMMU_CONFIG 0x010
67 #define  SMMU_CONFIG_ENABLE (1 << 0)
68
69 #define SMMU_TLB_CONFIG 0x14
70 #define  SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
71 #define  SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
72 #define  SMMU_TLB_CONFIG_ACTIVE_LINES(x) ((x) & 0x3f)
73
74 #define SMMU_PTC_CONFIG 0x18
75 #define  SMMU_PTC_CONFIG_ENABLE (1 << 29)
76 #define  SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
77 #define  SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
78
79 #define SMMU_PTB_ASID 0x01c
80 #define  SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
81
82 #define SMMU_PTB_DATA 0x020
83 #define  SMMU_PTB_DATA_VALUE(page, attr) (page_to_phys(page) >> 12 | (attr))
84
85 #define SMMU_MK_PDE(page, attr) (page_to_phys(page) >> SMMU_PTE_SHIFT | (attr))
86
87 #define SMMU_TLB_FLUSH 0x030
88 #define  SMMU_TLB_FLUSH_VA_MATCH_ALL     (0 << 0)
89 #define  SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
90 #define  SMMU_TLB_FLUSH_VA_MATCH_GROUP   (3 << 0)
91 #define  SMMU_TLB_FLUSH_ASID(x)          (((x) & 0x7f) << 24)
92 #define  SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
93                                           SMMU_TLB_FLUSH_VA_MATCH_SECTION)
94 #define  SMMU_TLB_FLUSH_VA_GROUP(addr)   ((((addr) & 0xffffc000) >> 12) | \
95                                           SMMU_TLB_FLUSH_VA_MATCH_GROUP)
96 #define  SMMU_TLB_FLUSH_ASID_MATCH       (1 << 31)
97
98 #define SMMU_PTC_FLUSH 0x034
99 #define  SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
100 #define  SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
101
102 #define SMMU_PTC_FLUSH_HI 0x9b8
103 #define  SMMU_PTC_FLUSH_HI_MASK 0x3
104
105 /* per-SWGROUP SMMU_*_ASID register */
106 #define SMMU_ASID_ENABLE (1 << 31)
107 #define SMMU_ASID_MASK 0x7f
108 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
109
110 /* page table definitions */
111 #define SMMU_NUM_PDE 1024
112 #define SMMU_NUM_PTE 1024
113
114 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
115 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
116
117 #define SMMU_PDE_SHIFT 22
118 #define SMMU_PTE_SHIFT 12
119
120 #define SMMU_PD_READABLE        (1 << 31)
121 #define SMMU_PD_WRITABLE        (1 << 30)
122 #define SMMU_PD_NONSECURE       (1 << 29)
123
124 #define SMMU_PDE_READABLE       (1 << 31)
125 #define SMMU_PDE_WRITABLE       (1 << 30)
126 #define SMMU_PDE_NONSECURE      (1 << 29)
127 #define SMMU_PDE_NEXT           (1 << 28)
128
129 #define SMMU_PTE_READABLE       (1 << 31)
130 #define SMMU_PTE_WRITABLE       (1 << 30)
131 #define SMMU_PTE_NONSECURE      (1 << 29)
132
133 #define SMMU_PDE_ATTR           (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
134                                  SMMU_PDE_NONSECURE)
135 #define SMMU_PTE_ATTR           (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
136                                  SMMU_PTE_NONSECURE)
137
138 static unsigned int iova_pd_index(unsigned long iova)
139 {
140         return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
141 }
142
143 static unsigned int iova_pt_index(unsigned long iova)
144 {
145         return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
146 }
147
148 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, struct page *page,
149                                   unsigned long offset)
150 {
151         phys_addr_t phys = page ? page_to_phys(page) : 0;
152         u32 value;
153
154         if (page) {
155                 offset &= ~(smmu->mc->soc->atom_size - 1);
156
157                 if (smmu->mc->soc->num_address_bits > 32) {
158 #ifdef CONFIG_PHYS_ADDR_T_64BIT
159                         value = (phys >> 32) & SMMU_PTC_FLUSH_HI_MASK;
160 #else
161                         value = 0;
162 #endif
163                         smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
164                 }
165
166                 value = (phys + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
167         } else {
168                 value = SMMU_PTC_FLUSH_TYPE_ALL;
169         }
170
171         smmu_writel(smmu, value, SMMU_PTC_FLUSH);
172 }
173
174 static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
175 {
176         smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
177 }
178
179 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
180                                        unsigned long asid)
181 {
182         u32 value;
183
184         value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
185                 SMMU_TLB_FLUSH_VA_MATCH_ALL;
186         smmu_writel(smmu, value, SMMU_TLB_FLUSH);
187 }
188
189 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
190                                           unsigned long asid,
191                                           unsigned long iova)
192 {
193         u32 value;
194
195         value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
196                 SMMU_TLB_FLUSH_VA_SECTION(iova);
197         smmu_writel(smmu, value, SMMU_TLB_FLUSH);
198 }
199
200 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
201                                         unsigned long asid,
202                                         unsigned long iova)
203 {
204         u32 value;
205
206         value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
207                 SMMU_TLB_FLUSH_VA_GROUP(iova);
208         smmu_writel(smmu, value, SMMU_TLB_FLUSH);
209 }
210
211 static inline void smmu_flush(struct tegra_smmu *smmu)
212 {
213         smmu_readl(smmu, SMMU_CONFIG);
214 }
215
216 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
217 {
218         unsigned long id;
219
220         mutex_lock(&smmu->lock);
221
222         id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
223         if (id >= smmu->soc->num_asids) {
224                 mutex_unlock(&smmu->lock);
225                 return -ENOSPC;
226         }
227
228         set_bit(id, smmu->asids);
229         *idp = id;
230
231         mutex_unlock(&smmu->lock);
232         return 0;
233 }
234
235 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
236 {
237         mutex_lock(&smmu->lock);
238         clear_bit(id, smmu->asids);
239         mutex_unlock(&smmu->lock);
240 }
241
242 static bool tegra_smmu_capable(enum iommu_cap cap)
243 {
244         return false;
245 }
246
247 static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
248 {
249         struct tegra_smmu_as *as;
250         unsigned int i;
251         uint32_t *pd;
252
253         if (type != IOMMU_DOMAIN_UNMANAGED)
254                 return NULL;
255
256         as = kzalloc(sizeof(*as), GFP_KERNEL);
257         if (!as)
258                 return NULL;
259
260         as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
261
262         as->pd = alloc_page(GFP_KERNEL | __GFP_DMA);
263         if (!as->pd) {
264                 kfree(as);
265                 return NULL;
266         }
267
268         as->count = alloc_page(GFP_KERNEL);
269         if (!as->count) {
270                 __free_page(as->pd);
271                 kfree(as);
272                 return NULL;
273         }
274
275         as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
276         if (!as->pts) {
277                 __free_page(as->count);
278                 __free_page(as->pd);
279                 kfree(as);
280                 return NULL;
281         }
282
283         /* clear PDEs */
284         pd = page_address(as->pd);
285         SetPageReserved(as->pd);
286
287         for (i = 0; i < SMMU_NUM_PDE; i++)
288                 pd[i] = 0;
289
290         /* clear PDE usage counters */
291         pd = page_address(as->count);
292         SetPageReserved(as->count);
293
294         for (i = 0; i < SMMU_NUM_PDE; i++)
295                 pd[i] = 0;
296
297         /* setup aperture */
298         as->domain.geometry.aperture_start = 0;
299         as->domain.geometry.aperture_end = 0xffffffff;
300         as->domain.geometry.force_aperture = true;
301
302         return &as->domain;
303 }
304
305 static void tegra_smmu_domain_free(struct iommu_domain *domain)
306 {
307         struct tegra_smmu_as *as = to_smmu_as(domain);
308
309         /* TODO: free page directory and page tables */
310         ClearPageReserved(as->pd);
311
312         kfree(as);
313 }
314
315 static const struct tegra_smmu_swgroup *
316 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
317 {
318         const struct tegra_smmu_swgroup *group = NULL;
319         unsigned int i;
320
321         for (i = 0; i < smmu->soc->num_swgroups; i++) {
322                 if (smmu->soc->swgroups[i].swgroup == swgroup) {
323                         group = &smmu->soc->swgroups[i];
324                         break;
325                 }
326         }
327
328         return group;
329 }
330
331 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
332                               unsigned int asid)
333 {
334         const struct tegra_smmu_swgroup *group;
335         unsigned int i;
336         u32 value;
337
338         for (i = 0; i < smmu->soc->num_clients; i++) {
339                 const struct tegra_mc_client *client = &smmu->soc->clients[i];
340
341                 if (client->swgroup != swgroup)
342                         continue;
343
344                 value = smmu_readl(smmu, client->smmu.reg);
345                 value |= BIT(client->smmu.bit);
346                 smmu_writel(smmu, value, client->smmu.reg);
347         }
348
349         group = tegra_smmu_find_swgroup(smmu, swgroup);
350         if (group) {
351                 value = smmu_readl(smmu, group->reg);
352                 value &= ~SMMU_ASID_MASK;
353                 value |= SMMU_ASID_VALUE(asid);
354                 value |= SMMU_ASID_ENABLE;
355                 smmu_writel(smmu, value, group->reg);
356         }
357 }
358
359 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
360                                unsigned int asid)
361 {
362         const struct tegra_smmu_swgroup *group;
363         unsigned int i;
364         u32 value;
365
366         group = tegra_smmu_find_swgroup(smmu, swgroup);
367         if (group) {
368                 value = smmu_readl(smmu, group->reg);
369                 value &= ~SMMU_ASID_MASK;
370                 value |= SMMU_ASID_VALUE(asid);
371                 value &= ~SMMU_ASID_ENABLE;
372                 smmu_writel(smmu, value, group->reg);
373         }
374
375         for (i = 0; i < smmu->soc->num_clients; i++) {
376                 const struct tegra_mc_client *client = &smmu->soc->clients[i];
377
378                 if (client->swgroup != swgroup)
379                         continue;
380
381                 value = smmu_readl(smmu, client->smmu.reg);
382                 value &= ~BIT(client->smmu.bit);
383                 smmu_writel(smmu, value, client->smmu.reg);
384         }
385 }
386
387 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
388                                  struct tegra_smmu_as *as)
389 {
390         u32 value;
391         int err;
392
393         if (as->use_count > 0) {
394                 as->use_count++;
395                 return 0;
396         }
397
398         err = tegra_smmu_alloc_asid(smmu, &as->id);
399         if (err < 0)
400                 return err;
401
402         smmu->soc->ops->flush_dcache(as->pd, 0, SMMU_SIZE_PD);
403         smmu_flush_ptc(smmu, as->pd, 0);
404         smmu_flush_tlb_asid(smmu, as->id);
405
406         smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
407         value = SMMU_PTB_DATA_VALUE(as->pd, as->attr);
408         smmu_writel(smmu, value, SMMU_PTB_DATA);
409         smmu_flush(smmu);
410
411         as->smmu = smmu;
412         as->use_count++;
413
414         return 0;
415 }
416
417 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
418                                     struct tegra_smmu_as *as)
419 {
420         if (--as->use_count > 0)
421                 return;
422
423         tegra_smmu_free_asid(smmu, as->id);
424         as->smmu = NULL;
425 }
426
427 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
428                                  struct device *dev)
429 {
430         struct tegra_smmu *smmu = dev->archdata.iommu;
431         struct tegra_smmu_as *as = to_smmu_as(domain);
432         struct device_node *np = dev->of_node;
433         struct of_phandle_args args;
434         unsigned int index = 0;
435         int err = 0;
436
437         while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
438                                            &args)) {
439                 unsigned int swgroup = args.args[0];
440
441                 if (args.np != smmu->dev->of_node) {
442                         of_node_put(args.np);
443                         continue;
444                 }
445
446                 of_node_put(args.np);
447
448                 err = tegra_smmu_as_prepare(smmu, as);
449                 if (err < 0)
450                         return err;
451
452                 tegra_smmu_enable(smmu, swgroup, as->id);
453                 index++;
454         }
455
456         if (index == 0)
457                 return -ENODEV;
458
459         return 0;
460 }
461
462 static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
463 {
464         struct tegra_smmu_as *as = to_smmu_as(domain);
465         struct device_node *np = dev->of_node;
466         struct tegra_smmu *smmu = as->smmu;
467         struct of_phandle_args args;
468         unsigned int index = 0;
469
470         while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
471                                            &args)) {
472                 unsigned int swgroup = args.args[0];
473
474                 if (args.np != smmu->dev->of_node) {
475                         of_node_put(args.np);
476                         continue;
477                 }
478
479                 of_node_put(args.np);
480
481                 tegra_smmu_disable(smmu, swgroup, as->id);
482                 tegra_smmu_as_unprepare(smmu, as);
483                 index++;
484         }
485 }
486
487 static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
488 {
489         u32 *pt = page_address(pt_page);
490
491         return pt + iova_pt_index(iova);
492 }
493
494 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
495                                   struct page **pagep)
496 {
497         unsigned int pd_index = iova_pd_index(iova);
498         struct page *pt_page;
499
500         pt_page = as->pts[pd_index];
501         if (!pt_page)
502                 return NULL;
503
504         *pagep = pt_page;
505
506         return tegra_smmu_pte_offset(pt_page, iova);
507 }
508
509 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
510                        struct page **pagep)
511 {
512         u32 *pd = page_address(as->pd), *pt, *count;
513         unsigned int pde = iova_pd_index(iova);
514         struct tegra_smmu *smmu = as->smmu;
515         struct page *page;
516         unsigned int i;
517
518         if (!as->pts[pde]) {
519                 page = alloc_page(GFP_KERNEL | __GFP_DMA);
520                 if (!page)
521                         return NULL;
522
523                 pt = page_address(page);
524                 SetPageReserved(page);
525
526                 for (i = 0; i < SMMU_NUM_PTE; i++)
527                         pt[i] = 0;
528
529                 as->pts[pde] = page;
530
531                 smmu->soc->ops->flush_dcache(page, 0, SMMU_SIZE_PT);
532
533                 pd[pde] = SMMU_MK_PDE(page, SMMU_PDE_ATTR | SMMU_PDE_NEXT);
534
535                 smmu->soc->ops->flush_dcache(as->pd, pde << 2, 4);
536                 smmu_flush_ptc(smmu, as->pd, pde << 2);
537                 smmu_flush_tlb_section(smmu, as->id, iova);
538                 smmu_flush(smmu);
539         } else {
540                 page = as->pts[pde];
541         }
542
543         *pagep = page;
544
545         pt = page_address(page);
546
547         /* Keep track of entries in this page table. */
548         count = page_address(as->count);
549         if (pt[iova_pt_index(iova)] == 0)
550                 count[pde]++;
551
552         return tegra_smmu_pte_offset(page, iova);
553 }
554
555 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
556 {
557         struct tegra_smmu *smmu = as->smmu;
558         unsigned int pde = iova_pd_index(iova);
559         u32 *count = page_address(as->count);
560         u32 *pd = page_address(as->pd);
561         struct page *page = as->pts[pde];
562
563         /*
564          * When no entries in this page table are used anymore, return the
565          * memory page to the system.
566          */
567         if (--count[pde] == 0) {
568                 unsigned int offset = pde * sizeof(*pd);
569
570                 /* Clear the page directory entry first */
571                 pd[pde] = 0;
572
573                 /* Flush the page directory entry */
574                 smmu->soc->ops->flush_dcache(as->pd, offset, sizeof(*pd));
575                 smmu_flush_ptc(smmu, as->pd, offset);
576                 smmu_flush_tlb_section(smmu, as->id, iova);
577                 smmu_flush(smmu);
578
579                 /* Finally, free the page */
580                 ClearPageReserved(page);
581                 __free_page(page);
582                 as->pts[pde] = NULL;
583         }
584 }
585
586 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
587                                u32 *pte, struct page *pte_page, u32 val)
588 {
589         struct tegra_smmu *smmu = as->smmu;
590         unsigned long offset = offset_in_page(pte);
591
592         *pte = val;
593
594         smmu->soc->ops->flush_dcache(pte_page, offset, 4);
595         smmu_flush_ptc(smmu, pte_page, offset);
596         smmu_flush_tlb_group(smmu, as->id, iova);
597         smmu_flush(smmu);
598 }
599
600 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
601                           phys_addr_t paddr, size_t size, int prot)
602 {
603         struct tegra_smmu_as *as = to_smmu_as(domain);
604         struct page *page;
605         u32 *pte;
606
607         pte = as_get_pte(as, iova, &page);
608         if (!pte)
609                 return -ENOMEM;
610
611         tegra_smmu_set_pte(as, iova, pte, page,
612                            __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
613
614         return 0;
615 }
616
617 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
618                                size_t size)
619 {
620         struct tegra_smmu_as *as = to_smmu_as(domain);
621         struct page *pte_page;
622         u32 *pte;
623
624         pte = tegra_smmu_pte_lookup(as, iova, &pte_page);
625         if (!pte || !*pte)
626                 return 0;
627
628         tegra_smmu_set_pte(as, iova, pte, pte_page, 0);
629         tegra_smmu_pte_put_use(as, iova);
630
631         return size;
632 }
633
634 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
635                                            dma_addr_t iova)
636 {
637         struct tegra_smmu_as *as = to_smmu_as(domain);
638         struct page *pte_page;
639         unsigned long pfn;
640         u32 *pte;
641
642         pte = tegra_smmu_pte_lookup(as, iova, &pte_page);
643         if (!pte || !*pte)
644                 return 0;
645
646         pfn = *pte & as->smmu->pfn_mask;
647
648         return PFN_PHYS(pfn);
649 }
650
651 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
652 {
653         struct platform_device *pdev;
654         struct tegra_mc *mc;
655
656         pdev = of_find_device_by_node(np);
657         if (!pdev)
658                 return NULL;
659
660         mc = platform_get_drvdata(pdev);
661         if (!mc)
662                 return NULL;
663
664         return mc->smmu;
665 }
666
667 static int tegra_smmu_add_device(struct device *dev)
668 {
669         struct device_node *np = dev->of_node;
670         struct of_phandle_args args;
671         unsigned int index = 0;
672
673         while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
674                                           &args) == 0) {
675                 struct tegra_smmu *smmu;
676
677                 smmu = tegra_smmu_find(args.np);
678                 if (smmu) {
679                         /*
680                          * Only a single IOMMU master interface is currently
681                          * supported by the Linux kernel, so abort after the
682                          * first match.
683                          */
684                         dev->archdata.iommu = smmu;
685                         break;
686                 }
687
688                 index++;
689         }
690
691         return 0;
692 }
693
694 static void tegra_smmu_remove_device(struct device *dev)
695 {
696         dev->archdata.iommu = NULL;
697 }
698
699 static const struct iommu_ops tegra_smmu_ops = {
700         .capable = tegra_smmu_capable,
701         .domain_alloc = tegra_smmu_domain_alloc,
702         .domain_free = tegra_smmu_domain_free,
703         .attach_dev = tegra_smmu_attach_dev,
704         .detach_dev = tegra_smmu_detach_dev,
705         .add_device = tegra_smmu_add_device,
706         .remove_device = tegra_smmu_remove_device,
707         .map = tegra_smmu_map,
708         .unmap = tegra_smmu_unmap,
709         .map_sg = default_iommu_map_sg,
710         .iova_to_phys = tegra_smmu_iova_to_phys,
711
712         .pgsize_bitmap = SZ_4K,
713 };
714
715 static void tegra_smmu_ahb_enable(void)
716 {
717         static const struct of_device_id ahb_match[] = {
718                 { .compatible = "nvidia,tegra30-ahb", },
719                 { }
720         };
721         struct device_node *ahb;
722
723         ahb = of_find_matching_node(NULL, ahb_match);
724         if (ahb) {
725                 tegra_ahb_enable_smmu(ahb);
726                 of_node_put(ahb);
727         }
728 }
729
730 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
731 {
732         struct tegra_smmu *smmu = s->private;
733         unsigned int i;
734         u32 value;
735
736         seq_printf(s, "swgroup    enabled  ASID\n");
737         seq_printf(s, "------------------------\n");
738
739         for (i = 0; i < smmu->soc->num_swgroups; i++) {
740                 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
741                 const char *status;
742                 unsigned int asid;
743
744                 value = smmu_readl(smmu, group->reg);
745
746                 if (value & SMMU_ASID_ENABLE)
747                         status = "yes";
748                 else
749                         status = "no";
750
751                 asid = value & SMMU_ASID_MASK;
752
753                 seq_printf(s, "%-9s  %-7s  %#04x\n", group->name, status,
754                            asid);
755         }
756
757         return 0;
758 }
759
760 static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
761 {
762         return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
763 }
764
765 static const struct file_operations tegra_smmu_swgroups_fops = {
766         .open = tegra_smmu_swgroups_open,
767         .read = seq_read,
768         .llseek = seq_lseek,
769         .release = single_release,
770 };
771
772 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
773 {
774         struct tegra_smmu *smmu = s->private;
775         unsigned int i;
776         u32 value;
777
778         seq_printf(s, "client       enabled\n");
779         seq_printf(s, "--------------------\n");
780
781         for (i = 0; i < smmu->soc->num_clients; i++) {
782                 const struct tegra_mc_client *client = &smmu->soc->clients[i];
783                 const char *status;
784
785                 value = smmu_readl(smmu, client->smmu.reg);
786
787                 if (value & BIT(client->smmu.bit))
788                         status = "yes";
789                 else
790                         status = "no";
791
792                 seq_printf(s, "%-12s %s\n", client->name, status);
793         }
794
795         return 0;
796 }
797
798 static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
799 {
800         return single_open(file, tegra_smmu_clients_show, inode->i_private);
801 }
802
803 static const struct file_operations tegra_smmu_clients_fops = {
804         .open = tegra_smmu_clients_open,
805         .read = seq_read,
806         .llseek = seq_lseek,
807         .release = single_release,
808 };
809
810 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
811 {
812         smmu->debugfs = debugfs_create_dir("smmu", NULL);
813         if (!smmu->debugfs)
814                 return;
815
816         debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
817                             &tegra_smmu_swgroups_fops);
818         debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
819                             &tegra_smmu_clients_fops);
820 }
821
822 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
823 {
824         debugfs_remove_recursive(smmu->debugfs);
825 }
826
827 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
828                                     const struct tegra_smmu_soc *soc,
829                                     struct tegra_mc *mc)
830 {
831         struct tegra_smmu *smmu;
832         size_t size;
833         u32 value;
834         int err;
835
836         /* This can happen on Tegra20 which doesn't have an SMMU */
837         if (!soc)
838                 return NULL;
839
840         smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
841         if (!smmu)
842                 return ERR_PTR(-ENOMEM);
843
844         /*
845          * This is a bit of a hack. Ideally we'd want to simply return this
846          * value. However the IOMMU registration process will attempt to add
847          * all devices to the IOMMU when bus_set_iommu() is called. In order
848          * not to rely on global variables to track the IOMMU instance, we
849          * set it here so that it can be looked up from the .add_device()
850          * callback via the IOMMU device's .drvdata field.
851          */
852         mc->smmu = smmu;
853
854         size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
855
856         smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
857         if (!smmu->asids)
858                 return ERR_PTR(-ENOMEM);
859
860         mutex_init(&smmu->lock);
861
862         smmu->regs = mc->regs;
863         smmu->soc = soc;
864         smmu->dev = dev;
865         smmu->mc = mc;
866
867         smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
868         dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
869                 mc->soc->num_address_bits, smmu->pfn_mask);
870
871         value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
872
873         if (soc->supports_request_limit)
874                 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
875
876         smmu_writel(smmu, value, SMMU_PTC_CONFIG);
877
878         value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
879                 SMMU_TLB_CONFIG_ACTIVE_LINES(0x20);
880
881         if (soc->supports_round_robin_arbitration)
882                 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
883
884         smmu_writel(smmu, value, SMMU_TLB_CONFIG);
885
886         smmu_flush_ptc(smmu, NULL, 0);
887         smmu_flush_tlb(smmu);
888         smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
889         smmu_flush(smmu);
890
891         tegra_smmu_ahb_enable();
892
893         err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
894         if (err < 0)
895                 return ERR_PTR(err);
896
897         if (IS_ENABLED(CONFIG_DEBUG_FS))
898                 tegra_smmu_debugfs_init(smmu);
899
900         return smmu;
901 }
902
903 void tegra_smmu_remove(struct tegra_smmu *smmu)
904 {
905         if (IS_ENABLED(CONFIG_DEBUG_FS))
906                 tegra_smmu_debugfs_exit(smmu);
907 }