Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * IOMMU API for ARM architected SMMU implementations.
0004  *
0005  * Copyright (C) 2013 ARM Limited
0006  *
0007  * Author: Will Deacon <will.deacon@arm.com>
0008  */
0009 
0010 #ifndef _ARM_SMMU_H
0011 #define _ARM_SMMU_H
0012 
0013 #include <linux/atomic.h>
0014 #include <linux/bitfield.h>
0015 #include <linux/bits.h>
0016 #include <linux/clk.h>
0017 #include <linux/device.h>
0018 #include <linux/io-64-nonatomic-hi-lo.h>
0019 #include <linux/io-pgtable.h>
0020 #include <linux/iommu.h>
0021 #include <linux/irqreturn.h>
0022 #include <linux/mutex.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/types.h>
0025 
0026 /* Configuration registers */
0027 #define ARM_SMMU_GR0_sCR0       0x0
0028 #define ARM_SMMU_sCR0_VMID16EN      BIT(31)
0029 #define ARM_SMMU_sCR0_BSU       GENMASK(15, 14)
0030 #define ARM_SMMU_sCR0_FB        BIT(13)
0031 #define ARM_SMMU_sCR0_PTM       BIT(12)
0032 #define ARM_SMMU_sCR0_VMIDPNE       BIT(11)
0033 #define ARM_SMMU_sCR0_USFCFG        BIT(10)
0034 #define ARM_SMMU_sCR0_GCFGFIE       BIT(5)
0035 #define ARM_SMMU_sCR0_GCFGFRE       BIT(4)
0036 #define ARM_SMMU_sCR0_EXIDENABLE    BIT(3)
0037 #define ARM_SMMU_sCR0_GFIE      BIT(2)
0038 #define ARM_SMMU_sCR0_GFRE      BIT(1)
0039 #define ARM_SMMU_sCR0_CLIENTPD      BIT(0)
0040 
0041 /* Auxiliary Configuration register */
0042 #define ARM_SMMU_GR0_sACR       0x10
0043 
0044 /* Identification registers */
0045 #define ARM_SMMU_GR0_ID0        0x20
0046 #define ARM_SMMU_ID0_S1TS       BIT(30)
0047 #define ARM_SMMU_ID0_S2TS       BIT(29)
0048 #define ARM_SMMU_ID0_NTS        BIT(28)
0049 #define ARM_SMMU_ID0_SMS        BIT(27)
0050 #define ARM_SMMU_ID0_ATOSNS     BIT(26)
0051 #define ARM_SMMU_ID0_PTFS_NO_AARCH32    BIT(25)
0052 #define ARM_SMMU_ID0_PTFS_NO_AARCH32S   BIT(24)
0053 #define ARM_SMMU_ID0_NUMIRPT        GENMASK(23, 16)
0054 #define ARM_SMMU_ID0_CTTW       BIT(14)
0055 #define ARM_SMMU_ID0_NUMSIDB        GENMASK(12, 9)
0056 #define ARM_SMMU_ID0_EXIDS      BIT(8)
0057 #define ARM_SMMU_ID0_NUMSMRG        GENMASK(7, 0)
0058 
0059 #define ARM_SMMU_GR0_ID1        0x24
0060 #define ARM_SMMU_ID1_PAGESIZE       BIT(31)
0061 #define ARM_SMMU_ID1_NUMPAGENDXB    GENMASK(30, 28)
0062 #define ARM_SMMU_ID1_NUMS2CB        GENMASK(23, 16)
0063 #define ARM_SMMU_ID1_NUMCB      GENMASK(7, 0)
0064 
0065 #define ARM_SMMU_GR0_ID2        0x28
0066 #define ARM_SMMU_ID2_VMID16     BIT(15)
0067 #define ARM_SMMU_ID2_PTFS_64K       BIT(14)
0068 #define ARM_SMMU_ID2_PTFS_16K       BIT(13)
0069 #define ARM_SMMU_ID2_PTFS_4K        BIT(12)
0070 #define ARM_SMMU_ID2_UBS        GENMASK(11, 8)
0071 #define ARM_SMMU_ID2_OAS        GENMASK(7, 4)
0072 #define ARM_SMMU_ID2_IAS        GENMASK(3, 0)
0073 
0074 #define ARM_SMMU_GR0_ID3        0x2c
0075 #define ARM_SMMU_GR0_ID4        0x30
0076 #define ARM_SMMU_GR0_ID5        0x34
0077 #define ARM_SMMU_GR0_ID6        0x38
0078 
0079 #define ARM_SMMU_GR0_ID7        0x3c
0080 #define ARM_SMMU_ID7_MAJOR      GENMASK(7, 4)
0081 #define ARM_SMMU_ID7_MINOR      GENMASK(3, 0)
0082 
0083 #define ARM_SMMU_GR0_sGFSR      0x48
0084 #define ARM_SMMU_sGFSR_USF      BIT(1)
0085 
0086 #define ARM_SMMU_GR0_sGFSYNR0       0x50
0087 #define ARM_SMMU_GR0_sGFSYNR1       0x54
0088 #define ARM_SMMU_GR0_sGFSYNR2       0x58
0089 
0090 /* Global TLB invalidation */
0091 #define ARM_SMMU_GR0_TLBIVMID       0x64
0092 #define ARM_SMMU_GR0_TLBIALLNSNH    0x68
0093 #define ARM_SMMU_GR0_TLBIALLH       0x6c
0094 #define ARM_SMMU_GR0_sTLBGSYNC      0x70
0095 
0096 #define ARM_SMMU_GR0_sTLBGSTATUS    0x74
0097 #define ARM_SMMU_sTLBGSTATUS_GSACTIVE   BIT(0)
0098 
0099 /* Stream mapping registers */
0100 #define ARM_SMMU_GR0_SMR(n)     (0x800 + ((n) << 2))
0101 #define ARM_SMMU_SMR_VALID      BIT(31)
0102 #define ARM_SMMU_SMR_MASK       GENMASK(31, 16)
0103 #define ARM_SMMU_SMR_ID         GENMASK(15, 0)
0104 
0105 #define ARM_SMMU_GR0_S2CR(n)        (0xc00 + ((n) << 2))
0106 #define ARM_SMMU_S2CR_PRIVCFG       GENMASK(25, 24)
0107 enum arm_smmu_s2cr_privcfg {
0108     S2CR_PRIVCFG_DEFAULT,
0109     S2CR_PRIVCFG_DIPAN,
0110     S2CR_PRIVCFG_UNPRIV,
0111     S2CR_PRIVCFG_PRIV,
0112 };
0113 #define ARM_SMMU_S2CR_TYPE      GENMASK(17, 16)
0114 enum arm_smmu_s2cr_type {
0115     S2CR_TYPE_TRANS,
0116     S2CR_TYPE_BYPASS,
0117     S2CR_TYPE_FAULT,
0118 };
0119 #define ARM_SMMU_S2CR_EXIDVALID     BIT(10)
0120 #define ARM_SMMU_S2CR_CBNDX     GENMASK(7, 0)
0121 
0122 /* Context bank attribute registers */
0123 #define ARM_SMMU_GR1_CBAR(n)        (0x0 + ((n) << 2))
0124 #define ARM_SMMU_CBAR_IRPTNDX       GENMASK(31, 24)
0125 #define ARM_SMMU_CBAR_TYPE      GENMASK(17, 16)
0126 enum arm_smmu_cbar_type {
0127     CBAR_TYPE_S2_TRANS,
0128     CBAR_TYPE_S1_TRANS_S2_BYPASS,
0129     CBAR_TYPE_S1_TRANS_S2_FAULT,
0130     CBAR_TYPE_S1_TRANS_S2_TRANS,
0131 };
0132 #define ARM_SMMU_CBAR_S1_MEMATTR    GENMASK(15, 12)
0133 #define ARM_SMMU_CBAR_S1_MEMATTR_WB 0xf
0134 #define ARM_SMMU_CBAR_S1_BPSHCFG    GENMASK(9, 8)
0135 #define ARM_SMMU_CBAR_S1_BPSHCFG_NSH    3
0136 #define ARM_SMMU_CBAR_VMID      GENMASK(7, 0)
0137 
0138 #define ARM_SMMU_GR1_CBFRSYNRA(n)   (0x400 + ((n) << 2))
0139 
0140 #define ARM_SMMU_GR1_CBA2R(n)       (0x800 + ((n) << 2))
0141 #define ARM_SMMU_CBA2R_VMID16       GENMASK(31, 16)
0142 #define ARM_SMMU_CBA2R_VA64     BIT(0)
0143 
0144 #define ARM_SMMU_CB_SCTLR       0x0
0145 #define ARM_SMMU_SCTLR_S1_ASIDPNE   BIT(12)
0146 #define ARM_SMMU_SCTLR_CFCFG        BIT(7)
0147 #define ARM_SMMU_SCTLR_HUPCF        BIT(8)
0148 #define ARM_SMMU_SCTLR_CFIE     BIT(6)
0149 #define ARM_SMMU_SCTLR_CFRE     BIT(5)
0150 #define ARM_SMMU_SCTLR_E        BIT(4)
0151 #define ARM_SMMU_SCTLR_AFE      BIT(2)
0152 #define ARM_SMMU_SCTLR_TRE      BIT(1)
0153 #define ARM_SMMU_SCTLR_M        BIT(0)
0154 
0155 #define ARM_SMMU_CB_ACTLR       0x4
0156 
0157 #define ARM_SMMU_CB_RESUME      0x8
0158 #define ARM_SMMU_RESUME_TERMINATE   BIT(0)
0159 
0160 #define ARM_SMMU_CB_TCR2        0x10
0161 #define ARM_SMMU_TCR2_SEP       GENMASK(17, 15)
0162 #define ARM_SMMU_TCR2_SEP_UPSTREAM  0x7
0163 #define ARM_SMMU_TCR2_AS        BIT(4)
0164 #define ARM_SMMU_TCR2_PASIZE        GENMASK(3, 0)
0165 
0166 #define ARM_SMMU_CB_TTBR0       0x20
0167 #define ARM_SMMU_CB_TTBR1       0x28
0168 #define ARM_SMMU_TTBRn_ASID     GENMASK_ULL(63, 48)
0169 
0170 #define ARM_SMMU_CB_TCR         0x30
0171 #define ARM_SMMU_TCR_EAE        BIT(31)
0172 #define ARM_SMMU_TCR_EPD1       BIT(23)
0173 #define ARM_SMMU_TCR_A1         BIT(22)
0174 #define ARM_SMMU_TCR_TG0        GENMASK(15, 14)
0175 #define ARM_SMMU_TCR_SH0        GENMASK(13, 12)
0176 #define ARM_SMMU_TCR_ORGN0      GENMASK(11, 10)
0177 #define ARM_SMMU_TCR_IRGN0      GENMASK(9, 8)
0178 #define ARM_SMMU_TCR_EPD0       BIT(7)
0179 #define ARM_SMMU_TCR_T0SZ       GENMASK(5, 0)
0180 
0181 #define ARM_SMMU_VTCR_RES1      BIT(31)
0182 #define ARM_SMMU_VTCR_PS        GENMASK(18, 16)
0183 #define ARM_SMMU_VTCR_TG0       ARM_SMMU_TCR_TG0
0184 #define ARM_SMMU_VTCR_SH0       ARM_SMMU_TCR_SH0
0185 #define ARM_SMMU_VTCR_ORGN0     ARM_SMMU_TCR_ORGN0
0186 #define ARM_SMMU_VTCR_IRGN0     ARM_SMMU_TCR_IRGN0
0187 #define ARM_SMMU_VTCR_SL0       GENMASK(7, 6)
0188 #define ARM_SMMU_VTCR_T0SZ      ARM_SMMU_TCR_T0SZ
0189 
0190 #define ARM_SMMU_CB_CONTEXTIDR      0x34
0191 #define ARM_SMMU_CB_S1_MAIR0        0x38
0192 #define ARM_SMMU_CB_S1_MAIR1        0x3c
0193 
0194 #define ARM_SMMU_CB_PAR         0x50
0195 #define ARM_SMMU_CB_PAR_F       BIT(0)
0196 
0197 #define ARM_SMMU_CB_FSR         0x58
0198 #define ARM_SMMU_FSR_MULTI      BIT(31)
0199 #define ARM_SMMU_FSR_SS         BIT(30)
0200 #define ARM_SMMU_FSR_UUT        BIT(8)
0201 #define ARM_SMMU_FSR_ASF        BIT(7)
0202 #define ARM_SMMU_FSR_TLBLKF     BIT(6)
0203 #define ARM_SMMU_FSR_TLBMCF     BIT(5)
0204 #define ARM_SMMU_FSR_EF         BIT(4)
0205 #define ARM_SMMU_FSR_PF         BIT(3)
0206 #define ARM_SMMU_FSR_AFF        BIT(2)
0207 #define ARM_SMMU_FSR_TF         BIT(1)
0208 
0209 #define ARM_SMMU_FSR_IGN        (ARM_SMMU_FSR_AFF |     \
0210                      ARM_SMMU_FSR_ASF |     \
0211                      ARM_SMMU_FSR_TLBMCF |      \
0212                      ARM_SMMU_FSR_TLBLKF)
0213 
0214 #define ARM_SMMU_FSR_FAULT      (ARM_SMMU_FSR_MULTI |       \
0215                      ARM_SMMU_FSR_SS |      \
0216                      ARM_SMMU_FSR_UUT |     \
0217                      ARM_SMMU_FSR_EF |      \
0218                      ARM_SMMU_FSR_PF |      \
0219                      ARM_SMMU_FSR_TF |      \
0220                      ARM_SMMU_FSR_IGN)
0221 
0222 #define ARM_SMMU_CB_FAR         0x60
0223 
0224 #define ARM_SMMU_CB_FSYNR0      0x68
0225 #define ARM_SMMU_FSYNR0_WNR     BIT(4)
0226 
0227 #define ARM_SMMU_CB_FSYNR1      0x6c
0228 
0229 #define ARM_SMMU_CB_S1_TLBIVA       0x600
0230 #define ARM_SMMU_CB_S1_TLBIASID     0x610
0231 #define ARM_SMMU_CB_S1_TLBIVAL      0x620
0232 #define ARM_SMMU_CB_S2_TLBIIPAS2    0x630
0233 #define ARM_SMMU_CB_S2_TLBIIPAS2L   0x638
0234 #define ARM_SMMU_CB_TLBSYNC     0x7f0
0235 #define ARM_SMMU_CB_TLBSTATUS       0x7f4
0236 #define ARM_SMMU_CB_ATS1PR      0x800
0237 
0238 #define ARM_SMMU_CB_ATSR        0x8f0
0239 #define ARM_SMMU_ATSR_ACTIVE        BIT(0)
0240 
0241 
0242 /* Maximum number of context banks per SMMU */
0243 #define ARM_SMMU_MAX_CBS        128
0244 
0245 #define TLB_LOOP_TIMEOUT        1000000 /* 1s! */
0246 #define TLB_SPIN_COUNT          10
0247 
0248 /* Shared driver definitions */
0249 enum arm_smmu_arch_version {
0250     ARM_SMMU_V1,
0251     ARM_SMMU_V1_64K,
0252     ARM_SMMU_V2,
0253 };
0254 
0255 enum arm_smmu_implementation {
0256     GENERIC_SMMU,
0257     ARM_MMU500,
0258     CAVIUM_SMMUV2,
0259     QCOM_SMMUV2,
0260 };
0261 
0262 struct arm_smmu_s2cr {
0263     struct iommu_group      *group;
0264     int             count;
0265     enum arm_smmu_s2cr_type     type;
0266     enum arm_smmu_s2cr_privcfg  privcfg;
0267     u8              cbndx;
0268 };
0269 
0270 struct arm_smmu_smr {
0271     u16             mask;
0272     u16             id;
0273     bool                valid;
0274     bool                pinned;
0275 };
0276 
0277 struct arm_smmu_device {
0278     struct device           *dev;
0279 
0280     void __iomem            *base;
0281     phys_addr_t         ioaddr;
0282     unsigned int            numpage;
0283     unsigned int            pgshift;
0284 
0285 #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
0286 #define ARM_SMMU_FEAT_STREAM_MATCH  (1 << 1)
0287 #define ARM_SMMU_FEAT_TRANS_S1      (1 << 2)
0288 #define ARM_SMMU_FEAT_TRANS_S2      (1 << 3)
0289 #define ARM_SMMU_FEAT_TRANS_NESTED  (1 << 4)
0290 #define ARM_SMMU_FEAT_TRANS_OPS     (1 << 5)
0291 #define ARM_SMMU_FEAT_VMID16        (1 << 6)
0292 #define ARM_SMMU_FEAT_FMT_AARCH64_4K    (1 << 7)
0293 #define ARM_SMMU_FEAT_FMT_AARCH64_16K   (1 << 8)
0294 #define ARM_SMMU_FEAT_FMT_AARCH64_64K   (1 << 9)
0295 #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
0296 #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
0297 #define ARM_SMMU_FEAT_EXIDS     (1 << 12)
0298     u32             features;
0299 
0300     enum arm_smmu_arch_version  version;
0301     enum arm_smmu_implementation    model;
0302     const struct arm_smmu_impl  *impl;
0303 
0304     u32             num_context_banks;
0305     u32             num_s2_context_banks;
0306     DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
0307     struct arm_smmu_cb      *cbs;
0308     atomic_t            irptndx;
0309 
0310     u32             num_mapping_groups;
0311     u16             streamid_mask;
0312     u16             smr_mask_mask;
0313     struct arm_smmu_smr     *smrs;
0314     struct arm_smmu_s2cr        *s2crs;
0315     struct mutex            stream_map_mutex;
0316 
0317     unsigned long           va_size;
0318     unsigned long           ipa_size;
0319     unsigned long           pa_size;
0320     unsigned long           pgsize_bitmap;
0321 
0322     int             num_context_irqs;
0323     int             num_clks;
0324     unsigned int            *irqs;
0325     struct clk_bulk_data        *clks;
0326 
0327     spinlock_t          global_sync_lock;
0328 
0329     /* IOMMU core code handle */
0330     struct iommu_device     iommu;
0331 };
0332 
0333 enum arm_smmu_context_fmt {
0334     ARM_SMMU_CTX_FMT_NONE,
0335     ARM_SMMU_CTX_FMT_AARCH64,
0336     ARM_SMMU_CTX_FMT_AARCH32_L,
0337     ARM_SMMU_CTX_FMT_AARCH32_S,
0338 };
0339 
0340 struct arm_smmu_cfg {
0341     u8              cbndx;
0342     u8              irptndx;
0343     union {
0344         u16         asid;
0345         u16         vmid;
0346     };
0347     enum arm_smmu_cbar_type     cbar;
0348     enum arm_smmu_context_fmt   fmt;
0349     bool                flush_walk_prefer_tlbiasid;
0350 };
0351 #define ARM_SMMU_INVALID_IRPTNDX    0xff
0352 
0353 struct arm_smmu_cb {
0354     u64             ttbr[2];
0355     u32             tcr[2];
0356     u32             mair[2];
0357     struct arm_smmu_cfg     *cfg;
0358 };
0359 
0360 enum arm_smmu_domain_stage {
0361     ARM_SMMU_DOMAIN_S1 = 0,
0362     ARM_SMMU_DOMAIN_S2,
0363     ARM_SMMU_DOMAIN_NESTED,
0364     ARM_SMMU_DOMAIN_BYPASS,
0365 };
0366 
0367 struct arm_smmu_domain {
0368     struct arm_smmu_device      *smmu;
0369     struct io_pgtable_ops       *pgtbl_ops;
0370     unsigned long           pgtbl_quirks;
0371     const struct iommu_flush_ops    *flush_ops;
0372     struct arm_smmu_cfg     cfg;
0373     enum arm_smmu_domain_stage  stage;
0374     struct mutex            init_mutex; /* Protects smmu pointer */
0375     spinlock_t          cb_lock; /* Serialises ATS1* ops and TLB syncs */
0376     struct iommu_domain     domain;
0377 };
0378 
0379 struct arm_smmu_master_cfg {
0380     struct arm_smmu_device      *smmu;
0381     s16             smendx[];
0382 };
0383 
0384 static inline u32 arm_smmu_lpae_tcr(const struct io_pgtable_cfg *cfg)
0385 {
0386     u32 tcr = FIELD_PREP(ARM_SMMU_TCR_TG0, cfg->arm_lpae_s1_cfg.tcr.tg) |
0387         FIELD_PREP(ARM_SMMU_TCR_SH0, cfg->arm_lpae_s1_cfg.tcr.sh) |
0388         FIELD_PREP(ARM_SMMU_TCR_ORGN0, cfg->arm_lpae_s1_cfg.tcr.orgn) |
0389         FIELD_PREP(ARM_SMMU_TCR_IRGN0, cfg->arm_lpae_s1_cfg.tcr.irgn) |
0390         FIELD_PREP(ARM_SMMU_TCR_T0SZ, cfg->arm_lpae_s1_cfg.tcr.tsz);
0391 
0392        /*
0393     * When TTBR1 is selected shift the TCR fields by 16 bits and disable
0394     * translation in TTBR0
0395     */
0396     if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) {
0397         tcr = (tcr << 16) & ~ARM_SMMU_TCR_A1;
0398         tcr |= ARM_SMMU_TCR_EPD0;
0399     } else
0400         tcr |= ARM_SMMU_TCR_EPD1;
0401 
0402     return tcr;
0403 }
0404 
0405 static inline u32 arm_smmu_lpae_tcr2(const struct io_pgtable_cfg *cfg)
0406 {
0407     return FIELD_PREP(ARM_SMMU_TCR2_PASIZE, cfg->arm_lpae_s1_cfg.tcr.ips) |
0408            FIELD_PREP(ARM_SMMU_TCR2_SEP, ARM_SMMU_TCR2_SEP_UPSTREAM);
0409 }
0410 
0411 static inline u32 arm_smmu_lpae_vtcr(const struct io_pgtable_cfg *cfg)
0412 {
0413     return ARM_SMMU_VTCR_RES1 |
0414            FIELD_PREP(ARM_SMMU_VTCR_PS, cfg->arm_lpae_s2_cfg.vtcr.ps) |
0415            FIELD_PREP(ARM_SMMU_VTCR_TG0, cfg->arm_lpae_s2_cfg.vtcr.tg) |
0416            FIELD_PREP(ARM_SMMU_VTCR_SH0, cfg->arm_lpae_s2_cfg.vtcr.sh) |
0417            FIELD_PREP(ARM_SMMU_VTCR_ORGN0, cfg->arm_lpae_s2_cfg.vtcr.orgn) |
0418            FIELD_PREP(ARM_SMMU_VTCR_IRGN0, cfg->arm_lpae_s2_cfg.vtcr.irgn) |
0419            FIELD_PREP(ARM_SMMU_VTCR_SL0, cfg->arm_lpae_s2_cfg.vtcr.sl) |
0420            FIELD_PREP(ARM_SMMU_VTCR_T0SZ, cfg->arm_lpae_s2_cfg.vtcr.tsz);
0421 }
0422 
0423 /* Implementation details, yay! */
0424 struct arm_smmu_impl {
0425     u32 (*read_reg)(struct arm_smmu_device *smmu, int page, int offset);
0426     void (*write_reg)(struct arm_smmu_device *smmu, int page, int offset,
0427               u32 val);
0428     u64 (*read_reg64)(struct arm_smmu_device *smmu, int page, int offset);
0429     void (*write_reg64)(struct arm_smmu_device *smmu, int page, int offset,
0430                 u64 val);
0431     int (*cfg_probe)(struct arm_smmu_device *smmu);
0432     int (*reset)(struct arm_smmu_device *smmu);
0433     int (*init_context)(struct arm_smmu_domain *smmu_domain,
0434             struct io_pgtable_cfg *cfg, struct device *dev);
0435     void (*tlb_sync)(struct arm_smmu_device *smmu, int page, int sync,
0436              int status);
0437     int (*def_domain_type)(struct device *dev);
0438     irqreturn_t (*global_fault)(int irq, void *dev);
0439     irqreturn_t (*context_fault)(int irq, void *dev);
0440     int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain,
0441                   struct arm_smmu_device *smmu,
0442                   struct device *dev, int start);
0443     void (*write_s2cr)(struct arm_smmu_device *smmu, int idx);
0444     void (*write_sctlr)(struct arm_smmu_device *smmu, int idx, u32 reg);
0445     void (*probe_finalize)(struct arm_smmu_device *smmu, struct device *dev);
0446 };
0447 
0448 #define INVALID_SMENDX          -1
0449 #define cfg_smendx(cfg, fw, i) \
0450     (i >= fw->num_ids ? INVALID_SMENDX : cfg->smendx[i])
0451 #define for_each_cfg_sme(cfg, fw, i, idx) \
0452     for (i = 0; idx = cfg_smendx(cfg, fw, i), i < fw->num_ids; ++i)
0453 
0454 static inline int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
0455 {
0456     int idx;
0457 
0458     do {
0459         idx = find_next_zero_bit(map, end, start);
0460         if (idx == end)
0461             return -ENOSPC;
0462     } while (test_and_set_bit(idx, map));
0463 
0464     return idx;
0465 }
0466 
0467 static inline void __iomem *arm_smmu_page(struct arm_smmu_device *smmu, int n)
0468 {
0469     return smmu->base + (n << smmu->pgshift);
0470 }
0471 
0472 static inline u32 arm_smmu_readl(struct arm_smmu_device *smmu, int page, int offset)
0473 {
0474     if (smmu->impl && unlikely(smmu->impl->read_reg))
0475         return smmu->impl->read_reg(smmu, page, offset);
0476     return readl_relaxed(arm_smmu_page(smmu, page) + offset);
0477 }
0478 
0479 static inline void arm_smmu_writel(struct arm_smmu_device *smmu, int page,
0480                    int offset, u32 val)
0481 {
0482     if (smmu->impl && unlikely(smmu->impl->write_reg))
0483         smmu->impl->write_reg(smmu, page, offset, val);
0484     else
0485         writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
0486 }
0487 
0488 static inline u64 arm_smmu_readq(struct arm_smmu_device *smmu, int page, int offset)
0489 {
0490     if (smmu->impl && unlikely(smmu->impl->read_reg64))
0491         return smmu->impl->read_reg64(smmu, page, offset);
0492     return readq_relaxed(arm_smmu_page(smmu, page) + offset);
0493 }
0494 
0495 static inline void arm_smmu_writeq(struct arm_smmu_device *smmu, int page,
0496                    int offset, u64 val)
0497 {
0498     if (smmu->impl && unlikely(smmu->impl->write_reg64))
0499         smmu->impl->write_reg64(smmu, page, offset, val);
0500     else
0501         writeq_relaxed(val, arm_smmu_page(smmu, page) + offset);
0502 }
0503 
0504 #define ARM_SMMU_GR0        0
0505 #define ARM_SMMU_GR1        1
0506 #define ARM_SMMU_CB(s, n)   ((s)->numpage + (n))
0507 
0508 #define arm_smmu_gr0_read(s, o)     \
0509     arm_smmu_readl((s), ARM_SMMU_GR0, (o))
0510 #define arm_smmu_gr0_write(s, o, v) \
0511     arm_smmu_writel((s), ARM_SMMU_GR0, (o), (v))
0512 
0513 #define arm_smmu_gr1_read(s, o)     \
0514     arm_smmu_readl((s), ARM_SMMU_GR1, (o))
0515 #define arm_smmu_gr1_write(s, o, v) \
0516     arm_smmu_writel((s), ARM_SMMU_GR1, (o), (v))
0517 
0518 #define arm_smmu_cb_read(s, n, o)   \
0519     arm_smmu_readl((s), ARM_SMMU_CB((s), (n)), (o))
0520 #define arm_smmu_cb_write(s, n, o, v)   \
0521     arm_smmu_writel((s), ARM_SMMU_CB((s), (n)), (o), (v))
0522 #define arm_smmu_cb_readq(s, n, o)  \
0523     arm_smmu_readq((s), ARM_SMMU_CB((s), (n)), (o))
0524 #define arm_smmu_cb_writeq(s, n, o, v)  \
0525     arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v))
0526 
0527 struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu);
0528 struct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu);
0529 struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu);
0530 
0531 void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx);
0532 int arm_mmu500_reset(struct arm_smmu_device *smmu);
0533 
0534 #endif /* _ARM_SMMU_H */