arm-smmu.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * IOMMU API for ARM architected SMMU implementations.
  4. *
  5. * Copyright (C) 2013 ARM Limited
  6. *
  7. * Author: Will Deacon <[email protected]>
  8. *
  9. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  10. */
  11. #ifndef _ARM_SMMU_H
  12. #define _ARM_SMMU_H
  13. #include <linux/atomic.h>
  14. #include <linux/bitfield.h>
  15. #include <linux/bits.h>
  16. #include <linux/clk.h>
  17. #include <linux/device.h>
  18. #include <linux/io-64-nonatomic-hi-lo.h>
  19. #include <linux/io-pgtable.h>
  20. #include <linux/iommu.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/mutex.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/types.h>
  25. #include <linux/qcom-iommu-util.h>
  26. #include <linux/qcom-io-pgtable.h>
  27. /* Configuration registers */
  28. #define ARM_SMMU_GR0_sCR0 0x0
  29. #define ARM_SMMU_sCR0_VMID16EN BIT(31)
  30. #define ARM_SMMU_sCR0_SHCFG GENMASK(23, 22)
  31. #define ARM_SMMU_sCR0_SHCFG_NSH 0x3
  32. #define ARM_SMMU_sCR0_BSU GENMASK(15, 14)
  33. #define ARM_SMMU_sCR0_FB BIT(13)
  34. #define ARM_SMMU_sCR0_PTM BIT(12)
  35. #define ARM_SMMU_sCR0_VMIDPNE BIT(11)
  36. #define ARM_SMMU_sCR0_USFCFG BIT(10)
  37. #define ARM_SMMU_sCR0_GCFGFIE BIT(5)
  38. #define ARM_SMMU_sCR0_GCFGFRE BIT(4)
  39. #define ARM_SMMU_sCR0_EXIDENABLE BIT(3)
  40. #define ARM_SMMU_sCR0_GFIE BIT(2)
  41. #define ARM_SMMU_sCR0_GFRE BIT(1)
  42. #define ARM_SMMU_sCR0_CLIENTPD BIT(0)
  43. /* Auxiliary Configuration register */
  44. #define ARM_SMMU_GR0_sACR 0x10
  45. /* Identification registers */
  46. #define ARM_SMMU_GR0_ID0 0x20
  47. #define ARM_SMMU_ID0_S1TS BIT(30)
  48. #define ARM_SMMU_ID0_S2TS BIT(29)
  49. #define ARM_SMMU_ID0_NTS BIT(28)
  50. #define ARM_SMMU_ID0_SMS BIT(27)
  51. #define ARM_SMMU_ID0_ATOSNS BIT(26)
  52. #define ARM_SMMU_ID0_PTFS_NO_AARCH32 BIT(25)
  53. #define ARM_SMMU_ID0_PTFS_NO_AARCH32S BIT(24)
  54. #define ARM_SMMU_ID0_NUMIRPT GENMASK(23, 16)
  55. #define ARM_SMMU_ID0_CTTW BIT(14)
  56. #define ARM_SMMU_ID0_NUMSIDB GENMASK(12, 9)
  57. #define ARM_SMMU_ID0_EXIDS BIT(8)
  58. #define ARM_SMMU_ID0_NUMSMRG GENMASK(7, 0)
  59. #define ARM_SMMU_GR0_ID1 0x24
  60. #define ARM_SMMU_ID1_PAGESIZE BIT(31)
  61. #define ARM_SMMU_ID1_NUMPAGENDXB GENMASK(30, 28)
  62. #define ARM_SMMU_ID1_NUMS2CB GENMASK(23, 16)
  63. #define ARM_SMMU_ID1_NUMCB GENMASK(7, 0)
  64. #define ARM_SMMU_GR0_ID2 0x28
  65. #define ARM_SMMU_ID2_VMID16 BIT(15)
  66. #define ARM_SMMU_ID2_PTFS_64K BIT(14)
  67. #define ARM_SMMU_ID2_PTFS_16K BIT(13)
  68. #define ARM_SMMU_ID2_PTFS_4K BIT(12)
  69. #define ARM_SMMU_ID2_UBS GENMASK(11, 8)
  70. #define ARM_SMMU_ID2_OAS GENMASK(7, 4)
  71. #define ARM_SMMU_ID2_IAS GENMASK(3, 0)
  72. #define ARM_SMMU_GR0_ID3 0x2c
  73. #define ARM_SMMU_GR0_ID4 0x30
  74. #define ARM_SMMU_GR0_ID5 0x34
  75. #define ARM_SMMU_GR0_ID6 0x38
  76. #define ARM_SMMU_GR0_ID7 0x3c
  77. #define ARM_SMMU_ID7_MAJOR GENMASK(7, 4)
  78. #define ARM_SMMU_ID7_MINOR GENMASK(3, 0)
  79. #define ARM_SMMU_GR0_sGFSR 0x48
  80. #define ARM_SMMU_sGFSR_USF BIT(1)
  81. #define ARM_SMMU_GR0_sGFSYNR0 0x50
  82. #define ARM_SMMU_GR0_sGFSYNR1 0x54
  83. #define ARM_SMMU_GR0_sGFSYNR2 0x58
  84. /* Global TLB invalidation */
  85. #define ARM_SMMU_GR0_TLBIVMID 0x64
  86. #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
  87. #define ARM_SMMU_GR0_TLBIALLH 0x6c
  88. #define ARM_SMMU_GR0_sTLBGSYNC 0x70
  89. #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
  90. #define ARM_SMMU_sTLBGSTATUS_GSACTIVE BIT(0)
  91. /* Stream mapping registers */
  92. #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
  93. #define ARM_SMMU_SMR_VALID BIT(31)
  94. #define ARM_SMMU_SMR_MASK GENMASK(31, 16)
  95. #define ARM_SMMU_SMR_ID GENMASK(15, 0)
  96. #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
  97. #define ARM_SMMU_S2CR_PRIVCFG GENMASK(25, 24)
  98. enum arm_smmu_s2cr_privcfg {
  99. S2CR_PRIVCFG_DEFAULT,
  100. S2CR_PRIVCFG_DIPAN,
  101. S2CR_PRIVCFG_UNPRIV,
  102. S2CR_PRIVCFG_PRIV,
  103. };
  104. #define ARM_SMMU_S2CR_TYPE GENMASK(17, 16)
  105. enum arm_smmu_s2cr_type {
  106. S2CR_TYPE_TRANS,
  107. S2CR_TYPE_BYPASS,
  108. S2CR_TYPE_FAULT,
  109. };
  110. #define ARM_SMMU_S2CR_EXIDVALID BIT(10)
  111. #define ARM_SMMU_S2CR_SHCFG GENMASK(9, 8)
  112. #define ARM_SMMU_S2CR_SHCFG_NSH 0x3
  113. #define ARM_SMMU_S2CR_CBNDX GENMASK(7, 0)
  114. /* Context bank attribute registers */
  115. #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
  116. #define ARM_SMMU_CBAR_IRPTNDX GENMASK(31, 24)
  117. #define ARM_SMMU_CBAR_TYPE GENMASK(17, 16)
  118. enum arm_smmu_cbar_type {
  119. CBAR_TYPE_S2_TRANS,
  120. CBAR_TYPE_S1_TRANS_S2_BYPASS,
  121. CBAR_TYPE_S1_TRANS_S2_FAULT,
  122. CBAR_TYPE_S1_TRANS_S2_TRANS,
  123. };
  124. #define ARM_SMMU_CBAR_S1_MEMATTR GENMASK(15, 12)
  125. #define ARM_SMMU_CBAR_S1_MEMATTR_WB 0xf
  126. #define ARM_SMMU_CBAR_S1_BPSHCFG GENMASK(9, 8)
  127. #define ARM_SMMU_CBAR_S1_BPSHCFG_NSH 3
  128. #define ARM_SMMU_CBAR_VMID GENMASK(7, 0)
  129. #define ARM_SMMU_GR1_CBFRSYNRA(n) (0x400 + ((n) << 2))
  130. #define CBFRSYNRA_SID_MASK (0xffff)
  131. #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
  132. #define ARM_SMMU_CBA2R_VMID16 GENMASK(31, 16)
  133. #define ARM_SMMU_CBA2R_VA64 BIT(0)
  134. #define ARM_SMMU_CB_SCTLR 0x0
  135. #define ARM_SMMU_SCTLR_WACFG GENMASK(27, 26)
  136. #define ARM_SMMU_SCTLR_WACFG_WA 0x2
  137. #define ARM_SMMU_SCTLR_RACFG GENMASK(25, 24)
  138. #define ARM_SMMU_SCTLR_RACFG_RA 0x2
  139. #define ARM_SMMU_SCTLR_SHCFG GENMASK(23, 22)
  140. #define ARM_SMMU_SCTLR_SHCFG_OSH 0x1
  141. #define ARM_SMMU_SCTLR_SHCFG_NSH 0x3
  142. #define ARM_SMMU_SCTLR_MTCFG BIT(20)
  143. #define ARM_SMMU_SCTLR_MEM_ATTR GENMASK(19, 16)
  144. #define ARM_SMMU_SCTLR_MEM_ATTR_OISH_WB_CACHE 0xf
  145. #define ARM_SMMU_SCTLR_S1_ASIDPNE BIT(12)
  146. #define ARM_SMMU_SCTLR_CFCFG BIT(7)
  147. #define ARM_SMMU_SCTLR_HUPCF BIT(8)
  148. #define ARM_SMMU_SCTLR_CFIE BIT(6)
  149. #define ARM_SMMU_SCTLR_CFRE BIT(5)
  150. #define ARM_SMMU_SCTLR_E BIT(4)
  151. #define ARM_SMMU_SCTLR_AFE BIT(2)
  152. #define ARM_SMMU_SCTLR_TRE BIT(1)
  153. #define ARM_SMMU_SCTLR_M BIT(0)
  154. #define ARM_SMMU_CB_ACTLR 0x4
  155. #define ARM_SMMU_CB_RESUME 0x8
  156. #define ARM_SMMU_RESUME_TERMINATE BIT(0)
  157. #define ARM_SMMU_RESUME_RESUME 0
  158. #define ARM_SMMU_CB_TCR2 0x10
  159. #define ARM_SMMU_TCR2_SEP GENMASK(17, 15)
  160. #define ARM_SMMU_TCR2_SEP_UPSTREAM 0x7
  161. #define ARM_SMMU_TCR2_AS BIT(4)
  162. #define ARM_SMMU_TCR2_PASIZE GENMASK(3, 0)
  163. #define ARM_SMMU_CB_TTBR0 0x20
  164. #define ARM_SMMU_CB_TTBR1 0x28
  165. #define ARM_SMMU_TTBRn_ASID GENMASK_ULL(63, 48)
  166. #define ARM_SMMU_CB_TCR 0x30
  167. #define ARM_SMMU_TCR_EAE BIT(31)
  168. #define ARM_SMMU_TCR_EPD1 BIT(23)
  169. #define ARM_SMMU_TCR_A1 BIT(22)
  170. #define ARM_SMMU_TCR_TG0 GENMASK(15, 14)
  171. #define ARM_SMMU_TCR_SH0 GENMASK(13, 12)
  172. #define ARM_SMMU_TCR_ORGN0 GENMASK(11, 10)
  173. #define ARM_SMMU_TCR_IRGN0 GENMASK(9, 8)
  174. #define ARM_SMMU_TCR_EPD0 BIT(7)
  175. #define ARM_SMMU_TCR_T0SZ GENMASK(5, 0)
  176. #define ARM_SMMU_VTCR_RES1 BIT(31)
  177. #define ARM_SMMU_VTCR_PS GENMASK(18, 16)
  178. #define ARM_SMMU_VTCR_TG0 ARM_SMMU_TCR_TG0
  179. #define ARM_SMMU_VTCR_SH0 ARM_SMMU_TCR_SH0
  180. #define ARM_SMMU_VTCR_ORGN0 ARM_SMMU_TCR_ORGN0
  181. #define ARM_SMMU_VTCR_IRGN0 ARM_SMMU_TCR_IRGN0
  182. #define ARM_SMMU_VTCR_SL0 GENMASK(7, 6)
  183. #define ARM_SMMU_VTCR_T0SZ ARM_SMMU_TCR_T0SZ
  184. #define ARM_SMMU_CB_CONTEXTIDR 0x34
  185. #define ARM_SMMU_CB_S1_MAIR0 0x38
  186. #define ARM_SMMU_CB_S1_MAIR1 0x3c
  187. #define ARM_SMMU_CB_PAR 0x50
  188. #define ARM_SMMU_CB_PAR_F BIT(0)
  189. #define ARM_SMMU_CB_FSR 0x58
  190. #define ARM_SMMU_FSR_MULTI BIT(31)
  191. #define ARM_SMMU_FSR_SS BIT(30)
  192. #define ARM_SMMU_FSR_UUT BIT(8)
  193. #define ARM_SMMU_FSR_ASF BIT(7)
  194. #define ARM_SMMU_FSR_TLBLKF BIT(6)
  195. #define ARM_SMMU_FSR_TLBMCF BIT(5)
  196. #define ARM_SMMU_FSR_EF BIT(4)
  197. #define ARM_SMMU_FSR_PF BIT(3)
  198. #define ARM_SMMU_FSR_AFF BIT(2)
  199. #define ARM_SMMU_FSR_TF BIT(1)
  200. #define ARM_SMMU_FSR_IGN (ARM_SMMU_FSR_AFF | \
  201. ARM_SMMU_FSR_ASF | \
  202. ARM_SMMU_FSR_TLBMCF | \
  203. ARM_SMMU_FSR_TLBLKF)
  204. #define ARM_SMMU_FSR_FAULT (ARM_SMMU_FSR_MULTI | \
  205. ARM_SMMU_FSR_SS | \
  206. ARM_SMMU_FSR_UUT | \
  207. ARM_SMMU_FSR_EF | \
  208. ARM_SMMU_FSR_PF | \
  209. ARM_SMMU_FSR_TF | \
  210. ARM_SMMU_FSR_IGN)
  211. #define ARM_SMMU_CB_FSRRESTORE 0x5c
  212. #define ARM_SMMU_CB_FAR 0x60
  213. #define ARM_SMMU_CB_FSYNR0 0x68
  214. #define ARM_SMMU_FSYNR0_WNR BIT(4)
  215. #define ARM_SMMU_FSYNR0_PNU BIT(5)
  216. #define ARM_SMMU_FSYNR0_IND BIT(6)
  217. #define ARM_SMMU_FSYNR0_NSATTR BIT(8)
  218. #define ARM_SMMU_CB_FSYNR1 0x6c
  219. #define ARM_SMMU_FSYNR1_BID GENMASK(15, 13)
  220. #define ARM_SMMU_FSYNR1_PID GENMASK(12, 8)
  221. #define ARM_SMMU_FSYNR1_MID GENMASK(7, 0)
  222. #define ARM_SMMU_CB_FSYNR1 0x6c
  223. #define ARM_SMMU_CB_S1_TLBIVA 0x600
  224. #define ARM_SMMU_CB_S1_TLBIASID 0x610
  225. #define ARM_SMMU_CB_S1_TLBIVAL 0x620
  226. #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
  227. #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
  228. #define ARM_SMMU_CB_TLBSYNC 0x7f0
  229. #define ARM_SMMU_CB_TLBSTATUS 0x7f4
  230. #define ARM_SMMU_CB_ATS1PR 0x800
  231. /* Implementation Defined Register Space 5 registers*/
  232. /* Relative to IMPL_DEF5 page */
  233. #ifdef CONFIG_ARM_SMMU_TESTBUS_DUMP_GEN3AUTO
  234. #define ARM_SMMU_STATS_SYNC_INV_TBU_ACK 0x51c
  235. #else
  236. #define ARM_SMMU_STATS_SYNC_INV_TBU_ACK 0x5dc
  237. #endif
  238. #define TBU_SYNC_ACK GENMASK(31, 17)
  239. #define TBU_SYNC_REQ BIT(16)
  240. #define TBU_INV_ACK GENMASK(15, 1)
  241. #define TBU_INV_REQ BIT(0)
  242. #define APPS_SMMU_TBU_REG_ACCESS_REQ_NS 0x5f8
  243. #define APPS_SMMU_TBU_REG_ACCESS_ACK_NS 0x5fc
  244. /* Relative to SMMU_BASE */
  245. #define ARM_SMMU_TBU_PWR_STATUS 0x2204
  246. /* Relative SMMU_BASE */
  247. #define ARM_SMMU_MMU2QSS_AND_SAFE_WAIT_CNTR 0x2670
  248. #define TCU_SYNC_IN_PRGSS BIT(20)
  249. #define TCU_INV_IN_PRGSS BIT(16)
  250. #define ARM_SMMU_CB_ATSR 0x8f0
  251. #define ARM_SMMU_ATSR_ACTIVE BIT(0)
  252. /* Maximum number of context banks per SMMU */
  253. #define ARM_SMMU_MAX_CBS 128
  254. #define TLB_LOOP_TIMEOUT 500000 /* 500ms */
  255. #define TLB_SPIN_COUNT 10
  256. #define TLB_LOOP_INC_MAX 1000 /*1ms*/
  257. /* Shared driver definitions */
  258. enum arm_smmu_arch_version {
  259. ARM_SMMU_V1,
  260. ARM_SMMU_V1_64K,
  261. ARM_SMMU_V2,
  262. };
  263. enum arm_smmu_implementation {
  264. GENERIC_SMMU,
  265. ARM_MMU500,
  266. CAVIUM_SMMUV2,
  267. QCOM_SMMUV2,
  268. QCOM_SMMUV500,
  269. };
  270. /*
  271. * Describes resources required for on/off power operation.
  272. * Separate reference count is provided for atomic/nonatomic
  273. * operations.
  274. */
  275. struct arm_smmu_power_resources {
  276. struct device *dev;
  277. struct clk **clocks;
  278. int num_clocks;
  279. struct regulator_bulk_data *gdscs;
  280. int num_gdscs;
  281. struct icc_path *icc_path;
  282. /* Protects power_count */
  283. struct mutex power_lock;
  284. int power_count;
  285. int (*resume)(struct arm_smmu_power_resources *pwr);
  286. void (*suspend)(struct arm_smmu_power_resources *pwr);
  287. };
  288. struct arm_smmu_s2cr {
  289. struct iommu_group *group;
  290. int count;
  291. enum arm_smmu_s2cr_type type;
  292. enum arm_smmu_s2cr_privcfg privcfg;
  293. u8 cbndx;
  294. bool pinned;
  295. };
  296. struct arm_smmu_smr {
  297. u16 mask;
  298. u16 id;
  299. bool valid;
  300. bool pinned;
  301. bool used;
  302. };
  303. struct arm_smmu_device {
  304. struct device *dev;
  305. void __iomem *base;
  306. phys_addr_t ioaddr;
  307. unsigned int numpage;
  308. unsigned int pgshift;
  309. #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
  310. #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
  311. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
  312. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
  313. #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
  314. #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
  315. #define ARM_SMMU_FEAT_VMID16 (1 << 6)
  316. #define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
  317. #define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
  318. #define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
  319. #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
  320. #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
  321. #define ARM_SMMU_FEAT_EXIDS (1 << 12)
  322. u32 features;
  323. #define ARM_SMMU_OPT_FATAL_ASF (1 << 0)
  324. #define ARM_SMMU_OPT_3LVL_TABLES (1 << 2)
  325. #define ARM_SMMU_OPT_NO_ASID_RETENTION (1 << 3)
  326. #define ARM_SMMU_OPT_DISABLE_ATOS (1 << 4)
  327. #define ARM_SMMU_OPT_CONTEXT_FAULT_RETRY (1 << 5)
  328. #define ARM_SMMU_OPT_MULTI_MATCH_HANDOFF_SMR (1 << 6)
  329. #define ARM_SMMU_OPT_IGNORE_NUMPAGENDXB (1 << 7)
  330. u32 options;
  331. enum arm_smmu_arch_version version;
  332. enum arm_smmu_implementation model;
  333. const struct arm_smmu_impl *impl;
  334. u32 num_context_banks;
  335. u32 num_s2_context_banks;
  336. DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
  337. struct arm_smmu_cb *cbs;
  338. atomic_t irptndx;
  339. u32 num_mapping_groups;
  340. u16 streamid_mask;
  341. u16 smr_mask_mask;
  342. struct arm_smmu_smr *smrs;
  343. struct arm_smmu_s2cr *s2crs;
  344. struct mutex stream_map_mutex;
  345. unsigned long va_size;
  346. unsigned long ipa_size;
  347. unsigned long pa_size;
  348. unsigned long pgsize_bitmap;
  349. int num_context_irqs;
  350. int num_clks;
  351. unsigned int *irqs;
  352. struct clk_bulk_data *clks;
  353. spinlock_t global_sync_lock;
  354. /* IOMMU core code handle */
  355. struct iommu_device iommu;
  356. /* Specific to QCOM */
  357. struct arm_smmu_impl_def_reg *impl_def_attach_registers;
  358. unsigned int num_impl_def_attach_registers;
  359. struct arm_smmu_power_resources *pwr;
  360. /* used for qsmmuv500 scm_io_readl */
  361. phys_addr_t phys_addr;
  362. unsigned long sync_timed_out;
  363. };
  364. enum arm_smmu_context_fmt {
  365. ARM_SMMU_CTX_FMT_NONE,
  366. ARM_SMMU_CTX_FMT_AARCH64,
  367. ARM_SMMU_CTX_FMT_AARCH32_L,
  368. ARM_SMMU_CTX_FMT_AARCH32_S,
  369. };
  370. struct arm_smmu_cfg {
  371. u8 cbndx;
  372. u8 irptndx;
  373. union {
  374. u16 asid;
  375. u16 vmid;
  376. };
  377. u32 procid;
  378. struct {
  379. u32 wacfg:2;
  380. u32 racfg:2;
  381. u32 shcfg:2;
  382. u32 mtcfg:1;
  383. u32 memattr:4;
  384. u32 hupcf:1;
  385. u32 cfcfg:1;
  386. u32 cfre:1;
  387. u32 m:1;
  388. } sctlr;
  389. enum arm_smmu_cbar_type cbar;
  390. enum arm_smmu_context_fmt fmt;
  391. bool flush_walk_prefer_tlbiasid;
  392. };
  393. #define ARM_SMMU_INVALID_IRPTNDX 0xff
  394. struct arm_smmu_cb {
  395. u64 ttbr[2];
  396. u32 tcr[2];
  397. u32 mair[2];
  398. u32 sctlr;
  399. struct arm_smmu_cfg *cfg;
  400. };
  401. enum arm_smmu_domain_stage {
  402. ARM_SMMU_DOMAIN_S1 = 0,
  403. ARM_SMMU_DOMAIN_S2,
  404. ARM_SMMU_DOMAIN_NESTED,
  405. ARM_SMMU_DOMAIN_BYPASS,
  406. };
  407. struct arm_smmu_fault_model {
  408. char non_fatal : 1;
  409. char no_cfre : 1;
  410. char no_stall : 1;
  411. char hupcf : 1;
  412. };
  413. struct arm_smmu_mapping_cfg {
  414. char s1_bypass : 1;
  415. char atomic : 1;
  416. char fast : 1;
  417. };
  418. struct arm_smmu_domain {
  419. struct arm_smmu_device *smmu;
  420. struct device *dev;
  421. struct io_pgtable_ops *pgtbl_ops;
  422. unsigned long pgtbl_quirks;
  423. bool force_coherent_walk;
  424. const struct iommu_flush_ops *flush_ops;
  425. struct arm_smmu_cfg cfg;
  426. enum arm_smmu_domain_stage stage;
  427. struct mutex init_mutex; /* Protects smmu pointer */
  428. spinlock_t cb_lock; /* Serialises ATS1* ops */
  429. spinlock_t sync_lock; /* Serialises TLB syncs */
  430. struct arm_smmu_fault_model fault_model;
  431. struct arm_smmu_mapping_cfg mapping_cfg;
  432. bool delayed_s1_trans_enable;
  433. u32 secure_vmid;
  434. fault_handler_irq_t fault_handler_irq;
  435. void *handler_irq_token;
  436. /*
  437. * Track PMDs which require tlb invalidate prior to being
  438. * freed, or before their iovas can be reused by iommu_map().
  439. */
  440. spinlock_t iotlb_gather_lock;
  441. struct list_head *freelist;
  442. bool deferred_flush;
  443. struct iommu_debug_attachment *logger;
  444. struct iommu_domain domain;
  445. struct qcom_io_pgtable_info pgtbl_info;
  446. enum io_pgtable_fmt pgtbl_fmt;
  447. /* mapping_cfg.atomic indicates that runtime power management should be disabled. */
  448. bool rpm_always_on;
  449. /* skip tlb management. */
  450. bool skip_tlb_management;
  451. #ifdef CONFIG_ARM_SMMU_CONTEXT_FAULT_RETRY
  452. u64 prev_fault_address;
  453. u32 fault_retry_counter;
  454. #endif
  455. };
  456. struct arm_smmu_master_cfg {
  457. struct arm_smmu_device *smmu;
  458. s16 smendx[];
  459. };
  460. static inline u32 arm_smmu_lpae_tcr(const struct io_pgtable_cfg *cfg)
  461. {
  462. u32 tcr = FIELD_PREP(ARM_SMMU_TCR_TG0, cfg->arm_lpae_s1_cfg.tcr.tg) |
  463. FIELD_PREP(ARM_SMMU_TCR_SH0, cfg->arm_lpae_s1_cfg.tcr.sh) |
  464. FIELD_PREP(ARM_SMMU_TCR_ORGN0, cfg->arm_lpae_s1_cfg.tcr.orgn) |
  465. FIELD_PREP(ARM_SMMU_TCR_IRGN0, cfg->arm_lpae_s1_cfg.tcr.irgn) |
  466. FIELD_PREP(ARM_SMMU_TCR_T0SZ, cfg->arm_lpae_s1_cfg.tcr.tsz);
  467. /*
  468. * When TTBR1 is selected shift the TCR fields by 16 bits and disable
  469. * translation in TTBR0
  470. */
  471. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) {
  472. tcr = (tcr << 16) & ~ARM_SMMU_TCR_A1;
  473. tcr |= ARM_SMMU_TCR_EPD0;
  474. } else
  475. tcr |= ARM_SMMU_TCR_EPD1;
  476. return tcr;
  477. }
  478. static inline u32 arm_smmu_lpae_tcr2(const struct io_pgtable_cfg *cfg)
  479. {
  480. return FIELD_PREP(ARM_SMMU_TCR2_PASIZE, cfg->arm_lpae_s1_cfg.tcr.ips) |
  481. FIELD_PREP(ARM_SMMU_TCR2_SEP, ARM_SMMU_TCR2_SEP_UPSTREAM);
  482. }
  483. static inline u32 arm_smmu_lpae_vtcr(const struct io_pgtable_cfg *cfg)
  484. {
  485. return ARM_SMMU_VTCR_RES1 |
  486. FIELD_PREP(ARM_SMMU_VTCR_PS, cfg->arm_lpae_s2_cfg.vtcr.ps) |
  487. FIELD_PREP(ARM_SMMU_VTCR_TG0, cfg->arm_lpae_s2_cfg.vtcr.tg) |
  488. FIELD_PREP(ARM_SMMU_VTCR_SH0, cfg->arm_lpae_s2_cfg.vtcr.sh) |
  489. FIELD_PREP(ARM_SMMU_VTCR_ORGN0, cfg->arm_lpae_s2_cfg.vtcr.orgn) |
  490. FIELD_PREP(ARM_SMMU_VTCR_IRGN0, cfg->arm_lpae_s2_cfg.vtcr.irgn) |
  491. FIELD_PREP(ARM_SMMU_VTCR_SL0, cfg->arm_lpae_s2_cfg.vtcr.sl) |
  492. FIELD_PREP(ARM_SMMU_VTCR_T0SZ, cfg->arm_lpae_s2_cfg.vtcr.tsz);
  493. }
  494. static inline u32 arm_smmu_lpae_sctlr(struct arm_smmu_cfg *cfg)
  495. {
  496. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  497. return FIELD_PREP(ARM_SMMU_SCTLR_WACFG, cfg->sctlr.wacfg) |
  498. FIELD_PREP(ARM_SMMU_SCTLR_RACFG, cfg->sctlr.racfg) |
  499. FIELD_PREP(ARM_SMMU_SCTLR_SHCFG, cfg->sctlr.shcfg) |
  500. FIELD_PREP(ARM_SMMU_SCTLR_MTCFG, cfg->sctlr.mtcfg) |
  501. FIELD_PREP(ARM_SMMU_SCTLR_MEM_ATTR, cfg->sctlr.memattr) |
  502. FIELD_PREP(ARM_SMMU_SCTLR_S1_ASIDPNE, stage1) |
  503. FIELD_PREP(ARM_SMMU_SCTLR_HUPCF, cfg->sctlr.hupcf) |
  504. FIELD_PREP(ARM_SMMU_SCTLR_CFCFG, cfg->sctlr.cfcfg) |
  505. ARM_SMMU_SCTLR_CFIE |
  506. FIELD_PREP(ARM_SMMU_SCTLR_CFRE, cfg->sctlr.cfre) |
  507. FIELD_PREP(ARM_SMMU_SCTLR_E, IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
  508. ARM_SMMU_SCTLR_AFE |
  509. ARM_SMMU_SCTLR_TRE |
  510. FIELD_PREP(ARM_SMMU_SCTLR_M, cfg->sctlr.m);
  511. }
  512. /* Implementation details, yay! */
  513. struct arm_smmu_impl {
  514. u32 (*read_reg)(struct arm_smmu_device *smmu, int page, int offset);
  515. void (*write_reg)(struct arm_smmu_device *smmu, int page, int offset,
  516. u32 val);
  517. u64 (*read_reg64)(struct arm_smmu_device *smmu, int page, int offset);
  518. void (*write_reg64)(struct arm_smmu_device *smmu, int page, int offset,
  519. u64 val);
  520. int (*cfg_probe)(struct arm_smmu_device *smmu);
  521. int (*reset)(struct arm_smmu_device *smmu);
  522. int (*init_context)(struct arm_smmu_domain *smmu_domain,
  523. struct io_pgtable_cfg *cfg, struct device *dev);
  524. void (*init_context_bank)(struct arm_smmu_domain *smmu_domain,
  525. struct device *dev);
  526. phys_addr_t (*iova_to_phys_hard)(struct arm_smmu_domain *smmu_domain,
  527. struct qcom_iommu_atos_txn *txn);
  528. void (*tlb_sync_timeout)(struct arm_smmu_device *smmu);
  529. void (*device_remove)(struct arm_smmu_device *smmu);
  530. int (*device_group)(struct device *dev, struct iommu_group *group);
  531. void (*tlb_sync)(struct arm_smmu_device *smmu, int page, int sync,
  532. int status);
  533. int (*def_domain_type)(struct device *dev);
  534. irqreturn_t (*global_fault)(int irq, void *dev);
  535. irqreturn_t (*context_fault)(int irq, void *dev);
  536. int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain,
  537. struct arm_smmu_device *smmu,
  538. struct device *dev, int start);
  539. void (*write_s2cr)(struct arm_smmu_device *smmu, int idx);
  540. void (*write_sctlr)(struct arm_smmu_device *smmu, int idx, u32 reg);
  541. void (*probe_finalize)(struct arm_smmu_device *smmu, struct device *dev);
  542. };
  543. #define INVALID_SMENDX -1
  544. #define cfg_smendx(cfg, fw, i) \
  545. (i >= fw->num_ids ? INVALID_SMENDX : cfg->smendx[i])
  546. #define for_each_cfg_sme(cfg, fw, i, idx) \
  547. for (i = 0; idx = cfg_smendx(cfg, fw, i), i < fw->num_ids; ++i)
  548. static inline int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
  549. {
  550. int idx;
  551. do {
  552. idx = find_next_zero_bit(map, end, start);
  553. if (idx == end)
  554. return -ENOSPC;
  555. } while (test_and_set_bit(idx, map));
  556. return idx;
  557. }
  558. static inline void __iomem *arm_smmu_page(struct arm_smmu_device *smmu, int n)
  559. {
  560. return smmu->base + (n << smmu->pgshift);
  561. }
  562. static inline u32 arm_smmu_readl(struct arm_smmu_device *smmu, int page, int offset)
  563. {
  564. if (smmu->impl && unlikely(smmu->impl->read_reg))
  565. return smmu->impl->read_reg(smmu, page, offset);
  566. return readl_relaxed(arm_smmu_page(smmu, page) + offset);
  567. }
  568. static inline void arm_smmu_writel(struct arm_smmu_device *smmu, int page,
  569. int offset, u32 val)
  570. {
  571. if (smmu->impl && unlikely(smmu->impl->write_reg))
  572. smmu->impl->write_reg(smmu, page, offset, val);
  573. else
  574. writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
  575. }
  576. static inline u64 arm_smmu_readq(struct arm_smmu_device *smmu, int page, int offset)
  577. {
  578. if (smmu->impl && unlikely(smmu->impl->read_reg64))
  579. return smmu->impl->read_reg64(smmu, page, offset);
  580. return readq_relaxed(arm_smmu_page(smmu, page) + offset);
  581. }
  582. static inline void arm_smmu_writeq(struct arm_smmu_device *smmu, int page,
  583. int offset, u64 val)
  584. {
  585. if (smmu->impl && unlikely(smmu->impl->write_reg64))
  586. smmu->impl->write_reg64(smmu, page, offset, val);
  587. else
  588. writeq_relaxed(val, arm_smmu_page(smmu, page) + offset);
  589. }
  590. #define ARM_SMMU_GR0 0
  591. #define ARM_SMMU_GR1 1
  592. /*
  593. * Implementation defined space starts after SMMU GR space, so IMPL_DEF page n
  594. * is page n + 2 in the SMMU register space.
  595. */
  596. #define ARM_SMMU_IMPL_DEF0 2
  597. #define ARM_SMMU_IMPL_DEF4 6
  598. #ifdef CONFIG_ARM_SMMU_TESTBUS_DUMP_GEN3AUTO
  599. #define ARM_SMMU_IMPL_DEF5 ARM_SMMU_IMPL_DEF0
  600. #else
  601. #define ARM_SMMU_IMPL_DEF5 7
  602. #endif
  603. #define ARM_SMMU_CB(s, n) ((s)->numpage + (n))
  604. #define arm_smmu_gr0_read(s, o) \
  605. arm_smmu_readl((s), ARM_SMMU_GR0, (o))
  606. #define arm_smmu_gr0_write(s, o, v) \
  607. arm_smmu_writel((s), ARM_SMMU_GR0, (o), (v))
  608. #define arm_smmu_gr1_read(s, o) \
  609. arm_smmu_readl((s), ARM_SMMU_GR1, (o))
  610. #define arm_smmu_gr1_write(s, o, v) \
  611. arm_smmu_writel((s), ARM_SMMU_GR1, (o), (v))
  612. #define arm_smmu_cb_read(s, n, o) \
  613. arm_smmu_readl((s), ARM_SMMU_CB((s), (n)), (o))
  614. #define arm_smmu_cb_write(s, n, o, v) \
  615. arm_smmu_writel((s), ARM_SMMU_CB((s), (n)), (o), (v))
  616. #define arm_smmu_cb_readq(s, n, o) \
  617. arm_smmu_readq((s), ARM_SMMU_CB((s), (n)), (o))
  618. #define arm_smmu_cb_writeq(s, n, o, v) \
  619. arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v))
  620. struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu);
  621. struct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu);
  622. struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu);
  623. struct arm_smmu_device *qsmmuv500_impl_init(struct arm_smmu_device *smmu);
  624. struct arm_smmu_device *qsmmuv2_impl_init(struct arm_smmu_device *smmu);
  625. struct arm_smmu_device *qcom_adreno_smmu_impl_init(struct arm_smmu_device *smmu);
  626. void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx);
  627. int arm_mmu500_reset(struct arm_smmu_device *smmu);
  628. int arm_smmu_power_on(struct arm_smmu_power_resources *pwr);
  629. void arm_smmu_power_off(struct arm_smmu_device *smmu,
  630. struct arm_smmu_power_resources *pwr);
  631. struct arm_smmu_power_resources *arm_smmu_init_power_resources(
  632. struct device *dev);
  633. extern struct platform_driver qsmmuv500_tbu_driver;
  634. /* Misc. constants */
  635. #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
  636. #endif /* _ARM_SMMU_H */