]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/iommu/arm-smmu.c
UBUNTU: Ubuntu-raspi2-4.10.0-1011.14
[mirror_ubuntu-zesty-kernel.git] / drivers / iommu / arm-smmu.c
CommitLineData
45ae7cff
WD
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
45ae7cff 26 * - Context fault reporting
2c8a6aa0 27 * - Extended Stream ID (16 bit)
45ae7cff
WD
28 */
29
30#define pr_fmt(fmt) "arm-smmu: " fmt
31
d6fcd3b1
LP
32#include <linux/acpi.h>
33#include <linux/acpi_iort.h>
1f3d5ca4 34#include <linux/atomic.h>
45ae7cff 35#include <linux/delay.h>
9adb9594 36#include <linux/dma-iommu.h>
45ae7cff
WD
37#include <linux/dma-mapping.h>
38#include <linux/err.h>
39#include <linux/interrupt.h>
40#include <linux/io.h>
f9a05f05 41#include <linux/io-64-nonatomic-hi-lo.h>
45ae7cff 42#include <linux/iommu.h>
859a732e 43#include <linux/iopoll.h>
45ae7cff
WD
44#include <linux/module.h>
45#include <linux/of.h>
bae2c2d4 46#include <linux/of_address.h>
d6fc5d97 47#include <linux/of_device.h>
adfec2e7 48#include <linux/of_iommu.h>
a9a1b0b5 49#include <linux/pci.h>
45ae7cff
WD
50#include <linux/platform_device.h>
51#include <linux/slab.h>
52#include <linux/spinlock.h>
53
54#include <linux/amba/bus.h>
55
518f7136 56#include "io-pgtable.h"
45ae7cff 57
45ae7cff
WD
58/* Maximum number of context banks per SMMU */
59#define ARM_SMMU_MAX_CBS 128
60
45ae7cff
WD
61/* SMMU global address space */
62#define ARM_SMMU_GR0(smmu) ((smmu)->base)
c757e852 63#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
45ae7cff 64
3a5df8ff
AH
65/*
66 * SMMU global address space with conditional offset to access secure
67 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
68 * nsGFSYNR0: 0x450)
69 */
70#define ARM_SMMU_GR0_NS(smmu) \
71 ((smmu)->base + \
72 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
73 ? 0x400 : 0))
74
f9a05f05
RM
75/*
76 * Some 64-bit registers only make sense to write atomically, but in such
77 * cases all the data relevant to AArch32 formats lies within the lower word,
78 * therefore this actually makes more sense than it might first appear.
79 */
668b4ada 80#ifdef CONFIG_64BIT
f9a05f05 81#define smmu_write_atomic_lq writeq_relaxed
668b4ada 82#else
f9a05f05 83#define smmu_write_atomic_lq writel_relaxed
668b4ada
TC
84#endif
85
45ae7cff
WD
86/* Configuration registers */
87#define ARM_SMMU_GR0_sCR0 0x0
88#define sCR0_CLIENTPD (1 << 0)
89#define sCR0_GFRE (1 << 1)
90#define sCR0_GFIE (1 << 2)
2c8a6aa0 91#define sCR0_EXIDENABLE (1 << 3)
45ae7cff
WD
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)
4e3e9b69 98#define sCR0_VMID16EN (1 << 31)
45ae7cff
WD
99#define sCR0_BSU_SHIFT 14
100#define sCR0_BSU_MASK 0x3
101
3ca3712a
PF
102/* Auxiliary Configuration register */
103#define ARM_SMMU_GR0_sACR 0x10
104
45ae7cff
WD
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
45ae7cff
WD
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)
859a732e 123#define ID0_ATOSNS (1 << 26)
7602b871
RM
124#define ID0_PTFS_NO_AARCH32 (1 << 25)
125#define ID0_PTFS_NO_AARCH32S (1 << 24)
45ae7cff
WD
126#define ID0_CTTW (1 << 14)
127#define ID0_NUMIRPT_SHIFT 16
128#define ID0_NUMIRPT_MASK 0xff
3c8766d0
OH
129#define ID0_NUMSIDB_SHIFT 9
130#define ID0_NUMSIDB_MASK 0xf
2c8a6aa0 131#define ID0_EXIDS (1 << 8)
45ae7cff
WD
132#define ID0_NUMSMRG_SHIFT 0
133#define ID0_NUMSMRG_MASK 0xff
134
135#define ID1_PAGESIZE (1 << 31)
136#define ID1_NUMPAGENDXB_SHIFT 28
137#define ID1_NUMPAGENDXB_MASK 7
138#define ID1_NUMS2CB_SHIFT 16
139#define ID1_NUMS2CB_MASK 0xff
140#define ID1_NUMCB_SHIFT 0
141#define ID1_NUMCB_MASK 0xff
142
143#define ID2_OAS_SHIFT 4
144#define ID2_OAS_MASK 0xf
145#define ID2_IAS_SHIFT 0
146#define ID2_IAS_MASK 0xf
147#define ID2_UBS_SHIFT 8
148#define ID2_UBS_MASK 0xf
149#define ID2_PTFS_4K (1 << 12)
150#define ID2_PTFS_16K (1 << 13)
151#define ID2_PTFS_64K (1 << 14)
4e3e9b69 152#define ID2_VMID16 (1 << 15)
45ae7cff 153
3ca3712a
PF
154#define ID7_MAJOR_SHIFT 4
155#define ID7_MAJOR_MASK 0xf
45ae7cff 156
45ae7cff 157/* Global TLB invalidation */
45ae7cff
WD
158#define ARM_SMMU_GR0_TLBIVMID 0x64
159#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
160#define ARM_SMMU_GR0_TLBIALLH 0x6c
161#define ARM_SMMU_GR0_sTLBGSYNC 0x70
162#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
163#define sTLBGSTATUS_GSACTIVE (1 << 0)
164#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
165
166/* Stream mapping registers */
167#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
168#define SMR_VALID (1 << 31)
169#define SMR_MASK_SHIFT 16
45ae7cff 170#define SMR_ID_SHIFT 0
45ae7cff
WD
171
172#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
173#define S2CR_CBNDX_SHIFT 0
174#define S2CR_CBNDX_MASK 0xff
2c8a6aa0 175#define S2CR_EXIDVALID (1 << 10)
45ae7cff
WD
176#define S2CR_TYPE_SHIFT 16
177#define S2CR_TYPE_MASK 0x3
8e8b203e
RM
178enum arm_smmu_s2cr_type {
179 S2CR_TYPE_TRANS,
180 S2CR_TYPE_BYPASS,
181 S2CR_TYPE_FAULT,
182};
45ae7cff 183
d346180e 184#define S2CR_PRIVCFG_SHIFT 24
8e8b203e
RM
185#define S2CR_PRIVCFG_MASK 0x3
186enum arm_smmu_s2cr_privcfg {
187 S2CR_PRIVCFG_DEFAULT,
188 S2CR_PRIVCFG_DIPAN,
189 S2CR_PRIVCFG_UNPRIV,
190 S2CR_PRIVCFG_PRIV,
191};
d346180e 192
45ae7cff
WD
193/* Context bank attribute registers */
194#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
195#define CBAR_VMID_SHIFT 0
196#define CBAR_VMID_MASK 0xff
57ca90f6
WD
197#define CBAR_S1_BPSHCFG_SHIFT 8
198#define CBAR_S1_BPSHCFG_MASK 3
199#define CBAR_S1_BPSHCFG_NSH 3
45ae7cff
WD
200#define CBAR_S1_MEMATTR_SHIFT 12
201#define CBAR_S1_MEMATTR_MASK 0xf
202#define CBAR_S1_MEMATTR_WB 0xf
203#define CBAR_TYPE_SHIFT 16
204#define CBAR_TYPE_MASK 0x3
205#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
206#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
207#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
208#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
209#define CBAR_IRPTNDX_SHIFT 24
210#define CBAR_IRPTNDX_MASK 0xff
211
212#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
213#define CBA2R_RW64_32BIT (0 << 0)
214#define CBA2R_RW64_64BIT (1 << 0)
4e3e9b69
TC
215#define CBA2R_VMID_SHIFT 16
216#define CBA2R_VMID_MASK 0xffff
45ae7cff
WD
217
218/* Translation context bank */
219#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
c757e852 220#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
45ae7cff
WD
221
222#define ARM_SMMU_CB_SCTLR 0x0
f0cfffc4 223#define ARM_SMMU_CB_ACTLR 0x4
45ae7cff
WD
224#define ARM_SMMU_CB_RESUME 0x8
225#define ARM_SMMU_CB_TTBCR2 0x10
668b4ada
TC
226#define ARM_SMMU_CB_TTBR0 0x20
227#define ARM_SMMU_CB_TTBR1 0x28
45ae7cff 228#define ARM_SMMU_CB_TTBCR 0x30
6070529b 229#define ARM_SMMU_CB_CONTEXTIDR 0x34
45ae7cff 230#define ARM_SMMU_CB_S1_MAIR0 0x38
518f7136 231#define ARM_SMMU_CB_S1_MAIR1 0x3c
f9a05f05 232#define ARM_SMMU_CB_PAR 0x50
45ae7cff 233#define ARM_SMMU_CB_FSR 0x58
f9a05f05 234#define ARM_SMMU_CB_FAR 0x60
45ae7cff 235#define ARM_SMMU_CB_FSYNR0 0x68
518f7136 236#define ARM_SMMU_CB_S1_TLBIVA 0x600
1463fe44 237#define ARM_SMMU_CB_S1_TLBIASID 0x610
518f7136
WD
238#define ARM_SMMU_CB_S1_TLBIVAL 0x620
239#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
240#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
661d962f 241#define ARM_SMMU_CB_ATS1PR 0x800
859a732e 242#define ARM_SMMU_CB_ATSR 0x8f0
45ae7cff
WD
243
244#define SCTLR_S1_ASIDPNE (1 << 12)
245#define SCTLR_CFCFG (1 << 7)
246#define SCTLR_CFIE (1 << 6)
247#define SCTLR_CFRE (1 << 5)
248#define SCTLR_E (1 << 4)
249#define SCTLR_AFE (1 << 2)
250#define SCTLR_TRE (1 << 1)
251#define SCTLR_M (1 << 0)
45ae7cff 252
f0cfffc4
RM
253#define ARM_MMU500_ACTLR_CPRE (1 << 1)
254
3ca3712a 255#define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
6eb18d4a 256#define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8)
3ca3712a 257
859a732e
MH
258#define CB_PAR_F (1 << 0)
259
260#define ATSR_ACTIVE (1 << 0)
261
45ae7cff
WD
262#define RESUME_RETRY (0 << 0)
263#define RESUME_TERMINATE (1 << 0)
264
45ae7cff 265#define TTBCR2_SEP_SHIFT 15
5dc5616e 266#define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
9e61bdf4 267#define TTBCR2_AS (1 << 4)
45ae7cff 268
668b4ada 269#define TTBRn_ASID_SHIFT 48
45ae7cff
WD
270
271#define FSR_MULTI (1 << 31)
272#define FSR_SS (1 << 30)
273#define FSR_UUT (1 << 8)
274#define FSR_ASF (1 << 7)
275#define FSR_TLBLKF (1 << 6)
276#define FSR_TLBMCF (1 << 5)
277#define FSR_EF (1 << 4)
278#define FSR_PF (1 << 3)
279#define FSR_AFF (1 << 2)
280#define FSR_TF (1 << 1)
281
2907320d
MH
282#define FSR_IGN (FSR_AFF | FSR_ASF | \
283 FSR_TLBMCF | FSR_TLBLKF)
284#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
adaba320 285 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
45ae7cff
WD
286
287#define FSYNR0_WNR (1 << 4)
288
b8a07ed7
EA
289#define MSI_IOVA_BASE 0x8000000
290#define MSI_IOVA_LENGTH 0x100000
291
4cf740b0 292static int force_stage;
25a1c96c 293module_param(force_stage, int, S_IRUGO);
4cf740b0
WD
294MODULE_PARM_DESC(force_stage,
295 "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.");
25a1c96c
RM
296static bool disable_bypass;
297module_param(disable_bypass, bool, S_IRUGO);
298MODULE_PARM_DESC(disable_bypass,
299 "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.");
4cf740b0 300
09360403 301enum arm_smmu_arch_version {
b7862e35
RM
302 ARM_SMMU_V1,
303 ARM_SMMU_V1_64K,
09360403
RM
304 ARM_SMMU_V2,
305};
306
67b65a3f
RM
307enum arm_smmu_implementation {
308 GENERIC_SMMU,
f0cfffc4 309 ARM_MMU500,
e086d912 310 CAVIUM_SMMUV2,
67b65a3f
RM
311};
312
f6b14b3c
RM
313/* Until ACPICA headers cover IORT rev. C */
314#ifndef ACPI_IORT_SMMU_CORELINK_MMU401
315#define ACPI_IORT_SMMU_CORELINK_MMU401 0x4
316#endif
317#ifndef ACPI_IORT_SMMU_CAVIUM_THUNDERX
318#define ACPI_IORT_SMMU_CAVIUM_THUNDERX 0x5
319#endif
320
8e8b203e 321struct arm_smmu_s2cr {
588888a7
RM
322 struct iommu_group *group;
323 int count;
8e8b203e
RM
324 enum arm_smmu_s2cr_type type;
325 enum arm_smmu_s2cr_privcfg privcfg;
326 u8 cbndx;
327};
328
329#define s2cr_init_val (struct arm_smmu_s2cr){ \
330 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
331}
332
45ae7cff 333struct arm_smmu_smr {
45ae7cff
WD
334 u16 mask;
335 u16 id;
1f3d5ca4 336 bool valid;
45ae7cff
WD
337};
338
a9a1b0b5 339struct arm_smmu_master_cfg {
f80cd885 340 struct arm_smmu_device *smmu;
adfec2e7 341 s16 smendx[];
45ae7cff 342};
1f3d5ca4 343#define INVALID_SMENDX -1
adfec2e7
RM
344#define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
345#define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
8c82d6ec
RM
346#define fwspec_smendx(fw, i) \
347 (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
adfec2e7 348#define for_each_cfg_sme(fw, i, idx) \
8c82d6ec 349 for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
45ae7cff
WD
350
351struct arm_smmu_device {
352 struct device *dev;
45ae7cff
WD
353
354 void __iomem *base;
355 unsigned long size;
c757e852 356 unsigned long pgshift;
45ae7cff
WD
357
358#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
359#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
360#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
361#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
362#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
859a732e 363#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
4e3e9b69 364#define ARM_SMMU_FEAT_VMID16 (1 << 6)
7602b871
RM
365#define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
366#define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
367#define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
368#define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
369#define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
2c8a6aa0 370#define ARM_SMMU_FEAT_EXIDS (1 << 12)
45ae7cff 371 u32 features;
3a5df8ff
AH
372
373#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
374 u32 options;
09360403 375 enum arm_smmu_arch_version version;
67b65a3f 376 enum arm_smmu_implementation model;
45ae7cff
WD
377
378 u32 num_context_banks;
379 u32 num_s2_context_banks;
380 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
381 atomic_t irptndx;
382
383 u32 num_mapping_groups;
21174240
RM
384 u16 streamid_mask;
385 u16 smr_mask_mask;
1f3d5ca4 386 struct arm_smmu_smr *smrs;
8e8b203e 387 struct arm_smmu_s2cr *s2crs;
588888a7 388 struct mutex stream_map_mutex;
45ae7cff 389
518f7136
WD
390 unsigned long va_size;
391 unsigned long ipa_size;
392 unsigned long pa_size;
d5466357 393 unsigned long pgsize_bitmap;
45ae7cff
WD
394
395 u32 num_global_irqs;
396 u32 num_context_irqs;
397 unsigned int *irqs;
398
1bd37a68 399 u32 cavium_id_base; /* Specific to Cavium */
17554084
JR
400
401 /* IOMMU core code handle */
402 struct iommu_device iommu;
45ae7cff
WD
403};
404
7602b871
RM
405enum arm_smmu_context_fmt {
406 ARM_SMMU_CTX_FMT_NONE,
407 ARM_SMMU_CTX_FMT_AARCH64,
408 ARM_SMMU_CTX_FMT_AARCH32_L,
409 ARM_SMMU_CTX_FMT_AARCH32_S,
45ae7cff
WD
410};
411
412struct arm_smmu_cfg {
45ae7cff
WD
413 u8 cbndx;
414 u8 irptndx;
415 u32 cbar;
7602b871 416 enum arm_smmu_context_fmt fmt;
45ae7cff 417};
faea13b7 418#define INVALID_IRPTNDX 0xff
45ae7cff 419
1bd37a68
TC
420#define ARM_SMMU_CB_ASID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx)
421#define ARM_SMMU_CB_VMID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx + 1)
ecfadb6e 422
c752ce45
WD
423enum arm_smmu_domain_stage {
424 ARM_SMMU_DOMAIN_S1 = 0,
425 ARM_SMMU_DOMAIN_S2,
426 ARM_SMMU_DOMAIN_NESTED,
819b6dfa 427 ARM_SMMU_DOMAIN_BYPASS,
c752ce45
WD
428};
429
45ae7cff 430struct arm_smmu_domain {
44680eed 431 struct arm_smmu_device *smmu;
518f7136
WD
432 struct io_pgtable_ops *pgtbl_ops;
433 spinlock_t pgtbl_lock;
44680eed 434 struct arm_smmu_cfg cfg;
c752ce45 435 enum arm_smmu_domain_stage stage;
518f7136 436 struct mutex init_mutex; /* Protects smmu pointer */
1d672638 437 struct iommu_domain domain;
45ae7cff
WD
438};
439
3a5df8ff
AH
440struct arm_smmu_option_prop {
441 u32 opt;
442 const char *prop;
443};
444
1bd37a68
TC
445static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
446
021bb842
RM
447static bool using_legacy_binding, using_generic_binding;
448
2907320d 449static struct arm_smmu_option_prop arm_smmu_options[] = {
3a5df8ff
AH
450 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
451 { 0, NULL},
452};
453
1d672638
JR
454static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
455{
456 return container_of(dom, struct arm_smmu_domain, domain);
457}
458
3a5df8ff
AH
459static void parse_driver_options(struct arm_smmu_device *smmu)
460{
461 int i = 0;
2907320d 462
3a5df8ff
AH
463 do {
464 if (of_property_read_bool(smmu->dev->of_node,
465 arm_smmu_options[i].prop)) {
466 smmu->options |= arm_smmu_options[i].opt;
467 dev_notice(smmu->dev, "option %s\n",
468 arm_smmu_options[i].prop);
469 }
470 } while (arm_smmu_options[++i].opt);
471}
472
8f68f8e2 473static struct device_node *dev_get_dev_node(struct device *dev)
a9a1b0b5
WD
474{
475 if (dev_is_pci(dev)) {
476 struct pci_bus *bus = to_pci_dev(dev)->bus;
2907320d 477
a9a1b0b5
WD
478 while (!pci_is_root_bus(bus))
479 bus = bus->parent;
f80cd885 480 return of_node_get(bus->bridge->parent->of_node);
a9a1b0b5
WD
481 }
482
f80cd885 483 return of_node_get(dev->of_node);
a9a1b0b5
WD
484}
485
f80cd885 486static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
45ae7cff 487{
f80cd885
RM
488 *((__be32 *)data) = cpu_to_be32(alias);
489 return 0; /* Continue walking */
45ae7cff
WD
490}
491
f80cd885 492static int __find_legacy_master_phandle(struct device *dev, void *data)
a9a1b0b5 493{
f80cd885
RM
494 struct of_phandle_iterator *it = *(void **)data;
495 struct device_node *np = it->node;
496 int err;
497
498 of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
499 "#stream-id-cells", 0)
500 if (it->node == np) {
501 *(void **)data = dev;
502 return 1;
503 }
504 it->node = np;
505 return err == -ENOENT ? 0 : err;
a9a1b0b5
WD
506}
507
d6fc5d97 508static struct platform_driver arm_smmu_driver;
adfec2e7 509static struct iommu_ops arm_smmu_ops;
d6fc5d97 510
adfec2e7
RM
511static int arm_smmu_register_legacy_master(struct device *dev,
512 struct arm_smmu_device **smmu)
45ae7cff 513{
adfec2e7 514 struct device *smmu_dev;
f80cd885
RM
515 struct device_node *np;
516 struct of_phandle_iterator it;
517 void *data = &it;
adfec2e7 518 u32 *sids;
f80cd885
RM
519 __be32 pci_sid;
520 int err;
45ae7cff 521
f80cd885
RM
522 np = dev_get_dev_node(dev);
523 if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
524 of_node_put(np);
525 return -ENODEV;
526 }
45ae7cff 527
f80cd885 528 it.node = np;
d6fc5d97
RM
529 err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
530 __find_legacy_master_phandle);
adfec2e7 531 smmu_dev = data;
f80cd885
RM
532 of_node_put(np);
533 if (err == 0)
534 return -ENODEV;
535 if (err < 0)
536 return err;
45ae7cff 537
f80cd885
RM
538 if (dev_is_pci(dev)) {
539 /* "mmu-masters" assumes Stream ID == Requester ID */
540 pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
541 &pci_sid);
542 it.cur = &pci_sid;
543 it.cur_count = 1;
544 }
45ae7cff 545
adfec2e7
RM
546 err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
547 &arm_smmu_ops);
548 if (err)
549 return err;
45ae7cff 550
adfec2e7
RM
551 sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
552 if (!sids)
553 return -ENOMEM;
44680eed 554
adfec2e7
RM
555 *smmu = dev_get_drvdata(smmu_dev);
556 of_phandle_iterator_args(&it, sids, it.cur_count);
557 err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
558 kfree(sids);
559 return err;
45ae7cff
WD
560}
561
562static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
563{
564 int idx;
565
566 do {
567 idx = find_next_zero_bit(map, end, start);
568 if (idx == end)
569 return -ENOSPC;
570 } while (test_and_set_bit(idx, map));
571
572 return idx;
573}
574
575static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
576{
577 clear_bit(idx, map);
578}
579
580/* Wait for any pending TLB invalidations to complete */
518f7136 581static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
45ae7cff
WD
582{
583 int count = 0;
584 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
585
586 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
587 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
588 & sTLBGSTATUS_GSACTIVE) {
589 cpu_relax();
590 if (++count == TLB_LOOP_TIMEOUT) {
591 dev_err_ratelimited(smmu->dev,
592 "TLB sync timed out -- SMMU may be deadlocked\n");
593 return;
594 }
595 udelay(1);
596 }
597}
598
518f7136
WD
599static void arm_smmu_tlb_sync(void *cookie)
600{
601 struct arm_smmu_domain *smmu_domain = cookie;
602 __arm_smmu_tlb_sync(smmu_domain->smmu);
603}
604
605static void arm_smmu_tlb_inv_context(void *cookie)
1463fe44 606{
518f7136 607 struct arm_smmu_domain *smmu_domain = cookie;
44680eed
WD
608 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
609 struct arm_smmu_device *smmu = smmu_domain->smmu;
1463fe44 610 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
518f7136 611 void __iomem *base;
1463fe44
WD
612
613 if (stage1) {
614 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1bd37a68 615 writel_relaxed(ARM_SMMU_CB_ASID(smmu, cfg),
ecfadb6e 616 base + ARM_SMMU_CB_S1_TLBIASID);
1463fe44
WD
617 } else {
618 base = ARM_SMMU_GR0(smmu);
1bd37a68 619 writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg),
ecfadb6e 620 base + ARM_SMMU_GR0_TLBIVMID);
1463fe44
WD
621 }
622
518f7136
WD
623 __arm_smmu_tlb_sync(smmu);
624}
625
626static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
06c610e8 627 size_t granule, bool leaf, void *cookie)
518f7136
WD
628{
629 struct arm_smmu_domain *smmu_domain = cookie;
630 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
631 struct arm_smmu_device *smmu = smmu_domain->smmu;
632 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
633 void __iomem *reg;
634
635 if (stage1) {
636 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
637 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
638
7602b871 639 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
518f7136 640 iova &= ~12UL;
1bd37a68 641 iova |= ARM_SMMU_CB_ASID(smmu, cfg);
75df1386
RM
642 do {
643 writel_relaxed(iova, reg);
644 iova += granule;
645 } while (size -= granule);
518f7136
WD
646 } else {
647 iova >>= 12;
1bd37a68 648 iova |= (u64)ARM_SMMU_CB_ASID(smmu, cfg) << 48;
75df1386
RM
649 do {
650 writeq_relaxed(iova, reg);
651 iova += granule >> 12;
652 } while (size -= granule);
518f7136 653 }
518f7136
WD
654 } else if (smmu->version == ARM_SMMU_V2) {
655 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
656 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
657 ARM_SMMU_CB_S2_TLBIIPAS2;
75df1386
RM
658 iova >>= 12;
659 do {
f9a05f05 660 smmu_write_atomic_lq(iova, reg);
75df1386
RM
661 iova += granule >> 12;
662 } while (size -= granule);
518f7136
WD
663 } else {
664 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
1bd37a68 665 writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), reg);
518f7136
WD
666 }
667}
668
5896f3a3 669static const struct iommu_gather_ops arm_smmu_gather_ops = {
518f7136
WD
670 .tlb_flush_all = arm_smmu_tlb_inv_context,
671 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
672 .tlb_sync = arm_smmu_tlb_sync,
518f7136
WD
673};
674
45ae7cff
WD
675static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
676{
3714ce1d 677 u32 fsr, fsynr;
45ae7cff
WD
678 unsigned long iova;
679 struct iommu_domain *domain = dev;
1d672638 680 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed
WD
681 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
682 struct arm_smmu_device *smmu = smmu_domain->smmu;
45ae7cff
WD
683 void __iomem *cb_base;
684
44680eed 685 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
45ae7cff
WD
686 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
687
688 if (!(fsr & FSR_FAULT))
689 return IRQ_NONE;
690
45ae7cff 691 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
f9a05f05 692 iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR);
45ae7cff 693
3714ce1d
WD
694 dev_err_ratelimited(smmu->dev,
695 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cb=%d\n",
696 fsr, iova, fsynr, cfg->cbndx);
45ae7cff 697
3714ce1d
WD
698 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
699 return IRQ_HANDLED;
45ae7cff
WD
700}
701
702static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
703{
704 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
705 struct arm_smmu_device *smmu = dev;
3a5df8ff 706 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
45ae7cff
WD
707
708 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
709 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
710 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
711 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
712
3a5df8ff
AH
713 if (!gfsr)
714 return IRQ_NONE;
715
45ae7cff
WD
716 dev_err_ratelimited(smmu->dev,
717 "Unexpected global fault, this could be serious\n");
718 dev_err_ratelimited(smmu->dev,
719 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
720 gfsr, gfsynr0, gfsynr1, gfsynr2);
721
722 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
adaba320 723 return IRQ_HANDLED;
45ae7cff
WD
724}
725
518f7136
WD
726static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
727 struct io_pgtable_cfg *pgtbl_cfg)
45ae7cff 728{
6070529b 729 u32 reg, reg2;
668b4ada 730 u64 reg64;
45ae7cff 731 bool stage1;
44680eed
WD
732 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
733 struct arm_smmu_device *smmu = smmu_domain->smmu;
c88ae5de 734 void __iomem *cb_base, *gr1_base;
45ae7cff 735
45ae7cff 736 gr1_base = ARM_SMMU_GR1(smmu);
44680eed
WD
737 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
738 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
45ae7cff 739
4a1c93cb 740 if (smmu->version > ARM_SMMU_V1) {
7602b871
RM
741 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
742 reg = CBA2R_RW64_64BIT;
743 else
744 reg = CBA2R_RW64_32BIT;
4e3e9b69
TC
745 /* 16-bit VMIDs live in CBA2R */
746 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1bd37a68 747 reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBA2R_VMID_SHIFT;
4e3e9b69 748
4a1c93cb
WD
749 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
750 }
751
45ae7cff 752 /* CBAR */
44680eed 753 reg = cfg->cbar;
b7862e35 754 if (smmu->version < ARM_SMMU_V2)
2907320d 755 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
45ae7cff 756
57ca90f6
WD
757 /*
758 * Use the weakest shareability/memory types, so they are
759 * overridden by the ttbcr/pte.
760 */
761 if (stage1) {
762 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
763 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
4e3e9b69
TC
764 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
765 /* 8-bit VMIDs live in CBAR */
1bd37a68 766 reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBAR_VMID_SHIFT;
57ca90f6 767 }
44680eed 768 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
45ae7cff 769
518f7136
WD
770 /* TTBRs */
771 if (stage1) {
6070529b
RM
772 u16 asid = ARM_SMMU_CB_ASID(smmu, cfg);
773
774 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
775 reg = pgtbl_cfg->arm_v7s_cfg.ttbr[0];
776 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0);
777 reg = pgtbl_cfg->arm_v7s_cfg.ttbr[1];
778 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1);
779 writel_relaxed(asid, cb_base + ARM_SMMU_CB_CONTEXTIDR);
780 } else {
781 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
782 reg64 |= (u64)asid << TTBRn_ASID_SHIFT;
783 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
784 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
785 reg64 |= (u64)asid << TTBRn_ASID_SHIFT;
786 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR1);
787 }
518f7136 788 } else {
668b4ada 789 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
f9a05f05 790 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
518f7136 791 }
a65217a4 792
518f7136
WD
793 /* TTBCR */
794 if (stage1) {
6070529b
RM
795 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
796 reg = pgtbl_cfg->arm_v7s_cfg.tcr;
797 reg2 = 0;
798 } else {
799 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
800 reg2 = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
801 reg2 |= TTBCR2_SEP_UPSTREAM;
9e61bdf4
TN
802 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
803 reg2 |= TTBCR2_AS;
45ae7cff 804 }
6070529b
RM
805 if (smmu->version > ARM_SMMU_V1)
806 writel_relaxed(reg2, cb_base + ARM_SMMU_CB_TTBCR2);
45ae7cff 807 } else {
518f7136 808 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
45ae7cff 809 }
6070529b 810 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
45ae7cff 811
518f7136 812 /* MAIRs (stage-1 only) */
45ae7cff 813 if (stage1) {
6070529b
RM
814 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
815 reg = pgtbl_cfg->arm_v7s_cfg.prrr;
816 reg2 = pgtbl_cfg->arm_v7s_cfg.nmrr;
817 } else {
818 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
819 reg2 = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
820 }
45ae7cff 821 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
6070529b 822 writel_relaxed(reg2, cb_base + ARM_SMMU_CB_S1_MAIR1);
45ae7cff
WD
823 }
824
45ae7cff 825 /* SCTLR */
6070529b 826 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | SCTLR_M;
45ae7cff
WD
827 if (stage1)
828 reg |= SCTLR_S1_ASIDPNE;
829#ifdef __BIG_ENDIAN
830 reg |= SCTLR_E;
831#endif
25724841 832 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
45ae7cff
WD
833}
834
835static int arm_smmu_init_domain_context(struct iommu_domain *domain,
44680eed 836 struct arm_smmu_device *smmu)
45ae7cff 837{
a18037b2 838 int irq, start, ret = 0;
518f7136
WD
839 unsigned long ias, oas;
840 struct io_pgtable_ops *pgtbl_ops;
841 struct io_pgtable_cfg pgtbl_cfg;
842 enum io_pgtable_fmt fmt;
1d672638 843 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed 844 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
45ae7cff 845
518f7136 846 mutex_lock(&smmu_domain->init_mutex);
a18037b2
MH
847 if (smmu_domain->smmu)
848 goto out_unlock;
849
819b6dfa
WD
850 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
851 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
852 smmu_domain->smmu = smmu;
853 goto out_unlock;
854 }
855
c752ce45
WD
856 /*
857 * Mapping the requested stage onto what we support is surprisingly
858 * complicated, mainly because the spec allows S1+S2 SMMUs without
859 * support for nested translation. That means we end up with the
860 * following table:
861 *
862 * Requested Supported Actual
863 * S1 N S1
864 * S1 S1+S2 S1
865 * S1 S2 S2
866 * S1 S1 S1
867 * N N N
868 * N S1+S2 S2
869 * N S2 S2
870 * N S1 S1
871 *
872 * Note that you can't actually request stage-2 mappings.
873 */
874 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
875 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
876 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
877 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
878
7602b871
RM
879 /*
880 * Choosing a suitable context format is even more fiddly. Until we
881 * grow some way for the caller to express a preference, and/or move
882 * the decision into the io-pgtable code where it arguably belongs,
883 * just aim for the closest thing to the rest of the system, and hope
884 * that the hardware isn't esoteric enough that we can't assume AArch64
885 * support to be a superset of AArch32 support...
886 */
887 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
888 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
6070529b
RM
889 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
890 !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
891 (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
892 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
893 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
7602b871
RM
894 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
895 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
896 ARM_SMMU_FEAT_FMT_AARCH64_16K |
897 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
898 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
899
900 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
901 ret = -EINVAL;
902 goto out_unlock;
903 }
904
c752ce45
WD
905 switch (smmu_domain->stage) {
906 case ARM_SMMU_DOMAIN_S1:
907 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
908 start = smmu->num_s2_context_banks;
518f7136
WD
909 ias = smmu->va_size;
910 oas = smmu->ipa_size;
7602b871 911 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
518f7136 912 fmt = ARM_64_LPAE_S1;
6070529b 913 } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
518f7136 914 fmt = ARM_32_LPAE_S1;
7602b871
RM
915 ias = min(ias, 32UL);
916 oas = min(oas, 40UL);
6070529b
RM
917 } else {
918 fmt = ARM_V7S;
919 ias = min(ias, 32UL);
920 oas = min(oas, 32UL);
7602b871 921 }
c752ce45
WD
922 break;
923 case ARM_SMMU_DOMAIN_NESTED:
45ae7cff
WD
924 /*
925 * We will likely want to change this if/when KVM gets
926 * involved.
927 */
c752ce45 928 case ARM_SMMU_DOMAIN_S2:
9c5c92e3
WD
929 cfg->cbar = CBAR_TYPE_S2_TRANS;
930 start = 0;
518f7136
WD
931 ias = smmu->ipa_size;
932 oas = smmu->pa_size;
7602b871 933 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
518f7136 934 fmt = ARM_64_LPAE_S2;
7602b871 935 } else {
518f7136 936 fmt = ARM_32_LPAE_S2;
7602b871
RM
937 ias = min(ias, 40UL);
938 oas = min(oas, 40UL);
939 }
c752ce45
WD
940 break;
941 default:
942 ret = -EINVAL;
943 goto out_unlock;
45ae7cff
WD
944 }
945
946 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
947 smmu->num_context_banks);
287980e4 948 if (ret < 0)
a18037b2 949 goto out_unlock;
45ae7cff 950
44680eed 951 cfg->cbndx = ret;
b7862e35 952 if (smmu->version < ARM_SMMU_V2) {
44680eed
WD
953 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
954 cfg->irptndx %= smmu->num_context_irqs;
45ae7cff 955 } else {
44680eed 956 cfg->irptndx = cfg->cbndx;
45ae7cff
WD
957 }
958
518f7136 959 pgtbl_cfg = (struct io_pgtable_cfg) {
d5466357 960 .pgsize_bitmap = smmu->pgsize_bitmap,
518f7136
WD
961 .ias = ias,
962 .oas = oas,
963 .tlb = &arm_smmu_gather_ops,
2df7a25c 964 .iommu_dev = smmu->dev,
518f7136
WD
965 };
966
967 smmu_domain->smmu = smmu;
968 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
969 if (!pgtbl_ops) {
970 ret = -ENOMEM;
971 goto out_clear_smmu;
972 }
973
d5466357
RM
974 /* Update the domain's page sizes to reflect the page table format */
975 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
455eb7d3
RM
976 domain->geometry.aperture_end = (1UL << ias) - 1;
977 domain->geometry.force_aperture = true;
a18037b2 978
518f7136
WD
979 /* Initialise the context bank with our page table cfg */
980 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
981
982 /*
983 * Request context fault interrupt. Do this last to avoid the
984 * handler seeing a half-initialised domain state.
985 */
44680eed 986 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
bee14004
PF
987 ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
988 IRQF_SHARED, "arm-smmu-context-fault", domain);
287980e4 989 if (ret < 0) {
45ae7cff 990 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
44680eed
WD
991 cfg->irptndx, irq);
992 cfg->irptndx = INVALID_IRPTNDX;
45ae7cff
WD
993 }
994
518f7136
WD
995 mutex_unlock(&smmu_domain->init_mutex);
996
997 /* Publish page table ops for map/unmap */
998 smmu_domain->pgtbl_ops = pgtbl_ops;
a9a1b0b5 999 return 0;
45ae7cff 1000
518f7136
WD
1001out_clear_smmu:
1002 smmu_domain->smmu = NULL;
a18037b2 1003out_unlock:
518f7136 1004 mutex_unlock(&smmu_domain->init_mutex);
45ae7cff
WD
1005 return ret;
1006}
1007
1008static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
1009{
1d672638 1010 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed
WD
1011 struct arm_smmu_device *smmu = smmu_domain->smmu;
1012 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1463fe44 1013 void __iomem *cb_base;
45ae7cff
WD
1014 int irq;
1015
819b6dfa 1016 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
45ae7cff
WD
1017 return;
1018
518f7136
WD
1019 /*
1020 * Disable the context bank and free the page tables before freeing
1021 * it.
1022 */
44680eed 1023 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1463fe44 1024 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1463fe44 1025
44680eed
WD
1026 if (cfg->irptndx != INVALID_IRPTNDX) {
1027 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
bee14004 1028 devm_free_irq(smmu->dev, irq, domain);
45ae7cff
WD
1029 }
1030
44830b0c 1031 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
44680eed 1032 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
45ae7cff
WD
1033}
1034
1d672638 1035static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
45ae7cff
WD
1036{
1037 struct arm_smmu_domain *smmu_domain;
45ae7cff 1038
819b6dfa
WD
1039 if (type != IOMMU_DOMAIN_UNMANAGED &&
1040 type != IOMMU_DOMAIN_DMA &&
1041 type != IOMMU_DOMAIN_IDENTITY)
1d672638 1042 return NULL;
45ae7cff
WD
1043 /*
1044 * Allocate the domain and initialise some of its data structures.
1045 * We can't really do anything meaningful until we've added a
1046 * master.
1047 */
1048 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1049 if (!smmu_domain)
1d672638 1050 return NULL;
45ae7cff 1051
021bb842
RM
1052 if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
1053 iommu_get_dma_cookie(&smmu_domain->domain))) {
9adb9594
RM
1054 kfree(smmu_domain);
1055 return NULL;
1056 }
1057
518f7136
WD
1058 mutex_init(&smmu_domain->init_mutex);
1059 spin_lock_init(&smmu_domain->pgtbl_lock);
1d672638
JR
1060
1061 return &smmu_domain->domain;
45ae7cff
WD
1062}
1063
1d672638 1064static void arm_smmu_domain_free(struct iommu_domain *domain)
45ae7cff 1065{
1d672638 1066 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1463fe44
WD
1067
1068 /*
1069 * Free the domain resources. We assume that all devices have
1070 * already been detached.
1071 */
9adb9594 1072 iommu_put_dma_cookie(domain);
45ae7cff 1073 arm_smmu_destroy_domain_context(domain);
45ae7cff
WD
1074 kfree(smmu_domain);
1075}
1076
1f3d5ca4
RM
1077static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
1078{
1079 struct arm_smmu_smr *smr = smmu->smrs + idx;
f80cd885 1080 u32 reg = smr->id << SMR_ID_SHIFT | smr->mask << SMR_MASK_SHIFT;
1f3d5ca4 1081
2c8a6aa0 1082 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
1f3d5ca4
RM
1083 reg |= SMR_VALID;
1084 writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx));
1085}
1086
8e8b203e
RM
1087static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
1088{
1089 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
1090 u32 reg = (s2cr->type & S2CR_TYPE_MASK) << S2CR_TYPE_SHIFT |
1091 (s2cr->cbndx & S2CR_CBNDX_MASK) << S2CR_CBNDX_SHIFT |
1092 (s2cr->privcfg & S2CR_PRIVCFG_MASK) << S2CR_PRIVCFG_SHIFT;
1093
2c8a6aa0
AM
1094 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
1095 smmu->smrs[idx].valid)
1096 reg |= S2CR_EXIDVALID;
8e8b203e
RM
1097 writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx));
1098}
1099
1100static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
1101{
1102 arm_smmu_write_s2cr(smmu, idx);
1103 if (smmu->smrs)
1104 arm_smmu_write_smr(smmu, idx);
1105}
1106
2c8a6aa0
AM
1107/*
1108 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
1109 * should be called after sCR0 is written.
1110 */
1111static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
1112{
1113 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1114 u32 smr;
1115
1116 if (!smmu->smrs)
1117 return;
1118
1119 /*
1120 * SMR.ID bits may not be preserved if the corresponding MASK
1121 * bits are set, so check each one separately. We can reject
1122 * masters later if they try to claim IDs outside these masks.
1123 */
1124 smr = smmu->streamid_mask << SMR_ID_SHIFT;
1125 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1126 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1127 smmu->streamid_mask = smr >> SMR_ID_SHIFT;
1128
1129 smr = smmu->streamid_mask << SMR_MASK_SHIFT;
1130 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1131 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1132 smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT;
1133}
1134
588888a7 1135static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
1f3d5ca4
RM
1136{
1137 struct arm_smmu_smr *smrs = smmu->smrs;
588888a7 1138 int i, free_idx = -ENOSPC;
1f3d5ca4 1139
588888a7
RM
1140 /* Stream indexing is blissfully easy */
1141 if (!smrs)
1142 return id;
1143
1144 /* Validating SMRs is... less so */
1145 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1146 if (!smrs[i].valid) {
1147 /*
1148 * Note the first free entry we come across, which
1149 * we'll claim in the end if nothing else matches.
1150 */
1151 if (free_idx < 0)
1152 free_idx = i;
1f3d5ca4
RM
1153 continue;
1154 }
588888a7
RM
1155 /*
1156 * If the new entry is _entirely_ matched by an existing entry,
1157 * then reuse that, with the guarantee that there also cannot
1158 * be any subsequent conflicting entries. In normal use we'd
1159 * expect simply identical entries for this case, but there's
1160 * no harm in accommodating the generalisation.
1161 */
1162 if ((mask & smrs[i].mask) == mask &&
1163 !((id ^ smrs[i].id) & ~smrs[i].mask))
1164 return i;
1165 /*
1166 * If the new entry has any other overlap with an existing one,
1167 * though, then there always exists at least one stream ID
1168 * which would cause a conflict, and we can't allow that risk.
1169 */
1170 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1171 return -EINVAL;
1172 }
1f3d5ca4 1173
588888a7
RM
1174 return free_idx;
1175}
1176
1177static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1178{
1179 if (--smmu->s2crs[idx].count)
1180 return false;
1181
1182 smmu->s2crs[idx] = s2cr_init_val;
1183 if (smmu->smrs)
1184 smmu->smrs[idx].valid = false;
1185
1186 return true;
1187}
1188
1189static int arm_smmu_master_alloc_smes(struct device *dev)
1190{
adfec2e7
RM
1191 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1192 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
588888a7
RM
1193 struct arm_smmu_device *smmu = cfg->smmu;
1194 struct arm_smmu_smr *smrs = smmu->smrs;
1195 struct iommu_group *group;
1196 int i, idx, ret;
1197
1198 mutex_lock(&smmu->stream_map_mutex);
1199 /* Figure out a viable stream map entry allocation */
adfec2e7 1200 for_each_cfg_sme(fwspec, i, idx) {
021bb842
RM
1201 u16 sid = fwspec->ids[i];
1202 u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
1203
588888a7
RM
1204 if (idx != INVALID_SMENDX) {
1205 ret = -EEXIST;
1206 goto out_err;
45ae7cff
WD
1207 }
1208
021bb842 1209 ret = arm_smmu_find_sme(smmu, sid, mask);
588888a7
RM
1210 if (ret < 0)
1211 goto out_err;
1212
1213 idx = ret;
1214 if (smrs && smmu->s2crs[idx].count == 0) {
021bb842
RM
1215 smrs[idx].id = sid;
1216 smrs[idx].mask = mask;
588888a7
RM
1217 smrs[idx].valid = true;
1218 }
1219 smmu->s2crs[idx].count++;
1220 cfg->smendx[i] = (s16)idx;
45ae7cff
WD
1221 }
1222
588888a7
RM
1223 group = iommu_group_get_for_dev(dev);
1224 if (!group)
1225 group = ERR_PTR(-ENOMEM);
1226 if (IS_ERR(group)) {
1227 ret = PTR_ERR(group);
1228 goto out_err;
1229 }
1230 iommu_group_put(group);
1f3d5ca4 1231
45ae7cff 1232 /* It worked! Now, poke the actual hardware */
adfec2e7 1233 for_each_cfg_sme(fwspec, i, idx) {
588888a7
RM
1234 arm_smmu_write_sme(smmu, idx);
1235 smmu->s2crs[idx].group = group;
1236 }
45ae7cff 1237
588888a7 1238 mutex_unlock(&smmu->stream_map_mutex);
45ae7cff
WD
1239 return 0;
1240
588888a7 1241out_err:
1f3d5ca4 1242 while (i--) {
588888a7 1243 arm_smmu_free_sme(smmu, cfg->smendx[i]);
1f3d5ca4
RM
1244 cfg->smendx[i] = INVALID_SMENDX;
1245 }
588888a7
RM
1246 mutex_unlock(&smmu->stream_map_mutex);
1247 return ret;
45ae7cff
WD
1248}
1249
adfec2e7 1250static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec)
45ae7cff 1251{
adfec2e7
RM
1252 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
1253 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
d3097e39 1254 int i, idx;
43b412be 1255
588888a7 1256 mutex_lock(&smmu->stream_map_mutex);
adfec2e7 1257 for_each_cfg_sme(fwspec, i, idx) {
588888a7
RM
1258 if (arm_smmu_free_sme(smmu, idx))
1259 arm_smmu_write_sme(smmu, idx);
1f3d5ca4 1260 cfg->smendx[i] = INVALID_SMENDX;
45ae7cff 1261 }
588888a7 1262 mutex_unlock(&smmu->stream_map_mutex);
45ae7cff
WD
1263}
1264
45ae7cff 1265static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
adfec2e7 1266 struct iommu_fwspec *fwspec)
45ae7cff 1267{
44680eed 1268 struct arm_smmu_device *smmu = smmu_domain->smmu;
8e8b203e 1269 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
8e8b203e 1270 u8 cbndx = smmu_domain->cfg.cbndx;
819b6dfa 1271 enum arm_smmu_s2cr_type type;
588888a7 1272 int i, idx;
45ae7cff 1273
819b6dfa
WD
1274 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1275 type = S2CR_TYPE_BYPASS;
1276 else
1277 type = S2CR_TYPE_TRANS;
1278
adfec2e7 1279 for_each_cfg_sme(fwspec, i, idx) {
8e8b203e 1280 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
588888a7 1281 continue;
1f3d5ca4 1282
8e8b203e
RM
1283 s2cr[idx].type = type;
1284 s2cr[idx].privcfg = S2CR_PRIVCFG_UNPRIV;
1285 s2cr[idx].cbndx = cbndx;
1286 arm_smmu_write_s2cr(smmu, idx);
43b412be 1287 }
8e8b203e 1288 return 0;
bc7f2ce0
WD
1289}
1290
45ae7cff
WD
1291static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1292{
a18037b2 1293 int ret;
adfec2e7
RM
1294 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1295 struct arm_smmu_device *smmu;
1d672638 1296 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
45ae7cff 1297
adfec2e7 1298 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
45ae7cff
WD
1299 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1300 return -ENXIO;
1301 }
1302
fba4f8e5
RM
1303 /*
1304 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1305 * domains between of_xlate() and add_device() - we have no way to cope
1306 * with that, so until ARM gets converted to rely on groups and default
1307 * domains, just say no (but more politely than by dereferencing NULL).
1308 * This should be at least a WARN_ON once that's sorted.
1309 */
1310 if (!fwspec->iommu_priv)
1311 return -ENODEV;
1312
adfec2e7 1313 smmu = fwspec_smmu(fwspec);
518f7136 1314 /* Ensure that the domain is finalised */
adfec2e7 1315 ret = arm_smmu_init_domain_context(domain, smmu);
287980e4 1316 if (ret < 0)
518f7136
WD
1317 return ret;
1318
45ae7cff 1319 /*
44680eed
WD
1320 * Sanity check the domain. We don't support domains across
1321 * different SMMUs.
45ae7cff 1322 */
adfec2e7 1323 if (smmu_domain->smmu != smmu) {
45ae7cff
WD
1324 dev_err(dev,
1325 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
adfec2e7 1326 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
a18037b2 1327 return -EINVAL;
45ae7cff 1328 }
45ae7cff
WD
1329
1330 /* Looks ok, so add the device to the domain */
adfec2e7 1331 return arm_smmu_domain_add_master(smmu_domain, fwspec);
45ae7cff
WD
1332}
1333
45ae7cff 1334static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
b410aed9 1335 phys_addr_t paddr, size_t size, int prot)
45ae7cff 1336{
518f7136
WD
1337 int ret;
1338 unsigned long flags;
1d672638 1339 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1340 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
45ae7cff 1341
518f7136 1342 if (!ops)
45ae7cff
WD
1343 return -ENODEV;
1344
518f7136
WD
1345 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1346 ret = ops->map(ops, iova, paddr, size, prot);
1347 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1348 return ret;
45ae7cff
WD
1349}
1350
1351static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1352 size_t size)
1353{
518f7136
WD
1354 size_t ret;
1355 unsigned long flags;
1d672638 1356 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1357 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
45ae7cff 1358
518f7136
WD
1359 if (!ops)
1360 return 0;
1361
1362 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1363 ret = ops->unmap(ops, iova, size);
1364 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1365 return ret;
45ae7cff
WD
1366}
1367
859a732e
MH
1368static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1369 dma_addr_t iova)
1370{
1d672638 1371 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
859a732e
MH
1372 struct arm_smmu_device *smmu = smmu_domain->smmu;
1373 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1374 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1375 struct device *dev = smmu->dev;
1376 void __iomem *cb_base;
1377 u32 tmp;
1378 u64 phys;
661d962f 1379 unsigned long va;
859a732e
MH
1380
1381 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1382
661d962f
RM
1383 /* ATS1 registers can only be written atomically */
1384 va = iova & ~0xfffUL;
661d962f 1385 if (smmu->version == ARM_SMMU_V2)
f9a05f05
RM
1386 smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR);
1387 else /* Register is only 32-bit in v1 */
661d962f 1388 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
859a732e
MH
1389
1390 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1391 !(tmp & ATSR_ACTIVE), 5, 50)) {
1392 dev_err(dev,
077124c9 1393 "iova to phys timed out on %pad. Falling back to software table walk.\n",
859a732e
MH
1394 &iova);
1395 return ops->iova_to_phys(ops, iova);
1396 }
1397
f9a05f05 1398 phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR);
859a732e
MH
1399 if (phys & CB_PAR_F) {
1400 dev_err(dev, "translation fault!\n");
1401 dev_err(dev, "PAR = 0x%llx\n", phys);
1402 return 0;
1403 }
1404
1405 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1406}
1407
45ae7cff 1408static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
859a732e 1409 dma_addr_t iova)
45ae7cff 1410{
518f7136
WD
1411 phys_addr_t ret;
1412 unsigned long flags;
1d672638 1413 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1414 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
45ae7cff 1415
518f7136 1416 if (!ops)
a44a9791 1417 return 0;
45ae7cff 1418
518f7136 1419 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
83a60ed8
BR
1420 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1421 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
859a732e 1422 ret = arm_smmu_iova_to_phys_hard(domain, iova);
83a60ed8 1423 } else {
859a732e 1424 ret = ops->iova_to_phys(ops, iova);
83a60ed8
BR
1425 }
1426
518f7136 1427 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
859a732e 1428
518f7136 1429 return ret;
45ae7cff
WD
1430}
1431
1fd0c775 1432static bool arm_smmu_capable(enum iommu_cap cap)
45ae7cff 1433{
d0948945
WD
1434 switch (cap) {
1435 case IOMMU_CAP_CACHE_COHERENCY:
1fd0c775
JR
1436 /*
1437 * Return true here as the SMMU can always send out coherent
1438 * requests.
1439 */
1440 return true;
0029a8dd
AM
1441 case IOMMU_CAP_NOEXEC:
1442 return true;
d0948945 1443 default:
1fd0c775 1444 return false;
d0948945 1445 }
45ae7cff 1446}
45ae7cff 1447
021bb842
RM
1448static int arm_smmu_match_node(struct device *dev, void *data)
1449{
ce9babe5 1450 return dev->fwnode == data;
021bb842
RM
1451}
1452
ce9babe5
LP
1453static
1454struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
021bb842
RM
1455{
1456 struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
ce9babe5 1457 fwnode, arm_smmu_match_node);
021bb842
RM
1458 put_device(dev);
1459 return dev ? dev_get_drvdata(dev) : NULL;
1460}
1461
f80cd885 1462static int arm_smmu_add_device(struct device *dev)
45ae7cff 1463{
adfec2e7 1464 struct arm_smmu_device *smmu;
03edb226 1465 struct arm_smmu_master_cfg *cfg;
021bb842 1466 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
f80cd885 1467 int i, ret;
8f68f8e2 1468
021bb842
RM
1469 if (using_legacy_binding) {
1470 ret = arm_smmu_register_legacy_master(dev, &smmu);
1471 fwspec = dev->iommu_fwspec;
1472 if (ret)
1473 goto out_free;
3c117b54 1474 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
ce9babe5 1475 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
021bb842
RM
1476 } else {
1477 return -ENODEV;
1478 }
a9a1b0b5 1479
f80cd885 1480 ret = -EINVAL;
adfec2e7
RM
1481 for (i = 0; i < fwspec->num_ids; i++) {
1482 u16 sid = fwspec->ids[i];
021bb842 1483 u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
03edb226 1484
adfec2e7 1485 if (sid & ~smmu->streamid_mask) {
f80cd885 1486 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
021bb842
RM
1487 sid, smmu->streamid_mask);
1488 goto out_free;
1489 }
1490 if (mask & ~smmu->smr_mask_mask) {
1491 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1492 sid, smmu->smr_mask_mask);
f80cd885
RM
1493 goto out_free;
1494 }
1f3d5ca4 1495 }
5fc63a7c 1496
adfec2e7
RM
1497 ret = -ENOMEM;
1498 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1499 GFP_KERNEL);
1500 if (!cfg)
1501 goto out_free;
1502
1503 cfg->smmu = smmu;
1504 fwspec->iommu_priv = cfg;
1505 while (i--)
1506 cfg->smendx[i] = INVALID_SMENDX;
1507
588888a7 1508 ret = arm_smmu_master_alloc_smes(dev);
adfec2e7
RM
1509 if (ret)
1510 goto out_free;
1511
17554084
JR
1512 iommu_device_link(&smmu->iommu, dev);
1513
adfec2e7 1514 return 0;
f80cd885
RM
1515
1516out_free:
adfec2e7
RM
1517 if (fwspec)
1518 kfree(fwspec->iommu_priv);
1519 iommu_fwspec_free(dev);
f80cd885 1520 return ret;
03edb226
WD
1521}
1522
45ae7cff
WD
1523static void arm_smmu_remove_device(struct device *dev)
1524{
adfec2e7 1525 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
17554084
JR
1526 struct arm_smmu_master_cfg *cfg;
1527 struct arm_smmu_device *smmu;
1528
8e8b203e 1529
adfec2e7 1530 if (!fwspec || fwspec->ops != &arm_smmu_ops)
f80cd885 1531 return;
8e8b203e 1532
17554084
JR
1533 cfg = fwspec->iommu_priv;
1534 smmu = cfg->smmu;
1535
1536 iommu_device_unlink(&smmu->iommu, dev);
adfec2e7 1537 arm_smmu_master_free_smes(fwspec);
5fc63a7c 1538 iommu_group_remove_device(dev);
adfec2e7
RM
1539 kfree(fwspec->iommu_priv);
1540 iommu_fwspec_free(dev);
45ae7cff
WD
1541}
1542
af659932
JR
1543static struct iommu_group *arm_smmu_device_group(struct device *dev)
1544{
adfec2e7
RM
1545 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1546 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
588888a7
RM
1547 struct iommu_group *group = NULL;
1548 int i, idx;
1549
adfec2e7 1550 for_each_cfg_sme(fwspec, i, idx) {
588888a7
RM
1551 if (group && smmu->s2crs[idx].group &&
1552 group != smmu->s2crs[idx].group)
1553 return ERR_PTR(-EINVAL);
1554
1555 group = smmu->s2crs[idx].group;
1556 }
1557
1558 if (group)
e1b44cbe 1559 return iommu_group_ref_get(group);
af659932
JR
1560
1561 if (dev_is_pci(dev))
1562 group = pci_device_group(dev);
1563 else
1564 group = generic_device_group(dev);
1565
af659932
JR
1566 return group;
1567}
1568
c752ce45
WD
1569static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1570 enum iommu_attr attr, void *data)
1571{
1d672638 1572 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
c752ce45 1573
a07cee71
WD
1574 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
1575 return -EINVAL;
1576
c752ce45
WD
1577 switch (attr) {
1578 case DOMAIN_ATTR_NESTING:
1579 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1580 return 0;
1581 default:
1582 return -ENODEV;
1583 }
1584}
1585
1586static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1587 enum iommu_attr attr, void *data)
1588{
518f7136 1589 int ret = 0;
1d672638 1590 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
c752ce45 1591
a07cee71
WD
1592 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
1593 return -EINVAL;
1594
518f7136
WD
1595 mutex_lock(&smmu_domain->init_mutex);
1596
c752ce45
WD
1597 switch (attr) {
1598 case DOMAIN_ATTR_NESTING:
518f7136
WD
1599 if (smmu_domain->smmu) {
1600 ret = -EPERM;
1601 goto out_unlock;
1602 }
1603
c752ce45
WD
1604 if (*(int *)data)
1605 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1606 else
1607 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1608
518f7136 1609 break;
c752ce45 1610 default:
518f7136 1611 ret = -ENODEV;
c752ce45 1612 }
518f7136
WD
1613
1614out_unlock:
1615 mutex_unlock(&smmu_domain->init_mutex);
1616 return ret;
c752ce45
WD
1617}
1618
021bb842
RM
1619static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1620{
1621 u32 fwid = 0;
1622
1623 if (args->args_count > 0)
1624 fwid |= (u16)args->args[0];
1625
1626 if (args->args_count > 1)
1627 fwid |= (u16)args->args[1] << SMR_MASK_SHIFT;
1628
1629 return iommu_fwspec_add_ids(dev, &fwid, 1);
1630}
1631
b8a07ed7
EA
1632static void arm_smmu_get_resv_regions(struct device *dev,
1633 struct list_head *head)
1634{
1635 struct iommu_resv_region *region;
1636 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1637
1638 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1639 prot, IOMMU_RESV_MSI);
1640 if (!region)
1641 return;
1642
1643 list_add_tail(&region->list, head);
1644}
1645
1646static void arm_smmu_put_resv_regions(struct device *dev,
1647 struct list_head *head)
1648{
1649 struct iommu_resv_region *entry, *next;
1650
1651 list_for_each_entry_safe(entry, next, head, list)
1652 kfree(entry);
1653}
1654
518f7136 1655static struct iommu_ops arm_smmu_ops = {
c752ce45 1656 .capable = arm_smmu_capable,
1d672638
JR
1657 .domain_alloc = arm_smmu_domain_alloc,
1658 .domain_free = arm_smmu_domain_free,
c752ce45 1659 .attach_dev = arm_smmu_attach_dev,
c752ce45
WD
1660 .map = arm_smmu_map,
1661 .unmap = arm_smmu_unmap,
76771c93 1662 .map_sg = default_iommu_map_sg,
c752ce45
WD
1663 .iova_to_phys = arm_smmu_iova_to_phys,
1664 .add_device = arm_smmu_add_device,
1665 .remove_device = arm_smmu_remove_device,
af659932 1666 .device_group = arm_smmu_device_group,
c752ce45
WD
1667 .domain_get_attr = arm_smmu_domain_get_attr,
1668 .domain_set_attr = arm_smmu_domain_set_attr,
021bb842 1669 .of_xlate = arm_smmu_of_xlate,
b8a07ed7
EA
1670 .get_resv_regions = arm_smmu_get_resv_regions,
1671 .put_resv_regions = arm_smmu_put_resv_regions,
518f7136 1672 .pgsize_bitmap = -1UL, /* Restricted during device attach */
45ae7cff
WD
1673};
1674
1675static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1676{
1677 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
659db6f6 1678 void __iomem *cb_base;
1f3d5ca4 1679 int i;
3ca3712a 1680 u32 reg, major;
659db6f6 1681
3a5df8ff
AH
1682 /* clear global FSR */
1683 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1684 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
45ae7cff 1685
1f3d5ca4
RM
1686 /*
1687 * Reset stream mapping groups: Initial values mark all SMRn as
1688 * invalid and all S2CRn as bypass unless overridden.
1689 */
8e8b203e
RM
1690 for (i = 0; i < smmu->num_mapping_groups; ++i)
1691 arm_smmu_write_sme(smmu, i);
45ae7cff 1692
6eb18d4a
NG
1693 if (smmu->model == ARM_MMU500) {
1694 /*
1695 * Before clearing ARM_MMU500_ACTLR_CPRE, need to
1696 * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
1697 * bit is only present in MMU-500r2 onwards.
1698 */
1699 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7);
1700 major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK;
3ca3712a 1701 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR);
6eb18d4a
NG
1702 if (major >= 2)
1703 reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
1704 /*
1705 * Allow unmatched Stream IDs to allocate bypass
1706 * TLB entries for reduced latency.
1707 */
1708 reg |= ARM_MMU500_ACR_SMTNMB_TLBEN;
3ca3712a
PF
1709 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR);
1710 }
1711
659db6f6
AH
1712 /* Make sure all context banks are disabled and clear CB_FSR */
1713 for (i = 0; i < smmu->num_context_banks; ++i) {
1714 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1715 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1716 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
f0cfffc4
RM
1717 /*
1718 * Disable MMU-500's not-particularly-beneficial next-page
1719 * prefetcher for the sake of errata #841119 and #826419.
1720 */
1721 if (smmu->model == ARM_MMU500) {
1722 reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR);
1723 reg &= ~ARM_MMU500_ACTLR_CPRE;
1724 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR);
1725 }
659db6f6 1726 }
1463fe44 1727
45ae7cff 1728 /* Invalidate the TLB, just in case */
45ae7cff
WD
1729 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1730 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1731
3a5df8ff 1732 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
659db6f6 1733
45ae7cff 1734 /* Enable fault reporting */
659db6f6 1735 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
45ae7cff
WD
1736
1737 /* Disable TLB broadcasting. */
659db6f6 1738 reg |= (sCR0_VMIDPNE | sCR0_PTM);
45ae7cff 1739
25a1c96c
RM
1740 /* Enable client access, handling unmatched streams as appropriate */
1741 reg &= ~sCR0_CLIENTPD;
1742 if (disable_bypass)
1743 reg |= sCR0_USFCFG;
1744 else
1745 reg &= ~sCR0_USFCFG;
45ae7cff
WD
1746
1747 /* Disable forced broadcasting */
659db6f6 1748 reg &= ~sCR0_FB;
45ae7cff
WD
1749
1750 /* Don't upgrade barriers */
659db6f6 1751 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
45ae7cff 1752
4e3e9b69
TC
1753 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1754 reg |= sCR0_VMID16EN;
1755
2c8a6aa0
AM
1756 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1757 reg |= sCR0_EXIDENABLE;
1758
45ae7cff 1759 /* Push the button */
518f7136 1760 __arm_smmu_tlb_sync(smmu);
3a5df8ff 1761 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
45ae7cff
WD
1762}
1763
1764static int arm_smmu_id_size_to_bits(int size)
1765{
1766 switch (size) {
1767 case 0:
1768 return 32;
1769 case 1:
1770 return 36;
1771 case 2:
1772 return 40;
1773 case 3:
1774 return 42;
1775 case 4:
1776 return 44;
1777 case 5:
1778 default:
1779 return 48;
1780 }
1781}
1782
1783static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1784{
1785 unsigned long size;
1786 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1787 u32 id;
bbb8a184 1788 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
8e8b203e 1789 int i;
45ae7cff
WD
1790
1791 dev_notice(smmu->dev, "probing hardware configuration...\n");
b7862e35
RM
1792 dev_notice(smmu->dev, "SMMUv%d with:\n",
1793 smmu->version == ARM_SMMU_V2 ? 2 : 1);
45ae7cff
WD
1794
1795 /* ID0 */
1796 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
4cf740b0
WD
1797
1798 /* Restrict available stages based on module parameter */
1799 if (force_stage == 1)
1800 id &= ~(ID0_S2TS | ID0_NTS);
1801 else if (force_stage == 2)
1802 id &= ~(ID0_S1TS | ID0_NTS);
1803
45ae7cff
WD
1804 if (id & ID0_S1TS) {
1805 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1806 dev_notice(smmu->dev, "\tstage 1 translation\n");
1807 }
1808
1809 if (id & ID0_S2TS) {
1810 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1811 dev_notice(smmu->dev, "\tstage 2 translation\n");
1812 }
1813
1814 if (id & ID0_NTS) {
1815 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1816 dev_notice(smmu->dev, "\tnested translation\n");
1817 }
1818
1819 if (!(smmu->features &
4cf740b0 1820 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
45ae7cff
WD
1821 dev_err(smmu->dev, "\tno translation support!\n");
1822 return -ENODEV;
1823 }
1824
b7862e35
RM
1825 if ((id & ID0_S1TS) &&
1826 ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
859a732e
MH
1827 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1828 dev_notice(smmu->dev, "\taddress translation ops\n");
1829 }
1830
bae2c2d4
RM
1831 /*
1832 * In order for DMA API calls to work properly, we must defer to what
bbb8a184 1833 * the FW says about coherency, regardless of what the hardware claims.
bae2c2d4
RM
1834 * Fortunately, this also opens up a workaround for systems where the
1835 * ID register value has ended up configured incorrectly.
1836 */
bae2c2d4 1837 cttw_reg = !!(id & ID0_CTTW);
bbb8a184 1838 if (cttw_fw || cttw_reg)
bae2c2d4 1839 dev_notice(smmu->dev, "\t%scoherent table walk\n",
bbb8a184
LP
1840 cttw_fw ? "" : "non-");
1841 if (cttw_fw != cttw_reg)
bae2c2d4 1842 dev_notice(smmu->dev,
bbb8a184 1843 "\t(IDR0.CTTW overridden by FW configuration)\n");
45ae7cff 1844
21174240 1845 /* Max. number of entries we have for stream matching/indexing */
2c8a6aa0
AM
1846 if (smmu->version == ARM_SMMU_V2 && id & ID0_EXIDS) {
1847 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1848 size = 1 << 16;
1849 } else {
1850 size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK);
1851 }
21174240 1852 smmu->streamid_mask = size - 1;
45ae7cff 1853 if (id & ID0_SMS) {
45ae7cff 1854 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
21174240
RM
1855 size = (id >> ID0_NUMSMRG_SHIFT) & ID0_NUMSMRG_MASK;
1856 if (size == 0) {
45ae7cff
WD
1857 dev_err(smmu->dev,
1858 "stream-matching supported, but no SMRs present!\n");
1859 return -ENODEV;
1860 }
1861
1f3d5ca4
RM
1862 /* Zero-initialised to mark as invalid */
1863 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1864 GFP_KERNEL);
1865 if (!smmu->smrs)
1866 return -ENOMEM;
1867
45ae7cff 1868 dev_notice(smmu->dev,
2c8a6aa0 1869 "\tstream matching with %lu register groups", size);
45ae7cff 1870 }
8e8b203e
RM
1871 /* s2cr->type == 0 means translation, so initialise explicitly */
1872 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1873 GFP_KERNEL);
1874 if (!smmu->s2crs)
1875 return -ENOMEM;
1876 for (i = 0; i < size; i++)
1877 smmu->s2crs[i] = s2cr_init_val;
1878
21174240 1879 smmu->num_mapping_groups = size;
588888a7 1880 mutex_init(&smmu->stream_map_mutex);
45ae7cff 1881
7602b871
RM
1882 if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
1883 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1884 if (!(id & ID0_PTFS_NO_AARCH32S))
1885 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1886 }
1887
45ae7cff
WD
1888 /* ID1 */
1889 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
c757e852 1890 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
45ae7cff 1891
c55af7f7 1892 /* Check for size mismatch of SMMU address space from mapped region */
518f7136 1893 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
c757e852 1894 size *= 2 << smmu->pgshift;
c55af7f7 1895 if (smmu->size != size)
2907320d
MH
1896 dev_warn(smmu->dev,
1897 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1898 size, smmu->size);
45ae7cff 1899
518f7136 1900 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
45ae7cff
WD
1901 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1902 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1903 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1904 return -ENODEV;
1905 }
1906 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1907 smmu->num_context_banks, smmu->num_s2_context_banks);
e086d912
RM
1908 /*
1909 * Cavium CN88xx erratum #27704.
1910 * Ensure ASID and VMID allocation is unique across all SMMUs in
1911 * the system.
1912 */
1913 if (smmu->model == CAVIUM_SMMUV2) {
1914 smmu->cavium_id_base =
1915 atomic_add_return(smmu->num_context_banks,
1916 &cavium_smmu_context_count);
1917 smmu->cavium_id_base -= smmu->num_context_banks;
1918 }
45ae7cff
WD
1919
1920 /* ID2 */
1921 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1922 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
518f7136 1923 smmu->ipa_size = size;
45ae7cff 1924
518f7136 1925 /* The output mask is also applied for bypass */
45ae7cff 1926 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
518f7136 1927 smmu->pa_size = size;
45ae7cff 1928
4e3e9b69
TC
1929 if (id & ID2_VMID16)
1930 smmu->features |= ARM_SMMU_FEAT_VMID16;
1931
f1d84548
RM
1932 /*
1933 * What the page table walker can address actually depends on which
1934 * descriptor format is in use, but since a) we don't know that yet,
1935 * and b) it can vary per context bank, this will have to do...
1936 */
1937 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1938 dev_warn(smmu->dev,
1939 "failed to set DMA mask for table walker\n");
1940
b7862e35 1941 if (smmu->version < ARM_SMMU_V2) {
518f7136 1942 smmu->va_size = smmu->ipa_size;
b7862e35
RM
1943 if (smmu->version == ARM_SMMU_V1_64K)
1944 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
45ae7cff 1945 } else {
45ae7cff 1946 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
518f7136 1947 smmu->va_size = arm_smmu_id_size_to_bits(size);
518f7136 1948 if (id & ID2_PTFS_4K)
7602b871 1949 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
518f7136 1950 if (id & ID2_PTFS_16K)
7602b871 1951 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
518f7136 1952 if (id & ID2_PTFS_64K)
7602b871 1953 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
45ae7cff
WD
1954 }
1955
7602b871 1956 /* Now we've corralled the various formats, what'll it do? */
7602b871 1957 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
d5466357 1958 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
7602b871
RM
1959 if (smmu->features &
1960 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
d5466357 1961 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
7602b871 1962 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
d5466357 1963 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
7602b871 1964 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
d5466357
RM
1965 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
1966
1967 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1968 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1969 else
1970 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1971 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1972 smmu->pgsize_bitmap);
7602b871 1973
518f7136 1974
28d6007b
WD
1975 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1976 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
518f7136 1977 smmu->va_size, smmu->ipa_size);
28d6007b
WD
1978
1979 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1980 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
518f7136 1981 smmu->ipa_size, smmu->pa_size);
28d6007b 1982
45ae7cff
WD
1983 return 0;
1984}
1985
67b65a3f
RM
1986struct arm_smmu_match_data {
1987 enum arm_smmu_arch_version version;
1988 enum arm_smmu_implementation model;
1989};
1990
1991#define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1992static struct arm_smmu_match_data name = { .version = ver, .model = imp }
1993
1994ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1995ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
b7862e35 1996ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
f0cfffc4 1997ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
e086d912 1998ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
67b65a3f 1999
09b5269a 2000static const struct of_device_id arm_smmu_of_match[] = {
67b65a3f
RM
2001 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
2002 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
2003 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
b7862e35 2004 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
f0cfffc4 2005 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
e086d912 2006 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
09360403
RM
2007 { },
2008};
2009MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2010
d6fcd3b1
LP
2011#ifdef CONFIG_ACPI
2012static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
2013{
2014 int ret = 0;
2015
2016 switch (model) {
2017 case ACPI_IORT_SMMU_V1:
2018 case ACPI_IORT_SMMU_CORELINK_MMU400:
2019 smmu->version = ARM_SMMU_V1;
2020 smmu->model = GENERIC_SMMU;
2021 break;
f6b14b3c
RM
2022 case ACPI_IORT_SMMU_CORELINK_MMU401:
2023 smmu->version = ARM_SMMU_V1_64K;
2024 smmu->model = GENERIC_SMMU;
2025 break;
d6fcd3b1
LP
2026 case ACPI_IORT_SMMU_V2:
2027 smmu->version = ARM_SMMU_V2;
2028 smmu->model = GENERIC_SMMU;
2029 break;
2030 case ACPI_IORT_SMMU_CORELINK_MMU500:
2031 smmu->version = ARM_SMMU_V2;
2032 smmu->model = ARM_MMU500;
2033 break;
f6b14b3c
RM
2034 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
2035 smmu->version = ARM_SMMU_V2;
2036 smmu->model = CAVIUM_SMMUV2;
2037 break;
d6fcd3b1
LP
2038 default:
2039 ret = -ENODEV;
2040 }
2041
2042 return ret;
2043}
2044
2045static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2046 struct arm_smmu_device *smmu)
2047{
2048 struct device *dev = smmu->dev;
2049 struct acpi_iort_node *node =
2050 *(struct acpi_iort_node **)dev_get_platdata(dev);
2051 struct acpi_iort_smmu *iort_smmu;
2052 int ret;
2053
2054 /* Retrieve SMMU1/2 specific data */
2055 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
2056
2057 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
2058 if (ret < 0)
2059 return ret;
2060
2061 /* Ignore the configuration access interrupt */
2062 smmu->num_global_irqs = 1;
2063
2064 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
2065 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2066
2067 return 0;
2068}
2069#else
2070static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2071 struct arm_smmu_device *smmu)
2072{
2073 return -ENODEV;
2074}
2075#endif
2076
bbb8a184
LP
2077static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2078 struct arm_smmu_device *smmu)
45ae7cff 2079{
67b65a3f 2080 const struct arm_smmu_match_data *data;
45ae7cff 2081 struct device *dev = &pdev->dev;
021bb842
RM
2082 bool legacy_binding;
2083
bbb8a184
LP
2084 if (of_property_read_u32(dev->of_node, "#global-interrupts",
2085 &smmu->num_global_irqs)) {
2086 dev_err(dev, "missing #global-interrupts property\n");
2087 return -ENODEV;
2088 }
2089
2090 data = of_device_get_match_data(dev);
2091 smmu->version = data->version;
2092 smmu->model = data->model;
2093
2094 parse_driver_options(smmu);
2095
021bb842
RM
2096 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
2097 if (legacy_binding && !using_generic_binding) {
2098 if (!using_legacy_binding)
2099 pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
2100 using_legacy_binding = true;
2101 } else if (!legacy_binding && !using_legacy_binding) {
2102 using_generic_binding = true;
2103 } else {
2104 dev_err(dev, "not probing due to mismatched DT properties\n");
2105 return -ENODEV;
2106 }
45ae7cff 2107
bbb8a184
LP
2108 if (of_dma_is_coherent(dev->of_node))
2109 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2110
2111 return 0;
2112}
2113
2114static int arm_smmu_device_probe(struct platform_device *pdev)
2115{
2116 struct resource *res;
17554084 2117 resource_size_t ioaddr;
bbb8a184
LP
2118 struct arm_smmu_device *smmu;
2119 struct device *dev = &pdev->dev;
2120 int num_irqs, i, err;
2121
45ae7cff
WD
2122 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2123 if (!smmu) {
2124 dev_err(dev, "failed to allocate arm_smmu_device\n");
2125 return -ENOMEM;
2126 }
2127 smmu->dev = dev;
2128
d6fcd3b1
LP
2129 if (dev->of_node)
2130 err = arm_smmu_device_dt_probe(pdev, smmu);
2131 else
2132 err = arm_smmu_device_acpi_probe(pdev, smmu);
2133
bbb8a184
LP
2134 if (err)
2135 return err;
09360403 2136
45ae7cff 2137 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
17554084 2138 ioaddr = res->start;
8a7f4312
JL
2139 smmu->base = devm_ioremap_resource(dev, res);
2140 if (IS_ERR(smmu->base))
2141 return PTR_ERR(smmu->base);
45ae7cff 2142 smmu->size = resource_size(res);
45ae7cff 2143
45ae7cff
WD
2144 num_irqs = 0;
2145 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2146 num_irqs++;
2147 if (num_irqs > smmu->num_global_irqs)
2148 smmu->num_context_irqs++;
2149 }
2150
44a08de2
AH
2151 if (!smmu->num_context_irqs) {
2152 dev_err(dev, "found %d interrupts but expected at least %d\n",
2153 num_irqs, smmu->num_global_irqs + 1);
2154 return -ENODEV;
45ae7cff 2155 }
45ae7cff
WD
2156
2157 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
2158 GFP_KERNEL);
2159 if (!smmu->irqs) {
2160 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2161 return -ENOMEM;
2162 }
2163
2164 for (i = 0; i < num_irqs; ++i) {
2165 int irq = platform_get_irq(pdev, i);
2907320d 2166
45ae7cff
WD
2167 if (irq < 0) {
2168 dev_err(dev, "failed to get irq index %d\n", i);
2169 return -ENODEV;
2170 }
2171 smmu->irqs[i] = irq;
2172 }
2173
3c8766d0
OH
2174 err = arm_smmu_device_cfg_probe(smmu);
2175 if (err)
2176 return err;
2177
b7862e35 2178 if (smmu->version == ARM_SMMU_V2 &&
45ae7cff
WD
2179 smmu->num_context_banks != smmu->num_context_irqs) {
2180 dev_err(dev,
2181 "found only %d context interrupt(s) but %d required\n",
2182 smmu->num_context_irqs, smmu->num_context_banks);
f80cd885 2183 return -ENODEV;
45ae7cff
WD
2184 }
2185
45ae7cff 2186 for (i = 0; i < smmu->num_global_irqs; ++i) {
bee14004
PF
2187 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2188 arm_smmu_global_fault,
2189 IRQF_SHARED,
2190 "arm-smmu global fault",
2191 smmu);
45ae7cff
WD
2192 if (err) {
2193 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2194 i, smmu->irqs[i]);
f80cd885 2195 return err;
45ae7cff
WD
2196 }
2197 }
2198
17554084
JR
2199 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2200 "smmu.%pa", &ioaddr);
2201 if (err) {
2202 dev_err(dev, "Failed to register iommu in sysfs\n");
2203 return err;
2204 }
2205
2206 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2207 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2208
2209 err = iommu_device_register(&smmu->iommu);
2210 if (err) {
2211 dev_err(dev, "Failed to register iommu\n");
2212 return err;
2213 }
2214
ce9babe5 2215 iommu_register_instance(dev->fwnode, &arm_smmu_ops);
d6fc5d97 2216 platform_set_drvdata(pdev, smmu);
fd90cecb 2217 arm_smmu_device_reset(smmu);
2c8a6aa0 2218 arm_smmu_test_smr_masks(smmu);
021bb842
RM
2219
2220 /* Oh, for a proper bus abstraction */
2221 if (!iommu_present(&platform_bus_type))
2222 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2223#ifdef CONFIG_ARM_AMBA
2224 if (!iommu_present(&amba_bustype))
2225 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2226#endif
2227#ifdef CONFIG_PCI
2228 if (!iommu_present(&pci_bus_type)) {
2229 pci_request_acs();
2230 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2231 }
2232#endif
45ae7cff 2233 return 0;
45ae7cff
WD
2234}
2235
2236static int arm_smmu_device_remove(struct platform_device *pdev)
2237{
d6fc5d97 2238 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
45ae7cff
WD
2239
2240 if (!smmu)
2241 return -ENODEV;
2242
ecfadb6e 2243 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
d6fc5d97 2244 dev_err(&pdev->dev, "removing device with active domains!\n");
45ae7cff 2245
45ae7cff 2246 /* Turn the thing off */
2907320d 2247 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
45ae7cff
WD
2248 return 0;
2249}
2250
45ae7cff
WD
2251static struct platform_driver arm_smmu_driver = {
2252 .driver = {
45ae7cff
WD
2253 .name = "arm-smmu",
2254 .of_match_table = of_match_ptr(arm_smmu_of_match),
2255 },
bbb8a184 2256 .probe = arm_smmu_device_probe,
45ae7cff
WD
2257 .remove = arm_smmu_device_remove,
2258};
2259
2260static int __init arm_smmu_init(void)
2261{
021bb842
RM
2262 static bool registered;
2263 int ret = 0;
45ae7cff 2264
021bb842
RM
2265 if (!registered) {
2266 ret = platform_driver_register(&arm_smmu_driver);
2267 registered = !ret;
112c898b 2268 }
021bb842 2269 return ret;
45ae7cff
WD
2270}
2271
2272static void __exit arm_smmu_exit(void)
2273{
2274 return platform_driver_unregister(&arm_smmu_driver);
2275}
2276
b1950b27 2277subsys_initcall(arm_smmu_init);
45ae7cff
WD
2278module_exit(arm_smmu_exit);
2279
021bb842
RM
2280static int __init arm_smmu_of_init(struct device_node *np)
2281{
2282 int ret = arm_smmu_init();
2283
2284 if (ret)
2285 return ret;
2286
2287 if (!of_platform_device_create(np, NULL, platform_bus_type.dev_root))
2288 return -ENODEV;
2289
2290 return 0;
2291}
2292IOMMU_OF_DECLARE(arm_smmuv1, "arm,smmu-v1", arm_smmu_of_init);
2293IOMMU_OF_DECLARE(arm_smmuv2, "arm,smmu-v2", arm_smmu_of_init);
2294IOMMU_OF_DECLARE(arm_mmu400, "arm,mmu-400", arm_smmu_of_init);
2295IOMMU_OF_DECLARE(arm_mmu401, "arm,mmu-401", arm_smmu_of_init);
2296IOMMU_OF_DECLARE(arm_mmu500, "arm,mmu-500", arm_smmu_of_init);
2297IOMMU_OF_DECLARE(cavium_smmuv2, "cavium,smmu-v2", arm_smmu_of_init);
2298
d6fcd3b1
LP
2299#ifdef CONFIG_ACPI
2300static int __init arm_smmu_acpi_init(struct acpi_table_header *table)
2301{
2302 if (iort_node_match(ACPI_IORT_NODE_SMMU))
2303 return arm_smmu_init();
2304
2305 return 0;
2306}
2307IORT_ACPI_DECLARE(arm_smmu, ACPI_SIG_IORT, arm_smmu_acpi_init);
2308#endif
2309
45ae7cff
WD
2310MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2311MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2312MODULE_LICENSE("GPL v2");