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