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 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 26 | * - Context fault reporting |
| 27 | */ |
| 28 | |
| 29 | #define pr_fmt(fmt) "arm-smmu: " fmt |
| 30 | |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 31 | #include <linux/atomic.h> |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 32 | #include <linux/delay.h> |
Robin Murphy | 9adb959 | 2016-01-26 18:06:36 +0000 | [diff] [blame] | 33 | #include <linux/dma-iommu.h> |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 34 | #include <linux/dma-mapping.h> |
| 35 | #include <linux/err.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/io.h> |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 38 | #include <linux/io-64-nonatomic-hi-lo.h> |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 39 | #include <linux/iommu.h> |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 40 | #include <linux/iopoll.h> |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/of.h> |
Robin Murphy | bae2c2d | 2015-07-29 19:46:05 +0100 | [diff] [blame] | 43 | #include <linux/of_address.h> |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 44 | #include <linux/of_device.h> |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 45 | #include <linux/of_iommu.h> |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 46 | #include <linux/pci.h> |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 47 | #include <linux/platform_device.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/spinlock.h> |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 50 | #include <soc/qcom/secure_buffer.h> |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 51 | #include <linux/of_platform.h> |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 52 | #include <linux/msm-bus.h> |
| 53 | #include <dt-bindings/msm/msm-bus-ids.h> |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 54 | |
| 55 | #include <linux/amba/bus.h> |
| 56 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 57 | #include "io-pgtable.h" |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 58 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 59 | /* Maximum number of context banks per SMMU */ |
| 60 | #define ARM_SMMU_MAX_CBS 128 |
| 61 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 62 | /* SMMU global address space */ |
| 63 | #define ARM_SMMU_GR0(smmu) ((smmu)->base) |
Will Deacon | c757e85 | 2014-07-30 11:33:25 +0100 | [diff] [blame] | 64 | #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift)) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 65 | |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 66 | /* |
| 67 | * SMMU global address space with conditional offset to access secure |
| 68 | * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448, |
| 69 | * nsGFSYNR0: 0x450) |
| 70 | */ |
| 71 | #define ARM_SMMU_GR0_NS(smmu) \ |
| 72 | ((smmu)->base + \ |
| 73 | ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \ |
| 74 | ? 0x400 : 0)) |
| 75 | |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 76 | /* |
| 77 | * Some 64-bit registers only make sense to write atomically, but in such |
| 78 | * cases all the data relevant to AArch32 formats lies within the lower word, |
| 79 | * therefore this actually makes more sense than it might first appear. |
| 80 | */ |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_64BIT |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 82 | #define smmu_write_atomic_lq writeq_relaxed |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 83 | #else |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 84 | #define smmu_write_atomic_lq writel_relaxed |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 85 | #endif |
| 86 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 87 | /* Configuration registers */ |
| 88 | #define ARM_SMMU_GR0_sCR0 0x0 |
| 89 | #define sCR0_CLIENTPD (1 << 0) |
| 90 | #define sCR0_GFRE (1 << 1) |
| 91 | #define sCR0_GFIE (1 << 2) |
| 92 | #define sCR0_GCFGFRE (1 << 4) |
| 93 | #define sCR0_GCFGFIE (1 << 5) |
| 94 | #define sCR0_USFCFG (1 << 10) |
| 95 | #define sCR0_VMIDPNE (1 << 11) |
| 96 | #define sCR0_PTM (1 << 12) |
| 97 | #define sCR0_FB (1 << 13) |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 98 | #define sCR0_VMID16EN (1 << 31) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 99 | #define sCR0_BSU_SHIFT 14 |
| 100 | #define sCR0_BSU_MASK 0x3 |
| 101 | |
Peng Fan | 3ca3712 | 2016-05-03 21:50:30 +0800 | [diff] [blame] | 102 | /* Auxiliary Configuration register */ |
| 103 | #define ARM_SMMU_GR0_sACR 0x10 |
| 104 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 105 | /* Identification registers */ |
| 106 | #define ARM_SMMU_GR0_ID0 0x20 |
| 107 | #define ARM_SMMU_GR0_ID1 0x24 |
| 108 | #define ARM_SMMU_GR0_ID2 0x28 |
| 109 | #define ARM_SMMU_GR0_ID3 0x2c |
| 110 | #define ARM_SMMU_GR0_ID4 0x30 |
| 111 | #define ARM_SMMU_GR0_ID5 0x34 |
| 112 | #define ARM_SMMU_GR0_ID6 0x38 |
| 113 | #define ARM_SMMU_GR0_ID7 0x3c |
| 114 | #define ARM_SMMU_GR0_sGFSR 0x48 |
| 115 | #define ARM_SMMU_GR0_sGFSYNR0 0x50 |
| 116 | #define ARM_SMMU_GR0_sGFSYNR1 0x54 |
| 117 | #define ARM_SMMU_GR0_sGFSYNR2 0x58 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 118 | |
| 119 | #define ID0_S1TS (1 << 30) |
| 120 | #define ID0_S2TS (1 << 29) |
| 121 | #define ID0_NTS (1 << 28) |
| 122 | #define ID0_SMS (1 << 27) |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 123 | #define ID0_ATOSNS (1 << 26) |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 124 | #define ID0_PTFS_NO_AARCH32 (1 << 25) |
| 125 | #define ID0_PTFS_NO_AARCH32S (1 << 24) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 126 | #define ID0_CTTW (1 << 14) |
| 127 | #define ID0_NUMIRPT_SHIFT 16 |
| 128 | #define ID0_NUMIRPT_MASK 0xff |
Olav Haugan | 3c8766d | 2014-08-22 17:12:32 -0700 | [diff] [blame] | 129 | #define ID0_NUMSIDB_SHIFT 9 |
| 130 | #define ID0_NUMSIDB_MASK 0xf |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 131 | #define ID0_NUMSMRG_SHIFT 0 |
| 132 | #define ID0_NUMSMRG_MASK 0xff |
| 133 | |
| 134 | #define ID1_PAGESIZE (1 << 31) |
| 135 | #define ID1_NUMPAGENDXB_SHIFT 28 |
| 136 | #define ID1_NUMPAGENDXB_MASK 7 |
| 137 | #define ID1_NUMS2CB_SHIFT 16 |
| 138 | #define ID1_NUMS2CB_MASK 0xff |
| 139 | #define ID1_NUMCB_SHIFT 0 |
| 140 | #define ID1_NUMCB_MASK 0xff |
| 141 | |
| 142 | #define ID2_OAS_SHIFT 4 |
| 143 | #define ID2_OAS_MASK 0xf |
| 144 | #define ID2_IAS_SHIFT 0 |
| 145 | #define ID2_IAS_MASK 0xf |
| 146 | #define ID2_UBS_SHIFT 8 |
| 147 | #define ID2_UBS_MASK 0xf |
| 148 | #define ID2_PTFS_4K (1 << 12) |
| 149 | #define ID2_PTFS_16K (1 << 13) |
| 150 | #define ID2_PTFS_64K (1 << 14) |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 151 | #define ID2_VMID16 (1 << 15) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 152 | |
Peng Fan | 3ca3712 | 2016-05-03 21:50:30 +0800 | [diff] [blame] | 153 | #define ID7_MAJOR_SHIFT 4 |
| 154 | #define ID7_MAJOR_MASK 0xf |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 155 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 156 | /* Global TLB invalidation */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 157 | #define ARM_SMMU_GR0_TLBIVMID 0x64 |
| 158 | #define ARM_SMMU_GR0_TLBIALLNSNH 0x68 |
| 159 | #define ARM_SMMU_GR0_TLBIALLH 0x6c |
| 160 | #define ARM_SMMU_GR0_sTLBGSYNC 0x70 |
| 161 | #define ARM_SMMU_GR0_sTLBGSTATUS 0x74 |
| 162 | #define sTLBGSTATUS_GSACTIVE (1 << 0) |
Mitchel Humpherys | 849aa50 | 2015-11-09 11:50:58 -0800 | [diff] [blame] | 163 | #define TLB_LOOP_TIMEOUT 500000 /* 500ms */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 164 | |
| 165 | /* Stream mapping registers */ |
| 166 | #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2)) |
| 167 | #define SMR_VALID (1 << 31) |
| 168 | #define SMR_MASK_SHIFT 16 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 169 | #define SMR_ID_SHIFT 0 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 170 | |
| 171 | #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2)) |
| 172 | #define S2CR_CBNDX_SHIFT 0 |
| 173 | #define S2CR_CBNDX_MASK 0xff |
| 174 | #define S2CR_TYPE_SHIFT 16 |
| 175 | #define S2CR_TYPE_MASK 0x3 |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 176 | enum arm_smmu_s2cr_type { |
| 177 | S2CR_TYPE_TRANS, |
| 178 | S2CR_TYPE_BYPASS, |
| 179 | S2CR_TYPE_FAULT, |
| 180 | }; |
| 181 | |
| 182 | #define S2CR_PRIVCFG_SHIFT 24 |
| 183 | #define S2CR_PRIVCFG_MASK 0x3 |
| 184 | enum arm_smmu_s2cr_privcfg { |
| 185 | S2CR_PRIVCFG_DEFAULT, |
| 186 | S2CR_PRIVCFG_DIPAN, |
| 187 | S2CR_PRIVCFG_UNPRIV, |
| 188 | S2CR_PRIVCFG_PRIV, |
| 189 | }; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 190 | |
| 191 | /* Context bank attribute registers */ |
| 192 | #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2)) |
| 193 | #define CBAR_VMID_SHIFT 0 |
| 194 | #define CBAR_VMID_MASK 0xff |
Will Deacon | 57ca90f | 2014-02-06 14:59:05 +0000 | [diff] [blame] | 195 | #define CBAR_S1_BPSHCFG_SHIFT 8 |
| 196 | #define CBAR_S1_BPSHCFG_MASK 3 |
| 197 | #define CBAR_S1_BPSHCFG_NSH 3 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 198 | #define CBAR_S1_MEMATTR_SHIFT 12 |
| 199 | #define CBAR_S1_MEMATTR_MASK 0xf |
| 200 | #define CBAR_S1_MEMATTR_WB 0xf |
| 201 | #define CBAR_TYPE_SHIFT 16 |
| 202 | #define CBAR_TYPE_MASK 0x3 |
| 203 | #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT) |
| 204 | #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT) |
| 205 | #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT) |
| 206 | #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT) |
| 207 | #define CBAR_IRPTNDX_SHIFT 24 |
| 208 | #define CBAR_IRPTNDX_MASK 0xff |
| 209 | |
Shalaj Jain | 04059c5 | 2015-03-03 13:34:59 -0800 | [diff] [blame] | 210 | #define ARM_SMMU_GR1_CBFRSYNRA(n) (0x400 + ((n) << 2)) |
| 211 | #define CBFRSYNRA_SID_MASK (0xffff) |
| 212 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 213 | #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2)) |
| 214 | #define CBA2R_RW64_32BIT (0 << 0) |
| 215 | #define CBA2R_RW64_64BIT (1 << 0) |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 216 | #define CBA2R_VMID_SHIFT 16 |
| 217 | #define CBA2R_VMID_MASK 0xffff |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 218 | |
| 219 | /* Translation context bank */ |
| 220 | #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1)) |
Will Deacon | c757e85 | 2014-07-30 11:33:25 +0100 | [diff] [blame] | 221 | #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift)) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 222 | |
| 223 | #define ARM_SMMU_CB_SCTLR 0x0 |
Robin Murphy | f0cfffc | 2016-04-13 18:12:59 +0100 | [diff] [blame] | 224 | #define ARM_SMMU_CB_ACTLR 0x4 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 225 | #define ARM_SMMU_CB_RESUME 0x8 |
| 226 | #define ARM_SMMU_CB_TTBCR2 0x10 |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 227 | #define ARM_SMMU_CB_TTBR0 0x20 |
| 228 | #define ARM_SMMU_CB_TTBR1 0x28 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 229 | #define ARM_SMMU_CB_TTBCR 0x30 |
Jeremy Gebben | 8ac927c | 2015-07-10 16:43:22 -0600 | [diff] [blame] | 230 | #define ARM_SMMU_CB_CONTEXTIDR 0x34 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 231 | #define ARM_SMMU_CB_S1_MAIR0 0x38 |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 232 | #define ARM_SMMU_CB_S1_MAIR1 0x3c |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 233 | #define ARM_SMMU_CB_PAR 0x50 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 234 | #define ARM_SMMU_CB_FSR 0x58 |
Mitchel Humpherys | a52d6cc | 2015-07-09 17:26:15 -0700 | [diff] [blame] | 235 | #define ARM_SMMU_CB_FSRRESTORE 0x5c |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 236 | #define ARM_SMMU_CB_FAR 0x60 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 237 | #define ARM_SMMU_CB_FSYNR0 0x68 |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 238 | #define ARM_SMMU_CB_S1_TLBIVA 0x600 |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 239 | #define ARM_SMMU_CB_S1_TLBIASID 0x610 |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 240 | #define ARM_SMMU_CB_S1_TLBIVAL 0x620 |
| 241 | #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630 |
| 242 | #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638 |
Mitchel Humpherys | f300799 | 2015-06-19 15:00:14 -0700 | [diff] [blame] | 243 | #define ARM_SMMU_CB_TLBSYNC 0x7f0 |
| 244 | #define ARM_SMMU_CB_TLBSTATUS 0x7f4 |
| 245 | #define TLBSTATUS_SACTIVE (1 << 0) |
Robin Murphy | 661d962 | 2015-05-27 17:09:34 +0100 | [diff] [blame] | 246 | #define ARM_SMMU_CB_ATS1PR 0x800 |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 247 | #define ARM_SMMU_CB_ATSR 0x8f0 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 248 | |
| 249 | #define SCTLR_S1_ASIDPNE (1 << 12) |
| 250 | #define SCTLR_CFCFG (1 << 7) |
| 251 | #define SCTLR_CFIE (1 << 6) |
| 252 | #define SCTLR_CFRE (1 << 5) |
| 253 | #define SCTLR_E (1 << 4) |
| 254 | #define SCTLR_AFE (1 << 2) |
| 255 | #define SCTLR_TRE (1 << 1) |
| 256 | #define SCTLR_M (1 << 0) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 257 | |
Robin Murphy | f0cfffc | 2016-04-13 18:12:59 +0100 | [diff] [blame] | 258 | #define ARM_MMU500_ACTLR_CPRE (1 << 1) |
| 259 | |
Peng Fan | 3ca3712 | 2016-05-03 21:50:30 +0800 | [diff] [blame] | 260 | #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26) |
| 261 | |
Mitchel Humpherys | 952f40a | 2015-08-19 12:13:28 -0700 | [diff] [blame] | 262 | #define ARM_SMMU_IMPL_DEF0(smmu) \ |
| 263 | ((smmu)->base + (2 * (1 << (smmu)->pgshift))) |
| 264 | #define ARM_SMMU_IMPL_DEF1(smmu) \ |
| 265 | ((smmu)->base + (6 * (1 << (smmu)->pgshift))) |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 266 | #define CB_PAR_F (1 << 0) |
| 267 | |
| 268 | #define ATSR_ACTIVE (1 << 0) |
| 269 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 270 | #define RESUME_RETRY (0 << 0) |
| 271 | #define RESUME_TERMINATE (1 << 0) |
| 272 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 273 | #define TTBCR2_SEP_SHIFT 15 |
Will Deacon | 5dc5616 | 2015-05-08 17:44:22 +0100 | [diff] [blame] | 274 | #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 275 | |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 276 | #define TTBRn_ASID_SHIFT 48 |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 277 | |
| 278 | #define FSR_MULTI (1 << 31) |
| 279 | #define FSR_SS (1 << 30) |
| 280 | #define FSR_UUT (1 << 8) |
| 281 | #define FSR_ASF (1 << 7) |
| 282 | #define FSR_TLBLKF (1 << 6) |
| 283 | #define FSR_TLBMCF (1 << 5) |
| 284 | #define FSR_EF (1 << 4) |
| 285 | #define FSR_PF (1 << 3) |
| 286 | #define FSR_AFF (1 << 2) |
| 287 | #define FSR_TF (1 << 1) |
| 288 | |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 289 | #define FSR_IGN (FSR_AFF | FSR_ASF | \ |
| 290 | FSR_TLBMCF | FSR_TLBLKF) |
| 291 | #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \ |
Will Deacon | adaba32 | 2013-07-31 19:21:26 +0100 | [diff] [blame] | 292 | FSR_EF | FSR_PF | FSR_TF | FSR_IGN) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 293 | |
| 294 | #define FSYNR0_WNR (1 << 4) |
| 295 | |
Will Deacon | 4cf740b | 2014-07-14 19:47:39 +0100 | [diff] [blame] | 296 | static int force_stage; |
Robin Murphy | 25a1c96 | 2016-02-10 14:25:33 +0000 | [diff] [blame] | 297 | module_param(force_stage, int, S_IRUGO); |
Will Deacon | 4cf740b | 2014-07-14 19:47:39 +0100 | [diff] [blame] | 298 | MODULE_PARM_DESC(force_stage, |
| 299 | "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation."); |
Patrick Daly | a728cfd | 2016-11-15 17:49:29 -0800 | [diff] [blame] | 300 | static bool disable_bypass; |
Robin Murphy | 25a1c96 | 2016-02-10 14:25:33 +0000 | [diff] [blame] | 301 | module_param(disable_bypass, bool, S_IRUGO); |
| 302 | MODULE_PARM_DESC(disable_bypass, |
| 303 | "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU."); |
Will Deacon | 4cf740b | 2014-07-14 19:47:39 +0100 | [diff] [blame] | 304 | |
Robin Murphy | 0936040 | 2014-08-28 17:51:59 +0100 | [diff] [blame] | 305 | enum arm_smmu_arch_version { |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 306 | ARM_SMMU_V1, |
| 307 | ARM_SMMU_V1_64K, |
Robin Murphy | 0936040 | 2014-08-28 17:51:59 +0100 | [diff] [blame] | 308 | ARM_SMMU_V2, |
| 309 | }; |
| 310 | |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 311 | enum arm_smmu_implementation { |
| 312 | GENERIC_SMMU, |
Robin Murphy | f0cfffc | 2016-04-13 18:12:59 +0100 | [diff] [blame] | 313 | ARM_MMU500, |
Robin Murphy | e086d91 | 2016-04-13 18:12:58 +0100 | [diff] [blame] | 314 | CAVIUM_SMMUV2, |
Patrick Daly | f0d4e21 | 2016-06-20 15:50:14 -0700 | [diff] [blame] | 315 | QCOM_SMMUV2, |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 316 | QCOM_SMMUV500, |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 317 | }; |
| 318 | |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 319 | struct arm_smmu_device; |
| 320 | struct arm_smmu_arch_ops { |
| 321 | int (*init)(struct arm_smmu_device *smmu); |
| 322 | void (*device_reset)(struct arm_smmu_device *smmu); |
| 323 | phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain, |
| 324 | dma_addr_t iova); |
| 325 | void (*iova_to_phys_fault)(struct iommu_domain *domain, |
| 326 | dma_addr_t iova, phys_addr_t *phys1, |
| 327 | phys_addr_t *phys_post_tlbiall); |
| 328 | }; |
| 329 | |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 330 | struct arm_smmu_impl_def_reg { |
| 331 | u32 offset; |
| 332 | u32 value; |
| 333 | }; |
| 334 | |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 335 | struct arm_smmu_s2cr { |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 336 | struct iommu_group *group; |
| 337 | int count; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 338 | enum arm_smmu_s2cr_type type; |
| 339 | enum arm_smmu_s2cr_privcfg privcfg; |
| 340 | u8 cbndx; |
| 341 | }; |
| 342 | |
| 343 | #define s2cr_init_val (struct arm_smmu_s2cr){ \ |
| 344 | .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \ |
| 345 | } |
| 346 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 347 | struct arm_smmu_smr { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 348 | u16 mask; |
| 349 | u16 id; |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 350 | bool valid; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 351 | }; |
| 352 | |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 353 | struct arm_smmu_master_cfg { |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 354 | struct arm_smmu_device *smmu; |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 355 | s16 smendx[]; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 356 | }; |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 357 | #define INVALID_SMENDX -1 |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 358 | #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv) |
| 359 | #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu) |
| 360 | #define for_each_cfg_sme(fw, i, idx) \ |
| 361 | for (i = 0; idx = __fwspec_cfg(fw)->smendx[i], i < fw->num_ids; ++i) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 362 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 363 | /* |
| 364 | * Describes resources required for on/off power operation. |
| 365 | * Separate reference count is provided for atomic/nonatomic |
| 366 | * operations. |
| 367 | */ |
| 368 | struct arm_smmu_power_resources { |
| 369 | struct platform_device *pdev; |
| 370 | struct device *dev; |
| 371 | |
| 372 | struct clk **clocks; |
| 373 | int num_clocks; |
| 374 | |
| 375 | struct regulator_bulk_data *gdscs; |
| 376 | int num_gdscs; |
| 377 | |
| 378 | uint32_t bus_client; |
| 379 | struct msm_bus_scale_pdata *bus_dt_data; |
| 380 | |
| 381 | /* Protects power_count */ |
| 382 | struct mutex power_lock; |
| 383 | int power_count; |
| 384 | |
| 385 | /* Protects clock_refs_count */ |
| 386 | spinlock_t clock_refs_lock; |
| 387 | int clock_refs_count; |
| 388 | }; |
| 389 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 390 | struct arm_smmu_device { |
| 391 | struct device *dev; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 392 | |
| 393 | void __iomem *base; |
| 394 | unsigned long size; |
Will Deacon | c757e85 | 2014-07-30 11:33:25 +0100 | [diff] [blame] | 395 | unsigned long pgshift; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 396 | |
| 397 | #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0) |
| 398 | #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1) |
| 399 | #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2) |
| 400 | #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3) |
| 401 | #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4) |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 402 | #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5) |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 403 | #define ARM_SMMU_FEAT_VMID16 (1 << 6) |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 404 | #define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7) |
| 405 | #define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8) |
| 406 | #define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9) |
| 407 | #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10) |
| 408 | #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 409 | u32 features; |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 410 | |
| 411 | #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) |
Mitchel Humpherys | 07ba44b | 2015-01-30 14:58:52 -0800 | [diff] [blame] | 412 | #define ARM_SMMU_OPT_FATAL_ASF (1 << 1) |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 413 | #define ARM_SMMU_OPT_SKIP_INIT (1 << 2) |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 414 | #define ARM_SMMU_OPT_DYNAMIC (1 << 3) |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 415 | u32 options; |
Robin Murphy | 0936040 | 2014-08-28 17:51:59 +0100 | [diff] [blame] | 416 | enum arm_smmu_arch_version version; |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 417 | enum arm_smmu_implementation model; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 418 | |
| 419 | u32 num_context_banks; |
| 420 | u32 num_s2_context_banks; |
| 421 | DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS); |
| 422 | atomic_t irptndx; |
| 423 | |
| 424 | u32 num_mapping_groups; |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 425 | u16 streamid_mask; |
| 426 | u16 smr_mask_mask; |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 427 | struct arm_smmu_smr *smrs; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 428 | struct arm_smmu_s2cr *s2crs; |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 429 | struct mutex stream_map_mutex; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 430 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 431 | unsigned long va_size; |
| 432 | unsigned long ipa_size; |
| 433 | unsigned long pa_size; |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 434 | unsigned long pgsize_bitmap; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 435 | |
| 436 | u32 num_global_irqs; |
| 437 | u32 num_context_irqs; |
| 438 | unsigned int *irqs; |
| 439 | |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 440 | u32 cavium_id_base; /* Specific to Cavium */ |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 441 | /* Specific to QCOM */ |
| 442 | struct arm_smmu_impl_def_reg *impl_def_attach_registers; |
| 443 | unsigned int num_impl_def_attach_registers; |
Mitchel Humpherys | 2fbae2a | 2014-12-04 11:46:24 -0800 | [diff] [blame] | 444 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 445 | struct arm_smmu_power_resources *pwr; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 446 | |
Mitchel Humpherys | 2fbae2a | 2014-12-04 11:46:24 -0800 | [diff] [blame] | 447 | spinlock_t atos_lock; |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 448 | |
| 449 | /* protects idr */ |
| 450 | struct mutex idr_mutex; |
| 451 | struct idr asid_idr; |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 452 | |
| 453 | struct arm_smmu_arch_ops *arch_ops; |
| 454 | void *archdata; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 455 | }; |
| 456 | |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 457 | enum arm_smmu_context_fmt { |
| 458 | ARM_SMMU_CTX_FMT_NONE, |
| 459 | ARM_SMMU_CTX_FMT_AARCH64, |
| 460 | ARM_SMMU_CTX_FMT_AARCH32_L, |
| 461 | ARM_SMMU_CTX_FMT_AARCH32_S, |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | struct arm_smmu_cfg { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 465 | u8 cbndx; |
| 466 | u8 irptndx; |
| 467 | u32 cbar; |
Jeremy Gebben | 8ac927c | 2015-07-10 16:43:22 -0600 | [diff] [blame] | 468 | u32 procid; |
| 469 | u16 asid; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 470 | enum arm_smmu_context_fmt fmt; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 471 | }; |
Dan Carpenter | faea13b7 | 2013-08-21 09:33:30 +0100 | [diff] [blame] | 472 | #define INVALID_IRPTNDX 0xff |
Jeremy Gebben | 8ac927c | 2015-07-10 16:43:22 -0600 | [diff] [blame] | 473 | #define INVALID_CBNDX 0xff |
| 474 | #define INVALID_ASID 0xffff |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 475 | /* |
| 476 | * In V7L and V8L with TTBCR2.AS == 0, ASID is 8 bits. |
| 477 | * V8L 16 with TTBCR2.AS == 1 (16 bit ASID) isn't supported yet. |
| 478 | */ |
| 479 | #define MAX_ASID 0xff |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 480 | |
Jeremy Gebben | 8ac927c | 2015-07-10 16:43:22 -0600 | [diff] [blame] | 481 | #define ARM_SMMU_CB_ASID(smmu, cfg) ((cfg)->asid) |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 482 | #define ARM_SMMU_CB_VMID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx + 1) |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 483 | |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 484 | enum arm_smmu_domain_stage { |
| 485 | ARM_SMMU_DOMAIN_S1 = 0, |
| 486 | ARM_SMMU_DOMAIN_S2, |
| 487 | ARM_SMMU_DOMAIN_NESTED, |
| 488 | }; |
| 489 | |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 490 | struct arm_smmu_pte_info { |
| 491 | void *virt_addr; |
| 492 | size_t size; |
| 493 | struct list_head entry; |
| 494 | }; |
| 495 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 496 | struct arm_smmu_domain { |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 497 | struct arm_smmu_device *smmu; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 498 | struct io_pgtable_ops *pgtbl_ops; |
Mitchel Humpherys | 39e9c91 | 2015-04-15 15:14:15 -0700 | [diff] [blame] | 499 | struct io_pgtable_cfg pgtbl_cfg; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 500 | spinlock_t pgtbl_lock; |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 501 | struct arm_smmu_cfg cfg; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 502 | enum arm_smmu_domain_stage stage; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 503 | struct mutex init_mutex; /* Protects smmu pointer */ |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 504 | u32 attributes; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 505 | u32 secure_vmid; |
| 506 | struct list_head pte_info_list; |
| 507 | struct list_head unassign_list; |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 508 | struct mutex assign_lock; |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 509 | struct list_head secure_pool_list; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 510 | struct iommu_domain domain; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 511 | }; |
| 512 | |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 513 | struct arm_smmu_option_prop { |
| 514 | u32 opt; |
| 515 | const char *prop; |
| 516 | }; |
| 517 | |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 518 | static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0); |
| 519 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 520 | static bool using_legacy_binding, using_generic_binding; |
| 521 | |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 522 | static struct arm_smmu_option_prop arm_smmu_options[] = { |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 523 | { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" }, |
Mitchel Humpherys | 07ba44b | 2015-01-30 14:58:52 -0800 | [diff] [blame] | 524 | { ARM_SMMU_OPT_FATAL_ASF, "qcom,fatal-asf" }, |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 525 | { ARM_SMMU_OPT_SKIP_INIT, "qcom,skip-init" }, |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 526 | { ARM_SMMU_OPT_DYNAMIC, "qcom,dynamic" }, |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 527 | { 0, NULL}, |
| 528 | }; |
| 529 | |
Mitchel Humpherys | b8be413 | 2015-02-06 14:25:10 -0800 | [diff] [blame] | 530 | static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain, |
| 531 | dma_addr_t iova); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 532 | static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain, |
| 533 | dma_addr_t iova); |
Jeremy Gebben | fa24b0c | 2015-06-16 12:45:31 -0600 | [diff] [blame] | 534 | static void arm_smmu_destroy_domain_context(struct iommu_domain *domain); |
Mitchel Humpherys | 0ed5da6 | 2014-12-04 11:47:49 -0800 | [diff] [blame] | 535 | |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 536 | static int arm_smmu_prepare_pgtable(void *addr, void *cookie); |
| 537 | static void arm_smmu_unprepare_pgtable(void *cookie, void *addr, size_t size); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 538 | static int arm_smmu_assign_table(struct arm_smmu_domain *smmu_domain); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 539 | static void arm_smmu_unassign_table(struct arm_smmu_domain *smmu_domain); |
| 540 | |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 541 | static int arm_smmu_arch_init(struct arm_smmu_device *smmu); |
| 542 | static void arm_smmu_arch_device_reset(struct arm_smmu_device *smmu); |
| 543 | |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 544 | static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) |
| 545 | { |
| 546 | return container_of(dom, struct arm_smmu_domain, domain); |
| 547 | } |
| 548 | |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 549 | static void parse_driver_options(struct arm_smmu_device *smmu) |
| 550 | { |
| 551 | int i = 0; |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 552 | |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 553 | do { |
| 554 | if (of_property_read_bool(smmu->dev->of_node, |
| 555 | arm_smmu_options[i].prop)) { |
| 556 | smmu->options |= arm_smmu_options[i].opt; |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 557 | dev_dbg(smmu->dev, "option %s\n", |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 558 | arm_smmu_options[i].prop); |
| 559 | } |
| 560 | } while (arm_smmu_options[++i].opt); |
| 561 | } |
| 562 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 563 | static bool is_dynamic_domain(struct iommu_domain *domain) |
| 564 | { |
| 565 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 566 | |
| 567 | return !!(smmu_domain->attributes & (1 << DOMAIN_ATTR_DYNAMIC)); |
| 568 | } |
| 569 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 570 | static bool arm_smmu_is_domain_secure(struct arm_smmu_domain *smmu_domain) |
| 571 | { |
| 572 | return (smmu_domain->secure_vmid != VMID_INVAL); |
| 573 | } |
| 574 | |
| 575 | static void arm_smmu_secure_domain_lock(struct arm_smmu_domain *smmu_domain) |
| 576 | { |
| 577 | if (arm_smmu_is_domain_secure(smmu_domain)) |
| 578 | mutex_lock(&smmu_domain->assign_lock); |
| 579 | } |
| 580 | |
| 581 | static void arm_smmu_secure_domain_unlock(struct arm_smmu_domain *smmu_domain) |
| 582 | { |
| 583 | if (arm_smmu_is_domain_secure(smmu_domain)) |
| 584 | mutex_unlock(&smmu_domain->assign_lock); |
| 585 | } |
| 586 | |
Will Deacon | 8f68f8e | 2014-07-15 11:27:08 +0100 | [diff] [blame] | 587 | static struct device_node *dev_get_dev_node(struct device *dev) |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 588 | { |
| 589 | if (dev_is_pci(dev)) { |
| 590 | struct pci_bus *bus = to_pci_dev(dev)->bus; |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 591 | |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 592 | while (!pci_is_root_bus(bus)) |
| 593 | bus = bus->parent; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 594 | return of_node_get(bus->bridge->parent->of_node); |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 597 | return of_node_get(dev->of_node); |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 600 | static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 601 | { |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 602 | *((__be32 *)data) = cpu_to_be32(alias); |
| 603 | return 0; /* Continue walking */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 604 | } |
| 605 | |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 606 | static int __find_legacy_master_phandle(struct device *dev, void *data) |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 607 | { |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 608 | struct of_phandle_iterator *it = *(void **)data; |
| 609 | struct device_node *np = it->node; |
| 610 | int err; |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 611 | |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 612 | of_for_each_phandle(it, err, dev->of_node, "mmu-masters", |
| 613 | "#stream-id-cells", 0) |
| 614 | if (it->node == np) { |
| 615 | *(void **)data = dev; |
| 616 | return 1; |
Olav Haugan | 3c8766d | 2014-08-22 17:12:32 -0700 | [diff] [blame] | 617 | } |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 618 | it->node = np; |
| 619 | return err == -ENOENT ? 0 : err; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 622 | static struct platform_driver arm_smmu_driver; |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 623 | static struct iommu_ops arm_smmu_ops; |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 624 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 625 | static int arm_smmu_register_legacy_master(struct device *dev, |
| 626 | struct arm_smmu_device **smmu) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 627 | { |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 628 | struct device *smmu_dev; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 629 | struct device_node *np; |
| 630 | struct of_phandle_iterator it; |
| 631 | void *data = ⁢ |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 632 | u32 *sids; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 633 | __be32 pci_sid; |
| 634 | int err = 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 635 | |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 636 | memset(&it, sizeof(it), 0); |
| 637 | np = dev_get_dev_node(dev); |
| 638 | if (!np || !of_find_property(np, "#stream-id-cells", NULL)) { |
| 639 | of_node_put(np); |
| 640 | return -ENODEV; |
| 641 | } |
| 642 | |
| 643 | it.node = np; |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 644 | err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data, |
| 645 | __find_legacy_master_phandle); |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 646 | smmu_dev = data; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 647 | of_node_put(np); |
| 648 | if (err == 0) |
| 649 | return -ENODEV; |
| 650 | if (err < 0) |
| 651 | return err; |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 652 | |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 653 | if (dev_is_pci(dev)) { |
| 654 | /* "mmu-masters" assumes Stream ID == Requester ID */ |
| 655 | pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid, |
| 656 | &pci_sid); |
| 657 | it.cur = &pci_sid; |
| 658 | it.cur_count = 1; |
| 659 | } |
| 660 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 661 | err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode, |
| 662 | &arm_smmu_ops); |
| 663 | if (err) |
| 664 | return err; |
| 665 | |
| 666 | sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL); |
| 667 | if (!sids) |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 668 | return -ENOMEM; |
| 669 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 670 | *smmu = dev_get_drvdata(smmu_dev); |
| 671 | of_phandle_iterator_args(&it, sids, it.cur_count); |
| 672 | err = iommu_fwspec_add_ids(dev, sids, it.cur_count); |
| 673 | kfree(sids); |
| 674 | return err; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end) |
| 678 | { |
| 679 | int idx; |
| 680 | |
| 681 | do { |
| 682 | idx = find_next_zero_bit(map, end, start); |
| 683 | if (idx == end) |
| 684 | return -ENOSPC; |
| 685 | } while (test_and_set_bit(idx, map)); |
| 686 | |
| 687 | return idx; |
| 688 | } |
| 689 | |
| 690 | static void __arm_smmu_free_bitmap(unsigned long *map, int idx) |
| 691 | { |
| 692 | clear_bit(idx, map); |
| 693 | } |
| 694 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 695 | static int arm_smmu_prepare_clocks(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 696 | { |
| 697 | int i, ret = 0; |
| 698 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 699 | for (i = 0; i < pwr->num_clocks; ++i) { |
| 700 | ret = clk_prepare(pwr->clocks[i]); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 701 | if (ret) { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 702 | dev_err(pwr->dev, "Couldn't prepare clock #%d\n", i); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 703 | while (i--) |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 704 | clk_unprepare(pwr->clocks[i]); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | } |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 711 | static void arm_smmu_unprepare_clocks(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 712 | { |
| 713 | int i; |
| 714 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 715 | for (i = pwr->num_clocks; i; --i) |
| 716 | clk_unprepare(pwr->clocks[i - 1]); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 719 | static int arm_smmu_enable_clocks(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 720 | { |
| 721 | int i, ret = 0; |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 722 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 723 | for (i = 0; i < pwr->num_clocks; ++i) { |
| 724 | ret = clk_enable(pwr->clocks[i]); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 725 | if (ret) { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 726 | dev_err(pwr->dev, "Couldn't enable clock #%d\n", i); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 727 | while (i--) |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 728 | clk_disable(pwr->clocks[i]); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 729 | break; |
| 730 | } |
| 731 | } |
| 732 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 733 | return ret; |
| 734 | } |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 735 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 736 | static void arm_smmu_disable_clocks(struct arm_smmu_power_resources *pwr) |
| 737 | { |
| 738 | int i; |
| 739 | |
| 740 | for (i = pwr->num_clocks; i; --i) |
| 741 | clk_disable(pwr->clocks[i - 1]); |
| 742 | } |
| 743 | |
| 744 | static int arm_smmu_request_bus(struct arm_smmu_power_resources *pwr) |
| 745 | { |
| 746 | if (!pwr->bus_client) |
| 747 | return 0; |
| 748 | return msm_bus_scale_client_update_request(pwr->bus_client, 1); |
| 749 | } |
| 750 | |
| 751 | static void arm_smmu_unrequest_bus(struct arm_smmu_power_resources *pwr) |
| 752 | { |
| 753 | if (!pwr->bus_client) |
| 754 | return; |
| 755 | WARN_ON(msm_bus_scale_client_update_request(pwr->bus_client, 0)); |
| 756 | } |
| 757 | |
| 758 | /* Clocks must be prepared before this (arm_smmu_prepare_clocks) */ |
| 759 | static int arm_smmu_power_on_atomic(struct arm_smmu_power_resources *pwr) |
| 760 | { |
| 761 | int ret = 0; |
| 762 | unsigned long flags; |
| 763 | |
| 764 | spin_lock_irqsave(&pwr->clock_refs_lock, flags); |
| 765 | if (pwr->clock_refs_count > 0) { |
| 766 | pwr->clock_refs_count++; |
| 767 | spin_unlock_irqrestore(&pwr->clock_refs_lock, flags); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | ret = arm_smmu_enable_clocks(pwr); |
| 772 | if (!ret) |
| 773 | pwr->clock_refs_count = 1; |
| 774 | |
| 775 | spin_unlock_irqrestore(&pwr->clock_refs_lock, flags); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | /* Clocks should be unprepared after this (arm_smmu_unprepare_clocks) */ |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 780 | static void arm_smmu_power_off_atomic(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 781 | { |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 782 | unsigned long flags; |
| 783 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 784 | spin_lock_irqsave(&pwr->clock_refs_lock, flags); |
| 785 | if (pwr->clock_refs_count == 0) { |
| 786 | WARN(1, "%s: bad clock_ref_count\n", dev_name(pwr->dev)); |
| 787 | spin_unlock_irqrestore(&pwr->clock_refs_lock, flags); |
| 788 | return; |
| 789 | |
| 790 | } else if (pwr->clock_refs_count > 1) { |
| 791 | pwr->clock_refs_count--; |
| 792 | spin_unlock_irqrestore(&pwr->clock_refs_lock, flags); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 796 | arm_smmu_disable_clocks(pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 797 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 798 | pwr->clock_refs_count = 0; |
| 799 | spin_unlock_irqrestore(&pwr->clock_refs_lock, flags); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 802 | static int arm_smmu_power_on_slow(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 803 | { |
| 804 | int ret; |
| 805 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 806 | mutex_lock(&pwr->power_lock); |
| 807 | if (pwr->power_count > 0) { |
| 808 | pwr->power_count += 1; |
| 809 | mutex_unlock(&pwr->power_lock); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 813 | ret = regulator_bulk_enable(pwr->num_gdscs, pwr->gdscs); |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 814 | if (ret) |
| 815 | goto out_unlock; |
| 816 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 817 | ret = arm_smmu_request_bus(pwr); |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 818 | if (ret) |
| 819 | goto out_disable_regulators; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 820 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 821 | ret = arm_smmu_prepare_clocks(pwr); |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 822 | if (ret) |
| 823 | goto out_disable_bus; |
| 824 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 825 | pwr->power_count = 1; |
| 826 | mutex_unlock(&pwr->power_lock); |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 827 | return 0; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 828 | |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 829 | out_disable_bus: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 830 | arm_smmu_unrequest_bus(pwr); |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 831 | out_disable_regulators: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 832 | regulator_bulk_disable(pwr->num_gdscs, pwr->gdscs); |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 833 | out_unlock: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 834 | mutex_unlock(&pwr->power_lock); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 835 | return ret; |
| 836 | } |
| 837 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 838 | static void arm_smmu_power_off_slow(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 839 | { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 840 | mutex_lock(&pwr->power_lock); |
| 841 | if (pwr->power_count == 0) { |
| 842 | WARN(1, "%s: Bad power count\n", dev_name(pwr->dev)); |
| 843 | mutex_unlock(&pwr->power_lock); |
| 844 | return; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 845 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 846 | } else if (pwr->power_count > 1) { |
| 847 | pwr->power_count--; |
| 848 | mutex_unlock(&pwr->power_lock); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 849 | return; |
| 850 | } |
| 851 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 852 | arm_smmu_unprepare_clocks(pwr); |
| 853 | arm_smmu_unrequest_bus(pwr); |
| 854 | regulator_bulk_disable(pwr->num_gdscs, pwr->gdscs); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 855 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 856 | mutex_unlock(&pwr->power_lock); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 859 | static int arm_smmu_power_on(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 860 | { |
| 861 | int ret; |
| 862 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 863 | ret = arm_smmu_power_on_slow(pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 864 | if (ret) |
| 865 | return ret; |
| 866 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 867 | ret = arm_smmu_power_on_atomic(pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 868 | if (ret) |
| 869 | goto out_disable; |
| 870 | |
| 871 | return 0; |
| 872 | |
| 873 | out_disable: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 874 | arm_smmu_power_off_slow(pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 875 | return ret; |
| 876 | } |
| 877 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 878 | static void arm_smmu_power_off(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 879 | { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 880 | arm_smmu_power_off_atomic(pwr); |
| 881 | arm_smmu_power_off_slow(pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Must be used instead of arm_smmu_power_on if it may be called from |
| 886 | * atomic context |
| 887 | */ |
| 888 | static int arm_smmu_domain_power_on(struct iommu_domain *domain, |
| 889 | struct arm_smmu_device *smmu) |
| 890 | { |
| 891 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 892 | int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC); |
| 893 | |
| 894 | if (atomic_domain) |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 895 | return arm_smmu_power_on_atomic(smmu->pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 896 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 897 | return arm_smmu_power_on(smmu->pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Must be used instead of arm_smmu_power_on if it may be called from |
| 902 | * atomic context |
| 903 | */ |
| 904 | static void arm_smmu_domain_power_off(struct iommu_domain *domain, |
| 905 | struct arm_smmu_device *smmu) |
| 906 | { |
| 907 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 908 | int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC); |
| 909 | |
| 910 | if (atomic_domain) { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 911 | arm_smmu_power_off_atomic(smmu->pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 915 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 918 | /* Wait for any pending TLB invalidations to complete */ |
Mitchel Humpherys | f300799 | 2015-06-19 15:00:14 -0700 | [diff] [blame] | 919 | static void arm_smmu_tlb_sync_cb(struct arm_smmu_device *smmu, |
| 920 | int cbndx) |
| 921 | { |
| 922 | void __iomem *base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cbndx); |
| 923 | u32 val; |
| 924 | |
| 925 | writel_relaxed(0, base + ARM_SMMU_CB_TLBSYNC); |
| 926 | if (readl_poll_timeout_atomic(base + ARM_SMMU_CB_TLBSTATUS, val, |
| 927 | !(val & TLBSTATUS_SACTIVE), |
Mitchel Humpherys | 9b1b894 | 2015-06-25 18:17:15 -0700 | [diff] [blame] | 928 | 0, TLB_LOOP_TIMEOUT)) |
Mitchel Humpherys | f300799 | 2015-06-19 15:00:14 -0700 | [diff] [blame] | 929 | dev_err(smmu->dev, "TLBSYNC timeout!\n"); |
| 930 | } |
| 931 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 932 | static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 933 | { |
| 934 | int count = 0; |
| 935 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 936 | |
| 937 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC); |
| 938 | while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS) |
| 939 | & sTLBGSTATUS_GSACTIVE) { |
| 940 | cpu_relax(); |
| 941 | if (++count == TLB_LOOP_TIMEOUT) { |
| 942 | dev_err_ratelimited(smmu->dev, |
| 943 | "TLB sync timed out -- SMMU may be deadlocked\n"); |
| 944 | return; |
| 945 | } |
| 946 | udelay(1); |
| 947 | } |
| 948 | } |
| 949 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 950 | static void arm_smmu_tlb_sync(void *cookie) |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 951 | { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 952 | struct arm_smmu_domain *smmu_domain = cookie; |
Mitchel Humpherys | f300799 | 2015-06-19 15:00:14 -0700 | [diff] [blame] | 953 | arm_smmu_tlb_sync_cb(smmu_domain->smmu, smmu_domain->cfg.cbndx); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 956 | /* Must be called with clocks/regulators enabled */ |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 957 | static void arm_smmu_tlb_inv_context(void *cookie) |
| 958 | { |
| 959 | struct arm_smmu_domain *smmu_domain = cookie; |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 960 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 961 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 962 | bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 963 | void __iomem *base; |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 964 | |
| 965 | if (stage1) { |
| 966 | base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 967 | writel_relaxed(ARM_SMMU_CB_ASID(smmu, cfg), |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 968 | base + ARM_SMMU_CB_S1_TLBIASID); |
Mitchel Humpherys | f300799 | 2015-06-19 15:00:14 -0700 | [diff] [blame] | 969 | arm_smmu_tlb_sync_cb(smmu, cfg->cbndx); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 970 | } else { |
| 971 | base = ARM_SMMU_GR0(smmu); |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 972 | writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 973 | base + ARM_SMMU_GR0_TLBIVMID); |
Mitchel Humpherys | f300799 | 2015-06-19 15:00:14 -0700 | [diff] [blame] | 974 | __arm_smmu_tlb_sync(smmu); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 975 | } |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 978 | static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size, |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 979 | size_t granule, bool leaf, void *cookie) |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 980 | { |
| 981 | struct arm_smmu_domain *smmu_domain = cookie; |
| 982 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 983 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
| 984 | bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; |
| 985 | void __iomem *reg; |
| 986 | |
| 987 | if (stage1) { |
| 988 | reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 989 | reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA; |
| 990 | |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 991 | if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 992 | iova &= ~12UL; |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 993 | iova |= ARM_SMMU_CB_ASID(smmu, cfg); |
Robin Murphy | 75df138 | 2015-12-07 18:18:52 +0000 | [diff] [blame] | 994 | do { |
| 995 | writel_relaxed(iova, reg); |
| 996 | iova += granule; |
| 997 | } while (size -= granule); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 998 | } else { |
| 999 | iova >>= 12; |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 1000 | iova |= (u64)ARM_SMMU_CB_ASID(smmu, cfg) << 48; |
Robin Murphy | 75df138 | 2015-12-07 18:18:52 +0000 | [diff] [blame] | 1001 | do { |
| 1002 | writeq_relaxed(iova, reg); |
| 1003 | iova += granule >> 12; |
| 1004 | } while (size -= granule); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1005 | } |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1006 | } else if (smmu->version == ARM_SMMU_V2) { |
| 1007 | reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 1008 | reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L : |
| 1009 | ARM_SMMU_CB_S2_TLBIIPAS2; |
Robin Murphy | 75df138 | 2015-12-07 18:18:52 +0000 | [diff] [blame] | 1010 | iova >>= 12; |
| 1011 | do { |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 1012 | smmu_write_atomic_lq(iova, reg); |
Robin Murphy | 75df138 | 2015-12-07 18:18:52 +0000 | [diff] [blame] | 1013 | iova += granule >> 12; |
| 1014 | } while (size -= granule); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1015 | } else { |
| 1016 | reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID; |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 1017 | writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), reg); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1021 | struct arm_smmu_secure_pool_chunk { |
| 1022 | void *addr; |
| 1023 | size_t size; |
| 1024 | struct list_head list; |
| 1025 | }; |
| 1026 | |
| 1027 | static void *arm_smmu_secure_pool_remove(struct arm_smmu_domain *smmu_domain, |
| 1028 | size_t size) |
| 1029 | { |
| 1030 | struct arm_smmu_secure_pool_chunk *it; |
| 1031 | |
| 1032 | list_for_each_entry(it, &smmu_domain->secure_pool_list, list) { |
| 1033 | if (it->size == size) { |
| 1034 | void *addr = it->addr; |
| 1035 | |
| 1036 | list_del(&it->list); |
| 1037 | kfree(it); |
| 1038 | return addr; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | |
| 1045 | static int arm_smmu_secure_pool_add(struct arm_smmu_domain *smmu_domain, |
| 1046 | void *addr, size_t size) |
| 1047 | { |
| 1048 | struct arm_smmu_secure_pool_chunk *chunk; |
| 1049 | |
| 1050 | chunk = kmalloc(sizeof(*chunk), GFP_ATOMIC); |
| 1051 | if (!chunk) |
| 1052 | return -ENOMEM; |
| 1053 | |
| 1054 | chunk->addr = addr; |
| 1055 | chunk->size = size; |
| 1056 | memset(addr, 0, size); |
| 1057 | list_add(&chunk->list, &smmu_domain->secure_pool_list); |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | static void arm_smmu_secure_pool_destroy(struct arm_smmu_domain *smmu_domain) |
| 1063 | { |
| 1064 | struct arm_smmu_secure_pool_chunk *it, *i; |
| 1065 | |
| 1066 | list_for_each_entry_safe(it, i, &smmu_domain->secure_pool_list, list) { |
| 1067 | arm_smmu_unprepare_pgtable(smmu_domain, it->addr, it->size); |
| 1068 | /* pages will be freed later (after being unassigned) */ |
| 1069 | kfree(it); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1073 | static void *arm_smmu_alloc_pages_exact(void *cookie, |
| 1074 | size_t size, gfp_t gfp_mask) |
| 1075 | { |
| 1076 | int ret; |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1077 | void *page; |
| 1078 | struct arm_smmu_domain *smmu_domain = cookie; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1079 | |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1080 | if (!arm_smmu_is_domain_secure(smmu_domain)) |
| 1081 | return alloc_pages_exact(size, gfp_mask); |
| 1082 | |
| 1083 | page = arm_smmu_secure_pool_remove(smmu_domain, size); |
| 1084 | if (page) |
| 1085 | return page; |
| 1086 | |
| 1087 | page = alloc_pages_exact(size, gfp_mask); |
| 1088 | if (page) { |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1089 | ret = arm_smmu_prepare_pgtable(page, cookie); |
| 1090 | if (ret) { |
| 1091 | free_pages_exact(page, size); |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | return page; |
| 1097 | } |
| 1098 | |
| 1099 | static void arm_smmu_free_pages_exact(void *cookie, void *virt, size_t size) |
| 1100 | { |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1101 | struct arm_smmu_domain *smmu_domain = cookie; |
| 1102 | |
| 1103 | if (!arm_smmu_is_domain_secure(smmu_domain)) { |
| 1104 | free_pages_exact(virt, size); |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | if (arm_smmu_secure_pool_add(smmu_domain, virt, size)) |
| 1109 | arm_smmu_unprepare_pgtable(smmu_domain, virt, size); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1112 | static struct iommu_gather_ops arm_smmu_gather_ops = { |
| 1113 | .tlb_flush_all = arm_smmu_tlb_inv_context, |
| 1114 | .tlb_add_flush = arm_smmu_tlb_inv_range_nosync, |
| 1115 | .tlb_sync = arm_smmu_tlb_sync, |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1116 | .alloc_pages_exact = arm_smmu_alloc_pages_exact, |
| 1117 | .free_pages_exact = arm_smmu_free_pages_exact, |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1118 | }; |
| 1119 | |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1120 | static phys_addr_t arm_smmu_verify_fault(struct iommu_domain *domain, |
| 1121 | dma_addr_t iova, u32 fsr) |
| 1122 | { |
| 1123 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1124 | struct arm_smmu_device *smmu; |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1125 | phys_addr_t phys; |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 1126 | phys_addr_t phys_post_tlbiall; |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1127 | |
| 1128 | smmu = smmu_domain->smmu; |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1129 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 1130 | if (smmu->arch_ops && smmu->arch_ops->iova_to_phys_fault) { |
| 1131 | smmu->arch_ops->iova_to_phys_fault(domain, iova, &phys, |
| 1132 | &phys_post_tlbiall); |
| 1133 | } else { |
| 1134 | phys = arm_smmu_iova_to_phys_hard(domain, iova); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1135 | arm_smmu_tlb_inv_context(smmu_domain); |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 1136 | phys_post_tlbiall = arm_smmu_iova_to_phys_hard(domain, iova); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 1139 | if (phys != phys_post_tlbiall) { |
| 1140 | dev_err(smmu->dev, |
| 1141 | "ATOS results differed across TLBIALL...\n" |
| 1142 | "Before: %pa After: %pa\n", &phys, &phys_post_tlbiall); |
| 1143 | } |
| 1144 | if (!phys_post_tlbiall) { |
| 1145 | dev_err(smmu->dev, |
| 1146 | "ATOS still failed. If the page tables look good (check the software table walk) then hardware might be misbehaving.\n"); |
| 1147 | } |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1148 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 1149 | return phys_post_tlbiall; |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1152 | static irqreturn_t arm_smmu_context_fault(int irq, void *dev) |
| 1153 | { |
Sushmita Susheelendra | a474ae1 | 2015-06-02 15:46:24 -0600 | [diff] [blame] | 1154 | int flags, ret, tmp; |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1155 | u32 fsr, fsynr, resume; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1156 | unsigned long iova; |
| 1157 | struct iommu_domain *domain = dev; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1158 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1159 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 1160 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1161 | void __iomem *cb_base; |
Shalaj Jain | 04059c5 | 2015-03-03 13:34:59 -0800 | [diff] [blame] | 1162 | void __iomem *gr1_base; |
Mitchel Humpherys | 07ba44b | 2015-01-30 14:58:52 -0800 | [diff] [blame] | 1163 | bool fatal_asf = smmu->options & ARM_SMMU_OPT_FATAL_ASF; |
Mitchel Humpherys | b8be413 | 2015-02-06 14:25:10 -0800 | [diff] [blame] | 1164 | phys_addr_t phys_soft; |
Shalaj Jain | 04059c5 | 2015-03-03 13:34:59 -0800 | [diff] [blame] | 1165 | u32 frsynra; |
Mitchel Humpherys | cc8d12f | 2015-09-25 17:29:27 -0700 | [diff] [blame] | 1166 | bool non_fatal_fault = !!(smmu_domain->attributes & |
| 1167 | DOMAIN_ATTR_NON_FATAL_FAULTS); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1168 | |
Mitchel Humpherys | a8dabc9 | 2015-09-14 12:08:09 -0700 | [diff] [blame] | 1169 | static DEFINE_RATELIMIT_STATE(_rs, |
| 1170 | DEFAULT_RATELIMIT_INTERVAL, |
| 1171 | DEFAULT_RATELIMIT_BURST); |
| 1172 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1173 | ret = arm_smmu_power_on(smmu->pwr); |
Mitchel Humpherys | 3e52a7e | 2015-10-19 17:13:47 -0700 | [diff] [blame] | 1174 | if (ret) |
| 1175 | return IRQ_NONE; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1176 | |
Shalaj Jain | 04059c5 | 2015-03-03 13:34:59 -0800 | [diff] [blame] | 1177 | gr1_base = ARM_SMMU_GR1(smmu); |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1178 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1179 | fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR); |
| 1180 | |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1181 | if (!(fsr & FSR_FAULT)) { |
| 1182 | ret = IRQ_NONE; |
| 1183 | goto out_power_off; |
| 1184 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1185 | |
Mitchel Humpherys | 07ba44b | 2015-01-30 14:58:52 -0800 | [diff] [blame] | 1186 | if (fatal_asf && (fsr & FSR_ASF)) { |
| 1187 | dev_err(smmu->dev, |
| 1188 | "Took an address size fault. Refusing to recover.\n"); |
| 1189 | BUG(); |
| 1190 | } |
| 1191 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1192 | fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0); |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1193 | flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ; |
Sushmita Susheelendra | a474ae1 | 2015-06-02 15:46:24 -0600 | [diff] [blame] | 1194 | if (fsr & FSR_TF) |
| 1195 | flags |= IOMMU_FAULT_TRANSLATION; |
| 1196 | if (fsr & FSR_PF) |
| 1197 | flags |= IOMMU_FAULT_PERMISSION; |
Mitchel Humpherys | dd75e33 | 2015-08-19 11:02:33 -0700 | [diff] [blame] | 1198 | if (fsr & FSR_EF) |
| 1199 | flags |= IOMMU_FAULT_EXTERNAL; |
Sushmita Susheelendra | a474ae1 | 2015-06-02 15:46:24 -0600 | [diff] [blame] | 1200 | if (fsr & FSR_SS) |
| 1201 | flags |= IOMMU_FAULT_TRANSACTION_STALLED; |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1202 | |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 1203 | iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR); |
Mitchel Humpherys | b8be413 | 2015-02-06 14:25:10 -0800 | [diff] [blame] | 1204 | phys_soft = arm_smmu_iova_to_phys(domain, iova); |
Shalaj Jain | 04059c5 | 2015-03-03 13:34:59 -0800 | [diff] [blame] | 1205 | frsynra = readl_relaxed(gr1_base + ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx)); |
| 1206 | frsynra &= CBFRSYNRA_SID_MASK; |
Sushmita Susheelendra | a474ae1 | 2015-06-02 15:46:24 -0600 | [diff] [blame] | 1207 | tmp = report_iommu_fault(domain, smmu->dev, iova, flags); |
| 1208 | if (!tmp || (tmp == -EBUSY)) { |
Mitchel Humpherys | b8be413 | 2015-02-06 14:25:10 -0800 | [diff] [blame] | 1209 | dev_dbg(smmu->dev, |
| 1210 | "Context fault handled by client: iova=0x%08lx, fsr=0x%x, fsynr=0x%x, cb=%d\n", |
| 1211 | iova, fsr, fsynr, cfg->cbndx); |
| 1212 | dev_dbg(smmu->dev, |
| 1213 | "soft iova-to-phys=%pa\n", &phys_soft); |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1214 | ret = IRQ_HANDLED; |
Shrenuj Bansal | d5083c0 | 2015-09-18 14:59:09 -0700 | [diff] [blame] | 1215 | resume = RESUME_TERMINATE; |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1216 | } else { |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 1217 | phys_addr_t phys_atos = arm_smmu_verify_fault(domain, iova, |
| 1218 | fsr); |
Mitchel Humpherys | a8dabc9 | 2015-09-14 12:08:09 -0700 | [diff] [blame] | 1219 | if (__ratelimit(&_rs)) { |
| 1220 | dev_err(smmu->dev, |
| 1221 | "Unhandled context fault: iova=0x%08lx, fsr=0x%x, fsynr=0x%x, cb=%d\n", |
| 1222 | iova, fsr, fsynr, cfg->cbndx); |
| 1223 | dev_err(smmu->dev, "FAR = %016lx\n", |
| 1224 | (unsigned long)iova); |
| 1225 | dev_err(smmu->dev, |
| 1226 | "FSR = %08x [%s%s%s%s%s%s%s%s%s]\n", |
| 1227 | fsr, |
| 1228 | (fsr & 0x02) ? "TF " : "", |
| 1229 | (fsr & 0x04) ? "AFF " : "", |
| 1230 | (fsr & 0x08) ? "PF " : "", |
| 1231 | (fsr & 0x10) ? "EF " : "", |
| 1232 | (fsr & 0x20) ? "TLBMCF " : "", |
| 1233 | (fsr & 0x40) ? "TLBLKF " : "", |
| 1234 | (fsr & 0x80) ? "MHF " : "", |
| 1235 | (fsr & 0x40000000) ? "SS " : "", |
| 1236 | (fsr & 0x80000000) ? "MULTI " : ""); |
| 1237 | dev_err(smmu->dev, |
| 1238 | "soft iova-to-phys=%pa\n", &phys_soft); |
Mitchel Humpherys | d03b65d | 2015-11-05 11:50:29 -0800 | [diff] [blame] | 1239 | if (!phys_soft) |
| 1240 | dev_err(smmu->dev, |
| 1241 | "SOFTWARE TABLE WALK FAILED! Looks like %s accessed an unmapped address!\n", |
| 1242 | dev_name(smmu->dev)); |
Mitchel Humpherys | a8dabc9 | 2015-09-14 12:08:09 -0700 | [diff] [blame] | 1243 | dev_err(smmu->dev, |
| 1244 | "hard iova-to-phys (ATOS)=%pa\n", &phys_atos); |
| 1245 | dev_err(smmu->dev, "SID=0x%x\n", frsynra); |
| 1246 | } |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1247 | ret = IRQ_NONE; |
| 1248 | resume = RESUME_TERMINATE; |
Mitchel Humpherys | cc8d12f | 2015-09-25 17:29:27 -0700 | [diff] [blame] | 1249 | if (!non_fatal_fault) { |
| 1250 | dev_err(smmu->dev, |
| 1251 | "Unhandled arm-smmu context fault!\n"); |
| 1252 | BUG(); |
| 1253 | } |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1254 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1255 | |
Sushmita Susheelendra | a474ae1 | 2015-06-02 15:46:24 -0600 | [diff] [blame] | 1256 | /* |
| 1257 | * If the client returns -EBUSY, do not clear FSR and do not RESUME |
| 1258 | * if stalled. This is required to keep the IOMMU client stalled on |
| 1259 | * the outstanding fault. This gives the client a chance to take any |
| 1260 | * debug action and then terminate the stalled transaction. |
| 1261 | * So, the sequence in case of stall on fault should be: |
| 1262 | * 1) Do not clear FSR or write to RESUME here |
| 1263 | * 2) Client takes any debug action |
| 1264 | * 3) Client terminates the stalled transaction and resumes the IOMMU |
| 1265 | * 4) Client clears FSR. The FSR should only be cleared after 3) and |
| 1266 | * not before so that the fault remains outstanding. This ensures |
| 1267 | * SCTLR.HUPCF has the desired effect if subsequent transactions also |
| 1268 | * need to be terminated. |
| 1269 | */ |
| 1270 | if (tmp != -EBUSY) { |
| 1271 | /* Clear the faulting FSR */ |
| 1272 | writel_relaxed(fsr, cb_base + ARM_SMMU_CB_FSR); |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1273 | |
Sushmita Susheelendra | a474ae1 | 2015-06-02 15:46:24 -0600 | [diff] [blame] | 1274 | /* |
| 1275 | * Barrier required to ensure that the FSR is cleared |
| 1276 | * before resuming SMMU operation |
| 1277 | */ |
| 1278 | wmb(); |
| 1279 | |
| 1280 | /* Retry or terminate any stalled transactions */ |
| 1281 | if (fsr & FSR_SS) |
| 1282 | writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME); |
| 1283 | } |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1284 | |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1285 | out_power_off: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1286 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1287 | |
Patrick Daly | 5ba2811 | 2016-08-30 19:18:52 -0700 | [diff] [blame] | 1288 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | static irqreturn_t arm_smmu_global_fault(int irq, void *dev) |
| 1292 | { |
| 1293 | u32 gfsr, gfsynr0, gfsynr1, gfsynr2; |
| 1294 | struct arm_smmu_device *smmu = dev; |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 1295 | void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1296 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1297 | if (arm_smmu_power_on(smmu->pwr)) |
Mitchel Humpherys | 3e52a7e | 2015-10-19 17:13:47 -0700 | [diff] [blame] | 1298 | return IRQ_NONE; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1299 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1300 | gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR); |
| 1301 | gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0); |
| 1302 | gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1); |
| 1303 | gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2); |
| 1304 | |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1305 | if (!gfsr) { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1306 | arm_smmu_power_off(smmu->pwr); |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 1307 | return IRQ_NONE; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1308 | } |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 1309 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1310 | dev_err_ratelimited(smmu->dev, |
| 1311 | "Unexpected global fault, this could be serious\n"); |
| 1312 | dev_err_ratelimited(smmu->dev, |
| 1313 | "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n", |
| 1314 | gfsr, gfsynr0, gfsynr1, gfsynr2); |
| 1315 | |
| 1316 | writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR); |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1317 | arm_smmu_power_off(smmu->pwr); |
Will Deacon | adaba32 | 2013-07-31 19:21:26 +0100 | [diff] [blame] | 1318 | return IRQ_HANDLED; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1321 | static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain, |
| 1322 | struct io_pgtable_cfg *pgtbl_cfg) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1323 | { |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1324 | u32 reg, reg2; |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 1325 | u64 reg64; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1326 | bool stage1; |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1327 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 1328 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
Will Deacon | c88ae5d | 2015-10-13 17:53:24 +0100 | [diff] [blame] | 1329 | void __iomem *cb_base, *gr1_base; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1330 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1331 | gr1_base = ARM_SMMU_GR1(smmu); |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1332 | stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; |
| 1333 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1334 | |
Will Deacon | 4a1c93c | 2015-03-04 12:21:03 +0000 | [diff] [blame] | 1335 | if (smmu->version > ARM_SMMU_V1) { |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1336 | if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) |
| 1337 | reg = CBA2R_RW64_64BIT; |
| 1338 | else |
| 1339 | reg = CBA2R_RW64_32BIT; |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 1340 | /* 16-bit VMIDs live in CBA2R */ |
| 1341 | if (smmu->features & ARM_SMMU_FEAT_VMID16) |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 1342 | reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBA2R_VMID_SHIFT; |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 1343 | |
Will Deacon | 4a1c93c | 2015-03-04 12:21:03 +0000 | [diff] [blame] | 1344 | writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); |
| 1345 | } |
| 1346 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1347 | /* CBAR */ |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1348 | reg = cfg->cbar; |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 1349 | if (smmu->version < ARM_SMMU_V2) |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 1350 | reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1351 | |
Will Deacon | 57ca90f | 2014-02-06 14:59:05 +0000 | [diff] [blame] | 1352 | /* |
| 1353 | * Use the weakest shareability/memory types, so they are |
| 1354 | * overridden by the ttbcr/pte. |
| 1355 | */ |
| 1356 | if (stage1) { |
| 1357 | reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) | |
| 1358 | (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT); |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 1359 | } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) { |
| 1360 | /* 8-bit VMIDs live in CBAR */ |
Tirumalesh Chalamarla | 1bd37a6 | 2016-03-04 13:56:09 -0800 | [diff] [blame] | 1361 | reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBAR_VMID_SHIFT; |
Will Deacon | 57ca90f | 2014-02-06 14:59:05 +0000 | [diff] [blame] | 1362 | } |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1363 | writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx)); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1364 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1365 | /* TTBRs */ |
| 1366 | if (stage1) { |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1367 | u16 asid = ARM_SMMU_CB_ASID(smmu, cfg); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1368 | |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1369 | if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { |
| 1370 | reg = pgtbl_cfg->arm_v7s_cfg.ttbr[0]; |
| 1371 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0); |
| 1372 | reg = pgtbl_cfg->arm_v7s_cfg.ttbr[1]; |
| 1373 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1); |
| 1374 | writel_relaxed(asid, cb_base + ARM_SMMU_CB_CONTEXTIDR); |
| 1375 | } else { |
| 1376 | reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0]; |
| 1377 | reg64 |= (u64)asid << TTBRn_ASID_SHIFT; |
| 1378 | writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0); |
| 1379 | reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1]; |
| 1380 | reg64 |= (u64)asid << TTBRn_ASID_SHIFT; |
| 1381 | writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR1); |
| 1382 | } |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1383 | } else { |
Tirumalesh Chalamarla | 668b4ad | 2015-08-19 00:40:30 +0100 | [diff] [blame] | 1384 | reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 1385 | writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1386 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1387 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1388 | /* TTBCR */ |
| 1389 | if (stage1) { |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1390 | if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { |
| 1391 | reg = pgtbl_cfg->arm_v7s_cfg.tcr; |
| 1392 | reg2 = 0; |
| 1393 | } else { |
| 1394 | reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr; |
| 1395 | reg2 = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32; |
| 1396 | reg2 |= TTBCR2_SEP_UPSTREAM; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1397 | } |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1398 | if (smmu->version > ARM_SMMU_V1) |
| 1399 | writel_relaxed(reg2, cb_base + ARM_SMMU_CB_TTBCR2); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1400 | } else { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1401 | reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1402 | } |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1403 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1404 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1405 | /* MAIRs (stage-1 only) */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1406 | if (stage1) { |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1407 | if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { |
| 1408 | reg = pgtbl_cfg->arm_v7s_cfg.prrr; |
| 1409 | reg2 = pgtbl_cfg->arm_v7s_cfg.nmrr; |
| 1410 | } else { |
| 1411 | reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0]; |
| 1412 | reg2 = pgtbl_cfg->arm_lpae_s1_cfg.mair[1]; |
| 1413 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1414 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0); |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1415 | writel_relaxed(reg2, cb_base + ARM_SMMU_CB_S1_MAIR1); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1418 | /* SCTLR */ |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1419 | reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE; |
Patrick Daly | e62d336 | 2016-03-15 18:58:28 -0700 | [diff] [blame] | 1420 | if (!(smmu_domain->attributes & (1 << DOMAIN_ATTR_S1_BYPASS)) || |
| 1421 | !stage1) |
| 1422 | reg |= SCTLR_M; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1423 | if (stage1) |
| 1424 | reg |= SCTLR_S1_ASIDPNE; |
| 1425 | #ifdef __BIG_ENDIAN |
| 1426 | reg |= SCTLR_E; |
| 1427 | #endif |
Will Deacon | 2572484 | 2013-08-21 13:49:53 +0100 | [diff] [blame] | 1428 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1431 | static int arm_smmu_init_asid(struct iommu_domain *domain, |
| 1432 | struct arm_smmu_device *smmu) |
| 1433 | { |
| 1434 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 1435 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 1436 | bool dynamic = is_dynamic_domain(domain); |
| 1437 | int ret; |
| 1438 | |
| 1439 | if (!dynamic) { |
| 1440 | cfg->asid = cfg->cbndx + 1; |
| 1441 | } else { |
| 1442 | mutex_lock(&smmu->idr_mutex); |
| 1443 | ret = idr_alloc_cyclic(&smmu->asid_idr, domain, |
| 1444 | smmu->num_context_banks + 2, |
| 1445 | MAX_ASID + 1, GFP_KERNEL); |
| 1446 | |
| 1447 | mutex_unlock(&smmu->idr_mutex); |
| 1448 | if (ret < 0) { |
| 1449 | dev_err(smmu->dev, "dynamic ASID allocation failed: %d\n", |
| 1450 | ret); |
| 1451 | return ret; |
| 1452 | } |
| 1453 | cfg->asid = ret; |
| 1454 | } |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | static void arm_smmu_free_asid(struct iommu_domain *domain) |
| 1459 | { |
| 1460 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 1461 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
| 1462 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 1463 | bool dynamic = is_dynamic_domain(domain); |
| 1464 | |
| 1465 | if (cfg->asid == INVALID_ASID || !dynamic) |
| 1466 | return; |
| 1467 | |
| 1468 | mutex_lock(&smmu->idr_mutex); |
| 1469 | idr_remove(&smmu->asid_idr, cfg->asid); |
| 1470 | mutex_unlock(&smmu->idr_mutex); |
| 1471 | } |
| 1472 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1473 | static int arm_smmu_init_domain_context(struct iommu_domain *domain, |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1474 | struct arm_smmu_device *smmu) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1475 | { |
Mitchel Humpherys | a18037b | 2014-07-30 18:58:13 +0100 | [diff] [blame] | 1476 | int irq, start, ret = 0; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1477 | unsigned long ias, oas; |
| 1478 | struct io_pgtable_ops *pgtbl_ops; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1479 | enum io_pgtable_fmt fmt; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1480 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1481 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
Mitchel Humpherys | c625ce0 | 2015-10-07 14:03:50 -0700 | [diff] [blame] | 1482 | bool is_fast = smmu_domain->attributes & (1 << DOMAIN_ATTR_FAST); |
Patrick Daly | ce6786f | 2016-11-09 14:19:23 -0800 | [diff] [blame] | 1483 | unsigned long quirks = 0; |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1484 | bool dynamic; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1485 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1486 | mutex_lock(&smmu_domain->init_mutex); |
Mitchel Humpherys | a18037b | 2014-07-30 18:58:13 +0100 | [diff] [blame] | 1487 | if (smmu_domain->smmu) |
| 1488 | goto out_unlock; |
| 1489 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1490 | smmu_domain->cfg.irptndx = INVALID_IRPTNDX; |
| 1491 | smmu_domain->cfg.asid = INVALID_ASID; |
| 1492 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1493 | dynamic = is_dynamic_domain(domain); |
| 1494 | if (dynamic && !(smmu->options & ARM_SMMU_OPT_DYNAMIC)) { |
| 1495 | dev_err(smmu->dev, "dynamic domains not supported\n"); |
| 1496 | ret = -EPERM; |
| 1497 | goto out_unlock; |
| 1498 | } |
| 1499 | |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 1500 | /* |
| 1501 | * Mapping the requested stage onto what we support is surprisingly |
| 1502 | * complicated, mainly because the spec allows S1+S2 SMMUs without |
| 1503 | * support for nested translation. That means we end up with the |
| 1504 | * following table: |
| 1505 | * |
| 1506 | * Requested Supported Actual |
| 1507 | * S1 N S1 |
| 1508 | * S1 S1+S2 S1 |
| 1509 | * S1 S2 S2 |
| 1510 | * S1 S1 S1 |
| 1511 | * N N N |
| 1512 | * N S1+S2 S2 |
| 1513 | * N S2 S2 |
| 1514 | * N S1 S1 |
| 1515 | * |
| 1516 | * Note that you can't actually request stage-2 mappings. |
| 1517 | */ |
| 1518 | if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1)) |
| 1519 | smmu_domain->stage = ARM_SMMU_DOMAIN_S2; |
| 1520 | if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2)) |
| 1521 | smmu_domain->stage = ARM_SMMU_DOMAIN_S1; |
| 1522 | |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1523 | /* |
| 1524 | * Choosing a suitable context format is even more fiddly. Until we |
| 1525 | * grow some way for the caller to express a preference, and/or move |
| 1526 | * the decision into the io-pgtable code where it arguably belongs, |
| 1527 | * just aim for the closest thing to the rest of the system, and hope |
| 1528 | * that the hardware isn't esoteric enough that we can't assume AArch64 |
| 1529 | * support to be a superset of AArch32 support... |
| 1530 | */ |
| 1531 | if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L) |
| 1532 | cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L; |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1533 | if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) && |
| 1534 | !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) && |
| 1535 | (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) && |
| 1536 | (smmu_domain->stage == ARM_SMMU_DOMAIN_S1)) |
| 1537 | cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1538 | if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) && |
| 1539 | (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K | |
| 1540 | ARM_SMMU_FEAT_FMT_AARCH64_16K | |
| 1541 | ARM_SMMU_FEAT_FMT_AARCH64_4K))) |
| 1542 | cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64; |
| 1543 | |
| 1544 | if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) { |
| 1545 | ret = -EINVAL; |
| 1546 | goto out_unlock; |
| 1547 | } |
| 1548 | |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 1549 | switch (smmu_domain->stage) { |
| 1550 | case ARM_SMMU_DOMAIN_S1: |
| 1551 | cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS; |
| 1552 | start = smmu->num_s2_context_banks; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1553 | ias = smmu->va_size; |
| 1554 | oas = smmu->ipa_size; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1555 | if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1556 | fmt = ARM_64_LPAE_S1; |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1557 | } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1558 | fmt = ARM_32_LPAE_S1; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1559 | ias = min(ias, 32UL); |
| 1560 | oas = min(oas, 40UL); |
Robin Murphy | b94df6f | 2016-08-11 17:44:06 +0100 | [diff] [blame] | 1561 | } else { |
| 1562 | fmt = ARM_V7S; |
| 1563 | ias = min(ias, 32UL); |
| 1564 | oas = min(oas, 32UL); |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1565 | } |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 1566 | break; |
| 1567 | case ARM_SMMU_DOMAIN_NESTED: |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1568 | /* |
| 1569 | * We will likely want to change this if/when KVM gets |
| 1570 | * involved. |
| 1571 | */ |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 1572 | case ARM_SMMU_DOMAIN_S2: |
Will Deacon | 9c5c92e | 2014-06-25 12:12:41 +0100 | [diff] [blame] | 1573 | cfg->cbar = CBAR_TYPE_S2_TRANS; |
| 1574 | start = 0; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1575 | ias = smmu->ipa_size; |
| 1576 | oas = smmu->pa_size; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1577 | if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1578 | fmt = ARM_64_LPAE_S2; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1579 | } else { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1580 | fmt = ARM_32_LPAE_S2; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 1581 | ias = min(ias, 40UL); |
| 1582 | oas = min(oas, 40UL); |
| 1583 | } |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 1584 | break; |
| 1585 | default: |
| 1586 | ret = -EINVAL; |
| 1587 | goto out_unlock; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
Mitchel Humpherys | c625ce0 | 2015-10-07 14:03:50 -0700 | [diff] [blame] | 1590 | if (is_fast) |
| 1591 | fmt = ARM_V8L_FAST; |
| 1592 | |
Patrick Daly | ce6786f | 2016-11-09 14:19:23 -0800 | [diff] [blame] | 1593 | if (smmu_domain->attributes & (1 << DOMAIN_ATTR_USE_UPSTREAM_HINT)) |
| 1594 | quirks |= IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT; |
Mitchel Humpherys | c625ce0 | 2015-10-07 14:03:50 -0700 | [diff] [blame] | 1595 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1596 | /* Dynamic domains must set cbndx through domain attribute */ |
| 1597 | if (!dynamic) { |
| 1598 | ret = __arm_smmu_alloc_bitmap(smmu->context_map, start, |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1599 | smmu->num_context_banks); |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1600 | if (ret < 0) |
| 1601 | goto out_unlock; |
| 1602 | cfg->cbndx = ret; |
| 1603 | } |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 1604 | if (smmu->version < ARM_SMMU_V2) { |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1605 | cfg->irptndx = atomic_inc_return(&smmu->irptndx); |
| 1606 | cfg->irptndx %= smmu->num_context_irqs; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1607 | } else { |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1608 | cfg->irptndx = cfg->cbndx; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1609 | } |
| 1610 | |
Mitchel Humpherys | 39e9c91 | 2015-04-15 15:14:15 -0700 | [diff] [blame] | 1611 | smmu_domain->pgtbl_cfg = (struct io_pgtable_cfg) { |
Patrick Daly | ce6786f | 2016-11-09 14:19:23 -0800 | [diff] [blame] | 1612 | .quirks = quirks, |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 1613 | .pgsize_bitmap = smmu->pgsize_bitmap, |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1614 | .ias = ias, |
| 1615 | .oas = oas, |
| 1616 | .tlb = &arm_smmu_gather_ops, |
Robin Murphy | 2df7a25 | 2015-07-29 19:46:06 +0100 | [diff] [blame] | 1617 | .iommu_dev = smmu->dev, |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1618 | }; |
Mitchel Humpherys | a18037b | 2014-07-30 18:58:13 +0100 | [diff] [blame] | 1619 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1620 | smmu_domain->smmu = smmu; |
Mitchel Humpherys | 39e9c91 | 2015-04-15 15:14:15 -0700 | [diff] [blame] | 1621 | pgtbl_ops = alloc_io_pgtable_ops(fmt, &smmu_domain->pgtbl_cfg, |
| 1622 | smmu_domain); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1623 | if (!pgtbl_ops) { |
| 1624 | ret = -ENOMEM; |
| 1625 | goto out_clear_smmu; |
| 1626 | } |
| 1627 | |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1628 | /* |
| 1629 | * assign any page table memory that might have been allocated |
| 1630 | * during alloc_io_pgtable_ops |
| 1631 | */ |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1632 | arm_smmu_secure_domain_lock(smmu_domain); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1633 | arm_smmu_assign_table(smmu_domain); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1634 | arm_smmu_secure_domain_unlock(smmu_domain); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1635 | |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 1636 | /* Update the domain's page sizes to reflect the page table format */ |
Mitchel Humpherys | 39e9c91 | 2015-04-15 15:14:15 -0700 | [diff] [blame] | 1637 | domain->pgsize_bitmap = smmu_domain->pgtbl_cfg.pgsize_bitmap; |
Robin Murphy | d7a8d04 | 2016-09-12 17:13:58 +0100 | [diff] [blame] | 1638 | domain->geometry.aperture_end = (1UL << ias) - 1; |
| 1639 | domain->geometry.force_aperture = true; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1640 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1641 | /* Assign an asid */ |
| 1642 | ret = arm_smmu_init_asid(domain, smmu); |
| 1643 | if (ret) |
| 1644 | goto out_clear_smmu; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1645 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1646 | if (!dynamic) { |
| 1647 | /* Initialise the context bank with our page table cfg */ |
| 1648 | arm_smmu_init_context_bank(smmu_domain, |
| 1649 | &smmu_domain->pgtbl_cfg); |
| 1650 | |
| 1651 | /* |
| 1652 | * Request context fault interrupt. Do this last to avoid the |
| 1653 | * handler seeing a half-initialised domain state. |
| 1654 | */ |
| 1655 | irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx]; |
| 1656 | ret = devm_request_threaded_irq(smmu->dev, irq, NULL, |
Mitchel Humpherys | cca6011 | 2015-01-13 13:38:12 -0800 | [diff] [blame] | 1657 | arm_smmu_context_fault, IRQF_ONESHOT | IRQF_SHARED, |
| 1658 | "arm-smmu-context-fault", domain); |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1659 | if (ret < 0) { |
| 1660 | dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n", |
| 1661 | cfg->irptndx, irq); |
| 1662 | cfg->irptndx = INVALID_IRPTNDX; |
| 1663 | goto out_clear_smmu; |
| 1664 | } |
| 1665 | } else { |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1666 | cfg->irptndx = INVALID_IRPTNDX; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1667 | } |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1668 | mutex_unlock(&smmu_domain->init_mutex); |
| 1669 | |
| 1670 | /* Publish page table ops for map/unmap */ |
| 1671 | smmu_domain->pgtbl_ops = pgtbl_ops; |
Will Deacon | a9a1b0b | 2014-05-01 18:05:08 +0100 | [diff] [blame] | 1672 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1673 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1674 | out_clear_smmu: |
Jeremy Gebben | fa24b0c | 2015-06-16 12:45:31 -0600 | [diff] [blame] | 1675 | arm_smmu_destroy_domain_context(domain); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1676 | smmu_domain->smmu = NULL; |
Mitchel Humpherys | a18037b | 2014-07-30 18:58:13 +0100 | [diff] [blame] | 1677 | out_unlock: |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1678 | mutex_unlock(&smmu_domain->init_mutex); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1679 | return ret; |
| 1680 | } |
| 1681 | |
Patrick Daly | 77db4f9 | 2016-10-14 15:34:10 -0700 | [diff] [blame] | 1682 | static void arm_smmu_domain_reinit(struct arm_smmu_domain *smmu_domain) |
| 1683 | { |
| 1684 | smmu_domain->cfg.irptndx = INVALID_IRPTNDX; |
| 1685 | smmu_domain->cfg.cbndx = INVALID_CBNDX; |
| 1686 | smmu_domain->secure_vmid = VMID_INVAL; |
| 1687 | } |
| 1688 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1689 | static void arm_smmu_destroy_domain_context(struct iommu_domain *domain) |
| 1690 | { |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1691 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1692 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
| 1693 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1694 | void __iomem *cb_base; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1695 | int irq; |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1696 | bool dynamic; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1697 | int ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1698 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 1699 | if (!smmu) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1700 | return; |
| 1701 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1702 | ret = arm_smmu_power_on(smmu->pwr); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1703 | if (ret) { |
| 1704 | WARN_ONCE(ret, "Woops, powering on smmu %p failed. Leaking context bank\n", |
| 1705 | smmu); |
| 1706 | return; |
| 1707 | } |
| 1708 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1709 | dynamic = is_dynamic_domain(domain); |
| 1710 | if (dynamic) { |
| 1711 | arm_smmu_free_asid(domain); |
| 1712 | free_io_pgtable_ops(smmu_domain->pgtbl_ops); |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1713 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1714 | arm_smmu_secure_domain_lock(smmu_domain); |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1715 | arm_smmu_secure_pool_destroy(smmu_domain); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1716 | arm_smmu_unassign_table(smmu_domain); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1717 | arm_smmu_secure_domain_unlock(smmu_domain); |
Patrick Daly | 77db4f9 | 2016-10-14 15:34:10 -0700 | [diff] [blame] | 1718 | arm_smmu_domain_reinit(smmu_domain); |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 1719 | return; |
| 1720 | } |
| 1721 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1722 | /* |
| 1723 | * Disable the context bank and free the page tables before freeing |
| 1724 | * it. |
| 1725 | */ |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1726 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1727 | writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1728 | |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1729 | if (cfg->irptndx != INVALID_IRPTNDX) { |
| 1730 | irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx]; |
Peng Fan | bee1400 | 2016-07-04 17:38:22 +0800 | [diff] [blame] | 1731 | devm_free_irq(smmu->dev, irq, domain); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Markus Elfring | 44830b0 | 2015-11-06 18:32:41 +0100 | [diff] [blame] | 1734 | free_io_pgtable_ops(smmu_domain->pgtbl_ops); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1735 | arm_smmu_secure_domain_lock(smmu_domain); |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1736 | arm_smmu_secure_pool_destroy(smmu_domain); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1737 | arm_smmu_unassign_table(smmu_domain); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1738 | arm_smmu_secure_domain_unlock(smmu_domain); |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1739 | __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 1740 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1741 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 77db4f9 | 2016-10-14 15:34:10 -0700 | [diff] [blame] | 1742 | arm_smmu_domain_reinit(smmu_domain); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1743 | } |
| 1744 | |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1745 | static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1746 | { |
| 1747 | struct arm_smmu_domain *smmu_domain; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1748 | |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 1749 | /* Do not support DOMAIN_DMA for now */ |
| 1750 | if (type != IOMMU_DOMAIN_UNMANAGED) |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1751 | return NULL; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1752 | /* |
| 1753 | * Allocate the domain and initialise some of its data structures. |
| 1754 | * We can't really do anything meaningful until we've added a |
| 1755 | * master. |
| 1756 | */ |
| 1757 | smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL); |
| 1758 | if (!smmu_domain) |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1759 | return NULL; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1760 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 1761 | if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding || |
| 1762 | iommu_get_dma_cookie(&smmu_domain->domain))) { |
Robin Murphy | 9adb959 | 2016-01-26 18:06:36 +0000 | [diff] [blame] | 1763 | kfree(smmu_domain); |
| 1764 | return NULL; |
| 1765 | } |
| 1766 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 1767 | mutex_init(&smmu_domain->init_mutex); |
| 1768 | spin_lock_init(&smmu_domain->pgtbl_lock); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1769 | INIT_LIST_HEAD(&smmu_domain->pte_info_list); |
| 1770 | INIT_LIST_HEAD(&smmu_domain->unassign_list); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1771 | mutex_init(&smmu_domain->assign_lock); |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 1772 | INIT_LIST_HEAD(&smmu_domain->secure_pool_list); |
Patrick Daly | 77db4f9 | 2016-10-14 15:34:10 -0700 | [diff] [blame] | 1773 | arm_smmu_domain_reinit(smmu_domain); |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1774 | |
| 1775 | return &smmu_domain->domain; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1776 | } |
| 1777 | |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1778 | static void arm_smmu_domain_free(struct iommu_domain *domain) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1779 | { |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 1780 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 1781 | |
| 1782 | /* |
| 1783 | * Free the domain resources. We assume that all devices have |
| 1784 | * already been detached. |
| 1785 | */ |
Robin Murphy | 9adb959 | 2016-01-26 18:06:36 +0000 | [diff] [blame] | 1786 | iommu_put_dma_cookie(domain); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1787 | arm_smmu_destroy_domain_context(domain); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1788 | kfree(smmu_domain); |
| 1789 | } |
| 1790 | |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1791 | static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx) |
| 1792 | { |
| 1793 | struct arm_smmu_smr *smr = smmu->smrs + idx; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 1794 | u32 reg = smr->id << SMR_ID_SHIFT | smr->mask << SMR_MASK_SHIFT; |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1795 | |
| 1796 | if (smr->valid) |
| 1797 | reg |= SMR_VALID; |
| 1798 | writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx)); |
| 1799 | } |
| 1800 | |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 1801 | static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx) |
| 1802 | { |
| 1803 | struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx; |
| 1804 | u32 reg = (s2cr->type & S2CR_TYPE_MASK) << S2CR_TYPE_SHIFT | |
| 1805 | (s2cr->cbndx & S2CR_CBNDX_MASK) << S2CR_CBNDX_SHIFT | |
| 1806 | (s2cr->privcfg & S2CR_PRIVCFG_MASK) << S2CR_PRIVCFG_SHIFT; |
| 1807 | |
| 1808 | writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx)); |
| 1809 | } |
| 1810 | |
| 1811 | static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx) |
| 1812 | { |
| 1813 | arm_smmu_write_s2cr(smmu, idx); |
| 1814 | if (smmu->smrs) |
| 1815 | arm_smmu_write_smr(smmu, idx); |
| 1816 | } |
| 1817 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1818 | static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask) |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1819 | { |
| 1820 | struct arm_smmu_smr *smrs = smmu->smrs; |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1821 | int i, free_idx = -ENOSPC; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1822 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1823 | /* Stream indexing is blissfully easy */ |
| 1824 | if (!smrs) |
| 1825 | return id; |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1826 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1827 | /* Validating SMRs is... less so */ |
| 1828 | for (i = 0; i < smmu->num_mapping_groups; ++i) { |
| 1829 | if (!smrs[i].valid) { |
| 1830 | /* |
| 1831 | * Note the first free entry we come across, which |
| 1832 | * we'll claim in the end if nothing else matches. |
| 1833 | */ |
| 1834 | if (free_idx < 0) |
| 1835 | free_idx = i; |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1836 | continue; |
| 1837 | } |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1838 | /* |
| 1839 | * If the new entry is _entirely_ matched by an existing entry, |
| 1840 | * then reuse that, with the guarantee that there also cannot |
| 1841 | * be any subsequent conflicting entries. In normal use we'd |
| 1842 | * expect simply identical entries for this case, but there's |
| 1843 | * no harm in accommodating the generalisation. |
| 1844 | */ |
| 1845 | if ((mask & smrs[i].mask) == mask && |
| 1846 | !((id ^ smrs[i].id) & ~smrs[i].mask)) |
| 1847 | return i; |
| 1848 | /* |
| 1849 | * If the new entry has any other overlap with an existing one, |
| 1850 | * though, then there always exists at least one stream ID |
| 1851 | * which would cause a conflict, and we can't allow that risk. |
| 1852 | */ |
| 1853 | if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask))) |
| 1854 | return -EINVAL; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1857 | return free_idx; |
| 1858 | } |
| 1859 | |
| 1860 | static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx) |
| 1861 | { |
| 1862 | if (--smmu->s2crs[idx].count) |
| 1863 | return false; |
| 1864 | |
| 1865 | smmu->s2crs[idx] = s2cr_init_val; |
| 1866 | if (smmu->smrs) |
| 1867 | smmu->smrs[idx].valid = false; |
| 1868 | |
| 1869 | return true; |
| 1870 | } |
| 1871 | |
| 1872 | static int arm_smmu_master_alloc_smes(struct device *dev) |
| 1873 | { |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1874 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1875 | struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv; |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1876 | struct arm_smmu_device *smmu = cfg->smmu; |
| 1877 | struct arm_smmu_smr *smrs = smmu->smrs; |
| 1878 | struct iommu_group *group; |
| 1879 | int i, idx, ret; |
| 1880 | |
| 1881 | mutex_lock(&smmu->stream_map_mutex); |
| 1882 | /* Figure out a viable stream map entry allocation */ |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1883 | for_each_cfg_sme(fwspec, i, idx) { |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 1884 | u16 sid = fwspec->ids[i]; |
| 1885 | u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT; |
| 1886 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1887 | if (idx != INVALID_SMENDX) { |
| 1888 | ret = -EEXIST; |
| 1889 | goto out_err; |
| 1890 | } |
| 1891 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 1892 | ret = arm_smmu_find_sme(smmu, sid, mask); |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1893 | if (ret < 0) |
| 1894 | goto out_err; |
| 1895 | |
| 1896 | idx = ret; |
| 1897 | if (smrs && smmu->s2crs[idx].count == 0) { |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 1898 | smrs[idx].id = sid; |
| 1899 | smrs[idx].mask = mask; |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1900 | smrs[idx].valid = true; |
| 1901 | } |
| 1902 | smmu->s2crs[idx].count++; |
| 1903 | cfg->smendx[i] = (s16)idx; |
| 1904 | } |
| 1905 | |
| 1906 | group = iommu_group_get_for_dev(dev); |
| 1907 | if (!group) |
| 1908 | group = ERR_PTR(-ENOMEM); |
| 1909 | if (IS_ERR(group)) { |
| 1910 | ret = PTR_ERR(group); |
| 1911 | goto out_err; |
| 1912 | } |
| 1913 | iommu_group_put(group); |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1914 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1915 | /* It worked! Now, poke the actual hardware */ |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1916 | for_each_cfg_sme(fwspec, i, idx) { |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1917 | arm_smmu_write_sme(smmu, idx); |
| 1918 | smmu->s2crs[idx].group = group; |
| 1919 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1920 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1921 | mutex_unlock(&smmu->stream_map_mutex); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1922 | return 0; |
| 1923 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1924 | out_err: |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1925 | while (i--) { |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1926 | arm_smmu_free_sme(smmu, cfg->smendx[i]); |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1927 | cfg->smendx[i] = INVALID_SMENDX; |
| 1928 | } |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1929 | mutex_unlock(&smmu->stream_map_mutex); |
| 1930 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1933 | static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1934 | { |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1935 | struct arm_smmu_device *smmu = fwspec_smmu(fwspec); |
| 1936 | struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv; |
Robin Murphy | 1fb519a | 2016-09-12 17:13:53 +0100 | [diff] [blame] | 1937 | int i, idx; |
Will Deacon | 43b412b | 2014-07-15 11:22:24 +0100 | [diff] [blame] | 1938 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1939 | mutex_lock(&smmu->stream_map_mutex); |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1940 | for_each_cfg_sme(fwspec, i, idx) { |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1941 | if (arm_smmu_free_sme(smmu, idx)) |
| 1942 | arm_smmu_write_sme(smmu, idx); |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 1943 | cfg->smendx[i] = INVALID_SMENDX; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1944 | } |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1945 | mutex_unlock(&smmu->stream_map_mutex); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1948 | static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain, |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1949 | struct iommu_fwspec *fwspec) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1950 | { |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 1951 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 1952 | struct arm_smmu_s2cr *s2cr = smmu->s2crs; |
| 1953 | enum arm_smmu_s2cr_type type = S2CR_TYPE_TRANS; |
| 1954 | u8 cbndx = smmu_domain->cfg.cbndx; |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1955 | int i, idx; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1956 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 1957 | for_each_cfg_sme(fwspec, i, idx) { |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 1958 | if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx) |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 1959 | continue; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 1960 | |
| 1961 | s2cr[idx].type = type; |
| 1962 | s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT; |
| 1963 | s2cr[idx].cbndx = cbndx; |
| 1964 | arm_smmu_write_s2cr(smmu, idx); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 1970 | static void arm_smmu_detach_dev(struct iommu_domain *domain, |
| 1971 | struct device *dev) |
Will Deacon | bc7f2ce | 2016-02-17 17:41:57 +0000 | [diff] [blame] | 1972 | { |
Will Deacon | bc7f2ce | 2016-02-17 17:41:57 +0000 | [diff] [blame] | 1973 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 1974 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 1975 | int dynamic = smmu_domain->attributes & (1 << DOMAIN_ATTR_DYNAMIC); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 1976 | int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC); |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 1977 | |
| 1978 | if (dynamic) |
| 1979 | return; |
| 1980 | |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 1981 | if (!smmu) { |
| 1982 | dev_err(dev, "Domain not attached; cannot detach!\n"); |
| 1983 | return; |
| 1984 | } |
Will Deacon | bc7f2ce | 2016-02-17 17:41:57 +0000 | [diff] [blame] | 1985 | |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 1986 | /* Remove additional vote for atomic power */ |
| 1987 | if (atomic_domain) { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 1988 | WARN_ON(arm_smmu_power_on_atomic(smmu->pwr)); |
| 1989 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 1990 | } |
Will Deacon | bc7f2ce | 2016-02-17 17:41:57 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1993 | static int arm_smmu_assign_table(struct arm_smmu_domain *smmu_domain) |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1994 | { |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 1995 | int ret = 0; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 1996 | int dest_vmids[2] = {VMID_HLOS, smmu_domain->secure_vmid}; |
| 1997 | int dest_perms[2] = {PERM_READ | PERM_WRITE, PERM_READ}; |
| 1998 | int source_vmid = VMID_HLOS; |
| 1999 | struct arm_smmu_pte_info *pte_info, *temp; |
| 2000 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2001 | if (!arm_smmu_is_domain_secure(smmu_domain)) |
| 2002 | return ret; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2003 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2004 | list_for_each_entry(pte_info, &smmu_domain->pte_info_list, entry) { |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2005 | ret = hyp_assign_phys(virt_to_phys(pte_info->virt_addr), |
| 2006 | PAGE_SIZE, &source_vmid, 1, |
| 2007 | dest_vmids, dest_perms, 2); |
| 2008 | if (WARN_ON(ret)) |
| 2009 | break; |
| 2010 | } |
| 2011 | |
| 2012 | list_for_each_entry_safe(pte_info, temp, &smmu_domain->pte_info_list, |
| 2013 | entry) { |
| 2014 | list_del(&pte_info->entry); |
| 2015 | kfree(pte_info); |
| 2016 | } |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2017 | return ret; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | static void arm_smmu_unassign_table(struct arm_smmu_domain *smmu_domain) |
| 2021 | { |
| 2022 | int ret; |
| 2023 | int dest_vmids = VMID_HLOS; |
Neeti Desai | 6100737 | 2015-07-28 11:02:02 -0700 | [diff] [blame] | 2024 | int dest_perms = PERM_READ | PERM_WRITE | PERM_EXEC; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2025 | int source_vmlist[2] = {VMID_HLOS, smmu_domain->secure_vmid}; |
| 2026 | struct arm_smmu_pte_info *pte_info, *temp; |
| 2027 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2028 | if (!arm_smmu_is_domain_secure(smmu_domain)) |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2029 | return; |
| 2030 | |
| 2031 | list_for_each_entry(pte_info, &smmu_domain->unassign_list, entry) { |
| 2032 | ret = hyp_assign_phys(virt_to_phys(pte_info->virt_addr), |
| 2033 | PAGE_SIZE, source_vmlist, 2, |
| 2034 | &dest_vmids, &dest_perms, 1); |
| 2035 | if (WARN_ON(ret)) |
| 2036 | break; |
| 2037 | free_pages_exact(pte_info->virt_addr, pte_info->size); |
| 2038 | } |
| 2039 | |
| 2040 | list_for_each_entry_safe(pte_info, temp, &smmu_domain->unassign_list, |
| 2041 | entry) { |
| 2042 | list_del(&pte_info->entry); |
| 2043 | kfree(pte_info); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | static void arm_smmu_unprepare_pgtable(void *cookie, void *addr, size_t size) |
| 2048 | { |
| 2049 | struct arm_smmu_domain *smmu_domain = cookie; |
| 2050 | struct arm_smmu_pte_info *pte_info; |
| 2051 | |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 2052 | BUG_ON(!arm_smmu_is_domain_secure(smmu_domain)); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2053 | |
| 2054 | pte_info = kzalloc(sizeof(struct arm_smmu_pte_info), GFP_ATOMIC); |
| 2055 | if (!pte_info) |
| 2056 | return; |
| 2057 | |
| 2058 | pte_info->virt_addr = addr; |
| 2059 | pte_info->size = size; |
| 2060 | list_add_tail(&pte_info->entry, &smmu_domain->unassign_list); |
| 2061 | } |
| 2062 | |
| 2063 | static int arm_smmu_prepare_pgtable(void *addr, void *cookie) |
| 2064 | { |
| 2065 | struct arm_smmu_domain *smmu_domain = cookie; |
| 2066 | struct arm_smmu_pte_info *pte_info; |
| 2067 | |
Patrick Daly | b7dfda7 | 2016-10-04 14:42:58 -0700 | [diff] [blame] | 2068 | BUG_ON(!arm_smmu_is_domain_secure(smmu_domain)); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2069 | |
| 2070 | pte_info = kzalloc(sizeof(struct arm_smmu_pte_info), GFP_ATOMIC); |
| 2071 | if (!pte_info) |
| 2072 | return -ENOMEM; |
| 2073 | pte_info->virt_addr = addr; |
| 2074 | list_add_tail(&pte_info->entry, &smmu_domain->pte_info_list); |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2078 | static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) |
| 2079 | { |
Mitchel Humpherys | a18037b | 2014-07-30 18:58:13 +0100 | [diff] [blame] | 2080 | int ret; |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2081 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2082 | struct arm_smmu_device *smmu; |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2083 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 2084 | int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2085 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2086 | if (!fwspec || fwspec->ops != &arm_smmu_ops) { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2087 | dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); |
| 2088 | return -ENXIO; |
| 2089 | } |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2090 | |
Robin Murphy | 4f79b14 | 2016-10-17 12:06:21 +0100 | [diff] [blame] | 2091 | /* |
| 2092 | * FIXME: The arch/arm DMA API code tries to attach devices to its own |
| 2093 | * domains between of_xlate() and add_device() - we have no way to cope |
| 2094 | * with that, so until ARM gets converted to rely on groups and default |
| 2095 | * domains, just say no (but more politely than by dereferencing NULL). |
| 2096 | * This should be at least a WARN_ON once that's sorted. |
| 2097 | */ |
| 2098 | if (!fwspec->iommu_priv) |
| 2099 | return -ENODEV; |
| 2100 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2101 | smmu = fwspec_smmu(fwspec); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2102 | |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2103 | /* Enable Clocks and Power */ |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2104 | ret = arm_smmu_power_on(smmu->pwr); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2105 | if (ret) |
| 2106 | return ret; |
| 2107 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2108 | /* Ensure that the domain is finalised */ |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2109 | ret = arm_smmu_init_domain_context(domain, smmu); |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 2110 | if (ret < 0) |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2111 | goto out_power_off; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2112 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 2113 | /* Do not modify the SIDs, HW is still running */ |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2114 | if (is_dynamic_domain(domain)) { |
| 2115 | ret = 0; |
| 2116 | goto out_power_off; |
| 2117 | } |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 2118 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2119 | /* |
Will Deacon | 44680ee | 2014-06-25 11:29:12 +0100 | [diff] [blame] | 2120 | * Sanity check the domain. We don't support domains across |
| 2121 | * different SMMUs. |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2122 | */ |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2123 | if (smmu_domain->smmu != smmu) { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2124 | dev_err(dev, |
| 2125 | "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n", |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2126 | dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev)); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2127 | ret = -EINVAL; |
| 2128 | goto out_power_off; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2129 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2130 | |
| 2131 | /* Looks ok, so add the device to the domain */ |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2132 | ret = arm_smmu_domain_add_master(smmu_domain, fwspec); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2133 | |
| 2134 | out_power_off: |
Patrick Daly | 466237d | 2016-10-14 15:59:14 -0700 | [diff] [blame] | 2135 | /* |
| 2136 | * Keep an additional vote for non-atomic power until domain is |
| 2137 | * detached |
| 2138 | */ |
| 2139 | if (!ret && atomic_domain) { |
| 2140 | WARN_ON(arm_smmu_power_on(smmu->pwr)); |
| 2141 | arm_smmu_power_off_atomic(smmu->pwr); |
| 2142 | } |
| 2143 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2144 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2145 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2146 | return ret; |
| 2147 | } |
| 2148 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2149 | static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova, |
Will Deacon | b410aed | 2014-02-20 16:31:06 +0000 | [diff] [blame] | 2150 | phys_addr_t paddr, size_t size, int prot) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2151 | { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2152 | int ret; |
| 2153 | unsigned long flags; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2154 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2155 | struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2156 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2157 | if (!ops) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2158 | return -ENODEV; |
| 2159 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2160 | arm_smmu_secure_domain_lock(smmu_domain); |
| 2161 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2162 | spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); |
| 2163 | ret = ops->map(ops, iova, paddr, size, prot); |
| 2164 | spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2165 | |
| 2166 | arm_smmu_assign_table(smmu_domain); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2167 | arm_smmu_secure_domain_unlock(smmu_domain); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2168 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2169 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, |
| 2173 | size_t size) |
| 2174 | { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2175 | size_t ret; |
| 2176 | unsigned long flags; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2177 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2178 | struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2179 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2180 | if (!ops) |
| 2181 | return 0; |
| 2182 | |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 2183 | ret = arm_smmu_domain_power_on(domain, smmu_domain->smmu); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2184 | if (ret) |
| 2185 | return ret; |
| 2186 | |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2187 | arm_smmu_secure_domain_lock(smmu_domain); |
| 2188 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2189 | spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); |
| 2190 | ret = ops->unmap(ops, iova, size); |
| 2191 | spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2192 | |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 2193 | arm_smmu_domain_power_off(domain, smmu_domain->smmu); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2194 | /* |
| 2195 | * While splitting up block mappings, we might allocate page table |
| 2196 | * memory during unmap, so the vmids needs to be assigned to the |
| 2197 | * memory here as well. |
| 2198 | */ |
| 2199 | arm_smmu_assign_table(smmu_domain); |
| 2200 | /* Also unassign any pages that were free'd during unmap */ |
| 2201 | arm_smmu_unassign_table(smmu_domain); |
Patrick Daly | e271f21 | 2016-10-04 13:24:49 -0700 | [diff] [blame] | 2202 | arm_smmu_secure_domain_unlock(smmu_domain); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2203 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Mitchel Humpherys | 622bc04 | 2015-04-23 16:29:23 -0700 | [diff] [blame] | 2206 | static size_t arm_smmu_map_sg(struct iommu_domain *domain, unsigned long iova, |
| 2207 | struct scatterlist *sg, unsigned int nents, int prot) |
| 2208 | { |
| 2209 | int ret; |
Rohit Vaswani | 4d7cdd9 | 2015-08-18 17:57:44 -0700 | [diff] [blame] | 2210 | size_t size; |
Mitchel Humpherys | 622bc04 | 2015-04-23 16:29:23 -0700 | [diff] [blame] | 2211 | unsigned long flags; |
| 2212 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2213 | struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops; |
| 2214 | |
| 2215 | if (!ops) |
| 2216 | return -ENODEV; |
| 2217 | |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 2218 | ret = arm_smmu_domain_power_on(domain, smmu_domain->smmu); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2219 | if (ret) |
| 2220 | return ret; |
| 2221 | |
Mitchel Humpherys | 622bc04 | 2015-04-23 16:29:23 -0700 | [diff] [blame] | 2222 | spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); |
Rohit Vaswani | 4d7cdd9 | 2015-08-18 17:57:44 -0700 | [diff] [blame] | 2223 | ret = ops->map_sg(ops, iova, sg, nents, prot, &size); |
Mitchel Humpherys | 622bc04 | 2015-04-23 16:29:23 -0700 | [diff] [blame] | 2224 | spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); |
Rohit Vaswani | 4d7cdd9 | 2015-08-18 17:57:44 -0700 | [diff] [blame] | 2225 | |
| 2226 | if (!ret) |
| 2227 | arm_smmu_unmap(domain, iova, size); |
| 2228 | |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 2229 | arm_smmu_domain_power_off(domain, smmu_domain->smmu); |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2230 | arm_smmu_assign_table(smmu_domain); |
| 2231 | |
Mitchel Humpherys | 622bc04 | 2015-04-23 16:29:23 -0700 | [diff] [blame] | 2232 | return ret; |
| 2233 | } |
| 2234 | |
Mitchel Humpherys | fb11f70 | 2015-07-06 13:53:51 -0700 | [diff] [blame] | 2235 | static phys_addr_t __arm_smmu_iova_to_phys_hard(struct iommu_domain *domain, |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2236 | dma_addr_t iova) |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2237 | { |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2238 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2239 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
| 2240 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 2241 | struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; |
| 2242 | struct device *dev = smmu->dev; |
| 2243 | void __iomem *cb_base; |
| 2244 | u32 tmp; |
| 2245 | u64 phys; |
Robin Murphy | 661d962 | 2015-05-27 17:09:34 +0100 | [diff] [blame] | 2246 | unsigned long va; |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2247 | |
| 2248 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 2249 | |
Robin Murphy | 661d962 | 2015-05-27 17:09:34 +0100 | [diff] [blame] | 2250 | /* ATS1 registers can only be written atomically */ |
| 2251 | va = iova & ~0xfffUL; |
Robin Murphy | 661d962 | 2015-05-27 17:09:34 +0100 | [diff] [blame] | 2252 | if (smmu->version == ARM_SMMU_V2) |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 2253 | smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR); |
| 2254 | else /* Register is only 32-bit in v1 */ |
Robin Murphy | 661d962 | 2015-05-27 17:09:34 +0100 | [diff] [blame] | 2255 | writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR); |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2256 | |
| 2257 | if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp, |
| 2258 | !(tmp & ATSR_ACTIVE), 5, 50)) { |
Mitchel Humpherys | 0ed5da6 | 2014-12-04 11:47:49 -0800 | [diff] [blame] | 2259 | phys = ops->iova_to_phys(ops, iova); |
Mitchel Humpherys | d7e0971 | 2015-02-04 21:30:58 -0800 | [diff] [blame] | 2260 | dev_err(dev, |
| 2261 | "iova to phys timed out on %pad. software table walk result=%pa.\n", |
| 2262 | &iova, &phys); |
| 2263 | phys = 0; |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2264 | return phys; |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Robin Murphy | f9a05f0 | 2016-04-13 18:13:01 +0100 | [diff] [blame] | 2267 | phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR); |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2268 | if (phys & CB_PAR_F) { |
| 2269 | dev_err(dev, "translation fault!\n"); |
| 2270 | dev_err(dev, "PAR = 0x%llx\n", phys); |
Mitchel Humpherys | 0ed5da6 | 2014-12-04 11:47:49 -0800 | [diff] [blame] | 2271 | phys = 0; |
| 2272 | } else { |
| 2273 | phys = (phys & (PHYS_MASK & ~0xfffULL)) | (iova & 0xfff); |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2274 | } |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2275 | |
Mitchel Humpherys | 0ed5da6 | 2014-12-04 11:47:49 -0800 | [diff] [blame] | 2276 | return phys; |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2279 | static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain, |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2280 | dma_addr_t iova) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2281 | { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2282 | phys_addr_t ret; |
| 2283 | unsigned long flags; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2284 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2285 | struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2286 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2287 | if (!ops) |
Will Deacon | a44a979 | 2013-11-07 18:47:50 +0000 | [diff] [blame] | 2288 | return 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2289 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2290 | spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); |
Patrick Daly | a85a2fb | 2016-06-21 19:23:06 -0700 | [diff] [blame] | 2291 | ret = ops->iova_to_phys(ops, iova); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2292 | spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 2293 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2294 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2295 | } |
| 2296 | |
Mitchel Humpherys | fb11f70 | 2015-07-06 13:53:51 -0700 | [diff] [blame] | 2297 | /* |
| 2298 | * This function can sleep, and cannot be called from atomic context. Will |
| 2299 | * power on register block if required. This restriction does not apply to the |
| 2300 | * original iova_to_phys() op. |
| 2301 | */ |
| 2302 | static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain, |
| 2303 | dma_addr_t iova) |
| 2304 | { |
| 2305 | phys_addr_t ret = 0; |
| 2306 | unsigned long flags; |
| 2307 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2308 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2309 | if (smmu_domain->smmu->arch_ops && |
| 2310 | smmu_domain->smmu->arch_ops->iova_to_phys_hard) |
| 2311 | return smmu_domain->smmu->arch_ops->iova_to_phys_hard( |
| 2312 | domain, iova); |
Mitchel Humpherys | fb11f70 | 2015-07-06 13:53:51 -0700 | [diff] [blame] | 2313 | |
| 2314 | spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); |
| 2315 | if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS && |
| 2316 | smmu_domain->stage == ARM_SMMU_DOMAIN_S1) |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2317 | ret = __arm_smmu_iova_to_phys_hard(domain, iova); |
Mitchel Humpherys | fb11f70 | 2015-07-06 13:53:51 -0700 | [diff] [blame] | 2318 | |
| 2319 | spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); |
| 2320 | |
| 2321 | return ret; |
| 2322 | } |
| 2323 | |
Joerg Roedel | 1fd0c77 | 2014-09-05 10:49:34 +0200 | [diff] [blame] | 2324 | static bool arm_smmu_capable(enum iommu_cap cap) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2325 | { |
Will Deacon | d094894 | 2014-06-24 17:30:10 +0100 | [diff] [blame] | 2326 | switch (cap) { |
| 2327 | case IOMMU_CAP_CACHE_COHERENCY: |
Joerg Roedel | 1fd0c77 | 2014-09-05 10:49:34 +0200 | [diff] [blame] | 2328 | /* |
| 2329 | * Return true here as the SMMU can always send out coherent |
| 2330 | * requests. |
| 2331 | */ |
| 2332 | return true; |
Will Deacon | d094894 | 2014-06-24 17:30:10 +0100 | [diff] [blame] | 2333 | case IOMMU_CAP_INTR_REMAP: |
Joerg Roedel | 1fd0c77 | 2014-09-05 10:49:34 +0200 | [diff] [blame] | 2334 | return true; /* MSIs are just memory writes */ |
Antonios Motakis | 0029a8d | 2014-10-13 14:06:18 +0100 | [diff] [blame] | 2335 | case IOMMU_CAP_NOEXEC: |
| 2336 | return true; |
Will Deacon | d094894 | 2014-06-24 17:30:10 +0100 | [diff] [blame] | 2337 | default: |
Joerg Roedel | 1fd0c77 | 2014-09-05 10:49:34 +0200 | [diff] [blame] | 2338 | return false; |
Will Deacon | d094894 | 2014-06-24 17:30:10 +0100 | [diff] [blame] | 2339 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2340 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2341 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2342 | static int arm_smmu_match_node(struct device *dev, void *data) |
| 2343 | { |
| 2344 | return dev->of_node == data; |
| 2345 | } |
| 2346 | |
| 2347 | static struct arm_smmu_device *arm_smmu_get_by_node(struct device_node *np) |
| 2348 | { |
| 2349 | struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL, |
| 2350 | np, arm_smmu_match_node); |
| 2351 | put_device(dev); |
| 2352 | return dev ? dev_get_drvdata(dev) : NULL; |
| 2353 | } |
| 2354 | |
Will Deacon | 03edb22 | 2015-01-19 14:27:33 +0000 | [diff] [blame] | 2355 | static int arm_smmu_add_device(struct device *dev) |
| 2356 | { |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2357 | struct arm_smmu_device *smmu; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2358 | struct arm_smmu_master_cfg *cfg; |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2359 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2360 | int i, ret; |
| 2361 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2362 | if (using_legacy_binding) { |
| 2363 | ret = arm_smmu_register_legacy_master(dev, &smmu); |
| 2364 | fwspec = dev->iommu_fwspec; |
| 2365 | if (ret) |
| 2366 | goto out_free; |
Robin Murphy | 22e6f6c | 2016-11-02 17:31:32 +0000 | [diff] [blame^] | 2367 | } else if (fwspec && fwspec->ops == &arm_smmu_ops) { |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2368 | smmu = arm_smmu_get_by_node(to_of_node(fwspec->iommu_fwnode)); |
| 2369 | if (!smmu) |
| 2370 | return -ENODEV; |
| 2371 | } else { |
| 2372 | return -ENODEV; |
| 2373 | } |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2374 | |
| 2375 | ret = -EINVAL; |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2376 | for (i = 0; i < fwspec->num_ids; i++) { |
| 2377 | u16 sid = fwspec->ids[i]; |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2378 | u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2379 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2380 | if (sid & ~smmu->streamid_mask) { |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2381 | dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n", |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2382 | sid, smmu->streamid_mask); |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2383 | goto out_free; |
| 2384 | } |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2385 | if (mask & ~smmu->smr_mask_mask) { |
| 2386 | dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n", |
| 2387 | sid, smmu->smr_mask_mask); |
| 2388 | goto out_free; |
| 2389 | } |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2390 | } |
Will Deacon | 03edb22 | 2015-01-19 14:27:33 +0000 | [diff] [blame] | 2391 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2392 | ret = -ENOMEM; |
| 2393 | cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]), |
| 2394 | GFP_KERNEL); |
| 2395 | if (!cfg) |
| 2396 | goto out_free; |
| 2397 | |
| 2398 | cfg->smmu = smmu; |
| 2399 | fwspec->iommu_priv = cfg; |
| 2400 | while (i--) |
| 2401 | cfg->smendx[i] = INVALID_SMENDX; |
| 2402 | |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 2403 | ret = arm_smmu_master_alloc_smes(dev); |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2404 | if (ret) |
| 2405 | goto out_free; |
| 2406 | |
| 2407 | return 0; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2408 | |
| 2409 | out_free: |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2410 | if (fwspec) |
| 2411 | kfree(fwspec->iommu_priv); |
| 2412 | iommu_fwspec_free(dev); |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2413 | return ret; |
Will Deacon | 03edb22 | 2015-01-19 14:27:33 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2416 | static void arm_smmu_remove_device(struct device *dev) |
| 2417 | { |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2418 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 2419 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2420 | if (!fwspec || fwspec->ops != &arm_smmu_ops) |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 2421 | return; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 2422 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2423 | arm_smmu_master_free_smes(fwspec); |
Antonios Motakis | 5fc63a7 | 2013-10-18 16:08:29 +0100 | [diff] [blame] | 2424 | iommu_group_remove_device(dev); |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2425 | kfree(fwspec->iommu_priv); |
| 2426 | iommu_fwspec_free(dev); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2427 | } |
| 2428 | |
Joerg Roedel | af65993 | 2015-10-21 23:51:41 +0200 | [diff] [blame] | 2429 | static struct iommu_group *arm_smmu_device_group(struct device *dev) |
| 2430 | { |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2431 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 2432 | struct arm_smmu_device *smmu = fwspec_smmu(fwspec); |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 2433 | struct iommu_group *group = NULL; |
| 2434 | int i, idx; |
| 2435 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 2436 | for_each_cfg_sme(fwspec, i, idx) { |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 2437 | if (group && smmu->s2crs[idx].group && |
| 2438 | group != smmu->s2crs[idx].group) |
| 2439 | return ERR_PTR(-EINVAL); |
| 2440 | |
| 2441 | group = smmu->s2crs[idx].group; |
| 2442 | } |
| 2443 | |
| 2444 | if (group) |
| 2445 | return group; |
Joerg Roedel | af65993 | 2015-10-21 23:51:41 +0200 | [diff] [blame] | 2446 | |
| 2447 | if (dev_is_pci(dev)) |
| 2448 | group = pci_device_group(dev); |
| 2449 | else |
| 2450 | group = generic_device_group(dev); |
| 2451 | |
Joerg Roedel | af65993 | 2015-10-21 23:51:41 +0200 | [diff] [blame] | 2452 | return group; |
| 2453 | } |
| 2454 | |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2455 | static int arm_smmu_domain_get_attr(struct iommu_domain *domain, |
| 2456 | enum iommu_attr attr, void *data) |
| 2457 | { |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2458 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Jeremy Gebben | 7e47f9b | 2015-06-16 10:59:29 -0600 | [diff] [blame] | 2459 | int ret = 0; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2460 | |
| 2461 | switch (attr) { |
| 2462 | case DOMAIN_ATTR_NESTING: |
| 2463 | *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED); |
| 2464 | return 0; |
Mitchel Humpherys | cd9f07a | 2014-11-12 15:11:33 -0800 | [diff] [blame] | 2465 | case DOMAIN_ATTR_PT_BASE_ADDR: |
| 2466 | *((phys_addr_t *)data) = |
| 2467 | smmu_domain->pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0]; |
| 2468 | return 0; |
Jeremy Gebben | 7e47f9b | 2015-06-16 10:59:29 -0600 | [diff] [blame] | 2469 | case DOMAIN_ATTR_CONTEXT_BANK: |
| 2470 | /* context bank index isn't valid until we are attached */ |
| 2471 | if (smmu_domain->smmu == NULL) |
| 2472 | return -ENODEV; |
| 2473 | |
| 2474 | *((unsigned int *) data) = smmu_domain->cfg.cbndx; |
| 2475 | ret = 0; |
| 2476 | break; |
Jeremy Gebben | 8ac927c | 2015-07-10 16:43:22 -0600 | [diff] [blame] | 2477 | case DOMAIN_ATTR_TTBR0: { |
| 2478 | u64 val; |
| 2479 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
| 2480 | /* not valid until we are attached */ |
| 2481 | if (smmu == NULL) |
| 2482 | return -ENODEV; |
| 2483 | |
| 2484 | val = smmu_domain->pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0]; |
| 2485 | if (smmu_domain->cfg.cbar != CBAR_TYPE_S2_TRANS) |
| 2486 | val |= (u64)ARM_SMMU_CB_ASID(smmu, &smmu_domain->cfg) |
| 2487 | << (TTBRn_ASID_SHIFT); |
| 2488 | *((u64 *)data) = val; |
| 2489 | ret = 0; |
| 2490 | break; |
| 2491 | } |
| 2492 | case DOMAIN_ATTR_CONTEXTIDR: |
| 2493 | /* not valid until attached */ |
| 2494 | if (smmu_domain->smmu == NULL) |
| 2495 | return -ENODEV; |
| 2496 | *((u32 *)data) = smmu_domain->cfg.procid; |
| 2497 | ret = 0; |
| 2498 | break; |
| 2499 | case DOMAIN_ATTR_PROCID: |
| 2500 | *((u32 *)data) = smmu_domain->cfg.procid; |
| 2501 | ret = 0; |
| 2502 | break; |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 2503 | case DOMAIN_ATTR_DYNAMIC: |
| 2504 | *((int *)data) = !!(smmu_domain->attributes |
| 2505 | & (1 << DOMAIN_ATTR_DYNAMIC)); |
| 2506 | ret = 0; |
| 2507 | break; |
Mitchel Humpherys | cc8d12f | 2015-09-25 17:29:27 -0700 | [diff] [blame] | 2508 | case DOMAIN_ATTR_NON_FATAL_FAULTS: |
| 2509 | *((int *)data) = !!(smmu_domain->attributes |
| 2510 | & (1 << DOMAIN_ATTR_NON_FATAL_FAULTS)); |
| 2511 | ret = 0; |
| 2512 | break; |
Patrick Daly | e62d336 | 2016-03-15 18:58:28 -0700 | [diff] [blame] | 2513 | case DOMAIN_ATTR_S1_BYPASS: |
| 2514 | *((int *)data) = !!(smmu_domain->attributes |
| 2515 | & (1 << DOMAIN_ATTR_S1_BYPASS)); |
| 2516 | ret = 0; |
| 2517 | break; |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2518 | case DOMAIN_ATTR_SECURE_VMID: |
| 2519 | *((int *)data) = smmu_domain->secure_vmid; |
| 2520 | ret = 0; |
| 2521 | break; |
Mitchel Humpherys | b9dda59 | 2016-02-12 14:18:02 -0800 | [diff] [blame] | 2522 | case DOMAIN_ATTR_PGTBL_INFO: { |
| 2523 | struct iommu_pgtbl_info *info = data; |
| 2524 | |
| 2525 | if (!(smmu_domain->attributes & (1 << DOMAIN_ATTR_FAST))) { |
| 2526 | ret = -ENODEV; |
| 2527 | break; |
| 2528 | } |
| 2529 | info->pmds = smmu_domain->pgtbl_cfg.av8l_fast_cfg.pmds; |
| 2530 | ret = 0; |
| 2531 | break; |
| 2532 | } |
Mitchel Humpherys | c625ce0 | 2015-10-07 14:03:50 -0700 | [diff] [blame] | 2533 | case DOMAIN_ATTR_FAST: |
| 2534 | *((int *)data) = !!(smmu_domain->attributes |
| 2535 | & (1 << DOMAIN_ATTR_FAST)); |
| 2536 | ret = 0; |
| 2537 | break; |
Patrick Daly | ce6786f | 2016-11-09 14:19:23 -0800 | [diff] [blame] | 2538 | case DOMAIN_ATTR_USE_UPSTREAM_HINT: |
| 2539 | *((int *)data) = !!(smmu_domain->attributes & |
| 2540 | (1 << DOMAIN_ATTR_USE_UPSTREAM_HINT)); |
| 2541 | ret = 0; |
| 2542 | break; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2543 | default: |
| 2544 | return -ENODEV; |
| 2545 | } |
Jeremy Gebben | 7e47f9b | 2015-06-16 10:59:29 -0600 | [diff] [blame] | 2546 | return ret; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | static int arm_smmu_domain_set_attr(struct iommu_domain *domain, |
| 2550 | enum iommu_attr attr, void *data) |
| 2551 | { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2552 | int ret = 0; |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2553 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2554 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2555 | mutex_lock(&smmu_domain->init_mutex); |
| 2556 | |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2557 | switch (attr) { |
| 2558 | case DOMAIN_ATTR_NESTING: |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2559 | if (smmu_domain->smmu) { |
| 2560 | ret = -EPERM; |
| 2561 | goto out_unlock; |
| 2562 | } |
| 2563 | |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2564 | if (*(int *)data) |
| 2565 | smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; |
| 2566 | else |
| 2567 | smmu_domain->stage = ARM_SMMU_DOMAIN_S1; |
| 2568 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2569 | break; |
Jeremy Gebben | 8ac927c | 2015-07-10 16:43:22 -0600 | [diff] [blame] | 2570 | case DOMAIN_ATTR_PROCID: |
| 2571 | if (smmu_domain->smmu != NULL) { |
| 2572 | dev_err(smmu_domain->smmu->dev, |
| 2573 | "cannot change procid attribute while attached\n"); |
| 2574 | ret = -EBUSY; |
| 2575 | break; |
| 2576 | } |
| 2577 | smmu_domain->cfg.procid = *((u32 *)data); |
| 2578 | ret = 0; |
| 2579 | break; |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 2580 | case DOMAIN_ATTR_DYNAMIC: { |
| 2581 | int dynamic = *((int *)data); |
| 2582 | |
| 2583 | if (smmu_domain->smmu != NULL) { |
| 2584 | dev_err(smmu_domain->smmu->dev, |
| 2585 | "cannot change dynamic attribute while attached\n"); |
| 2586 | ret = -EBUSY; |
| 2587 | break; |
| 2588 | } |
| 2589 | |
| 2590 | if (dynamic) |
| 2591 | smmu_domain->attributes |= 1 << DOMAIN_ATTR_DYNAMIC; |
| 2592 | else |
| 2593 | smmu_domain->attributes &= ~(1 << DOMAIN_ATTR_DYNAMIC); |
| 2594 | ret = 0; |
| 2595 | break; |
| 2596 | } |
| 2597 | case DOMAIN_ATTR_CONTEXT_BANK: |
| 2598 | /* context bank can't be set while attached */ |
| 2599 | if (smmu_domain->smmu != NULL) { |
| 2600 | ret = -EBUSY; |
| 2601 | break; |
| 2602 | } |
| 2603 | /* ... and it can only be set for dynamic contexts. */ |
| 2604 | if (!(smmu_domain->attributes & (1 << DOMAIN_ATTR_DYNAMIC))) { |
| 2605 | ret = -EINVAL; |
| 2606 | break; |
| 2607 | } |
| 2608 | |
| 2609 | /* this will be validated during attach */ |
| 2610 | smmu_domain->cfg.cbndx = *((unsigned int *)data); |
| 2611 | ret = 0; |
| 2612 | break; |
Mitchel Humpherys | cc8d12f | 2015-09-25 17:29:27 -0700 | [diff] [blame] | 2613 | case DOMAIN_ATTR_NON_FATAL_FAULTS: { |
| 2614 | u32 non_fatal_faults = *((int *)data); |
| 2615 | |
| 2616 | if (non_fatal_faults) |
| 2617 | smmu_domain->attributes |= |
| 2618 | 1 << DOMAIN_ATTR_NON_FATAL_FAULTS; |
| 2619 | else |
| 2620 | smmu_domain->attributes &= |
| 2621 | ~(1 << DOMAIN_ATTR_NON_FATAL_FAULTS); |
| 2622 | ret = 0; |
| 2623 | break; |
| 2624 | } |
Patrick Daly | e62d336 | 2016-03-15 18:58:28 -0700 | [diff] [blame] | 2625 | case DOMAIN_ATTR_S1_BYPASS: { |
| 2626 | int bypass = *((int *)data); |
| 2627 | |
| 2628 | /* bypass can't be changed while attached */ |
| 2629 | if (smmu_domain->smmu != NULL) { |
| 2630 | ret = -EBUSY; |
| 2631 | break; |
| 2632 | } |
| 2633 | if (bypass) |
| 2634 | smmu_domain->attributes |= 1 << DOMAIN_ATTR_S1_BYPASS; |
| 2635 | else |
| 2636 | smmu_domain->attributes &= |
| 2637 | ~(1 << DOMAIN_ATTR_S1_BYPASS); |
| 2638 | |
| 2639 | ret = 0; |
| 2640 | break; |
| 2641 | } |
Patrick Daly | 8befb66 | 2016-08-17 20:03:28 -0700 | [diff] [blame] | 2642 | case DOMAIN_ATTR_ATOMIC: |
| 2643 | { |
| 2644 | int atomic_ctx = *((int *)data); |
| 2645 | |
| 2646 | /* can't be changed while attached */ |
| 2647 | if (smmu_domain->smmu != NULL) { |
| 2648 | ret = -EBUSY; |
| 2649 | break; |
| 2650 | } |
| 2651 | if (atomic_ctx) |
| 2652 | smmu_domain->attributes |= (1 << DOMAIN_ATTR_ATOMIC); |
| 2653 | else |
| 2654 | smmu_domain->attributes &= ~(1 << DOMAIN_ATTR_ATOMIC); |
| 2655 | break; |
| 2656 | } |
Patrick Daly | c11d108 | 2016-09-01 15:52:44 -0700 | [diff] [blame] | 2657 | case DOMAIN_ATTR_SECURE_VMID: |
| 2658 | if (smmu_domain->secure_vmid != VMID_INVAL) { |
| 2659 | ret = -ENODEV; |
| 2660 | WARN(1, "secure vmid already set!"); |
| 2661 | break; |
| 2662 | } |
| 2663 | smmu_domain->secure_vmid = *((int *)data); |
| 2664 | break; |
Mitchel Humpherys | c625ce0 | 2015-10-07 14:03:50 -0700 | [diff] [blame] | 2665 | case DOMAIN_ATTR_FAST: |
| 2666 | if (*((int *)data)) |
| 2667 | smmu_domain->attributes |= 1 << DOMAIN_ATTR_FAST; |
| 2668 | ret = 0; |
| 2669 | break; |
Patrick Daly | ce6786f | 2016-11-09 14:19:23 -0800 | [diff] [blame] | 2670 | case DOMAIN_ATTR_USE_UPSTREAM_HINT: |
| 2671 | /* can't be changed while attached */ |
| 2672 | if (smmu_domain->smmu != NULL) { |
| 2673 | ret = -EBUSY; |
| 2674 | break; |
| 2675 | } |
| 2676 | if (*((int *)data)) |
| 2677 | smmu_domain->attributes |= |
| 2678 | 1 << DOMAIN_ATTR_USE_UPSTREAM_HINT; |
| 2679 | ret = 0; |
| 2680 | break; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2681 | default: |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2682 | ret = -ENODEV; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2683 | } |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2684 | |
| 2685 | out_unlock: |
| 2686 | mutex_unlock(&smmu_domain->init_mutex); |
| 2687 | return ret; |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2688 | } |
| 2689 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2690 | static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args) |
| 2691 | { |
| 2692 | u32 fwid = 0; |
| 2693 | |
| 2694 | if (args->args_count > 0) |
| 2695 | fwid |= (u16)args->args[0]; |
| 2696 | |
| 2697 | if (args->args_count > 1) |
| 2698 | fwid |= (u16)args->args[1] << SMR_MASK_SHIFT; |
| 2699 | |
| 2700 | return iommu_fwspec_add_ids(dev, &fwid, 1); |
| 2701 | } |
| 2702 | |
Mitchel Humpherys | a52d6cc | 2015-07-09 17:26:15 -0700 | [diff] [blame] | 2703 | static void arm_smmu_trigger_fault(struct iommu_domain *domain, |
| 2704 | unsigned long flags) |
| 2705 | { |
| 2706 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2707 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 2708 | struct arm_smmu_device *smmu; |
| 2709 | void __iomem *cb_base; |
| 2710 | |
| 2711 | if (!smmu_domain->smmu) { |
| 2712 | pr_err("Can't trigger faults on non-attached domains\n"); |
| 2713 | return; |
| 2714 | } |
| 2715 | |
| 2716 | smmu = smmu_domain->smmu; |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2717 | if (arm_smmu_power_on(smmu->pwr)) |
Mitchel Humpherys | 3e52a7e | 2015-10-19 17:13:47 -0700 | [diff] [blame] | 2718 | return; |
Mitchel Humpherys | a52d6cc | 2015-07-09 17:26:15 -0700 | [diff] [blame] | 2719 | |
| 2720 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 2721 | dev_err(smmu->dev, "Writing 0x%lx to FSRRESTORE on cb %d\n", |
| 2722 | flags, cfg->cbndx); |
| 2723 | writel_relaxed(flags, cb_base + ARM_SMMU_CB_FSRRESTORE); |
Mitchel Humpherys | 017ee4b | 2015-10-21 13:59:50 -0700 | [diff] [blame] | 2724 | /* give the interrupt time to fire... */ |
| 2725 | msleep(1000); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2726 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2727 | arm_smmu_power_off(smmu->pwr); |
Mitchel Humpherys | a52d6cc | 2015-07-09 17:26:15 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Mitchel Humpherys | fd55700 | 2015-08-21 14:07:59 -0700 | [diff] [blame] | 2730 | static unsigned long arm_smmu_reg_read(struct iommu_domain *domain, |
| 2731 | unsigned long offset) |
| 2732 | { |
| 2733 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2734 | struct arm_smmu_device *smmu; |
| 2735 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 2736 | void __iomem *cb_base; |
| 2737 | unsigned long val; |
| 2738 | |
| 2739 | if (offset >= SZ_4K) { |
| 2740 | pr_err("Invalid offset: 0x%lx\n", offset); |
| 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | smmu = smmu_domain->smmu; |
| 2745 | if (!smmu) { |
| 2746 | WARN(1, "Can't read registers of a detached domain\n"); |
| 2747 | val = 0; |
| 2748 | return val; |
| 2749 | } |
| 2750 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2751 | if (arm_smmu_power_on(smmu->pwr)) |
Mitchel Humpherys | 3e52a7e | 2015-10-19 17:13:47 -0700 | [diff] [blame] | 2752 | return 0; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2753 | |
Mitchel Humpherys | fd55700 | 2015-08-21 14:07:59 -0700 | [diff] [blame] | 2754 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 2755 | val = readl_relaxed(cb_base + offset); |
| 2756 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2757 | arm_smmu_power_off(smmu->pwr); |
Mitchel Humpherys | fd55700 | 2015-08-21 14:07:59 -0700 | [diff] [blame] | 2758 | return val; |
| 2759 | } |
| 2760 | |
| 2761 | static void arm_smmu_reg_write(struct iommu_domain *domain, |
| 2762 | unsigned long offset, unsigned long val) |
| 2763 | { |
| 2764 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2765 | struct arm_smmu_device *smmu; |
| 2766 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 2767 | void __iomem *cb_base; |
| 2768 | |
| 2769 | if (offset >= SZ_4K) { |
| 2770 | pr_err("Invalid offset: 0x%lx\n", offset); |
| 2771 | return; |
| 2772 | } |
| 2773 | |
| 2774 | smmu = smmu_domain->smmu; |
| 2775 | if (!smmu) { |
| 2776 | WARN(1, "Can't read registers of a detached domain\n"); |
| 2777 | return; |
| 2778 | } |
| 2779 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2780 | if (arm_smmu_power_on(smmu->pwr)) |
Mitchel Humpherys | 3e52a7e | 2015-10-19 17:13:47 -0700 | [diff] [blame] | 2781 | return; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2782 | |
Mitchel Humpherys | fd55700 | 2015-08-21 14:07:59 -0700 | [diff] [blame] | 2783 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 2784 | writel_relaxed(val, cb_base + offset); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 2785 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2786 | arm_smmu_power_off(smmu->pwr); |
Mitchel Humpherys | fd55700 | 2015-08-21 14:07:59 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
Mitchel Humpherys | 2dcc234 | 2015-12-03 11:20:03 -0800 | [diff] [blame] | 2789 | static void arm_smmu_tlbi_domain(struct iommu_domain *domain) |
| 2790 | { |
| 2791 | arm_smmu_tlb_inv_context(to_smmu_domain(domain)); |
| 2792 | } |
| 2793 | |
Mitchel Humpherys | 74299ca | 2015-12-14 16:12:00 -0800 | [diff] [blame] | 2794 | static int arm_smmu_enable_config_clocks(struct iommu_domain *domain) |
| 2795 | { |
| 2796 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2797 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2798 | return arm_smmu_power_on(smmu_domain->smmu->pwr); |
Mitchel Humpherys | 74299ca | 2015-12-14 16:12:00 -0800 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | static void arm_smmu_disable_config_clocks(struct iommu_domain *domain) |
| 2802 | { |
| 2803 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2804 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2805 | arm_smmu_power_off(smmu_domain->smmu->pwr); |
Mitchel Humpherys | 74299ca | 2015-12-14 16:12:00 -0800 | [diff] [blame] | 2806 | } |
| 2807 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2808 | static struct iommu_ops arm_smmu_ops = { |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2809 | .capable = arm_smmu_capable, |
Joerg Roedel | 1d67263 | 2015-03-26 13:43:10 +0100 | [diff] [blame] | 2810 | .domain_alloc = arm_smmu_domain_alloc, |
| 2811 | .domain_free = arm_smmu_domain_free, |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2812 | .attach_dev = arm_smmu_attach_dev, |
Patrick Daly | 0980131 | 2016-08-29 17:02:52 -0700 | [diff] [blame] | 2813 | .detach_dev = arm_smmu_detach_dev, |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2814 | .map = arm_smmu_map, |
| 2815 | .unmap = arm_smmu_unmap, |
Mitchel Humpherys | 622bc04 | 2015-04-23 16:29:23 -0700 | [diff] [blame] | 2816 | .map_sg = arm_smmu_map_sg, |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2817 | .iova_to_phys = arm_smmu_iova_to_phys, |
Mitchel Humpherys | fb11f70 | 2015-07-06 13:53:51 -0700 | [diff] [blame] | 2818 | .iova_to_phys_hard = arm_smmu_iova_to_phys_hard, |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2819 | .add_device = arm_smmu_add_device, |
| 2820 | .remove_device = arm_smmu_remove_device, |
Joerg Roedel | af65993 | 2015-10-21 23:51:41 +0200 | [diff] [blame] | 2821 | .device_group = arm_smmu_device_group, |
Will Deacon | c752ce4 | 2014-06-25 22:46:31 +0100 | [diff] [blame] | 2822 | .domain_get_attr = arm_smmu_domain_get_attr, |
| 2823 | .domain_set_attr = arm_smmu_domain_set_attr, |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 2824 | .of_xlate = arm_smmu_of_xlate, |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 2825 | .pgsize_bitmap = -1UL, /* Restricted during device attach */ |
Mitchel Humpherys | a52d6cc | 2015-07-09 17:26:15 -0700 | [diff] [blame] | 2826 | .trigger_fault = arm_smmu_trigger_fault, |
Mitchel Humpherys | fd55700 | 2015-08-21 14:07:59 -0700 | [diff] [blame] | 2827 | .reg_read = arm_smmu_reg_read, |
| 2828 | .reg_write = arm_smmu_reg_write, |
Mitchel Humpherys | 2dcc234 | 2015-12-03 11:20:03 -0800 | [diff] [blame] | 2829 | .tlbi_domain = arm_smmu_tlbi_domain, |
Mitchel Humpherys | 74299ca | 2015-12-14 16:12:00 -0800 | [diff] [blame] | 2830 | .enable_config_clocks = arm_smmu_enable_config_clocks, |
| 2831 | .disable_config_clocks = arm_smmu_disable_config_clocks, |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 2832 | }; |
| 2833 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2834 | #define IMPL_DEF1_MICRO_MMU_CTRL 0 |
| 2835 | #define MICRO_MMU_CTRL_LOCAL_HALT_REQ (1 << 2) |
| 2836 | #define MICRO_MMU_CTRL_IDLE (1 << 3) |
| 2837 | |
| 2838 | /* Definitions for implementation-defined registers */ |
| 2839 | #define ACTLR_QCOM_OSH_SHIFT 28 |
| 2840 | #define ACTLR_QCOM_OSH 1 |
| 2841 | |
| 2842 | #define ACTLR_QCOM_ISH_SHIFT 29 |
| 2843 | #define ACTLR_QCOM_ISH 1 |
| 2844 | |
| 2845 | #define ACTLR_QCOM_NSH_SHIFT 30 |
| 2846 | #define ACTLR_QCOM_NSH 1 |
| 2847 | |
| 2848 | static int qsmmuv2_wait_for_halt(struct arm_smmu_device *smmu) |
Mitchel Humpherys | 952f40a | 2015-08-19 12:13:28 -0700 | [diff] [blame] | 2849 | { |
| 2850 | void __iomem *impl_def1_base = ARM_SMMU_IMPL_DEF1(smmu); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2851 | u32 tmp; |
Mitchel Humpherys | 952f40a | 2015-08-19 12:13:28 -0700 | [diff] [blame] | 2852 | |
| 2853 | if (readl_poll_timeout_atomic(impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL, |
| 2854 | tmp, (tmp & MICRO_MMU_CTRL_IDLE), |
| 2855 | 0, 30000)) { |
| 2856 | dev_err(smmu->dev, "Couldn't halt SMMU!\n"); |
| 2857 | return -EBUSY; |
| 2858 | } |
| 2859 | |
| 2860 | return 0; |
| 2861 | } |
| 2862 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2863 | static int __qsmmuv2_halt(struct arm_smmu_device *smmu, bool wait) |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2864 | { |
| 2865 | void __iomem *impl_def1_base = ARM_SMMU_IMPL_DEF1(smmu); |
| 2866 | u32 reg; |
| 2867 | |
| 2868 | reg = readl_relaxed(impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL); |
| 2869 | reg |= MICRO_MMU_CTRL_LOCAL_HALT_REQ; |
| 2870 | writel_relaxed(reg, impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL); |
| 2871 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2872 | return wait ? qsmmuv2_wait_for_halt(smmu) : 0; |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2873 | } |
| 2874 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2875 | static int qsmmuv2_halt(struct arm_smmu_device *smmu) |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2876 | { |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2877 | return __qsmmuv2_halt(smmu, true); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2880 | static int qsmmuv2_halt_nowait(struct arm_smmu_device *smmu) |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2881 | { |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2882 | return __qsmmuv2_halt(smmu, false); |
Patrick Daly | d54eafd | 2016-08-23 17:01:43 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2885 | static void qsmmuv2_resume(struct arm_smmu_device *smmu) |
Mitchel Humpherys | 952f40a | 2015-08-19 12:13:28 -0700 | [diff] [blame] | 2886 | { |
| 2887 | void __iomem *impl_def1_base = ARM_SMMU_IMPL_DEF1(smmu); |
| 2888 | u32 reg; |
| 2889 | |
| 2890 | reg = readl_relaxed(impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL); |
| 2891 | reg &= ~MICRO_MMU_CTRL_LOCAL_HALT_REQ; |
| 2892 | writel_relaxed(reg, impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL); |
| 2893 | } |
| 2894 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2895 | static void qsmmuv2_device_reset(struct arm_smmu_device *smmu) |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 2896 | { |
| 2897 | int i; |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2898 | u32 val; |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 2899 | struct arm_smmu_impl_def_reg *regs = smmu->impl_def_attach_registers; |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2900 | void __iomem *cb_base; |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 2901 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2902 | /* |
| 2903 | * SCTLR.M must be disabled here per ARM SMMUv2 spec |
| 2904 | * to prevent table walks with an inconsistent state. |
| 2905 | */ |
| 2906 | for (i = 0; i < smmu->num_context_banks; ++i) { |
| 2907 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i); |
| 2908 | val = ACTLR_QCOM_ISH << ACTLR_QCOM_ISH_SHIFT | |
| 2909 | ACTLR_QCOM_OSH << ACTLR_QCOM_OSH_SHIFT | |
| 2910 | ACTLR_QCOM_NSH << ACTLR_QCOM_NSH_SHIFT; |
| 2911 | writel_relaxed(val, cb_base + ARM_SMMU_CB_ACTLR); |
| 2912 | } |
| 2913 | |
| 2914 | /* Program implementation defined registers */ |
| 2915 | qsmmuv2_halt(smmu); |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 2916 | for (i = 0; i < smmu->num_impl_def_attach_registers; ++i) |
| 2917 | writel_relaxed(regs[i].value, |
| 2918 | ARM_SMMU_GR0(smmu) + regs[i].offset); |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2919 | qsmmuv2_resume(smmu); |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2922 | static phys_addr_t __qsmmuv2_iova_to_phys_hard(struct iommu_domain *domain, |
| 2923 | dma_addr_t iova, bool halt) |
| 2924 | { |
| 2925 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2926 | struct arm_smmu_device *smmu = smmu_domain->smmu; |
| 2927 | int ret; |
| 2928 | phys_addr_t phys = 0; |
| 2929 | unsigned long flags; |
| 2930 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2931 | ret = arm_smmu_power_on(smmu_domain->smmu->pwr); |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2932 | if (ret) |
| 2933 | return 0; |
| 2934 | |
| 2935 | if (halt) { |
| 2936 | ret = qsmmuv2_halt(smmu); |
| 2937 | if (ret) |
| 2938 | goto out_power_off; |
| 2939 | } |
| 2940 | |
| 2941 | spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); |
| 2942 | spin_lock(&smmu->atos_lock); |
| 2943 | phys = __arm_smmu_iova_to_phys_hard(domain, iova); |
| 2944 | spin_unlock(&smmu->atos_lock); |
| 2945 | spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); |
| 2946 | |
| 2947 | if (halt) |
| 2948 | qsmmuv2_resume(smmu); |
| 2949 | |
| 2950 | out_power_off: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 2951 | arm_smmu_power_off(smmu_domain->smmu->pwr); |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 2952 | return phys; |
| 2953 | } |
| 2954 | |
| 2955 | static phys_addr_t qsmmuv2_iova_to_phys_hard(struct iommu_domain *domain, |
| 2956 | dma_addr_t iova) |
| 2957 | { |
| 2958 | return __qsmmuv2_iova_to_phys_hard(domain, iova, true); |
| 2959 | } |
| 2960 | |
| 2961 | static void qsmmuv2_iova_to_phys_fault( |
| 2962 | struct iommu_domain *domain, |
| 2963 | dma_addr_t iova, phys_addr_t *phys, |
| 2964 | phys_addr_t *phys_post_tlbiall) |
| 2965 | { |
| 2966 | struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); |
| 2967 | struct arm_smmu_cfg *cfg = &smmu_domain->cfg; |
| 2968 | struct arm_smmu_device *smmu; |
| 2969 | void __iomem *cb_base; |
| 2970 | u64 sctlr, sctlr_orig; |
| 2971 | u32 fsr; |
| 2972 | |
| 2973 | smmu = smmu_domain->smmu; |
| 2974 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); |
| 2975 | |
| 2976 | qsmmuv2_halt_nowait(smmu); |
| 2977 | |
| 2978 | writel_relaxed(RESUME_TERMINATE, cb_base + ARM_SMMU_CB_RESUME); |
| 2979 | |
| 2980 | qsmmuv2_wait_for_halt(smmu); |
| 2981 | |
| 2982 | /* clear FSR to allow ATOS to log any faults */ |
| 2983 | fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR); |
| 2984 | writel_relaxed(fsr, cb_base + ARM_SMMU_CB_FSR); |
| 2985 | |
| 2986 | /* disable stall mode momentarily */ |
| 2987 | sctlr_orig = readl_relaxed(cb_base + ARM_SMMU_CB_SCTLR); |
| 2988 | sctlr = sctlr_orig & ~SCTLR_CFCFG; |
| 2989 | writel_relaxed(sctlr, cb_base + ARM_SMMU_CB_SCTLR); |
| 2990 | |
| 2991 | *phys = __qsmmuv2_iova_to_phys_hard(domain, iova, false); |
| 2992 | arm_smmu_tlb_inv_context(smmu_domain); |
| 2993 | *phys_post_tlbiall = __qsmmuv2_iova_to_phys_hard(domain, iova, false); |
| 2994 | |
| 2995 | /* restore SCTLR */ |
| 2996 | writel_relaxed(sctlr_orig, cb_base + ARM_SMMU_CB_SCTLR); |
| 2997 | |
| 2998 | qsmmuv2_resume(smmu); |
| 2999 | } |
| 3000 | |
| 3001 | struct arm_smmu_arch_ops qsmmuv2_arch_ops = { |
| 3002 | .device_reset = qsmmuv2_device_reset, |
| 3003 | .iova_to_phys_hard = qsmmuv2_iova_to_phys_hard, |
| 3004 | .iova_to_phys_fault = qsmmuv2_iova_to_phys_fault, |
| 3005 | }; |
| 3006 | |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 3007 | static void arm_smmu_context_bank_reset(struct arm_smmu_device *smmu) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3008 | { |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 3009 | int i; |
| 3010 | u32 reg, major; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3011 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3012 | void __iomem *cb_base; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3013 | |
Peng Fan | 3ca3712 | 2016-05-03 21:50:30 +0800 | [diff] [blame] | 3014 | /* |
| 3015 | * Before clearing ARM_MMU500_ACTLR_CPRE, need to |
| 3016 | * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK |
| 3017 | * bit is only present in MMU-500r2 onwards. |
| 3018 | */ |
| 3019 | reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7); |
| 3020 | major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK; |
| 3021 | if ((smmu->model == ARM_MMU500) && (major >= 2)) { |
| 3022 | reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR); |
| 3023 | reg &= ~ARM_MMU500_ACR_CACHE_LOCK; |
| 3024 | writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR); |
| 3025 | } |
| 3026 | |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3027 | /* Make sure all context banks are disabled and clear CB_FSR */ |
| 3028 | for (i = 0; i < smmu->num_context_banks; ++i) { |
| 3029 | cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i); |
| 3030 | writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); |
| 3031 | writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR); |
Robin Murphy | f0cfffc | 2016-04-13 18:12:59 +0100 | [diff] [blame] | 3032 | /* |
| 3033 | * Disable MMU-500's not-particularly-beneficial next-page |
| 3034 | * prefetcher for the sake of errata #841119 and #826419. |
| 3035 | */ |
| 3036 | if (smmu->model == ARM_MMU500) { |
| 3037 | reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR); |
| 3038 | reg &= ~ARM_MMU500_ACTLR_CPRE; |
| 3039 | writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR); |
| 3040 | } |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3041 | } |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | static void arm_smmu_device_reset(struct arm_smmu_device *smmu) |
| 3045 | { |
| 3046 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 3047 | int i; |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 3048 | u32 reg; |
| 3049 | |
| 3050 | /* clear global FSR */ |
| 3051 | reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR); |
| 3052 | writel_relaxed(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR); |
| 3053 | |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 3054 | /* |
| 3055 | * Reset stream mapping groups: Initial values mark all SMRn as |
| 3056 | * invalid and all S2CRn as bypass unless overridden. |
| 3057 | */ |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 3058 | if (!(smmu->options & ARM_SMMU_OPT_SKIP_INIT)) { |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 3059 | for (i = 0; i < smmu->num_mapping_groups; ++i) |
| 3060 | arm_smmu_write_sme(smmu, i); |
Mitchel Humpherys | 9c2f648 | 2015-01-13 15:28:40 -0800 | [diff] [blame] | 3061 | |
| 3062 | arm_smmu_context_bank_reset(smmu); |
| 3063 | } |
Will Deacon | 1463fe4 | 2013-07-31 19:21:27 +0100 | [diff] [blame] | 3064 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3065 | /* Invalidate the TLB, just in case */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3066 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH); |
| 3067 | writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH); |
| 3068 | |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 3069 | reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0); |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3070 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3071 | /* Enable fault reporting */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3072 | reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3073 | |
| 3074 | /* Disable TLB broadcasting. */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3075 | reg |= (sCR0_VMIDPNE | sCR0_PTM); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3076 | |
Robin Murphy | 25a1c96 | 2016-02-10 14:25:33 +0000 | [diff] [blame] | 3077 | /* Enable client access, handling unmatched streams as appropriate */ |
| 3078 | reg &= ~sCR0_CLIENTPD; |
| 3079 | if (disable_bypass) |
| 3080 | reg |= sCR0_USFCFG; |
| 3081 | else |
| 3082 | reg &= ~sCR0_USFCFG; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3083 | |
| 3084 | /* Disable forced broadcasting */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3085 | reg &= ~sCR0_FB; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3086 | |
| 3087 | /* Don't upgrade barriers */ |
Andreas Herrmann | 659db6f | 2013-10-01 13:39:09 +0100 | [diff] [blame] | 3088 | reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3089 | |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 3090 | if (smmu->features & ARM_SMMU_FEAT_VMID16) |
| 3091 | reg |= sCR0_VMID16EN; |
| 3092 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3093 | /* Push the button */ |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3094 | __arm_smmu_tlb_sync(smmu); |
Andreas Herrmann | 3a5df8f | 2014-01-30 18:18:04 +0000 | [diff] [blame] | 3095 | writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0); |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3096 | |
| 3097 | /* Manage any implementation defined features */ |
| 3098 | arm_smmu_arch_device_reset(smmu); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | static int arm_smmu_id_size_to_bits(int size) |
| 3102 | { |
| 3103 | switch (size) { |
| 3104 | case 0: |
| 3105 | return 32; |
| 3106 | case 1: |
| 3107 | return 36; |
| 3108 | case 2: |
| 3109 | return 40; |
| 3110 | case 3: |
| 3111 | return 42; |
| 3112 | case 4: |
| 3113 | return 44; |
| 3114 | case 5: |
| 3115 | default: |
| 3116 | return 48; |
| 3117 | } |
| 3118 | } |
| 3119 | |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 3120 | static int arm_smmu_parse_impl_def_registers(struct arm_smmu_device *smmu) |
| 3121 | { |
| 3122 | struct device *dev = smmu->dev; |
| 3123 | int i, ntuples, ret; |
| 3124 | u32 *tuples; |
| 3125 | struct arm_smmu_impl_def_reg *regs, *regit; |
| 3126 | |
| 3127 | if (!of_find_property(dev->of_node, "attach-impl-defs", &ntuples)) |
| 3128 | return 0; |
| 3129 | |
| 3130 | ntuples /= sizeof(u32); |
| 3131 | if (ntuples % 2) { |
| 3132 | dev_err(dev, |
| 3133 | "Invalid number of attach-impl-defs registers: %d\n", |
| 3134 | ntuples); |
| 3135 | return -EINVAL; |
| 3136 | } |
| 3137 | |
| 3138 | regs = devm_kmalloc( |
| 3139 | dev, sizeof(*smmu->impl_def_attach_registers) * ntuples, |
| 3140 | GFP_KERNEL); |
| 3141 | if (!regs) |
| 3142 | return -ENOMEM; |
| 3143 | |
| 3144 | tuples = devm_kmalloc(dev, sizeof(u32) * ntuples * 2, GFP_KERNEL); |
| 3145 | if (!tuples) |
| 3146 | return -ENOMEM; |
| 3147 | |
| 3148 | ret = of_property_read_u32_array(dev->of_node, "attach-impl-defs", |
| 3149 | tuples, ntuples); |
| 3150 | if (ret) |
| 3151 | return ret; |
| 3152 | |
| 3153 | for (i = 0, regit = regs; i < ntuples; i += 2, ++regit) { |
| 3154 | regit->offset = tuples[i]; |
| 3155 | regit->value = tuples[i + 1]; |
| 3156 | } |
| 3157 | |
| 3158 | devm_kfree(dev, tuples); |
| 3159 | |
| 3160 | smmu->impl_def_attach_registers = regs; |
| 3161 | smmu->num_impl_def_attach_registers = ntuples / 2; |
| 3162 | |
| 3163 | return 0; |
| 3164 | } |
| 3165 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3166 | |
| 3167 | static int arm_smmu_init_clocks(struct arm_smmu_power_resources *pwr) |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3168 | { |
| 3169 | const char *cname; |
| 3170 | struct property *prop; |
| 3171 | int i; |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3172 | struct device *dev = pwr->dev; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3173 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3174 | pwr->num_clocks = |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3175 | of_property_count_strings(dev->of_node, "clock-names"); |
| 3176 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3177 | if (pwr->num_clocks < 1) { |
| 3178 | pwr->num_clocks = 0; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3179 | return 0; |
Patrick Daly | f0c58e1 | 2016-10-12 22:15:36 -0700 | [diff] [blame] | 3180 | } |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3181 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3182 | pwr->clocks = devm_kzalloc( |
| 3183 | dev, sizeof(*pwr->clocks) * pwr->num_clocks, |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3184 | GFP_KERNEL); |
| 3185 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3186 | if (!pwr->clocks) |
| 3187 | return -ENOMEM; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3188 | |
| 3189 | i = 0; |
| 3190 | of_property_for_each_string(dev->of_node, "clock-names", |
| 3191 | prop, cname) { |
| 3192 | struct clk *c = devm_clk_get(dev, cname); |
| 3193 | |
| 3194 | if (IS_ERR(c)) { |
| 3195 | dev_err(dev, "Couldn't get clock: %s", |
| 3196 | cname); |
Mathew Joseph Karimpanal | 0c4fd1b | 2016-03-16 11:48:34 -0700 | [diff] [blame] | 3197 | return PTR_ERR(c); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | if (clk_get_rate(c) == 0) { |
| 3201 | long rate = clk_round_rate(c, 1000); |
| 3202 | |
| 3203 | clk_set_rate(c, rate); |
| 3204 | } |
| 3205 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3206 | pwr->clocks[i] = c; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3207 | |
| 3208 | ++i; |
| 3209 | } |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3213 | static int arm_smmu_init_regulators(struct arm_smmu_power_resources *pwr) |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 3214 | { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3215 | const char *cname; |
| 3216 | struct property *prop; |
| 3217 | int i, ret = 0; |
| 3218 | struct device *dev = pwr->dev; |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 3219 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3220 | pwr->num_gdscs = |
| 3221 | of_property_count_strings(dev->of_node, "qcom,regulator-names"); |
| 3222 | |
| 3223 | if (pwr->num_gdscs < 1) { |
| 3224 | pwr->num_gdscs = 0; |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 3225 | return 0; |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3226 | } |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 3227 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3228 | pwr->gdscs = devm_kzalloc( |
| 3229 | dev, sizeof(*pwr->gdscs) * pwr->num_gdscs, GFP_KERNEL); |
| 3230 | |
| 3231 | if (!pwr->gdscs) |
| 3232 | return -ENOMEM; |
| 3233 | |
| 3234 | i = 0; |
| 3235 | of_property_for_each_string(dev->of_node, "qcom,regulator-names", |
| 3236 | prop, cname) |
| 3237 | pwr->gdscs[i].supply = cname; |
| 3238 | |
| 3239 | ret = devm_regulator_bulk_get(dev, pwr->num_gdscs, pwr->gdscs); |
| 3240 | return ret; |
| 3241 | } |
| 3242 | |
| 3243 | static int arm_smmu_init_bus_scaling(struct arm_smmu_power_resources *pwr) |
| 3244 | { |
| 3245 | struct device *dev = pwr->dev; |
| 3246 | |
| 3247 | /* We don't want the bus APIs to print an error message */ |
| 3248 | if (!of_find_property(dev->of_node, "qcom,msm-bus,name", NULL)) { |
| 3249 | dev_dbg(dev, "No bus scaling info\n"); |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
| 3253 | pwr->bus_dt_data = msm_bus_cl_get_pdata(pwr->pdev); |
| 3254 | if (!pwr->bus_dt_data) { |
| 3255 | dev_err(dev, "Unable to read bus-scaling from devicetree\n"); |
| 3256 | return -EINVAL; |
| 3257 | } |
| 3258 | |
| 3259 | pwr->bus_client = msm_bus_scale_register_client(pwr->bus_dt_data); |
| 3260 | if (!pwr->bus_client) { |
| 3261 | dev_err(dev, "Bus client registration failed\n"); |
| 3262 | msm_bus_cl_clear_pdata(pwr->bus_dt_data); |
| 3263 | return -EINVAL; |
| 3264 | } |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 3265 | |
| 3266 | return 0; |
| 3267 | } |
| 3268 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3269 | /* |
| 3270 | * Cleanup done by devm. Any non-devm resources must clean up themselves. |
| 3271 | */ |
| 3272 | static struct arm_smmu_power_resources *arm_smmu_init_power_resources( |
| 3273 | struct platform_device *pdev) |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3274 | { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3275 | struct arm_smmu_power_resources *pwr; |
| 3276 | int ret; |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3277 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3278 | pwr = devm_kzalloc(&pdev->dev, sizeof(*pwr), GFP_KERNEL); |
| 3279 | if (!pwr) |
| 3280 | return ERR_PTR(-ENOMEM); |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3281 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3282 | pwr->dev = &pdev->dev; |
| 3283 | pwr->pdev = pdev; |
| 3284 | mutex_init(&pwr->power_lock); |
| 3285 | spin_lock_init(&pwr->clock_refs_lock); |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3286 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3287 | ret = arm_smmu_init_clocks(pwr); |
| 3288 | if (ret) |
| 3289 | return ERR_PTR(ret); |
Patrick Daly | cf7b0de | 2016-10-06 17:04:49 -0700 | [diff] [blame] | 3290 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3291 | ret = arm_smmu_init_regulators(pwr); |
| 3292 | if (ret) |
| 3293 | return ERR_PTR(ret); |
| 3294 | |
| 3295 | ret = arm_smmu_init_bus_scaling(pwr); |
| 3296 | if (ret) |
| 3297 | return ERR_PTR(ret); |
| 3298 | |
| 3299 | return pwr; |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
Patrick Daly | cf7b0de | 2016-10-06 17:04:49 -0700 | [diff] [blame] | 3302 | /* |
| 3303 | * Bus APIs are not devm-safe. |
| 3304 | */ |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3305 | static void arm_smmu_exit_power_resources(struct arm_smmu_power_resources *pwr) |
Patrick Daly | cf7b0de | 2016-10-06 17:04:49 -0700 | [diff] [blame] | 3306 | { |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3307 | msm_bus_scale_unregister_client(pwr->bus_client); |
| 3308 | msm_bus_cl_clear_pdata(pwr->bus_dt_data); |
Patrick Daly | cf7b0de | 2016-10-06 17:04:49 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3311 | static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu) |
| 3312 | { |
| 3313 | unsigned long size; |
| 3314 | void __iomem *gr0_base = ARM_SMMU_GR0(smmu); |
| 3315 | u32 id; |
Robin Murphy | bae2c2d | 2015-07-29 19:46:05 +0100 | [diff] [blame] | 3316 | bool cttw_dt, cttw_reg; |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 3317 | int i; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3318 | |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3319 | dev_dbg(smmu->dev, "probing hardware configuration...\n"); |
| 3320 | dev_dbg(smmu->dev, "SMMUv%d with:\n", |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 3321 | smmu->version == ARM_SMMU_V2 ? 2 : 1); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3322 | |
| 3323 | /* ID0 */ |
| 3324 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0); |
Will Deacon | 4cf740b | 2014-07-14 19:47:39 +0100 | [diff] [blame] | 3325 | |
| 3326 | /* Restrict available stages based on module parameter */ |
| 3327 | if (force_stage == 1) |
| 3328 | id &= ~(ID0_S2TS | ID0_NTS); |
| 3329 | else if (force_stage == 2) |
| 3330 | id &= ~(ID0_S1TS | ID0_NTS); |
| 3331 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3332 | if (id & ID0_S1TS) { |
| 3333 | smmu->features |= ARM_SMMU_FEAT_TRANS_S1; |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3334 | dev_dbg(smmu->dev, "\tstage 1 translation\n"); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | if (id & ID0_S2TS) { |
| 3338 | smmu->features |= ARM_SMMU_FEAT_TRANS_S2; |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3339 | dev_dbg(smmu->dev, "\tstage 2 translation\n"); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | if (id & ID0_NTS) { |
| 3343 | smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED; |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3344 | dev_dbg(smmu->dev, "\tnested translation\n"); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | if (!(smmu->features & |
Will Deacon | 4cf740b | 2014-07-14 19:47:39 +0100 | [diff] [blame] | 3348 | (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3349 | dev_err(smmu->dev, "\tno translation support!\n"); |
| 3350 | return -ENODEV; |
| 3351 | } |
| 3352 | |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 3353 | if ((id & ID0_S1TS) && |
| 3354 | ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) { |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 3355 | smmu->features |= ARM_SMMU_FEAT_TRANS_OPS; |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3356 | dev_dbg(smmu->dev, "\taddress translation ops\n"); |
Mitchel Humpherys | 859a732 | 2014-10-29 21:13:40 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Robin Murphy | bae2c2d | 2015-07-29 19:46:05 +0100 | [diff] [blame] | 3359 | /* |
| 3360 | * In order for DMA API calls to work properly, we must defer to what |
| 3361 | * the DT says about coherency, regardless of what the hardware claims. |
| 3362 | * Fortunately, this also opens up a workaround for systems where the |
| 3363 | * ID register value has ended up configured incorrectly. |
| 3364 | */ |
| 3365 | cttw_dt = of_dma_is_coherent(smmu->dev->of_node); |
| 3366 | cttw_reg = !!(id & ID0_CTTW); |
| 3367 | if (cttw_dt) |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3368 | smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK; |
Robin Murphy | bae2c2d | 2015-07-29 19:46:05 +0100 | [diff] [blame] | 3369 | if (cttw_dt || cttw_reg) |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3370 | dev_dbg(smmu->dev, "\t%scoherent table walk\n", |
Robin Murphy | bae2c2d | 2015-07-29 19:46:05 +0100 | [diff] [blame] | 3371 | cttw_dt ? "" : "non-"); |
| 3372 | if (cttw_dt != cttw_reg) |
| 3373 | dev_notice(smmu->dev, |
| 3374 | "\t(IDR0.CTTW overridden by dma-coherent property)\n"); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3375 | |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3376 | /* Max. number of entries we have for stream matching/indexing */ |
| 3377 | size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK); |
| 3378 | smmu->streamid_mask = size - 1; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3379 | if (id & ID0_SMS) { |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3380 | u32 smr; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3381 | |
| 3382 | smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH; |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3383 | size = (id >> ID0_NUMSMRG_SHIFT) & ID0_NUMSMRG_MASK; |
| 3384 | if (size == 0) { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3385 | dev_err(smmu->dev, |
| 3386 | "stream-matching supported, but no SMRs present!\n"); |
| 3387 | return -ENODEV; |
| 3388 | } |
| 3389 | |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3390 | /* |
| 3391 | * SMR.ID bits may not be preserved if the corresponding MASK |
| 3392 | * bits are set, so check each one separately. We can reject |
| 3393 | * masters later if they try to claim IDs outside these masks. |
| 3394 | */ |
| 3395 | smr = smmu->streamid_mask << SMR_ID_SHIFT; |
| 3396 | writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); |
| 3397 | smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); |
| 3398 | smmu->streamid_mask = smr >> SMR_ID_SHIFT; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3399 | |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3400 | smr = smmu->streamid_mask << SMR_MASK_SHIFT; |
| 3401 | writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0)); |
| 3402 | smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0)); |
| 3403 | smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT; |
Dhaval Patel | 031d746 | 2015-05-09 14:47:29 -0700 | [diff] [blame] | 3404 | |
Robin Murphy | 468f494 | 2016-09-12 17:13:49 +0100 | [diff] [blame] | 3405 | /* Zero-initialised to mark as invalid */ |
| 3406 | smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs), |
| 3407 | GFP_KERNEL); |
| 3408 | if (!smmu->smrs) |
| 3409 | return -ENOMEM; |
| 3410 | |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3411 | dev_notice(smmu->dev, |
| 3412 | "\tstream matching with %lu register groups, mask 0x%x", |
| 3413 | size, smmu->smr_mask_mask); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3414 | } |
Robin Murphy | a754fd1 | 2016-09-12 17:13:50 +0100 | [diff] [blame] | 3415 | /* s2cr->type == 0 means translation, so initialise explicitly */ |
| 3416 | smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs), |
| 3417 | GFP_KERNEL); |
| 3418 | if (!smmu->s2crs) |
| 3419 | return -ENOMEM; |
| 3420 | for (i = 0; i < size; i++) |
| 3421 | smmu->s2crs[i] = s2cr_init_val; |
| 3422 | |
Robin Murphy | 5386780 | 2016-09-12 17:13:48 +0100 | [diff] [blame] | 3423 | smmu->num_mapping_groups = size; |
Robin Murphy | 6668f69 | 2016-09-12 17:13:54 +0100 | [diff] [blame] | 3424 | mutex_init(&smmu->stream_map_mutex); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3425 | |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3426 | if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) { |
| 3427 | smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L; |
| 3428 | if (!(id & ID0_PTFS_NO_AARCH32S)) |
| 3429 | smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S; |
| 3430 | } |
| 3431 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3432 | /* ID1 */ |
| 3433 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1); |
Will Deacon | c757e85 | 2014-07-30 11:33:25 +0100 | [diff] [blame] | 3434 | smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3435 | |
Andreas Herrmann | c55af7f | 2013-10-01 13:39:06 +0100 | [diff] [blame] | 3436 | /* Check for size mismatch of SMMU address space from mapped region */ |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3437 | size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); |
Will Deacon | c757e85 | 2014-07-30 11:33:25 +0100 | [diff] [blame] | 3438 | size *= 2 << smmu->pgshift; |
Andreas Herrmann | c55af7f | 2013-10-01 13:39:06 +0100 | [diff] [blame] | 3439 | if (smmu->size != size) |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 3440 | dev_warn(smmu->dev, |
| 3441 | "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n", |
| 3442 | size, smmu->size); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3443 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3444 | smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3445 | smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK; |
| 3446 | if (smmu->num_s2_context_banks > smmu->num_context_banks) { |
| 3447 | dev_err(smmu->dev, "impossible number of S2 context banks!\n"); |
| 3448 | return -ENODEV; |
| 3449 | } |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3450 | dev_dbg(smmu->dev, "\t%u context banks (%u stage-2 only)\n", |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3451 | smmu->num_context_banks, smmu->num_s2_context_banks); |
Robin Murphy | e086d91 | 2016-04-13 18:12:58 +0100 | [diff] [blame] | 3452 | /* |
| 3453 | * Cavium CN88xx erratum #27704. |
| 3454 | * Ensure ASID and VMID allocation is unique across all SMMUs in |
| 3455 | * the system. |
| 3456 | */ |
| 3457 | if (smmu->model == CAVIUM_SMMUV2) { |
| 3458 | smmu->cavium_id_base = |
| 3459 | atomic_add_return(smmu->num_context_banks, |
| 3460 | &cavium_smmu_context_count); |
| 3461 | smmu->cavium_id_base -= smmu->num_context_banks; |
| 3462 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3463 | |
| 3464 | /* ID2 */ |
| 3465 | id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); |
| 3466 | size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3467 | smmu->ipa_size = size; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3468 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3469 | /* The output mask is also applied for bypass */ |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3470 | size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3471 | smmu->pa_size = size; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3472 | |
Tirumalesh Chalamarla | 4e3e9b6 | 2016-02-23 10:19:00 -0800 | [diff] [blame] | 3473 | if (id & ID2_VMID16) |
| 3474 | smmu->features |= ARM_SMMU_FEAT_VMID16; |
| 3475 | |
Robin Murphy | f1d8454 | 2015-03-04 16:41:05 +0000 | [diff] [blame] | 3476 | /* |
| 3477 | * What the page table walker can address actually depends on which |
| 3478 | * descriptor format is in use, but since a) we don't know that yet, |
| 3479 | * and b) it can vary per context bank, this will have to do... |
| 3480 | */ |
| 3481 | if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size))) |
| 3482 | dev_warn(smmu->dev, |
| 3483 | "failed to set DMA mask for table walker\n"); |
| 3484 | |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 3485 | if (smmu->version < ARM_SMMU_V2) { |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3486 | smmu->va_size = smmu->ipa_size; |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 3487 | if (smmu->version == ARM_SMMU_V1_64K) |
| 3488 | smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3489 | } else { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3490 | size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3491 | smmu->va_size = arm_smmu_id_size_to_bits(size); |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3492 | if (id & ID2_PTFS_4K) |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3493 | smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3494 | if (id & ID2_PTFS_16K) |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3495 | smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K; |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3496 | if (id & ID2_PTFS_64K) |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3497 | smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3498 | } |
| 3499 | |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3500 | /* Now we've corralled the various formats, what'll it do? */ |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3501 | if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 3502 | smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3503 | if (smmu->features & |
| 3504 | (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K)) |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 3505 | smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3506 | if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K) |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 3507 | smmu->pgsize_bitmap |= SZ_16K | SZ_32M; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3508 | if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K) |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 3509 | smmu->pgsize_bitmap |= SZ_64K | SZ_512M; |
Robin Murphy | 7602b87 | 2016-04-28 17:12:09 +0100 | [diff] [blame] | 3510 | |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 3511 | if (arm_smmu_ops.pgsize_bitmap == -1UL) |
| 3512 | arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap; |
| 3513 | else |
| 3514 | arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap; |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3515 | dev_dbg(smmu->dev, "\tSupported page sizes: 0x%08lx\n", |
Robin Murphy | d546635 | 2016-05-09 17:20:09 +0100 | [diff] [blame] | 3516 | smmu->pgsize_bitmap); |
| 3517 | |
Will Deacon | 518f713 | 2014-11-14 17:17:54 +0000 | [diff] [blame] | 3518 | |
Will Deacon | 28d6007 | 2014-09-01 16:24:48 +0100 | [diff] [blame] | 3519 | if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3520 | dev_dbg(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n", |
| 3521 | smmu->va_size, smmu->ipa_size); |
Will Deacon | 28d6007 | 2014-09-01 16:24:48 +0100 | [diff] [blame] | 3522 | |
| 3523 | if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) |
Mitchel Humpherys | ba82258 | 2015-10-20 11:37:41 -0700 | [diff] [blame] | 3524 | dev_dbg(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n", |
| 3525 | smmu->ipa_size, smmu->pa_size); |
Will Deacon | 28d6007 | 2014-09-01 16:24:48 +0100 | [diff] [blame] | 3526 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3527 | return 0; |
| 3528 | } |
| 3529 | |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3530 | static int arm_smmu_arch_init(struct arm_smmu_device *smmu) |
| 3531 | { |
| 3532 | if (!smmu->arch_ops) |
| 3533 | return 0; |
| 3534 | if (!smmu->arch_ops->init) |
| 3535 | return 0; |
| 3536 | return smmu->arch_ops->init(smmu); |
| 3537 | } |
| 3538 | |
| 3539 | static void arm_smmu_arch_device_reset(struct arm_smmu_device *smmu) |
| 3540 | { |
| 3541 | if (!smmu->arch_ops) |
| 3542 | return; |
| 3543 | if (!smmu->arch_ops->device_reset) |
| 3544 | return; |
| 3545 | return smmu->arch_ops->device_reset(smmu); |
| 3546 | } |
| 3547 | |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3548 | struct arm_smmu_match_data { |
| 3549 | enum arm_smmu_arch_version version; |
| 3550 | enum arm_smmu_implementation model; |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3551 | struct arm_smmu_arch_ops *arch_ops; |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3552 | }; |
| 3553 | |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3554 | #define ARM_SMMU_MATCH_DATA(name, ver, imp, ops) \ |
| 3555 | static struct arm_smmu_match_data name = { \ |
| 3556 | .version = ver, \ |
| 3557 | .model = imp, \ |
| 3558 | .arch_ops = ops, \ |
| 3559 | } \ |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3560 | |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 3561 | struct arm_smmu_arch_ops qsmmuv500_arch_ops; |
| 3562 | |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3563 | ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU, NULL); |
| 3564 | ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU, NULL); |
| 3565 | ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU, NULL); |
| 3566 | ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500, NULL); |
| 3567 | ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2, NULL); |
Patrick Daly | ad441dd | 2016-09-15 15:50:46 -0700 | [diff] [blame] | 3568 | ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2, &qsmmuv2_arch_ops); |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 3569 | ARM_SMMU_MATCH_DATA(qcom_smmuv500, ARM_SMMU_V2, QCOM_SMMUV500, |
| 3570 | &qsmmuv500_arch_ops); |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3571 | |
Joerg Roedel | 09b5269 | 2014-10-02 12:24:45 +0200 | [diff] [blame] | 3572 | static const struct of_device_id arm_smmu_of_match[] = { |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3573 | { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 }, |
| 3574 | { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 }, |
| 3575 | { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 }, |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 3576 | { .compatible = "arm,mmu-401", .data = &arm_mmu401 }, |
Robin Murphy | f0cfffc | 2016-04-13 18:12:59 +0100 | [diff] [blame] | 3577 | { .compatible = "arm,mmu-500", .data = &arm_mmu500 }, |
Robin Murphy | e086d91 | 2016-04-13 18:12:58 +0100 | [diff] [blame] | 3578 | { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 }, |
Patrick Daly | f0d4e21 | 2016-06-20 15:50:14 -0700 | [diff] [blame] | 3579 | { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 }, |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 3580 | { .compatible = "qcom,qsmmu-v500", .data = &qcom_smmuv500 }, |
Robin Murphy | 0936040 | 2014-08-28 17:51:59 +0100 | [diff] [blame] | 3581 | { }, |
| 3582 | }; |
| 3583 | MODULE_DEVICE_TABLE(of, arm_smmu_of_match); |
| 3584 | |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 3585 | static int qsmmuv500_tbu_register(struct device *dev, void *data); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3586 | static int arm_smmu_device_dt_probe(struct platform_device *pdev) |
| 3587 | { |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3588 | const struct arm_smmu_match_data *data; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3589 | struct resource *res; |
| 3590 | struct arm_smmu_device *smmu; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3591 | struct device *dev = &pdev->dev; |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 3592 | int num_irqs, i, err; |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 3593 | bool legacy_binding; |
| 3594 | |
| 3595 | legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL); |
| 3596 | if (legacy_binding && !using_generic_binding) { |
| 3597 | if (!using_legacy_binding) |
| 3598 | pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n"); |
| 3599 | using_legacy_binding = true; |
| 3600 | } else if (!legacy_binding && !using_legacy_binding) { |
| 3601 | using_generic_binding = true; |
| 3602 | } else { |
| 3603 | dev_err(dev, "not probing due to mismatched DT properties\n"); |
| 3604 | return -ENODEV; |
| 3605 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3606 | |
| 3607 | smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL); |
| 3608 | if (!smmu) { |
| 3609 | dev_err(dev, "failed to allocate arm_smmu_device\n"); |
| 3610 | return -ENOMEM; |
| 3611 | } |
| 3612 | smmu->dev = dev; |
Mitchel Humpherys | 2fbae2a | 2014-12-04 11:46:24 -0800 | [diff] [blame] | 3613 | spin_lock_init(&smmu->atos_lock); |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 3614 | idr_init(&smmu->asid_idr); |
| 3615 | mutex_init(&smmu->idr_mutex); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3616 | |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 3617 | data = of_device_get_match_data(dev); |
Robin Murphy | 67b65a3 | 2016-04-13 18:12:57 +0100 | [diff] [blame] | 3618 | smmu->version = data->version; |
| 3619 | smmu->model = data->model; |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3620 | smmu->arch_ops = data->arch_ops; |
Robin Murphy | 0936040 | 2014-08-28 17:51:59 +0100 | [diff] [blame] | 3621 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3622 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Julia Lawall | 8a7f431 | 2013-08-19 12:20:37 +0100 | [diff] [blame] | 3623 | smmu->base = devm_ioremap_resource(dev, res); |
| 3624 | if (IS_ERR(smmu->base)) |
| 3625 | return PTR_ERR(smmu->base); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3626 | smmu->size = resource_size(res); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3627 | |
| 3628 | if (of_property_read_u32(dev->of_node, "#global-interrupts", |
| 3629 | &smmu->num_global_irqs)) { |
| 3630 | dev_err(dev, "missing #global-interrupts property\n"); |
| 3631 | return -ENODEV; |
| 3632 | } |
| 3633 | |
| 3634 | num_irqs = 0; |
| 3635 | while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) { |
| 3636 | num_irqs++; |
| 3637 | if (num_irqs > smmu->num_global_irqs) |
| 3638 | smmu->num_context_irqs++; |
| 3639 | } |
| 3640 | |
Andreas Herrmann | 44a08de | 2013-10-01 13:39:07 +0100 | [diff] [blame] | 3641 | if (!smmu->num_context_irqs) { |
| 3642 | dev_err(dev, "found %d interrupts but expected at least %d\n", |
| 3643 | num_irqs, smmu->num_global_irqs + 1); |
| 3644 | return -ENODEV; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3645 | } |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3646 | |
| 3647 | smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs, |
| 3648 | GFP_KERNEL); |
| 3649 | if (!smmu->irqs) { |
| 3650 | dev_err(dev, "failed to allocate %d irqs\n", num_irqs); |
| 3651 | return -ENOMEM; |
| 3652 | } |
| 3653 | |
| 3654 | for (i = 0; i < num_irqs; ++i) { |
| 3655 | int irq = platform_get_irq(pdev, i); |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 3656 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3657 | if (irq < 0) { |
| 3658 | dev_err(dev, "failed to get irq index %d\n", i); |
| 3659 | return -ENODEV; |
| 3660 | } |
| 3661 | smmu->irqs[i] = irq; |
| 3662 | } |
| 3663 | |
Dhaval Patel | 031d746 | 2015-05-09 14:47:29 -0700 | [diff] [blame] | 3664 | parse_driver_options(smmu); |
| 3665 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3666 | smmu->pwr = arm_smmu_init_power_resources(pdev); |
| 3667 | if (IS_ERR(smmu->pwr)) |
| 3668 | return PTR_ERR(smmu->pwr); |
Mitchel Humpherys | f7666ae | 2014-07-23 17:35:07 -0700 | [diff] [blame] | 3669 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3670 | err = arm_smmu_power_on(smmu->pwr); |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3671 | if (err) |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3672 | goto out_exit_power_resources; |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3673 | |
| 3674 | err = arm_smmu_device_cfg_probe(smmu); |
| 3675 | if (err) |
| 3676 | goto out_power_off; |
| 3677 | |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 3678 | err = arm_smmu_parse_impl_def_registers(smmu); |
| 3679 | if (err) |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 3680 | goto out_power_off; |
Mitchel Humpherys | 5494a5e | 2014-08-14 17:44:49 -0700 | [diff] [blame] | 3681 | |
Robin Murphy | b7862e3 | 2016-04-13 18:13:03 +0100 | [diff] [blame] | 3682 | if (smmu->version == ARM_SMMU_V2 && |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3683 | smmu->num_context_banks != smmu->num_context_irqs) { |
| 3684 | dev_err(dev, |
Mitchel Humpherys | 73230ce | 2014-12-11 17:18:02 -0800 | [diff] [blame] | 3685 | "found %d context interrupt(s) but have %d context banks. assuming %d context interrupts.\n", |
| 3686 | smmu->num_context_irqs, smmu->num_context_banks, |
| 3687 | smmu->num_context_banks); |
| 3688 | smmu->num_context_irqs = smmu->num_context_banks; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3689 | } |
| 3690 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3691 | for (i = 0; i < smmu->num_global_irqs; ++i) { |
Mitchel Humpherys | c4f2a80 | 2015-02-04 21:29:46 -0800 | [diff] [blame] | 3692 | err = devm_request_threaded_irq(smmu->dev, smmu->irqs[i], |
| 3693 | NULL, arm_smmu_global_fault, |
| 3694 | IRQF_ONESHOT | IRQF_SHARED, |
| 3695 | "arm-smmu global fault", smmu); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3696 | if (err) { |
| 3697 | dev_err(dev, "failed to request global IRQ %d (%u)\n", |
| 3698 | i, smmu->irqs[i]); |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 3699 | goto out_power_off; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3703 | err = arm_smmu_arch_init(smmu); |
| 3704 | if (err) |
Robin Murphy | d5b4178 | 2016-09-14 15:21:39 +0100 | [diff] [blame] | 3705 | goto out_power_off; |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3706 | |
Robin Murphy | 06e393e | 2016-09-12 17:13:55 +0100 | [diff] [blame] | 3707 | of_iommu_set_ops(dev->of_node, &arm_smmu_ops); |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 3708 | platform_set_drvdata(pdev, smmu); |
Will Deacon | fd90cec | 2013-08-21 13:56:34 +0100 | [diff] [blame] | 3709 | arm_smmu_device_reset(smmu); |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3710 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | d747620 | 2016-09-08 18:23:28 -0700 | [diff] [blame] | 3711 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 3712 | /* Oh, for a proper bus abstraction */ |
| 3713 | if (!iommu_present(&platform_bus_type)) |
| 3714 | bus_set_iommu(&platform_bus_type, &arm_smmu_ops); |
| 3715 | #ifdef CONFIG_ARM_AMBA |
| 3716 | if (!iommu_present(&amba_bustype)) |
| 3717 | bus_set_iommu(&amba_bustype, &arm_smmu_ops); |
| 3718 | #endif |
| 3719 | #ifdef CONFIG_PCI |
| 3720 | if (!iommu_present(&pci_bus_type)) { |
| 3721 | pci_request_acs(); |
| 3722 | bus_set_iommu(&pci_bus_type, &arm_smmu_ops); |
| 3723 | } |
| 3724 | #endif |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3725 | return 0; |
| 3726 | |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3727 | out_power_off: |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3728 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3729 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3730 | out_exit_power_resources: |
| 3731 | arm_smmu_exit_power_resources(smmu->pwr); |
Patrick Daly | cf7b0de | 2016-10-06 17:04:49 -0700 | [diff] [blame] | 3732 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3733 | return err; |
| 3734 | } |
| 3735 | |
| 3736 | static int arm_smmu_device_remove(struct platform_device *pdev) |
| 3737 | { |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 3738 | struct arm_smmu_device *smmu = platform_get_drvdata(pdev); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3739 | |
| 3740 | if (!smmu) |
| 3741 | return -ENODEV; |
| 3742 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3743 | if (arm_smmu_power_on(smmu->pwr)) |
Mitchel Humpherys | 3e52a7e | 2015-10-19 17:13:47 -0700 | [diff] [blame] | 3744 | return -EINVAL; |
| 3745 | |
Will Deacon | ecfadb6 | 2013-07-31 19:21:28 +0100 | [diff] [blame] | 3746 | if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS)) |
Robin Murphy | fe52d4f | 2016-09-12 17:13:52 +0100 | [diff] [blame] | 3747 | dev_err(&pdev->dev, "removing device with active domains!\n"); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3748 | |
Patrick Daly | c190d93 | 2016-08-30 17:23:28 -0700 | [diff] [blame] | 3749 | idr_destroy(&smmu->asid_idr); |
| 3750 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3751 | /* Turn the thing off */ |
Mitchel Humpherys | 2907320 | 2014-07-08 09:52:18 -0700 | [diff] [blame] | 3752 | writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0); |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3753 | arm_smmu_power_off(smmu->pwr); |
Patrick Daly | 3a8a88a | 2016-07-22 12:24:05 -0700 | [diff] [blame] | 3754 | |
Patrick Daly | 5b3d8c6 | 2016-11-01 15:34:11 -0700 | [diff] [blame] | 3755 | arm_smmu_exit_power_resources(smmu->pwr); |
Patrick Daly | 2764f95 | 2016-09-06 19:22:44 -0700 | [diff] [blame] | 3756 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3757 | return 0; |
| 3758 | } |
| 3759 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3760 | static struct platform_driver arm_smmu_driver = { |
| 3761 | .driver = { |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3762 | .name = "arm-smmu", |
| 3763 | .of_match_table = of_match_ptr(arm_smmu_of_match), |
| 3764 | }, |
| 3765 | .probe = arm_smmu_device_dt_probe, |
| 3766 | .remove = arm_smmu_device_remove, |
| 3767 | }; |
| 3768 | |
| 3769 | static int __init arm_smmu_init(void) |
| 3770 | { |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 3771 | static bool registered; |
| 3772 | int ret = 0; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3773 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 3774 | if (!registered) { |
| 3775 | ret = platform_driver_register(&arm_smmu_driver); |
| 3776 | registered = !ret; |
Wei Chen | 112c898 | 2016-06-13 17:20:17 +0800 | [diff] [blame] | 3777 | } |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 3778 | return ret; |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | static void __exit arm_smmu_exit(void) |
| 3782 | { |
| 3783 | return platform_driver_unregister(&arm_smmu_driver); |
| 3784 | } |
| 3785 | |
Andreas Herrmann | b1950b2 | 2013-10-01 13:39:05 +0100 | [diff] [blame] | 3786 | subsys_initcall(arm_smmu_init); |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 3787 | module_exit(arm_smmu_exit); |
| 3788 | |
Robin Murphy | 7e96c74 | 2016-09-14 15:26:46 +0100 | [diff] [blame] | 3789 | static int __init arm_smmu_of_init(struct device_node *np) |
| 3790 | { |
| 3791 | int ret = arm_smmu_init(); |
| 3792 | |
| 3793 | if (ret) |
| 3794 | return ret; |
| 3795 | |
| 3796 | if (!of_platform_device_create(np, NULL, platform_bus_type.dev_root)) |
| 3797 | return -ENODEV; |
| 3798 | |
| 3799 | return 0; |
| 3800 | } |
| 3801 | IOMMU_OF_DECLARE(arm_smmuv1, "arm,smmu-v1", arm_smmu_of_init); |
| 3802 | IOMMU_OF_DECLARE(arm_smmuv2, "arm,smmu-v2", arm_smmu_of_init); |
| 3803 | IOMMU_OF_DECLARE(arm_mmu400, "arm,mmu-400", arm_smmu_of_init); |
| 3804 | IOMMU_OF_DECLARE(arm_mmu401, "arm,mmu-401", arm_smmu_of_init); |
| 3805 | IOMMU_OF_DECLARE(arm_mmu500, "arm,mmu-500", arm_smmu_of_init); |
| 3806 | IOMMU_OF_DECLARE(cavium_smmuv2, "cavium,smmu-v2", arm_smmu_of_init); |
| 3807 | IOMMU_OF_DECLARE(qcom_smmuv2, "qcom,smmu-v2", arm_smmu_of_init); |
| 3808 | IOMMU_OF_DECLARE(qcom_mmu500, "qcom,qsmmu-v500", arm_smmu_of_init); |
| 3809 | |
Patrick Daly | 1f8a288 | 2016-09-12 17:32:05 -0700 | [diff] [blame] | 3810 | #define DEBUG_SID_HALT_REG 0x0 |
| 3811 | #define DEBUG_SID_HALT_VAL (0x1 << 16) |
| 3812 | |
| 3813 | #define DEBUG_SR_HALT_ACK_REG 0x20 |
| 3814 | #define DEBUG_SR_HALT_ACK_VAL (0x1 << 1) |
| 3815 | |
| 3816 | #define TBU_DBG_TIMEOUT_US 30000 |
| 3817 | |
| 3818 | struct qsmmuv500_tbu_device { |
| 3819 | struct list_head list; |
| 3820 | struct device *dev; |
| 3821 | struct arm_smmu_device *smmu; |
| 3822 | void __iomem *base; |
| 3823 | void __iomem *status_reg; |
| 3824 | |
| 3825 | struct arm_smmu_power_resources *pwr; |
| 3826 | |
| 3827 | /* Protects halt count */ |
| 3828 | spinlock_t halt_lock; |
| 3829 | u32 halt_count; |
| 3830 | }; |
| 3831 | |
| 3832 | static int qsmmuv500_tbu_power_on_all(struct arm_smmu_device *smmu) |
| 3833 | { |
| 3834 | struct qsmmuv500_tbu_device *tbu; |
| 3835 | struct list_head *list = smmu->archdata; |
| 3836 | int ret = 0; |
| 3837 | |
| 3838 | list_for_each_entry(tbu, list, list) { |
| 3839 | ret = arm_smmu_power_on(tbu->pwr); |
| 3840 | if (ret) |
| 3841 | break; |
| 3842 | } |
| 3843 | if (!ret) |
| 3844 | return 0; |
| 3845 | |
| 3846 | list_for_each_entry_continue_reverse(tbu, list, list) { |
| 3847 | arm_smmu_power_off(tbu->pwr); |
| 3848 | } |
| 3849 | return ret; |
| 3850 | } |
| 3851 | |
| 3852 | static void qsmmuv500_tbu_power_off_all(struct arm_smmu_device *smmu) |
| 3853 | { |
| 3854 | struct qsmmuv500_tbu_device *tbu; |
| 3855 | struct list_head *list = smmu->archdata; |
| 3856 | |
| 3857 | list_for_each_entry_reverse(tbu, list, list) { |
| 3858 | arm_smmu_power_off(tbu->pwr); |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | static int qsmmuv500_tbu_halt(struct qsmmuv500_tbu_device *tbu) |
| 3863 | { |
| 3864 | unsigned long flags; |
| 3865 | u32 val; |
| 3866 | void __iomem *base; |
| 3867 | |
| 3868 | spin_lock_irqsave(&tbu->halt_lock, flags); |
| 3869 | if (tbu->halt_count) { |
| 3870 | tbu->halt_count++; |
| 3871 | spin_unlock_irqrestore(&tbu->halt_lock, flags); |
| 3872 | return 0; |
| 3873 | } |
| 3874 | |
| 3875 | base = tbu->base; |
| 3876 | val = readl_relaxed(base + DEBUG_SID_HALT_REG); |
| 3877 | val |= DEBUG_SID_HALT_VAL; |
| 3878 | writel_relaxed(val, base + DEBUG_SID_HALT_REG); |
| 3879 | |
| 3880 | if (readl_poll_timeout_atomic(base + DEBUG_SR_HALT_ACK_REG, |
| 3881 | val, (val & DEBUG_SR_HALT_ACK_VAL), |
| 3882 | 0, TBU_DBG_TIMEOUT_US)) { |
| 3883 | dev_err(tbu->dev, "Couldn't halt TBU!\n"); |
| 3884 | spin_unlock_irqrestore(&tbu->halt_lock, flags); |
| 3885 | return -ETIMEDOUT; |
| 3886 | } |
| 3887 | |
| 3888 | tbu->halt_count = 1; |
| 3889 | spin_unlock_irqrestore(&tbu->halt_lock, flags); |
| 3890 | return 0; |
| 3891 | } |
| 3892 | |
| 3893 | static void qsmmuv500_tbu_resume(struct qsmmuv500_tbu_device *tbu) |
| 3894 | { |
| 3895 | unsigned long flags; |
| 3896 | u32 val; |
| 3897 | void __iomem *base; |
| 3898 | |
| 3899 | spin_lock_irqsave(&tbu->halt_lock, flags); |
| 3900 | if (!tbu->halt_count) { |
| 3901 | WARN(1, "%s: bad tbu->halt_count", dev_name(tbu->dev)); |
| 3902 | spin_unlock_irqrestore(&tbu->halt_lock, flags); |
| 3903 | return; |
| 3904 | |
| 3905 | } else if (tbu->halt_count > 1) { |
| 3906 | tbu->halt_count--; |
| 3907 | spin_unlock_irqrestore(&tbu->halt_lock, flags); |
| 3908 | return; |
| 3909 | } |
| 3910 | |
| 3911 | base = tbu->base; |
| 3912 | val = readl_relaxed(base + DEBUG_SID_HALT_REG); |
| 3913 | val &= ~DEBUG_SID_HALT_VAL; |
| 3914 | writel_relaxed(val, base + DEBUG_SID_HALT_REG); |
| 3915 | |
| 3916 | tbu->halt_count = 0; |
| 3917 | spin_unlock_irqrestore(&tbu->halt_lock, flags); |
| 3918 | } |
| 3919 | |
| 3920 | static int qsmmuv500_halt_all(struct arm_smmu_device *smmu) |
| 3921 | { |
| 3922 | struct qsmmuv500_tbu_device *tbu; |
| 3923 | struct list_head *list = smmu->archdata; |
| 3924 | int ret = 0; |
| 3925 | |
| 3926 | list_for_each_entry(tbu, list, list) { |
| 3927 | ret = qsmmuv500_tbu_halt(tbu); |
| 3928 | if (ret) |
| 3929 | break; |
| 3930 | } |
| 3931 | |
| 3932 | if (!ret) |
| 3933 | return 0; |
| 3934 | |
| 3935 | list_for_each_entry_continue_reverse(tbu, list, list) { |
| 3936 | qsmmuv500_tbu_resume(tbu); |
| 3937 | } |
| 3938 | return ret; |
| 3939 | } |
| 3940 | |
| 3941 | static void qsmmuv500_resume_all(struct arm_smmu_device *smmu) |
| 3942 | { |
| 3943 | struct qsmmuv500_tbu_device *tbu; |
| 3944 | struct list_head *list = smmu->archdata; |
| 3945 | |
| 3946 | list_for_each_entry(tbu, list, list) { |
| 3947 | qsmmuv500_tbu_resume(tbu); |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | static void qsmmuv500_device_reset(struct arm_smmu_device *smmu) |
| 3952 | { |
| 3953 | int i, ret; |
| 3954 | struct arm_smmu_impl_def_reg *regs = smmu->impl_def_attach_registers; |
| 3955 | |
| 3956 | ret = qsmmuv500_tbu_power_on_all(smmu); |
| 3957 | if (ret) |
| 3958 | return; |
| 3959 | |
| 3960 | /* Program implementation defined registers */ |
| 3961 | qsmmuv500_halt_all(smmu); |
| 3962 | for (i = 0; i < smmu->num_impl_def_attach_registers; ++i) |
| 3963 | writel_relaxed(regs[i].value, |
| 3964 | ARM_SMMU_GR0(smmu) + regs[i].offset); |
| 3965 | qsmmuv500_resume_all(smmu); |
| 3966 | qsmmuv500_tbu_power_off_all(smmu); |
| 3967 | } |
| 3968 | |
| 3969 | static int qsmmuv500_tbu_register(struct device *dev, void *data) |
| 3970 | { |
| 3971 | struct arm_smmu_device *smmu = data; |
| 3972 | struct qsmmuv500_tbu_device *tbu; |
| 3973 | struct list_head *list = smmu->archdata; |
| 3974 | |
| 3975 | if (!dev->driver) { |
| 3976 | dev_err(dev, "TBU failed probe, QSMMUV500 cannot continue!\n"); |
| 3977 | return -EINVAL; |
| 3978 | } |
| 3979 | |
| 3980 | tbu = dev_get_drvdata(dev); |
| 3981 | |
| 3982 | INIT_LIST_HEAD(&tbu->list); |
| 3983 | tbu->smmu = smmu; |
| 3984 | list_add(&tbu->list, list); |
| 3985 | return 0; |
| 3986 | } |
| 3987 | |
| 3988 | static int qsmmuv500_arch_init(struct arm_smmu_device *smmu) |
| 3989 | { |
| 3990 | struct device *dev = smmu->dev; |
| 3991 | struct list_head *list; |
| 3992 | int ret; |
| 3993 | |
| 3994 | list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL); |
| 3995 | if (!list) |
| 3996 | return -ENOMEM; |
| 3997 | |
| 3998 | INIT_LIST_HEAD(list); |
| 3999 | smmu->archdata = list; |
| 4000 | |
| 4001 | ret = of_platform_populate(dev->of_node, NULL, NULL, dev); |
| 4002 | if (ret) |
| 4003 | return ret; |
| 4004 | |
| 4005 | /* Attempt to register child devices */ |
| 4006 | ret = device_for_each_child(dev, smmu, qsmmuv500_tbu_register); |
| 4007 | if (ret) |
| 4008 | return -EINVAL; |
| 4009 | |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
| 4013 | struct arm_smmu_arch_ops qsmmuv500_arch_ops = { |
| 4014 | .init = qsmmuv500_arch_init, |
| 4015 | .device_reset = qsmmuv500_device_reset, |
| 4016 | }; |
| 4017 | |
| 4018 | static const struct of_device_id qsmmuv500_tbu_of_match[] = { |
| 4019 | {.compatible = "qcom,qsmmuv500-tbu"}, |
| 4020 | {} |
| 4021 | }; |
| 4022 | |
| 4023 | static int qsmmuv500_tbu_probe(struct platform_device *pdev) |
| 4024 | { |
| 4025 | struct resource *res; |
| 4026 | struct device *dev = &pdev->dev; |
| 4027 | struct qsmmuv500_tbu_device *tbu; |
| 4028 | |
| 4029 | tbu = devm_kzalloc(dev, sizeof(*tbu), GFP_KERNEL); |
| 4030 | if (!tbu) |
| 4031 | return -ENOMEM; |
| 4032 | |
| 4033 | INIT_LIST_HEAD(&tbu->list); |
| 4034 | tbu->dev = dev; |
| 4035 | spin_lock_init(&tbu->halt_lock); |
| 4036 | |
| 4037 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base"); |
| 4038 | tbu->base = devm_ioremap_resource(dev, res); |
| 4039 | if (IS_ERR(tbu->base)) |
| 4040 | return PTR_ERR(tbu->base); |
| 4041 | |
| 4042 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "status-reg"); |
| 4043 | tbu->status_reg = devm_ioremap_resource(dev, res); |
| 4044 | if (IS_ERR(tbu->status_reg)) |
| 4045 | return PTR_ERR(tbu->status_reg); |
| 4046 | |
| 4047 | tbu->pwr = arm_smmu_init_power_resources(pdev); |
| 4048 | if (IS_ERR(tbu->pwr)) |
| 4049 | return PTR_ERR(tbu->pwr); |
| 4050 | |
| 4051 | dev_set_drvdata(dev, tbu); |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
| 4055 | static struct platform_driver qsmmuv500_tbu_driver = { |
| 4056 | .driver = { |
| 4057 | .name = "qsmmuv500-tbu", |
| 4058 | .of_match_table = of_match_ptr(qsmmuv500_tbu_of_match), |
| 4059 | }, |
| 4060 | .probe = qsmmuv500_tbu_probe, |
| 4061 | }; |
| 4062 | |
| 4063 | static int __init qsmmuv500_tbu_init(void) |
| 4064 | { |
| 4065 | return platform_driver_register(&qsmmuv500_tbu_driver); |
| 4066 | } |
| 4067 | subsys_initcall(qsmmuv500_tbu_init); |
| 4068 | |
Will Deacon | 45ae7cf | 2013-06-24 18:31:25 +0100 | [diff] [blame] | 4069 | MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations"); |
| 4070 | MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>"); |
| 4071 | MODULE_LICENSE("GPL v2"); |