iommu/tegra-smmu: Use kcalloc() to allocate counter array
[cascardo/linux.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         u32 *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 = kcalloc(SMMU_NUM_PDE, sizeof(u32), 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                 kfree(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         /* setup aperture */
291         as->domain.geometry.aperture_start = 0;
292         as->domain.geometry.aperture_end = 0xffffffff;
293         as->domain.geometry.force_aperture = true;
294
295         return &as->domain;
296 }
297
298 static void tegra_smmu_domain_free(struct iommu_domain *domain)
299 {
300         struct tegra_smmu_as *as = to_smmu_as(domain);
301
302         /* TODO: free page directory and page tables */
303         ClearPageReserved(as->pd);
304
305         kfree(as);
306 }
307
308 static const struct tegra_smmu_swgroup *
309 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
310 {
311         const struct tegra_smmu_swgroup *group = NULL;
312         unsigned int i;
313
314         for (i = 0; i < smmu->soc->num_swgroups; i++) {
315                 if (smmu->soc->swgroups[i].swgroup == swgroup) {
316                         group = &smmu->soc->swgroups[i];
317                         break;
318                 }
319         }
320
321         return group;
322 }
323
324 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
325                               unsigned int asid)
326 {
327         const struct tegra_smmu_swgroup *group;
328         unsigned int i;
329         u32 value;
330
331         for (i = 0; i < smmu->soc->num_clients; i++) {
332                 const struct tegra_mc_client *client = &smmu->soc->clients[i];
333
334                 if (client->swgroup != swgroup)
335                         continue;
336
337                 value = smmu_readl(smmu, client->smmu.reg);
338                 value |= BIT(client->smmu.bit);
339                 smmu_writel(smmu, value, client->smmu.reg);
340         }
341
342         group = tegra_smmu_find_swgroup(smmu, swgroup);
343         if (group) {
344                 value = smmu_readl(smmu, group->reg);
345                 value &= ~SMMU_ASID_MASK;
346                 value |= SMMU_ASID_VALUE(asid);
347                 value |= SMMU_ASID_ENABLE;
348                 smmu_writel(smmu, value, group->reg);
349         }
350 }
351
352 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
353                                unsigned int asid)
354 {
355         const struct tegra_smmu_swgroup *group;
356         unsigned int i;
357         u32 value;
358
359         group = tegra_smmu_find_swgroup(smmu, swgroup);
360         if (group) {
361                 value = smmu_readl(smmu, group->reg);
362                 value &= ~SMMU_ASID_MASK;
363                 value |= SMMU_ASID_VALUE(asid);
364                 value &= ~SMMU_ASID_ENABLE;
365                 smmu_writel(smmu, value, group->reg);
366         }
367
368         for (i = 0; i < smmu->soc->num_clients; i++) {
369                 const struct tegra_mc_client *client = &smmu->soc->clients[i];
370
371                 if (client->swgroup != swgroup)
372                         continue;
373
374                 value = smmu_readl(smmu, client->smmu.reg);
375                 value &= ~BIT(client->smmu.bit);
376                 smmu_writel(smmu, value, client->smmu.reg);
377         }
378 }
379
380 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
381                                  struct tegra_smmu_as *as)
382 {
383         u32 value;
384         int err;
385
386         if (as->use_count > 0) {
387                 as->use_count++;
388                 return 0;
389         }
390
391         err = tegra_smmu_alloc_asid(smmu, &as->id);
392         if (err < 0)
393                 return err;
394
395         smmu->soc->ops->flush_dcache(as->pd, 0, SMMU_SIZE_PD);
396         smmu_flush_ptc(smmu, as->pd, 0);
397         smmu_flush_tlb_asid(smmu, as->id);
398
399         smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
400         value = SMMU_PTB_DATA_VALUE(as->pd, as->attr);
401         smmu_writel(smmu, value, SMMU_PTB_DATA);
402         smmu_flush(smmu);
403
404         as->smmu = smmu;
405         as->use_count++;
406
407         return 0;
408 }
409
410 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
411                                     struct tegra_smmu_as *as)
412 {
413         if (--as->use_count > 0)
414                 return;
415
416         tegra_smmu_free_asid(smmu, as->id);
417         as->smmu = NULL;
418 }
419
420 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
421                                  struct device *dev)
422 {
423         struct tegra_smmu *smmu = dev->archdata.iommu;
424         struct tegra_smmu_as *as = to_smmu_as(domain);
425         struct device_node *np = dev->of_node;
426         struct of_phandle_args args;
427         unsigned int index = 0;
428         int err = 0;
429
430         while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
431                                            &args)) {
432                 unsigned int swgroup = args.args[0];
433
434                 if (args.np != smmu->dev->of_node) {
435                         of_node_put(args.np);
436                         continue;
437                 }
438
439                 of_node_put(args.np);
440
441                 err = tegra_smmu_as_prepare(smmu, as);
442                 if (err < 0)
443                         return err;
444
445                 tegra_smmu_enable(smmu, swgroup, as->id);
446                 index++;
447         }
448
449         if (index == 0)
450                 return -ENODEV;
451
452         return 0;
453 }
454
455 static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
456 {
457         struct tegra_smmu_as *as = to_smmu_as(domain);
458         struct device_node *np = dev->of_node;
459         struct tegra_smmu *smmu = as->smmu;
460         struct of_phandle_args args;
461         unsigned int index = 0;
462
463         while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
464                                            &args)) {
465                 unsigned int swgroup = args.args[0];
466
467                 if (args.np != smmu->dev->of_node) {
468                         of_node_put(args.np);
469                         continue;
470                 }
471
472                 of_node_put(args.np);
473
474                 tegra_smmu_disable(smmu, swgroup, as->id);
475                 tegra_smmu_as_unprepare(smmu, as);
476                 index++;
477         }
478 }
479
480 static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
481 {
482         u32 *pt = page_address(pt_page);
483
484         return pt + iova_pt_index(iova);
485 }
486
487 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
488                                   struct page **pagep)
489 {
490         unsigned int pd_index = iova_pd_index(iova);
491         struct page *pt_page;
492
493         pt_page = as->pts[pd_index];
494         if (!pt_page)
495                 return NULL;
496
497         *pagep = pt_page;
498
499         return tegra_smmu_pte_offset(pt_page, iova);
500 }
501
502 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
503                        struct page **pagep)
504 {
505         u32 *pd = page_address(as->pd), *pt;
506         unsigned int pde = iova_pd_index(iova);
507         struct tegra_smmu *smmu = as->smmu;
508         struct page *page;
509         unsigned int i;
510
511         if (!as->pts[pde]) {
512                 page = alloc_page(GFP_KERNEL | __GFP_DMA);
513                 if (!page)
514                         return NULL;
515
516                 pt = page_address(page);
517                 SetPageReserved(page);
518
519                 for (i = 0; i < SMMU_NUM_PTE; i++)
520                         pt[i] = 0;
521
522                 as->pts[pde] = page;
523
524                 smmu->soc->ops->flush_dcache(page, 0, SMMU_SIZE_PT);
525
526                 pd[pde] = SMMU_MK_PDE(page, SMMU_PDE_ATTR | SMMU_PDE_NEXT);
527
528                 smmu->soc->ops->flush_dcache(as->pd, pde << 2, 4);
529                 smmu_flush_ptc(smmu, as->pd, pde << 2);
530                 smmu_flush_tlb_section(smmu, as->id, iova);
531                 smmu_flush(smmu);
532         } else {
533                 page = as->pts[pde];
534         }
535
536         *pagep = page;
537
538         pt = page_address(page);
539
540         /* Keep track of entries in this page table. */
541         if (pt[iova_pt_index(iova)] == 0)
542                 as->count[pde]++;
543
544         return tegra_smmu_pte_offset(page, iova);
545 }
546
547 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
548 {
549         struct tegra_smmu *smmu = as->smmu;
550         unsigned int pde = iova_pd_index(iova);
551         u32 *pd = page_address(as->pd);
552         struct page *page = as->pts[pde];
553
554         /*
555          * When no entries in this page table are used anymore, return the
556          * memory page to the system.
557          */
558         if (--as->count[pde] == 0) {
559                 unsigned int offset = pde * sizeof(*pd);
560
561                 /* Clear the page directory entry first */
562                 pd[pde] = 0;
563
564                 /* Flush the page directory entry */
565                 smmu->soc->ops->flush_dcache(as->pd, offset, sizeof(*pd));
566                 smmu_flush_ptc(smmu, as->pd, offset);
567                 smmu_flush_tlb_section(smmu, as->id, iova);
568                 smmu_flush(smmu);
569
570                 /* Finally, free the page */
571                 ClearPageReserved(page);
572                 __free_page(page);
573                 as->pts[pde] = NULL;
574         }
575 }
576
577 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
578                                u32 *pte, struct page *pte_page, u32 val)
579 {
580         struct tegra_smmu *smmu = as->smmu;
581         unsigned long offset = offset_in_page(pte);
582
583         *pte = val;
584
585         smmu->soc->ops->flush_dcache(pte_page, offset, 4);
586         smmu_flush_ptc(smmu, pte_page, offset);
587         smmu_flush_tlb_group(smmu, as->id, iova);
588         smmu_flush(smmu);
589 }
590
591 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
592                           phys_addr_t paddr, size_t size, int prot)
593 {
594         struct tegra_smmu_as *as = to_smmu_as(domain);
595         struct page *page;
596         u32 *pte;
597
598         pte = as_get_pte(as, iova, &page);
599         if (!pte)
600                 return -ENOMEM;
601
602         tegra_smmu_set_pte(as, iova, pte, page,
603                            __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
604
605         return 0;
606 }
607
608 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
609                                size_t size)
610 {
611         struct tegra_smmu_as *as = to_smmu_as(domain);
612         struct page *pte_page;
613         u32 *pte;
614
615         pte = tegra_smmu_pte_lookup(as, iova, &pte_page);
616         if (!pte || !*pte)
617                 return 0;
618
619         tegra_smmu_set_pte(as, iova, pte, pte_page, 0);
620         tegra_smmu_pte_put_use(as, iova);
621
622         return size;
623 }
624
625 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
626                                            dma_addr_t iova)
627 {
628         struct tegra_smmu_as *as = to_smmu_as(domain);
629         struct page *pte_page;
630         unsigned long pfn;
631         u32 *pte;
632
633         pte = tegra_smmu_pte_lookup(as, iova, &pte_page);
634         if (!pte || !*pte)
635                 return 0;
636
637         pfn = *pte & as->smmu->pfn_mask;
638
639         return PFN_PHYS(pfn);
640 }
641
642 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
643 {
644         struct platform_device *pdev;
645         struct tegra_mc *mc;
646
647         pdev = of_find_device_by_node(np);
648         if (!pdev)
649                 return NULL;
650
651         mc = platform_get_drvdata(pdev);
652         if (!mc)
653                 return NULL;
654
655         return mc->smmu;
656 }
657
658 static int tegra_smmu_add_device(struct device *dev)
659 {
660         struct device_node *np = dev->of_node;
661         struct of_phandle_args args;
662         unsigned int index = 0;
663
664         while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
665                                           &args) == 0) {
666                 struct tegra_smmu *smmu;
667
668                 smmu = tegra_smmu_find(args.np);
669                 if (smmu) {
670                         /*
671                          * Only a single IOMMU master interface is currently
672                          * supported by the Linux kernel, so abort after the
673                          * first match.
674                          */
675                         dev->archdata.iommu = smmu;
676                         break;
677                 }
678
679                 index++;
680         }
681
682         return 0;
683 }
684
685 static void tegra_smmu_remove_device(struct device *dev)
686 {
687         dev->archdata.iommu = NULL;
688 }
689
690 static const struct iommu_ops tegra_smmu_ops = {
691         .capable = tegra_smmu_capable,
692         .domain_alloc = tegra_smmu_domain_alloc,
693         .domain_free = tegra_smmu_domain_free,
694         .attach_dev = tegra_smmu_attach_dev,
695         .detach_dev = tegra_smmu_detach_dev,
696         .add_device = tegra_smmu_add_device,
697         .remove_device = tegra_smmu_remove_device,
698         .map = tegra_smmu_map,
699         .unmap = tegra_smmu_unmap,
700         .map_sg = default_iommu_map_sg,
701         .iova_to_phys = tegra_smmu_iova_to_phys,
702
703         .pgsize_bitmap = SZ_4K,
704 };
705
706 static void tegra_smmu_ahb_enable(void)
707 {
708         static const struct of_device_id ahb_match[] = {
709                 { .compatible = "nvidia,tegra30-ahb", },
710                 { }
711         };
712         struct device_node *ahb;
713
714         ahb = of_find_matching_node(NULL, ahb_match);
715         if (ahb) {
716                 tegra_ahb_enable_smmu(ahb);
717                 of_node_put(ahb);
718         }
719 }
720
721 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
722 {
723         struct tegra_smmu *smmu = s->private;
724         unsigned int i;
725         u32 value;
726
727         seq_printf(s, "swgroup    enabled  ASID\n");
728         seq_printf(s, "------------------------\n");
729
730         for (i = 0; i < smmu->soc->num_swgroups; i++) {
731                 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
732                 const char *status;
733                 unsigned int asid;
734
735                 value = smmu_readl(smmu, group->reg);
736
737                 if (value & SMMU_ASID_ENABLE)
738                         status = "yes";
739                 else
740                         status = "no";
741
742                 asid = value & SMMU_ASID_MASK;
743
744                 seq_printf(s, "%-9s  %-7s  %#04x\n", group->name, status,
745                            asid);
746         }
747
748         return 0;
749 }
750
751 static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
752 {
753         return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
754 }
755
756 static const struct file_operations tegra_smmu_swgroups_fops = {
757         .open = tegra_smmu_swgroups_open,
758         .read = seq_read,
759         .llseek = seq_lseek,
760         .release = single_release,
761 };
762
763 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
764 {
765         struct tegra_smmu *smmu = s->private;
766         unsigned int i;
767         u32 value;
768
769         seq_printf(s, "client       enabled\n");
770         seq_printf(s, "--------------------\n");
771
772         for (i = 0; i < smmu->soc->num_clients; i++) {
773                 const struct tegra_mc_client *client = &smmu->soc->clients[i];
774                 const char *status;
775
776                 value = smmu_readl(smmu, client->smmu.reg);
777
778                 if (value & BIT(client->smmu.bit))
779                         status = "yes";
780                 else
781                         status = "no";
782
783                 seq_printf(s, "%-12s %s\n", client->name, status);
784         }
785
786         return 0;
787 }
788
789 static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
790 {
791         return single_open(file, tegra_smmu_clients_show, inode->i_private);
792 }
793
794 static const struct file_operations tegra_smmu_clients_fops = {
795         .open = tegra_smmu_clients_open,
796         .read = seq_read,
797         .llseek = seq_lseek,
798         .release = single_release,
799 };
800
801 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
802 {
803         smmu->debugfs = debugfs_create_dir("smmu", NULL);
804         if (!smmu->debugfs)
805                 return;
806
807         debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
808                             &tegra_smmu_swgroups_fops);
809         debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
810                             &tegra_smmu_clients_fops);
811 }
812
813 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
814 {
815         debugfs_remove_recursive(smmu->debugfs);
816 }
817
818 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
819                                     const struct tegra_smmu_soc *soc,
820                                     struct tegra_mc *mc)
821 {
822         struct tegra_smmu *smmu;
823         size_t size;
824         u32 value;
825         int err;
826
827         /* This can happen on Tegra20 which doesn't have an SMMU */
828         if (!soc)
829                 return NULL;
830
831         smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
832         if (!smmu)
833                 return ERR_PTR(-ENOMEM);
834
835         /*
836          * This is a bit of a hack. Ideally we'd want to simply return this
837          * value. However the IOMMU registration process will attempt to add
838          * all devices to the IOMMU when bus_set_iommu() is called. In order
839          * not to rely on global variables to track the IOMMU instance, we
840          * set it here so that it can be looked up from the .add_device()
841          * callback via the IOMMU device's .drvdata field.
842          */
843         mc->smmu = smmu;
844
845         size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
846
847         smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
848         if (!smmu->asids)
849                 return ERR_PTR(-ENOMEM);
850
851         mutex_init(&smmu->lock);
852
853         smmu->regs = mc->regs;
854         smmu->soc = soc;
855         smmu->dev = dev;
856         smmu->mc = mc;
857
858         smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
859         dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
860                 mc->soc->num_address_bits, smmu->pfn_mask);
861
862         value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
863
864         if (soc->supports_request_limit)
865                 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
866
867         smmu_writel(smmu, value, SMMU_PTC_CONFIG);
868
869         value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
870                 SMMU_TLB_CONFIG_ACTIVE_LINES(0x20);
871
872         if (soc->supports_round_robin_arbitration)
873                 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
874
875         smmu_writel(smmu, value, SMMU_TLB_CONFIG);
876
877         smmu_flush_ptc(smmu, NULL, 0);
878         smmu_flush_tlb(smmu);
879         smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
880         smmu_flush(smmu);
881
882         tegra_smmu_ahb_enable();
883
884         err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
885         if (err < 0)
886                 return ERR_PTR(err);
887
888         if (IS_ENABLED(CONFIG_DEBUG_FS))
889                 tegra_smmu_debugfs_init(smmu);
890
891         return smmu;
892 }
893
894 void tegra_smmu_remove(struct tegra_smmu *smmu)
895 {
896         if (IS_ENABLED(CONFIG_DEBUG_FS))
897                 tegra_smmu_debugfs_exit(smmu);
898 }