rtc: ds1307: factor out century bit handling
[linux-2.6/btrfs-unstable.git] / drivers / iommu / tegra-smmu.c
blobeeb19f560a05ee54d87da40859e206626f1418a6
1 /*
2 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
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 */
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>
20 #include <soc/tegra/ahb.h>
21 #include <soc/tegra/mc.h>
23 struct tegra_smmu {
24 void __iomem *regs;
25 struct device *dev;
27 struct tegra_mc *mc;
28 const struct tegra_smmu_soc *soc;
30 unsigned long pfn_mask;
31 unsigned long tlb_mask;
33 unsigned long *asids;
34 struct mutex lock;
36 struct list_head list;
38 struct dentry *debugfs;
41 struct tegra_smmu_as {
42 struct iommu_domain domain;
43 struct tegra_smmu *smmu;
44 unsigned int use_count;
45 u32 *count;
46 struct page **pts;
47 struct page *pd;
48 dma_addr_t pd_dma;
49 unsigned id;
50 u32 attr;
53 static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
55 return container_of(dom, struct tegra_smmu_as, domain);
58 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
59 unsigned long offset)
61 writel(value, smmu->regs + offset);
64 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
66 return readl(smmu->regs + offset);
69 #define SMMU_CONFIG 0x010
70 #define SMMU_CONFIG_ENABLE (1 << 0)
72 #define SMMU_TLB_CONFIG 0x14
73 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
74 #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
75 #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
76 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
78 #define SMMU_PTC_CONFIG 0x18
79 #define SMMU_PTC_CONFIG_ENABLE (1 << 29)
80 #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
81 #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
83 #define SMMU_PTB_ASID 0x01c
84 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
86 #define SMMU_PTB_DATA 0x020
87 #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
89 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
91 #define SMMU_TLB_FLUSH 0x030
92 #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
93 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
94 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
95 #define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24)
96 #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
97 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
98 #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
99 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
100 #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
102 #define SMMU_PTC_FLUSH 0x034
103 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
104 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
106 #define SMMU_PTC_FLUSH_HI 0x9b8
107 #define SMMU_PTC_FLUSH_HI_MASK 0x3
109 /* per-SWGROUP SMMU_*_ASID register */
110 #define SMMU_ASID_ENABLE (1 << 31)
111 #define SMMU_ASID_MASK 0x7f
112 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
114 /* page table definitions */
115 #define SMMU_NUM_PDE 1024
116 #define SMMU_NUM_PTE 1024
118 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
119 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
121 #define SMMU_PDE_SHIFT 22
122 #define SMMU_PTE_SHIFT 12
124 #define SMMU_PD_READABLE (1 << 31)
125 #define SMMU_PD_WRITABLE (1 << 30)
126 #define SMMU_PD_NONSECURE (1 << 29)
128 #define SMMU_PDE_READABLE (1 << 31)
129 #define SMMU_PDE_WRITABLE (1 << 30)
130 #define SMMU_PDE_NONSECURE (1 << 29)
131 #define SMMU_PDE_NEXT (1 << 28)
133 #define SMMU_PTE_READABLE (1 << 31)
134 #define SMMU_PTE_WRITABLE (1 << 30)
135 #define SMMU_PTE_NONSECURE (1 << 29)
137 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
138 SMMU_PDE_NONSECURE)
139 #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
140 SMMU_PTE_NONSECURE)
142 static unsigned int iova_pd_index(unsigned long iova)
144 return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
147 static unsigned int iova_pt_index(unsigned long iova)
149 return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
152 static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
154 addr >>= 12;
155 return (addr & smmu->pfn_mask) == addr;
158 static dma_addr_t smmu_pde_to_dma(u32 pde)
160 return pde << 12;
163 static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
165 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
168 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
169 unsigned long offset)
171 u32 value;
173 offset &= ~(smmu->mc->soc->atom_size - 1);
175 if (smmu->mc->soc->num_address_bits > 32) {
176 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
177 value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
178 #else
179 value = 0;
180 #endif
181 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
184 value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
185 smmu_writel(smmu, value, SMMU_PTC_FLUSH);
188 static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
190 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
193 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
194 unsigned long asid)
196 u32 value;
198 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
199 SMMU_TLB_FLUSH_VA_MATCH_ALL;
200 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
203 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
204 unsigned long asid,
205 unsigned long iova)
207 u32 value;
209 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
210 SMMU_TLB_FLUSH_VA_SECTION(iova);
211 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
214 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
215 unsigned long asid,
216 unsigned long iova)
218 u32 value;
220 value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
221 SMMU_TLB_FLUSH_VA_GROUP(iova);
222 smmu_writel(smmu, value, SMMU_TLB_FLUSH);
225 static inline void smmu_flush(struct tegra_smmu *smmu)
227 smmu_readl(smmu, SMMU_CONFIG);
230 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
232 unsigned long id;
234 mutex_lock(&smmu->lock);
236 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
237 if (id >= smmu->soc->num_asids) {
238 mutex_unlock(&smmu->lock);
239 return -ENOSPC;
242 set_bit(id, smmu->asids);
243 *idp = id;
245 mutex_unlock(&smmu->lock);
246 return 0;
249 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
251 mutex_lock(&smmu->lock);
252 clear_bit(id, smmu->asids);
253 mutex_unlock(&smmu->lock);
256 static bool tegra_smmu_capable(enum iommu_cap cap)
258 return false;
261 static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
263 struct tegra_smmu_as *as;
265 if (type != IOMMU_DOMAIN_UNMANAGED)
266 return NULL;
268 as = kzalloc(sizeof(*as), GFP_KERNEL);
269 if (!as)
270 return NULL;
272 as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
274 as->pd = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
275 if (!as->pd) {
276 kfree(as);
277 return NULL;
280 as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
281 if (!as->count) {
282 __free_page(as->pd);
283 kfree(as);
284 return NULL;
287 as->pts = kcalloc(SMMU_NUM_PDE, sizeof(*as->pts), GFP_KERNEL);
288 if (!as->pts) {
289 kfree(as->count);
290 __free_page(as->pd);
291 kfree(as);
292 return NULL;
295 /* setup aperture */
296 as->domain.geometry.aperture_start = 0;
297 as->domain.geometry.aperture_end = 0xffffffff;
298 as->domain.geometry.force_aperture = true;
300 return &as->domain;
303 static void tegra_smmu_domain_free(struct iommu_domain *domain)
305 struct tegra_smmu_as *as = to_smmu_as(domain);
307 /* TODO: free page directory and page tables */
309 kfree(as);
312 static const struct tegra_smmu_swgroup *
313 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
315 const struct tegra_smmu_swgroup *group = NULL;
316 unsigned int i;
318 for (i = 0; i < smmu->soc->num_swgroups; i++) {
319 if (smmu->soc->swgroups[i].swgroup == swgroup) {
320 group = &smmu->soc->swgroups[i];
321 break;
325 return group;
328 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
329 unsigned int asid)
331 const struct tegra_smmu_swgroup *group;
332 unsigned int i;
333 u32 value;
335 for (i = 0; i < smmu->soc->num_clients; i++) {
336 const struct tegra_mc_client *client = &smmu->soc->clients[i];
338 if (client->swgroup != swgroup)
339 continue;
341 value = smmu_readl(smmu, client->smmu.reg);
342 value |= BIT(client->smmu.bit);
343 smmu_writel(smmu, value, client->smmu.reg);
346 group = tegra_smmu_find_swgroup(smmu, swgroup);
347 if (group) {
348 value = smmu_readl(smmu, group->reg);
349 value &= ~SMMU_ASID_MASK;
350 value |= SMMU_ASID_VALUE(asid);
351 value |= SMMU_ASID_ENABLE;
352 smmu_writel(smmu, value, group->reg);
356 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
357 unsigned int asid)
359 const struct tegra_smmu_swgroup *group;
360 unsigned int i;
361 u32 value;
363 group = tegra_smmu_find_swgroup(smmu, swgroup);
364 if (group) {
365 value = smmu_readl(smmu, group->reg);
366 value &= ~SMMU_ASID_MASK;
367 value |= SMMU_ASID_VALUE(asid);
368 value &= ~SMMU_ASID_ENABLE;
369 smmu_writel(smmu, value, group->reg);
372 for (i = 0; i < smmu->soc->num_clients; i++) {
373 const struct tegra_mc_client *client = &smmu->soc->clients[i];
375 if (client->swgroup != swgroup)
376 continue;
378 value = smmu_readl(smmu, client->smmu.reg);
379 value &= ~BIT(client->smmu.bit);
380 smmu_writel(smmu, value, client->smmu.reg);
384 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
385 struct tegra_smmu_as *as)
387 u32 value;
388 int err;
390 if (as->use_count > 0) {
391 as->use_count++;
392 return 0;
395 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD,
396 DMA_TO_DEVICE);
397 if (dma_mapping_error(smmu->dev, as->pd_dma))
398 return -ENOMEM;
400 /* We can't handle 64-bit DMA addresses */
401 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
402 err = -ENOMEM;
403 goto err_unmap;
406 err = tegra_smmu_alloc_asid(smmu, &as->id);
407 if (err < 0)
408 goto err_unmap;
410 smmu_flush_ptc(smmu, as->pd_dma, 0);
411 smmu_flush_tlb_asid(smmu, as->id);
413 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
414 value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
415 smmu_writel(smmu, value, SMMU_PTB_DATA);
416 smmu_flush(smmu);
418 as->smmu = smmu;
419 as->use_count++;
421 return 0;
423 err_unmap:
424 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
425 return err;
428 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
429 struct tegra_smmu_as *as)
431 if (--as->use_count > 0)
432 return;
434 tegra_smmu_free_asid(smmu, as->id);
436 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
438 as->smmu = NULL;
441 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
442 struct device *dev)
444 struct tegra_smmu *smmu = dev->archdata.iommu;
445 struct tegra_smmu_as *as = to_smmu_as(domain);
446 struct device_node *np = dev->of_node;
447 struct of_phandle_args args;
448 unsigned int index = 0;
449 int err = 0;
451 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
452 &args)) {
453 unsigned int swgroup = args.args[0];
455 if (args.np != smmu->dev->of_node) {
456 of_node_put(args.np);
457 continue;
460 of_node_put(args.np);
462 err = tegra_smmu_as_prepare(smmu, as);
463 if (err < 0)
464 return err;
466 tegra_smmu_enable(smmu, swgroup, as->id);
467 index++;
470 if (index == 0)
471 return -ENODEV;
473 return 0;
476 static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
478 struct tegra_smmu_as *as = to_smmu_as(domain);
479 struct device_node *np = dev->of_node;
480 struct tegra_smmu *smmu = as->smmu;
481 struct of_phandle_args args;
482 unsigned int index = 0;
484 while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
485 &args)) {
486 unsigned int swgroup = args.args[0];
488 if (args.np != smmu->dev->of_node) {
489 of_node_put(args.np);
490 continue;
493 of_node_put(args.np);
495 tegra_smmu_disable(smmu, swgroup, as->id);
496 tegra_smmu_as_unprepare(smmu, as);
497 index++;
501 static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
502 u32 value)
504 unsigned int pd_index = iova_pd_index(iova);
505 struct tegra_smmu *smmu = as->smmu;
506 u32 *pd = page_address(as->pd);
507 unsigned long offset = pd_index * sizeof(*pd);
509 /* Set the page directory entry first */
510 pd[pd_index] = value;
512 /* The flush the page directory entry from caches */
513 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
514 sizeof(*pd), DMA_TO_DEVICE);
516 /* And flush the iommu */
517 smmu_flush_ptc(smmu, as->pd_dma, offset);
518 smmu_flush_tlb_section(smmu, as->id, iova);
519 smmu_flush(smmu);
522 static u32 *tegra_smmu_pte_offset(struct page *pt_page, unsigned long iova)
524 u32 *pt = page_address(pt_page);
526 return pt + iova_pt_index(iova);
529 static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
530 dma_addr_t *dmap)
532 unsigned int pd_index = iova_pd_index(iova);
533 struct page *pt_page;
534 u32 *pd;
536 pt_page = as->pts[pd_index];
537 if (!pt_page)
538 return NULL;
540 pd = page_address(as->pd);
541 *dmap = smmu_pde_to_dma(pd[pd_index]);
543 return tegra_smmu_pte_offset(pt_page, iova);
546 static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
547 dma_addr_t *dmap)
549 unsigned int pde = iova_pd_index(iova);
550 struct tegra_smmu *smmu = as->smmu;
552 if (!as->pts[pde]) {
553 struct page *page;
554 dma_addr_t dma;
556 page = alloc_page(GFP_KERNEL | __GFP_DMA | __GFP_ZERO);
557 if (!page)
558 return NULL;
560 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT,
561 DMA_TO_DEVICE);
562 if (dma_mapping_error(smmu->dev, dma)) {
563 __free_page(page);
564 return NULL;
567 if (!smmu_dma_addr_valid(smmu, dma)) {
568 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT,
569 DMA_TO_DEVICE);
570 __free_page(page);
571 return NULL;
574 as->pts[pde] = page;
576 tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
577 SMMU_PDE_NEXT));
579 *dmap = dma;
580 } else {
581 u32 *pd = page_address(as->pd);
583 *dmap = smmu_pde_to_dma(pd[pde]);
586 return tegra_smmu_pte_offset(as->pts[pde], iova);
589 static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
591 unsigned int pd_index = iova_pd_index(iova);
593 as->count[pd_index]++;
596 static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
598 unsigned int pde = iova_pd_index(iova);
599 struct page *page = as->pts[pde];
602 * When no entries in this page table are used anymore, return the
603 * memory page to the system.
605 if (--as->count[pde] == 0) {
606 struct tegra_smmu *smmu = as->smmu;
607 u32 *pd = page_address(as->pd);
608 dma_addr_t pte_dma = smmu_pde_to_dma(pd[pde]);
610 tegra_smmu_set_pde(as, iova, 0);
612 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE);
613 __free_page(page);
614 as->pts[pde] = NULL;
618 static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
619 u32 *pte, dma_addr_t pte_dma, u32 val)
621 struct tegra_smmu *smmu = as->smmu;
622 unsigned long offset = offset_in_page(pte);
624 *pte = val;
626 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
627 4, DMA_TO_DEVICE);
628 smmu_flush_ptc(smmu, pte_dma, offset);
629 smmu_flush_tlb_group(smmu, as->id, iova);
630 smmu_flush(smmu);
633 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
634 phys_addr_t paddr, size_t size, int prot)
636 struct tegra_smmu_as *as = to_smmu_as(domain);
637 dma_addr_t pte_dma;
638 u32 *pte;
640 pte = as_get_pte(as, iova, &pte_dma);
641 if (!pte)
642 return -ENOMEM;
644 /* If we aren't overwriting a pre-existing entry, increment use */
645 if (*pte == 0)
646 tegra_smmu_pte_get_use(as, iova);
648 tegra_smmu_set_pte(as, iova, pte, pte_dma,
649 __phys_to_pfn(paddr) | SMMU_PTE_ATTR);
651 return 0;
654 static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
655 size_t size)
657 struct tegra_smmu_as *as = to_smmu_as(domain);
658 dma_addr_t pte_dma;
659 u32 *pte;
661 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
662 if (!pte || !*pte)
663 return 0;
665 tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
666 tegra_smmu_pte_put_use(as, iova);
668 return size;
671 static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
672 dma_addr_t iova)
674 struct tegra_smmu_as *as = to_smmu_as(domain);
675 unsigned long pfn;
676 dma_addr_t pte_dma;
677 u32 *pte;
679 pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
680 if (!pte || !*pte)
681 return 0;
683 pfn = *pte & as->smmu->pfn_mask;
685 return PFN_PHYS(pfn);
688 static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
690 struct platform_device *pdev;
691 struct tegra_mc *mc;
693 pdev = of_find_device_by_node(np);
694 if (!pdev)
695 return NULL;
697 mc = platform_get_drvdata(pdev);
698 if (!mc)
699 return NULL;
701 return mc->smmu;
704 static int tegra_smmu_add_device(struct device *dev)
706 struct device_node *np = dev->of_node;
707 struct of_phandle_args args;
708 unsigned int index = 0;
710 while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
711 &args) == 0) {
712 struct tegra_smmu *smmu;
714 smmu = tegra_smmu_find(args.np);
715 if (smmu) {
717 * Only a single IOMMU master interface is currently
718 * supported by the Linux kernel, so abort after the
719 * first match.
721 dev->archdata.iommu = smmu;
722 break;
725 index++;
728 return 0;
731 static void tegra_smmu_remove_device(struct device *dev)
733 dev->archdata.iommu = NULL;
736 static const struct iommu_ops tegra_smmu_ops = {
737 .capable = tegra_smmu_capable,
738 .domain_alloc = tegra_smmu_domain_alloc,
739 .domain_free = tegra_smmu_domain_free,
740 .attach_dev = tegra_smmu_attach_dev,
741 .detach_dev = tegra_smmu_detach_dev,
742 .add_device = tegra_smmu_add_device,
743 .remove_device = tegra_smmu_remove_device,
744 .map = tegra_smmu_map,
745 .unmap = tegra_smmu_unmap,
746 .map_sg = default_iommu_map_sg,
747 .iova_to_phys = tegra_smmu_iova_to_phys,
749 .pgsize_bitmap = SZ_4K,
752 static void tegra_smmu_ahb_enable(void)
754 static const struct of_device_id ahb_match[] = {
755 { .compatible = "nvidia,tegra30-ahb", },
758 struct device_node *ahb;
760 ahb = of_find_matching_node(NULL, ahb_match);
761 if (ahb) {
762 tegra_ahb_enable_smmu(ahb);
763 of_node_put(ahb);
767 static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
769 struct tegra_smmu *smmu = s->private;
770 unsigned int i;
771 u32 value;
773 seq_printf(s, "swgroup enabled ASID\n");
774 seq_printf(s, "------------------------\n");
776 for (i = 0; i < smmu->soc->num_swgroups; i++) {
777 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
778 const char *status;
779 unsigned int asid;
781 value = smmu_readl(smmu, group->reg);
783 if (value & SMMU_ASID_ENABLE)
784 status = "yes";
785 else
786 status = "no";
788 asid = value & SMMU_ASID_MASK;
790 seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
791 asid);
794 return 0;
797 static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
799 return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
802 static const struct file_operations tegra_smmu_swgroups_fops = {
803 .open = tegra_smmu_swgroups_open,
804 .read = seq_read,
805 .llseek = seq_lseek,
806 .release = single_release,
809 static int tegra_smmu_clients_show(struct seq_file *s, void *data)
811 struct tegra_smmu *smmu = s->private;
812 unsigned int i;
813 u32 value;
815 seq_printf(s, "client enabled\n");
816 seq_printf(s, "--------------------\n");
818 for (i = 0; i < smmu->soc->num_clients; i++) {
819 const struct tegra_mc_client *client = &smmu->soc->clients[i];
820 const char *status;
822 value = smmu_readl(smmu, client->smmu.reg);
824 if (value & BIT(client->smmu.bit))
825 status = "yes";
826 else
827 status = "no";
829 seq_printf(s, "%-12s %s\n", client->name, status);
832 return 0;
835 static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
837 return single_open(file, tegra_smmu_clients_show, inode->i_private);
840 static const struct file_operations tegra_smmu_clients_fops = {
841 .open = tegra_smmu_clients_open,
842 .read = seq_read,
843 .llseek = seq_lseek,
844 .release = single_release,
847 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
849 smmu->debugfs = debugfs_create_dir("smmu", NULL);
850 if (!smmu->debugfs)
851 return;
853 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
854 &tegra_smmu_swgroups_fops);
855 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
856 &tegra_smmu_clients_fops);
859 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
861 debugfs_remove_recursive(smmu->debugfs);
864 struct tegra_smmu *tegra_smmu_probe(struct device *dev,
865 const struct tegra_smmu_soc *soc,
866 struct tegra_mc *mc)
868 struct tegra_smmu *smmu;
869 size_t size;
870 u32 value;
871 int err;
873 /* This can happen on Tegra20 which doesn't have an SMMU */
874 if (!soc)
875 return NULL;
877 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
878 if (!smmu)
879 return ERR_PTR(-ENOMEM);
882 * This is a bit of a hack. Ideally we'd want to simply return this
883 * value. However the IOMMU registration process will attempt to add
884 * all devices to the IOMMU when bus_set_iommu() is called. In order
885 * not to rely on global variables to track the IOMMU instance, we
886 * set it here so that it can be looked up from the .add_device()
887 * callback via the IOMMU device's .drvdata field.
889 mc->smmu = smmu;
891 size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
893 smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
894 if (!smmu->asids)
895 return ERR_PTR(-ENOMEM);
897 mutex_init(&smmu->lock);
899 smmu->regs = mc->regs;
900 smmu->soc = soc;
901 smmu->dev = dev;
902 smmu->mc = mc;
904 smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
905 dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
906 mc->soc->num_address_bits, smmu->pfn_mask);
907 smmu->tlb_mask = (smmu->soc->num_tlb_lines << 1) - 1;
908 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
909 smmu->tlb_mask);
911 value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
913 if (soc->supports_request_limit)
914 value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
916 smmu_writel(smmu, value, SMMU_PTC_CONFIG);
918 value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
919 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
921 if (soc->supports_round_robin_arbitration)
922 value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
924 smmu_writel(smmu, value, SMMU_TLB_CONFIG);
926 smmu_flush_ptc_all(smmu);
927 smmu_flush_tlb(smmu);
928 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
929 smmu_flush(smmu);
931 tegra_smmu_ahb_enable();
933 err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
934 if (err < 0)
935 return ERR_PTR(err);
937 if (IS_ENABLED(CONFIG_DEBUG_FS))
938 tegra_smmu_debugfs_init(smmu);
940 return smmu;
943 void tegra_smmu_remove(struct tegra_smmu *smmu)
945 if (IS_ENABLED(CONFIG_DEBUG_FS))
946 tegra_smmu_debugfs_exit(smmu);