3434#include <linux/interrupt.h>
3535#include <linux/io.h>
3636#include <linux/iommu.h>
37+ #include <linux/iopoll.h>
3738#include <linux/module.h>
3839#include <linux/of.h>
3940#include <linux/pci.h>
100101#define ID0_S2TS (1 << 29)
101102#define ID0_NTS (1 << 28)
102103#define ID0_SMS (1 << 27)
104+ #define ID0_ATOSNS (1 << 26)
103105#define ID0_CTTW (1 << 14)
104106#define ID0_NUMIRPT_SHIFT 16
105107#define ID0_NUMIRPT_MASK 0xff
189191#define ARM_SMMU_CB_TTBCR 0x30
190192#define ARM_SMMU_CB_S1_MAIR0 0x38
191193#define ARM_SMMU_CB_S1_MAIR1 0x3c
194+ #define ARM_SMMU_CB_PAR_LO 0x50
195+ #define ARM_SMMU_CB_PAR_HI 0x54
192196#define ARM_SMMU_CB_FSR 0x58
193197#define ARM_SMMU_CB_FAR_LO 0x60
194198#define ARM_SMMU_CB_FAR_HI 0x64
198202#define ARM_SMMU_CB_S1_TLBIVAL 0x620
199203#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
200204#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
205+ #define ARM_SMMU_CB_ATS1PR_LO 0x800
206+ #define ARM_SMMU_CB_ATS1PR_HI 0x804
207+ #define ARM_SMMU_CB_ATSR 0x8f0
201208
202209#define SCTLR_S1_ASIDPNE (1 << 12)
203210#define SCTLR_CFCFG (1 << 7)
209216#define SCTLR_M (1 << 0)
210217#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
211218
219+ #define CB_PAR_F (1 << 0)
220+
221+ #define ATSR_ACTIVE (1 << 0)
222+
212223#define RESUME_RETRY (0 << 0)
213224#define RESUME_TERMINATE (1 << 0)
214225
@@ -282,6 +293,7 @@ struct arm_smmu_device {
282293#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
283294#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
284295#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
296+ #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
285297 u32 features ;
286298
287299#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
@@ -1220,8 +1232,52 @@ static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
12201232 return ret ;
12211233}
12221234
1235+ static phys_addr_t arm_smmu_iova_to_phys_hard (struct iommu_domain * domain ,
1236+ dma_addr_t iova )
1237+ {
1238+ struct arm_smmu_domain * smmu_domain = domain -> priv ;
1239+ struct arm_smmu_device * smmu = smmu_domain -> smmu ;
1240+ struct arm_smmu_cfg * cfg = & smmu_domain -> cfg ;
1241+ struct io_pgtable_ops * ops = smmu_domain -> pgtbl_ops ;
1242+ struct device * dev = smmu -> dev ;
1243+ void __iomem * cb_base ;
1244+ u32 tmp ;
1245+ u64 phys ;
1246+
1247+ cb_base = ARM_SMMU_CB_BASE (smmu ) + ARM_SMMU_CB (smmu , cfg -> cbndx );
1248+
1249+ if (smmu -> version == 1 ) {
1250+ u32 reg = iova & ~0xfff ;
1251+ writel_relaxed (reg , cb_base + ARM_SMMU_CB_ATS1PR_LO );
1252+ } else {
1253+ u32 reg = iova & ~0xfff ;
1254+ writel_relaxed (reg , cb_base + ARM_SMMU_CB_ATS1PR_LO );
1255+ reg = (iova & ~0xfff ) >> 32 ;
1256+ writel_relaxed (reg , cb_base + ARM_SMMU_CB_ATS1PR_HI );
1257+ }
1258+
1259+ if (readl_poll_timeout_atomic (cb_base + ARM_SMMU_CB_ATSR , tmp ,
1260+ !(tmp & ATSR_ACTIVE ), 5 , 50 )) {
1261+ dev_err (dev ,
1262+ "iova to phys timed out on 0x%pad. Falling back to software table walk.\n" ,
1263+ & iova );
1264+ return ops -> iova_to_phys (ops , iova );
1265+ }
1266+
1267+ phys = readl_relaxed (cb_base + ARM_SMMU_CB_PAR_LO );
1268+ phys |= ((u64 )readl_relaxed (cb_base + ARM_SMMU_CB_PAR_HI )) << 32 ;
1269+
1270+ if (phys & CB_PAR_F ) {
1271+ dev_err (dev , "translation fault!\n" );
1272+ dev_err (dev , "PAR = 0x%llx\n" , phys );
1273+ return 0 ;
1274+ }
1275+
1276+ return (phys & GENMASK_ULL (39 , 12 )) | (iova & 0xfff );
1277+ }
1278+
12231279static phys_addr_t arm_smmu_iova_to_phys (struct iommu_domain * domain ,
1224- dma_addr_t iova )
1280+ dma_addr_t iova )
12251281{
12261282 phys_addr_t ret ;
12271283 unsigned long flags ;
@@ -1232,8 +1288,12 @@ static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
12321288 return 0 ;
12331289
12341290 spin_lock_irqsave (& smmu_domain -> pgtbl_lock , flags );
1235- ret = ops -> iova_to_phys (ops , iova );
1291+ if (smmu_domain -> smmu -> features & ARM_SMMU_FEAT_TRANS_OPS )
1292+ ret = arm_smmu_iova_to_phys_hard (domain , iova );
1293+ else
1294+ ret = ops -> iova_to_phys (ops , iova );
12361295 spin_unlock_irqrestore (& smmu_domain -> pgtbl_lock , flags );
1296+
12371297 return ret ;
12381298}
12391299
@@ -1496,6 +1556,11 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
14961556 return - ENODEV ;
14971557 }
14981558
1559+ if (smmu -> version == 1 || (!(id & ID0_ATOSNS ) && (id & ID0_S1TS ))) {
1560+ smmu -> features |= ARM_SMMU_FEAT_TRANS_OPS ;
1561+ dev_notice (smmu -> dev , "\taddress translation ops\n" );
1562+ }
1563+
14991564 if (id & ID0_CTTW ) {
15001565 smmu -> features |= ARM_SMMU_FEAT_COHERENT_WALK ;
15011566 dev_notice (smmu -> dev , "\tcoherent table walk\n" );
0 commit comments