18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * IOMMU API for ARM architected SMMU implementations.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 ARM Limited
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Will Deacon <will.deacon@arm.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _ARM_SMMU_H
118c2ecf20Sopenharmony_ci#define _ARM_SMMU_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/atomic.h>
148c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
158c2ecf20Sopenharmony_ci#include <linux/bits.h>
168c2ecf20Sopenharmony_ci#include <linux/clk.h>
178c2ecf20Sopenharmony_ci#include <linux/device.h>
188c2ecf20Sopenharmony_ci#include <linux/io-64-nonatomic-hi-lo.h>
198c2ecf20Sopenharmony_ci#include <linux/io-pgtable.h>
208c2ecf20Sopenharmony_ci#include <linux/iommu.h>
218c2ecf20Sopenharmony_ci#include <linux/irqreturn.h>
228c2ecf20Sopenharmony_ci#include <linux/mutex.h>
238c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
248c2ecf20Sopenharmony_ci#include <linux/types.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Configuration registers */
278c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sCR0		0x0
288c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_VMID16EN		BIT(31)
298c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_BSU		GENMASK(15, 14)
308c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_FB		BIT(13)
318c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_PTM		BIT(12)
328c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_VMIDPNE		BIT(11)
338c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_USFCFG		BIT(10)
348c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_GCFGFIE		BIT(5)
358c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_GCFGFRE		BIT(4)
368c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_EXIDENABLE	BIT(3)
378c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_GFIE		BIT(2)
388c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_GFRE		BIT(1)
398c2ecf20Sopenharmony_ci#define ARM_SMMU_sCR0_CLIENTPD		BIT(0)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Auxiliary Configuration register */
428c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sACR		0x10
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Identification registers */
458c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID0		0x20
468c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_S1TS		BIT(30)
478c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_S2TS		BIT(29)
488c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_NTS		BIT(28)
498c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_SMS		BIT(27)
508c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_ATOSNS		BIT(26)
518c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_PTFS_NO_AARCH32	BIT(25)
528c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_PTFS_NO_AARCH32S	BIT(24)
538c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_NUMIRPT		GENMASK(23, 16)
548c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_CTTW		BIT(14)
558c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_NUMSIDB		GENMASK(12, 9)
568c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_EXIDS		BIT(8)
578c2ecf20Sopenharmony_ci#define ARM_SMMU_ID0_NUMSMRG		GENMASK(7, 0)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID1		0x24
608c2ecf20Sopenharmony_ci#define ARM_SMMU_ID1_PAGESIZE		BIT(31)
618c2ecf20Sopenharmony_ci#define ARM_SMMU_ID1_NUMPAGENDXB	GENMASK(30, 28)
628c2ecf20Sopenharmony_ci#define ARM_SMMU_ID1_NUMS2CB		GENMASK(23, 16)
638c2ecf20Sopenharmony_ci#define ARM_SMMU_ID1_NUMCB		GENMASK(7, 0)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID2		0x28
668c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_VMID16		BIT(15)
678c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_PTFS_64K		BIT(14)
688c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_PTFS_16K		BIT(13)
698c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_PTFS_4K		BIT(12)
708c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_UBS		GENMASK(11, 8)
718c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_OAS		GENMASK(7, 4)
728c2ecf20Sopenharmony_ci#define ARM_SMMU_ID2_IAS		GENMASK(3, 0)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID3		0x2c
758c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID4		0x30
768c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID5		0x34
778c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID6		0x38
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_ID7		0x3c
808c2ecf20Sopenharmony_ci#define ARM_SMMU_ID7_MAJOR		GENMASK(7, 4)
818c2ecf20Sopenharmony_ci#define ARM_SMMU_ID7_MINOR		GENMASK(3, 0)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sGFSR		0x48
848c2ecf20Sopenharmony_ci#define ARM_SMMU_sGFSR_USF		BIT(1)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sGFSYNR0		0x50
878c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sGFSYNR1		0x54
888c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sGFSYNR2		0x58
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* Global TLB invalidation */
918c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_TLBIVMID		0x64
928c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_TLBIALLNSNH	0x68
938c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_TLBIALLH		0x6c
948c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sTLBGSYNC		0x70
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_sTLBGSTATUS	0x74
978c2ecf20Sopenharmony_ci#define ARM_SMMU_sTLBGSTATUS_GSACTIVE	BIT(0)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* Stream mapping registers */
1008c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_SMR(n)		(0x800 + ((n) << 2))
1018c2ecf20Sopenharmony_ci#define ARM_SMMU_SMR_VALID		BIT(31)
1028c2ecf20Sopenharmony_ci#define ARM_SMMU_SMR_MASK		GENMASK(31, 16)
1038c2ecf20Sopenharmony_ci#define ARM_SMMU_SMR_ID			GENMASK(15, 0)
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0_S2CR(n)		(0xc00 + ((n) << 2))
1068c2ecf20Sopenharmony_ci#define ARM_SMMU_S2CR_PRIVCFG		GENMASK(25, 24)
1078c2ecf20Sopenharmony_cienum arm_smmu_s2cr_privcfg {
1088c2ecf20Sopenharmony_ci	S2CR_PRIVCFG_DEFAULT,
1098c2ecf20Sopenharmony_ci	S2CR_PRIVCFG_DIPAN,
1108c2ecf20Sopenharmony_ci	S2CR_PRIVCFG_UNPRIV,
1118c2ecf20Sopenharmony_ci	S2CR_PRIVCFG_PRIV,
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci#define ARM_SMMU_S2CR_TYPE		GENMASK(17, 16)
1148c2ecf20Sopenharmony_cienum arm_smmu_s2cr_type {
1158c2ecf20Sopenharmony_ci	S2CR_TYPE_TRANS,
1168c2ecf20Sopenharmony_ci	S2CR_TYPE_BYPASS,
1178c2ecf20Sopenharmony_ci	S2CR_TYPE_FAULT,
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci#define ARM_SMMU_S2CR_EXIDVALID		BIT(10)
1208c2ecf20Sopenharmony_ci#define ARM_SMMU_S2CR_CBNDX		GENMASK(7, 0)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/* Context bank attribute registers */
1238c2ecf20Sopenharmony_ci#define ARM_SMMU_GR1_CBAR(n)		(0x0 + ((n) << 2))
1248c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_IRPTNDX		GENMASK(31, 24)
1258c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_TYPE		GENMASK(17, 16)
1268c2ecf20Sopenharmony_cienum arm_smmu_cbar_type {
1278c2ecf20Sopenharmony_ci	CBAR_TYPE_S2_TRANS,
1288c2ecf20Sopenharmony_ci	CBAR_TYPE_S1_TRANS_S2_BYPASS,
1298c2ecf20Sopenharmony_ci	CBAR_TYPE_S1_TRANS_S2_FAULT,
1308c2ecf20Sopenharmony_ci	CBAR_TYPE_S1_TRANS_S2_TRANS,
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_S1_MEMATTR	GENMASK(15, 12)
1338c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_S1_MEMATTR_WB	0xf
1348c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_S1_BPSHCFG	GENMASK(9, 8)
1358c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_S1_BPSHCFG_NSH	3
1368c2ecf20Sopenharmony_ci#define ARM_SMMU_CBAR_VMID		GENMASK(7, 0)
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define ARM_SMMU_GR1_CBFRSYNRA(n)	(0x400 + ((n) << 2))
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#define ARM_SMMU_GR1_CBA2R(n)		(0x800 + ((n) << 2))
1418c2ecf20Sopenharmony_ci#define ARM_SMMU_CBA2R_VMID16		GENMASK(31, 16)
1428c2ecf20Sopenharmony_ci#define ARM_SMMU_CBA2R_VA64		BIT(0)
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_SCTLR		0x0
1458c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_S1_ASIDPNE	BIT(12)
1468c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_CFCFG		BIT(7)
1478c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_CFIE		BIT(6)
1488c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_CFRE		BIT(5)
1498c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_E		BIT(4)
1508c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_AFE		BIT(2)
1518c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_TRE		BIT(1)
1528c2ecf20Sopenharmony_ci#define ARM_SMMU_SCTLR_M		BIT(0)
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_ACTLR		0x4
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_RESUME		0x8
1578c2ecf20Sopenharmony_ci#define ARM_SMMU_RESUME_TERMINATE	BIT(0)
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_TCR2		0x10
1608c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR2_SEP		GENMASK(17, 15)
1618c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR2_SEP_UPSTREAM	0x7
1628c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR2_AS		BIT(4)
1638c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR2_PASIZE		GENMASK(3, 0)
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_TTBR0		0x20
1668c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_TTBR1		0x28
1678c2ecf20Sopenharmony_ci#define ARM_SMMU_TTBRn_ASID		GENMASK_ULL(63, 48)
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_TCR			0x30
1708c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_EAE		BIT(31)
1718c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_EPD1		BIT(23)
1728c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_A1			BIT(22)
1738c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_TG0		GENMASK(15, 14)
1748c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_SH0		GENMASK(13, 12)
1758c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_ORGN0		GENMASK(11, 10)
1768c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_IRGN0		GENMASK(9, 8)
1778c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_EPD0		BIT(7)
1788c2ecf20Sopenharmony_ci#define ARM_SMMU_TCR_T0SZ		GENMASK(5, 0)
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_RES1		BIT(31)
1818c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_PS		GENMASK(18, 16)
1828c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_TG0		ARM_SMMU_TCR_TG0
1838c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_SH0		ARM_SMMU_TCR_SH0
1848c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_ORGN0		ARM_SMMU_TCR_ORGN0
1858c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_IRGN0		ARM_SMMU_TCR_IRGN0
1868c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_SL0		GENMASK(7, 6)
1878c2ecf20Sopenharmony_ci#define ARM_SMMU_VTCR_T0SZ		ARM_SMMU_TCR_T0SZ
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_CONTEXTIDR		0x34
1908c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S1_MAIR0		0x38
1918c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S1_MAIR1		0x3c
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_PAR			0x50
1948c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_PAR_F		BIT(0)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_FSR			0x58
1978c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_MULTI		BIT(31)
1988c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_SS			BIT(30)
1998c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_UUT		BIT(8)
2008c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_ASF		BIT(7)
2018c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_TLBLKF		BIT(6)
2028c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_TLBMCF		BIT(5)
2038c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_EF			BIT(4)
2048c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_PF			BIT(3)
2058c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_AFF		BIT(2)
2068c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_TF			BIT(1)
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_IGN		(ARM_SMMU_FSR_AFF |		\
2098c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_ASF |		\
2108c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_TLBMCF |		\
2118c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_TLBLKF)
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define ARM_SMMU_FSR_FAULT		(ARM_SMMU_FSR_MULTI |		\
2148c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_SS |		\
2158c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_UUT |		\
2168c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_EF |		\
2178c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_PF |		\
2188c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_TF |		\
2198c2ecf20Sopenharmony_ci					 ARM_SMMU_FSR_IGN)
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_FAR			0x60
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_FSYNR0		0x68
2248c2ecf20Sopenharmony_ci#define ARM_SMMU_FSYNR0_WNR		BIT(4)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S1_TLBIVA		0x600
2278c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S1_TLBIASID		0x610
2288c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S1_TLBIVAL		0x620
2298c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S2_TLBIIPAS2	0x630
2308c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_S2_TLBIIPAS2L	0x638
2318c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_TLBSYNC		0x7f0
2328c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_TLBSTATUS		0x7f4
2338c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_ATS1PR		0x800
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define ARM_SMMU_CB_ATSR		0x8f0
2368c2ecf20Sopenharmony_ci#define ARM_SMMU_ATSR_ACTIVE		BIT(0)
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* Maximum number of context banks per SMMU */
2408c2ecf20Sopenharmony_ci#define ARM_SMMU_MAX_CBS		128
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci#define TLB_LOOP_TIMEOUT		1000000	/* 1s! */
2438c2ecf20Sopenharmony_ci#define TLB_SPIN_COUNT			10
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci/* Shared driver definitions */
2468c2ecf20Sopenharmony_cienum arm_smmu_arch_version {
2478c2ecf20Sopenharmony_ci	ARM_SMMU_V1,
2488c2ecf20Sopenharmony_ci	ARM_SMMU_V1_64K,
2498c2ecf20Sopenharmony_ci	ARM_SMMU_V2,
2508c2ecf20Sopenharmony_ci};
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cienum arm_smmu_implementation {
2538c2ecf20Sopenharmony_ci	GENERIC_SMMU,
2548c2ecf20Sopenharmony_ci	ARM_MMU500,
2558c2ecf20Sopenharmony_ci	CAVIUM_SMMUV2,
2568c2ecf20Sopenharmony_ci	QCOM_SMMUV2,
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistruct arm_smmu_s2cr {
2608c2ecf20Sopenharmony_ci	struct iommu_group		*group;
2618c2ecf20Sopenharmony_ci	int				count;
2628c2ecf20Sopenharmony_ci	enum arm_smmu_s2cr_type		type;
2638c2ecf20Sopenharmony_ci	enum arm_smmu_s2cr_privcfg	privcfg;
2648c2ecf20Sopenharmony_ci	u8				cbndx;
2658c2ecf20Sopenharmony_ci};
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistruct arm_smmu_smr {
2688c2ecf20Sopenharmony_ci	u16				mask;
2698c2ecf20Sopenharmony_ci	u16				id;
2708c2ecf20Sopenharmony_ci	bool				valid;
2718c2ecf20Sopenharmony_ci	bool				pinned;
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistruct arm_smmu_device {
2758c2ecf20Sopenharmony_ci	struct device			*dev;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	void __iomem			*base;
2788c2ecf20Sopenharmony_ci	unsigned int			numpage;
2798c2ecf20Sopenharmony_ci	unsigned int			pgshift;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_COHERENT_WALK	(1 << 0)
2828c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_STREAM_MATCH	(1 << 1)
2838c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_S1		(1 << 2)
2848c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_S2		(1 << 3)
2858c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_NESTED	(1 << 4)
2868c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_TRANS_OPS		(1 << 5)
2878c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_VMID16		(1 << 6)
2888c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH64_4K	(1 << 7)
2898c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH64_16K	(1 << 8)
2908c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH64_64K	(1 << 9)
2918c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH32_L	(1 << 10)
2928c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_FMT_AARCH32_S	(1 << 11)
2938c2ecf20Sopenharmony_ci#define ARM_SMMU_FEAT_EXIDS		(1 << 12)
2948c2ecf20Sopenharmony_ci	u32				features;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	enum arm_smmu_arch_version	version;
2978c2ecf20Sopenharmony_ci	enum arm_smmu_implementation	model;
2988c2ecf20Sopenharmony_ci	const struct arm_smmu_impl	*impl;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	u32				num_context_banks;
3018c2ecf20Sopenharmony_ci	u32				num_s2_context_banks;
3028c2ecf20Sopenharmony_ci	DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
3038c2ecf20Sopenharmony_ci	struct arm_smmu_cb		*cbs;
3048c2ecf20Sopenharmony_ci	atomic_t			irptndx;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	u32				num_mapping_groups;
3078c2ecf20Sopenharmony_ci	u16				streamid_mask;
3088c2ecf20Sopenharmony_ci	u16				smr_mask_mask;
3098c2ecf20Sopenharmony_ci	struct arm_smmu_smr		*smrs;
3108c2ecf20Sopenharmony_ci	struct arm_smmu_s2cr		*s2crs;
3118c2ecf20Sopenharmony_ci	struct mutex			stream_map_mutex;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	unsigned long			va_size;
3148c2ecf20Sopenharmony_ci	unsigned long			ipa_size;
3158c2ecf20Sopenharmony_ci	unsigned long			pa_size;
3168c2ecf20Sopenharmony_ci	unsigned long			pgsize_bitmap;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	u32				num_global_irqs;
3198c2ecf20Sopenharmony_ci	u32				num_context_irqs;
3208c2ecf20Sopenharmony_ci	unsigned int			*irqs;
3218c2ecf20Sopenharmony_ci	struct clk_bulk_data		*clks;
3228c2ecf20Sopenharmony_ci	int				num_clks;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	spinlock_t			global_sync_lock;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	/* IOMMU core code handle */
3278c2ecf20Sopenharmony_ci	struct iommu_device		iommu;
3288c2ecf20Sopenharmony_ci};
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cienum arm_smmu_context_fmt {
3318c2ecf20Sopenharmony_ci	ARM_SMMU_CTX_FMT_NONE,
3328c2ecf20Sopenharmony_ci	ARM_SMMU_CTX_FMT_AARCH64,
3338c2ecf20Sopenharmony_ci	ARM_SMMU_CTX_FMT_AARCH32_L,
3348c2ecf20Sopenharmony_ci	ARM_SMMU_CTX_FMT_AARCH32_S,
3358c2ecf20Sopenharmony_ci};
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistruct arm_smmu_cfg {
3388c2ecf20Sopenharmony_ci	u8				cbndx;
3398c2ecf20Sopenharmony_ci	u8				irptndx;
3408c2ecf20Sopenharmony_ci	union {
3418c2ecf20Sopenharmony_ci		u16			asid;
3428c2ecf20Sopenharmony_ci		u16			vmid;
3438c2ecf20Sopenharmony_ci	};
3448c2ecf20Sopenharmony_ci	enum arm_smmu_cbar_type		cbar;
3458c2ecf20Sopenharmony_ci	enum arm_smmu_context_fmt	fmt;
3468c2ecf20Sopenharmony_ci};
3478c2ecf20Sopenharmony_ci#define ARM_SMMU_INVALID_IRPTNDX	0xff
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistruct arm_smmu_cb {
3508c2ecf20Sopenharmony_ci	u64				ttbr[2];
3518c2ecf20Sopenharmony_ci	u32				tcr[2];
3528c2ecf20Sopenharmony_ci	u32				mair[2];
3538c2ecf20Sopenharmony_ci	struct arm_smmu_cfg		*cfg;
3548c2ecf20Sopenharmony_ci};
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cienum arm_smmu_domain_stage {
3578c2ecf20Sopenharmony_ci	ARM_SMMU_DOMAIN_S1 = 0,
3588c2ecf20Sopenharmony_ci	ARM_SMMU_DOMAIN_S2,
3598c2ecf20Sopenharmony_ci	ARM_SMMU_DOMAIN_NESTED,
3608c2ecf20Sopenharmony_ci	ARM_SMMU_DOMAIN_BYPASS,
3618c2ecf20Sopenharmony_ci};
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistruct arm_smmu_domain {
3648c2ecf20Sopenharmony_ci	struct arm_smmu_device		*smmu;
3658c2ecf20Sopenharmony_ci	struct io_pgtable_ops		*pgtbl_ops;
3668c2ecf20Sopenharmony_ci	const struct iommu_flush_ops	*flush_ops;
3678c2ecf20Sopenharmony_ci	struct arm_smmu_cfg		cfg;
3688c2ecf20Sopenharmony_ci	enum arm_smmu_domain_stage	stage;
3698c2ecf20Sopenharmony_ci	bool				non_strict;
3708c2ecf20Sopenharmony_ci	struct mutex			init_mutex; /* Protects smmu pointer */
3718c2ecf20Sopenharmony_ci	spinlock_t			cb_lock; /* Serialises ATS1* ops and TLB syncs */
3728c2ecf20Sopenharmony_ci	struct iommu_domain		domain;
3738c2ecf20Sopenharmony_ci};
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistruct arm_smmu_master_cfg {
3768c2ecf20Sopenharmony_ci	struct arm_smmu_device		*smmu;
3778c2ecf20Sopenharmony_ci	s16				smendx[];
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic inline u32 arm_smmu_lpae_tcr(const struct io_pgtable_cfg *cfg)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	u32 tcr = FIELD_PREP(ARM_SMMU_TCR_TG0, cfg->arm_lpae_s1_cfg.tcr.tg) |
3838c2ecf20Sopenharmony_ci		FIELD_PREP(ARM_SMMU_TCR_SH0, cfg->arm_lpae_s1_cfg.tcr.sh) |
3848c2ecf20Sopenharmony_ci		FIELD_PREP(ARM_SMMU_TCR_ORGN0, cfg->arm_lpae_s1_cfg.tcr.orgn) |
3858c2ecf20Sopenharmony_ci		FIELD_PREP(ARM_SMMU_TCR_IRGN0, cfg->arm_lpae_s1_cfg.tcr.irgn) |
3868c2ecf20Sopenharmony_ci		FIELD_PREP(ARM_SMMU_TCR_T0SZ, cfg->arm_lpae_s1_cfg.tcr.tsz);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci       /*
3898c2ecf20Sopenharmony_ci	* When TTBR1 is selected shift the TCR fields by 16 bits and disable
3908c2ecf20Sopenharmony_ci	* translation in TTBR0
3918c2ecf20Sopenharmony_ci	*/
3928c2ecf20Sopenharmony_ci	if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) {
3938c2ecf20Sopenharmony_ci		tcr = (tcr << 16) & ~ARM_SMMU_TCR_A1;
3948c2ecf20Sopenharmony_ci		tcr |= ARM_SMMU_TCR_EPD0;
3958c2ecf20Sopenharmony_ci	} else
3968c2ecf20Sopenharmony_ci		tcr |= ARM_SMMU_TCR_EPD1;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return tcr;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic inline u32 arm_smmu_lpae_tcr2(const struct io_pgtable_cfg *cfg)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	return FIELD_PREP(ARM_SMMU_TCR2_PASIZE, cfg->arm_lpae_s1_cfg.tcr.ips) |
4048c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_TCR2_SEP, ARM_SMMU_TCR2_SEP_UPSTREAM);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic inline u32 arm_smmu_lpae_vtcr(const struct io_pgtable_cfg *cfg)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	return ARM_SMMU_VTCR_RES1 |
4108c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_PS, cfg->arm_lpae_s2_cfg.vtcr.ps) |
4118c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_TG0, cfg->arm_lpae_s2_cfg.vtcr.tg) |
4128c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_SH0, cfg->arm_lpae_s2_cfg.vtcr.sh) |
4138c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_ORGN0, cfg->arm_lpae_s2_cfg.vtcr.orgn) |
4148c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_IRGN0, cfg->arm_lpae_s2_cfg.vtcr.irgn) |
4158c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_SL0, cfg->arm_lpae_s2_cfg.vtcr.sl) |
4168c2ecf20Sopenharmony_ci	       FIELD_PREP(ARM_SMMU_VTCR_T0SZ, cfg->arm_lpae_s2_cfg.vtcr.tsz);
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci/* Implementation details, yay! */
4208c2ecf20Sopenharmony_cistruct arm_smmu_impl {
4218c2ecf20Sopenharmony_ci	u32 (*read_reg)(struct arm_smmu_device *smmu, int page, int offset);
4228c2ecf20Sopenharmony_ci	void (*write_reg)(struct arm_smmu_device *smmu, int page, int offset,
4238c2ecf20Sopenharmony_ci			  u32 val);
4248c2ecf20Sopenharmony_ci	u64 (*read_reg64)(struct arm_smmu_device *smmu, int page, int offset);
4258c2ecf20Sopenharmony_ci	void (*write_reg64)(struct arm_smmu_device *smmu, int page, int offset,
4268c2ecf20Sopenharmony_ci			    u64 val);
4278c2ecf20Sopenharmony_ci	int (*cfg_probe)(struct arm_smmu_device *smmu);
4288c2ecf20Sopenharmony_ci	int (*reset)(struct arm_smmu_device *smmu);
4298c2ecf20Sopenharmony_ci	int (*init_context)(struct arm_smmu_domain *smmu_domain,
4308c2ecf20Sopenharmony_ci			struct io_pgtable_cfg *cfg, struct device *dev);
4318c2ecf20Sopenharmony_ci	void (*tlb_sync)(struct arm_smmu_device *smmu, int page, int sync,
4328c2ecf20Sopenharmony_ci			 int status);
4338c2ecf20Sopenharmony_ci	int (*def_domain_type)(struct device *dev);
4348c2ecf20Sopenharmony_ci	irqreturn_t (*global_fault)(int irq, void *dev);
4358c2ecf20Sopenharmony_ci	irqreturn_t (*context_fault)(int irq, void *dev);
4368c2ecf20Sopenharmony_ci	int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain,
4378c2ecf20Sopenharmony_ci				  struct arm_smmu_device *smmu,
4388c2ecf20Sopenharmony_ci				  struct device *dev, int start);
4398c2ecf20Sopenharmony_ci	void (*write_s2cr)(struct arm_smmu_device *smmu, int idx);
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci#define INVALID_SMENDX			-1
4438c2ecf20Sopenharmony_ci#define cfg_smendx(cfg, fw, i) \
4448c2ecf20Sopenharmony_ci	(i >= fw->num_ids ? INVALID_SMENDX : cfg->smendx[i])
4458c2ecf20Sopenharmony_ci#define for_each_cfg_sme(cfg, fw, i, idx) \
4468c2ecf20Sopenharmony_ci	for (i = 0; idx = cfg_smendx(cfg, fw, i), i < fw->num_ids; ++i)
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic inline int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	int idx;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	do {
4538c2ecf20Sopenharmony_ci		idx = find_next_zero_bit(map, end, start);
4548c2ecf20Sopenharmony_ci		if (idx == end)
4558c2ecf20Sopenharmony_ci			return -ENOSPC;
4568c2ecf20Sopenharmony_ci	} while (test_and_set_bit(idx, map));
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return idx;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic inline void __iomem *arm_smmu_page(struct arm_smmu_device *smmu, int n)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	return smmu->base + (n << smmu->pgshift);
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic inline u32 arm_smmu_readl(struct arm_smmu_device *smmu, int page, int offset)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	if (smmu->impl && unlikely(smmu->impl->read_reg))
4698c2ecf20Sopenharmony_ci		return smmu->impl->read_reg(smmu, page, offset);
4708c2ecf20Sopenharmony_ci	return readl_relaxed(arm_smmu_page(smmu, page) + offset);
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic inline void arm_smmu_writel(struct arm_smmu_device *smmu, int page,
4748c2ecf20Sopenharmony_ci				   int offset, u32 val)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	if (smmu->impl && unlikely(smmu->impl->write_reg))
4778c2ecf20Sopenharmony_ci		smmu->impl->write_reg(smmu, page, offset, val);
4788c2ecf20Sopenharmony_ci	else
4798c2ecf20Sopenharmony_ci		writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic inline u64 arm_smmu_readq(struct arm_smmu_device *smmu, int page, int offset)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	if (smmu->impl && unlikely(smmu->impl->read_reg64))
4858c2ecf20Sopenharmony_ci		return smmu->impl->read_reg64(smmu, page, offset);
4868c2ecf20Sopenharmony_ci	return readq_relaxed(arm_smmu_page(smmu, page) + offset);
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic inline void arm_smmu_writeq(struct arm_smmu_device *smmu, int page,
4908c2ecf20Sopenharmony_ci				   int offset, u64 val)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	if (smmu->impl && unlikely(smmu->impl->write_reg64))
4938c2ecf20Sopenharmony_ci		smmu->impl->write_reg64(smmu, page, offset, val);
4948c2ecf20Sopenharmony_ci	else
4958c2ecf20Sopenharmony_ci		writeq_relaxed(val, arm_smmu_page(smmu, page) + offset);
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci#define ARM_SMMU_GR0		0
4998c2ecf20Sopenharmony_ci#define ARM_SMMU_GR1		1
5008c2ecf20Sopenharmony_ci#define ARM_SMMU_CB(s, n)	((s)->numpage + (n))
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci#define arm_smmu_gr0_read(s, o)		\
5038c2ecf20Sopenharmony_ci	arm_smmu_readl((s), ARM_SMMU_GR0, (o))
5048c2ecf20Sopenharmony_ci#define arm_smmu_gr0_write(s, o, v)	\
5058c2ecf20Sopenharmony_ci	arm_smmu_writel((s), ARM_SMMU_GR0, (o), (v))
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci#define arm_smmu_gr1_read(s, o)		\
5088c2ecf20Sopenharmony_ci	arm_smmu_readl((s), ARM_SMMU_GR1, (o))
5098c2ecf20Sopenharmony_ci#define arm_smmu_gr1_write(s, o, v)	\
5108c2ecf20Sopenharmony_ci	arm_smmu_writel((s), ARM_SMMU_GR1, (o), (v))
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci#define arm_smmu_cb_read(s, n, o)	\
5138c2ecf20Sopenharmony_ci	arm_smmu_readl((s), ARM_SMMU_CB((s), (n)), (o))
5148c2ecf20Sopenharmony_ci#define arm_smmu_cb_write(s, n, o, v)	\
5158c2ecf20Sopenharmony_ci	arm_smmu_writel((s), ARM_SMMU_CB((s), (n)), (o), (v))
5168c2ecf20Sopenharmony_ci#define arm_smmu_cb_readq(s, n, o)	\
5178c2ecf20Sopenharmony_ci	arm_smmu_readq((s), ARM_SMMU_CB((s), (n)), (o))
5188c2ecf20Sopenharmony_ci#define arm_smmu_cb_writeq(s, n, o, v)	\
5198c2ecf20Sopenharmony_ci	arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v))
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistruct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu);
5228c2ecf20Sopenharmony_cistruct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu);
5238c2ecf20Sopenharmony_cistruct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_civoid arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx);
5268c2ecf20Sopenharmony_ciint arm_mmu500_reset(struct arm_smmu_device *smmu);
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci#endif /* _ARM_SMMU_H */
529