Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IOMMU API for ARM architected SMMU implementations. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | * |
| 17 | * Copyright (C) 2013 ARM Limited |
| 18 | * |
| 19 | * Author: Will Deacon <will.deacon@arm.com> |
| 20 | * |
| 21 | * This driver currently supports: |
| 22 | * - SMMUv1 and v2 implementations |
| 23 | * - Stream-matching and stream-indexing |
| 24 | * - v7/v8 long-descriptor format |
| 25 | * - Non-secure access to the SMMU |
| 26 | * - 4k and 64k pages, with contiguous pte hints. |
Will Deacon | 06f983d | 2013-11-05 15:55:04 +0000 | [diff] [blame] | 27 | * - Up to 42-bit addressing (dependent on VA_BITS) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 28 | * - Context fault reporting |
| 29 | */ |
| 30 | |
| 31 | #define pr_fmt(fmt) "arm-smmu: " fmt |
| 32 | |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/dma-mapping.h> |
| 35 | #include <linux/err.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/io.h> |
| 38 | #include <linux/iommu.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/of.h> |
| 42 | #include <linux/platform_device.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/spinlock.h> |
| 45 | |
| 46 | #include <linux/amba/bus.h> |
| 47 | |
| 48 | #include <asm/pgalloc.h> |
| 49 | |
| 50 | /* Maximum number of stream IDs assigned to a single device */ |
| 51 | #define MAX_MASTER_STREAMIDS 8 |
| 52 | |
| 53 | /* Maximum number of context banks per SMMU */ |
| 54 | #define ARM_SMMU_MAX_CBS 128 |
| 55 | |
| 56 | /* Maximum number of mapping groups per SMMU */ |
| 57 | #define ARM_SMMU_MAX_SMRS 128 |
| 58 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 59 | /* SMMU global address space */ |
| 60 | #define ARM_SMMU_GR0(smmu) ((smmu)->base) |
| 61 | #define ARM_SMMU_GR1(smmu) ((smmu)->base + (smmu)->pagesize) |
| 62 | |
| 63 | /* Page table bits */ |
Will Deacon | cf2d45b | 2013-11-05 16:32:00 +0000 | [diff] [blame] | 64 | #define ARM_SMMU_PTE_XN (((pteval_t)3) << 53) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 65 | #define ARM_SMMU_PTE_CONT (((pteval_t)1) << 52) |
| 66 | #define ARM_SMMU_PTE_AF (((pteval_t)1) << 10) |
| 67 | #define ARM_SMMU_PTE_SH_NS (((pteval_t)0) << 8) |
| 68 | #define ARM_SMMU_PTE_SH_OS (((pteval_t)2) << 8) |
| 69 | #define ARM_SMMU_PTE_SH_IS (((pteval_t)3) << 8) |
Will Deacon | cf2d45b | 2013-11-05 16:32:00 +0000 | [diff] [blame] | 70 | #define ARM_SMMU_PTE_PAGE (((pteval_t)3) << 0) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 71 | |
| 72 | #if PAGE_SIZE == SZ_4K |
| 73 | #define ARM_SMMU_PTE_CONT_ENTRIES 16 |
| 74 | #elif PAGE_SIZE == SZ_64K |
| 75 | #define ARM_SMMU_PTE_CONT_ENTRIES 32 |
| 76 | #else |
| 77 | #define ARM_SMMU_PTE_CONT_ENTRIES 1 |
| 78 | #endif |
| 79 | |
| 80 | #define ARM_SMMU_PTE_CONT_SIZE (PAGE_SIZE * ARM_SMMU_PTE_CONT_ENTRIES) |
| 81 | #define ARM_SMMU_PTE_CONT_MASK (~(ARM_SMMU_PTE_CONT_SIZE - 1)) |
| 82 | #define ARM_SMMU_PTE_HWTABLE_SIZE (PTRS_PER_PTE * sizeof(pte_t)) |
| 83 | |
| 84 | /* Stage-1 PTE */ |
| 85 | #define ARM_SMMU_PTE_AP_UNPRIV (((pteval_t)1) << 6) |
| 86 | #define ARM_SMMU_PTE_AP_RDONLY (((pteval_t)2) << 6) |
| 87 | #define ARM_SMMU_PTE_ATTRINDX_SHIFT 2 |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 88 | #define ARM_SMMU_PTE_nG (((pteval_t)1) << 11) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 89 | |
| 90 | /* Stage-2 PTE */ |
| 91 | #define ARM_SMMU_PTE_HAP_FAULT (((pteval_t)0) << 6) |
| 92 | #define ARM_SMMU_PTE_HAP_READ (((pteval_t)1) << 6) |
| 93 | #define ARM_SMMU_PTE_HAP_WRITE (((pteval_t)2) << 6) |
| 94 | #define ARM_SMMU_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2) |
| 95 | #define ARM_SMMU_PTE_MEMATTR_NC (((pteval_t)0x5) << 2) |
| 96 | #define ARM_SMMU_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2) |
| 97 | |
| 98 | /* Configuration registers */ |
| 99 | #define ARM_SMMU_GR0_sCR0 0x0 |
| 100 | #define sCR0_CLIENTPD (1 << 0) |
| 101 | #define sCR0_GFRE (1 << 1) |
| 102 | #define sCR0_GFIE (1 << 2) |
| 103 | #define sCR0_GCFGFRE (1 << 4) |
| 104 | #define sCR0_GCFGFIE (1 << 5) |
| 105 | #define sCR0_USFCFG (1 << 10) |
| 106 | #define sCR0_VMIDPNE (1 << 11) |
| 107 | #define sCR0_PTM (1 << 12) |
| 108 | #define sCR0_FB (1 << 13) |
| 109 | #define sCR0_BSU_SHIFT 14 |
| 110 | #define sCR0_BSU_MASK 0x3 |
| 111 | |
| 112 | /* Identification registers */ |
| 113 | #define ARM_SMMU_GR0_ID0 0x20 |
| 114 | #define ARM_SMMU_GR0_ID1 0x24 |
| 115 | #define ARM_SMMU_GR0_ID2 0x28 |
| 116 | #define ARM_SMMU_GR0_ID3 0x2c |
| 117 | #define ARM_SMMU_GR0_ID4 0x30 |
| 118 | #define ARM_SMMU_GR0_ID5 0x34 |
| 119 | #define ARM_SMMU_GR0_ID6 0x38 |
| 120 | #define ARM_SMMU_GR0_ID7 0x3c |
| 121 | #define ARM_SMMU_GR0_sGFSR 0x48 |
| 122 | #define ARM_SMMU_GR0_sGFSYNR0 0x50 |
| 123 | #define ARM_SMMU_GR0_sGFSYNR1 0x54 |
| 124 | #define ARM_SMMU_GR0_sGFSYNR2 0x58 |
| 125 | #define ARM_SMMU_GR0_PIDR0 0xfe0 |
| 126 | #define ARM_SMMU_GR0_PIDR1 0xfe4 |
| 127 | #define ARM_SMMU_GR0_PIDR2 0xfe8 |
| 128 | |
| 129 | #define ID0_S1TS (1 << 30) |
| 130 | #define ID0_S2TS (1 << 29) |
| 131 | #define ID0_NTS (1 << 28) |
| 132 | #define ID0_SMS (1 << 27) |
| 133 | #define ID0_PTFS_SHIFT 24 |
| 134 | #define ID0_PTFS_MASK 0x2 |
| 135 | #define ID0_PTFS_V8_ONLY 0x2 |
| 136 | #define ID0_CTTW (1 << 14) |
| 137 | #define ID0_NUMIRPT_SHIFT 16 |
| 138 | #define ID0_NUMIRPT_MASK 0xff |
| 139 | #define ID0_NUMSMRG_SHIFT 0 |
| 140 | #define ID0_NUMSMRG_MASK 0xff |
| 141 | |
| 142 | #define ID1_PAGESIZE (1 << 31) |
| 143 | #define ID1_NUMPAGENDXB_SHIFT 28 |
| 144 | #define ID1_NUMPAGENDXB_MASK 7 |
| 145 | #define ID1_NUMS2CB_SHIFT 16 |
| 146 | #define ID1_NUMS2CB_MASK 0xff |
| 147 | #define ID1_NUMCB_SHIFT 0 |
| 148 | #define ID1_NUMCB_MASK 0xff |
| 149 | |
| 150 | #define ID2_OAS_SHIFT 4 |
| 151 | #define ID2_OAS_MASK 0xf |
| 152 | #define ID2_IAS_SHIFT 0 |
| 153 | #define ID2_IAS_MASK 0xf |
| 154 | #define ID2_UBS_SHIFT 8 |
| 155 | #define ID2_UBS_MASK 0xf |
| 156 | #define ID2_PTFS_4K (1 << 12) |
| 157 | #define ID2_PTFS_16K (1 << 13) |
| 158 | #define ID2_PTFS_64K (1 << 14) |
| 159 | |
| 160 | #define PIDR2_ARCH_SHIFT 4 |
| 161 | #define PIDR2_ARCH_MASK 0xf |
| 162 | |
| 163 | /* Global TLB invalidation */ |
| 164 | #define ARM_SMMU_GR0_STLBIALL 0x60 |
| 165 | #define ARM_SMMU_GR0_TLBIVMID 0x64 |
| 166 | #define ARM_SMMU_GR0_TLBIALLNSNH 0x68 |
| 167 | #define ARM_SMMU_GR0_TLBIALLH 0x6c |
| 168 | #define ARM_SMMU_GR0_sTLBGSYNC 0x70 |
| 169 | #define ARM_SMMU_GR0_sTLBGSTATUS 0x74 |
| 170 | #define sTLBGSTATUS_GSACTIVE (1 << 0) |
| 171 | #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */ |
| 172 | |
| 173 | /* Stream mapping registers */ |
| 174 | #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2)) |
| 175 | #define SMR_VALID (1 << 31) |
| 176 | #define SMR_MASK_SHIFT 16 |
| 177 | #define SMR_MASK_MASK 0x7fff |
| 178 | #define SMR_ID_SHIFT 0 |
| 179 | #define SMR_ID_MASK 0x7fff |
| 180 | |
| 181 | #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2)) |
| 182 | #define S2CR_CBNDX_SHIFT 0 |
| 183 | #define S2CR_CBNDX_MASK 0xff |
| 184 | #define S2CR_TYPE_SHIFT 16 |
| 185 | #define S2CR_TYPE_MASK 0x3 |
| 186 | #define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT) |
| 187 | #define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT) |
| 188 | #define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT) |
| 189 | |
| 190 | /* Context bank attribute registers */ |
| 191 | #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2)) |
| 192 | #define CBAR_VMID_SHIFT 0 |
| 193 | #define CBAR_VMID_MASK 0xff |
| 194 | #define CBAR_S1_MEMATTR_SHIFT 12 |
| 195 | #define CBAR_S1_MEMATTR_MASK 0xf |
| 196 | #define CBAR_S1_MEMATTR_WB 0xf |
| 197 | #define CBAR_TYPE_SHIFT 16 |
| 198 | #define CBAR_TYPE_MASK 0x3 |
| 199 | #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT) |
| 200 | #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT) |
| 201 | #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT) |
| 202 | #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT) |
| 203 | #define CBAR_IRPTNDX_SHIFT 24 |
| 204 | #define CBAR_IRPTNDX_MASK 0xff |
| 205 | |
| 206 | #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2)) |
| 207 | #define CBA2R_RW64_32BIT (0 << 0) |
| 208 | #define CBA2R_RW64_64BIT (1 << 0) |
| 209 | |
| 210 | /* Translation context bank */ |
| 211 | #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1)) |
| 212 | #define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize) |
| 213 | |
| 214 | #define ARM_SMMU_CB_SCTLR 0x0 |
| 215 | #define ARM_SMMU_CB_RESUME 0x8 |
| 216 | #define ARM_SMMU_CB_TTBCR2 0x10 |
| 217 | #define ARM_SMMU_CB_TTBR0_LO 0x20 |
| 218 | #define ARM_SMMU_CB_TTBR0_HI 0x24 |
| 219 | #define ARM_SMMU_CB_TTBCR 0x30 |
| 220 | #define ARM_SMMU_CB_S1_MAIR0 0x38 |
| 221 | #define ARM_SMMU_CB_FSR 0x58 |
| 222 | #define ARM_SMMU_CB_FAR_LO 0x60 |
| 223 | #define ARM_SMMU_CB_FAR_HI 0x64 |
| 224 | #define ARM_SMMU_CB_FSYNR0 0x68 |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 225 | #define ARM_SMMU_CB_S1_TLBIASID 0x610 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 226 | |
| 227 | #define SCTLR_S1_ASIDPNE (1 << 12) |
| 228 | #define SCTLR_CFCFG (1 << 7) |
| 229 | #define SCTLR_CFIE (1 << 6) |
| 230 | #define SCTLR_CFRE (1 << 5) |
| 231 | #define SCTLR_E (1 << 4) |
| 232 | #define SCTLR_AFE (1 << 2) |
| 233 | #define SCTLR_TRE (1 << 1) |
| 234 | #define SCTLR_M (1 << 0) |
| 235 | #define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE) |
| 236 | |
| 237 | #define RESUME_RETRY (0 << 0) |
| 238 | #define RESUME_TERMINATE (1 << 0) |
| 239 | |
| 240 | #define TTBCR_EAE (1 << 31) |
| 241 | |
| 242 | #define TTBCR_PASIZE_SHIFT 16 |
| 243 | #define TTBCR_PASIZE_MASK 0x7 |
| 244 | |
| 245 | #define TTBCR_TG0_4K (0 << 14) |
| 246 | #define TTBCR_TG0_64K (1 << 14) |
| 247 | |
| 248 | #define TTBCR_SH0_SHIFT 12 |
| 249 | #define TTBCR_SH0_MASK 0x3 |
| 250 | #define TTBCR_SH_NS 0 |
| 251 | #define TTBCR_SH_OS 2 |
| 252 | #define TTBCR_SH_IS 3 |
| 253 | |
| 254 | #define TTBCR_ORGN0_SHIFT 10 |
| 255 | #define TTBCR_IRGN0_SHIFT 8 |
| 256 | #define TTBCR_RGN_MASK 0x3 |
| 257 | #define TTBCR_RGN_NC 0 |
| 258 | #define TTBCR_RGN_WBWA 1 |
| 259 | #define TTBCR_RGN_WT 2 |
| 260 | #define TTBCR_RGN_WB 3 |
| 261 | |
| 262 | #define TTBCR_SL0_SHIFT 6 |
| 263 | #define TTBCR_SL0_MASK 0x3 |
| 264 | #define TTBCR_SL0_LVL_2 0 |
| 265 | #define TTBCR_SL0_LVL_1 1 |
| 266 | |
| 267 | #define TTBCR_T1SZ_SHIFT 16 |
| 268 | #define TTBCR_T0SZ_SHIFT 0 |
| 269 | #define TTBCR_SZ_MASK 0xf |
| 270 | |
| 271 | #define TTBCR2_SEP_SHIFT 15 |
| 272 | #define TTBCR2_SEP_MASK 0x7 |
| 273 | |
| 274 | #define TTBCR2_PASIZE_SHIFT 0 |
| 275 | #define TTBCR2_PASIZE_MASK 0x7 |
| 276 | |
| 277 | /* Common definitions for PASize and SEP fields */ |
| 278 | #define TTBCR2_ADDR_32 0 |
| 279 | #define TTBCR2_ADDR_36 1 |
| 280 | #define TTBCR2_ADDR_40 2 |
| 281 | #define TTBCR2_ADDR_42 3 |
| 282 | #define TTBCR2_ADDR_44 4 |
| 283 | #define TTBCR2_ADDR_48 5 |
| 284 | |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 285 | #define TTBRn_HI_ASID_SHIFT 16 |
| 286 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 287 | #define MAIR_ATTR_SHIFT(n) ((n) << 3) |
| 288 | #define MAIR_ATTR_MASK 0xff |
| 289 | #define MAIR_ATTR_DEVICE 0x04 |
| 290 | #define MAIR_ATTR_NC 0x44 |
| 291 | #define MAIR_ATTR_WBRWA 0xff |
| 292 | #define MAIR_ATTR_IDX_NC 0 |
| 293 | #define MAIR_ATTR_IDX_CACHE 1 |
| 294 | #define MAIR_ATTR_IDX_DEV 2 |
| 295 | |
| 296 | #define FSR_MULTI (1 << 31) |
| 297 | #define FSR_SS (1 << 30) |
| 298 | #define FSR_UUT (1 << 8) |
| 299 | #define FSR_ASF (1 << 7) |
| 300 | #define FSR_TLBLKF (1 << 6) |
| 301 | #define FSR_TLBMCF (1 << 5) |
| 302 | #define FSR_EF (1 << 4) |
| 303 | #define FSR_PF (1 << 3) |
| 304 | #define FSR_AFF (1 << 2) |
| 305 | #define FSR_TF (1 << 1) |
| 306 | |
| 307 | #define FSR_IGN (FSR_AFF | FSR_ASF | FSR_TLBMCF | \ |
| 308 | FSR_TLBLKF) |
| 309 | #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \ |
Will Deacon | adaba32 | 2013-07-31 19:21:26 +0100 | [diff] [blame] | 310 | FSR_EF | FSR_PF | FSR_TF | FSR_IGN) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 311 | |
| 312 | #define FSYNR0_WNR (1 << 4) |
| 313 | |
| 314 | struct arm_smmu_smr { |
| 315 | u8 idx; |
| 316 | u16 mask; |
| 317 | u16 id; |
| 318 | }; |
| 319 | |
| 320 | struct arm_smmu_master { |
| 321 | struct device_node *of_node; |
| 322 | |
| 323 | /* |
| 324 | * The following is specific to the master's position in the |
| 325 | * SMMU chain. |
| 326 | */ |
| 327 | struct rb_node node; |
| 328 | int num_streamids; |
| 329 | u16 streamids[MAX_MASTER_STREAMIDS]; |
| 330 | |
| 331 | /* |
| 332 | * We only need to allocate these on the root SMMU, as we |
| 333 | * configure unmatched streams to bypass translation. |
| 334 | */ |
| 335 | struct arm_smmu_smr *smrs; |
| 336 | }; |
| 337 | |
| 338 | struct arm_smmu_device { |
| 339 | struct device *dev; |
| 340 | struct device_node *parent_of_node; |
| 341 | |
| 342 | void __iomem *base; |
| 343 | unsigned long size; |
| 344 | unsigned long pagesize; |
| 345 | |
| 346 | #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0) |
| 347 | #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1) |
| 348 | #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2) |
| 349 | #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3) |
| 350 | #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4) |
| 351 | u32 features; |
| 352 | int version; |
| 353 | |
| 354 | u32 num_context_banks; |
| 355 | u32 num_s2_context_banks; |
| 356 | DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS); |
| 357 | atomic_t irptndx; |
| 358 | |
| 359 | u32 num_mapping_groups; |
| 360 | DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS); |
| 361 | |
| 362 | unsigned long input_size; |
| 363 | unsigned long s1_output_size; |
| 364 | unsigned long s2_output_size; |
| 365 | |
| 366 | u32 num_global_irqs; |
| 367 | u32 num_context_irqs; |
| 368 | unsigned int *irqs; |
| 369 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 370 | struct list_head list; |
| 371 | struct rb_root masters; |
| 372 | }; |
| 373 | |
| 374 | struct arm_smmu_cfg { |
| 375 | struct arm_smmu_device *smmu; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 376 | u8 cbndx; |
| 377 | u8 irptndx; |
| 378 | u32 cbar; |
| 379 | pgd_t *pgd; |
| 380 | }; |
Dan Carpenter | faea13b7 | 2013-08-21 09:33:30 +0100 | [diff] [blame] | 381 | #define INVALID_IRPTNDX 0xff |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 382 | |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 383 | #define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx) |
| 384 | #define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1) |
| 385 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 386 | struct arm_smmu_domain { |
| 387 | /* |
| 388 | * A domain can span across multiple, chained SMMUs and requires |
| 389 | * all devices within the domain to follow the same translation |
| 390 | * path. |
| 391 | */ |
| 392 | struct arm_smmu_device *leaf_smmu; |
| 393 | struct arm_smmu_cfg root_cfg; |
| 394 | phys_addr_t output_mask; |
| 395 | |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 396 | spinlock_t lock; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | static DEFINE_SPINLOCK(arm_smmu_devices_lock); |
| 400 | static LIST_HEAD(arm_smmu_devices); |
| 401 | |
| 402 | static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, |
| 403 | struct device_node *dev_node) |
| 404 | { |
| 405 | struct rb_node *node = smmu->masters.rb_node; |
| 406 | |
| 407 | while (node) { |
| 408 | struct arm_smmu_master *master; |
| 409 | master = container_of(node, struct arm_smmu_master, node); |
| 410 | |
| 411 | if (dev_node < master->of_node) |
| 412 | node = node->rb_left; |
| 413 | else if (dev_node > master->of_node) |
| 414 | node = node->rb_right; |
| 415 | else |
| 416 | return master; |
| 417 | } |
| 418 | |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | static int insert_smmu_master(struct arm_smmu_device *smmu, |
| 423 | struct arm_smmu_master *master) |
| 424 | { |
| 425 | struct rb_node **new, *parent; |
| 426 | |
| 427 | new = &smmu->masters.rb_node; |
| 428 | parent = NULL; |
| 429 | while (*new) { |
| 430 | struct arm_smmu_master *this; |
| 431 | this = container_of(*new, struct arm_smmu_master, node); |
| 432 | |
| 433 | parent = *new; |
| 434 | if (master->of_node < this->of_node) |
| 435 | new = &((*new)->rb_left); |
| 436 | else if (master->of_node > this->of_node) |
| 437 | new = &((*new)->rb_right); |
| 438 | else |
| 439 | return -EEXIST; |
| 440 | } |
| 441 | |
| 442 | rb_link_node(&master->node, parent, new); |
| 443 | rb_insert_color(&master->node, &smmu->masters); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int register_smmu_master(struct arm_smmu_device *smmu, |
| 448 | struct device *dev, |
| 449 | struct of_phandle_args *masterspec) |
| 450 | { |
| 451 | int i; |
| 452 | struct arm_smmu_master *master; |
| 453 | |
| 454 | master = find_smmu_master(smmu, masterspec->np); |
| 455 | if (master) { |
| 456 | dev_err(dev, |
| 457 | "rejecting multiple registrations for master device %s\n", |
| 458 | masterspec->np->name); |
| 459 | return -EBUSY; |
| 460 | } |
| 461 | |
| 462 | if (masterspec->args_count > MAX_MASTER_STREAMIDS) { |
| 463 | dev_err(dev, |
| 464 | "reached maximum number (%d) of stream IDs for master device %s\n", |
| 465 | MAX_MASTER_STREAMIDS, masterspec->np->name); |
| 466 | return -ENOSPC; |
| 467 | } |
| 468 | |
| 469 | master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL); |
| 470 | if (!master) |
| 471 | return -ENOMEM; |
| 472 | |
| 473 | master->of_node = masterspec->np; |
| 474 | master->num_streamids = masterspec->args_count; |
| 475 | |
| 476 | for (i = 0; i < master->num_streamids; ++i) |
| 477 | master->streamids[i] = masterspec->args[i]; |
| 478 | |
| 479 | return insert_smmu_master(smmu, master); |
| 480 | } |
| 481 | |
| 482 | static struct arm_smmu_device *find_parent_smmu(struct arm_smmu_device *smmu) |
| 483 | { |
| 484 | struct arm_smmu_device *parent; |
| 485 | |
| 486 | if (!smmu->parent_of_node) |
| 487 | return NULL; |
| 488 | |
| 489 | spin_lock(&arm_smmu_devices_lock); |
| 490 | list_for_each_entry(parent, &arm_smmu_devices, list) |
| 491 | if (parent->dev->of_node == smmu->parent_of_node) |
| 492 | goto out_unlock; |
| 493 | |
| 494 | parent = NULL; |
| 495 | dev_warn(smmu->dev, |
| 496 | "Failed to find SMMU parent despite parent in DT\n"); |
| 497 | out_unlock: |
| 498 | spin_unlock(&arm_smmu_devices_lock); |
| 499 | return parent; |
| 500 | } |
| 501 | |
| 502 | static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end) |
| 503 | { |
| 504 | int idx; |
| 505 | |
| 506 | do { |
| 507 | idx = find_next_zero_bit(map, end, start); |
| 508 | if (idx == end) |
| 509 | return -ENOSPC; |
| 510 | } while (test_and_set_bit(idx, map)); |
| 511 | |
| 512 | return idx; |
| 513 | } |
| 514 | |
| 515 | static void __arm_smmu_free_bitmap(unsigned long *map, int idx) |
| 516 | { |
| 517 | clear_bit(idx, map); |
| 518 | } |
| 519 | |
| 520 | /* Wait for any pending TLB invalidations to complete */ |
| 521 | static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu) |
| 522 | { |
| 523 | int count = 0; |
| 524 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 525 | |
| 526 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC); |
| 527 | while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS) |
| 528 | & sTLBGSTATUS_GSACTIVE) { |
| 529 | cpu_relax(); |
| 530 | if (++count == TLB_LOOP_TIMEOUT) { |
| 531 | dev_err_ratelimited(smmu->dev, |
| 532 | "TLB sync timed out -- SMMU may be deadlocked\n"); |
| 533 | return; |
| 534 | } |
| 535 | udelay(1); |
| 536 | } |
| 537 | } |
| 538 | |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 539 | static void arm_smmu_tlb_inv_context(struct arm_smmu_cfg *cfg) |
| 540 | { |
| 541 | struct arm_smmu_device *smmu = cfg->smmu; |
| 542 | void __iomem *base = ARM_SMMU_GR0(smmu); |
| 543 | bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; |
| 544 | |
| 545 | if (stage1) { |
| 546 | base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 547 | writel_relaxed(ARM_SMMU_CB_ASID(cfg), |
| 548 | base + ARM_SMMU_CB_S1_TLBIASID); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 549 | } else { |
| 550 | base = ARM_SMMU_GR0(smmu); |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 551 | writel_relaxed(ARM_SMMU_CB_VMID(cfg), |
| 552 | base + ARM_SMMU_GR0_TLBIVMID); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | arm_smmu_tlb_sync(smmu); |
| 556 | } |
| 557 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 558 | static irqreturn_t arm_smmu_context_fault(int irq, void *dev) |
| 559 | { |
| 560 | int flags, ret; |
| 561 | u32 fsr, far, fsynr, resume; |
| 562 | unsigned long iova; |
| 563 | struct iommu_domain *domain = dev; |
| 564 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 565 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
| 566 | struct arm_smmu_device *smmu = root_cfg->smmu; |
| 567 | void __iomem *cb_base; |
| 568 | |
| 569 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx); |
| 570 | fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR); |
| 571 | |
| 572 | if (!(fsr & FSR_FAULT)) |
| 573 | return IRQ_NONE; |
| 574 | |
| 575 | if (fsr & FSR_IGN) |
| 576 | dev_err_ratelimited(smmu->dev, |
| 577 | "Unexpected context fault (fsr 0x%u)\n", |
| 578 | fsr); |
| 579 | |
| 580 | fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0); |
| 581 | flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ; |
| 582 | |
| 583 | far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO); |
| 584 | iova = far; |
| 585 | #ifdef CONFIG_64BIT |
| 586 | far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI); |
| 587 | iova |= ((unsigned long)far << 32); |
| 588 | #endif |
| 589 | |
| 590 | if (!report_iommu_fault(domain, smmu->dev, iova, flags)) { |
| 591 | ret = IRQ_HANDLED; |
| 592 | resume = RESUME_RETRY; |
| 593 | } else { |
Andreas Herrmann | 2ef0f03 | 2013-10-01 13:39:08 +0100 | [diff] [blame] | 594 | dev_err_ratelimited(smmu->dev, |
| 595 | "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n", |
| 596 | iova, fsynr, root_cfg->cbndx); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 597 | ret = IRQ_NONE; |
| 598 | resume = RESUME_TERMINATE; |
| 599 | } |
| 600 | |
| 601 | /* Clear the faulting FSR */ |
| 602 | writel(fsr, cb_base + ARM_SMMU_CB_FSR); |
| 603 | |
| 604 | /* Retry or terminate any stalled transactions */ |
| 605 | if (fsr & FSR_SS) |
| 606 | writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME); |
| 607 | |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static irqreturn_t arm_smmu_global_fault(int irq, void *dev) |
| 612 | { |
| 613 | u32 gfsr, gfsynr0, gfsynr1, gfsynr2; |
| 614 | struct arm_smmu_device *smmu = dev; |
| 615 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 616 | |
| 617 | gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR); |
Will Deacon | adaba32 | 2013-07-31 19:21:26 +0100 | [diff] [blame] | 618 | if (!gfsr) |
| 619 | return IRQ_NONE; |
| 620 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 621 | gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0); |
| 622 | gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1); |
| 623 | gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2); |
| 624 | |
| 625 | dev_err_ratelimited(smmu->dev, |
| 626 | "Unexpected global fault, this could be serious\n"); |
| 627 | dev_err_ratelimited(smmu->dev, |
| 628 | "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n", |
| 629 | gfsr, gfsynr0, gfsynr1, gfsynr2); |
| 630 | |
| 631 | writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR); |
Will Deacon | adaba32 | 2013-07-31 19:21:26 +0100 | [diff] [blame] | 632 | return IRQ_HANDLED; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) |
| 636 | { |
| 637 | u32 reg; |
| 638 | bool stage1; |
| 639 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
| 640 | struct arm_smmu_device *smmu = root_cfg->smmu; |
| 641 | void __iomem *cb_base, *gr0_base, *gr1_base; |
| 642 | |
| 643 | gr0_base = ARM_SMMU_GR0(smmu); |
| 644 | gr1_base = ARM_SMMU_GR1(smmu); |
| 645 | stage1 = root_cfg->cbar != CBAR_TYPE_S2_TRANS; |
| 646 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx); |
| 647 | |
| 648 | /* CBAR */ |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 649 | reg = root_cfg->cbar; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 650 | if (smmu->version == 1) |
| 651 | reg |= root_cfg->irptndx << CBAR_IRPTNDX_SHIFT; |
| 652 | |
| 653 | /* Use the weakest memory type, so it is overridden by the pte */ |
| 654 | if (stage1) |
| 655 | reg |= (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 656 | else |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 657 | reg |= ARM_SMMU_CB_VMID(root_cfg) << CBAR_VMID_SHIFT; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 658 | writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(root_cfg->cbndx)); |
| 659 | |
| 660 | if (smmu->version > 1) { |
| 661 | /* CBA2R */ |
| 662 | #ifdef CONFIG_64BIT |
| 663 | reg = CBA2R_RW64_64BIT; |
| 664 | #else |
| 665 | reg = CBA2R_RW64_32BIT; |
| 666 | #endif |
| 667 | writel_relaxed(reg, |
| 668 | gr1_base + ARM_SMMU_GR1_CBA2R(root_cfg->cbndx)); |
| 669 | |
| 670 | /* TTBCR2 */ |
| 671 | switch (smmu->input_size) { |
| 672 | case 32: |
| 673 | reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT); |
| 674 | break; |
| 675 | case 36: |
| 676 | reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT); |
| 677 | break; |
| 678 | case 39: |
| 679 | reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT); |
| 680 | break; |
| 681 | case 42: |
| 682 | reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT); |
| 683 | break; |
| 684 | case 44: |
| 685 | reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT); |
| 686 | break; |
| 687 | case 48: |
| 688 | reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT); |
| 689 | break; |
| 690 | } |
| 691 | |
| 692 | switch (smmu->s1_output_size) { |
| 693 | case 32: |
| 694 | reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT); |
| 695 | break; |
| 696 | case 36: |
| 697 | reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT); |
| 698 | break; |
| 699 | case 39: |
| 700 | reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT); |
| 701 | break; |
| 702 | case 42: |
| 703 | reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT); |
| 704 | break; |
| 705 | case 44: |
| 706 | reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT); |
| 707 | break; |
| 708 | case 48: |
| 709 | reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT); |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | if (stage1) |
| 714 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2); |
| 715 | } |
| 716 | |
| 717 | /* TTBR0 */ |
| 718 | reg = __pa(root_cfg->pgd); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 719 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO); |
| 720 | reg = (phys_addr_t)__pa(root_cfg->pgd) >> 32; |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 721 | if (stage1) |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 722 | reg |= ARM_SMMU_CB_ASID(root_cfg) << TTBRn_HI_ASID_SHIFT; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 723 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * TTBCR |
| 727 | * We use long descriptor, with inner-shareable WBWA tables in TTBR0. |
| 728 | */ |
| 729 | if (smmu->version > 1) { |
| 730 | if (PAGE_SIZE == SZ_4K) |
| 731 | reg = TTBCR_TG0_4K; |
| 732 | else |
| 733 | reg = TTBCR_TG0_64K; |
| 734 | |
| 735 | if (!stage1) { |
| 736 | switch (smmu->s2_output_size) { |
| 737 | case 32: |
| 738 | reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT); |
| 739 | break; |
| 740 | case 36: |
| 741 | reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT); |
| 742 | break; |
| 743 | case 40: |
| 744 | reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT); |
| 745 | break; |
| 746 | case 42: |
| 747 | reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT); |
| 748 | break; |
| 749 | case 44: |
| 750 | reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT); |
| 751 | break; |
| 752 | case 48: |
| 753 | reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT); |
| 754 | break; |
| 755 | } |
| 756 | } else { |
| 757 | reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT; |
| 758 | } |
| 759 | } else { |
| 760 | reg = 0; |
| 761 | } |
| 762 | |
| 763 | reg |= TTBCR_EAE | |
| 764 | (TTBCR_SH_IS << TTBCR_SH0_SHIFT) | |
| 765 | (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) | |
| 766 | (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT) | |
| 767 | (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT); |
| 768 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR); |
| 769 | |
| 770 | /* MAIR0 (stage-1 only) */ |
| 771 | if (stage1) { |
| 772 | reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) | |
| 773 | (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) | |
| 774 | (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV)); |
| 775 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0); |
| 776 | } |
| 777 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 778 | /* SCTLR */ |
| 779 | reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP; |
| 780 | if (stage1) |
| 781 | reg |= SCTLR_S1_ASIDPNE; |
| 782 | #ifdef __BIG_ENDIAN |
| 783 | reg |= SCTLR_E; |
| 784 | #endif |
Will Deacon | 2572484 | 2013-08-21 13:49:53 +0100 | [diff] [blame] | 785 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static int arm_smmu_init_domain_context(struct iommu_domain *domain, |
| 789 | struct device *dev) |
| 790 | { |
| 791 | int irq, ret, start; |
| 792 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 793 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
| 794 | struct arm_smmu_device *smmu, *parent; |
| 795 | |
| 796 | /* |
| 797 | * Walk the SMMU chain to find the root device for this chain. |
| 798 | * We assume that no masters have translations which terminate |
| 799 | * early, and therefore check that the root SMMU does indeed have |
| 800 | * a StreamID for the master in question. |
| 801 | */ |
| 802 | parent = dev->archdata.iommu; |
| 803 | smmu_domain->output_mask = -1; |
| 804 | do { |
| 805 | smmu = parent; |
| 806 | smmu_domain->output_mask &= (1ULL << smmu->s2_output_size) - 1; |
| 807 | } while ((parent = find_parent_smmu(smmu))); |
| 808 | |
| 809 | if (!find_smmu_master(smmu, dev->of_node)) { |
| 810 | dev_err(dev, "unable to find root SMMU for device\n"); |
| 811 | return -ENODEV; |
| 812 | } |
| 813 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 814 | if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) { |
| 815 | /* |
| 816 | * We will likely want to change this if/when KVM gets |
| 817 | * involved. |
| 818 | */ |
| 819 | root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS; |
| 820 | start = smmu->num_s2_context_banks; |
| 821 | } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) { |
| 822 | root_cfg->cbar = CBAR_TYPE_S2_TRANS; |
| 823 | start = 0; |
| 824 | } else { |
| 825 | root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS; |
| 826 | start = smmu->num_s2_context_banks; |
| 827 | } |
| 828 | |
| 829 | ret = __arm_smmu_alloc_bitmap(smmu->context_map, start, |
| 830 | smmu->num_context_banks); |
| 831 | if (IS_ERR_VALUE(ret)) |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 832 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 833 | |
| 834 | root_cfg->cbndx = ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 835 | if (smmu->version == 1) { |
| 836 | root_cfg->irptndx = atomic_inc_return(&smmu->irptndx); |
| 837 | root_cfg->irptndx %= smmu->num_context_irqs; |
| 838 | } else { |
| 839 | root_cfg->irptndx = root_cfg->cbndx; |
| 840 | } |
| 841 | |
| 842 | irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx]; |
| 843 | ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED, |
| 844 | "arm-smmu-context-fault", domain); |
| 845 | if (IS_ERR_VALUE(ret)) { |
| 846 | dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n", |
| 847 | root_cfg->irptndx, irq); |
Dan Carpenter | faea13b7 | 2013-08-21 09:33:30 +0100 | [diff] [blame] | 848 | root_cfg->irptndx = INVALID_IRPTNDX; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 849 | goto out_free_context; |
| 850 | } |
| 851 | |
| 852 | root_cfg->smmu = smmu; |
| 853 | arm_smmu_init_context_bank(smmu_domain); |
| 854 | return ret; |
| 855 | |
| 856 | out_free_context: |
| 857 | __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | static void arm_smmu_destroy_domain_context(struct iommu_domain *domain) |
| 862 | { |
| 863 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 864 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
| 865 | struct arm_smmu_device *smmu = root_cfg->smmu; |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 866 | void __iomem *cb_base; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 867 | int irq; |
| 868 | |
| 869 | if (!smmu) |
| 870 | return; |
| 871 | |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 872 | /* Disable the context bank and nuke the TLB before freeing it. */ |
| 873 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx); |
| 874 | writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); |
| 875 | arm_smmu_tlb_inv_context(root_cfg); |
| 876 | |
Dan Carpenter | faea13b7 | 2013-08-21 09:33:30 +0100 | [diff] [blame] | 877 | if (root_cfg->irptndx != INVALID_IRPTNDX) { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 878 | irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx]; |
| 879 | free_irq(irq, domain); |
| 880 | } |
| 881 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 882 | __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx); |
| 883 | } |
| 884 | |
| 885 | static int arm_smmu_domain_init(struct iommu_domain *domain) |
| 886 | { |
| 887 | struct arm_smmu_domain *smmu_domain; |
| 888 | pgd_t *pgd; |
| 889 | |
| 890 | /* |
| 891 | * Allocate the domain and initialise some of its data structures. |
| 892 | * We can't really do anything meaningful until we've added a |
| 893 | * master. |
| 894 | */ |
| 895 | smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL); |
| 896 | if (!smmu_domain) |
| 897 | return -ENOMEM; |
| 898 | |
| 899 | pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL); |
| 900 | if (!pgd) |
| 901 | goto out_free_domain; |
| 902 | smmu_domain->root_cfg.pgd = pgd; |
| 903 | |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 904 | spin_lock_init(&smmu_domain->lock); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 905 | domain->priv = smmu_domain; |
| 906 | return 0; |
| 907 | |
| 908 | out_free_domain: |
| 909 | kfree(smmu_domain); |
| 910 | return -ENOMEM; |
| 911 | } |
| 912 | |
| 913 | static void arm_smmu_free_ptes(pmd_t *pmd) |
| 914 | { |
| 915 | pgtable_t table = pmd_pgtable(*pmd); |
| 916 | pgtable_page_dtor(table); |
| 917 | __free_page(table); |
| 918 | } |
| 919 | |
| 920 | static void arm_smmu_free_pmds(pud_t *pud) |
| 921 | { |
| 922 | int i; |
| 923 | pmd_t *pmd, *pmd_base = pmd_offset(pud, 0); |
| 924 | |
| 925 | pmd = pmd_base; |
| 926 | for (i = 0; i < PTRS_PER_PMD; ++i) { |
| 927 | if (pmd_none(*pmd)) |
| 928 | continue; |
| 929 | |
| 930 | arm_smmu_free_ptes(pmd); |
| 931 | pmd++; |
| 932 | } |
| 933 | |
| 934 | pmd_free(NULL, pmd_base); |
| 935 | } |
| 936 | |
| 937 | static void arm_smmu_free_puds(pgd_t *pgd) |
| 938 | { |
| 939 | int i; |
| 940 | pud_t *pud, *pud_base = pud_offset(pgd, 0); |
| 941 | |
| 942 | pud = pud_base; |
| 943 | for (i = 0; i < PTRS_PER_PUD; ++i) { |
| 944 | if (pud_none(*pud)) |
| 945 | continue; |
| 946 | |
| 947 | arm_smmu_free_pmds(pud); |
| 948 | pud++; |
| 949 | } |
| 950 | |
| 951 | pud_free(NULL, pud_base); |
| 952 | } |
| 953 | |
| 954 | static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain) |
| 955 | { |
| 956 | int i; |
| 957 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
| 958 | pgd_t *pgd, *pgd_base = root_cfg->pgd; |
| 959 | |
| 960 | /* |
| 961 | * Recursively free the page tables for this domain. We don't |
| 962 | * care about speculative TLB filling, because the TLB will be |
| 963 | * nuked next time this context bank is re-allocated and no devices |
| 964 | * currently map to these tables. |
| 965 | */ |
| 966 | pgd = pgd_base; |
| 967 | for (i = 0; i < PTRS_PER_PGD; ++i) { |
| 968 | if (pgd_none(*pgd)) |
| 969 | continue; |
| 970 | arm_smmu_free_puds(pgd); |
| 971 | pgd++; |
| 972 | } |
| 973 | |
| 974 | kfree(pgd_base); |
| 975 | } |
| 976 | |
| 977 | static void arm_smmu_domain_destroy(struct iommu_domain *domain) |
| 978 | { |
| 979 | struct arm_smmu_domain *smmu_domain = domain->priv; |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 980 | |
| 981 | /* |
| 982 | * Free the domain resources. We assume that all devices have |
| 983 | * already been detached. |
| 984 | */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 985 | arm_smmu_destroy_domain_context(domain); |
| 986 | arm_smmu_free_pgtables(smmu_domain); |
| 987 | kfree(smmu_domain); |
| 988 | } |
| 989 | |
| 990 | static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu, |
| 991 | struct arm_smmu_master *master) |
| 992 | { |
| 993 | int i; |
| 994 | struct arm_smmu_smr *smrs; |
| 995 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 996 | |
| 997 | if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH)) |
| 998 | return 0; |
| 999 | |
| 1000 | if (master->smrs) |
| 1001 | return -EEXIST; |
| 1002 | |
| 1003 | smrs = kmalloc(sizeof(*smrs) * master->num_streamids, GFP_KERNEL); |
| 1004 | if (!smrs) { |
| 1005 | dev_err(smmu->dev, "failed to allocate %d SMRs for master %s\n", |
| 1006 | master->num_streamids, master->of_node->name); |
| 1007 | return -ENOMEM; |
| 1008 | } |
| 1009 | |
| 1010 | /* Allocate the SMRs on the root SMMU */ |
| 1011 | for (i = 0; i < master->num_streamids; ++i) { |
| 1012 | int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0, |
| 1013 | smmu->num_mapping_groups); |
| 1014 | if (IS_ERR_VALUE(idx)) { |
| 1015 | dev_err(smmu->dev, "failed to allocate free SMR\n"); |
| 1016 | goto err_free_smrs; |
| 1017 | } |
| 1018 | |
| 1019 | smrs[i] = (struct arm_smmu_smr) { |
| 1020 | .idx = idx, |
| 1021 | .mask = 0, /* We don't currently share SMRs */ |
| 1022 | .id = master->streamids[i], |
| 1023 | }; |
| 1024 | } |
| 1025 | |
| 1026 | /* It worked! Now, poke the actual hardware */ |
| 1027 | for (i = 0; i < master->num_streamids; ++i) { |
| 1028 | u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT | |
| 1029 | smrs[i].mask << SMR_MASK_SHIFT; |
| 1030 | writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx)); |
| 1031 | } |
| 1032 | |
| 1033 | master->smrs = smrs; |
| 1034 | return 0; |
| 1035 | |
| 1036 | err_free_smrs: |
| 1037 | while (--i >= 0) |
| 1038 | __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx); |
| 1039 | kfree(smrs); |
| 1040 | return -ENOSPC; |
| 1041 | } |
| 1042 | |
| 1043 | static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu, |
| 1044 | struct arm_smmu_master *master) |
| 1045 | { |
| 1046 | int i; |
| 1047 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 1048 | struct arm_smmu_smr *smrs = master->smrs; |
| 1049 | |
| 1050 | /* Invalidate the SMRs before freeing back to the allocator */ |
| 1051 | for (i = 0; i < master->num_streamids; ++i) { |
| 1052 | u8 idx = smrs[i].idx; |
| 1053 | writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx)); |
| 1054 | __arm_smmu_free_bitmap(smmu->smr_map, idx); |
| 1055 | } |
| 1056 | |
| 1057 | master->smrs = NULL; |
| 1058 | kfree(smrs); |
| 1059 | } |
| 1060 | |
| 1061 | static void arm_smmu_bypass_stream_mapping(struct arm_smmu_device *smmu, |
| 1062 | struct arm_smmu_master *master) |
| 1063 | { |
| 1064 | int i; |
| 1065 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 1066 | |
| 1067 | for (i = 0; i < master->num_streamids; ++i) { |
| 1068 | u16 sid = master->streamids[i]; |
| 1069 | writel_relaxed(S2CR_TYPE_BYPASS, |
| 1070 | gr0_base + ARM_SMMU_GR0_S2CR(sid)); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain, |
| 1075 | struct arm_smmu_master *master) |
| 1076 | { |
| 1077 | int i, ret; |
| 1078 | struct arm_smmu_device *parent, *smmu = smmu_domain->root_cfg.smmu; |
| 1079 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 1080 | |
| 1081 | ret = arm_smmu_master_configure_smrs(smmu, master); |
| 1082 | if (ret) |
| 1083 | return ret; |
| 1084 | |
| 1085 | /* Bypass the leaves */ |
| 1086 | smmu = smmu_domain->leaf_smmu; |
| 1087 | while ((parent = find_parent_smmu(smmu))) { |
| 1088 | /* |
| 1089 | * We won't have a StreamID match for anything but the root |
| 1090 | * smmu, so we only need to worry about StreamID indexing, |
| 1091 | * where we must install bypass entries in the S2CRs. |
| 1092 | */ |
| 1093 | if (smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) |
| 1094 | continue; |
| 1095 | |
| 1096 | arm_smmu_bypass_stream_mapping(smmu, master); |
| 1097 | smmu = parent; |
| 1098 | } |
| 1099 | |
| 1100 | /* Now we're at the root, time to point at our context bank */ |
| 1101 | for (i = 0; i < master->num_streamids; ++i) { |
| 1102 | u32 idx, s2cr; |
| 1103 | idx = master->smrs ? master->smrs[i].idx : master->streamids[i]; |
| 1104 | s2cr = (S2CR_TYPE_TRANS << S2CR_TYPE_SHIFT) | |
| 1105 | (smmu_domain->root_cfg.cbndx << S2CR_CBNDX_SHIFT); |
| 1106 | writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx)); |
| 1107 | } |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain, |
| 1113 | struct arm_smmu_master *master) |
| 1114 | { |
| 1115 | struct arm_smmu_device *smmu = smmu_domain->root_cfg.smmu; |
| 1116 | |
| 1117 | /* |
| 1118 | * We *must* clear the S2CR first, because freeing the SMR means |
| 1119 | * that it can be re-allocated immediately. |
| 1120 | */ |
| 1121 | arm_smmu_bypass_stream_mapping(smmu, master); |
| 1122 | arm_smmu_master_free_smrs(smmu, master); |
| 1123 | } |
| 1124 | |
| 1125 | static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 1126 | { |
| 1127 | int ret = -EINVAL; |
| 1128 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 1129 | struct arm_smmu_device *device_smmu = dev->archdata.iommu; |
| 1130 | struct arm_smmu_master *master; |
| 1131 | |
| 1132 | if (!device_smmu) { |
| 1133 | dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); |
| 1134 | return -ENXIO; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Sanity check the domain. We don't currently support domains |
| 1139 | * that cross between different SMMU chains. |
| 1140 | */ |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1141 | spin_lock(&smmu_domain->lock); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1142 | if (!smmu_domain->leaf_smmu) { |
| 1143 | /* Now that we have a master, we can finalise the domain */ |
| 1144 | ret = arm_smmu_init_domain_context(domain, dev); |
| 1145 | if (IS_ERR_VALUE(ret)) |
| 1146 | goto err_unlock; |
| 1147 | |
| 1148 | smmu_domain->leaf_smmu = device_smmu; |
| 1149 | } else if (smmu_domain->leaf_smmu != device_smmu) { |
| 1150 | dev_err(dev, |
| 1151 | "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n", |
| 1152 | dev_name(smmu_domain->leaf_smmu->dev), |
| 1153 | dev_name(device_smmu->dev)); |
| 1154 | goto err_unlock; |
| 1155 | } |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1156 | spin_unlock(&smmu_domain->lock); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1157 | |
| 1158 | /* Looks ok, so add the device to the domain */ |
| 1159 | master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node); |
| 1160 | if (!master) |
| 1161 | return -ENODEV; |
| 1162 | |
| 1163 | return arm_smmu_domain_add_master(smmu_domain, master); |
| 1164 | |
| 1165 | err_unlock: |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1166 | spin_unlock(&smmu_domain->lock); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1167 | return ret; |
| 1168 | } |
| 1169 | |
| 1170 | static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) |
| 1171 | { |
| 1172 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 1173 | struct arm_smmu_master *master; |
| 1174 | |
| 1175 | master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node); |
| 1176 | if (master) |
| 1177 | arm_smmu_domain_remove_master(smmu_domain, master); |
| 1178 | } |
| 1179 | |
| 1180 | static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr, |
| 1181 | size_t size) |
| 1182 | { |
| 1183 | unsigned long offset = (unsigned long)addr & ~PAGE_MASK; |
| 1184 | |
| 1185 | /* |
| 1186 | * If the SMMU can't walk tables in the CPU caches, treat them |
| 1187 | * like non-coherent DMA since we need to flush the new entries |
| 1188 | * all the way out to memory. There's no possibility of recursion |
| 1189 | * here as the SMMU table walker will not be wired through another |
| 1190 | * SMMU. |
| 1191 | */ |
| 1192 | if (!(smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)) |
| 1193 | dma_map_page(smmu->dev, virt_to_page(addr), offset, size, |
| 1194 | DMA_TO_DEVICE); |
| 1195 | } |
| 1196 | |
| 1197 | static bool arm_smmu_pte_is_contiguous_range(unsigned long addr, |
| 1198 | unsigned long end) |
| 1199 | { |
| 1200 | return !(addr & ~ARM_SMMU_PTE_CONT_MASK) && |
| 1201 | (addr + ARM_SMMU_PTE_CONT_SIZE <= end); |
| 1202 | } |
| 1203 | |
| 1204 | static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd, |
| 1205 | unsigned long addr, unsigned long end, |
| 1206 | unsigned long pfn, int flags, int stage) |
| 1207 | { |
| 1208 | pte_t *pte, *start; |
Will Deacon | cf2d45b | 2013-11-05 16:32:00 +0000 | [diff] [blame] | 1209 | pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1210 | |
| 1211 | if (pmd_none(*pmd)) { |
| 1212 | /* Allocate a new set of tables */ |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1213 | pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1214 | if (!table) |
| 1215 | return -ENOMEM; |
| 1216 | |
| 1217 | arm_smmu_flush_pgtable(smmu, page_address(table), |
| 1218 | ARM_SMMU_PTE_HWTABLE_SIZE); |
Kirill A. Shutemov | 01058e7 | 2013-11-14 14:31:49 -0800 | [diff] [blame] | 1219 | if (!pgtable_page_ctor(table)) { |
| 1220 | __free_page(table); |
| 1221 | return -ENOMEM; |
| 1222 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1223 | pmd_populate(NULL, pmd, table); |
| 1224 | arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd)); |
| 1225 | } |
| 1226 | |
| 1227 | if (stage == 1) { |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1228 | pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1229 | if (!(flags & IOMMU_WRITE) && (flags & IOMMU_READ)) |
| 1230 | pteval |= ARM_SMMU_PTE_AP_RDONLY; |
| 1231 | |
| 1232 | if (flags & IOMMU_CACHE) |
| 1233 | pteval |= (MAIR_ATTR_IDX_CACHE << |
| 1234 | ARM_SMMU_PTE_ATTRINDX_SHIFT); |
| 1235 | } else { |
| 1236 | pteval |= ARM_SMMU_PTE_HAP_FAULT; |
| 1237 | if (flags & IOMMU_READ) |
| 1238 | pteval |= ARM_SMMU_PTE_HAP_READ; |
| 1239 | if (flags & IOMMU_WRITE) |
| 1240 | pteval |= ARM_SMMU_PTE_HAP_WRITE; |
| 1241 | if (flags & IOMMU_CACHE) |
| 1242 | pteval |= ARM_SMMU_PTE_MEMATTR_OIWB; |
| 1243 | else |
| 1244 | pteval |= ARM_SMMU_PTE_MEMATTR_NC; |
| 1245 | } |
| 1246 | |
| 1247 | /* If no access, create a faulting entry to avoid TLB fills */ |
Will Deacon | cf2d45b | 2013-11-05 16:32:00 +0000 | [diff] [blame] | 1248 | if (flags & IOMMU_EXEC) |
| 1249 | pteval &= ~ARM_SMMU_PTE_XN; |
| 1250 | else if (!(flags & (IOMMU_READ | IOMMU_WRITE))) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1251 | pteval &= ~ARM_SMMU_PTE_PAGE; |
| 1252 | |
| 1253 | pteval |= ARM_SMMU_PTE_SH_IS; |
| 1254 | start = pmd_page_vaddr(*pmd) + pte_index(addr); |
| 1255 | pte = start; |
| 1256 | |
| 1257 | /* |
| 1258 | * Install the page table entries. This is fairly complicated |
| 1259 | * since we attempt to make use of the contiguous hint in the |
| 1260 | * ptes where possible. The contiguous hint indicates a series |
| 1261 | * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically |
| 1262 | * contiguous region with the following constraints: |
| 1263 | * |
| 1264 | * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE |
| 1265 | * - Each pte in the region has the contiguous hint bit set |
| 1266 | * |
| 1267 | * This complicates unmapping (also handled by this code, when |
| 1268 | * neither IOMMU_READ or IOMMU_WRITE are set) because it is |
| 1269 | * possible, yet highly unlikely, that a client may unmap only |
| 1270 | * part of a contiguous range. This requires clearing of the |
| 1271 | * contiguous hint bits in the range before installing the new |
| 1272 | * faulting entries. |
| 1273 | * |
| 1274 | * Note that re-mapping an address range without first unmapping |
| 1275 | * it is not supported, so TLB invalidation is not required here |
| 1276 | * and is instead performed at unmap and domain-init time. |
| 1277 | */ |
| 1278 | do { |
| 1279 | int i = 1; |
| 1280 | pteval &= ~ARM_SMMU_PTE_CONT; |
| 1281 | |
| 1282 | if (arm_smmu_pte_is_contiguous_range(addr, end)) { |
| 1283 | i = ARM_SMMU_PTE_CONT_ENTRIES; |
| 1284 | pteval |= ARM_SMMU_PTE_CONT; |
| 1285 | } else if (pte_val(*pte) & |
| 1286 | (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) { |
| 1287 | int j; |
| 1288 | pte_t *cont_start; |
| 1289 | unsigned long idx = pte_index(addr); |
| 1290 | |
| 1291 | idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1); |
| 1292 | cont_start = pmd_page_vaddr(*pmd) + idx; |
| 1293 | for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j) |
| 1294 | pte_val(*(cont_start + j)) &= ~ARM_SMMU_PTE_CONT; |
| 1295 | |
| 1296 | arm_smmu_flush_pgtable(smmu, cont_start, |
| 1297 | sizeof(*pte) * |
| 1298 | ARM_SMMU_PTE_CONT_ENTRIES); |
| 1299 | } |
| 1300 | |
| 1301 | do { |
| 1302 | *pte = pfn_pte(pfn, __pgprot(pteval)); |
| 1303 | } while (pte++, pfn++, addr += PAGE_SIZE, --i); |
| 1304 | } while (addr != end); |
| 1305 | |
| 1306 | arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start)); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud, |
| 1311 | unsigned long addr, unsigned long end, |
| 1312 | phys_addr_t phys, int flags, int stage) |
| 1313 | { |
| 1314 | int ret; |
| 1315 | pmd_t *pmd; |
| 1316 | unsigned long next, pfn = __phys_to_pfn(phys); |
| 1317 | |
| 1318 | #ifndef __PAGETABLE_PMD_FOLDED |
| 1319 | if (pud_none(*pud)) { |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1320 | pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1321 | if (!pmd) |
| 1322 | return -ENOMEM; |
Yifan Zhang | 97a6442 | 2014-01-03 12:01:26 +0000 | [diff] [blame] | 1323 | |
| 1324 | pud_populate(NULL, pud, pmd); |
| 1325 | arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud)); |
| 1326 | |
| 1327 | pmd += pmd_index(addr); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1328 | } else |
| 1329 | #endif |
| 1330 | pmd = pmd_offset(pud, addr); |
| 1331 | |
| 1332 | do { |
| 1333 | next = pmd_addr_end(addr, end); |
| 1334 | ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, end, pfn, |
| 1335 | flags, stage); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1336 | phys += next - addr; |
| 1337 | } while (pmd++, addr = next, addr < end); |
| 1338 | |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd, |
| 1343 | unsigned long addr, unsigned long end, |
| 1344 | phys_addr_t phys, int flags, int stage) |
| 1345 | { |
| 1346 | int ret = 0; |
| 1347 | pud_t *pud; |
| 1348 | unsigned long next; |
| 1349 | |
| 1350 | #ifndef __PAGETABLE_PUD_FOLDED |
| 1351 | if (pgd_none(*pgd)) { |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1352 | pud = (pud_t *)get_zeroed_page(GFP_ATOMIC); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1353 | if (!pud) |
| 1354 | return -ENOMEM; |
Yifan Zhang | 97a6442 | 2014-01-03 12:01:26 +0000 | [diff] [blame] | 1355 | |
| 1356 | pgd_populate(NULL, pgd, pud); |
| 1357 | arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd)); |
| 1358 | |
| 1359 | pud += pud_index(addr); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1360 | } else |
| 1361 | #endif |
| 1362 | pud = pud_offset(pgd, addr); |
| 1363 | |
| 1364 | do { |
| 1365 | next = pud_addr_end(addr, end); |
| 1366 | ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys, |
| 1367 | flags, stage); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1368 | phys += next - addr; |
| 1369 | } while (pud++, addr = next, addr < end); |
| 1370 | |
| 1371 | return ret; |
| 1372 | } |
| 1373 | |
| 1374 | static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain, |
| 1375 | unsigned long iova, phys_addr_t paddr, |
| 1376 | size_t size, int flags) |
| 1377 | { |
| 1378 | int ret, stage; |
| 1379 | unsigned long end; |
| 1380 | phys_addr_t input_mask, output_mask; |
| 1381 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
| 1382 | pgd_t *pgd = root_cfg->pgd; |
| 1383 | struct arm_smmu_device *smmu = root_cfg->smmu; |
| 1384 | |
| 1385 | if (root_cfg->cbar == CBAR_TYPE_S2_TRANS) { |
| 1386 | stage = 2; |
| 1387 | output_mask = (1ULL << smmu->s2_output_size) - 1; |
| 1388 | } else { |
| 1389 | stage = 1; |
| 1390 | output_mask = (1ULL << smmu->s1_output_size) - 1; |
| 1391 | } |
| 1392 | |
| 1393 | if (!pgd) |
| 1394 | return -EINVAL; |
| 1395 | |
| 1396 | if (size & ~PAGE_MASK) |
| 1397 | return -EINVAL; |
| 1398 | |
| 1399 | input_mask = (1ULL << smmu->input_size) - 1; |
| 1400 | if ((phys_addr_t)iova & ~input_mask) |
| 1401 | return -ERANGE; |
| 1402 | |
| 1403 | if (paddr & ~output_mask) |
| 1404 | return -ERANGE; |
| 1405 | |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1406 | spin_lock(&smmu_domain->lock); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1407 | pgd += pgd_index(iova); |
| 1408 | end = iova + size; |
| 1409 | do { |
| 1410 | unsigned long next = pgd_addr_end(iova, end); |
| 1411 | |
| 1412 | ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr, |
| 1413 | flags, stage); |
| 1414 | if (ret) |
| 1415 | goto out_unlock; |
| 1416 | |
| 1417 | paddr += next - iova; |
| 1418 | iova = next; |
| 1419 | } while (pgd++, iova != end); |
| 1420 | |
| 1421 | out_unlock: |
Will Deacon | c9d09e2 | 2014-02-04 22:12:42 +0000 | [diff] [blame^] | 1422 | spin_unlock(&smmu_domain->lock); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1423 | |
| 1424 | /* Ensure new page tables are visible to the hardware walker */ |
| 1425 | if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) |
| 1426 | dsb(); |
| 1427 | |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
| 1431 | static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova, |
| 1432 | phys_addr_t paddr, size_t size, int flags) |
| 1433 | { |
| 1434 | struct arm_smmu_domain *smmu_domain = domain->priv; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1435 | |
Will Deacon | 5552ecd | 2013-11-08 15:08:06 +0000 | [diff] [blame] | 1436 | if (!smmu_domain) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1437 | return -ENODEV; |
| 1438 | |
| 1439 | /* Check for silent address truncation up the SMMU chain. */ |
| 1440 | if ((phys_addr_t)iova & ~smmu_domain->output_mask) |
| 1441 | return -ERANGE; |
| 1442 | |
| 1443 | return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, flags); |
| 1444 | } |
| 1445 | |
| 1446 | static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 1447 | size_t size) |
| 1448 | { |
| 1449 | int ret; |
| 1450 | struct arm_smmu_domain *smmu_domain = domain->priv; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1451 | |
| 1452 | ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1453 | arm_smmu_tlb_inv_context(&smmu_domain->root_cfg); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1454 | return ret ? ret : size; |
| 1455 | } |
| 1456 | |
| 1457 | static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain, |
| 1458 | dma_addr_t iova) |
| 1459 | { |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1460 | pgd_t *pgdp, pgd; |
| 1461 | pud_t pud; |
| 1462 | pmd_t pmd; |
| 1463 | pte_t pte; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1464 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 1465 | struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1466 | |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1467 | pgdp = root_cfg->pgd; |
| 1468 | if (!pgdp) |
| 1469 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1470 | |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1471 | pgd = *(pgdp + pgd_index(iova)); |
| 1472 | if (pgd_none(pgd)) |
| 1473 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1474 | |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1475 | pud = *pud_offset(&pgd, iova); |
| 1476 | if (pud_none(pud)) |
| 1477 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1478 | |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1479 | pmd = *pmd_offset(&pud, iova); |
| 1480 | if (pmd_none(pmd)) |
| 1481 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1482 | |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1483 | pte = *(pmd_page_vaddr(pmd) + pte_index(iova)); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1484 | if (pte_none(pte)) |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1485 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1486 | |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 1487 | return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | static int arm_smmu_domain_has_cap(struct iommu_domain *domain, |
| 1491 | unsigned long cap) |
| 1492 | { |
| 1493 | unsigned long caps = 0; |
| 1494 | struct arm_smmu_domain *smmu_domain = domain->priv; |
| 1495 | |
| 1496 | if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) |
| 1497 | caps |= IOMMU_CAP_CACHE_COHERENCY; |
| 1498 | |
| 1499 | return !!(cap & caps); |
| 1500 | } |
| 1501 | |
| 1502 | static int arm_smmu_add_device(struct device *dev) |
| 1503 | { |
| 1504 | struct arm_smmu_device *child, *parent, *smmu; |
| 1505 | struct arm_smmu_master *master = NULL; |
Antonios Motakis | 5fc63a7 | 2013-10-18 16:08:29 +0100 | [diff] [blame] | 1506 | struct iommu_group *group; |
| 1507 | int ret; |
| 1508 | |
| 1509 | if (dev->archdata.iommu) { |
| 1510 | dev_warn(dev, "IOMMU driver already assigned to device\n"); |
| 1511 | return -EINVAL; |
| 1512 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1513 | |
| 1514 | spin_lock(&arm_smmu_devices_lock); |
| 1515 | list_for_each_entry(parent, &arm_smmu_devices, list) { |
| 1516 | smmu = parent; |
| 1517 | |
| 1518 | /* Try to find a child of the current SMMU. */ |
| 1519 | list_for_each_entry(child, &arm_smmu_devices, list) { |
| 1520 | if (child->parent_of_node == parent->dev->of_node) { |
| 1521 | /* Does the child sit above our master? */ |
| 1522 | master = find_smmu_master(child, dev->of_node); |
| 1523 | if (master) { |
| 1524 | smmu = NULL; |
| 1525 | break; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | /* We found some children, so keep searching. */ |
| 1531 | if (!smmu) { |
| 1532 | master = NULL; |
| 1533 | continue; |
| 1534 | } |
| 1535 | |
| 1536 | master = find_smmu_master(smmu, dev->of_node); |
| 1537 | if (master) |
| 1538 | break; |
| 1539 | } |
| 1540 | spin_unlock(&arm_smmu_devices_lock); |
| 1541 | |
| 1542 | if (!master) |
| 1543 | return -ENODEV; |
| 1544 | |
Antonios Motakis | 5fc63a7 | 2013-10-18 16:08:29 +0100 | [diff] [blame] | 1545 | group = iommu_group_alloc(); |
| 1546 | if (IS_ERR(group)) { |
| 1547 | dev_err(dev, "Failed to allocate IOMMU group\n"); |
| 1548 | return PTR_ERR(group); |
| 1549 | } |
| 1550 | |
| 1551 | ret = iommu_group_add_device(group, dev); |
| 1552 | iommu_group_put(group); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1553 | dev->archdata.iommu = smmu; |
Antonios Motakis | 5fc63a7 | 2013-10-18 16:08:29 +0100 | [diff] [blame] | 1554 | |
| 1555 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | static void arm_smmu_remove_device(struct device *dev) |
| 1559 | { |
| 1560 | dev->archdata.iommu = NULL; |
Antonios Motakis | 5fc63a7 | 2013-10-18 16:08:29 +0100 | [diff] [blame] | 1561 | iommu_group_remove_device(dev); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | static struct iommu_ops arm_smmu_ops = { |
| 1565 | .domain_init = arm_smmu_domain_init, |
| 1566 | .domain_destroy = arm_smmu_domain_destroy, |
| 1567 | .attach_dev = arm_smmu_attach_dev, |
| 1568 | .detach_dev = arm_smmu_detach_dev, |
| 1569 | .map = arm_smmu_map, |
| 1570 | .unmap = arm_smmu_unmap, |
| 1571 | .iova_to_phys = arm_smmu_iova_to_phys, |
| 1572 | .domain_has_cap = arm_smmu_domain_has_cap, |
| 1573 | .add_device = arm_smmu_add_device, |
| 1574 | .remove_device = arm_smmu_remove_device, |
| 1575 | .pgsize_bitmap = (SECTION_SIZE | |
| 1576 | ARM_SMMU_PTE_CONT_SIZE | |
| 1577 | PAGE_SIZE), |
| 1578 | }; |
| 1579 | |
| 1580 | static void arm_smmu_device_reset(struct arm_smmu_device *smmu) |
| 1581 | { |
| 1582 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1583 | void __iomem *cb_base; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1584 | int i = 0; |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1585 | u32 reg; |
| 1586 | |
| 1587 | /* Clear Global FSR */ |
| 1588 | reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR); |
| 1589 | writel(reg, gr0_base + ARM_SMMU_GR0_sGFSR); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1590 | |
| 1591 | /* Mark all SMRn as invalid and all S2CRn as bypass */ |
| 1592 | for (i = 0; i < smmu->num_mapping_groups; ++i) { |
| 1593 | writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(i)); |
| 1594 | writel_relaxed(S2CR_TYPE_BYPASS, gr0_base + ARM_SMMU_GR0_S2CR(i)); |
| 1595 | } |
| 1596 | |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1597 | /* Make sure all context banks are disabled and clear CB_FSR */ |
| 1598 | for (i = 0; i < smmu->num_context_banks; ++i) { |
| 1599 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i); |
| 1600 | writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); |
| 1601 | writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR); |
| 1602 | } |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1603 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1604 | /* Invalidate the TLB, just in case */ |
| 1605 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL); |
| 1606 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH); |
| 1607 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH); |
| 1608 | |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1609 | reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sCR0); |
| 1610 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1611 | /* Enable fault reporting */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1612 | reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1613 | |
| 1614 | /* Disable TLB broadcasting. */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1615 | reg |= (sCR0_VMIDPNE | sCR0_PTM); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1616 | |
| 1617 | /* Enable client access, but bypass when no mapping is found */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1618 | reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1619 | |
| 1620 | /* Disable forced broadcasting */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1621 | reg &= ~sCR0_FB; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1622 | |
| 1623 | /* Don't upgrade barriers */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1624 | reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* Push the button */ |
| 1627 | arm_smmu_tlb_sync(smmu); |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 1628 | writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sCR0); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | static int arm_smmu_id_size_to_bits(int size) |
| 1632 | { |
| 1633 | switch (size) { |
| 1634 | case 0: |
| 1635 | return 32; |
| 1636 | case 1: |
| 1637 | return 36; |
| 1638 | case 2: |
| 1639 | return 40; |
| 1640 | case 3: |
| 1641 | return 42; |
| 1642 | case 4: |
| 1643 | return 44; |
| 1644 | case 5: |
| 1645 | default: |
| 1646 | return 48; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) |
| 1651 | { |
| 1652 | unsigned long size; |
| 1653 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 1654 | u32 id; |
| 1655 | |
| 1656 | dev_notice(smmu->dev, "probing hardware configuration...\n"); |
| 1657 | |
| 1658 | /* Primecell ID */ |
| 1659 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2); |
| 1660 | smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1; |
| 1661 | dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version); |
| 1662 | |
| 1663 | /* ID0 */ |
| 1664 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0); |
| 1665 | #ifndef CONFIG_64BIT |
| 1666 | if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) { |
| 1667 | dev_err(smmu->dev, "\tno v7 descriptor support!\n"); |
| 1668 | return -ENODEV; |
| 1669 | } |
| 1670 | #endif |
| 1671 | if (id & ID0_S1TS) { |
| 1672 | smmu->features |= ARM_SMMU_FEAT_TRANS_S1; |
| 1673 | dev_notice(smmu->dev, "\tstage 1 translation\n"); |
| 1674 | } |
| 1675 | |
| 1676 | if (id & ID0_S2TS) { |
| 1677 | smmu->features |= ARM_SMMU_FEAT_TRANS_S2; |
| 1678 | dev_notice(smmu->dev, "\tstage 2 translation\n"); |
| 1679 | } |
| 1680 | |
| 1681 | if (id & ID0_NTS) { |
| 1682 | smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED; |
| 1683 | dev_notice(smmu->dev, "\tnested translation\n"); |
| 1684 | } |
| 1685 | |
| 1686 | if (!(smmu->features & |
| 1687 | (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 | |
| 1688 | ARM_SMMU_FEAT_TRANS_NESTED))) { |
| 1689 | dev_err(smmu->dev, "\tno translation support!\n"); |
| 1690 | return -ENODEV; |
| 1691 | } |
| 1692 | |
| 1693 | if (id & ID0_CTTW) { |
| 1694 | smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK; |
| 1695 | dev_notice(smmu->dev, "\tcoherent table walk\n"); |
| 1696 | } |
| 1697 | |
| 1698 | if (id & ID0_SMS) { |
| 1699 | u32 smr, sid, mask; |
| 1700 | |
| 1701 | smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH; |
| 1702 | smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) & |
| 1703 | ID0_NUMSMRG_MASK; |
| 1704 | if (smmu->num_mapping_groups == 0) { |
| 1705 | dev_err(smmu->dev, |
| 1706 | "stream-matching supported, but no SMRs present!\n"); |
| 1707 | return -ENODEV; |
| 1708 | } |
| 1709 | |
| 1710 | smr = SMR_MASK_MASK << SMR_MASK_SHIFT; |
| 1711 | smr |= (SMR_ID_MASK << SMR_ID_SHIFT); |
| 1712 | writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); |
| 1713 | smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); |
| 1714 | |
| 1715 | mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK; |
| 1716 | sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK; |
| 1717 | if ((mask & sid) != sid) { |
| 1718 | dev_err(smmu->dev, |
| 1719 | "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n", |
| 1720 | mask, sid); |
| 1721 | return -ENODEV; |
| 1722 | } |
| 1723 | |
| 1724 | dev_notice(smmu->dev, |
| 1725 | "\tstream matching with %u register groups, mask 0x%x", |
| 1726 | smmu->num_mapping_groups, mask); |
| 1727 | } |
| 1728 | |
| 1729 | /* ID1 */ |
| 1730 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1); |
| 1731 | smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K; |
| 1732 | |
Andreas Herrmann | c55af7f | 2013-10-01 13:39:06 +0100 | [diff] [blame] | 1733 | /* Check for size mismatch of SMMU address space from mapped region */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1734 | size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); |
| 1735 | size *= (smmu->pagesize << 1); |
Andreas Herrmann | c55af7f | 2013-10-01 13:39:06 +0100 | [diff] [blame] | 1736 | if (smmu->size != size) |
| 1737 | dev_warn(smmu->dev, "SMMU address space size (0x%lx) differs " |
| 1738 | "from mapped region size (0x%lx)!\n", size, smmu->size); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1739 | |
| 1740 | smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & |
| 1741 | ID1_NUMS2CB_MASK; |
| 1742 | smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK; |
| 1743 | if (smmu->num_s2_context_banks > smmu->num_context_banks) { |
| 1744 | dev_err(smmu->dev, "impossible number of S2 context banks!\n"); |
| 1745 | return -ENODEV; |
| 1746 | } |
| 1747 | dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n", |
| 1748 | smmu->num_context_banks, smmu->num_s2_context_banks); |
| 1749 | |
| 1750 | /* ID2 */ |
| 1751 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); |
| 1752 | size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK); |
| 1753 | |
| 1754 | /* |
| 1755 | * Stage-1 output limited by stage-2 input size due to pgd |
| 1756 | * allocation (PTRS_PER_PGD). |
| 1757 | */ |
| 1758 | #ifdef CONFIG_64BIT |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1759 | smmu->s1_output_size = min(39UL, size); |
| 1760 | #else |
| 1761 | smmu->s1_output_size = min(32UL, size); |
| 1762 | #endif |
| 1763 | |
| 1764 | /* The stage-2 output mask is also applied for bypass */ |
| 1765 | size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); |
| 1766 | smmu->s2_output_size = min((unsigned long)PHYS_MASK_SHIFT, size); |
| 1767 | |
| 1768 | if (smmu->version == 1) { |
| 1769 | smmu->input_size = 32; |
| 1770 | } else { |
| 1771 | #ifdef CONFIG_64BIT |
| 1772 | size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; |
Will Deacon | 06f983d | 2013-11-05 15:55:04 +0000 | [diff] [blame] | 1773 | size = min(VA_BITS, arm_smmu_id_size_to_bits(size)); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1774 | #else |
| 1775 | size = 32; |
| 1776 | #endif |
| 1777 | smmu->input_size = size; |
| 1778 | |
| 1779 | if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) || |
| 1780 | (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) || |
| 1781 | (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) { |
| 1782 | dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n", |
| 1783 | PAGE_SIZE); |
| 1784 | return -ENODEV; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | dev_notice(smmu->dev, |
| 1789 | "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n", |
| 1790 | smmu->input_size, smmu->s1_output_size, smmu->s2_output_size); |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | static int arm_smmu_device_dt_probe(struct platform_device *pdev) |
| 1795 | { |
| 1796 | struct resource *res; |
| 1797 | struct arm_smmu_device *smmu; |
| 1798 | struct device_node *dev_node; |
| 1799 | struct device *dev = &pdev->dev; |
| 1800 | struct rb_node *node; |
| 1801 | struct of_phandle_args masterspec; |
| 1802 | int num_irqs, i, err; |
| 1803 | |
| 1804 | smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL); |
| 1805 | if (!smmu) { |
| 1806 | dev_err(dev, "failed to allocate arm_smmu_device\n"); |
| 1807 | return -ENOMEM; |
| 1808 | } |
| 1809 | smmu->dev = dev; |
| 1810 | |
| 1811 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Julia Lawall | 8a7f431 | 2013-08-19 12:20:37 +0100 | [diff] [blame] | 1812 | smmu->base = devm_ioremap_resource(dev, res); |
| 1813 | if (IS_ERR(smmu->base)) |
| 1814 | return PTR_ERR(smmu->base); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1815 | smmu->size = resource_size(res); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1816 | |
| 1817 | if (of_property_read_u32(dev->of_node, "#global-interrupts", |
| 1818 | &smmu->num_global_irqs)) { |
| 1819 | dev_err(dev, "missing #global-interrupts property\n"); |
| 1820 | return -ENODEV; |
| 1821 | } |
| 1822 | |
| 1823 | num_irqs = 0; |
| 1824 | while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) { |
| 1825 | num_irqs++; |
| 1826 | if (num_irqs > smmu->num_global_irqs) |
| 1827 | smmu->num_context_irqs++; |
| 1828 | } |
| 1829 | |
Andreas Herrmann | 44a08de | 2013-10-01 13:39:07 +0100 | [diff] [blame] | 1830 | if (!smmu->num_context_irqs) { |
| 1831 | dev_err(dev, "found %d interrupts but expected at least %d\n", |
| 1832 | num_irqs, smmu->num_global_irqs + 1); |
| 1833 | return -ENODEV; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1834 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1835 | |
| 1836 | smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs, |
| 1837 | GFP_KERNEL); |
| 1838 | if (!smmu->irqs) { |
| 1839 | dev_err(dev, "failed to allocate %d irqs\n", num_irqs); |
| 1840 | return -ENOMEM; |
| 1841 | } |
| 1842 | |
| 1843 | for (i = 0; i < num_irqs; ++i) { |
| 1844 | int irq = platform_get_irq(pdev, i); |
| 1845 | if (irq < 0) { |
| 1846 | dev_err(dev, "failed to get irq index %d\n", i); |
| 1847 | return -ENODEV; |
| 1848 | } |
| 1849 | smmu->irqs[i] = irq; |
| 1850 | } |
| 1851 | |
| 1852 | i = 0; |
| 1853 | smmu->masters = RB_ROOT; |
| 1854 | while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters", |
| 1855 | "#stream-id-cells", i, |
| 1856 | &masterspec)) { |
| 1857 | err = register_smmu_master(smmu, dev, &masterspec); |
| 1858 | if (err) { |
| 1859 | dev_err(dev, "failed to add master %s\n", |
| 1860 | masterspec.np->name); |
| 1861 | goto out_put_masters; |
| 1862 | } |
| 1863 | |
| 1864 | i++; |
| 1865 | } |
| 1866 | dev_notice(dev, "registered %d master devices\n", i); |
| 1867 | |
| 1868 | if ((dev_node = of_parse_phandle(dev->of_node, "smmu-parent", 0))) |
| 1869 | smmu->parent_of_node = dev_node; |
| 1870 | |
| 1871 | err = arm_smmu_device_cfg_probe(smmu); |
| 1872 | if (err) |
| 1873 | goto out_put_parent; |
| 1874 | |
| 1875 | if (smmu->version > 1 && |
| 1876 | smmu->num_context_banks != smmu->num_context_irqs) { |
| 1877 | dev_err(dev, |
| 1878 | "found only %d context interrupt(s) but %d required\n", |
| 1879 | smmu->num_context_irqs, smmu->num_context_banks); |
Wei Yongjun | 89a23cd | 2013-11-15 09:42:30 +0000 | [diff] [blame] | 1880 | err = -ENODEV; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1881 | goto out_put_parent; |
| 1882 | } |
| 1883 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1884 | for (i = 0; i < smmu->num_global_irqs; ++i) { |
| 1885 | err = request_irq(smmu->irqs[i], |
| 1886 | arm_smmu_global_fault, |
| 1887 | IRQF_SHARED, |
| 1888 | "arm-smmu global fault", |
| 1889 | smmu); |
| 1890 | if (err) { |
| 1891 | dev_err(dev, "failed to request global IRQ %d (%u)\n", |
| 1892 | i, smmu->irqs[i]); |
| 1893 | goto out_free_irqs; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | INIT_LIST_HEAD(&smmu->list); |
| 1898 | spin_lock(&arm_smmu_devices_lock); |
| 1899 | list_add(&smmu->list, &arm_smmu_devices); |
| 1900 | spin_unlock(&arm_smmu_devices_lock); |
Will Deacon | fd90cec | 2013-08-21 13:56:34 +0100 | [diff] [blame] | 1901 | |
| 1902 | arm_smmu_device_reset(smmu); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1903 | return 0; |
| 1904 | |
| 1905 | out_free_irqs: |
| 1906 | while (i--) |
| 1907 | free_irq(smmu->irqs[i], smmu); |
| 1908 | |
| 1909 | out_put_parent: |
| 1910 | if (smmu->parent_of_node) |
| 1911 | of_node_put(smmu->parent_of_node); |
| 1912 | |
| 1913 | out_put_masters: |
| 1914 | for (node = rb_first(&smmu->masters); node; node = rb_next(node)) { |
| 1915 | struct arm_smmu_master *master; |
| 1916 | master = container_of(node, struct arm_smmu_master, node); |
| 1917 | of_node_put(master->of_node); |
| 1918 | } |
| 1919 | |
| 1920 | return err; |
| 1921 | } |
| 1922 | |
| 1923 | static int arm_smmu_device_remove(struct platform_device *pdev) |
| 1924 | { |
| 1925 | int i; |
| 1926 | struct device *dev = &pdev->dev; |
| 1927 | struct arm_smmu_device *curr, *smmu = NULL; |
| 1928 | struct rb_node *node; |
| 1929 | |
| 1930 | spin_lock(&arm_smmu_devices_lock); |
| 1931 | list_for_each_entry(curr, &arm_smmu_devices, list) { |
| 1932 | if (curr->dev == dev) { |
| 1933 | smmu = curr; |
| 1934 | list_del(&smmu->list); |
| 1935 | break; |
| 1936 | } |
| 1937 | } |
| 1938 | spin_unlock(&arm_smmu_devices_lock); |
| 1939 | |
| 1940 | if (!smmu) |
| 1941 | return -ENODEV; |
| 1942 | |
| 1943 | if (smmu->parent_of_node) |
| 1944 | of_node_put(smmu->parent_of_node); |
| 1945 | |
| 1946 | for (node = rb_first(&smmu->masters); node; node = rb_next(node)) { |
| 1947 | struct arm_smmu_master *master; |
| 1948 | master = container_of(node, struct arm_smmu_master, node); |
| 1949 | of_node_put(master->of_node); |
| 1950 | } |
| 1951 | |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 1952 | if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS)) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1953 | dev_err(dev, "removing device with active domains!\n"); |
| 1954 | |
| 1955 | for (i = 0; i < smmu->num_global_irqs; ++i) |
| 1956 | free_irq(smmu->irqs[i], smmu); |
| 1957 | |
| 1958 | /* Turn the thing off */ |
Will Deacon | 2572484 | 2013-08-21 13:49:53 +0100 | [diff] [blame] | 1959 | writel_relaxed(sCR0_CLIENTPD, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_sCR0); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | #ifdef CONFIG_OF |
| 1964 | static struct of_device_id arm_smmu_of_match[] = { |
| 1965 | { .compatible = "arm,smmu-v1", }, |
| 1966 | { .compatible = "arm,smmu-v2", }, |
| 1967 | { .compatible = "arm,mmu-400", }, |
| 1968 | { .compatible = "arm,mmu-500", }, |
| 1969 | { }, |
| 1970 | }; |
| 1971 | MODULE_DEVICE_TABLE(of, arm_smmu_of_match); |
| 1972 | #endif |
| 1973 | |
| 1974 | static struct platform_driver arm_smmu_driver = { |
| 1975 | .driver = { |
| 1976 | .owner = THIS_MODULE, |
| 1977 | .name = "arm-smmu", |
| 1978 | .of_match_table = of_match_ptr(arm_smmu_of_match), |
| 1979 | }, |
| 1980 | .probe = arm_smmu_device_dt_probe, |
| 1981 | .remove = arm_smmu_device_remove, |
| 1982 | }; |
| 1983 | |
| 1984 | static int __init arm_smmu_init(void) |
| 1985 | { |
| 1986 | int ret; |
| 1987 | |
| 1988 | ret = platform_driver_register(&arm_smmu_driver); |
| 1989 | if (ret) |
| 1990 | return ret; |
| 1991 | |
| 1992 | /* Oh, for a proper bus abstraction */ |
Dan Carpenter | 6614ee7 | 2013-08-21 09:34:20 +0100 | [diff] [blame] | 1993 | if (!iommu_present(&platform_bus_type)) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1994 | bus_set_iommu(&platform_bus_type, &arm_smmu_ops); |
| 1995 | |
Dan Carpenter | 6614ee7 | 2013-08-21 09:34:20 +0100 | [diff] [blame] | 1996 | if (!iommu_present(&amba_bustype)) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1997 | bus_set_iommu(&amba_bustype, &arm_smmu_ops); |
| 1998 | |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | static void __exit arm_smmu_exit(void) |
| 2003 | { |
| 2004 | return platform_driver_unregister(&arm_smmu_driver); |
| 2005 | } |
| 2006 | |
Andreas Herrmann | b1950b2 | 2013-10-01 13:39:05 +0100 | [diff] [blame] | 2007 | subsys_initcall(arm_smmu_init); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2008 | module_exit(arm_smmu_exit); |
| 2009 | |
| 2010 | MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations"); |
| 2011 | MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>"); |
| 2012 | MODULE_LICENSE("GPL v2"); |