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