]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/iommu/arm-smmu.c
iommu/arm-smmu: Allow disabling unmatched stream bypass
[mirror_ubuntu-artful-kernel.git] / drivers / iommu / arm-smmu.c
CommitLineData
45ae7cff
WD
1/*
2 * IOMMU API for ARM architected SMMU implementations.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 *
17 * Copyright (C) 2013 ARM Limited
18 *
19 * Author: Will Deacon <will.deacon@arm.com>
20 *
21 * This driver currently supports:
22 * - SMMUv1 and v2 implementations
23 * - Stream-matching and stream-indexing
24 * - v7/v8 long-descriptor format
25 * - Non-secure access to the SMMU
45ae7cff
WD
26 * - Context fault reporting
27 */
28
29#define pr_fmt(fmt) "arm-smmu: " fmt
30
31#include <linux/delay.h>
9adb9594 32#include <linux/dma-iommu.h>
45ae7cff
WD
33#include <linux/dma-mapping.h>
34#include <linux/err.h>
35#include <linux/interrupt.h>
36#include <linux/io.h>
37#include <linux/iommu.h>
859a732e 38#include <linux/iopoll.h>
45ae7cff
WD
39#include <linux/module.h>
40#include <linux/of.h>
bae2c2d4 41#include <linux/of_address.h>
a9a1b0b5 42#include <linux/pci.h>
45ae7cff
WD
43#include <linux/platform_device.h>
44#include <linux/slab.h>
45#include <linux/spinlock.h>
46
47#include <linux/amba/bus.h>
48
518f7136 49#include "io-pgtable.h"
45ae7cff
WD
50
51/* Maximum number of stream IDs assigned to a single device */
636e97b0 52#define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
45ae7cff
WD
53
54/* Maximum number of context banks per SMMU */
55#define ARM_SMMU_MAX_CBS 128
56
57/* Maximum number of mapping groups per SMMU */
58#define ARM_SMMU_MAX_SMRS 128
59
45ae7cff
WD
60/* SMMU global address space */
61#define ARM_SMMU_GR0(smmu) ((smmu)->base)
c757e852 62#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
45ae7cff 63
3a5df8ff
AH
64/*
65 * SMMU global address space with conditional offset to access secure
66 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
67 * nsGFSYNR0: 0x450)
68 */
69#define ARM_SMMU_GR0_NS(smmu) \
70 ((smmu)->base + \
71 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
72 ? 0x400 : 0))
73
668b4ada
TC
74#ifdef CONFIG_64BIT
75#define smmu_writeq writeq_relaxed
76#else
77#define smmu_writeq(reg64, addr) \
78 do { \
79 u64 __val = (reg64); \
80 void __iomem *__addr = (addr); \
81 writel_relaxed(__val >> 32, __addr + 4); \
82 writel_relaxed(__val, __addr); \
83 } while (0)
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)
91#define sCR0_GCFGFRE (1 << 4)
92#define sCR0_GCFGFIE (1 << 5)
93#define sCR0_USFCFG (1 << 10)
94#define sCR0_VMIDPNE (1 << 11)
95#define sCR0_PTM (1 << 12)
96#define sCR0_FB (1 << 13)
97#define sCR0_BSU_SHIFT 14
98#define sCR0_BSU_MASK 0x3
99
100/* Identification registers */
101#define ARM_SMMU_GR0_ID0 0x20
102#define ARM_SMMU_GR0_ID1 0x24
103#define ARM_SMMU_GR0_ID2 0x28
104#define ARM_SMMU_GR0_ID3 0x2c
105#define ARM_SMMU_GR0_ID4 0x30
106#define ARM_SMMU_GR0_ID5 0x34
107#define ARM_SMMU_GR0_ID6 0x38
108#define ARM_SMMU_GR0_ID7 0x3c
109#define ARM_SMMU_GR0_sGFSR 0x48
110#define ARM_SMMU_GR0_sGFSYNR0 0x50
111#define ARM_SMMU_GR0_sGFSYNR1 0x54
112#define ARM_SMMU_GR0_sGFSYNR2 0x58
45ae7cff
WD
113
114#define ID0_S1TS (1 << 30)
115#define ID0_S2TS (1 << 29)
116#define ID0_NTS (1 << 28)
117#define ID0_SMS (1 << 27)
859a732e 118#define ID0_ATOSNS (1 << 26)
45ae7cff
WD
119#define ID0_CTTW (1 << 14)
120#define ID0_NUMIRPT_SHIFT 16
121#define ID0_NUMIRPT_MASK 0xff
3c8766d0
OH
122#define ID0_NUMSIDB_SHIFT 9
123#define ID0_NUMSIDB_MASK 0xf
45ae7cff
WD
124#define ID0_NUMSMRG_SHIFT 0
125#define ID0_NUMSMRG_MASK 0xff
126
127#define ID1_PAGESIZE (1 << 31)
128#define ID1_NUMPAGENDXB_SHIFT 28
129#define ID1_NUMPAGENDXB_MASK 7
130#define ID1_NUMS2CB_SHIFT 16
131#define ID1_NUMS2CB_MASK 0xff
132#define ID1_NUMCB_SHIFT 0
133#define ID1_NUMCB_MASK 0xff
134
135#define ID2_OAS_SHIFT 4
136#define ID2_OAS_MASK 0xf
137#define ID2_IAS_SHIFT 0
138#define ID2_IAS_MASK 0xf
139#define ID2_UBS_SHIFT 8
140#define ID2_UBS_MASK 0xf
141#define ID2_PTFS_4K (1 << 12)
142#define ID2_PTFS_16K (1 << 13)
143#define ID2_PTFS_64K (1 << 14)
144
45ae7cff 145/* Global TLB invalidation */
45ae7cff
WD
146#define ARM_SMMU_GR0_TLBIVMID 0x64
147#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
148#define ARM_SMMU_GR0_TLBIALLH 0x6c
149#define ARM_SMMU_GR0_sTLBGSYNC 0x70
150#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
151#define sTLBGSTATUS_GSACTIVE (1 << 0)
152#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
153
154/* Stream mapping registers */
155#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
156#define SMR_VALID (1 << 31)
157#define SMR_MASK_SHIFT 16
158#define SMR_MASK_MASK 0x7fff
159#define SMR_ID_SHIFT 0
160#define SMR_ID_MASK 0x7fff
161
162#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
163#define S2CR_CBNDX_SHIFT 0
164#define S2CR_CBNDX_MASK 0xff
165#define S2CR_TYPE_SHIFT 16
166#define S2CR_TYPE_MASK 0x3
167#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
168#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
169#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
170
d346180e
RM
171#define S2CR_PRIVCFG_SHIFT 24
172#define S2CR_PRIVCFG_UNPRIV (2 << S2CR_PRIVCFG_SHIFT)
173
45ae7cff
WD
174/* Context bank attribute registers */
175#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
176#define CBAR_VMID_SHIFT 0
177#define CBAR_VMID_MASK 0xff
57ca90f6
WD
178#define CBAR_S1_BPSHCFG_SHIFT 8
179#define CBAR_S1_BPSHCFG_MASK 3
180#define CBAR_S1_BPSHCFG_NSH 3
45ae7cff
WD
181#define CBAR_S1_MEMATTR_SHIFT 12
182#define CBAR_S1_MEMATTR_MASK 0xf
183#define CBAR_S1_MEMATTR_WB 0xf
184#define CBAR_TYPE_SHIFT 16
185#define CBAR_TYPE_MASK 0x3
186#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
187#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
188#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
189#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
190#define CBAR_IRPTNDX_SHIFT 24
191#define CBAR_IRPTNDX_MASK 0xff
192
193#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
194#define CBA2R_RW64_32BIT (0 << 0)
195#define CBA2R_RW64_64BIT (1 << 0)
196
197/* Translation context bank */
198#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
c757e852 199#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
45ae7cff
WD
200
201#define ARM_SMMU_CB_SCTLR 0x0
202#define ARM_SMMU_CB_RESUME 0x8
203#define ARM_SMMU_CB_TTBCR2 0x10
668b4ada
TC
204#define ARM_SMMU_CB_TTBR0 0x20
205#define ARM_SMMU_CB_TTBR1 0x28
45ae7cff
WD
206#define ARM_SMMU_CB_TTBCR 0x30
207#define ARM_SMMU_CB_S1_MAIR0 0x38
518f7136 208#define ARM_SMMU_CB_S1_MAIR1 0x3c
859a732e
MH
209#define ARM_SMMU_CB_PAR_LO 0x50
210#define ARM_SMMU_CB_PAR_HI 0x54
45ae7cff
WD
211#define ARM_SMMU_CB_FSR 0x58
212#define ARM_SMMU_CB_FAR_LO 0x60
213#define ARM_SMMU_CB_FAR_HI 0x64
214#define ARM_SMMU_CB_FSYNR0 0x68
518f7136 215#define ARM_SMMU_CB_S1_TLBIVA 0x600
1463fe44 216#define ARM_SMMU_CB_S1_TLBIASID 0x610
518f7136
WD
217#define ARM_SMMU_CB_S1_TLBIVAL 0x620
218#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
219#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
661d962f 220#define ARM_SMMU_CB_ATS1PR 0x800
859a732e 221#define ARM_SMMU_CB_ATSR 0x8f0
45ae7cff
WD
222
223#define SCTLR_S1_ASIDPNE (1 << 12)
224#define SCTLR_CFCFG (1 << 7)
225#define SCTLR_CFIE (1 << 6)
226#define SCTLR_CFRE (1 << 5)
227#define SCTLR_E (1 << 4)
228#define SCTLR_AFE (1 << 2)
229#define SCTLR_TRE (1 << 1)
230#define SCTLR_M (1 << 0)
231#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
232
859a732e
MH
233#define CB_PAR_F (1 << 0)
234
235#define ATSR_ACTIVE (1 << 0)
236
45ae7cff
WD
237#define RESUME_RETRY (0 << 0)
238#define RESUME_TERMINATE (1 << 0)
239
45ae7cff 240#define TTBCR2_SEP_SHIFT 15
5dc5616e 241#define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
45ae7cff 242
668b4ada 243#define TTBRn_ASID_SHIFT 48
45ae7cff
WD
244
245#define FSR_MULTI (1 << 31)
246#define FSR_SS (1 << 30)
247#define FSR_UUT (1 << 8)
248#define FSR_ASF (1 << 7)
249#define FSR_TLBLKF (1 << 6)
250#define FSR_TLBMCF (1 << 5)
251#define FSR_EF (1 << 4)
252#define FSR_PF (1 << 3)
253#define FSR_AFF (1 << 2)
254#define FSR_TF (1 << 1)
255
2907320d
MH
256#define FSR_IGN (FSR_AFF | FSR_ASF | \
257 FSR_TLBMCF | FSR_TLBLKF)
258#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
adaba320 259 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
45ae7cff
WD
260
261#define FSYNR0_WNR (1 << 4)
262
4cf740b0 263static int force_stage;
25a1c96c 264module_param(force_stage, int, S_IRUGO);
4cf740b0
WD
265MODULE_PARM_DESC(force_stage,
266 "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
267static bool disable_bypass;
268module_param(disable_bypass, bool, S_IRUGO);
269MODULE_PARM_DESC(disable_bypass,
270 "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 271
09360403
RM
272enum arm_smmu_arch_version {
273 ARM_SMMU_V1 = 1,
274 ARM_SMMU_V2,
275};
276
45ae7cff
WD
277struct arm_smmu_smr {
278 u8 idx;
279 u16 mask;
280 u16 id;
281};
282
a9a1b0b5 283struct arm_smmu_master_cfg {
45ae7cff
WD
284 int num_streamids;
285 u16 streamids[MAX_MASTER_STREAMIDS];
45ae7cff
WD
286 struct arm_smmu_smr *smrs;
287};
288
a9a1b0b5
WD
289struct arm_smmu_master {
290 struct device_node *of_node;
a9a1b0b5
WD
291 struct rb_node node;
292 struct arm_smmu_master_cfg cfg;
293};
294
45ae7cff
WD
295struct arm_smmu_device {
296 struct device *dev;
45ae7cff
WD
297
298 void __iomem *base;
299 unsigned long size;
c757e852 300 unsigned long pgshift;
45ae7cff
WD
301
302#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
303#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
304#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
305#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
306#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
859a732e 307#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
45ae7cff 308 u32 features;
3a5df8ff
AH
309
310#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
311 u32 options;
09360403 312 enum arm_smmu_arch_version version;
45ae7cff
WD
313
314 u32 num_context_banks;
315 u32 num_s2_context_banks;
316 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
317 atomic_t irptndx;
318
319 u32 num_mapping_groups;
320 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
321
518f7136
WD
322 unsigned long va_size;
323 unsigned long ipa_size;
324 unsigned long pa_size;
45ae7cff
WD
325
326 u32 num_global_irqs;
327 u32 num_context_irqs;
328 unsigned int *irqs;
329
45ae7cff
WD
330 struct list_head list;
331 struct rb_root masters;
332};
333
334struct arm_smmu_cfg {
45ae7cff
WD
335 u8 cbndx;
336 u8 irptndx;
337 u32 cbar;
45ae7cff 338};
faea13b7 339#define INVALID_IRPTNDX 0xff
45ae7cff 340
ecfadb6e
WD
341#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
342#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
343
c752ce45
WD
344enum arm_smmu_domain_stage {
345 ARM_SMMU_DOMAIN_S1 = 0,
346 ARM_SMMU_DOMAIN_S2,
347 ARM_SMMU_DOMAIN_NESTED,
348};
349
45ae7cff 350struct arm_smmu_domain {
44680eed 351 struct arm_smmu_device *smmu;
518f7136
WD
352 struct io_pgtable_ops *pgtbl_ops;
353 spinlock_t pgtbl_lock;
44680eed 354 struct arm_smmu_cfg cfg;
c752ce45 355 enum arm_smmu_domain_stage stage;
518f7136 356 struct mutex init_mutex; /* Protects smmu pointer */
1d672638 357 struct iommu_domain domain;
45ae7cff
WD
358};
359
518f7136
WD
360static struct iommu_ops arm_smmu_ops;
361
45ae7cff
WD
362static DEFINE_SPINLOCK(arm_smmu_devices_lock);
363static LIST_HEAD(arm_smmu_devices);
364
3a5df8ff
AH
365struct arm_smmu_option_prop {
366 u32 opt;
367 const char *prop;
368};
369
2907320d 370static struct arm_smmu_option_prop arm_smmu_options[] = {
3a5df8ff
AH
371 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
372 { 0, NULL},
373};
374
1d672638
JR
375static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
376{
377 return container_of(dom, struct arm_smmu_domain, domain);
378}
379
3a5df8ff
AH
380static void parse_driver_options(struct arm_smmu_device *smmu)
381{
382 int i = 0;
2907320d 383
3a5df8ff
AH
384 do {
385 if (of_property_read_bool(smmu->dev->of_node,
386 arm_smmu_options[i].prop)) {
387 smmu->options |= arm_smmu_options[i].opt;
388 dev_notice(smmu->dev, "option %s\n",
389 arm_smmu_options[i].prop);
390 }
391 } while (arm_smmu_options[++i].opt);
392}
393
8f68f8e2 394static struct device_node *dev_get_dev_node(struct device *dev)
a9a1b0b5
WD
395{
396 if (dev_is_pci(dev)) {
397 struct pci_bus *bus = to_pci_dev(dev)->bus;
2907320d 398
a9a1b0b5
WD
399 while (!pci_is_root_bus(bus))
400 bus = bus->parent;
8f68f8e2 401 return bus->bridge->parent->of_node;
a9a1b0b5
WD
402 }
403
8f68f8e2 404 return dev->of_node;
a9a1b0b5
WD
405}
406
45ae7cff
WD
407static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
408 struct device_node *dev_node)
409{
410 struct rb_node *node = smmu->masters.rb_node;
411
412 while (node) {
413 struct arm_smmu_master *master;
2907320d 414
45ae7cff
WD
415 master = container_of(node, struct arm_smmu_master, node);
416
417 if (dev_node < master->of_node)
418 node = node->rb_left;
419 else if (dev_node > master->of_node)
420 node = node->rb_right;
421 else
422 return master;
423 }
424
425 return NULL;
426}
427
a9a1b0b5 428static struct arm_smmu_master_cfg *
8f68f8e2 429find_smmu_master_cfg(struct device *dev)
a9a1b0b5 430{
8f68f8e2
WD
431 struct arm_smmu_master_cfg *cfg = NULL;
432 struct iommu_group *group = iommu_group_get(dev);
a9a1b0b5 433
8f68f8e2
WD
434 if (group) {
435 cfg = iommu_group_get_iommudata(group);
436 iommu_group_put(group);
437 }
a9a1b0b5 438
8f68f8e2 439 return cfg;
a9a1b0b5
WD
440}
441
45ae7cff
WD
442static int insert_smmu_master(struct arm_smmu_device *smmu,
443 struct arm_smmu_master *master)
444{
445 struct rb_node **new, *parent;
446
447 new = &smmu->masters.rb_node;
448 parent = NULL;
449 while (*new) {
2907320d
MH
450 struct arm_smmu_master *this
451 = container_of(*new, struct arm_smmu_master, node);
45ae7cff
WD
452
453 parent = *new;
454 if (master->of_node < this->of_node)
455 new = &((*new)->rb_left);
456 else if (master->of_node > this->of_node)
457 new = &((*new)->rb_right);
458 else
459 return -EEXIST;
460 }
461
462 rb_link_node(&master->node, parent, new);
463 rb_insert_color(&master->node, &smmu->masters);
464 return 0;
465}
466
467static int register_smmu_master(struct arm_smmu_device *smmu,
468 struct device *dev,
469 struct of_phandle_args *masterspec)
470{
471 int i;
472 struct arm_smmu_master *master;
473
474 master = find_smmu_master(smmu, masterspec->np);
475 if (master) {
476 dev_err(dev,
477 "rejecting multiple registrations for master device %s\n",
478 masterspec->np->name);
479 return -EBUSY;
480 }
481
482 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
483 dev_err(dev,
484 "reached maximum number (%d) of stream IDs for master device %s\n",
485 MAX_MASTER_STREAMIDS, masterspec->np->name);
486 return -ENOSPC;
487 }
488
489 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
490 if (!master)
491 return -ENOMEM;
492
a9a1b0b5
WD
493 master->of_node = masterspec->np;
494 master->cfg.num_streamids = masterspec->args_count;
45ae7cff 495
3c8766d0
OH
496 for (i = 0; i < master->cfg.num_streamids; ++i) {
497 u16 streamid = masterspec->args[i];
45ae7cff 498
3c8766d0
OH
499 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
500 (streamid >= smmu->num_mapping_groups)) {
501 dev_err(dev,
502 "stream ID for master device %s greater than maximum allowed (%d)\n",
503 masterspec->np->name, smmu->num_mapping_groups);
504 return -ERANGE;
505 }
506 master->cfg.streamids[i] = streamid;
507 }
45ae7cff
WD
508 return insert_smmu_master(smmu, master);
509}
510
44680eed 511static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
45ae7cff 512{
44680eed 513 struct arm_smmu_device *smmu;
a9a1b0b5 514 struct arm_smmu_master *master = NULL;
8f68f8e2 515 struct device_node *dev_node = dev_get_dev_node(dev);
45ae7cff
WD
516
517 spin_lock(&arm_smmu_devices_lock);
44680eed 518 list_for_each_entry(smmu, &arm_smmu_devices, list) {
a9a1b0b5
WD
519 master = find_smmu_master(smmu, dev_node);
520 if (master)
521 break;
522 }
45ae7cff 523 spin_unlock(&arm_smmu_devices_lock);
44680eed 524
a9a1b0b5 525 return master ? smmu : NULL;
45ae7cff
WD
526}
527
528static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
529{
530 int idx;
531
532 do {
533 idx = find_next_zero_bit(map, end, start);
534 if (idx == end)
535 return -ENOSPC;
536 } while (test_and_set_bit(idx, map));
537
538 return idx;
539}
540
541static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
542{
543 clear_bit(idx, map);
544}
545
546/* Wait for any pending TLB invalidations to complete */
518f7136 547static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
45ae7cff
WD
548{
549 int count = 0;
550 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
551
552 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
553 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
554 & sTLBGSTATUS_GSACTIVE) {
555 cpu_relax();
556 if (++count == TLB_LOOP_TIMEOUT) {
557 dev_err_ratelimited(smmu->dev,
558 "TLB sync timed out -- SMMU may be deadlocked\n");
559 return;
560 }
561 udelay(1);
562 }
563}
564
518f7136
WD
565static void arm_smmu_tlb_sync(void *cookie)
566{
567 struct arm_smmu_domain *smmu_domain = cookie;
568 __arm_smmu_tlb_sync(smmu_domain->smmu);
569}
570
571static void arm_smmu_tlb_inv_context(void *cookie)
1463fe44 572{
518f7136 573 struct arm_smmu_domain *smmu_domain = cookie;
44680eed
WD
574 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
575 struct arm_smmu_device *smmu = smmu_domain->smmu;
1463fe44 576 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
518f7136 577 void __iomem *base;
1463fe44
WD
578
579 if (stage1) {
580 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
ecfadb6e
WD
581 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
582 base + ARM_SMMU_CB_S1_TLBIASID);
1463fe44
WD
583 } else {
584 base = ARM_SMMU_GR0(smmu);
ecfadb6e
WD
585 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
586 base + ARM_SMMU_GR0_TLBIVMID);
1463fe44
WD
587 }
588
518f7136
WD
589 __arm_smmu_tlb_sync(smmu);
590}
591
592static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
06c610e8 593 size_t granule, bool leaf, void *cookie)
518f7136
WD
594{
595 struct arm_smmu_domain *smmu_domain = cookie;
596 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
597 struct arm_smmu_device *smmu = smmu_domain->smmu;
598 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
599 void __iomem *reg;
600
601 if (stage1) {
602 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
603 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
604
605 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
606 iova &= ~12UL;
607 iova |= ARM_SMMU_CB_ASID(cfg);
75df1386
RM
608 do {
609 writel_relaxed(iova, reg);
610 iova += granule;
611 } while (size -= granule);
518f7136
WD
612#ifdef CONFIG_64BIT
613 } else {
614 iova >>= 12;
615 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
75df1386
RM
616 do {
617 writeq_relaxed(iova, reg);
618 iova += granule >> 12;
619 } while (size -= granule);
518f7136
WD
620#endif
621 }
622#ifdef CONFIG_64BIT
623 } else if (smmu->version == ARM_SMMU_V2) {
624 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
625 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
626 ARM_SMMU_CB_S2_TLBIIPAS2;
75df1386
RM
627 iova >>= 12;
628 do {
629 writeq_relaxed(iova, reg);
630 iova += granule >> 12;
631 } while (size -= granule);
518f7136
WD
632#endif
633 } else {
634 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
635 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
636 }
637}
638
518f7136
WD
639static struct iommu_gather_ops arm_smmu_gather_ops = {
640 .tlb_flush_all = arm_smmu_tlb_inv_context,
641 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
642 .tlb_sync = arm_smmu_tlb_sync,
518f7136
WD
643};
644
45ae7cff
WD
645static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
646{
647 int flags, ret;
648 u32 fsr, far, fsynr, resume;
649 unsigned long iova;
650 struct iommu_domain *domain = dev;
1d672638 651 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed
WD
652 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
653 struct arm_smmu_device *smmu = smmu_domain->smmu;
45ae7cff
WD
654 void __iomem *cb_base;
655
44680eed 656 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
45ae7cff
WD
657 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
658
659 if (!(fsr & FSR_FAULT))
660 return IRQ_NONE;
661
662 if (fsr & FSR_IGN)
663 dev_err_ratelimited(smmu->dev,
70c9a7db 664 "Unexpected context fault (fsr 0x%x)\n",
45ae7cff
WD
665 fsr);
666
667 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
668 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
669
670 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
671 iova = far;
672#ifdef CONFIG_64BIT
673 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
674 iova |= ((unsigned long)far << 32);
675#endif
676
677 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
678 ret = IRQ_HANDLED;
679 resume = RESUME_RETRY;
680 } else {
2ef0f031
AH
681 dev_err_ratelimited(smmu->dev,
682 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
44680eed 683 iova, fsynr, cfg->cbndx);
45ae7cff
WD
684 ret = IRQ_NONE;
685 resume = RESUME_TERMINATE;
686 }
687
688 /* Clear the faulting FSR */
689 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
690
691 /* Retry or terminate any stalled transactions */
692 if (fsr & FSR_SS)
693 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
694
695 return ret;
696}
697
698static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
699{
700 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
701 struct arm_smmu_device *smmu = dev;
3a5df8ff 702 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
45ae7cff
WD
703
704 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
705 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
706 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
707 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
708
3a5df8ff
AH
709 if (!gfsr)
710 return IRQ_NONE;
711
45ae7cff
WD
712 dev_err_ratelimited(smmu->dev,
713 "Unexpected global fault, this could be serious\n");
714 dev_err_ratelimited(smmu->dev,
715 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
716 gfsr, gfsynr0, gfsynr1, gfsynr2);
717
718 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
adaba320 719 return IRQ_HANDLED;
45ae7cff
WD
720}
721
518f7136
WD
722static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
723 struct io_pgtable_cfg *pgtbl_cfg)
45ae7cff
WD
724{
725 u32 reg;
668b4ada 726 u64 reg64;
45ae7cff 727 bool stage1;
44680eed
WD
728 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
729 struct arm_smmu_device *smmu = smmu_domain->smmu;
c88ae5de 730 void __iomem *cb_base, *gr1_base;
45ae7cff 731
45ae7cff 732 gr1_base = ARM_SMMU_GR1(smmu);
44680eed
WD
733 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
734 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
45ae7cff 735
4a1c93cb
WD
736 if (smmu->version > ARM_SMMU_V1) {
737 /*
738 * CBA2R.
739 * *Must* be initialised before CBAR thanks to VMID16
740 * architectural oversight affected some implementations.
741 */
742#ifdef CONFIG_64BIT
743 reg = CBA2R_RW64_64BIT;
744#else
745 reg = CBA2R_RW64_32BIT;
746#endif
747 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
748 }
749
45ae7cff 750 /* CBAR */
44680eed 751 reg = cfg->cbar;
09360403 752 if (smmu->version == ARM_SMMU_V1)
2907320d 753 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
45ae7cff 754
57ca90f6
WD
755 /*
756 * Use the weakest shareability/memory types, so they are
757 * overridden by the ttbcr/pte.
758 */
759 if (stage1) {
760 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
761 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
762 } else {
44680eed 763 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
57ca90f6 764 }
44680eed 765 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
45ae7cff 766
518f7136
WD
767 /* TTBRs */
768 if (stage1) {
668b4ada
TC
769 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
770
771 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT;
772 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0);
773
774 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
775 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT;
776 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR1);
518f7136 777 } else {
668b4ada
TC
778 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
779 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0);
518f7136 780 }
a65217a4 781
518f7136
WD
782 /* TTBCR */
783 if (stage1) {
784 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
785 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
786 if (smmu->version > ARM_SMMU_V1) {
787 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
5dc5616e 788 reg |= TTBCR2_SEP_UPSTREAM;
518f7136 789 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
45ae7cff
WD
790 }
791 } else {
518f7136
WD
792 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
793 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
45ae7cff
WD
794 }
795
518f7136 796 /* MAIRs (stage-1 only) */
45ae7cff 797 if (stage1) {
518f7136 798 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
45ae7cff 799 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
518f7136
WD
800 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
801 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
45ae7cff
WD
802 }
803
45ae7cff
WD
804 /* SCTLR */
805 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
806 if (stage1)
807 reg |= SCTLR_S1_ASIDPNE;
808#ifdef __BIG_ENDIAN
809 reg |= SCTLR_E;
810#endif
25724841 811 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
45ae7cff
WD
812}
813
814static int arm_smmu_init_domain_context(struct iommu_domain *domain,
44680eed 815 struct arm_smmu_device *smmu)
45ae7cff 816{
a18037b2 817 int irq, start, ret = 0;
518f7136
WD
818 unsigned long ias, oas;
819 struct io_pgtable_ops *pgtbl_ops;
820 struct io_pgtable_cfg pgtbl_cfg;
821 enum io_pgtable_fmt fmt;
1d672638 822 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed 823 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
45ae7cff 824
518f7136 825 mutex_lock(&smmu_domain->init_mutex);
a18037b2
MH
826 if (smmu_domain->smmu)
827 goto out_unlock;
828
c752ce45
WD
829 /*
830 * Mapping the requested stage onto what we support is surprisingly
831 * complicated, mainly because the spec allows S1+S2 SMMUs without
832 * support for nested translation. That means we end up with the
833 * following table:
834 *
835 * Requested Supported Actual
836 * S1 N S1
837 * S1 S1+S2 S1
838 * S1 S2 S2
839 * S1 S1 S1
840 * N N N
841 * N S1+S2 S2
842 * N S2 S2
843 * N S1 S1
844 *
845 * Note that you can't actually request stage-2 mappings.
846 */
847 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
848 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
849 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
850 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
851
852 switch (smmu_domain->stage) {
853 case ARM_SMMU_DOMAIN_S1:
854 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
855 start = smmu->num_s2_context_banks;
518f7136
WD
856 ias = smmu->va_size;
857 oas = smmu->ipa_size;
858 if (IS_ENABLED(CONFIG_64BIT))
859 fmt = ARM_64_LPAE_S1;
860 else
861 fmt = ARM_32_LPAE_S1;
c752ce45
WD
862 break;
863 case ARM_SMMU_DOMAIN_NESTED:
45ae7cff
WD
864 /*
865 * We will likely want to change this if/when KVM gets
866 * involved.
867 */
c752ce45 868 case ARM_SMMU_DOMAIN_S2:
9c5c92e3
WD
869 cfg->cbar = CBAR_TYPE_S2_TRANS;
870 start = 0;
518f7136
WD
871 ias = smmu->ipa_size;
872 oas = smmu->pa_size;
873 if (IS_ENABLED(CONFIG_64BIT))
874 fmt = ARM_64_LPAE_S2;
875 else
876 fmt = ARM_32_LPAE_S2;
c752ce45
WD
877 break;
878 default:
879 ret = -EINVAL;
880 goto out_unlock;
45ae7cff
WD
881 }
882
883 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
884 smmu->num_context_banks);
885 if (IS_ERR_VALUE(ret))
a18037b2 886 goto out_unlock;
45ae7cff 887
44680eed 888 cfg->cbndx = ret;
09360403 889 if (smmu->version == ARM_SMMU_V1) {
44680eed
WD
890 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
891 cfg->irptndx %= smmu->num_context_irqs;
45ae7cff 892 } else {
44680eed 893 cfg->irptndx = cfg->cbndx;
45ae7cff
WD
894 }
895
518f7136
WD
896 pgtbl_cfg = (struct io_pgtable_cfg) {
897 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
898 .ias = ias,
899 .oas = oas,
900 .tlb = &arm_smmu_gather_ops,
2df7a25c 901 .iommu_dev = smmu->dev,
518f7136
WD
902 };
903
904 smmu_domain->smmu = smmu;
905 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
906 if (!pgtbl_ops) {
907 ret = -ENOMEM;
908 goto out_clear_smmu;
909 }
910
911 /* Update our support page sizes to reflect the page table format */
912 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
a18037b2 913
518f7136
WD
914 /* Initialise the context bank with our page table cfg */
915 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
916
917 /*
918 * Request context fault interrupt. Do this last to avoid the
919 * handler seeing a half-initialised domain state.
920 */
44680eed 921 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
45ae7cff
WD
922 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
923 "arm-smmu-context-fault", domain);
924 if (IS_ERR_VALUE(ret)) {
925 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
44680eed
WD
926 cfg->irptndx, irq);
927 cfg->irptndx = INVALID_IRPTNDX;
45ae7cff
WD
928 }
929
518f7136
WD
930 mutex_unlock(&smmu_domain->init_mutex);
931
932 /* Publish page table ops for map/unmap */
933 smmu_domain->pgtbl_ops = pgtbl_ops;
a9a1b0b5 934 return 0;
45ae7cff 935
518f7136
WD
936out_clear_smmu:
937 smmu_domain->smmu = NULL;
a18037b2 938out_unlock:
518f7136 939 mutex_unlock(&smmu_domain->init_mutex);
45ae7cff
WD
940 return ret;
941}
942
943static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
944{
1d672638 945 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed
WD
946 struct arm_smmu_device *smmu = smmu_domain->smmu;
947 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1463fe44 948 void __iomem *cb_base;
45ae7cff
WD
949 int irq;
950
951 if (!smmu)
952 return;
953
518f7136
WD
954 /*
955 * Disable the context bank and free the page tables before freeing
956 * it.
957 */
44680eed 958 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1463fe44 959 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1463fe44 960
44680eed
WD
961 if (cfg->irptndx != INVALID_IRPTNDX) {
962 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
45ae7cff
WD
963 free_irq(irq, domain);
964 }
965
44830b0c 966 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
44680eed 967 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
45ae7cff
WD
968}
969
1d672638 970static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
45ae7cff
WD
971{
972 struct arm_smmu_domain *smmu_domain;
45ae7cff 973
9adb9594 974 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
1d672638 975 return NULL;
45ae7cff
WD
976 /*
977 * Allocate the domain and initialise some of its data structures.
978 * We can't really do anything meaningful until we've added a
979 * master.
980 */
981 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
982 if (!smmu_domain)
1d672638 983 return NULL;
45ae7cff 984
9adb9594
RM
985 if (type == IOMMU_DOMAIN_DMA &&
986 iommu_get_dma_cookie(&smmu_domain->domain)) {
987 kfree(smmu_domain);
988 return NULL;
989 }
990
518f7136
WD
991 mutex_init(&smmu_domain->init_mutex);
992 spin_lock_init(&smmu_domain->pgtbl_lock);
1d672638
JR
993
994 return &smmu_domain->domain;
45ae7cff
WD
995}
996
1d672638 997static void arm_smmu_domain_free(struct iommu_domain *domain)
45ae7cff 998{
1d672638 999 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1463fe44
WD
1000
1001 /*
1002 * Free the domain resources. We assume that all devices have
1003 * already been detached.
1004 */
9adb9594 1005 iommu_put_dma_cookie(domain);
45ae7cff 1006 arm_smmu_destroy_domain_context(domain);
45ae7cff
WD
1007 kfree(smmu_domain);
1008}
1009
1010static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
a9a1b0b5 1011 struct arm_smmu_master_cfg *cfg)
45ae7cff
WD
1012{
1013 int i;
1014 struct arm_smmu_smr *smrs;
1015 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1016
1017 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1018 return 0;
1019
a9a1b0b5 1020 if (cfg->smrs)
45ae7cff
WD
1021 return -EEXIST;
1022
2907320d 1023 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
45ae7cff 1024 if (!smrs) {
a9a1b0b5
WD
1025 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1026 cfg->num_streamids);
45ae7cff
WD
1027 return -ENOMEM;
1028 }
1029
44680eed 1030 /* Allocate the SMRs on the SMMU */
a9a1b0b5 1031 for (i = 0; i < cfg->num_streamids; ++i) {
45ae7cff
WD
1032 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1033 smmu->num_mapping_groups);
1034 if (IS_ERR_VALUE(idx)) {
1035 dev_err(smmu->dev, "failed to allocate free SMR\n");
1036 goto err_free_smrs;
1037 }
1038
1039 smrs[i] = (struct arm_smmu_smr) {
1040 .idx = idx,
1041 .mask = 0, /* We don't currently share SMRs */
a9a1b0b5 1042 .id = cfg->streamids[i],
45ae7cff
WD
1043 };
1044 }
1045
1046 /* It worked! Now, poke the actual hardware */
a9a1b0b5 1047 for (i = 0; i < cfg->num_streamids; ++i) {
45ae7cff
WD
1048 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1049 smrs[i].mask << SMR_MASK_SHIFT;
1050 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1051 }
1052
a9a1b0b5 1053 cfg->smrs = smrs;
45ae7cff
WD
1054 return 0;
1055
1056err_free_smrs:
1057 while (--i >= 0)
1058 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1059 kfree(smrs);
1060 return -ENOSPC;
1061}
1062
1063static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
a9a1b0b5 1064 struct arm_smmu_master_cfg *cfg)
45ae7cff
WD
1065{
1066 int i;
1067 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
a9a1b0b5 1068 struct arm_smmu_smr *smrs = cfg->smrs;
45ae7cff 1069
43b412be
WD
1070 if (!smrs)
1071 return;
1072
45ae7cff 1073 /* Invalidate the SMRs before freeing back to the allocator */
a9a1b0b5 1074 for (i = 0; i < cfg->num_streamids; ++i) {
45ae7cff 1075 u8 idx = smrs[i].idx;
2907320d 1076
45ae7cff
WD
1077 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1078 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1079 }
1080
a9a1b0b5 1081 cfg->smrs = NULL;
45ae7cff
WD
1082 kfree(smrs);
1083}
1084
45ae7cff 1085static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
a9a1b0b5 1086 struct arm_smmu_master_cfg *cfg)
45ae7cff
WD
1087{
1088 int i, ret;
44680eed 1089 struct arm_smmu_device *smmu = smmu_domain->smmu;
45ae7cff
WD
1090 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1091
8f68f8e2 1092 /* Devices in an IOMMU group may already be configured */
a9a1b0b5 1093 ret = arm_smmu_master_configure_smrs(smmu, cfg);
45ae7cff 1094 if (ret)
8f68f8e2 1095 return ret == -EEXIST ? 0 : ret;
45ae7cff 1096
a9a1b0b5 1097 for (i = 0; i < cfg->num_streamids; ++i) {
45ae7cff 1098 u32 idx, s2cr;
2907320d 1099
a9a1b0b5 1100 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
d346180e 1101 s2cr = S2CR_TYPE_TRANS | S2CR_PRIVCFG_UNPRIV |
44680eed 1102 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
45ae7cff
WD
1103 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1104 }
1105
1106 return 0;
1107}
1108
1109static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
a9a1b0b5 1110 struct arm_smmu_master_cfg *cfg)
45ae7cff 1111{
43b412be 1112 int i;
44680eed 1113 struct arm_smmu_device *smmu = smmu_domain->smmu;
43b412be 1114 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
45ae7cff 1115
8f68f8e2
WD
1116 /* An IOMMU group is torn down by the first device to be removed */
1117 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1118 return;
45ae7cff
WD
1119
1120 /*
1121 * We *must* clear the S2CR first, because freeing the SMR means
1122 * that it can be re-allocated immediately.
1123 */
43b412be
WD
1124 for (i = 0; i < cfg->num_streamids; ++i) {
1125 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
25a1c96c 1126 u32 reg = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS;
43b412be 1127
25a1c96c 1128 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(idx));
43b412be
WD
1129 }
1130
a9a1b0b5 1131 arm_smmu_master_free_smrs(smmu, cfg);
45ae7cff
WD
1132}
1133
1134static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1135{
a18037b2 1136 int ret;
1d672638 1137 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1138 struct arm_smmu_device *smmu;
a9a1b0b5 1139 struct arm_smmu_master_cfg *cfg;
45ae7cff 1140
8f68f8e2 1141 smmu = find_smmu_for_device(dev);
44680eed 1142 if (!smmu) {
45ae7cff
WD
1143 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1144 return -ENXIO;
1145 }
1146
844e35bd
WD
1147 if (dev->archdata.iommu) {
1148 dev_err(dev, "already attached to IOMMU domain\n");
1149 return -EEXIST;
1150 }
1151
518f7136
WD
1152 /* Ensure that the domain is finalised */
1153 ret = arm_smmu_init_domain_context(domain, smmu);
1154 if (IS_ERR_VALUE(ret))
1155 return ret;
1156
45ae7cff 1157 /*
44680eed
WD
1158 * Sanity check the domain. We don't support domains across
1159 * different SMMUs.
45ae7cff 1160 */
518f7136 1161 if (smmu_domain->smmu != smmu) {
45ae7cff
WD
1162 dev_err(dev,
1163 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
a18037b2
MH
1164 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1165 return -EINVAL;
45ae7cff 1166 }
45ae7cff
WD
1167
1168 /* Looks ok, so add the device to the domain */
8f68f8e2 1169 cfg = find_smmu_master_cfg(dev);
a9a1b0b5 1170 if (!cfg)
45ae7cff
WD
1171 return -ENODEV;
1172
844e35bd
WD
1173 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1174 if (!ret)
1175 dev->archdata.iommu = domain;
45ae7cff
WD
1176 return ret;
1177}
1178
1179static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1180{
1d672638 1181 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
a9a1b0b5 1182 struct arm_smmu_master_cfg *cfg;
45ae7cff 1183
8f68f8e2 1184 cfg = find_smmu_master_cfg(dev);
844e35bd
WD
1185 if (!cfg)
1186 return;
1187
1188 dev->archdata.iommu = NULL;
1189 arm_smmu_domain_remove_master(smmu_domain, cfg);
45ae7cff
WD
1190}
1191
45ae7cff 1192static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
b410aed9 1193 phys_addr_t paddr, size_t size, int prot)
45ae7cff 1194{
518f7136
WD
1195 int ret;
1196 unsigned long flags;
1d672638 1197 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1198 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
45ae7cff 1199
518f7136 1200 if (!ops)
45ae7cff
WD
1201 return -ENODEV;
1202
518f7136
WD
1203 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1204 ret = ops->map(ops, iova, paddr, size, prot);
1205 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1206 return ret;
45ae7cff
WD
1207}
1208
1209static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1210 size_t size)
1211{
518f7136
WD
1212 size_t ret;
1213 unsigned long flags;
1d672638 1214 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1215 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
45ae7cff 1216
518f7136
WD
1217 if (!ops)
1218 return 0;
1219
1220 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1221 ret = ops->unmap(ops, iova, size);
1222 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1223 return ret;
45ae7cff
WD
1224}
1225
859a732e
MH
1226static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1227 dma_addr_t iova)
1228{
1d672638 1229 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
859a732e
MH
1230 struct arm_smmu_device *smmu = smmu_domain->smmu;
1231 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1232 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1233 struct device *dev = smmu->dev;
1234 void __iomem *cb_base;
1235 u32 tmp;
1236 u64 phys;
661d962f 1237 unsigned long va;
859a732e
MH
1238
1239 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1240
661d962f
RM
1241 /* ATS1 registers can only be written atomically */
1242 va = iova & ~0xfffUL;
661d962f 1243 if (smmu->version == ARM_SMMU_V2)
668b4ada 1244 smmu_writeq(va, cb_base + ARM_SMMU_CB_ATS1PR);
661d962f 1245 else
661d962f 1246 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
859a732e
MH
1247
1248 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1249 !(tmp & ATSR_ACTIVE), 5, 50)) {
1250 dev_err(dev,
077124c9 1251 "iova to phys timed out on %pad. Falling back to software table walk.\n",
859a732e
MH
1252 &iova);
1253 return ops->iova_to_phys(ops, iova);
1254 }
1255
1256 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1257 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1258
1259 if (phys & CB_PAR_F) {
1260 dev_err(dev, "translation fault!\n");
1261 dev_err(dev, "PAR = 0x%llx\n", phys);
1262 return 0;
1263 }
1264
1265 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1266}
1267
45ae7cff 1268static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
859a732e 1269 dma_addr_t iova)
45ae7cff 1270{
518f7136
WD
1271 phys_addr_t ret;
1272 unsigned long flags;
1d672638 1273 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
518f7136 1274 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
45ae7cff 1275
518f7136 1276 if (!ops)
a44a9791 1277 return 0;
45ae7cff 1278
518f7136 1279 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
83a60ed8
BR
1280 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1281 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
859a732e 1282 ret = arm_smmu_iova_to_phys_hard(domain, iova);
83a60ed8 1283 } else {
859a732e 1284 ret = ops->iova_to_phys(ops, iova);
83a60ed8
BR
1285 }
1286
518f7136 1287 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
859a732e 1288
518f7136 1289 return ret;
45ae7cff
WD
1290}
1291
1fd0c775 1292static bool arm_smmu_capable(enum iommu_cap cap)
45ae7cff 1293{
d0948945
WD
1294 switch (cap) {
1295 case IOMMU_CAP_CACHE_COHERENCY:
1fd0c775
JR
1296 /*
1297 * Return true here as the SMMU can always send out coherent
1298 * requests.
1299 */
1300 return true;
d0948945 1301 case IOMMU_CAP_INTR_REMAP:
1fd0c775 1302 return true; /* MSIs are just memory writes */
0029a8dd
AM
1303 case IOMMU_CAP_NOEXEC:
1304 return true;
d0948945 1305 default:
1fd0c775 1306 return false;
d0948945 1307 }
45ae7cff 1308}
45ae7cff 1309
a9a1b0b5
WD
1310static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1311{
1312 *((u16 *)data) = alias;
1313 return 0; /* Continue walking */
45ae7cff
WD
1314}
1315
8f68f8e2
WD
1316static void __arm_smmu_release_pci_iommudata(void *data)
1317{
1318 kfree(data);
1319}
1320
af659932
JR
1321static int arm_smmu_init_pci_device(struct pci_dev *pdev,
1322 struct iommu_group *group)
45ae7cff 1323{
03edb226 1324 struct arm_smmu_master_cfg *cfg;
af659932
JR
1325 u16 sid;
1326 int i;
a9a1b0b5 1327
03edb226
WD
1328 cfg = iommu_group_get_iommudata(group);
1329 if (!cfg) {
a9a1b0b5 1330 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
af659932
JR
1331 if (!cfg)
1332 return -ENOMEM;
a9a1b0b5 1333
03edb226
WD
1334 iommu_group_set_iommudata(group, cfg,
1335 __arm_smmu_release_pci_iommudata);
1336 }
8f68f8e2 1337
af659932
JR
1338 if (cfg->num_streamids >= MAX_MASTER_STREAMIDS)
1339 return -ENOSPC;
a9a1b0b5 1340
03edb226
WD
1341 /*
1342 * Assume Stream ID == Requester ID for now.
1343 * We need a way to describe the ID mappings in FDT.
1344 */
1345 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1346 for (i = 0; i < cfg->num_streamids; ++i)
1347 if (cfg->streamids[i] == sid)
1348 break;
1349
1350 /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
1351 if (i == cfg->num_streamids)
1352 cfg->streamids[cfg->num_streamids++] = sid;
5fc63a7c 1353
03edb226 1354 return 0;
45ae7cff
WD
1355}
1356
af659932
JR
1357static int arm_smmu_init_platform_device(struct device *dev,
1358 struct iommu_group *group)
03edb226 1359{
03edb226 1360 struct arm_smmu_device *smmu = find_smmu_for_device(dev);
af659932 1361 struct arm_smmu_master *master;
03edb226
WD
1362
1363 if (!smmu)
1364 return -ENODEV;
1365
1366 master = find_smmu_master(smmu, dev->of_node);
1367 if (!master)
1368 return -ENODEV;
1369
03edb226 1370 iommu_group_set_iommudata(group, &master->cfg, NULL);
af659932
JR
1371
1372 return 0;
03edb226
WD
1373}
1374
1375static int arm_smmu_add_device(struct device *dev)
1376{
af659932 1377 struct iommu_group *group;
03edb226 1378
af659932
JR
1379 group = iommu_group_get_for_dev(dev);
1380 if (IS_ERR(group))
1381 return PTR_ERR(group);
03edb226 1382
9a4a9d8c 1383 iommu_group_put(group);
af659932 1384 return 0;
03edb226
WD
1385}
1386
45ae7cff
WD
1387static void arm_smmu_remove_device(struct device *dev)
1388{
5fc63a7c 1389 iommu_group_remove_device(dev);
45ae7cff
WD
1390}
1391
af659932
JR
1392static struct iommu_group *arm_smmu_device_group(struct device *dev)
1393{
1394 struct iommu_group *group;
1395 int ret;
1396
1397 if (dev_is_pci(dev))
1398 group = pci_device_group(dev);
1399 else
1400 group = generic_device_group(dev);
1401
1402 if (IS_ERR(group))
1403 return group;
1404
1405 if (dev_is_pci(dev))
1406 ret = arm_smmu_init_pci_device(to_pci_dev(dev), group);
1407 else
1408 ret = arm_smmu_init_platform_device(dev, group);
1409
1410 if (ret) {
1411 iommu_group_put(group);
1412 group = ERR_PTR(ret);
1413 }
1414
1415 return group;
1416}
1417
c752ce45
WD
1418static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1419 enum iommu_attr attr, void *data)
1420{
1d672638 1421 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
c752ce45
WD
1422
1423 switch (attr) {
1424 case DOMAIN_ATTR_NESTING:
1425 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1426 return 0;
1427 default:
1428 return -ENODEV;
1429 }
1430}
1431
1432static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1433 enum iommu_attr attr, void *data)
1434{
518f7136 1435 int ret = 0;
1d672638 1436 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
c752ce45 1437
518f7136
WD
1438 mutex_lock(&smmu_domain->init_mutex);
1439
c752ce45
WD
1440 switch (attr) {
1441 case DOMAIN_ATTR_NESTING:
518f7136
WD
1442 if (smmu_domain->smmu) {
1443 ret = -EPERM;
1444 goto out_unlock;
1445 }
1446
c752ce45
WD
1447 if (*(int *)data)
1448 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1449 else
1450 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1451
518f7136 1452 break;
c752ce45 1453 default:
518f7136 1454 ret = -ENODEV;
c752ce45 1455 }
518f7136
WD
1456
1457out_unlock:
1458 mutex_unlock(&smmu_domain->init_mutex);
1459 return ret;
c752ce45
WD
1460}
1461
518f7136 1462static struct iommu_ops arm_smmu_ops = {
c752ce45 1463 .capable = arm_smmu_capable,
1d672638
JR
1464 .domain_alloc = arm_smmu_domain_alloc,
1465 .domain_free = arm_smmu_domain_free,
c752ce45
WD
1466 .attach_dev = arm_smmu_attach_dev,
1467 .detach_dev = arm_smmu_detach_dev,
1468 .map = arm_smmu_map,
1469 .unmap = arm_smmu_unmap,
76771c93 1470 .map_sg = default_iommu_map_sg,
c752ce45
WD
1471 .iova_to_phys = arm_smmu_iova_to_phys,
1472 .add_device = arm_smmu_add_device,
1473 .remove_device = arm_smmu_remove_device,
af659932 1474 .device_group = arm_smmu_device_group,
c752ce45
WD
1475 .domain_get_attr = arm_smmu_domain_get_attr,
1476 .domain_set_attr = arm_smmu_domain_set_attr,
518f7136 1477 .pgsize_bitmap = -1UL, /* Restricted during device attach */
45ae7cff
WD
1478};
1479
1480static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1481{
1482 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
659db6f6 1483 void __iomem *cb_base;
45ae7cff 1484 int i = 0;
659db6f6
AH
1485 u32 reg;
1486
3a5df8ff
AH
1487 /* clear global FSR */
1488 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1489 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
45ae7cff 1490
25a1c96c
RM
1491 /* Mark all SMRn as invalid and all S2CRn as bypass unless overridden */
1492 reg = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS;
45ae7cff 1493 for (i = 0; i < smmu->num_mapping_groups; ++i) {
3c8766d0 1494 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
25a1c96c 1495 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(i));
45ae7cff
WD
1496 }
1497
659db6f6
AH
1498 /* Make sure all context banks are disabled and clear CB_FSR */
1499 for (i = 0; i < smmu->num_context_banks; ++i) {
1500 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1501 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1502 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1503 }
1463fe44 1504
45ae7cff 1505 /* Invalidate the TLB, just in case */
45ae7cff
WD
1506 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1507 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1508
3a5df8ff 1509 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
659db6f6 1510
45ae7cff 1511 /* Enable fault reporting */
659db6f6 1512 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
45ae7cff
WD
1513
1514 /* Disable TLB broadcasting. */
659db6f6 1515 reg |= (sCR0_VMIDPNE | sCR0_PTM);
45ae7cff 1516
25a1c96c
RM
1517 /* Enable client access, handling unmatched streams as appropriate */
1518 reg &= ~sCR0_CLIENTPD;
1519 if (disable_bypass)
1520 reg |= sCR0_USFCFG;
1521 else
1522 reg &= ~sCR0_USFCFG;
45ae7cff
WD
1523
1524 /* Disable forced broadcasting */
659db6f6 1525 reg &= ~sCR0_FB;
45ae7cff
WD
1526
1527 /* Don't upgrade barriers */
659db6f6 1528 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
45ae7cff
WD
1529
1530 /* Push the button */
518f7136 1531 __arm_smmu_tlb_sync(smmu);
3a5df8ff 1532 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
45ae7cff
WD
1533}
1534
1535static int arm_smmu_id_size_to_bits(int size)
1536{
1537 switch (size) {
1538 case 0:
1539 return 32;
1540 case 1:
1541 return 36;
1542 case 2:
1543 return 40;
1544 case 3:
1545 return 42;
1546 case 4:
1547 return 44;
1548 case 5:
1549 default:
1550 return 48;
1551 }
1552}
1553
1554static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1555{
1556 unsigned long size;
1557 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1558 u32 id;
bae2c2d4 1559 bool cttw_dt, cttw_reg;
45ae7cff
WD
1560
1561 dev_notice(smmu->dev, "probing hardware configuration...\n");
45ae7cff
WD
1562 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1563
1564 /* ID0 */
1565 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
4cf740b0
WD
1566
1567 /* Restrict available stages based on module parameter */
1568 if (force_stage == 1)
1569 id &= ~(ID0_S2TS | ID0_NTS);
1570 else if (force_stage == 2)
1571 id &= ~(ID0_S1TS | ID0_NTS);
1572
45ae7cff
WD
1573 if (id & ID0_S1TS) {
1574 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1575 dev_notice(smmu->dev, "\tstage 1 translation\n");
1576 }
1577
1578 if (id & ID0_S2TS) {
1579 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1580 dev_notice(smmu->dev, "\tstage 2 translation\n");
1581 }
1582
1583 if (id & ID0_NTS) {
1584 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1585 dev_notice(smmu->dev, "\tnested translation\n");
1586 }
1587
1588 if (!(smmu->features &
4cf740b0 1589 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
45ae7cff
WD
1590 dev_err(smmu->dev, "\tno translation support!\n");
1591 return -ENODEV;
1592 }
1593
d38f0ff9 1594 if ((id & ID0_S1TS) && ((smmu->version == 1) || !(id & ID0_ATOSNS))) {
859a732e
MH
1595 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1596 dev_notice(smmu->dev, "\taddress translation ops\n");
1597 }
1598
bae2c2d4
RM
1599 /*
1600 * In order for DMA API calls to work properly, we must defer to what
1601 * the DT says about coherency, regardless of what the hardware claims.
1602 * Fortunately, this also opens up a workaround for systems where the
1603 * ID register value has ended up configured incorrectly.
1604 */
1605 cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
1606 cttw_reg = !!(id & ID0_CTTW);
1607 if (cttw_dt)
45ae7cff 1608 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
bae2c2d4
RM
1609 if (cttw_dt || cttw_reg)
1610 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1611 cttw_dt ? "" : "non-");
1612 if (cttw_dt != cttw_reg)
1613 dev_notice(smmu->dev,
1614 "\t(IDR0.CTTW overridden by dma-coherent property)\n");
45ae7cff
WD
1615
1616 if (id & ID0_SMS) {
1617 u32 smr, sid, mask;
1618
1619 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1620 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1621 ID0_NUMSMRG_MASK;
1622 if (smmu->num_mapping_groups == 0) {
1623 dev_err(smmu->dev,
1624 "stream-matching supported, but no SMRs present!\n");
1625 return -ENODEV;
1626 }
1627
1628 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1629 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1630 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1631 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1632
1633 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1634 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1635 if ((mask & sid) != sid) {
1636 dev_err(smmu->dev,
1637 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1638 mask, sid);
1639 return -ENODEV;
1640 }
1641
1642 dev_notice(smmu->dev,
1643 "\tstream matching with %u register groups, mask 0x%x",
1644 smmu->num_mapping_groups, mask);
3c8766d0
OH
1645 } else {
1646 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1647 ID0_NUMSIDB_MASK;
45ae7cff
WD
1648 }
1649
1650 /* ID1 */
1651 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
c757e852 1652 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
45ae7cff 1653
c55af7f7 1654 /* Check for size mismatch of SMMU address space from mapped region */
518f7136 1655 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
c757e852 1656 size *= 2 << smmu->pgshift;
c55af7f7 1657 if (smmu->size != size)
2907320d
MH
1658 dev_warn(smmu->dev,
1659 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1660 size, smmu->size);
45ae7cff 1661
518f7136 1662 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
45ae7cff
WD
1663 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1664 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1665 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1666 return -ENODEV;
1667 }
1668 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1669 smmu->num_context_banks, smmu->num_s2_context_banks);
1670
1671 /* ID2 */
1672 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1673 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
518f7136 1674 smmu->ipa_size = size;
45ae7cff 1675
518f7136 1676 /* The output mask is also applied for bypass */
45ae7cff 1677 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
518f7136 1678 smmu->pa_size = size;
45ae7cff 1679
f1d84548
RM
1680 /*
1681 * What the page table walker can address actually depends on which
1682 * descriptor format is in use, but since a) we don't know that yet,
1683 * and b) it can vary per context bank, this will have to do...
1684 */
1685 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1686 dev_warn(smmu->dev,
1687 "failed to set DMA mask for table walker\n");
1688
09360403 1689 if (smmu->version == ARM_SMMU_V1) {
518f7136
WD
1690 smmu->va_size = smmu->ipa_size;
1691 size = SZ_4K | SZ_2M | SZ_1G;
45ae7cff 1692 } else {
45ae7cff 1693 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
518f7136
WD
1694 smmu->va_size = arm_smmu_id_size_to_bits(size);
1695#ifndef CONFIG_64BIT
1696 smmu->va_size = min(32UL, smmu->va_size);
45ae7cff 1697#endif
518f7136
WD
1698 size = 0;
1699 if (id & ID2_PTFS_4K)
1700 size |= SZ_4K | SZ_2M | SZ_1G;
1701 if (id & ID2_PTFS_16K)
1702 size |= SZ_16K | SZ_32M;
1703 if (id & ID2_PTFS_64K)
1704 size |= SZ_64K | SZ_512M;
45ae7cff
WD
1705 }
1706
518f7136
WD
1707 arm_smmu_ops.pgsize_bitmap &= size;
1708 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1709
28d6007b
WD
1710 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1711 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
518f7136 1712 smmu->va_size, smmu->ipa_size);
28d6007b
WD
1713
1714 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1715 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
518f7136 1716 smmu->ipa_size, smmu->pa_size);
28d6007b 1717
45ae7cff
WD
1718 return 0;
1719}
1720
09b5269a 1721static const struct of_device_id arm_smmu_of_match[] = {
09360403
RM
1722 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1723 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1724 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
d3aba046 1725 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
09360403
RM
1726 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1727 { },
1728};
1729MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1730
45ae7cff
WD
1731static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1732{
09360403 1733 const struct of_device_id *of_id;
45ae7cff
WD
1734 struct resource *res;
1735 struct arm_smmu_device *smmu;
45ae7cff
WD
1736 struct device *dev = &pdev->dev;
1737 struct rb_node *node;
1738 struct of_phandle_args masterspec;
1739 int num_irqs, i, err;
1740
1741 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1742 if (!smmu) {
1743 dev_err(dev, "failed to allocate arm_smmu_device\n");
1744 return -ENOMEM;
1745 }
1746 smmu->dev = dev;
1747
09360403
RM
1748 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1749 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1750
45ae7cff 1751 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
8a7f4312
JL
1752 smmu->base = devm_ioremap_resource(dev, res);
1753 if (IS_ERR(smmu->base))
1754 return PTR_ERR(smmu->base);
45ae7cff 1755 smmu->size = resource_size(res);
45ae7cff
WD
1756
1757 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1758 &smmu->num_global_irqs)) {
1759 dev_err(dev, "missing #global-interrupts property\n");
1760 return -ENODEV;
1761 }
1762
1763 num_irqs = 0;
1764 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1765 num_irqs++;
1766 if (num_irqs > smmu->num_global_irqs)
1767 smmu->num_context_irqs++;
1768 }
1769
44a08de2
AH
1770 if (!smmu->num_context_irqs) {
1771 dev_err(dev, "found %d interrupts but expected at least %d\n",
1772 num_irqs, smmu->num_global_irqs + 1);
1773 return -ENODEV;
45ae7cff 1774 }
45ae7cff
WD
1775
1776 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1777 GFP_KERNEL);
1778 if (!smmu->irqs) {
1779 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1780 return -ENOMEM;
1781 }
1782
1783 for (i = 0; i < num_irqs; ++i) {
1784 int irq = platform_get_irq(pdev, i);
2907320d 1785
45ae7cff
WD
1786 if (irq < 0) {
1787 dev_err(dev, "failed to get irq index %d\n", i);
1788 return -ENODEV;
1789 }
1790 smmu->irqs[i] = irq;
1791 }
1792
3c8766d0
OH
1793 err = arm_smmu_device_cfg_probe(smmu);
1794 if (err)
1795 return err;
1796
45ae7cff
WD
1797 i = 0;
1798 smmu->masters = RB_ROOT;
1799 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1800 "#stream-id-cells", i,
1801 &masterspec)) {
1802 err = register_smmu_master(smmu, dev, &masterspec);
1803 if (err) {
1804 dev_err(dev, "failed to add master %s\n",
1805 masterspec.np->name);
1806 goto out_put_masters;
1807 }
1808
1809 i++;
1810 }
1811 dev_notice(dev, "registered %d master devices\n", i);
1812
3a5df8ff
AH
1813 parse_driver_options(smmu);
1814
09360403 1815 if (smmu->version > ARM_SMMU_V1 &&
45ae7cff
WD
1816 smmu->num_context_banks != smmu->num_context_irqs) {
1817 dev_err(dev,
1818 "found only %d context interrupt(s) but %d required\n",
1819 smmu->num_context_irqs, smmu->num_context_banks);
89a23cde 1820 err = -ENODEV;
44680eed 1821 goto out_put_masters;
45ae7cff
WD
1822 }
1823
45ae7cff
WD
1824 for (i = 0; i < smmu->num_global_irqs; ++i) {
1825 err = request_irq(smmu->irqs[i],
1826 arm_smmu_global_fault,
1827 IRQF_SHARED,
1828 "arm-smmu global fault",
1829 smmu);
1830 if (err) {
1831 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1832 i, smmu->irqs[i]);
1833 goto out_free_irqs;
1834 }
1835 }
1836
1837 INIT_LIST_HEAD(&smmu->list);
1838 spin_lock(&arm_smmu_devices_lock);
1839 list_add(&smmu->list, &arm_smmu_devices);
1840 spin_unlock(&arm_smmu_devices_lock);
fd90cecb
WD
1841
1842 arm_smmu_device_reset(smmu);
45ae7cff
WD
1843 return 0;
1844
1845out_free_irqs:
1846 while (i--)
1847 free_irq(smmu->irqs[i], smmu);
1848
45ae7cff
WD
1849out_put_masters:
1850 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
2907320d
MH
1851 struct arm_smmu_master *master
1852 = container_of(node, struct arm_smmu_master, node);
45ae7cff
WD
1853 of_node_put(master->of_node);
1854 }
1855
1856 return err;
1857}
1858
1859static int arm_smmu_device_remove(struct platform_device *pdev)
1860{
1861 int i;
1862 struct device *dev = &pdev->dev;
1863 struct arm_smmu_device *curr, *smmu = NULL;
1864 struct rb_node *node;
1865
1866 spin_lock(&arm_smmu_devices_lock);
1867 list_for_each_entry(curr, &arm_smmu_devices, list) {
1868 if (curr->dev == dev) {
1869 smmu = curr;
1870 list_del(&smmu->list);
1871 break;
1872 }
1873 }
1874 spin_unlock(&arm_smmu_devices_lock);
1875
1876 if (!smmu)
1877 return -ENODEV;
1878
45ae7cff 1879 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
2907320d
MH
1880 struct arm_smmu_master *master
1881 = container_of(node, struct arm_smmu_master, node);
45ae7cff
WD
1882 of_node_put(master->of_node);
1883 }
1884
ecfadb6e 1885 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
45ae7cff
WD
1886 dev_err(dev, "removing device with active domains!\n");
1887
1888 for (i = 0; i < smmu->num_global_irqs; ++i)
1889 free_irq(smmu->irqs[i], smmu);
1890
1891 /* Turn the thing off */
2907320d 1892 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
45ae7cff
WD
1893 return 0;
1894}
1895
45ae7cff
WD
1896static struct platform_driver arm_smmu_driver = {
1897 .driver = {
45ae7cff
WD
1898 .name = "arm-smmu",
1899 .of_match_table = of_match_ptr(arm_smmu_of_match),
1900 },
1901 .probe = arm_smmu_device_dt_probe,
1902 .remove = arm_smmu_device_remove,
1903};
1904
1905static int __init arm_smmu_init(void)
1906{
0e7d37ad 1907 struct device_node *np;
45ae7cff
WD
1908 int ret;
1909
0e7d37ad
TR
1910 /*
1911 * Play nice with systems that don't have an ARM SMMU by checking that
1912 * an ARM SMMU exists in the system before proceeding with the driver
1913 * and IOMMU bus operation registration.
1914 */
1915 np = of_find_matching_node(NULL, arm_smmu_of_match);
1916 if (!np)
1917 return 0;
1918
1919 of_node_put(np);
1920
45ae7cff
WD
1921 ret = platform_driver_register(&arm_smmu_driver);
1922 if (ret)
1923 return ret;
1924
1925 /* Oh, for a proper bus abstraction */
6614ee77 1926 if (!iommu_present(&platform_bus_type))
45ae7cff
WD
1927 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1928
d123cf82 1929#ifdef CONFIG_ARM_AMBA
6614ee77 1930 if (!iommu_present(&amba_bustype))
45ae7cff 1931 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
d123cf82 1932#endif
45ae7cff 1933
a9a1b0b5
WD
1934#ifdef CONFIG_PCI
1935 if (!iommu_present(&pci_bus_type))
1936 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1937#endif
1938
45ae7cff
WD
1939 return 0;
1940}
1941
1942static void __exit arm_smmu_exit(void)
1943{
1944 return platform_driver_unregister(&arm_smmu_driver);
1945}
1946
b1950b27 1947subsys_initcall(arm_smmu_init);
45ae7cff
WD
1948module_exit(arm_smmu_exit);
1949
1950MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1951MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1952MODULE_LICENSE("GPL v2");