162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * IOMMU API for ARM architected SMMU implementations. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2013 ARM Limited 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Author: Will Deacon <will.deacon@arm.com> 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#ifndef _ARM_SMMU_H 1162306a36Sopenharmony_ci#define _ARM_SMMU_H 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include <linux/atomic.h> 1462306a36Sopenharmony_ci#include <linux/bitfield.h> 1562306a36Sopenharmony_ci#include <linux/bits.h> 1662306a36Sopenharmony_ci#include <linux/clk.h> 1762306a36Sopenharmony_ci#include <linux/device.h> 1862306a36Sopenharmony_ci#include <linux/io-64-nonatomic-hi-lo.h> 1962306a36Sopenharmony_ci#include <linux/io-pgtable.h> 2062306a36Sopenharmony_ci#include <linux/iommu.h> 2162306a36Sopenharmony_ci#include <linux/irqreturn.h> 2262306a36Sopenharmony_ci#include <linux/mutex.h> 2362306a36Sopenharmony_ci#include <linux/spinlock.h> 2462306a36Sopenharmony_ci#include <linux/types.h> 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci/* Configuration registers */ 2762306a36Sopenharmony_ci#define ARM_SMMU_GR0_sCR0 0x0 2862306a36Sopenharmony_ci#define ARM_SMMU_sCR0_VMID16EN BIT(31) 2962306a36Sopenharmony_ci#define ARM_SMMU_sCR0_BSU GENMASK(15, 14) 3062306a36Sopenharmony_ci#define ARM_SMMU_sCR0_FB BIT(13) 3162306a36Sopenharmony_ci#define ARM_SMMU_sCR0_PTM BIT(12) 3262306a36Sopenharmony_ci#define ARM_SMMU_sCR0_VMIDPNE BIT(11) 3362306a36Sopenharmony_ci#define ARM_SMMU_sCR0_USFCFG BIT(10) 3462306a36Sopenharmony_ci#define ARM_SMMU_sCR0_GCFGFIE BIT(5) 3562306a36Sopenharmony_ci#define ARM_SMMU_sCR0_GCFGFRE BIT(4) 3662306a36Sopenharmony_ci#define ARM_SMMU_sCR0_EXIDENABLE BIT(3) 3762306a36Sopenharmony_ci#define ARM_SMMU_sCR0_GFIE BIT(2) 3862306a36Sopenharmony_ci#define ARM_SMMU_sCR0_GFRE BIT(1) 3962306a36Sopenharmony_ci#define ARM_SMMU_sCR0_CLIENTPD BIT(0) 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/* Auxiliary Configuration register */ 4262306a36Sopenharmony_ci#define ARM_SMMU_GR0_sACR 0x10 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci/* Identification registers */ 4562306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID0 0x20 4662306a36Sopenharmony_ci#define ARM_SMMU_ID0_S1TS BIT(30) 4762306a36Sopenharmony_ci#define ARM_SMMU_ID0_S2TS BIT(29) 4862306a36Sopenharmony_ci#define ARM_SMMU_ID0_NTS BIT(28) 4962306a36Sopenharmony_ci#define ARM_SMMU_ID0_SMS BIT(27) 5062306a36Sopenharmony_ci#define ARM_SMMU_ID0_ATOSNS BIT(26) 5162306a36Sopenharmony_ci#define ARM_SMMU_ID0_PTFS_NO_AARCH32 BIT(25) 5262306a36Sopenharmony_ci#define ARM_SMMU_ID0_PTFS_NO_AARCH32S BIT(24) 5362306a36Sopenharmony_ci#define ARM_SMMU_ID0_NUMIRPT GENMASK(23, 16) 5462306a36Sopenharmony_ci#define ARM_SMMU_ID0_CTTW BIT(14) 5562306a36Sopenharmony_ci#define ARM_SMMU_ID0_NUMSIDB GENMASK(12, 9) 5662306a36Sopenharmony_ci#define ARM_SMMU_ID0_EXIDS BIT(8) 5762306a36Sopenharmony_ci#define ARM_SMMU_ID0_NUMSMRG GENMASK(7, 0) 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID1 0x24 6062306a36Sopenharmony_ci#define ARM_SMMU_ID1_PAGESIZE BIT(31) 6162306a36Sopenharmony_ci#define ARM_SMMU_ID1_NUMPAGENDXB GENMASK(30, 28) 6262306a36Sopenharmony_ci#define ARM_SMMU_ID1_NUMS2CB GENMASK(23, 16) 6362306a36Sopenharmony_ci#define ARM_SMMU_ID1_NUMCB GENMASK(7, 0) 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID2 0x28 6662306a36Sopenharmony_ci#define ARM_SMMU_ID2_VMID16 BIT(15) 6762306a36Sopenharmony_ci#define ARM_SMMU_ID2_PTFS_64K BIT(14) 6862306a36Sopenharmony_ci#define ARM_SMMU_ID2_PTFS_16K BIT(13) 6962306a36Sopenharmony_ci#define ARM_SMMU_ID2_PTFS_4K BIT(12) 7062306a36Sopenharmony_ci#define ARM_SMMU_ID2_UBS GENMASK(11, 8) 7162306a36Sopenharmony_ci#define ARM_SMMU_ID2_OAS GENMASK(7, 4) 7262306a36Sopenharmony_ci#define ARM_SMMU_ID2_IAS GENMASK(3, 0) 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID3 0x2c 7562306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID4 0x30 7662306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID5 0x34 7762306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID6 0x38 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci#define ARM_SMMU_GR0_ID7 0x3c 8062306a36Sopenharmony_ci#define ARM_SMMU_ID7_MAJOR GENMASK(7, 4) 8162306a36Sopenharmony_ci#define ARM_SMMU_ID7_MINOR GENMASK(3, 0) 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci#define ARM_SMMU_GR0_sGFSR 0x48 8462306a36Sopenharmony_ci#define ARM_SMMU_sGFSR_USF BIT(1) 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci#define ARM_SMMU_GR0_sGFSYNR0 0x50 8762306a36Sopenharmony_ci#define ARM_SMMU_GR0_sGFSYNR1 0x54 8862306a36Sopenharmony_ci#define ARM_SMMU_GR0_sGFSYNR2 0x58 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci/* Global TLB invalidation */ 9162306a36Sopenharmony_ci#define ARM_SMMU_GR0_TLBIVMID 0x64 9262306a36Sopenharmony_ci#define ARM_SMMU_GR0_TLBIALLNSNH 0x68 9362306a36Sopenharmony_ci#define ARM_SMMU_GR0_TLBIALLH 0x6c 9462306a36Sopenharmony_ci#define ARM_SMMU_GR0_sTLBGSYNC 0x70 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci#define ARM_SMMU_GR0_sTLBGSTATUS 0x74 9762306a36Sopenharmony_ci#define ARM_SMMU_sTLBGSTATUS_GSACTIVE BIT(0) 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci/* Stream mapping registers */ 10062306a36Sopenharmony_ci#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2)) 10162306a36Sopenharmony_ci#define ARM_SMMU_SMR_VALID BIT(31) 10262306a36Sopenharmony_ci#define ARM_SMMU_SMR_MASK GENMASK(31, 16) 10362306a36Sopenharmony_ci#define ARM_SMMU_SMR_ID GENMASK(15, 0) 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2)) 10662306a36Sopenharmony_ci#define ARM_SMMU_S2CR_PRIVCFG GENMASK(25, 24) 10762306a36Sopenharmony_cienum arm_smmu_s2cr_privcfg { 10862306a36Sopenharmony_ci S2CR_PRIVCFG_DEFAULT, 10962306a36Sopenharmony_ci S2CR_PRIVCFG_DIPAN, 11062306a36Sopenharmony_ci S2CR_PRIVCFG_UNPRIV, 11162306a36Sopenharmony_ci S2CR_PRIVCFG_PRIV, 11262306a36Sopenharmony_ci}; 11362306a36Sopenharmony_ci#define ARM_SMMU_S2CR_TYPE GENMASK(17, 16) 11462306a36Sopenharmony_cienum arm_smmu_s2cr_type { 11562306a36Sopenharmony_ci S2CR_TYPE_TRANS, 11662306a36Sopenharmony_ci S2CR_TYPE_BYPASS, 11762306a36Sopenharmony_ci S2CR_TYPE_FAULT, 11862306a36Sopenharmony_ci}; 11962306a36Sopenharmony_ci#define ARM_SMMU_S2CR_EXIDVALID BIT(10) 12062306a36Sopenharmony_ci#define ARM_SMMU_S2CR_CBNDX GENMASK(7, 0) 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci/* Context bank attribute registers */ 12362306a36Sopenharmony_ci#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2)) 12462306a36Sopenharmony_ci#define ARM_SMMU_CBAR_IRPTNDX GENMASK(31, 24) 12562306a36Sopenharmony_ci#define ARM_SMMU_CBAR_TYPE GENMASK(17, 16) 12662306a36Sopenharmony_cienum arm_smmu_cbar_type { 12762306a36Sopenharmony_ci CBAR_TYPE_S2_TRANS, 12862306a36Sopenharmony_ci CBAR_TYPE_S1_TRANS_S2_BYPASS, 12962306a36Sopenharmony_ci CBAR_TYPE_S1_TRANS_S2_FAULT, 13062306a36Sopenharmony_ci CBAR_TYPE_S1_TRANS_S2_TRANS, 13162306a36Sopenharmony_ci}; 13262306a36Sopenharmony_ci#define ARM_SMMU_CBAR_S1_MEMATTR GENMASK(15, 12) 13362306a36Sopenharmony_ci#define ARM_SMMU_CBAR_S1_MEMATTR_WB 0xf 13462306a36Sopenharmony_ci#define ARM_SMMU_CBAR_S1_BPSHCFG GENMASK(9, 8) 13562306a36Sopenharmony_ci#define ARM_SMMU_CBAR_S1_BPSHCFG_NSH 3 13662306a36Sopenharmony_ci#define ARM_SMMU_CBAR_VMID GENMASK(7, 0) 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci#define ARM_SMMU_GR1_CBFRSYNRA(n) (0x400 + ((n) << 2)) 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2)) 14162306a36Sopenharmony_ci#define ARM_SMMU_CBA2R_VMID16 GENMASK(31, 16) 14262306a36Sopenharmony_ci#define ARM_SMMU_CBA2R_VA64 BIT(0) 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci#define ARM_SMMU_CB_SCTLR 0x0 14562306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_S1_ASIDPNE BIT(12) 14662306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_CFCFG BIT(7) 14762306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_HUPCF BIT(8) 14862306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_CFIE BIT(6) 14962306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_CFRE BIT(5) 15062306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_E BIT(4) 15162306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_AFE BIT(2) 15262306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_TRE BIT(1) 15362306a36Sopenharmony_ci#define ARM_SMMU_SCTLR_M BIT(0) 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci#define ARM_SMMU_CB_ACTLR 0x4 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci#define ARM_SMMU_CB_RESUME 0x8 15862306a36Sopenharmony_ci#define ARM_SMMU_RESUME_TERMINATE BIT(0) 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci#define ARM_SMMU_CB_TCR2 0x10 16162306a36Sopenharmony_ci#define ARM_SMMU_TCR2_SEP GENMASK(17, 15) 16262306a36Sopenharmony_ci#define ARM_SMMU_TCR2_SEP_UPSTREAM 0x7 16362306a36Sopenharmony_ci#define ARM_SMMU_TCR2_AS BIT(4) 16462306a36Sopenharmony_ci#define ARM_SMMU_TCR2_PASIZE GENMASK(3, 0) 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci#define ARM_SMMU_CB_TTBR0 0x20 16762306a36Sopenharmony_ci#define ARM_SMMU_CB_TTBR1 0x28 16862306a36Sopenharmony_ci#define ARM_SMMU_TTBRn_ASID GENMASK_ULL(63, 48) 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci#define ARM_SMMU_CB_TCR 0x30 17162306a36Sopenharmony_ci#define ARM_SMMU_TCR_EAE BIT(31) 17262306a36Sopenharmony_ci#define ARM_SMMU_TCR_EPD1 BIT(23) 17362306a36Sopenharmony_ci#define ARM_SMMU_TCR_A1 BIT(22) 17462306a36Sopenharmony_ci#define ARM_SMMU_TCR_TG0 GENMASK(15, 14) 17562306a36Sopenharmony_ci#define ARM_SMMU_TCR_SH0 GENMASK(13, 12) 17662306a36Sopenharmony_ci#define ARM_SMMU_TCR_ORGN0 GENMASK(11, 10) 17762306a36Sopenharmony_ci#define ARM_SMMU_TCR_IRGN0 GENMASK(9, 8) 17862306a36Sopenharmony_ci#define ARM_SMMU_TCR_EPD0 BIT(7) 17962306a36Sopenharmony_ci#define ARM_SMMU_TCR_T0SZ GENMASK(5, 0) 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci#define ARM_SMMU_VTCR_RES1 BIT(31) 18262306a36Sopenharmony_ci#define ARM_SMMU_VTCR_PS GENMASK(18, 16) 18362306a36Sopenharmony_ci#define ARM_SMMU_VTCR_TG0 ARM_SMMU_TCR_TG0 18462306a36Sopenharmony_ci#define ARM_SMMU_VTCR_SH0 ARM_SMMU_TCR_SH0 18562306a36Sopenharmony_ci#define ARM_SMMU_VTCR_ORGN0 ARM_SMMU_TCR_ORGN0 18662306a36Sopenharmony_ci#define ARM_SMMU_VTCR_IRGN0 ARM_SMMU_TCR_IRGN0 18762306a36Sopenharmony_ci#define ARM_SMMU_VTCR_SL0 GENMASK(7, 6) 18862306a36Sopenharmony_ci#define ARM_SMMU_VTCR_T0SZ ARM_SMMU_TCR_T0SZ 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci#define ARM_SMMU_CB_CONTEXTIDR 0x34 19162306a36Sopenharmony_ci#define ARM_SMMU_CB_S1_MAIR0 0x38 19262306a36Sopenharmony_ci#define ARM_SMMU_CB_S1_MAIR1 0x3c 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci#define ARM_SMMU_CB_PAR 0x50 19562306a36Sopenharmony_ci#define ARM_SMMU_CB_PAR_F BIT(0) 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci#define ARM_SMMU_CB_FSR 0x58 19862306a36Sopenharmony_ci#define ARM_SMMU_FSR_MULTI BIT(31) 19962306a36Sopenharmony_ci#define ARM_SMMU_FSR_SS BIT(30) 20062306a36Sopenharmony_ci#define ARM_SMMU_FSR_UUT BIT(8) 20162306a36Sopenharmony_ci#define ARM_SMMU_FSR_ASF BIT(7) 20262306a36Sopenharmony_ci#define ARM_SMMU_FSR_TLBLKF BIT(6) 20362306a36Sopenharmony_ci#define ARM_SMMU_FSR_TLBMCF BIT(5) 20462306a36Sopenharmony_ci#define ARM_SMMU_FSR_EF BIT(4) 20562306a36Sopenharmony_ci#define ARM_SMMU_FSR_PF BIT(3) 20662306a36Sopenharmony_ci#define ARM_SMMU_FSR_AFF BIT(2) 20762306a36Sopenharmony_ci#define ARM_SMMU_FSR_TF BIT(1) 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci#define ARM_SMMU_FSR_IGN (ARM_SMMU_FSR_AFF | \ 21062306a36Sopenharmony_ci ARM_SMMU_FSR_ASF | \ 21162306a36Sopenharmony_ci ARM_SMMU_FSR_TLBMCF | \ 21262306a36Sopenharmony_ci ARM_SMMU_FSR_TLBLKF) 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci#define ARM_SMMU_FSR_FAULT (ARM_SMMU_FSR_MULTI | \ 21562306a36Sopenharmony_ci ARM_SMMU_FSR_SS | \ 21662306a36Sopenharmony_ci ARM_SMMU_FSR_UUT | \ 21762306a36Sopenharmony_ci ARM_SMMU_FSR_EF | \ 21862306a36Sopenharmony_ci ARM_SMMU_FSR_PF | \ 21962306a36Sopenharmony_ci ARM_SMMU_FSR_TF | \ 22062306a36Sopenharmony_ci ARM_SMMU_FSR_IGN) 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci#define ARM_SMMU_CB_FAR 0x60 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci#define ARM_SMMU_CB_FSYNR0 0x68 22562306a36Sopenharmony_ci#define ARM_SMMU_FSYNR0_WNR BIT(4) 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci#define ARM_SMMU_CB_FSYNR1 0x6c 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci#define ARM_SMMU_CB_S1_TLBIVA 0x600 23062306a36Sopenharmony_ci#define ARM_SMMU_CB_S1_TLBIASID 0x610 23162306a36Sopenharmony_ci#define ARM_SMMU_CB_S1_TLBIVAL 0x620 23262306a36Sopenharmony_ci#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630 23362306a36Sopenharmony_ci#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638 23462306a36Sopenharmony_ci#define ARM_SMMU_CB_TLBSYNC 0x7f0 23562306a36Sopenharmony_ci#define ARM_SMMU_CB_TLBSTATUS 0x7f4 23662306a36Sopenharmony_ci#define ARM_SMMU_CB_ATS1PR 0x800 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci#define ARM_SMMU_CB_ATSR 0x8f0 23962306a36Sopenharmony_ci#define ARM_SMMU_ATSR_ACTIVE BIT(0) 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci/* Maximum number of context banks per SMMU */ 24362306a36Sopenharmony_ci#define ARM_SMMU_MAX_CBS 128 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */ 24662306a36Sopenharmony_ci#define TLB_SPIN_COUNT 10 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci/* Shared driver definitions */ 24962306a36Sopenharmony_cienum arm_smmu_arch_version { 25062306a36Sopenharmony_ci ARM_SMMU_V1, 25162306a36Sopenharmony_ci ARM_SMMU_V1_64K, 25262306a36Sopenharmony_ci ARM_SMMU_V2, 25362306a36Sopenharmony_ci}; 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_cienum arm_smmu_implementation { 25662306a36Sopenharmony_ci GENERIC_SMMU, 25762306a36Sopenharmony_ci ARM_MMU500, 25862306a36Sopenharmony_ci CAVIUM_SMMUV2, 25962306a36Sopenharmony_ci QCOM_SMMUV2, 26062306a36Sopenharmony_ci}; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_cistruct arm_smmu_s2cr { 26362306a36Sopenharmony_ci struct iommu_group *group; 26462306a36Sopenharmony_ci int count; 26562306a36Sopenharmony_ci enum arm_smmu_s2cr_type type; 26662306a36Sopenharmony_ci enum arm_smmu_s2cr_privcfg privcfg; 26762306a36Sopenharmony_ci u8 cbndx; 26862306a36Sopenharmony_ci}; 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_cistruct arm_smmu_smr { 27162306a36Sopenharmony_ci u16 mask; 27262306a36Sopenharmony_ci u16 id; 27362306a36Sopenharmony_ci bool valid; 27462306a36Sopenharmony_ci bool pinned; 27562306a36Sopenharmony_ci}; 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_cistruct arm_smmu_device { 27862306a36Sopenharmony_ci struct device *dev; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci void __iomem *base; 28162306a36Sopenharmony_ci phys_addr_t ioaddr; 28262306a36Sopenharmony_ci unsigned int numpage; 28362306a36Sopenharmony_ci unsigned int pgshift; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0) 28662306a36Sopenharmony_ci#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1) 28762306a36Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2) 28862306a36Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3) 28962306a36Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4) 29062306a36Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5) 29162306a36Sopenharmony_ci#define ARM_SMMU_FEAT_VMID16 (1 << 6) 29262306a36Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7) 29362306a36Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8) 29462306a36Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9) 29562306a36Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10) 29662306a36Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11) 29762306a36Sopenharmony_ci#define ARM_SMMU_FEAT_EXIDS (1 << 12) 29862306a36Sopenharmony_ci u32 features; 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci enum arm_smmu_arch_version version; 30162306a36Sopenharmony_ci enum arm_smmu_implementation model; 30262306a36Sopenharmony_ci const struct arm_smmu_impl *impl; 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ci u32 num_context_banks; 30562306a36Sopenharmony_ci u32 num_s2_context_banks; 30662306a36Sopenharmony_ci DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS); 30762306a36Sopenharmony_ci struct arm_smmu_cb *cbs; 30862306a36Sopenharmony_ci atomic_t irptndx; 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci u32 num_mapping_groups; 31162306a36Sopenharmony_ci u16 streamid_mask; 31262306a36Sopenharmony_ci u16 smr_mask_mask; 31362306a36Sopenharmony_ci struct arm_smmu_smr *smrs; 31462306a36Sopenharmony_ci struct arm_smmu_s2cr *s2crs; 31562306a36Sopenharmony_ci struct mutex stream_map_mutex; 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci unsigned long va_size; 31862306a36Sopenharmony_ci unsigned long ipa_size; 31962306a36Sopenharmony_ci unsigned long pa_size; 32062306a36Sopenharmony_ci unsigned long pgsize_bitmap; 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci int num_context_irqs; 32362306a36Sopenharmony_ci int num_clks; 32462306a36Sopenharmony_ci unsigned int *irqs; 32562306a36Sopenharmony_ci struct clk_bulk_data *clks; 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci spinlock_t global_sync_lock; 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci /* IOMMU core code handle */ 33062306a36Sopenharmony_ci struct iommu_device iommu; 33162306a36Sopenharmony_ci}; 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_cienum arm_smmu_context_fmt { 33462306a36Sopenharmony_ci ARM_SMMU_CTX_FMT_NONE, 33562306a36Sopenharmony_ci ARM_SMMU_CTX_FMT_AARCH64, 33662306a36Sopenharmony_ci ARM_SMMU_CTX_FMT_AARCH32_L, 33762306a36Sopenharmony_ci ARM_SMMU_CTX_FMT_AARCH32_S, 33862306a36Sopenharmony_ci}; 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_cistruct arm_smmu_cfg { 34162306a36Sopenharmony_ci u8 cbndx; 34262306a36Sopenharmony_ci u8 irptndx; 34362306a36Sopenharmony_ci union { 34462306a36Sopenharmony_ci u16 asid; 34562306a36Sopenharmony_ci u16 vmid; 34662306a36Sopenharmony_ci }; 34762306a36Sopenharmony_ci enum arm_smmu_cbar_type cbar; 34862306a36Sopenharmony_ci enum arm_smmu_context_fmt fmt; 34962306a36Sopenharmony_ci bool flush_walk_prefer_tlbiasid; 35062306a36Sopenharmony_ci}; 35162306a36Sopenharmony_ci#define ARM_SMMU_INVALID_IRPTNDX 0xff 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_cistruct arm_smmu_cb { 35462306a36Sopenharmony_ci u64 ttbr[2]; 35562306a36Sopenharmony_ci u32 tcr[2]; 35662306a36Sopenharmony_ci u32 mair[2]; 35762306a36Sopenharmony_ci struct arm_smmu_cfg *cfg; 35862306a36Sopenharmony_ci}; 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_cienum arm_smmu_domain_stage { 36162306a36Sopenharmony_ci ARM_SMMU_DOMAIN_S1 = 0, 36262306a36Sopenharmony_ci ARM_SMMU_DOMAIN_S2, 36362306a36Sopenharmony_ci ARM_SMMU_DOMAIN_NESTED, 36462306a36Sopenharmony_ci ARM_SMMU_DOMAIN_BYPASS, 36562306a36Sopenharmony_ci}; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_cistruct arm_smmu_domain { 36862306a36Sopenharmony_ci struct arm_smmu_device *smmu; 36962306a36Sopenharmony_ci struct io_pgtable_ops *pgtbl_ops; 37062306a36Sopenharmony_ci unsigned long pgtbl_quirks; 37162306a36Sopenharmony_ci const struct iommu_flush_ops *flush_ops; 37262306a36Sopenharmony_ci struct arm_smmu_cfg cfg; 37362306a36Sopenharmony_ci enum arm_smmu_domain_stage stage; 37462306a36Sopenharmony_ci struct mutex init_mutex; /* Protects smmu pointer */ 37562306a36Sopenharmony_ci spinlock_t cb_lock; /* Serialises ATS1* ops and TLB syncs */ 37662306a36Sopenharmony_ci struct iommu_domain domain; 37762306a36Sopenharmony_ci}; 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_cistruct arm_smmu_master_cfg { 38062306a36Sopenharmony_ci struct arm_smmu_device *smmu; 38162306a36Sopenharmony_ci s16 smendx[]; 38262306a36Sopenharmony_ci}; 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_cistatic inline u32 arm_smmu_lpae_tcr(const struct io_pgtable_cfg *cfg) 38562306a36Sopenharmony_ci{ 38662306a36Sopenharmony_ci u32 tcr = FIELD_PREP(ARM_SMMU_TCR_TG0, cfg->arm_lpae_s1_cfg.tcr.tg) | 38762306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_TCR_SH0, cfg->arm_lpae_s1_cfg.tcr.sh) | 38862306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_TCR_ORGN0, cfg->arm_lpae_s1_cfg.tcr.orgn) | 38962306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_TCR_IRGN0, cfg->arm_lpae_s1_cfg.tcr.irgn) | 39062306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_TCR_T0SZ, cfg->arm_lpae_s1_cfg.tcr.tsz); 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci /* 39362306a36Sopenharmony_ci * When TTBR1 is selected shift the TCR fields by 16 bits and disable 39462306a36Sopenharmony_ci * translation in TTBR0 39562306a36Sopenharmony_ci */ 39662306a36Sopenharmony_ci if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) { 39762306a36Sopenharmony_ci tcr = (tcr << 16) & ~ARM_SMMU_TCR_A1; 39862306a36Sopenharmony_ci tcr |= ARM_SMMU_TCR_EPD0; 39962306a36Sopenharmony_ci } else 40062306a36Sopenharmony_ci tcr |= ARM_SMMU_TCR_EPD1; 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci return tcr; 40362306a36Sopenharmony_ci} 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_cistatic inline u32 arm_smmu_lpae_tcr2(const struct io_pgtable_cfg *cfg) 40662306a36Sopenharmony_ci{ 40762306a36Sopenharmony_ci return FIELD_PREP(ARM_SMMU_TCR2_PASIZE, cfg->arm_lpae_s1_cfg.tcr.ips) | 40862306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_TCR2_SEP, ARM_SMMU_TCR2_SEP_UPSTREAM); 40962306a36Sopenharmony_ci} 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_cistatic inline u32 arm_smmu_lpae_vtcr(const struct io_pgtable_cfg *cfg) 41262306a36Sopenharmony_ci{ 41362306a36Sopenharmony_ci return ARM_SMMU_VTCR_RES1 | 41462306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_PS, cfg->arm_lpae_s2_cfg.vtcr.ps) | 41562306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_TG0, cfg->arm_lpae_s2_cfg.vtcr.tg) | 41662306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_SH0, cfg->arm_lpae_s2_cfg.vtcr.sh) | 41762306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_ORGN0, cfg->arm_lpae_s2_cfg.vtcr.orgn) | 41862306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_IRGN0, cfg->arm_lpae_s2_cfg.vtcr.irgn) | 41962306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_SL0, cfg->arm_lpae_s2_cfg.vtcr.sl) | 42062306a36Sopenharmony_ci FIELD_PREP(ARM_SMMU_VTCR_T0SZ, cfg->arm_lpae_s2_cfg.vtcr.tsz); 42162306a36Sopenharmony_ci} 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci/* Implementation details, yay! */ 42462306a36Sopenharmony_cistruct arm_smmu_impl { 42562306a36Sopenharmony_ci u32 (*read_reg)(struct arm_smmu_device *smmu, int page, int offset); 42662306a36Sopenharmony_ci void (*write_reg)(struct arm_smmu_device *smmu, int page, int offset, 42762306a36Sopenharmony_ci u32 val); 42862306a36Sopenharmony_ci u64 (*read_reg64)(struct arm_smmu_device *smmu, int page, int offset); 42962306a36Sopenharmony_ci void (*write_reg64)(struct arm_smmu_device *smmu, int page, int offset, 43062306a36Sopenharmony_ci u64 val); 43162306a36Sopenharmony_ci int (*cfg_probe)(struct arm_smmu_device *smmu); 43262306a36Sopenharmony_ci int (*reset)(struct arm_smmu_device *smmu); 43362306a36Sopenharmony_ci int (*init_context)(struct arm_smmu_domain *smmu_domain, 43462306a36Sopenharmony_ci struct io_pgtable_cfg *cfg, struct device *dev); 43562306a36Sopenharmony_ci void (*tlb_sync)(struct arm_smmu_device *smmu, int page, int sync, 43662306a36Sopenharmony_ci int status); 43762306a36Sopenharmony_ci int (*def_domain_type)(struct device *dev); 43862306a36Sopenharmony_ci irqreturn_t (*global_fault)(int irq, void *dev); 43962306a36Sopenharmony_ci irqreturn_t (*context_fault)(int irq, void *dev); 44062306a36Sopenharmony_ci int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain, 44162306a36Sopenharmony_ci struct arm_smmu_device *smmu, 44262306a36Sopenharmony_ci struct device *dev, int start); 44362306a36Sopenharmony_ci void (*write_s2cr)(struct arm_smmu_device *smmu, int idx); 44462306a36Sopenharmony_ci void (*write_sctlr)(struct arm_smmu_device *smmu, int idx, u32 reg); 44562306a36Sopenharmony_ci void (*probe_finalize)(struct arm_smmu_device *smmu, struct device *dev); 44662306a36Sopenharmony_ci}; 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_ci#define INVALID_SMENDX -1 44962306a36Sopenharmony_ci#define cfg_smendx(cfg, fw, i) \ 45062306a36Sopenharmony_ci (i >= fw->num_ids ? INVALID_SMENDX : cfg->smendx[i]) 45162306a36Sopenharmony_ci#define for_each_cfg_sme(cfg, fw, i, idx) \ 45262306a36Sopenharmony_ci for (i = 0; idx = cfg_smendx(cfg, fw, i), i < fw->num_ids; ++i) 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_cistatic inline int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end) 45562306a36Sopenharmony_ci{ 45662306a36Sopenharmony_ci int idx; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci do { 45962306a36Sopenharmony_ci idx = find_next_zero_bit(map, end, start); 46062306a36Sopenharmony_ci if (idx == end) 46162306a36Sopenharmony_ci return -ENOSPC; 46262306a36Sopenharmony_ci } while (test_and_set_bit(idx, map)); 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ci return idx; 46562306a36Sopenharmony_ci} 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_cistatic inline void __iomem *arm_smmu_page(struct arm_smmu_device *smmu, int n) 46862306a36Sopenharmony_ci{ 46962306a36Sopenharmony_ci return smmu->base + (n << smmu->pgshift); 47062306a36Sopenharmony_ci} 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_cistatic inline u32 arm_smmu_readl(struct arm_smmu_device *smmu, int page, int offset) 47362306a36Sopenharmony_ci{ 47462306a36Sopenharmony_ci if (smmu->impl && unlikely(smmu->impl->read_reg)) 47562306a36Sopenharmony_ci return smmu->impl->read_reg(smmu, page, offset); 47662306a36Sopenharmony_ci return readl_relaxed(arm_smmu_page(smmu, page) + offset); 47762306a36Sopenharmony_ci} 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_cistatic inline void arm_smmu_writel(struct arm_smmu_device *smmu, int page, 48062306a36Sopenharmony_ci int offset, u32 val) 48162306a36Sopenharmony_ci{ 48262306a36Sopenharmony_ci if (smmu->impl && unlikely(smmu->impl->write_reg)) 48362306a36Sopenharmony_ci smmu->impl->write_reg(smmu, page, offset, val); 48462306a36Sopenharmony_ci else 48562306a36Sopenharmony_ci writel_relaxed(val, arm_smmu_page(smmu, page) + offset); 48662306a36Sopenharmony_ci} 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_cistatic inline u64 arm_smmu_readq(struct arm_smmu_device *smmu, int page, int offset) 48962306a36Sopenharmony_ci{ 49062306a36Sopenharmony_ci if (smmu->impl && unlikely(smmu->impl->read_reg64)) 49162306a36Sopenharmony_ci return smmu->impl->read_reg64(smmu, page, offset); 49262306a36Sopenharmony_ci return readq_relaxed(arm_smmu_page(smmu, page) + offset); 49362306a36Sopenharmony_ci} 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_cistatic inline void arm_smmu_writeq(struct arm_smmu_device *smmu, int page, 49662306a36Sopenharmony_ci int offset, u64 val) 49762306a36Sopenharmony_ci{ 49862306a36Sopenharmony_ci if (smmu->impl && unlikely(smmu->impl->write_reg64)) 49962306a36Sopenharmony_ci smmu->impl->write_reg64(smmu, page, offset, val); 50062306a36Sopenharmony_ci else 50162306a36Sopenharmony_ci writeq_relaxed(val, arm_smmu_page(smmu, page) + offset); 50262306a36Sopenharmony_ci} 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ci#define ARM_SMMU_GR0 0 50562306a36Sopenharmony_ci#define ARM_SMMU_GR1 1 50662306a36Sopenharmony_ci#define ARM_SMMU_CB(s, n) ((s)->numpage + (n)) 50762306a36Sopenharmony_ci 50862306a36Sopenharmony_ci#define arm_smmu_gr0_read(s, o) \ 50962306a36Sopenharmony_ci arm_smmu_readl((s), ARM_SMMU_GR0, (o)) 51062306a36Sopenharmony_ci#define arm_smmu_gr0_write(s, o, v) \ 51162306a36Sopenharmony_ci arm_smmu_writel((s), ARM_SMMU_GR0, (o), (v)) 51262306a36Sopenharmony_ci 51362306a36Sopenharmony_ci#define arm_smmu_gr1_read(s, o) \ 51462306a36Sopenharmony_ci arm_smmu_readl((s), ARM_SMMU_GR1, (o)) 51562306a36Sopenharmony_ci#define arm_smmu_gr1_write(s, o, v) \ 51662306a36Sopenharmony_ci arm_smmu_writel((s), ARM_SMMU_GR1, (o), (v)) 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ci#define arm_smmu_cb_read(s, n, o) \ 51962306a36Sopenharmony_ci arm_smmu_readl((s), ARM_SMMU_CB((s), (n)), (o)) 52062306a36Sopenharmony_ci#define arm_smmu_cb_write(s, n, o, v) \ 52162306a36Sopenharmony_ci arm_smmu_writel((s), ARM_SMMU_CB((s), (n)), (o), (v)) 52262306a36Sopenharmony_ci#define arm_smmu_cb_readq(s, n, o) \ 52362306a36Sopenharmony_ci arm_smmu_readq((s), ARM_SMMU_CB((s), (n)), (o)) 52462306a36Sopenharmony_ci#define arm_smmu_cb_writeq(s, n, o, v) \ 52562306a36Sopenharmony_ci arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v)) 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_cistruct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu); 52862306a36Sopenharmony_cistruct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu); 52962306a36Sopenharmony_cistruct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu); 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_civoid arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx); 53262306a36Sopenharmony_ciint arm_mmu500_reset(struct arm_smmu_device *smmu); 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ci#endif /* _ARM_SMMU_H */ 535