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