blob: 0ae4dd39197f90e46cc298d9b89756dadfcf8d94 [file] [log] [blame]
Will Deacon45ae7cf2013-06-24 18:31:25 +01001/*
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
26 * - 4k and 64k pages, with contiguous pte hints.
Will Deacon06f983d2013-11-05 15:55:04 +000027 * - Up to 42-bit addressing (dependent on VA_BITS)
Will Deacon45ae7cf2013-06-24 18:31:25 +010028 * - Context fault reporting
29 */
30
31#define pr_fmt(fmt) "arm-smmu: " fmt
32
33#include <linux/delay.h>
34#include <linux/dma-mapping.h>
35#include <linux/err.h>
36#include <linux/interrupt.h>
37#include <linux/io.h>
38#include <linux/iommu.h>
39#include <linux/mm.h>
40#include <linux/module.h>
41#include <linux/of.h>
42#include <linux/platform_device.h>
43#include <linux/slab.h>
44#include <linux/spinlock.h>
45
46#include <linux/amba/bus.h>
47
48#include <asm/pgalloc.h>
49
50/* Maximum number of stream IDs assigned to a single device */
51#define MAX_MASTER_STREAMIDS 8
52
53/* Maximum number of context banks per SMMU */
54#define ARM_SMMU_MAX_CBS 128
55
56/* Maximum number of mapping groups per SMMU */
57#define ARM_SMMU_MAX_SMRS 128
58
Will Deacon45ae7cf2013-06-24 18:31:25 +010059/* SMMU global address space */
60#define ARM_SMMU_GR0(smmu) ((smmu)->base)
61#define ARM_SMMU_GR1(smmu) ((smmu)->base + (smmu)->pagesize)
62
63/* Page table bits */
Will Deaconcf2d45b2013-11-05 16:32:00 +000064#define ARM_SMMU_PTE_XN (((pteval_t)3) << 53)
Will Deacon45ae7cf2013-06-24 18:31:25 +010065#define ARM_SMMU_PTE_CONT (((pteval_t)1) << 52)
66#define ARM_SMMU_PTE_AF (((pteval_t)1) << 10)
67#define ARM_SMMU_PTE_SH_NS (((pteval_t)0) << 8)
68#define ARM_SMMU_PTE_SH_OS (((pteval_t)2) << 8)
69#define ARM_SMMU_PTE_SH_IS (((pteval_t)3) << 8)
Will Deaconcf2d45b2013-11-05 16:32:00 +000070#define ARM_SMMU_PTE_PAGE (((pteval_t)3) << 0)
Will Deacon45ae7cf2013-06-24 18:31:25 +010071
72#if PAGE_SIZE == SZ_4K
73#define ARM_SMMU_PTE_CONT_ENTRIES 16
74#elif PAGE_SIZE == SZ_64K
75#define ARM_SMMU_PTE_CONT_ENTRIES 32
76#else
77#define ARM_SMMU_PTE_CONT_ENTRIES 1
78#endif
79
80#define ARM_SMMU_PTE_CONT_SIZE (PAGE_SIZE * ARM_SMMU_PTE_CONT_ENTRIES)
81#define ARM_SMMU_PTE_CONT_MASK (~(ARM_SMMU_PTE_CONT_SIZE - 1))
Will Deacon45ae7cf2013-06-24 18:31:25 +010082
83/* Stage-1 PTE */
84#define ARM_SMMU_PTE_AP_UNPRIV (((pteval_t)1) << 6)
85#define ARM_SMMU_PTE_AP_RDONLY (((pteval_t)2) << 6)
86#define ARM_SMMU_PTE_ATTRINDX_SHIFT 2
Will Deacon1463fe42013-07-31 19:21:27 +010087#define ARM_SMMU_PTE_nG (((pteval_t)1) << 11)
Will Deacon45ae7cf2013-06-24 18:31:25 +010088
89/* Stage-2 PTE */
90#define ARM_SMMU_PTE_HAP_FAULT (((pteval_t)0) << 6)
91#define ARM_SMMU_PTE_HAP_READ (((pteval_t)1) << 6)
92#define ARM_SMMU_PTE_HAP_WRITE (((pteval_t)2) << 6)
93#define ARM_SMMU_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2)
94#define ARM_SMMU_PTE_MEMATTR_NC (((pteval_t)0x5) << 2)
95#define ARM_SMMU_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2)
96
97/* Configuration registers */
98#define ARM_SMMU_GR0_sCR0 0x0
99#define sCR0_CLIENTPD (1 << 0)
100#define sCR0_GFRE (1 << 1)
101#define sCR0_GFIE (1 << 2)
102#define sCR0_GCFGFRE (1 << 4)
103#define sCR0_GCFGFIE (1 << 5)
104#define sCR0_USFCFG (1 << 10)
105#define sCR0_VMIDPNE (1 << 11)
106#define sCR0_PTM (1 << 12)
107#define sCR0_FB (1 << 13)
108#define sCR0_BSU_SHIFT 14
109#define sCR0_BSU_MASK 0x3
110
111/* Identification registers */
112#define ARM_SMMU_GR0_ID0 0x20
113#define ARM_SMMU_GR0_ID1 0x24
114#define ARM_SMMU_GR0_ID2 0x28
115#define ARM_SMMU_GR0_ID3 0x2c
116#define ARM_SMMU_GR0_ID4 0x30
117#define ARM_SMMU_GR0_ID5 0x34
118#define ARM_SMMU_GR0_ID6 0x38
119#define ARM_SMMU_GR0_ID7 0x3c
120#define ARM_SMMU_GR0_sGFSR 0x48
121#define ARM_SMMU_GR0_sGFSYNR0 0x50
122#define ARM_SMMU_GR0_sGFSYNR1 0x54
123#define ARM_SMMU_GR0_sGFSYNR2 0x58
124#define ARM_SMMU_GR0_PIDR0 0xfe0
125#define ARM_SMMU_GR0_PIDR1 0xfe4
126#define ARM_SMMU_GR0_PIDR2 0xfe8
127
128#define ID0_S1TS (1 << 30)
129#define ID0_S2TS (1 << 29)
130#define ID0_NTS (1 << 28)
131#define ID0_SMS (1 << 27)
132#define ID0_PTFS_SHIFT 24
133#define ID0_PTFS_MASK 0x2
134#define ID0_PTFS_V8_ONLY 0x2
135#define ID0_CTTW (1 << 14)
136#define ID0_NUMIRPT_SHIFT 16
137#define ID0_NUMIRPT_MASK 0xff
138#define ID0_NUMSMRG_SHIFT 0
139#define ID0_NUMSMRG_MASK 0xff
140
141#define ID1_PAGESIZE (1 << 31)
142#define ID1_NUMPAGENDXB_SHIFT 28
143#define ID1_NUMPAGENDXB_MASK 7
144#define ID1_NUMS2CB_SHIFT 16
145#define ID1_NUMS2CB_MASK 0xff
146#define ID1_NUMCB_SHIFT 0
147#define ID1_NUMCB_MASK 0xff
148
149#define ID2_OAS_SHIFT 4
150#define ID2_OAS_MASK 0xf
151#define ID2_IAS_SHIFT 0
152#define ID2_IAS_MASK 0xf
153#define ID2_UBS_SHIFT 8
154#define ID2_UBS_MASK 0xf
155#define ID2_PTFS_4K (1 << 12)
156#define ID2_PTFS_16K (1 << 13)
157#define ID2_PTFS_64K (1 << 14)
158
159#define PIDR2_ARCH_SHIFT 4
160#define PIDR2_ARCH_MASK 0xf
161
162/* Global TLB invalidation */
163#define ARM_SMMU_GR0_STLBIALL 0x60
164#define ARM_SMMU_GR0_TLBIVMID 0x64
165#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
166#define ARM_SMMU_GR0_TLBIALLH 0x6c
167#define ARM_SMMU_GR0_sTLBGSYNC 0x70
168#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
169#define sTLBGSTATUS_GSACTIVE (1 << 0)
170#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
171
172/* Stream mapping registers */
173#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
174#define SMR_VALID (1 << 31)
175#define SMR_MASK_SHIFT 16
176#define SMR_MASK_MASK 0x7fff
177#define SMR_ID_SHIFT 0
178#define SMR_ID_MASK 0x7fff
179
180#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
181#define S2CR_CBNDX_SHIFT 0
182#define S2CR_CBNDX_MASK 0xff
183#define S2CR_TYPE_SHIFT 16
184#define S2CR_TYPE_MASK 0x3
185#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
186#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
187#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
188
189/* Context bank attribute registers */
190#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
191#define CBAR_VMID_SHIFT 0
192#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000193#define CBAR_S1_BPSHCFG_SHIFT 8
194#define CBAR_S1_BPSHCFG_MASK 3
195#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100196#define CBAR_S1_MEMATTR_SHIFT 12
197#define CBAR_S1_MEMATTR_MASK 0xf
198#define CBAR_S1_MEMATTR_WB 0xf
199#define CBAR_TYPE_SHIFT 16
200#define CBAR_TYPE_MASK 0x3
201#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
202#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
203#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
204#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
205#define CBAR_IRPTNDX_SHIFT 24
206#define CBAR_IRPTNDX_MASK 0xff
207
208#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
209#define CBA2R_RW64_32BIT (0 << 0)
210#define CBA2R_RW64_64BIT (1 << 0)
211
212/* Translation context bank */
213#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
214#define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize)
215
216#define ARM_SMMU_CB_SCTLR 0x0
217#define ARM_SMMU_CB_RESUME 0x8
218#define ARM_SMMU_CB_TTBCR2 0x10
219#define ARM_SMMU_CB_TTBR0_LO 0x20
220#define ARM_SMMU_CB_TTBR0_HI 0x24
221#define ARM_SMMU_CB_TTBCR 0x30
222#define ARM_SMMU_CB_S1_MAIR0 0x38
223#define ARM_SMMU_CB_FSR 0x58
224#define ARM_SMMU_CB_FAR_LO 0x60
225#define ARM_SMMU_CB_FAR_HI 0x64
226#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon1463fe42013-07-31 19:21:27 +0100227#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon45ae7cf2013-06-24 18:31:25 +0100228
229#define SCTLR_S1_ASIDPNE (1 << 12)
230#define SCTLR_CFCFG (1 << 7)
231#define SCTLR_CFIE (1 << 6)
232#define SCTLR_CFRE (1 << 5)
233#define SCTLR_E (1 << 4)
234#define SCTLR_AFE (1 << 2)
235#define SCTLR_TRE (1 << 1)
236#define SCTLR_M (1 << 0)
237#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
238
239#define RESUME_RETRY (0 << 0)
240#define RESUME_TERMINATE (1 << 0)
241
242#define TTBCR_EAE (1 << 31)
243
244#define TTBCR_PASIZE_SHIFT 16
245#define TTBCR_PASIZE_MASK 0x7
246
247#define TTBCR_TG0_4K (0 << 14)
248#define TTBCR_TG0_64K (1 << 14)
249
250#define TTBCR_SH0_SHIFT 12
251#define TTBCR_SH0_MASK 0x3
252#define TTBCR_SH_NS 0
253#define TTBCR_SH_OS 2
254#define TTBCR_SH_IS 3
255
256#define TTBCR_ORGN0_SHIFT 10
257#define TTBCR_IRGN0_SHIFT 8
258#define TTBCR_RGN_MASK 0x3
259#define TTBCR_RGN_NC 0
260#define TTBCR_RGN_WBWA 1
261#define TTBCR_RGN_WT 2
262#define TTBCR_RGN_WB 3
263
264#define TTBCR_SL0_SHIFT 6
265#define TTBCR_SL0_MASK 0x3
266#define TTBCR_SL0_LVL_2 0
267#define TTBCR_SL0_LVL_1 1
268
269#define TTBCR_T1SZ_SHIFT 16
270#define TTBCR_T0SZ_SHIFT 0
271#define TTBCR_SZ_MASK 0xf
272
273#define TTBCR2_SEP_SHIFT 15
274#define TTBCR2_SEP_MASK 0x7
275
276#define TTBCR2_PASIZE_SHIFT 0
277#define TTBCR2_PASIZE_MASK 0x7
278
279/* Common definitions for PASize and SEP fields */
280#define TTBCR2_ADDR_32 0
281#define TTBCR2_ADDR_36 1
282#define TTBCR2_ADDR_40 2
283#define TTBCR2_ADDR_42 3
284#define TTBCR2_ADDR_44 4
285#define TTBCR2_ADDR_48 5
286
Will Deacon1463fe42013-07-31 19:21:27 +0100287#define TTBRn_HI_ASID_SHIFT 16
288
Will Deacon45ae7cf2013-06-24 18:31:25 +0100289#define MAIR_ATTR_SHIFT(n) ((n) << 3)
290#define MAIR_ATTR_MASK 0xff
291#define MAIR_ATTR_DEVICE 0x04
292#define MAIR_ATTR_NC 0x44
293#define MAIR_ATTR_WBRWA 0xff
294#define MAIR_ATTR_IDX_NC 0
295#define MAIR_ATTR_IDX_CACHE 1
296#define MAIR_ATTR_IDX_DEV 2
297
298#define FSR_MULTI (1 << 31)
299#define FSR_SS (1 << 30)
300#define FSR_UUT (1 << 8)
301#define FSR_ASF (1 << 7)
302#define FSR_TLBLKF (1 << 6)
303#define FSR_TLBMCF (1 << 5)
304#define FSR_EF (1 << 4)
305#define FSR_PF (1 << 3)
306#define FSR_AFF (1 << 2)
307#define FSR_TF (1 << 1)
308
309#define FSR_IGN (FSR_AFF | FSR_ASF | FSR_TLBMCF | \
310 FSR_TLBLKF)
311#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100312 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100313
314#define FSYNR0_WNR (1 << 4)
315
316struct arm_smmu_smr {
317 u8 idx;
318 u16 mask;
319 u16 id;
320};
321
322struct arm_smmu_master {
323 struct device_node *of_node;
324
325 /*
326 * The following is specific to the master's position in the
327 * SMMU chain.
328 */
329 struct rb_node node;
330 int num_streamids;
331 u16 streamids[MAX_MASTER_STREAMIDS];
332
333 /*
334 * We only need to allocate these on the root SMMU, as we
335 * configure unmatched streams to bypass translation.
336 */
337 struct arm_smmu_smr *smrs;
338};
339
340struct arm_smmu_device {
341 struct device *dev;
342 struct device_node *parent_of_node;
343
344 void __iomem *base;
345 unsigned long size;
346 unsigned long pagesize;
347
348#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
349#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
350#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
351#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
352#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
353 u32 features;
354 int version;
355
356 u32 num_context_banks;
357 u32 num_s2_context_banks;
358 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
359 atomic_t irptndx;
360
361 u32 num_mapping_groups;
362 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
363
364 unsigned long input_size;
365 unsigned long s1_output_size;
366 unsigned long s2_output_size;
367
368 u32 num_global_irqs;
369 u32 num_context_irqs;
370 unsigned int *irqs;
371
Will Deacon45ae7cf2013-06-24 18:31:25 +0100372 struct list_head list;
373 struct rb_root masters;
374};
375
376struct arm_smmu_cfg {
377 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100378 u8 cbndx;
379 u8 irptndx;
380 u32 cbar;
381 pgd_t *pgd;
382};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100383#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100384
Will Deaconecfadb62013-07-31 19:21:28 +0100385#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
386#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
387
Will Deacon45ae7cf2013-06-24 18:31:25 +0100388struct arm_smmu_domain {
389 /*
390 * A domain can span across multiple, chained SMMUs and requires
391 * all devices within the domain to follow the same translation
392 * path.
393 */
394 struct arm_smmu_device *leaf_smmu;
395 struct arm_smmu_cfg root_cfg;
396 phys_addr_t output_mask;
397
Will Deaconc9d09e22014-02-04 22:12:42 +0000398 spinlock_t lock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100399};
400
401static DEFINE_SPINLOCK(arm_smmu_devices_lock);
402static LIST_HEAD(arm_smmu_devices);
403
404static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
405 struct device_node *dev_node)
406{
407 struct rb_node *node = smmu->masters.rb_node;
408
409 while (node) {
410 struct arm_smmu_master *master;
411 master = container_of(node, struct arm_smmu_master, node);
412
413 if (dev_node < master->of_node)
414 node = node->rb_left;
415 else if (dev_node > master->of_node)
416 node = node->rb_right;
417 else
418 return master;
419 }
420
421 return NULL;
422}
423
424static int insert_smmu_master(struct arm_smmu_device *smmu,
425 struct arm_smmu_master *master)
426{
427 struct rb_node **new, *parent;
428
429 new = &smmu->masters.rb_node;
430 parent = NULL;
431 while (*new) {
432 struct arm_smmu_master *this;
433 this = container_of(*new, struct arm_smmu_master, node);
434
435 parent = *new;
436 if (master->of_node < this->of_node)
437 new = &((*new)->rb_left);
438 else if (master->of_node > this->of_node)
439 new = &((*new)->rb_right);
440 else
441 return -EEXIST;
442 }
443
444 rb_link_node(&master->node, parent, new);
445 rb_insert_color(&master->node, &smmu->masters);
446 return 0;
447}
448
449static int register_smmu_master(struct arm_smmu_device *smmu,
450 struct device *dev,
451 struct of_phandle_args *masterspec)
452{
453 int i;
454 struct arm_smmu_master *master;
455
456 master = find_smmu_master(smmu, masterspec->np);
457 if (master) {
458 dev_err(dev,
459 "rejecting multiple registrations for master device %s\n",
460 masterspec->np->name);
461 return -EBUSY;
462 }
463
464 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
465 dev_err(dev,
466 "reached maximum number (%d) of stream IDs for master device %s\n",
467 MAX_MASTER_STREAMIDS, masterspec->np->name);
468 return -ENOSPC;
469 }
470
471 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
472 if (!master)
473 return -ENOMEM;
474
475 master->of_node = masterspec->np;
476 master->num_streamids = masterspec->args_count;
477
478 for (i = 0; i < master->num_streamids; ++i)
479 master->streamids[i] = masterspec->args[i];
480
481 return insert_smmu_master(smmu, master);
482}
483
484static struct arm_smmu_device *find_parent_smmu(struct arm_smmu_device *smmu)
485{
486 struct arm_smmu_device *parent;
487
488 if (!smmu->parent_of_node)
489 return NULL;
490
491 spin_lock(&arm_smmu_devices_lock);
492 list_for_each_entry(parent, &arm_smmu_devices, list)
493 if (parent->dev->of_node == smmu->parent_of_node)
494 goto out_unlock;
495
496 parent = NULL;
497 dev_warn(smmu->dev,
498 "Failed to find SMMU parent despite parent in DT\n");
499out_unlock:
500 spin_unlock(&arm_smmu_devices_lock);
501 return parent;
502}
503
504static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
505{
506 int idx;
507
508 do {
509 idx = find_next_zero_bit(map, end, start);
510 if (idx == end)
511 return -ENOSPC;
512 } while (test_and_set_bit(idx, map));
513
514 return idx;
515}
516
517static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
518{
519 clear_bit(idx, map);
520}
521
522/* Wait for any pending TLB invalidations to complete */
523static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
524{
525 int count = 0;
526 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
527
528 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
529 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
530 & sTLBGSTATUS_GSACTIVE) {
531 cpu_relax();
532 if (++count == TLB_LOOP_TIMEOUT) {
533 dev_err_ratelimited(smmu->dev,
534 "TLB sync timed out -- SMMU may be deadlocked\n");
535 return;
536 }
537 udelay(1);
538 }
539}
540
Will Deacon1463fe42013-07-31 19:21:27 +0100541static void arm_smmu_tlb_inv_context(struct arm_smmu_cfg *cfg)
542{
543 struct arm_smmu_device *smmu = cfg->smmu;
544 void __iomem *base = ARM_SMMU_GR0(smmu);
545 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
546
547 if (stage1) {
548 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100549 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
550 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100551 } else {
552 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100553 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
554 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100555 }
556
557 arm_smmu_tlb_sync(smmu);
558}
559
Will Deacon45ae7cf2013-06-24 18:31:25 +0100560static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
561{
562 int flags, ret;
563 u32 fsr, far, fsynr, resume;
564 unsigned long iova;
565 struct iommu_domain *domain = dev;
566 struct arm_smmu_domain *smmu_domain = domain->priv;
567 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
568 struct arm_smmu_device *smmu = root_cfg->smmu;
569 void __iomem *cb_base;
570
571 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
572 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
573
574 if (!(fsr & FSR_FAULT))
575 return IRQ_NONE;
576
577 if (fsr & FSR_IGN)
578 dev_err_ratelimited(smmu->dev,
579 "Unexpected context fault (fsr 0x%u)\n",
580 fsr);
581
582 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
583 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
584
585 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
586 iova = far;
587#ifdef CONFIG_64BIT
588 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
589 iova |= ((unsigned long)far << 32);
590#endif
591
592 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
593 ret = IRQ_HANDLED;
594 resume = RESUME_RETRY;
595 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100596 dev_err_ratelimited(smmu->dev,
597 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
598 iova, fsynr, root_cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100599 ret = IRQ_NONE;
600 resume = RESUME_TERMINATE;
601 }
602
603 /* Clear the faulting FSR */
604 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
605
606 /* Retry or terminate any stalled transactions */
607 if (fsr & FSR_SS)
608 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
609
610 return ret;
611}
612
613static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
614{
615 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
616 struct arm_smmu_device *smmu = dev;
617 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
618
619 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100620 if (!gfsr)
621 return IRQ_NONE;
622
Will Deacon45ae7cf2013-06-24 18:31:25 +0100623 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
624 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
625 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
626
627 dev_err_ratelimited(smmu->dev,
628 "Unexpected global fault, this could be serious\n");
629 dev_err_ratelimited(smmu->dev,
630 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
631 gfsr, gfsynr0, gfsynr1, gfsynr2);
632
633 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100634 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100635}
636
Will Deacon6dd35f42014-02-05 17:49:34 +0000637static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
638 size_t size)
639{
640 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
641
642
643 /* Ensure new page tables are visible to the hardware walker */
644 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
645 dsb();
646 } else {
647 /*
648 * If the SMMU can't walk tables in the CPU caches, treat them
649 * like non-coherent DMA since we need to flush the new entries
650 * all the way out to memory. There's no possibility of
651 * recursion here as the SMMU table walker will not be wired
652 * through another SMMU.
653 */
654 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
655 DMA_TO_DEVICE);
656 }
657}
658
Will Deacon45ae7cf2013-06-24 18:31:25 +0100659static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
660{
661 u32 reg;
662 bool stage1;
663 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
664 struct arm_smmu_device *smmu = root_cfg->smmu;
665 void __iomem *cb_base, *gr0_base, *gr1_base;
666
667 gr0_base = ARM_SMMU_GR0(smmu);
668 gr1_base = ARM_SMMU_GR1(smmu);
669 stage1 = root_cfg->cbar != CBAR_TYPE_S2_TRANS;
670 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
671
672 /* CBAR */
Will Deacon1463fe42013-07-31 19:21:27 +0100673 reg = root_cfg->cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100674 if (smmu->version == 1)
675 reg |= root_cfg->irptndx << CBAR_IRPTNDX_SHIFT;
676
Will Deacon57ca90f2014-02-06 14:59:05 +0000677 /*
678 * Use the weakest shareability/memory types, so they are
679 * overridden by the ttbcr/pte.
680 */
681 if (stage1) {
682 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
683 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
684 } else {
Will Deaconecfadb62013-07-31 19:21:28 +0100685 reg |= ARM_SMMU_CB_VMID(root_cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000686 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100687 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(root_cfg->cbndx));
688
689 if (smmu->version > 1) {
690 /* CBA2R */
691#ifdef CONFIG_64BIT
692 reg = CBA2R_RW64_64BIT;
693#else
694 reg = CBA2R_RW64_32BIT;
695#endif
696 writel_relaxed(reg,
697 gr1_base + ARM_SMMU_GR1_CBA2R(root_cfg->cbndx));
698
699 /* TTBCR2 */
700 switch (smmu->input_size) {
701 case 32:
702 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
703 break;
704 case 36:
705 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
706 break;
707 case 39:
708 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
709 break;
710 case 42:
711 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
712 break;
713 case 44:
714 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
715 break;
716 case 48:
717 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
718 break;
719 }
720
721 switch (smmu->s1_output_size) {
722 case 32:
723 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
724 break;
725 case 36:
726 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
727 break;
728 case 39:
729 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
730 break;
731 case 42:
732 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
733 break;
734 case 44:
735 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
736 break;
737 case 48:
738 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
739 break;
740 }
741
742 if (stage1)
743 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
744 }
745
746 /* TTBR0 */
Will Deacon6dd35f42014-02-05 17:49:34 +0000747 arm_smmu_flush_pgtable(smmu, root_cfg->pgd,
748 PTRS_PER_PGD * sizeof(pgd_t));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100749 reg = __pa(root_cfg->pgd);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100750 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
751 reg = (phys_addr_t)__pa(root_cfg->pgd) >> 32;
Will Deacon1463fe42013-07-31 19:21:27 +0100752 if (stage1)
Will Deaconecfadb62013-07-31 19:21:28 +0100753 reg |= ARM_SMMU_CB_ASID(root_cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100754 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100755
756 /*
757 * TTBCR
758 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
759 */
760 if (smmu->version > 1) {
761 if (PAGE_SIZE == SZ_4K)
762 reg = TTBCR_TG0_4K;
763 else
764 reg = TTBCR_TG0_64K;
765
766 if (!stage1) {
767 switch (smmu->s2_output_size) {
768 case 32:
769 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
770 break;
771 case 36:
772 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
773 break;
774 case 40:
775 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
776 break;
777 case 42:
778 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
779 break;
780 case 44:
781 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
782 break;
783 case 48:
784 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
785 break;
786 }
787 } else {
788 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
789 }
790 } else {
791 reg = 0;
792 }
793
794 reg |= TTBCR_EAE |
795 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
796 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
797 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT) |
798 (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
799 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
800
801 /* MAIR0 (stage-1 only) */
802 if (stage1) {
803 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
804 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
805 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
806 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
807 }
808
Will Deacon45ae7cf2013-06-24 18:31:25 +0100809 /* SCTLR */
810 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
811 if (stage1)
812 reg |= SCTLR_S1_ASIDPNE;
813#ifdef __BIG_ENDIAN
814 reg |= SCTLR_E;
815#endif
Will Deacon25724842013-08-21 13:49:53 +0100816 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100817}
818
819static int arm_smmu_init_domain_context(struct iommu_domain *domain,
820 struct device *dev)
821{
822 int irq, ret, start;
823 struct arm_smmu_domain *smmu_domain = domain->priv;
824 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
825 struct arm_smmu_device *smmu, *parent;
826
827 /*
828 * Walk the SMMU chain to find the root device for this chain.
829 * We assume that no masters have translations which terminate
830 * early, and therefore check that the root SMMU does indeed have
831 * a StreamID for the master in question.
832 */
833 parent = dev->archdata.iommu;
834 smmu_domain->output_mask = -1;
835 do {
836 smmu = parent;
837 smmu_domain->output_mask &= (1ULL << smmu->s2_output_size) - 1;
838 } while ((parent = find_parent_smmu(smmu)));
839
840 if (!find_smmu_master(smmu, dev->of_node)) {
841 dev_err(dev, "unable to find root SMMU for device\n");
842 return -ENODEV;
843 }
844
Will Deacon45ae7cf2013-06-24 18:31:25 +0100845 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
846 /*
847 * We will likely want to change this if/when KVM gets
848 * involved.
849 */
850 root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
851 start = smmu->num_s2_context_banks;
852 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) {
853 root_cfg->cbar = CBAR_TYPE_S2_TRANS;
854 start = 0;
855 } else {
856 root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
857 start = smmu->num_s2_context_banks;
858 }
859
860 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
861 smmu->num_context_banks);
862 if (IS_ERR_VALUE(ret))
Will Deaconecfadb62013-07-31 19:21:28 +0100863 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100864
865 root_cfg->cbndx = ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100866 if (smmu->version == 1) {
867 root_cfg->irptndx = atomic_inc_return(&smmu->irptndx);
868 root_cfg->irptndx %= smmu->num_context_irqs;
869 } else {
870 root_cfg->irptndx = root_cfg->cbndx;
871 }
872
873 irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
874 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
875 "arm-smmu-context-fault", domain);
876 if (IS_ERR_VALUE(ret)) {
877 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
878 root_cfg->irptndx, irq);
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100879 root_cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100880 goto out_free_context;
881 }
882
883 root_cfg->smmu = smmu;
884 arm_smmu_init_context_bank(smmu_domain);
885 return ret;
886
887out_free_context:
888 __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100889 return ret;
890}
891
892static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
893{
894 struct arm_smmu_domain *smmu_domain = domain->priv;
895 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
896 struct arm_smmu_device *smmu = root_cfg->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100897 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100898 int irq;
899
900 if (!smmu)
901 return;
902
Will Deacon1463fe42013-07-31 19:21:27 +0100903 /* Disable the context bank and nuke the TLB before freeing it. */
904 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
905 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
906 arm_smmu_tlb_inv_context(root_cfg);
907
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100908 if (root_cfg->irptndx != INVALID_IRPTNDX) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100909 irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
910 free_irq(irq, domain);
911 }
912
Will Deacon45ae7cf2013-06-24 18:31:25 +0100913 __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
914}
915
916static int arm_smmu_domain_init(struct iommu_domain *domain)
917{
918 struct arm_smmu_domain *smmu_domain;
919 pgd_t *pgd;
920
921 /*
922 * Allocate the domain and initialise some of its data structures.
923 * We can't really do anything meaningful until we've added a
924 * master.
925 */
926 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
927 if (!smmu_domain)
928 return -ENOMEM;
929
930 pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
931 if (!pgd)
932 goto out_free_domain;
933 smmu_domain->root_cfg.pgd = pgd;
934
Will Deaconc9d09e22014-02-04 22:12:42 +0000935 spin_lock_init(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100936 domain->priv = smmu_domain;
937 return 0;
938
939out_free_domain:
940 kfree(smmu_domain);
941 return -ENOMEM;
942}
943
944static void arm_smmu_free_ptes(pmd_t *pmd)
945{
946 pgtable_t table = pmd_pgtable(*pmd);
947 pgtable_page_dtor(table);
948 __free_page(table);
949}
950
951static void arm_smmu_free_pmds(pud_t *pud)
952{
953 int i;
954 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
955
956 pmd = pmd_base;
957 for (i = 0; i < PTRS_PER_PMD; ++i) {
958 if (pmd_none(*pmd))
959 continue;
960
961 arm_smmu_free_ptes(pmd);
962 pmd++;
963 }
964
965 pmd_free(NULL, pmd_base);
966}
967
968static void arm_smmu_free_puds(pgd_t *pgd)
969{
970 int i;
971 pud_t *pud, *pud_base = pud_offset(pgd, 0);
972
973 pud = pud_base;
974 for (i = 0; i < PTRS_PER_PUD; ++i) {
975 if (pud_none(*pud))
976 continue;
977
978 arm_smmu_free_pmds(pud);
979 pud++;
980 }
981
982 pud_free(NULL, pud_base);
983}
984
985static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
986{
987 int i;
988 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
989 pgd_t *pgd, *pgd_base = root_cfg->pgd;
990
991 /*
992 * Recursively free the page tables for this domain. We don't
993 * care about speculative TLB filling, because the TLB will be
994 * nuked next time this context bank is re-allocated and no devices
995 * currently map to these tables.
996 */
997 pgd = pgd_base;
998 for (i = 0; i < PTRS_PER_PGD; ++i) {
999 if (pgd_none(*pgd))
1000 continue;
1001 arm_smmu_free_puds(pgd);
1002 pgd++;
1003 }
1004
1005 kfree(pgd_base);
1006}
1007
1008static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1009{
1010 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001011
1012 /*
1013 * Free the domain resources. We assume that all devices have
1014 * already been detached.
1015 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001016 arm_smmu_destroy_domain_context(domain);
1017 arm_smmu_free_pgtables(smmu_domain);
1018 kfree(smmu_domain);
1019}
1020
1021static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
1022 struct arm_smmu_master *master)
1023{
1024 int i;
1025 struct arm_smmu_smr *smrs;
1026 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1027
1028 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1029 return 0;
1030
1031 if (master->smrs)
1032 return -EEXIST;
1033
1034 smrs = kmalloc(sizeof(*smrs) * master->num_streamids, GFP_KERNEL);
1035 if (!smrs) {
1036 dev_err(smmu->dev, "failed to allocate %d SMRs for master %s\n",
1037 master->num_streamids, master->of_node->name);
1038 return -ENOMEM;
1039 }
1040
1041 /* Allocate the SMRs on the root SMMU */
1042 for (i = 0; i < master->num_streamids; ++i) {
1043 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1044 smmu->num_mapping_groups);
1045 if (IS_ERR_VALUE(idx)) {
1046 dev_err(smmu->dev, "failed to allocate free SMR\n");
1047 goto err_free_smrs;
1048 }
1049
1050 smrs[i] = (struct arm_smmu_smr) {
1051 .idx = idx,
1052 .mask = 0, /* We don't currently share SMRs */
1053 .id = master->streamids[i],
1054 };
1055 }
1056
1057 /* It worked! Now, poke the actual hardware */
1058 for (i = 0; i < master->num_streamids; ++i) {
1059 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1060 smrs[i].mask << SMR_MASK_SHIFT;
1061 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1062 }
1063
1064 master->smrs = smrs;
1065 return 0;
1066
1067err_free_smrs:
1068 while (--i >= 0)
1069 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1070 kfree(smrs);
1071 return -ENOSPC;
1072}
1073
1074static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
1075 struct arm_smmu_master *master)
1076{
1077 int i;
1078 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1079 struct arm_smmu_smr *smrs = master->smrs;
1080
1081 /* Invalidate the SMRs before freeing back to the allocator */
1082 for (i = 0; i < master->num_streamids; ++i) {
1083 u8 idx = smrs[i].idx;
1084 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1085 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1086 }
1087
1088 master->smrs = NULL;
1089 kfree(smrs);
1090}
1091
1092static void arm_smmu_bypass_stream_mapping(struct arm_smmu_device *smmu,
1093 struct arm_smmu_master *master)
1094{
1095 int i;
1096 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1097
1098 for (i = 0; i < master->num_streamids; ++i) {
1099 u16 sid = master->streamids[i];
1100 writel_relaxed(S2CR_TYPE_BYPASS,
1101 gr0_base + ARM_SMMU_GR0_S2CR(sid));
1102 }
1103}
1104
1105static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1106 struct arm_smmu_master *master)
1107{
1108 int i, ret;
1109 struct arm_smmu_device *parent, *smmu = smmu_domain->root_cfg.smmu;
1110 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1111
1112 ret = arm_smmu_master_configure_smrs(smmu, master);
1113 if (ret)
1114 return ret;
1115
1116 /* Bypass the leaves */
1117 smmu = smmu_domain->leaf_smmu;
1118 while ((parent = find_parent_smmu(smmu))) {
1119 /*
1120 * We won't have a StreamID match for anything but the root
1121 * smmu, so we only need to worry about StreamID indexing,
1122 * where we must install bypass entries in the S2CRs.
1123 */
1124 if (smmu->features & ARM_SMMU_FEAT_STREAM_MATCH)
1125 continue;
1126
1127 arm_smmu_bypass_stream_mapping(smmu, master);
1128 smmu = parent;
1129 }
1130
1131 /* Now we're at the root, time to point at our context bank */
1132 for (i = 0; i < master->num_streamids; ++i) {
1133 u32 idx, s2cr;
1134 idx = master->smrs ? master->smrs[i].idx : master->streamids[i];
1135 s2cr = (S2CR_TYPE_TRANS << S2CR_TYPE_SHIFT) |
1136 (smmu_domain->root_cfg.cbndx << S2CR_CBNDX_SHIFT);
1137 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1138 }
1139
1140 return 0;
1141}
1142
1143static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
1144 struct arm_smmu_master *master)
1145{
1146 struct arm_smmu_device *smmu = smmu_domain->root_cfg.smmu;
1147
1148 /*
1149 * We *must* clear the S2CR first, because freeing the SMR means
1150 * that it can be re-allocated immediately.
1151 */
1152 arm_smmu_bypass_stream_mapping(smmu, master);
1153 arm_smmu_master_free_smrs(smmu, master);
1154}
1155
1156static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1157{
1158 int ret = -EINVAL;
1159 struct arm_smmu_domain *smmu_domain = domain->priv;
1160 struct arm_smmu_device *device_smmu = dev->archdata.iommu;
1161 struct arm_smmu_master *master;
1162
1163 if (!device_smmu) {
1164 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1165 return -ENXIO;
1166 }
1167
1168 /*
1169 * Sanity check the domain. We don't currently support domains
1170 * that cross between different SMMU chains.
1171 */
Will Deaconc9d09e22014-02-04 22:12:42 +00001172 spin_lock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001173 if (!smmu_domain->leaf_smmu) {
1174 /* Now that we have a master, we can finalise the domain */
1175 ret = arm_smmu_init_domain_context(domain, dev);
1176 if (IS_ERR_VALUE(ret))
1177 goto err_unlock;
1178
1179 smmu_domain->leaf_smmu = device_smmu;
1180 } else if (smmu_domain->leaf_smmu != device_smmu) {
1181 dev_err(dev,
1182 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1183 dev_name(smmu_domain->leaf_smmu->dev),
1184 dev_name(device_smmu->dev));
1185 goto err_unlock;
1186 }
Will Deaconc9d09e22014-02-04 22:12:42 +00001187 spin_unlock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001188
1189 /* Looks ok, so add the device to the domain */
1190 master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node);
1191 if (!master)
1192 return -ENODEV;
1193
1194 return arm_smmu_domain_add_master(smmu_domain, master);
1195
1196err_unlock:
Will Deaconc9d09e22014-02-04 22:12:42 +00001197 spin_unlock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001198 return ret;
1199}
1200
1201static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1202{
1203 struct arm_smmu_domain *smmu_domain = domain->priv;
1204 struct arm_smmu_master *master;
1205
1206 master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node);
1207 if (master)
1208 arm_smmu_domain_remove_master(smmu_domain, master);
1209}
1210
Will Deacon45ae7cf2013-06-24 18:31:25 +01001211static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1212 unsigned long end)
1213{
1214 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1215 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1216}
1217
1218static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1219 unsigned long addr, unsigned long end,
1220 unsigned long pfn, int flags, int stage)
1221{
1222 pte_t *pte, *start;
Will Deaconcf2d45b2013-11-05 16:32:00 +00001223 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001224
1225 if (pmd_none(*pmd)) {
1226 /* Allocate a new set of tables */
Will Deaconc9d09e22014-02-04 22:12:42 +00001227 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001228 if (!table)
1229 return -ENOMEM;
1230
Will Deacon6dd35f42014-02-05 17:49:34 +00001231 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
Kirill A. Shutemov01058e72013-11-14 14:31:49 -08001232 if (!pgtable_page_ctor(table)) {
1233 __free_page(table);
1234 return -ENOMEM;
1235 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001236 pmd_populate(NULL, pmd, table);
1237 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1238 }
1239
1240 if (stage == 1) {
Will Deacon1463fe42013-07-31 19:21:27 +01001241 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001242 if (!(flags & IOMMU_WRITE) && (flags & IOMMU_READ))
1243 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1244
1245 if (flags & IOMMU_CACHE)
1246 pteval |= (MAIR_ATTR_IDX_CACHE <<
1247 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1248 } else {
1249 pteval |= ARM_SMMU_PTE_HAP_FAULT;
1250 if (flags & IOMMU_READ)
1251 pteval |= ARM_SMMU_PTE_HAP_READ;
1252 if (flags & IOMMU_WRITE)
1253 pteval |= ARM_SMMU_PTE_HAP_WRITE;
1254 if (flags & IOMMU_CACHE)
1255 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1256 else
1257 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1258 }
1259
1260 /* If no access, create a faulting entry to avoid TLB fills */
Will Deaconcf2d45b2013-11-05 16:32:00 +00001261 if (flags & IOMMU_EXEC)
1262 pteval &= ~ARM_SMMU_PTE_XN;
1263 else if (!(flags & (IOMMU_READ | IOMMU_WRITE)))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001264 pteval &= ~ARM_SMMU_PTE_PAGE;
1265
1266 pteval |= ARM_SMMU_PTE_SH_IS;
1267 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1268 pte = start;
1269
1270 /*
1271 * Install the page table entries. This is fairly complicated
1272 * since we attempt to make use of the contiguous hint in the
1273 * ptes where possible. The contiguous hint indicates a series
1274 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1275 * contiguous region with the following constraints:
1276 *
1277 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1278 * - Each pte in the region has the contiguous hint bit set
1279 *
1280 * This complicates unmapping (also handled by this code, when
1281 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1282 * possible, yet highly unlikely, that a client may unmap only
1283 * part of a contiguous range. This requires clearing of the
1284 * contiguous hint bits in the range before installing the new
1285 * faulting entries.
1286 *
1287 * Note that re-mapping an address range without first unmapping
1288 * it is not supported, so TLB invalidation is not required here
1289 * and is instead performed at unmap and domain-init time.
1290 */
1291 do {
1292 int i = 1;
1293 pteval &= ~ARM_SMMU_PTE_CONT;
1294
1295 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1296 i = ARM_SMMU_PTE_CONT_ENTRIES;
1297 pteval |= ARM_SMMU_PTE_CONT;
1298 } else if (pte_val(*pte) &
1299 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1300 int j;
1301 pte_t *cont_start;
1302 unsigned long idx = pte_index(addr);
1303
1304 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1305 cont_start = pmd_page_vaddr(*pmd) + idx;
1306 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
1307 pte_val(*(cont_start + j)) &= ~ARM_SMMU_PTE_CONT;
1308
1309 arm_smmu_flush_pgtable(smmu, cont_start,
1310 sizeof(*pte) *
1311 ARM_SMMU_PTE_CONT_ENTRIES);
1312 }
1313
1314 do {
1315 *pte = pfn_pte(pfn, __pgprot(pteval));
1316 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1317 } while (addr != end);
1318
1319 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1320 return 0;
1321}
1322
1323static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1324 unsigned long addr, unsigned long end,
1325 phys_addr_t phys, int flags, int stage)
1326{
1327 int ret;
1328 pmd_t *pmd;
1329 unsigned long next, pfn = __phys_to_pfn(phys);
1330
1331#ifndef __PAGETABLE_PMD_FOLDED
1332 if (pud_none(*pud)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001333 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001334 if (!pmd)
1335 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001336
Will Deacon6dd35f42014-02-05 17:49:34 +00001337 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001338 pud_populate(NULL, pud, pmd);
1339 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1340
1341 pmd += pmd_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001342 } else
1343#endif
1344 pmd = pmd_offset(pud, addr);
1345
1346 do {
1347 next = pmd_addr_end(addr, end);
1348 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, end, pfn,
1349 flags, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001350 phys += next - addr;
1351 } while (pmd++, addr = next, addr < end);
1352
1353 return ret;
1354}
1355
1356static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1357 unsigned long addr, unsigned long end,
1358 phys_addr_t phys, int flags, int stage)
1359{
1360 int ret = 0;
1361 pud_t *pud;
1362 unsigned long next;
1363
1364#ifndef __PAGETABLE_PUD_FOLDED
1365 if (pgd_none(*pgd)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001366 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001367 if (!pud)
1368 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001369
Will Deacon6dd35f42014-02-05 17:49:34 +00001370 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001371 pgd_populate(NULL, pgd, pud);
1372 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1373
1374 pud += pud_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001375 } else
1376#endif
1377 pud = pud_offset(pgd, addr);
1378
1379 do {
1380 next = pud_addr_end(addr, end);
1381 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
1382 flags, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001383 phys += next - addr;
1384 } while (pud++, addr = next, addr < end);
1385
1386 return ret;
1387}
1388
1389static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1390 unsigned long iova, phys_addr_t paddr,
1391 size_t size, int flags)
1392{
1393 int ret, stage;
1394 unsigned long end;
1395 phys_addr_t input_mask, output_mask;
1396 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
1397 pgd_t *pgd = root_cfg->pgd;
1398 struct arm_smmu_device *smmu = root_cfg->smmu;
1399
1400 if (root_cfg->cbar == CBAR_TYPE_S2_TRANS) {
1401 stage = 2;
1402 output_mask = (1ULL << smmu->s2_output_size) - 1;
1403 } else {
1404 stage = 1;
1405 output_mask = (1ULL << smmu->s1_output_size) - 1;
1406 }
1407
1408 if (!pgd)
1409 return -EINVAL;
1410
1411 if (size & ~PAGE_MASK)
1412 return -EINVAL;
1413
1414 input_mask = (1ULL << smmu->input_size) - 1;
1415 if ((phys_addr_t)iova & ~input_mask)
1416 return -ERANGE;
1417
1418 if (paddr & ~output_mask)
1419 return -ERANGE;
1420
Will Deaconc9d09e22014-02-04 22:12:42 +00001421 spin_lock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001422 pgd += pgd_index(iova);
1423 end = iova + size;
1424 do {
1425 unsigned long next = pgd_addr_end(iova, end);
1426
1427 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
1428 flags, stage);
1429 if (ret)
1430 goto out_unlock;
1431
1432 paddr += next - iova;
1433 iova = next;
1434 } while (pgd++, iova != end);
1435
1436out_unlock:
Will Deaconc9d09e22014-02-04 22:12:42 +00001437 spin_unlock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001438
Will Deacon45ae7cf2013-06-24 18:31:25 +01001439 return ret;
1440}
1441
1442static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1443 phys_addr_t paddr, size_t size, int flags)
1444{
1445 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001446
Will Deacon5552ecd2013-11-08 15:08:06 +00001447 if (!smmu_domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001448 return -ENODEV;
1449
1450 /* Check for silent address truncation up the SMMU chain. */
1451 if ((phys_addr_t)iova & ~smmu_domain->output_mask)
1452 return -ERANGE;
1453
1454 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, flags);
1455}
1456
1457static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1458 size_t size)
1459{
1460 int ret;
1461 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001462
1463 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
Will Deacon1463fe42013-07-31 19:21:27 +01001464 arm_smmu_tlb_inv_context(&smmu_domain->root_cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001465 return ret ? ret : size;
1466}
1467
1468static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1469 dma_addr_t iova)
1470{
Will Deacona44a9792013-11-07 18:47:50 +00001471 pgd_t *pgdp, pgd;
1472 pud_t pud;
1473 pmd_t pmd;
1474 pte_t pte;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001475 struct arm_smmu_domain *smmu_domain = domain->priv;
1476 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001477
Will Deacona44a9792013-11-07 18:47:50 +00001478 pgdp = root_cfg->pgd;
1479 if (!pgdp)
1480 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001481
Will Deacona44a9792013-11-07 18:47:50 +00001482 pgd = *(pgdp + pgd_index(iova));
1483 if (pgd_none(pgd))
1484 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001485
Will Deacona44a9792013-11-07 18:47:50 +00001486 pud = *pud_offset(&pgd, iova);
1487 if (pud_none(pud))
1488 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001489
Will Deacona44a9792013-11-07 18:47:50 +00001490 pmd = *pmd_offset(&pud, iova);
1491 if (pmd_none(pmd))
1492 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001493
Will Deacona44a9792013-11-07 18:47:50 +00001494 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001495 if (pte_none(pte))
Will Deacona44a9792013-11-07 18:47:50 +00001496 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001497
Will Deacona44a9792013-11-07 18:47:50 +00001498 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001499}
1500
1501static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1502 unsigned long cap)
1503{
1504 unsigned long caps = 0;
1505 struct arm_smmu_domain *smmu_domain = domain->priv;
1506
1507 if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
1508 caps |= IOMMU_CAP_CACHE_COHERENCY;
1509
1510 return !!(cap & caps);
1511}
1512
1513static int arm_smmu_add_device(struct device *dev)
1514{
1515 struct arm_smmu_device *child, *parent, *smmu;
1516 struct arm_smmu_master *master = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001517 struct iommu_group *group;
1518 int ret;
1519
1520 if (dev->archdata.iommu) {
1521 dev_warn(dev, "IOMMU driver already assigned to device\n");
1522 return -EINVAL;
1523 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001524
1525 spin_lock(&arm_smmu_devices_lock);
1526 list_for_each_entry(parent, &arm_smmu_devices, list) {
1527 smmu = parent;
1528
1529 /* Try to find a child of the current SMMU. */
1530 list_for_each_entry(child, &arm_smmu_devices, list) {
1531 if (child->parent_of_node == parent->dev->of_node) {
1532 /* Does the child sit above our master? */
1533 master = find_smmu_master(child, dev->of_node);
1534 if (master) {
1535 smmu = NULL;
1536 break;
1537 }
1538 }
1539 }
1540
1541 /* We found some children, so keep searching. */
1542 if (!smmu) {
1543 master = NULL;
1544 continue;
1545 }
1546
1547 master = find_smmu_master(smmu, dev->of_node);
1548 if (master)
1549 break;
1550 }
1551 spin_unlock(&arm_smmu_devices_lock);
1552
1553 if (!master)
1554 return -ENODEV;
1555
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001556 group = iommu_group_alloc();
1557 if (IS_ERR(group)) {
1558 dev_err(dev, "Failed to allocate IOMMU group\n");
1559 return PTR_ERR(group);
1560 }
1561
1562 ret = iommu_group_add_device(group, dev);
1563 iommu_group_put(group);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001564 dev->archdata.iommu = smmu;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001565
1566 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001567}
1568
1569static void arm_smmu_remove_device(struct device *dev)
1570{
1571 dev->archdata.iommu = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001572 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001573}
1574
1575static struct iommu_ops arm_smmu_ops = {
1576 .domain_init = arm_smmu_domain_init,
1577 .domain_destroy = arm_smmu_domain_destroy,
1578 .attach_dev = arm_smmu_attach_dev,
1579 .detach_dev = arm_smmu_detach_dev,
1580 .map = arm_smmu_map,
1581 .unmap = arm_smmu_unmap,
1582 .iova_to_phys = arm_smmu_iova_to_phys,
1583 .domain_has_cap = arm_smmu_domain_has_cap,
1584 .add_device = arm_smmu_add_device,
1585 .remove_device = arm_smmu_remove_device,
1586 .pgsize_bitmap = (SECTION_SIZE |
1587 ARM_SMMU_PTE_CONT_SIZE |
1588 PAGE_SIZE),
1589};
1590
1591static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1592{
1593 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001594 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001595 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001596 u32 reg;
1597
1598 /* Clear Global FSR */
1599 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
1600 writel(reg, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001601
1602 /* Mark all SMRn as invalid and all S2CRn as bypass */
1603 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1604 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(i));
1605 writel_relaxed(S2CR_TYPE_BYPASS, gr0_base + ARM_SMMU_GR0_S2CR(i));
1606 }
1607
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001608 /* Make sure all context banks are disabled and clear CB_FSR */
1609 for (i = 0; i < smmu->num_context_banks; ++i) {
1610 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1611 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1612 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1613 }
Will Deacon1463fe42013-07-31 19:21:27 +01001614
Will Deacon45ae7cf2013-06-24 18:31:25 +01001615 /* Invalidate the TLB, just in case */
1616 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1617 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1618 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1619
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001620 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sCR0);
1621
Will Deacon45ae7cf2013-06-24 18:31:25 +01001622 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001623 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001624
1625 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001626 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001627
1628 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001629 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001630
1631 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001632 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001633
1634 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001635 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001636
1637 /* Push the button */
1638 arm_smmu_tlb_sync(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001639 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001640}
1641
1642static int arm_smmu_id_size_to_bits(int size)
1643{
1644 switch (size) {
1645 case 0:
1646 return 32;
1647 case 1:
1648 return 36;
1649 case 2:
1650 return 40;
1651 case 3:
1652 return 42;
1653 case 4:
1654 return 44;
1655 case 5:
1656 default:
1657 return 48;
1658 }
1659}
1660
1661static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1662{
1663 unsigned long size;
1664 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1665 u32 id;
1666
1667 dev_notice(smmu->dev, "probing hardware configuration...\n");
1668
1669 /* Primecell ID */
1670 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1671 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1672 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1673
1674 /* ID0 */
1675 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1676#ifndef CONFIG_64BIT
1677 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1678 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1679 return -ENODEV;
1680 }
1681#endif
1682 if (id & ID0_S1TS) {
1683 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1684 dev_notice(smmu->dev, "\tstage 1 translation\n");
1685 }
1686
1687 if (id & ID0_S2TS) {
1688 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1689 dev_notice(smmu->dev, "\tstage 2 translation\n");
1690 }
1691
1692 if (id & ID0_NTS) {
1693 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1694 dev_notice(smmu->dev, "\tnested translation\n");
1695 }
1696
1697 if (!(smmu->features &
1698 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 |
1699 ARM_SMMU_FEAT_TRANS_NESTED))) {
1700 dev_err(smmu->dev, "\tno translation support!\n");
1701 return -ENODEV;
1702 }
1703
1704 if (id & ID0_CTTW) {
1705 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1706 dev_notice(smmu->dev, "\tcoherent table walk\n");
1707 }
1708
1709 if (id & ID0_SMS) {
1710 u32 smr, sid, mask;
1711
1712 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1713 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1714 ID0_NUMSMRG_MASK;
1715 if (smmu->num_mapping_groups == 0) {
1716 dev_err(smmu->dev,
1717 "stream-matching supported, but no SMRs present!\n");
1718 return -ENODEV;
1719 }
1720
1721 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1722 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1723 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1724 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1725
1726 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1727 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1728 if ((mask & sid) != sid) {
1729 dev_err(smmu->dev,
1730 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1731 mask, sid);
1732 return -ENODEV;
1733 }
1734
1735 dev_notice(smmu->dev,
1736 "\tstream matching with %u register groups, mask 0x%x",
1737 smmu->num_mapping_groups, mask);
1738 }
1739
1740 /* ID1 */
1741 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1742 smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
1743
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001744 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001745 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
1746 size *= (smmu->pagesize << 1);
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001747 if (smmu->size != size)
1748 dev_warn(smmu->dev, "SMMU address space size (0x%lx) differs "
1749 "from mapped region size (0x%lx)!\n", size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001750
1751 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1752 ID1_NUMS2CB_MASK;
1753 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1754 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1755 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1756 return -ENODEV;
1757 }
1758 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1759 smmu->num_context_banks, smmu->num_s2_context_banks);
1760
1761 /* ID2 */
1762 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1763 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1764
1765 /*
1766 * Stage-1 output limited by stage-2 input size due to pgd
1767 * allocation (PTRS_PER_PGD).
1768 */
1769#ifdef CONFIG_64BIT
Will Deacon45ae7cf2013-06-24 18:31:25 +01001770 smmu->s1_output_size = min(39UL, size);
1771#else
1772 smmu->s1_output_size = min(32UL, size);
1773#endif
1774
1775 /* The stage-2 output mask is also applied for bypass */
1776 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
1777 smmu->s2_output_size = min((unsigned long)PHYS_MASK_SHIFT, size);
1778
1779 if (smmu->version == 1) {
1780 smmu->input_size = 32;
1781 } else {
1782#ifdef CONFIG_64BIT
1783 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon06f983d2013-11-05 15:55:04 +00001784 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001785#else
1786 size = 32;
1787#endif
1788 smmu->input_size = size;
1789
1790 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1791 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1792 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1793 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1794 PAGE_SIZE);
1795 return -ENODEV;
1796 }
1797 }
1798
1799 dev_notice(smmu->dev,
1800 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
1801 smmu->input_size, smmu->s1_output_size, smmu->s2_output_size);
1802 return 0;
1803}
1804
1805static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1806{
1807 struct resource *res;
1808 struct arm_smmu_device *smmu;
1809 struct device_node *dev_node;
1810 struct device *dev = &pdev->dev;
1811 struct rb_node *node;
1812 struct of_phandle_args masterspec;
1813 int num_irqs, i, err;
1814
1815 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1816 if (!smmu) {
1817 dev_err(dev, "failed to allocate arm_smmu_device\n");
1818 return -ENOMEM;
1819 }
1820 smmu->dev = dev;
1821
1822 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001823 smmu->base = devm_ioremap_resource(dev, res);
1824 if (IS_ERR(smmu->base))
1825 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001826 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001827
1828 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1829 &smmu->num_global_irqs)) {
1830 dev_err(dev, "missing #global-interrupts property\n");
1831 return -ENODEV;
1832 }
1833
1834 num_irqs = 0;
1835 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1836 num_irqs++;
1837 if (num_irqs > smmu->num_global_irqs)
1838 smmu->num_context_irqs++;
1839 }
1840
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001841 if (!smmu->num_context_irqs) {
1842 dev_err(dev, "found %d interrupts but expected at least %d\n",
1843 num_irqs, smmu->num_global_irqs + 1);
1844 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001845 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001846
1847 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1848 GFP_KERNEL);
1849 if (!smmu->irqs) {
1850 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1851 return -ENOMEM;
1852 }
1853
1854 for (i = 0; i < num_irqs; ++i) {
1855 int irq = platform_get_irq(pdev, i);
1856 if (irq < 0) {
1857 dev_err(dev, "failed to get irq index %d\n", i);
1858 return -ENODEV;
1859 }
1860 smmu->irqs[i] = irq;
1861 }
1862
1863 i = 0;
1864 smmu->masters = RB_ROOT;
1865 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1866 "#stream-id-cells", i,
1867 &masterspec)) {
1868 err = register_smmu_master(smmu, dev, &masterspec);
1869 if (err) {
1870 dev_err(dev, "failed to add master %s\n",
1871 masterspec.np->name);
1872 goto out_put_masters;
1873 }
1874
1875 i++;
1876 }
1877 dev_notice(dev, "registered %d master devices\n", i);
1878
1879 if ((dev_node = of_parse_phandle(dev->of_node, "smmu-parent", 0)))
1880 smmu->parent_of_node = dev_node;
1881
1882 err = arm_smmu_device_cfg_probe(smmu);
1883 if (err)
1884 goto out_put_parent;
1885
1886 if (smmu->version > 1 &&
1887 smmu->num_context_banks != smmu->num_context_irqs) {
1888 dev_err(dev,
1889 "found only %d context interrupt(s) but %d required\n",
1890 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cd2013-11-15 09:42:30 +00001891 err = -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001892 goto out_put_parent;
1893 }
1894
Will Deacon45ae7cf2013-06-24 18:31:25 +01001895 for (i = 0; i < smmu->num_global_irqs; ++i) {
1896 err = request_irq(smmu->irqs[i],
1897 arm_smmu_global_fault,
1898 IRQF_SHARED,
1899 "arm-smmu global fault",
1900 smmu);
1901 if (err) {
1902 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1903 i, smmu->irqs[i]);
1904 goto out_free_irqs;
1905 }
1906 }
1907
1908 INIT_LIST_HEAD(&smmu->list);
1909 spin_lock(&arm_smmu_devices_lock);
1910 list_add(&smmu->list, &arm_smmu_devices);
1911 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001912
1913 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001914 return 0;
1915
1916out_free_irqs:
1917 while (i--)
1918 free_irq(smmu->irqs[i], smmu);
1919
1920out_put_parent:
1921 if (smmu->parent_of_node)
1922 of_node_put(smmu->parent_of_node);
1923
1924out_put_masters:
1925 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
1926 struct arm_smmu_master *master;
1927 master = container_of(node, struct arm_smmu_master, node);
1928 of_node_put(master->of_node);
1929 }
1930
1931 return err;
1932}
1933
1934static int arm_smmu_device_remove(struct platform_device *pdev)
1935{
1936 int i;
1937 struct device *dev = &pdev->dev;
1938 struct arm_smmu_device *curr, *smmu = NULL;
1939 struct rb_node *node;
1940
1941 spin_lock(&arm_smmu_devices_lock);
1942 list_for_each_entry(curr, &arm_smmu_devices, list) {
1943 if (curr->dev == dev) {
1944 smmu = curr;
1945 list_del(&smmu->list);
1946 break;
1947 }
1948 }
1949 spin_unlock(&arm_smmu_devices_lock);
1950
1951 if (!smmu)
1952 return -ENODEV;
1953
1954 if (smmu->parent_of_node)
1955 of_node_put(smmu->parent_of_node);
1956
1957 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
1958 struct arm_smmu_master *master;
1959 master = container_of(node, struct arm_smmu_master, node);
1960 of_node_put(master->of_node);
1961 }
1962
Will Deaconecfadb62013-07-31 19:21:28 +01001963 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001964 dev_err(dev, "removing device with active domains!\n");
1965
1966 for (i = 0; i < smmu->num_global_irqs; ++i)
1967 free_irq(smmu->irqs[i], smmu);
1968
1969 /* Turn the thing off */
Will Deacon25724842013-08-21 13:49:53 +01001970 writel_relaxed(sCR0_CLIENTPD, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001971 return 0;
1972}
1973
1974#ifdef CONFIG_OF
1975static struct of_device_id arm_smmu_of_match[] = {
1976 { .compatible = "arm,smmu-v1", },
1977 { .compatible = "arm,smmu-v2", },
1978 { .compatible = "arm,mmu-400", },
1979 { .compatible = "arm,mmu-500", },
1980 { },
1981};
1982MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1983#endif
1984
1985static struct platform_driver arm_smmu_driver = {
1986 .driver = {
1987 .owner = THIS_MODULE,
1988 .name = "arm-smmu",
1989 .of_match_table = of_match_ptr(arm_smmu_of_match),
1990 },
1991 .probe = arm_smmu_device_dt_probe,
1992 .remove = arm_smmu_device_remove,
1993};
1994
1995static int __init arm_smmu_init(void)
1996{
1997 int ret;
1998
1999 ret = platform_driver_register(&arm_smmu_driver);
2000 if (ret)
2001 return ret;
2002
2003 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01002004 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002005 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2006
Dan Carpenter6614ee72013-08-21 09:34:20 +01002007 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002008 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2009
2010 return 0;
2011}
2012
2013static void __exit arm_smmu_exit(void)
2014{
2015 return platform_driver_unregister(&arm_smmu_driver);
2016}
2017
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002018subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002019module_exit(arm_smmu_exit);
2020
2021MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2022MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2023MODULE_LICENSE("GPL v2");