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