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