aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iommu/arm-smmu.c
diff options
context:
space:
mode:
authorJoerg Roedel <jroedel@suse.de>2016-05-09 06:03:37 -0400
committerJoerg Roedel <jroedel@suse.de>2016-05-09 06:03:37 -0400
commit8801561ce029ef2bf6927c53b834d711830bc8be (patch)
treef0c730e94e96a3a1f4ae102f311f3f0fd1ec8011 /drivers/iommu/arm-smmu.c
parent44549e8f5eea4e0a41b487b63e616cb089922b99 (diff)
parent3ca3712a42f9e632eb41da94ca4eab4f1fb06fcb (diff)
Merge branch 'for-joerg/arm-smmu/updates' of git://git.kernel.org/pub/scm/linux/kernel/git/will/linux into arm/smmu
Diffstat (limited to 'drivers/iommu/arm-smmu.c')
-rw-r--r--drivers/iommu/arm-smmu.c291
1 files changed, 210 insertions, 81 deletions
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 7c39ac4b9c53..7cd4ad98904a 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -34,6 +34,7 @@
34#include <linux/err.h> 34#include <linux/err.h>
35#include <linux/interrupt.h> 35#include <linux/interrupt.h>
36#include <linux/io.h> 36#include <linux/io.h>
37#include <linux/io-64-nonatomic-hi-lo.h>
37#include <linux/iommu.h> 38#include <linux/iommu.h>
38#include <linux/iopoll.h> 39#include <linux/iopoll.h>
39#include <linux/module.h> 40#include <linux/module.h>
@@ -71,16 +72,15 @@
71 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \ 72 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
72 ? 0x400 : 0)) 73 ? 0x400 : 0))
73 74
75/*
76 * Some 64-bit registers only make sense to write atomically, but in such
77 * cases all the data relevant to AArch32 formats lies within the lower word,
78 * therefore this actually makes more sense than it might first appear.
79 */
74#ifdef CONFIG_64BIT 80#ifdef CONFIG_64BIT
75#define smmu_writeq writeq_relaxed 81#define smmu_write_atomic_lq writeq_relaxed
76#else 82#else
77#define smmu_writeq(reg64, addr) \ 83#define smmu_write_atomic_lq writel_relaxed
78 do { \
79 u64 __val = (reg64); \
80 void __iomem *__addr = (addr); \
81 writel_relaxed(__val >> 32, __addr + 4); \
82 writel_relaxed(__val, __addr); \
83 } while (0)
84#endif 84#endif
85 85
86/* Configuration registers */ 86/* Configuration registers */
@@ -94,9 +94,13 @@
94#define sCR0_VMIDPNE (1 << 11) 94#define sCR0_VMIDPNE (1 << 11)
95#define sCR0_PTM (1 << 12) 95#define sCR0_PTM (1 << 12)
96#define sCR0_FB (1 << 13) 96#define sCR0_FB (1 << 13)
97#define sCR0_VMID16EN (1 << 31)
97#define sCR0_BSU_SHIFT 14 98#define sCR0_BSU_SHIFT 14
98#define sCR0_BSU_MASK 0x3 99#define sCR0_BSU_MASK 0x3
99 100
101/* Auxiliary Configuration register */
102#define ARM_SMMU_GR0_sACR 0x10
103
100/* Identification registers */ 104/* Identification registers */
101#define ARM_SMMU_GR0_ID0 0x20 105#define ARM_SMMU_GR0_ID0 0x20
102#define ARM_SMMU_GR0_ID1 0x24 106#define ARM_SMMU_GR0_ID1 0x24
@@ -116,6 +120,8 @@
116#define ID0_NTS (1 << 28) 120#define ID0_NTS (1 << 28)
117#define ID0_SMS (1 << 27) 121#define ID0_SMS (1 << 27)
118#define ID0_ATOSNS (1 << 26) 122#define ID0_ATOSNS (1 << 26)
123#define ID0_PTFS_NO_AARCH32 (1 << 25)
124#define ID0_PTFS_NO_AARCH32S (1 << 24)
119#define ID0_CTTW (1 << 14) 125#define ID0_CTTW (1 << 14)
120#define ID0_NUMIRPT_SHIFT 16 126#define ID0_NUMIRPT_SHIFT 16
121#define ID0_NUMIRPT_MASK 0xff 127#define ID0_NUMIRPT_MASK 0xff
@@ -141,6 +147,10 @@
141#define ID2_PTFS_4K (1 << 12) 147#define ID2_PTFS_4K (1 << 12)
142#define ID2_PTFS_16K (1 << 13) 148#define ID2_PTFS_16K (1 << 13)
143#define ID2_PTFS_64K (1 << 14) 149#define ID2_PTFS_64K (1 << 14)
150#define ID2_VMID16 (1 << 15)
151
152#define ID7_MAJOR_SHIFT 4
153#define ID7_MAJOR_MASK 0xf
144 154
145/* Global TLB invalidation */ 155/* Global TLB invalidation */
146#define ARM_SMMU_GR0_TLBIVMID 0x64 156#define ARM_SMMU_GR0_TLBIVMID 0x64
@@ -193,12 +203,15 @@
193#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2)) 203#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
194#define CBA2R_RW64_32BIT (0 << 0) 204#define CBA2R_RW64_32BIT (0 << 0)
195#define CBA2R_RW64_64BIT (1 << 0) 205#define CBA2R_RW64_64BIT (1 << 0)
206#define CBA2R_VMID_SHIFT 16
207#define CBA2R_VMID_MASK 0xffff
196 208
197/* Translation context bank */ 209/* Translation context bank */
198#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1)) 210#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
199#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift)) 211#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
200 212
201#define ARM_SMMU_CB_SCTLR 0x0 213#define ARM_SMMU_CB_SCTLR 0x0
214#define ARM_SMMU_CB_ACTLR 0x4
202#define ARM_SMMU_CB_RESUME 0x8 215#define ARM_SMMU_CB_RESUME 0x8
203#define ARM_SMMU_CB_TTBCR2 0x10 216#define ARM_SMMU_CB_TTBCR2 0x10
204#define ARM_SMMU_CB_TTBR0 0x20 217#define ARM_SMMU_CB_TTBR0 0x20
@@ -206,11 +219,9 @@
206#define ARM_SMMU_CB_TTBCR 0x30 219#define ARM_SMMU_CB_TTBCR 0x30
207#define ARM_SMMU_CB_S1_MAIR0 0x38 220#define ARM_SMMU_CB_S1_MAIR0 0x38
208#define ARM_SMMU_CB_S1_MAIR1 0x3c 221#define ARM_SMMU_CB_S1_MAIR1 0x3c
209#define ARM_SMMU_CB_PAR_LO 0x50 222#define ARM_SMMU_CB_PAR 0x50
210#define ARM_SMMU_CB_PAR_HI 0x54
211#define ARM_SMMU_CB_FSR 0x58 223#define ARM_SMMU_CB_FSR 0x58
212#define ARM_SMMU_CB_FAR_LO 0x60 224#define ARM_SMMU_CB_FAR 0x60
213#define ARM_SMMU_CB_FAR_HI 0x64
214#define ARM_SMMU_CB_FSYNR0 0x68 225#define ARM_SMMU_CB_FSYNR0 0x68
215#define ARM_SMMU_CB_S1_TLBIVA 0x600 226#define ARM_SMMU_CB_S1_TLBIVA 0x600
216#define ARM_SMMU_CB_S1_TLBIASID 0x610 227#define ARM_SMMU_CB_S1_TLBIASID 0x610
@@ -230,6 +241,10 @@
230#define SCTLR_M (1 << 0) 241#define SCTLR_M (1 << 0)
231#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE) 242#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
232 243
244#define ARM_MMU500_ACTLR_CPRE (1 << 1)
245
246#define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
247
233#define CB_PAR_F (1 << 0) 248#define CB_PAR_F (1 << 0)
234 249
235#define ATSR_ACTIVE (1 << 0) 250#define ATSR_ACTIVE (1 << 0)
@@ -270,10 +285,17 @@ MODULE_PARM_DESC(disable_bypass,
270 "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU."); 285 "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
271 286
272enum arm_smmu_arch_version { 287enum arm_smmu_arch_version {
273 ARM_SMMU_V1 = 1, 288 ARM_SMMU_V1,
289 ARM_SMMU_V1_64K,
274 ARM_SMMU_V2, 290 ARM_SMMU_V2,
275}; 291};
276 292
293enum arm_smmu_implementation {
294 GENERIC_SMMU,
295 ARM_MMU500,
296 CAVIUM_SMMUV2,
297};
298
277struct arm_smmu_smr { 299struct arm_smmu_smr {
278 u8 idx; 300 u8 idx;
279 u16 mask; 301 u16 mask;
@@ -305,11 +327,18 @@ struct arm_smmu_device {
305#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3) 327#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
306#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4) 328#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
307#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5) 329#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
330#define ARM_SMMU_FEAT_VMID16 (1 << 6)
331#define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
332#define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
333#define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
334#define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
335#define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
308 u32 features; 336 u32 features;
309 337
310#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) 338#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
311 u32 options; 339 u32 options;
312 enum arm_smmu_arch_version version; 340 enum arm_smmu_arch_version version;
341 enum arm_smmu_implementation model;
313 342
314 u32 num_context_banks; 343 u32 num_context_banks;
315 u32 num_s2_context_banks; 344 u32 num_s2_context_banks;
@@ -329,17 +358,27 @@ struct arm_smmu_device {
329 358
330 struct list_head list; 359 struct list_head list;
331 struct rb_root masters; 360 struct rb_root masters;
361
362 u32 cavium_id_base; /* Specific to Cavium */
363};
364
365enum arm_smmu_context_fmt {
366 ARM_SMMU_CTX_FMT_NONE,
367 ARM_SMMU_CTX_FMT_AARCH64,
368 ARM_SMMU_CTX_FMT_AARCH32_L,
369 ARM_SMMU_CTX_FMT_AARCH32_S,
332}; 370};
333 371
334struct arm_smmu_cfg { 372struct arm_smmu_cfg {
335 u8 cbndx; 373 u8 cbndx;
336 u8 irptndx; 374 u8 irptndx;
337 u32 cbar; 375 u32 cbar;
376 enum arm_smmu_context_fmt fmt;
338}; 377};
339#define INVALID_IRPTNDX 0xff 378#define INVALID_IRPTNDX 0xff
340 379
341#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx) 380#define ARM_SMMU_CB_ASID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx)
342#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1) 381#define ARM_SMMU_CB_VMID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx + 1)
343 382
344enum arm_smmu_domain_stage { 383enum arm_smmu_domain_stage {
345 ARM_SMMU_DOMAIN_S1 = 0, 384 ARM_SMMU_DOMAIN_S1 = 0,
@@ -367,6 +406,8 @@ struct arm_smmu_option_prop {
367 const char *prop; 406 const char *prop;
368}; 407};
369 408
409static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
410
370static struct arm_smmu_option_prop arm_smmu_options[] = { 411static struct arm_smmu_option_prop arm_smmu_options[] = {
371 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" }, 412 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
372 { 0, NULL}, 413 { 0, NULL},
@@ -578,11 +619,11 @@ static void arm_smmu_tlb_inv_context(void *cookie)
578 619
579 if (stage1) { 620 if (stage1) {
580 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 621 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
581 writel_relaxed(ARM_SMMU_CB_ASID(cfg), 622 writel_relaxed(ARM_SMMU_CB_ASID(smmu, cfg),
582 base + ARM_SMMU_CB_S1_TLBIASID); 623 base + ARM_SMMU_CB_S1_TLBIASID);
583 } else { 624 } else {
584 base = ARM_SMMU_GR0(smmu); 625 base = ARM_SMMU_GR0(smmu);
585 writel_relaxed(ARM_SMMU_CB_VMID(cfg), 626 writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg),
586 base + ARM_SMMU_GR0_TLBIVMID); 627 base + ARM_SMMU_GR0_TLBIVMID);
587 } 628 }
588 629
@@ -602,37 +643,33 @@ static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
602 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 643 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
603 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA; 644 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
604 645
605 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) { 646 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
606 iova &= ~12UL; 647 iova &= ~12UL;
607 iova |= ARM_SMMU_CB_ASID(cfg); 648 iova |= ARM_SMMU_CB_ASID(smmu, cfg);
608 do { 649 do {
609 writel_relaxed(iova, reg); 650 writel_relaxed(iova, reg);
610 iova += granule; 651 iova += granule;
611 } while (size -= granule); 652 } while (size -= granule);
612#ifdef CONFIG_64BIT
613 } else { 653 } else {
614 iova >>= 12; 654 iova >>= 12;
615 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48; 655 iova |= (u64)ARM_SMMU_CB_ASID(smmu, cfg) << 48;
616 do { 656 do {
617 writeq_relaxed(iova, reg); 657 writeq_relaxed(iova, reg);
618 iova += granule >> 12; 658 iova += granule >> 12;
619 } while (size -= granule); 659 } while (size -= granule);
620#endif
621 } 660 }
622#ifdef CONFIG_64BIT
623 } else if (smmu->version == ARM_SMMU_V2) { 661 } else if (smmu->version == ARM_SMMU_V2) {
624 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 662 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
625 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L : 663 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
626 ARM_SMMU_CB_S2_TLBIIPAS2; 664 ARM_SMMU_CB_S2_TLBIIPAS2;
627 iova >>= 12; 665 iova >>= 12;
628 do { 666 do {
629 writeq_relaxed(iova, reg); 667 smmu_write_atomic_lq(iova, reg);
630 iova += granule >> 12; 668 iova += granule >> 12;
631 } while (size -= granule); 669 } while (size -= granule);
632#endif
633 } else { 670 } else {
634 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID; 671 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
635 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg); 672 writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), reg);
636 } 673 }
637} 674}
638 675
@@ -645,7 +682,7 @@ static struct iommu_gather_ops arm_smmu_gather_ops = {
645static irqreturn_t arm_smmu_context_fault(int irq, void *dev) 682static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
646{ 683{
647 int flags, ret; 684 int flags, ret;
648 u32 fsr, far, fsynr, resume; 685 u32 fsr, fsynr, resume;
649 unsigned long iova; 686 unsigned long iova;
650 struct iommu_domain *domain = dev; 687 struct iommu_domain *domain = dev;
651 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 688 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
@@ -667,13 +704,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
667 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0); 704 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
668 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ; 705 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
669 706
670 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO); 707 iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR);
671 iova = far;
672#ifdef CONFIG_64BIT
673 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
674 iova |= ((unsigned long)far << 32);
675#endif
676
677 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) { 708 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
678 ret = IRQ_HANDLED; 709 ret = IRQ_HANDLED;
679 resume = RESUME_RETRY; 710 resume = RESUME_RETRY;
@@ -734,22 +765,20 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
734 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 765 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
735 766
736 if (smmu->version > ARM_SMMU_V1) { 767 if (smmu->version > ARM_SMMU_V1) {
737 /* 768 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
738 * CBA2R. 769 reg = CBA2R_RW64_64BIT;
739 * *Must* be initialised before CBAR thanks to VMID16 770 else
740 * architectural oversight affected some implementations. 771 reg = CBA2R_RW64_32BIT;
741 */ 772 /* 16-bit VMIDs live in CBA2R */
742#ifdef CONFIG_64BIT 773 if (smmu->features & ARM_SMMU_FEAT_VMID16)
743 reg = CBA2R_RW64_64BIT; 774 reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBA2R_VMID_SHIFT;
744#else 775
745 reg = CBA2R_RW64_32BIT;
746#endif
747 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); 776 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
748 } 777 }
749 778
750 /* CBAR */ 779 /* CBAR */
751 reg = cfg->cbar; 780 reg = cfg->cbar;
752 if (smmu->version == ARM_SMMU_V1) 781 if (smmu->version < ARM_SMMU_V2)
753 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT; 782 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
754 783
755 /* 784 /*
@@ -759,8 +788,9 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
759 if (stage1) { 788 if (stage1) {
760 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) | 789 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
761 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT); 790 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
762 } else { 791 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
763 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT; 792 /* 8-bit VMIDs live in CBAR */
793 reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBAR_VMID_SHIFT;
764 } 794 }
765 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx)); 795 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
766 796
@@ -768,15 +798,15 @@ static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
768 if (stage1) { 798 if (stage1) {
769 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0]; 799 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
770 800
771 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT; 801 reg64 |= ((u64)ARM_SMMU_CB_ASID(smmu, cfg)) << TTBRn_ASID_SHIFT;
772 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0); 802 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
773 803
774 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1]; 804 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
775 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT; 805 reg64 |= ((u64)ARM_SMMU_CB_ASID(smmu, cfg)) << TTBRn_ASID_SHIFT;
776 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR1); 806 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR1);
777 } else { 807 } else {
778 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; 808 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
779 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0); 809 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
780 } 810 }
781 811
782 /* TTBCR */ 812 /* TTBCR */
@@ -855,16 +885,40 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,
855 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2)) 885 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
856 smmu_domain->stage = ARM_SMMU_DOMAIN_S1; 886 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
857 887
888 /*
889 * Choosing a suitable context format is even more fiddly. Until we
890 * grow some way for the caller to express a preference, and/or move
891 * the decision into the io-pgtable code where it arguably belongs,
892 * just aim for the closest thing to the rest of the system, and hope
893 * that the hardware isn't esoteric enough that we can't assume AArch64
894 * support to be a superset of AArch32 support...
895 */
896 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
897 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
898 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
899 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
900 ARM_SMMU_FEAT_FMT_AARCH64_16K |
901 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
902 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
903
904 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
905 ret = -EINVAL;
906 goto out_unlock;
907 }
908
858 switch (smmu_domain->stage) { 909 switch (smmu_domain->stage) {
859 case ARM_SMMU_DOMAIN_S1: 910 case ARM_SMMU_DOMAIN_S1:
860 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS; 911 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
861 start = smmu->num_s2_context_banks; 912 start = smmu->num_s2_context_banks;
862 ias = smmu->va_size; 913 ias = smmu->va_size;
863 oas = smmu->ipa_size; 914 oas = smmu->ipa_size;
864 if (IS_ENABLED(CONFIG_64BIT)) 915 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
865 fmt = ARM_64_LPAE_S1; 916 fmt = ARM_64_LPAE_S1;
866 else 917 } else {
867 fmt = ARM_32_LPAE_S1; 918 fmt = ARM_32_LPAE_S1;
919 ias = min(ias, 32UL);
920 oas = min(oas, 40UL);
921 }
868 break; 922 break;
869 case ARM_SMMU_DOMAIN_NESTED: 923 case ARM_SMMU_DOMAIN_NESTED:
870 /* 924 /*
@@ -876,10 +930,13 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,
876 start = 0; 930 start = 0;
877 ias = smmu->ipa_size; 931 ias = smmu->ipa_size;
878 oas = smmu->pa_size; 932 oas = smmu->pa_size;
879 if (IS_ENABLED(CONFIG_64BIT)) 933 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
880 fmt = ARM_64_LPAE_S2; 934 fmt = ARM_64_LPAE_S2;
881 else 935 } else {
882 fmt = ARM_32_LPAE_S2; 936 fmt = ARM_32_LPAE_S2;
937 ias = min(ias, 40UL);
938 oas = min(oas, 40UL);
939 }
883 break; 940 break;
884 default: 941 default:
885 ret = -EINVAL; 942 ret = -EINVAL;
@@ -892,7 +949,7 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,
892 goto out_unlock; 949 goto out_unlock;
893 950
894 cfg->cbndx = ret; 951 cfg->cbndx = ret;
895 if (smmu->version == ARM_SMMU_V1) { 952 if (smmu->version < ARM_SMMU_V2) {
896 cfg->irptndx = atomic_inc_return(&smmu->irptndx); 953 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
897 cfg->irptndx %= smmu->num_context_irqs; 954 cfg->irptndx %= smmu->num_context_irqs;
898 } else { 955 } else {
@@ -1252,8 +1309,8 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1252 /* ATS1 registers can only be written atomically */ 1309 /* ATS1 registers can only be written atomically */
1253 va = iova & ~0xfffUL; 1310 va = iova & ~0xfffUL;
1254 if (smmu->version == ARM_SMMU_V2) 1311 if (smmu->version == ARM_SMMU_V2)
1255 smmu_writeq(va, cb_base + ARM_SMMU_CB_ATS1PR); 1312 smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR);
1256 else 1313 else /* Register is only 32-bit in v1 */
1257 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR); 1314 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
1258 1315
1259 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp, 1316 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
@@ -1264,9 +1321,7 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1264 return ops->iova_to_phys(ops, iova); 1321 return ops->iova_to_phys(ops, iova);
1265 } 1322 }
1266 1323
1267 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO); 1324 phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR);
1268 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1269
1270 if (phys & CB_PAR_F) { 1325 if (phys & CB_PAR_F) {
1271 dev_err(dev, "translation fault!\n"); 1326 dev_err(dev, "translation fault!\n");
1272 dev_err(dev, "PAR = 0x%llx\n", phys); 1327 dev_err(dev, "PAR = 0x%llx\n", phys);
@@ -1492,7 +1547,7 @@ static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1492 void __iomem *gr0_base = ARM_SMMU_GR0(smmu); 1547 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1493 void __iomem *cb_base; 1548 void __iomem *cb_base;
1494 int i = 0; 1549 int i = 0;
1495 u32 reg; 1550 u32 reg, major;
1496 1551
1497 /* clear global FSR */ 1552 /* clear global FSR */
1498 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR); 1553 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
@@ -1505,11 +1560,33 @@ static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1505 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(i)); 1560 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(i));
1506 } 1561 }
1507 1562
1563 /*
1564 * Before clearing ARM_MMU500_ACTLR_CPRE, need to
1565 * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
1566 * bit is only present in MMU-500r2 onwards.
1567 */
1568 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7);
1569 major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK;
1570 if ((smmu->model == ARM_MMU500) && (major >= 2)) {
1571 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR);
1572 reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
1573 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR);
1574 }
1575
1508 /* Make sure all context banks are disabled and clear CB_FSR */ 1576 /* Make sure all context banks are disabled and clear CB_FSR */
1509 for (i = 0; i < smmu->num_context_banks; ++i) { 1577 for (i = 0; i < smmu->num_context_banks; ++i) {
1510 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i); 1578 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1511 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); 1579 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1512 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR); 1580 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1581 /*
1582 * Disable MMU-500's not-particularly-beneficial next-page
1583 * prefetcher for the sake of errata #841119 and #826419.
1584 */
1585 if (smmu->model == ARM_MMU500) {
1586 reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR);
1587 reg &= ~ARM_MMU500_ACTLR_CPRE;
1588 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR);
1589 }
1513 } 1590 }
1514 1591
1515 /* Invalidate the TLB, just in case */ 1592 /* Invalidate the TLB, just in case */
@@ -1537,6 +1614,9 @@ static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1537 /* Don't upgrade barriers */ 1614 /* Don't upgrade barriers */
1538 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT); 1615 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
1539 1616
1617 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1618 reg |= sCR0_VMID16EN;
1619
1540 /* Push the button */ 1620 /* Push the button */
1541 __arm_smmu_tlb_sync(smmu); 1621 __arm_smmu_tlb_sync(smmu);
1542 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0); 1622 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
@@ -1569,7 +1649,8 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1569 bool cttw_dt, cttw_reg; 1649 bool cttw_dt, cttw_reg;
1570 1650
1571 dev_notice(smmu->dev, "probing hardware configuration...\n"); 1651 dev_notice(smmu->dev, "probing hardware configuration...\n");
1572 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version); 1652 dev_notice(smmu->dev, "SMMUv%d with:\n",
1653 smmu->version == ARM_SMMU_V2 ? 2 : 1);
1573 1654
1574 /* ID0 */ 1655 /* ID0 */
1575 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0); 1656 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
@@ -1601,7 +1682,8 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1601 return -ENODEV; 1682 return -ENODEV;
1602 } 1683 }
1603 1684
1604 if ((id & ID0_S1TS) && ((smmu->version == 1) || !(id & ID0_ATOSNS))) { 1685 if ((id & ID0_S1TS) &&
1686 ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
1605 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS; 1687 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1606 dev_notice(smmu->dev, "\taddress translation ops\n"); 1688 dev_notice(smmu->dev, "\taddress translation ops\n");
1607 } 1689 }
@@ -1657,6 +1739,12 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1657 ID0_NUMSIDB_MASK; 1739 ID0_NUMSIDB_MASK;
1658 } 1740 }
1659 1741
1742 if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
1743 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1744 if (!(id & ID0_PTFS_NO_AARCH32S))
1745 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1746 }
1747
1660 /* ID1 */ 1748 /* ID1 */
1661 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1); 1749 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1662 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12; 1750 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
@@ -1677,6 +1765,17 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1677 } 1765 }
1678 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n", 1766 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1679 smmu->num_context_banks, smmu->num_s2_context_banks); 1767 smmu->num_context_banks, smmu->num_s2_context_banks);
1768 /*
1769 * Cavium CN88xx erratum #27704.
1770 * Ensure ASID and VMID allocation is unique across all SMMUs in
1771 * the system.
1772 */
1773 if (smmu->model == CAVIUM_SMMUV2) {
1774 smmu->cavium_id_base =
1775 atomic_add_return(smmu->num_context_banks,
1776 &cavium_smmu_context_count);
1777 smmu->cavium_id_base -= smmu->num_context_banks;
1778 }
1680 1779
1681 /* ID2 */ 1780 /* ID2 */
1682 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); 1781 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
@@ -1687,6 +1786,9 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1687 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); 1786 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
1688 smmu->pa_size = size; 1787 smmu->pa_size = size;
1689 1788
1789 if (id & ID2_VMID16)
1790 smmu->features |= ARM_SMMU_FEAT_VMID16;
1791
1690 /* 1792 /*
1691 * What the page table walker can address actually depends on which 1793 * What the page table walker can address actually depends on which
1692 * descriptor format is in use, but since a) we don't know that yet, 1794 * descriptor format is in use, but since a) we don't know that yet,
@@ -1696,24 +1798,33 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1696 dev_warn(smmu->dev, 1798 dev_warn(smmu->dev,
1697 "failed to set DMA mask for table walker\n"); 1799 "failed to set DMA mask for table walker\n");
1698 1800
1699 if (smmu->version == ARM_SMMU_V1) { 1801 if (smmu->version < ARM_SMMU_V2) {
1700 smmu->va_size = smmu->ipa_size; 1802 smmu->va_size = smmu->ipa_size;
1701 size = SZ_4K | SZ_2M | SZ_1G; 1803 if (smmu->version == ARM_SMMU_V1_64K)
1804 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1702 } else { 1805 } else {
1703 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; 1806 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
1704 smmu->va_size = arm_smmu_id_size_to_bits(size); 1807 smmu->va_size = arm_smmu_id_size_to_bits(size);
1705#ifndef CONFIG_64BIT
1706 smmu->va_size = min(32UL, smmu->va_size);
1707#endif
1708 size = 0;
1709 if (id & ID2_PTFS_4K) 1808 if (id & ID2_PTFS_4K)
1710 size |= SZ_4K | SZ_2M | SZ_1G; 1809 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
1711 if (id & ID2_PTFS_16K) 1810 if (id & ID2_PTFS_16K)
1712 size |= SZ_16K | SZ_32M; 1811 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
1713 if (id & ID2_PTFS_64K) 1812 if (id & ID2_PTFS_64K)
1714 size |= SZ_64K | SZ_512M; 1813 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1715 } 1814 }
1716 1815
1816 /* Now we've corralled the various formats, what'll it do? */
1817 size = 0;
1818 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
1819 size |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
1820 if (smmu->features &
1821 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
1822 size |= SZ_4K | SZ_2M | SZ_1G;
1823 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
1824 size |= SZ_16K | SZ_32M;
1825 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
1826 size |= SZ_64K | SZ_512M;
1827
1717 arm_smmu_ops.pgsize_bitmap &= size; 1828 arm_smmu_ops.pgsize_bitmap &= size;
1718 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size); 1829 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1719 1830
@@ -1728,12 +1839,27 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1728 return 0; 1839 return 0;
1729} 1840}
1730 1841
1842struct arm_smmu_match_data {
1843 enum arm_smmu_arch_version version;
1844 enum arm_smmu_implementation model;
1845};
1846
1847#define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1848static struct arm_smmu_match_data name = { .version = ver, .model = imp }
1849
1850ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1851ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
1852ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
1853ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
1854ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
1855
1731static const struct of_device_id arm_smmu_of_match[] = { 1856static const struct of_device_id arm_smmu_of_match[] = {
1732 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 }, 1857 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1733 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 }, 1858 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1734 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 }, 1859 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
1735 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 }, 1860 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
1736 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 }, 1861 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
1862 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
1737 { }, 1863 { },
1738}; 1864};
1739MODULE_DEVICE_TABLE(of, arm_smmu_of_match); 1865MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
@@ -1741,6 +1867,7 @@ MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1741static int arm_smmu_device_dt_probe(struct platform_device *pdev) 1867static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1742{ 1868{
1743 const struct of_device_id *of_id; 1869 const struct of_device_id *of_id;
1870 const struct arm_smmu_match_data *data;
1744 struct resource *res; 1871 struct resource *res;
1745 struct arm_smmu_device *smmu; 1872 struct arm_smmu_device *smmu;
1746 struct device *dev = &pdev->dev; 1873 struct device *dev = &pdev->dev;
@@ -1756,7 +1883,9 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1756 smmu->dev = dev; 1883 smmu->dev = dev;
1757 1884
1758 of_id = of_match_node(arm_smmu_of_match, dev->of_node); 1885 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1759 smmu->version = (enum arm_smmu_arch_version)of_id->data; 1886 data = of_id->data;
1887 smmu->version = data->version;
1888 smmu->model = data->model;
1760 1889
1761 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1890 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1762 smmu->base = devm_ioremap_resource(dev, res); 1891 smmu->base = devm_ioremap_resource(dev, res);
@@ -1822,7 +1951,7 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1822 1951
1823 parse_driver_options(smmu); 1952 parse_driver_options(smmu);
1824 1953
1825 if (smmu->version > ARM_SMMU_V1 && 1954 if (smmu->version == ARM_SMMU_V2 &&
1826 smmu->num_context_banks != smmu->num_context_irqs) { 1955 smmu->num_context_banks != smmu->num_context_irqs) {
1827 dev_err(dev, 1956 dev_err(dev,
1828 "found only %d context interrupt(s) but %d required\n", 1957 "found only %d context interrupt(s) but %d required\n",