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