blob: 509f01f054d9da4f801c4aecac938884ce4bda84 [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
193#define CBAR_S1_MEMATTR_SHIFT 12
194#define CBAR_S1_MEMATTR_MASK 0xf
195#define CBAR_S1_MEMATTR_WB 0xf
196#define CBAR_TYPE_SHIFT 16
197#define CBAR_TYPE_MASK 0x3
198#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
199#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
200#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
201#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
202#define CBAR_IRPTNDX_SHIFT 24
203#define CBAR_IRPTNDX_MASK 0xff
204
205#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
206#define CBA2R_RW64_32BIT (0 << 0)
207#define CBA2R_RW64_64BIT (1 << 0)
208
209/* Translation context bank */
210#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
211#define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize)
212
213#define ARM_SMMU_CB_SCTLR 0x0
214#define ARM_SMMU_CB_RESUME 0x8
215#define ARM_SMMU_CB_TTBCR2 0x10
216#define ARM_SMMU_CB_TTBR0_LO 0x20
217#define ARM_SMMU_CB_TTBR0_HI 0x24
218#define ARM_SMMU_CB_TTBCR 0x30
219#define ARM_SMMU_CB_S1_MAIR0 0x38
220#define ARM_SMMU_CB_FSR 0x58
221#define ARM_SMMU_CB_FAR_LO 0x60
222#define ARM_SMMU_CB_FAR_HI 0x64
223#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon1463fe42013-07-31 19:21:27 +0100224#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon45ae7cf2013-06-24 18:31:25 +0100225
226#define SCTLR_S1_ASIDPNE (1 << 12)
227#define SCTLR_CFCFG (1 << 7)
228#define SCTLR_CFIE (1 << 6)
229#define SCTLR_CFRE (1 << 5)
230#define SCTLR_E (1 << 4)
231#define SCTLR_AFE (1 << 2)
232#define SCTLR_TRE (1 << 1)
233#define SCTLR_M (1 << 0)
234#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
235
236#define RESUME_RETRY (0 << 0)
237#define RESUME_TERMINATE (1 << 0)
238
239#define TTBCR_EAE (1 << 31)
240
241#define TTBCR_PASIZE_SHIFT 16
242#define TTBCR_PASIZE_MASK 0x7
243
244#define TTBCR_TG0_4K (0 << 14)
245#define TTBCR_TG0_64K (1 << 14)
246
247#define TTBCR_SH0_SHIFT 12
248#define TTBCR_SH0_MASK 0x3
249#define TTBCR_SH_NS 0
250#define TTBCR_SH_OS 2
251#define TTBCR_SH_IS 3
252
253#define TTBCR_ORGN0_SHIFT 10
254#define TTBCR_IRGN0_SHIFT 8
255#define TTBCR_RGN_MASK 0x3
256#define TTBCR_RGN_NC 0
257#define TTBCR_RGN_WBWA 1
258#define TTBCR_RGN_WT 2
259#define TTBCR_RGN_WB 3
260
261#define TTBCR_SL0_SHIFT 6
262#define TTBCR_SL0_MASK 0x3
263#define TTBCR_SL0_LVL_2 0
264#define TTBCR_SL0_LVL_1 1
265
266#define TTBCR_T1SZ_SHIFT 16
267#define TTBCR_T0SZ_SHIFT 0
268#define TTBCR_SZ_MASK 0xf
269
270#define TTBCR2_SEP_SHIFT 15
271#define TTBCR2_SEP_MASK 0x7
272
273#define TTBCR2_PASIZE_SHIFT 0
274#define TTBCR2_PASIZE_MASK 0x7
275
276/* Common definitions for PASize and SEP fields */
277#define TTBCR2_ADDR_32 0
278#define TTBCR2_ADDR_36 1
279#define TTBCR2_ADDR_40 2
280#define TTBCR2_ADDR_42 3
281#define TTBCR2_ADDR_44 4
282#define TTBCR2_ADDR_48 5
283
Will Deacon1463fe42013-07-31 19:21:27 +0100284#define TTBRn_HI_ASID_SHIFT 16
285
Will Deacon45ae7cf2013-06-24 18:31:25 +0100286#define MAIR_ATTR_SHIFT(n) ((n) << 3)
287#define MAIR_ATTR_MASK 0xff
288#define MAIR_ATTR_DEVICE 0x04
289#define MAIR_ATTR_NC 0x44
290#define MAIR_ATTR_WBRWA 0xff
291#define MAIR_ATTR_IDX_NC 0
292#define MAIR_ATTR_IDX_CACHE 1
293#define MAIR_ATTR_IDX_DEV 2
294
295#define FSR_MULTI (1 << 31)
296#define FSR_SS (1 << 30)
297#define FSR_UUT (1 << 8)
298#define FSR_ASF (1 << 7)
299#define FSR_TLBLKF (1 << 6)
300#define FSR_TLBMCF (1 << 5)
301#define FSR_EF (1 << 4)
302#define FSR_PF (1 << 3)
303#define FSR_AFF (1 << 2)
304#define FSR_TF (1 << 1)
305
306#define FSR_IGN (FSR_AFF | FSR_ASF | FSR_TLBMCF | \
307 FSR_TLBLKF)
308#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100309 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100310
311#define FSYNR0_WNR (1 << 4)
312
313struct arm_smmu_smr {
314 u8 idx;
315 u16 mask;
316 u16 id;
317};
318
319struct arm_smmu_master {
320 struct device_node *of_node;
321
322 /*
323 * The following is specific to the master's position in the
324 * SMMU chain.
325 */
326 struct rb_node node;
327 int num_streamids;
328 u16 streamids[MAX_MASTER_STREAMIDS];
329
330 /*
331 * We only need to allocate these on the root SMMU, as we
332 * configure unmatched streams to bypass translation.
333 */
334 struct arm_smmu_smr *smrs;
335};
336
337struct arm_smmu_device {
338 struct device *dev;
339 struct device_node *parent_of_node;
340
341 void __iomem *base;
342 unsigned long size;
343 unsigned long pagesize;
344
345#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
346#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
347#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
348#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
349#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
350 u32 features;
351 int version;
352
353 u32 num_context_banks;
354 u32 num_s2_context_banks;
355 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
356 atomic_t irptndx;
357
358 u32 num_mapping_groups;
359 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
360
361 unsigned long input_size;
362 unsigned long s1_output_size;
363 unsigned long s2_output_size;
364
365 u32 num_global_irqs;
366 u32 num_context_irqs;
367 unsigned int *irqs;
368
Will Deacon45ae7cf2013-06-24 18:31:25 +0100369 struct list_head list;
370 struct rb_root masters;
371};
372
373struct arm_smmu_cfg {
374 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100375 u8 cbndx;
376 u8 irptndx;
377 u32 cbar;
378 pgd_t *pgd;
379};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100380#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100381
Will Deaconecfadb62013-07-31 19:21:28 +0100382#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
383#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
384
Will Deacon45ae7cf2013-06-24 18:31:25 +0100385struct arm_smmu_domain {
386 /*
387 * A domain can span across multiple, chained SMMUs and requires
388 * all devices within the domain to follow the same translation
389 * path.
390 */
391 struct arm_smmu_device *leaf_smmu;
392 struct arm_smmu_cfg root_cfg;
393 phys_addr_t output_mask;
394
Will Deaconc9d09e22014-02-04 22:12:42 +0000395 spinlock_t lock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100396};
397
398static DEFINE_SPINLOCK(arm_smmu_devices_lock);
399static LIST_HEAD(arm_smmu_devices);
400
401static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
402 struct device_node *dev_node)
403{
404 struct rb_node *node = smmu->masters.rb_node;
405
406 while (node) {
407 struct arm_smmu_master *master;
408 master = container_of(node, struct arm_smmu_master, node);
409
410 if (dev_node < master->of_node)
411 node = node->rb_left;
412 else if (dev_node > master->of_node)
413 node = node->rb_right;
414 else
415 return master;
416 }
417
418 return NULL;
419}
420
421static int insert_smmu_master(struct arm_smmu_device *smmu,
422 struct arm_smmu_master *master)
423{
424 struct rb_node **new, *parent;
425
426 new = &smmu->masters.rb_node;
427 parent = NULL;
428 while (*new) {
429 struct arm_smmu_master *this;
430 this = container_of(*new, struct arm_smmu_master, node);
431
432 parent = *new;
433 if (master->of_node < this->of_node)
434 new = &((*new)->rb_left);
435 else if (master->of_node > this->of_node)
436 new = &((*new)->rb_right);
437 else
438 return -EEXIST;
439 }
440
441 rb_link_node(&master->node, parent, new);
442 rb_insert_color(&master->node, &smmu->masters);
443 return 0;
444}
445
446static int register_smmu_master(struct arm_smmu_device *smmu,
447 struct device *dev,
448 struct of_phandle_args *masterspec)
449{
450 int i;
451 struct arm_smmu_master *master;
452
453 master = find_smmu_master(smmu, masterspec->np);
454 if (master) {
455 dev_err(dev,
456 "rejecting multiple registrations for master device %s\n",
457 masterspec->np->name);
458 return -EBUSY;
459 }
460
461 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
462 dev_err(dev,
463 "reached maximum number (%d) of stream IDs for master device %s\n",
464 MAX_MASTER_STREAMIDS, masterspec->np->name);
465 return -ENOSPC;
466 }
467
468 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
469 if (!master)
470 return -ENOMEM;
471
472 master->of_node = masterspec->np;
473 master->num_streamids = masterspec->args_count;
474
475 for (i = 0; i < master->num_streamids; ++i)
476 master->streamids[i] = masterspec->args[i];
477
478 return insert_smmu_master(smmu, master);
479}
480
481static struct arm_smmu_device *find_parent_smmu(struct arm_smmu_device *smmu)
482{
483 struct arm_smmu_device *parent;
484
485 if (!smmu->parent_of_node)
486 return NULL;
487
488 spin_lock(&arm_smmu_devices_lock);
489 list_for_each_entry(parent, &arm_smmu_devices, list)
490 if (parent->dev->of_node == smmu->parent_of_node)
491 goto out_unlock;
492
493 parent = NULL;
494 dev_warn(smmu->dev,
495 "Failed to find SMMU parent despite parent in DT\n");
496out_unlock:
497 spin_unlock(&arm_smmu_devices_lock);
498 return parent;
499}
500
501static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
502{
503 int idx;
504
505 do {
506 idx = find_next_zero_bit(map, end, start);
507 if (idx == end)
508 return -ENOSPC;
509 } while (test_and_set_bit(idx, map));
510
511 return idx;
512}
513
514static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
515{
516 clear_bit(idx, map);
517}
518
519/* Wait for any pending TLB invalidations to complete */
520static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
521{
522 int count = 0;
523 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
524
525 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
526 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
527 & sTLBGSTATUS_GSACTIVE) {
528 cpu_relax();
529 if (++count == TLB_LOOP_TIMEOUT) {
530 dev_err_ratelimited(smmu->dev,
531 "TLB sync timed out -- SMMU may be deadlocked\n");
532 return;
533 }
534 udelay(1);
535 }
536}
537
Will Deacon1463fe42013-07-31 19:21:27 +0100538static void arm_smmu_tlb_inv_context(struct arm_smmu_cfg *cfg)
539{
540 struct arm_smmu_device *smmu = cfg->smmu;
541 void __iomem *base = ARM_SMMU_GR0(smmu);
542 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
543
544 if (stage1) {
545 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100546 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
547 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100548 } else {
549 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100550 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
551 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100552 }
553
554 arm_smmu_tlb_sync(smmu);
555}
556
Will Deacon45ae7cf2013-06-24 18:31:25 +0100557static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
558{
559 int flags, ret;
560 u32 fsr, far, fsynr, resume;
561 unsigned long iova;
562 struct iommu_domain *domain = dev;
563 struct arm_smmu_domain *smmu_domain = domain->priv;
564 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
565 struct arm_smmu_device *smmu = root_cfg->smmu;
566 void __iomem *cb_base;
567
568 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
569 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
570
571 if (!(fsr & FSR_FAULT))
572 return IRQ_NONE;
573
574 if (fsr & FSR_IGN)
575 dev_err_ratelimited(smmu->dev,
576 "Unexpected context fault (fsr 0x%u)\n",
577 fsr);
578
579 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
580 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
581
582 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
583 iova = far;
584#ifdef CONFIG_64BIT
585 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
586 iova |= ((unsigned long)far << 32);
587#endif
588
589 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
590 ret = IRQ_HANDLED;
591 resume = RESUME_RETRY;
592 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100593 dev_err_ratelimited(smmu->dev,
594 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
595 iova, fsynr, root_cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100596 ret = IRQ_NONE;
597 resume = RESUME_TERMINATE;
598 }
599
600 /* Clear the faulting FSR */
601 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
602
603 /* Retry or terminate any stalled transactions */
604 if (fsr & FSR_SS)
605 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
606
607 return ret;
608}
609
610static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
611{
612 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
613 struct arm_smmu_device *smmu = dev;
614 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
615
616 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100617 if (!gfsr)
618 return IRQ_NONE;
619
Will Deacon45ae7cf2013-06-24 18:31:25 +0100620 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
621 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
622 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
623
624 dev_err_ratelimited(smmu->dev,
625 "Unexpected global fault, this could be serious\n");
626 dev_err_ratelimited(smmu->dev,
627 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
628 gfsr, gfsynr0, gfsynr1, gfsynr2);
629
630 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100631 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100632}
633
Will Deacon6dd35f42014-02-05 17:49:34 +0000634static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
635 size_t size)
636{
637 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
638
639
640 /* Ensure new page tables are visible to the hardware walker */
641 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
642 dsb();
643 } else {
644 /*
645 * If the SMMU can't walk tables in the CPU caches, treat them
646 * like non-coherent DMA since we need to flush the new entries
647 * all the way out to memory. There's no possibility of
648 * recursion here as the SMMU table walker will not be wired
649 * through another SMMU.
650 */
651 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
652 DMA_TO_DEVICE);
653 }
654}
655
Will Deacon45ae7cf2013-06-24 18:31:25 +0100656static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
657{
658 u32 reg;
659 bool stage1;
660 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
661 struct arm_smmu_device *smmu = root_cfg->smmu;
662 void __iomem *cb_base, *gr0_base, *gr1_base;
663
664 gr0_base = ARM_SMMU_GR0(smmu);
665 gr1_base = ARM_SMMU_GR1(smmu);
666 stage1 = root_cfg->cbar != CBAR_TYPE_S2_TRANS;
667 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
668
669 /* CBAR */
Will Deacon1463fe42013-07-31 19:21:27 +0100670 reg = root_cfg->cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100671 if (smmu->version == 1)
672 reg |= root_cfg->irptndx << CBAR_IRPTNDX_SHIFT;
673
674 /* Use the weakest memory type, so it is overridden by the pte */
675 if (stage1)
676 reg |= (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
Will Deacon1463fe42013-07-31 19:21:27 +0100677 else
Will Deaconecfadb62013-07-31 19:21:28 +0100678 reg |= ARM_SMMU_CB_VMID(root_cfg) << CBAR_VMID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100679 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(root_cfg->cbndx));
680
681 if (smmu->version > 1) {
682 /* CBA2R */
683#ifdef CONFIG_64BIT
684 reg = CBA2R_RW64_64BIT;
685#else
686 reg = CBA2R_RW64_32BIT;
687#endif
688 writel_relaxed(reg,
689 gr1_base + ARM_SMMU_GR1_CBA2R(root_cfg->cbndx));
690
691 /* TTBCR2 */
692 switch (smmu->input_size) {
693 case 32:
694 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
695 break;
696 case 36:
697 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
698 break;
699 case 39:
700 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
701 break;
702 case 42:
703 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
704 break;
705 case 44:
706 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
707 break;
708 case 48:
709 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
710 break;
711 }
712
713 switch (smmu->s1_output_size) {
714 case 32:
715 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
716 break;
717 case 36:
718 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
719 break;
720 case 39:
721 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
722 break;
723 case 42:
724 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
725 break;
726 case 44:
727 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
728 break;
729 case 48:
730 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
731 break;
732 }
733
734 if (stage1)
735 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
736 }
737
738 /* TTBR0 */
Will Deacon6dd35f42014-02-05 17:49:34 +0000739 arm_smmu_flush_pgtable(smmu, root_cfg->pgd,
740 PTRS_PER_PGD * sizeof(pgd_t));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100741 reg = __pa(root_cfg->pgd);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100742 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
743 reg = (phys_addr_t)__pa(root_cfg->pgd) >> 32;
Will Deacon1463fe42013-07-31 19:21:27 +0100744 if (stage1)
Will Deaconecfadb62013-07-31 19:21:28 +0100745 reg |= ARM_SMMU_CB_ASID(root_cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100746 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100747
748 /*
749 * TTBCR
750 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
751 */
752 if (smmu->version > 1) {
753 if (PAGE_SIZE == SZ_4K)
754 reg = TTBCR_TG0_4K;
755 else
756 reg = TTBCR_TG0_64K;
757
758 if (!stage1) {
759 switch (smmu->s2_output_size) {
760 case 32:
761 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
762 break;
763 case 36:
764 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
765 break;
766 case 40:
767 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
768 break;
769 case 42:
770 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
771 break;
772 case 44:
773 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
774 break;
775 case 48:
776 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
777 break;
778 }
779 } else {
780 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
781 }
782 } else {
783 reg = 0;
784 }
785
786 reg |= TTBCR_EAE |
787 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
788 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
789 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT) |
790 (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
791 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
792
793 /* MAIR0 (stage-1 only) */
794 if (stage1) {
795 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
796 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
797 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
798 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
799 }
800
Will Deacon45ae7cf2013-06-24 18:31:25 +0100801 /* SCTLR */
802 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
803 if (stage1)
804 reg |= SCTLR_S1_ASIDPNE;
805#ifdef __BIG_ENDIAN
806 reg |= SCTLR_E;
807#endif
Will Deacon25724842013-08-21 13:49:53 +0100808 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100809}
810
811static int arm_smmu_init_domain_context(struct iommu_domain *domain,
812 struct device *dev)
813{
814 int irq, ret, start;
815 struct arm_smmu_domain *smmu_domain = domain->priv;
816 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
817 struct arm_smmu_device *smmu, *parent;
818
819 /*
820 * Walk the SMMU chain to find the root device for this chain.
821 * We assume that no masters have translations which terminate
822 * early, and therefore check that the root SMMU does indeed have
823 * a StreamID for the master in question.
824 */
825 parent = dev->archdata.iommu;
826 smmu_domain->output_mask = -1;
827 do {
828 smmu = parent;
829 smmu_domain->output_mask &= (1ULL << smmu->s2_output_size) - 1;
830 } while ((parent = find_parent_smmu(smmu)));
831
832 if (!find_smmu_master(smmu, dev->of_node)) {
833 dev_err(dev, "unable to find root SMMU for device\n");
834 return -ENODEV;
835 }
836
Will Deacon45ae7cf2013-06-24 18:31:25 +0100837 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
838 /*
839 * We will likely want to change this if/when KVM gets
840 * involved.
841 */
842 root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
843 start = smmu->num_s2_context_banks;
844 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) {
845 root_cfg->cbar = CBAR_TYPE_S2_TRANS;
846 start = 0;
847 } else {
848 root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
849 start = smmu->num_s2_context_banks;
850 }
851
852 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
853 smmu->num_context_banks);
854 if (IS_ERR_VALUE(ret))
Will Deaconecfadb62013-07-31 19:21:28 +0100855 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100856
857 root_cfg->cbndx = ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100858 if (smmu->version == 1) {
859 root_cfg->irptndx = atomic_inc_return(&smmu->irptndx);
860 root_cfg->irptndx %= smmu->num_context_irqs;
861 } else {
862 root_cfg->irptndx = root_cfg->cbndx;
863 }
864
865 irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
866 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
867 "arm-smmu-context-fault", domain);
868 if (IS_ERR_VALUE(ret)) {
869 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
870 root_cfg->irptndx, irq);
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100871 root_cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100872 goto out_free_context;
873 }
874
875 root_cfg->smmu = smmu;
876 arm_smmu_init_context_bank(smmu_domain);
877 return ret;
878
879out_free_context:
880 __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100881 return ret;
882}
883
884static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
885{
886 struct arm_smmu_domain *smmu_domain = domain->priv;
887 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
888 struct arm_smmu_device *smmu = root_cfg->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100889 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100890 int irq;
891
892 if (!smmu)
893 return;
894
Will Deacon1463fe42013-07-31 19:21:27 +0100895 /* Disable the context bank and nuke the TLB before freeing it. */
896 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
897 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
898 arm_smmu_tlb_inv_context(root_cfg);
899
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100900 if (root_cfg->irptndx != INVALID_IRPTNDX) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100901 irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
902 free_irq(irq, domain);
903 }
904
Will Deacon45ae7cf2013-06-24 18:31:25 +0100905 __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
906}
907
908static int arm_smmu_domain_init(struct iommu_domain *domain)
909{
910 struct arm_smmu_domain *smmu_domain;
911 pgd_t *pgd;
912
913 /*
914 * Allocate the domain and initialise some of its data structures.
915 * We can't really do anything meaningful until we've added a
916 * master.
917 */
918 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
919 if (!smmu_domain)
920 return -ENOMEM;
921
922 pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
923 if (!pgd)
924 goto out_free_domain;
925 smmu_domain->root_cfg.pgd = pgd;
926
Will Deaconc9d09e22014-02-04 22:12:42 +0000927 spin_lock_init(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100928 domain->priv = smmu_domain;
929 return 0;
930
931out_free_domain:
932 kfree(smmu_domain);
933 return -ENOMEM;
934}
935
936static void arm_smmu_free_ptes(pmd_t *pmd)
937{
938 pgtable_t table = pmd_pgtable(*pmd);
939 pgtable_page_dtor(table);
940 __free_page(table);
941}
942
943static void arm_smmu_free_pmds(pud_t *pud)
944{
945 int i;
946 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
947
948 pmd = pmd_base;
949 for (i = 0; i < PTRS_PER_PMD; ++i) {
950 if (pmd_none(*pmd))
951 continue;
952
953 arm_smmu_free_ptes(pmd);
954 pmd++;
955 }
956
957 pmd_free(NULL, pmd_base);
958}
959
960static void arm_smmu_free_puds(pgd_t *pgd)
961{
962 int i;
963 pud_t *pud, *pud_base = pud_offset(pgd, 0);
964
965 pud = pud_base;
966 for (i = 0; i < PTRS_PER_PUD; ++i) {
967 if (pud_none(*pud))
968 continue;
969
970 arm_smmu_free_pmds(pud);
971 pud++;
972 }
973
974 pud_free(NULL, pud_base);
975}
976
977static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
978{
979 int i;
980 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
981 pgd_t *pgd, *pgd_base = root_cfg->pgd;
982
983 /*
984 * Recursively free the page tables for this domain. We don't
985 * care about speculative TLB filling, because the TLB will be
986 * nuked next time this context bank is re-allocated and no devices
987 * currently map to these tables.
988 */
989 pgd = pgd_base;
990 for (i = 0; i < PTRS_PER_PGD; ++i) {
991 if (pgd_none(*pgd))
992 continue;
993 arm_smmu_free_puds(pgd);
994 pgd++;
995 }
996
997 kfree(pgd_base);
998}
999
1000static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1001{
1002 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001003
1004 /*
1005 * Free the domain resources. We assume that all devices have
1006 * already been detached.
1007 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001008 arm_smmu_destroy_domain_context(domain);
1009 arm_smmu_free_pgtables(smmu_domain);
1010 kfree(smmu_domain);
1011}
1012
1013static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
1014 struct arm_smmu_master *master)
1015{
1016 int i;
1017 struct arm_smmu_smr *smrs;
1018 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1019
1020 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1021 return 0;
1022
1023 if (master->smrs)
1024 return -EEXIST;
1025
1026 smrs = kmalloc(sizeof(*smrs) * master->num_streamids, GFP_KERNEL);
1027 if (!smrs) {
1028 dev_err(smmu->dev, "failed to allocate %d SMRs for master %s\n",
1029 master->num_streamids, master->of_node->name);
1030 return -ENOMEM;
1031 }
1032
1033 /* Allocate the SMRs on the root SMMU */
1034 for (i = 0; i < master->num_streamids; ++i) {
1035 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1036 smmu->num_mapping_groups);
1037 if (IS_ERR_VALUE(idx)) {
1038 dev_err(smmu->dev, "failed to allocate free SMR\n");
1039 goto err_free_smrs;
1040 }
1041
1042 smrs[i] = (struct arm_smmu_smr) {
1043 .idx = idx,
1044 .mask = 0, /* We don't currently share SMRs */
1045 .id = master->streamids[i],
1046 };
1047 }
1048
1049 /* It worked! Now, poke the actual hardware */
1050 for (i = 0; i < master->num_streamids; ++i) {
1051 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1052 smrs[i].mask << SMR_MASK_SHIFT;
1053 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1054 }
1055
1056 master->smrs = smrs;
1057 return 0;
1058
1059err_free_smrs:
1060 while (--i >= 0)
1061 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1062 kfree(smrs);
1063 return -ENOSPC;
1064}
1065
1066static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
1067 struct arm_smmu_master *master)
1068{
1069 int i;
1070 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1071 struct arm_smmu_smr *smrs = master->smrs;
1072
1073 /* Invalidate the SMRs before freeing back to the allocator */
1074 for (i = 0; i < master->num_streamids; ++i) {
1075 u8 idx = smrs[i].idx;
1076 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1077 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1078 }
1079
1080 master->smrs = NULL;
1081 kfree(smrs);
1082}
1083
1084static void arm_smmu_bypass_stream_mapping(struct arm_smmu_device *smmu,
1085 struct arm_smmu_master *master)
1086{
1087 int i;
1088 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1089
1090 for (i = 0; i < master->num_streamids; ++i) {
1091 u16 sid = master->streamids[i];
1092 writel_relaxed(S2CR_TYPE_BYPASS,
1093 gr0_base + ARM_SMMU_GR0_S2CR(sid));
1094 }
1095}
1096
1097static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1098 struct arm_smmu_master *master)
1099{
1100 int i, ret;
1101 struct arm_smmu_device *parent, *smmu = smmu_domain->root_cfg.smmu;
1102 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1103
1104 ret = arm_smmu_master_configure_smrs(smmu, master);
1105 if (ret)
1106 return ret;
1107
1108 /* Bypass the leaves */
1109 smmu = smmu_domain->leaf_smmu;
1110 while ((parent = find_parent_smmu(smmu))) {
1111 /*
1112 * We won't have a StreamID match for anything but the root
1113 * smmu, so we only need to worry about StreamID indexing,
1114 * where we must install bypass entries in the S2CRs.
1115 */
1116 if (smmu->features & ARM_SMMU_FEAT_STREAM_MATCH)
1117 continue;
1118
1119 arm_smmu_bypass_stream_mapping(smmu, master);
1120 smmu = parent;
1121 }
1122
1123 /* Now we're at the root, time to point at our context bank */
1124 for (i = 0; i < master->num_streamids; ++i) {
1125 u32 idx, s2cr;
1126 idx = master->smrs ? master->smrs[i].idx : master->streamids[i];
1127 s2cr = (S2CR_TYPE_TRANS << S2CR_TYPE_SHIFT) |
1128 (smmu_domain->root_cfg.cbndx << S2CR_CBNDX_SHIFT);
1129 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1130 }
1131
1132 return 0;
1133}
1134
1135static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
1136 struct arm_smmu_master *master)
1137{
1138 struct arm_smmu_device *smmu = smmu_domain->root_cfg.smmu;
1139
1140 /*
1141 * We *must* clear the S2CR first, because freeing the SMR means
1142 * that it can be re-allocated immediately.
1143 */
1144 arm_smmu_bypass_stream_mapping(smmu, master);
1145 arm_smmu_master_free_smrs(smmu, master);
1146}
1147
1148static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1149{
1150 int ret = -EINVAL;
1151 struct arm_smmu_domain *smmu_domain = domain->priv;
1152 struct arm_smmu_device *device_smmu = dev->archdata.iommu;
1153 struct arm_smmu_master *master;
1154
1155 if (!device_smmu) {
1156 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1157 return -ENXIO;
1158 }
1159
1160 /*
1161 * Sanity check the domain. We don't currently support domains
1162 * that cross between different SMMU chains.
1163 */
Will Deaconc9d09e22014-02-04 22:12:42 +00001164 spin_lock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001165 if (!smmu_domain->leaf_smmu) {
1166 /* Now that we have a master, we can finalise the domain */
1167 ret = arm_smmu_init_domain_context(domain, dev);
1168 if (IS_ERR_VALUE(ret))
1169 goto err_unlock;
1170
1171 smmu_domain->leaf_smmu = device_smmu;
1172 } else if (smmu_domain->leaf_smmu != device_smmu) {
1173 dev_err(dev,
1174 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1175 dev_name(smmu_domain->leaf_smmu->dev),
1176 dev_name(device_smmu->dev));
1177 goto err_unlock;
1178 }
Will Deaconc9d09e22014-02-04 22:12:42 +00001179 spin_unlock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001180
1181 /* Looks ok, so add the device to the domain */
1182 master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node);
1183 if (!master)
1184 return -ENODEV;
1185
1186 return arm_smmu_domain_add_master(smmu_domain, master);
1187
1188err_unlock:
Will Deaconc9d09e22014-02-04 22:12:42 +00001189 spin_unlock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001190 return ret;
1191}
1192
1193static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1194{
1195 struct arm_smmu_domain *smmu_domain = domain->priv;
1196 struct arm_smmu_master *master;
1197
1198 master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node);
1199 if (master)
1200 arm_smmu_domain_remove_master(smmu_domain, master);
1201}
1202
Will Deacon45ae7cf2013-06-24 18:31:25 +01001203static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1204 unsigned long end)
1205{
1206 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1207 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1208}
1209
1210static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1211 unsigned long addr, unsigned long end,
1212 unsigned long pfn, int flags, int stage)
1213{
1214 pte_t *pte, *start;
Will Deaconcf2d45b2013-11-05 16:32:00 +00001215 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001216
1217 if (pmd_none(*pmd)) {
1218 /* Allocate a new set of tables */
Will Deaconc9d09e22014-02-04 22:12:42 +00001219 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001220 if (!table)
1221 return -ENOMEM;
1222
Will Deacon6dd35f42014-02-05 17:49:34 +00001223 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
Kirill A. Shutemov01058e72013-11-14 14:31:49 -08001224 if (!pgtable_page_ctor(table)) {
1225 __free_page(table);
1226 return -ENOMEM;
1227 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001228 pmd_populate(NULL, pmd, table);
1229 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1230 }
1231
1232 if (stage == 1) {
Will Deacon1463fe42013-07-31 19:21:27 +01001233 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001234 if (!(flags & IOMMU_WRITE) && (flags & IOMMU_READ))
1235 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1236
1237 if (flags & IOMMU_CACHE)
1238 pteval |= (MAIR_ATTR_IDX_CACHE <<
1239 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1240 } else {
1241 pteval |= ARM_SMMU_PTE_HAP_FAULT;
1242 if (flags & IOMMU_READ)
1243 pteval |= ARM_SMMU_PTE_HAP_READ;
1244 if (flags & IOMMU_WRITE)
1245 pteval |= ARM_SMMU_PTE_HAP_WRITE;
1246 if (flags & IOMMU_CACHE)
1247 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1248 else
1249 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1250 }
1251
1252 /* If no access, create a faulting entry to avoid TLB fills */
Will Deaconcf2d45b2013-11-05 16:32:00 +00001253 if (flags & IOMMU_EXEC)
1254 pteval &= ~ARM_SMMU_PTE_XN;
1255 else if (!(flags & (IOMMU_READ | IOMMU_WRITE)))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001256 pteval &= ~ARM_SMMU_PTE_PAGE;
1257
1258 pteval |= ARM_SMMU_PTE_SH_IS;
1259 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1260 pte = start;
1261
1262 /*
1263 * Install the page table entries. This is fairly complicated
1264 * since we attempt to make use of the contiguous hint in the
1265 * ptes where possible. The contiguous hint indicates a series
1266 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1267 * contiguous region with the following constraints:
1268 *
1269 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1270 * - Each pte in the region has the contiguous hint bit set
1271 *
1272 * This complicates unmapping (also handled by this code, when
1273 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1274 * possible, yet highly unlikely, that a client may unmap only
1275 * part of a contiguous range. This requires clearing of the
1276 * contiguous hint bits in the range before installing the new
1277 * faulting entries.
1278 *
1279 * Note that re-mapping an address range without first unmapping
1280 * it is not supported, so TLB invalidation is not required here
1281 * and is instead performed at unmap and domain-init time.
1282 */
1283 do {
1284 int i = 1;
1285 pteval &= ~ARM_SMMU_PTE_CONT;
1286
1287 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1288 i = ARM_SMMU_PTE_CONT_ENTRIES;
1289 pteval |= ARM_SMMU_PTE_CONT;
1290 } else if (pte_val(*pte) &
1291 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1292 int j;
1293 pte_t *cont_start;
1294 unsigned long idx = pte_index(addr);
1295
1296 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1297 cont_start = pmd_page_vaddr(*pmd) + idx;
1298 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
1299 pte_val(*(cont_start + j)) &= ~ARM_SMMU_PTE_CONT;
1300
1301 arm_smmu_flush_pgtable(smmu, cont_start,
1302 sizeof(*pte) *
1303 ARM_SMMU_PTE_CONT_ENTRIES);
1304 }
1305
1306 do {
1307 *pte = pfn_pte(pfn, __pgprot(pteval));
1308 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1309 } while (addr != end);
1310
1311 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1312 return 0;
1313}
1314
1315static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1316 unsigned long addr, unsigned long end,
1317 phys_addr_t phys, int flags, int stage)
1318{
1319 int ret;
1320 pmd_t *pmd;
1321 unsigned long next, pfn = __phys_to_pfn(phys);
1322
1323#ifndef __PAGETABLE_PMD_FOLDED
1324 if (pud_none(*pud)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001325 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001326 if (!pmd)
1327 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001328
Will Deacon6dd35f42014-02-05 17:49:34 +00001329 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001330 pud_populate(NULL, pud, pmd);
1331 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1332
1333 pmd += pmd_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001334 } else
1335#endif
1336 pmd = pmd_offset(pud, addr);
1337
1338 do {
1339 next = pmd_addr_end(addr, end);
1340 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, end, pfn,
1341 flags, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001342 phys += next - addr;
1343 } while (pmd++, addr = next, addr < end);
1344
1345 return ret;
1346}
1347
1348static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1349 unsigned long addr, unsigned long end,
1350 phys_addr_t phys, int flags, int stage)
1351{
1352 int ret = 0;
1353 pud_t *pud;
1354 unsigned long next;
1355
1356#ifndef __PAGETABLE_PUD_FOLDED
1357 if (pgd_none(*pgd)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001358 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001359 if (!pud)
1360 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001361
Will Deacon6dd35f42014-02-05 17:49:34 +00001362 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001363 pgd_populate(NULL, pgd, pud);
1364 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1365
1366 pud += pud_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001367 } else
1368#endif
1369 pud = pud_offset(pgd, addr);
1370
1371 do {
1372 next = pud_addr_end(addr, end);
1373 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
1374 flags, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001375 phys += next - addr;
1376 } while (pud++, addr = next, addr < end);
1377
1378 return ret;
1379}
1380
1381static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1382 unsigned long iova, phys_addr_t paddr,
1383 size_t size, int flags)
1384{
1385 int ret, stage;
1386 unsigned long end;
1387 phys_addr_t input_mask, output_mask;
1388 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
1389 pgd_t *pgd = root_cfg->pgd;
1390 struct arm_smmu_device *smmu = root_cfg->smmu;
1391
1392 if (root_cfg->cbar == CBAR_TYPE_S2_TRANS) {
1393 stage = 2;
1394 output_mask = (1ULL << smmu->s2_output_size) - 1;
1395 } else {
1396 stage = 1;
1397 output_mask = (1ULL << smmu->s1_output_size) - 1;
1398 }
1399
1400 if (!pgd)
1401 return -EINVAL;
1402
1403 if (size & ~PAGE_MASK)
1404 return -EINVAL;
1405
1406 input_mask = (1ULL << smmu->input_size) - 1;
1407 if ((phys_addr_t)iova & ~input_mask)
1408 return -ERANGE;
1409
1410 if (paddr & ~output_mask)
1411 return -ERANGE;
1412
Will Deaconc9d09e22014-02-04 22:12:42 +00001413 spin_lock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001414 pgd += pgd_index(iova);
1415 end = iova + size;
1416 do {
1417 unsigned long next = pgd_addr_end(iova, end);
1418
1419 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
1420 flags, stage);
1421 if (ret)
1422 goto out_unlock;
1423
1424 paddr += next - iova;
1425 iova = next;
1426 } while (pgd++, iova != end);
1427
1428out_unlock:
Will Deaconc9d09e22014-02-04 22:12:42 +00001429 spin_unlock(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001430
Will Deacon45ae7cf2013-06-24 18:31:25 +01001431 return ret;
1432}
1433
1434static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1435 phys_addr_t paddr, size_t size, int flags)
1436{
1437 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001438
Will Deacon5552ecd2013-11-08 15:08:06 +00001439 if (!smmu_domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001440 return -ENODEV;
1441
1442 /* Check for silent address truncation up the SMMU chain. */
1443 if ((phys_addr_t)iova & ~smmu_domain->output_mask)
1444 return -ERANGE;
1445
1446 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, flags);
1447}
1448
1449static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1450 size_t size)
1451{
1452 int ret;
1453 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001454
1455 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
Will Deacon1463fe42013-07-31 19:21:27 +01001456 arm_smmu_tlb_inv_context(&smmu_domain->root_cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001457 return ret ? ret : size;
1458}
1459
1460static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1461 dma_addr_t iova)
1462{
Will Deacona44a97912013-11-07 18:47:50 +00001463 pgd_t *pgdp, pgd;
1464 pud_t pud;
1465 pmd_t pmd;
1466 pte_t pte;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001467 struct arm_smmu_domain *smmu_domain = domain->priv;
1468 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001469
Will Deacona44a97912013-11-07 18:47:50 +00001470 pgdp = root_cfg->pgd;
1471 if (!pgdp)
1472 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001473
Will Deacona44a97912013-11-07 18:47:50 +00001474 pgd = *(pgdp + pgd_index(iova));
1475 if (pgd_none(pgd))
1476 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001477
Will Deacona44a97912013-11-07 18:47:50 +00001478 pud = *pud_offset(&pgd, iova);
1479 if (pud_none(pud))
1480 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001481
Will Deacona44a97912013-11-07 18:47:50 +00001482 pmd = *pmd_offset(&pud, iova);
1483 if (pmd_none(pmd))
1484 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001485
Will Deacona44a97912013-11-07 18:47:50 +00001486 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001487 if (pte_none(pte))
Will Deacona44a97912013-11-07 18:47:50 +00001488 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001489
Will Deacona44a97912013-11-07 18:47:50 +00001490 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001491}
1492
1493static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1494 unsigned long cap)
1495{
1496 unsigned long caps = 0;
1497 struct arm_smmu_domain *smmu_domain = domain->priv;
1498
1499 if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
1500 caps |= IOMMU_CAP_CACHE_COHERENCY;
1501
1502 return !!(cap & caps);
1503}
1504
1505static int arm_smmu_add_device(struct device *dev)
1506{
1507 struct arm_smmu_device *child, *parent, *smmu;
1508 struct arm_smmu_master *master = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001509 struct iommu_group *group;
1510 int ret;
1511
1512 if (dev->archdata.iommu) {
1513 dev_warn(dev, "IOMMU driver already assigned to device\n");
1514 return -EINVAL;
1515 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001516
1517 spin_lock(&arm_smmu_devices_lock);
1518 list_for_each_entry(parent, &arm_smmu_devices, list) {
1519 smmu = parent;
1520
1521 /* Try to find a child of the current SMMU. */
1522 list_for_each_entry(child, &arm_smmu_devices, list) {
1523 if (child->parent_of_node == parent->dev->of_node) {
1524 /* Does the child sit above our master? */
1525 master = find_smmu_master(child, dev->of_node);
1526 if (master) {
1527 smmu = NULL;
1528 break;
1529 }
1530 }
1531 }
1532
1533 /* We found some children, so keep searching. */
1534 if (!smmu) {
1535 master = NULL;
1536 continue;
1537 }
1538
1539 master = find_smmu_master(smmu, dev->of_node);
1540 if (master)
1541 break;
1542 }
1543 spin_unlock(&arm_smmu_devices_lock);
1544
1545 if (!master)
1546 return -ENODEV;
1547
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001548 group = iommu_group_alloc();
1549 if (IS_ERR(group)) {
1550 dev_err(dev, "Failed to allocate IOMMU group\n");
1551 return PTR_ERR(group);
1552 }
1553
1554 ret = iommu_group_add_device(group, dev);
1555 iommu_group_put(group);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001556 dev->archdata.iommu = smmu;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001557
1558 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001559}
1560
1561static void arm_smmu_remove_device(struct device *dev)
1562{
1563 dev->archdata.iommu = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001564 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001565}
1566
1567static struct iommu_ops arm_smmu_ops = {
1568 .domain_init = arm_smmu_domain_init,
1569 .domain_destroy = arm_smmu_domain_destroy,
1570 .attach_dev = arm_smmu_attach_dev,
1571 .detach_dev = arm_smmu_detach_dev,
1572 .map = arm_smmu_map,
1573 .unmap = arm_smmu_unmap,
1574 .iova_to_phys = arm_smmu_iova_to_phys,
1575 .domain_has_cap = arm_smmu_domain_has_cap,
1576 .add_device = arm_smmu_add_device,
1577 .remove_device = arm_smmu_remove_device,
1578 .pgsize_bitmap = (SECTION_SIZE |
1579 ARM_SMMU_PTE_CONT_SIZE |
1580 PAGE_SIZE),
1581};
1582
1583static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1584{
1585 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001586 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001587 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001588 u32 reg;
1589
1590 /* Clear Global FSR */
1591 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
1592 writel(reg, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001593
1594 /* Mark all SMRn as invalid and all S2CRn as bypass */
1595 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1596 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(i));
1597 writel_relaxed(S2CR_TYPE_BYPASS, gr0_base + ARM_SMMU_GR0_S2CR(i));
1598 }
1599
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001600 /* Make sure all context banks are disabled and clear CB_FSR */
1601 for (i = 0; i < smmu->num_context_banks; ++i) {
1602 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1603 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1604 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1605 }
Will Deacon1463fe42013-07-31 19:21:27 +01001606
Will Deacon45ae7cf2013-06-24 18:31:25 +01001607 /* Invalidate the TLB, just in case */
1608 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1609 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1610 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1611
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001612 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sCR0);
1613
Will Deacon45ae7cf2013-06-24 18:31:25 +01001614 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001615 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001616
1617 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001618 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001619
1620 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001621 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001622
1623 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001624 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001625
1626 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001627 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001628
1629 /* Push the button */
1630 arm_smmu_tlb_sync(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001631 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001632}
1633
1634static int arm_smmu_id_size_to_bits(int size)
1635{
1636 switch (size) {
1637 case 0:
1638 return 32;
1639 case 1:
1640 return 36;
1641 case 2:
1642 return 40;
1643 case 3:
1644 return 42;
1645 case 4:
1646 return 44;
1647 case 5:
1648 default:
1649 return 48;
1650 }
1651}
1652
1653static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1654{
1655 unsigned long size;
1656 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1657 u32 id;
1658
1659 dev_notice(smmu->dev, "probing hardware configuration...\n");
1660
1661 /* Primecell ID */
1662 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1663 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1664 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1665
1666 /* ID0 */
1667 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1668#ifndef CONFIG_64BIT
1669 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1670 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1671 return -ENODEV;
1672 }
1673#endif
1674 if (id & ID0_S1TS) {
1675 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1676 dev_notice(smmu->dev, "\tstage 1 translation\n");
1677 }
1678
1679 if (id & ID0_S2TS) {
1680 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1681 dev_notice(smmu->dev, "\tstage 2 translation\n");
1682 }
1683
1684 if (id & ID0_NTS) {
1685 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1686 dev_notice(smmu->dev, "\tnested translation\n");
1687 }
1688
1689 if (!(smmu->features &
1690 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 |
1691 ARM_SMMU_FEAT_TRANS_NESTED))) {
1692 dev_err(smmu->dev, "\tno translation support!\n");
1693 return -ENODEV;
1694 }
1695
1696 if (id & ID0_CTTW) {
1697 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1698 dev_notice(smmu->dev, "\tcoherent table walk\n");
1699 }
1700
1701 if (id & ID0_SMS) {
1702 u32 smr, sid, mask;
1703
1704 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1705 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1706 ID0_NUMSMRG_MASK;
1707 if (smmu->num_mapping_groups == 0) {
1708 dev_err(smmu->dev,
1709 "stream-matching supported, but no SMRs present!\n");
1710 return -ENODEV;
1711 }
1712
1713 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1714 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1715 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1716 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1717
1718 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1719 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1720 if ((mask & sid) != sid) {
1721 dev_err(smmu->dev,
1722 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1723 mask, sid);
1724 return -ENODEV;
1725 }
1726
1727 dev_notice(smmu->dev,
1728 "\tstream matching with %u register groups, mask 0x%x",
1729 smmu->num_mapping_groups, mask);
1730 }
1731
1732 /* ID1 */
1733 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1734 smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
1735
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001736 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001737 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
1738 size *= (smmu->pagesize << 1);
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001739 if (smmu->size != size)
1740 dev_warn(smmu->dev, "SMMU address space size (0x%lx) differs "
1741 "from mapped region size (0x%lx)!\n", size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001742
1743 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1744 ID1_NUMS2CB_MASK;
1745 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1746 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1747 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1748 return -ENODEV;
1749 }
1750 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1751 smmu->num_context_banks, smmu->num_s2_context_banks);
1752
1753 /* ID2 */
1754 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1755 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1756
1757 /*
1758 * Stage-1 output limited by stage-2 input size due to pgd
1759 * allocation (PTRS_PER_PGD).
1760 */
1761#ifdef CONFIG_64BIT
Will Deacon45ae7cf2013-06-24 18:31:25 +01001762 smmu->s1_output_size = min(39UL, size);
1763#else
1764 smmu->s1_output_size = min(32UL, size);
1765#endif
1766
1767 /* The stage-2 output mask is also applied for bypass */
1768 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
1769 smmu->s2_output_size = min((unsigned long)PHYS_MASK_SHIFT, size);
1770
1771 if (smmu->version == 1) {
1772 smmu->input_size = 32;
1773 } else {
1774#ifdef CONFIG_64BIT
1775 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon06f983d2013-11-05 15:55:04 +00001776 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001777#else
1778 size = 32;
1779#endif
1780 smmu->input_size = size;
1781
1782 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1783 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1784 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1785 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1786 PAGE_SIZE);
1787 return -ENODEV;
1788 }
1789 }
1790
1791 dev_notice(smmu->dev,
1792 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
1793 smmu->input_size, smmu->s1_output_size, smmu->s2_output_size);
1794 return 0;
1795}
1796
1797static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1798{
1799 struct resource *res;
1800 struct arm_smmu_device *smmu;
1801 struct device_node *dev_node;
1802 struct device *dev = &pdev->dev;
1803 struct rb_node *node;
1804 struct of_phandle_args masterspec;
1805 int num_irqs, i, err;
1806
1807 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1808 if (!smmu) {
1809 dev_err(dev, "failed to allocate arm_smmu_device\n");
1810 return -ENOMEM;
1811 }
1812 smmu->dev = dev;
1813
1814 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001815 smmu->base = devm_ioremap_resource(dev, res);
1816 if (IS_ERR(smmu->base))
1817 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001818 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001819
1820 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1821 &smmu->num_global_irqs)) {
1822 dev_err(dev, "missing #global-interrupts property\n");
1823 return -ENODEV;
1824 }
1825
1826 num_irqs = 0;
1827 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1828 num_irqs++;
1829 if (num_irqs > smmu->num_global_irqs)
1830 smmu->num_context_irqs++;
1831 }
1832
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001833 if (!smmu->num_context_irqs) {
1834 dev_err(dev, "found %d interrupts but expected at least %d\n",
1835 num_irqs, smmu->num_global_irqs + 1);
1836 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001837 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001838
1839 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1840 GFP_KERNEL);
1841 if (!smmu->irqs) {
1842 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1843 return -ENOMEM;
1844 }
1845
1846 for (i = 0; i < num_irqs; ++i) {
1847 int irq = platform_get_irq(pdev, i);
1848 if (irq < 0) {
1849 dev_err(dev, "failed to get irq index %d\n", i);
1850 return -ENODEV;
1851 }
1852 smmu->irqs[i] = irq;
1853 }
1854
1855 i = 0;
1856 smmu->masters = RB_ROOT;
1857 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1858 "#stream-id-cells", i,
1859 &masterspec)) {
1860 err = register_smmu_master(smmu, dev, &masterspec);
1861 if (err) {
1862 dev_err(dev, "failed to add master %s\n",
1863 masterspec.np->name);
1864 goto out_put_masters;
1865 }
1866
1867 i++;
1868 }
1869 dev_notice(dev, "registered %d master devices\n", i);
1870
1871 if ((dev_node = of_parse_phandle(dev->of_node, "smmu-parent", 0)))
1872 smmu->parent_of_node = dev_node;
1873
1874 err = arm_smmu_device_cfg_probe(smmu);
1875 if (err)
1876 goto out_put_parent;
1877
1878 if (smmu->version > 1 &&
1879 smmu->num_context_banks != smmu->num_context_irqs) {
1880 dev_err(dev,
1881 "found only %d context interrupt(s) but %d required\n",
1882 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001883 err = -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001884 goto out_put_parent;
1885 }
1886
Will Deacon45ae7cf2013-06-24 18:31:25 +01001887 for (i = 0; i < smmu->num_global_irqs; ++i) {
1888 err = request_irq(smmu->irqs[i],
1889 arm_smmu_global_fault,
1890 IRQF_SHARED,
1891 "arm-smmu global fault",
1892 smmu);
1893 if (err) {
1894 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1895 i, smmu->irqs[i]);
1896 goto out_free_irqs;
1897 }
1898 }
1899
1900 INIT_LIST_HEAD(&smmu->list);
1901 spin_lock(&arm_smmu_devices_lock);
1902 list_add(&smmu->list, &arm_smmu_devices);
1903 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001904
1905 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001906 return 0;
1907
1908out_free_irqs:
1909 while (i--)
1910 free_irq(smmu->irqs[i], smmu);
1911
1912out_put_parent:
1913 if (smmu->parent_of_node)
1914 of_node_put(smmu->parent_of_node);
1915
1916out_put_masters:
1917 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
1918 struct arm_smmu_master *master;
1919 master = container_of(node, struct arm_smmu_master, node);
1920 of_node_put(master->of_node);
1921 }
1922
1923 return err;
1924}
1925
1926static int arm_smmu_device_remove(struct platform_device *pdev)
1927{
1928 int i;
1929 struct device *dev = &pdev->dev;
1930 struct arm_smmu_device *curr, *smmu = NULL;
1931 struct rb_node *node;
1932
1933 spin_lock(&arm_smmu_devices_lock);
1934 list_for_each_entry(curr, &arm_smmu_devices, list) {
1935 if (curr->dev == dev) {
1936 smmu = curr;
1937 list_del(&smmu->list);
1938 break;
1939 }
1940 }
1941 spin_unlock(&arm_smmu_devices_lock);
1942
1943 if (!smmu)
1944 return -ENODEV;
1945
1946 if (smmu->parent_of_node)
1947 of_node_put(smmu->parent_of_node);
1948
1949 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
1950 struct arm_smmu_master *master;
1951 master = container_of(node, struct arm_smmu_master, node);
1952 of_node_put(master->of_node);
1953 }
1954
Will Deaconecfadb62013-07-31 19:21:28 +01001955 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001956 dev_err(dev, "removing device with active domains!\n");
1957
1958 for (i = 0; i < smmu->num_global_irqs; ++i)
1959 free_irq(smmu->irqs[i], smmu);
1960
1961 /* Turn the thing off */
Will Deacon25724842013-08-21 13:49:53 +01001962 writel_relaxed(sCR0_CLIENTPD, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001963 return 0;
1964}
1965
1966#ifdef CONFIG_OF
1967static struct of_device_id arm_smmu_of_match[] = {
1968 { .compatible = "arm,smmu-v1", },
1969 { .compatible = "arm,smmu-v2", },
1970 { .compatible = "arm,mmu-400", },
1971 { .compatible = "arm,mmu-500", },
1972 { },
1973};
1974MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1975#endif
1976
1977static struct platform_driver arm_smmu_driver = {
1978 .driver = {
1979 .owner = THIS_MODULE,
1980 .name = "arm-smmu",
1981 .of_match_table = of_match_ptr(arm_smmu_of_match),
1982 },
1983 .probe = arm_smmu_device_dt_probe,
1984 .remove = arm_smmu_device_remove,
1985};
1986
1987static int __init arm_smmu_init(void)
1988{
1989 int ret;
1990
1991 ret = platform_driver_register(&arm_smmu_driver);
1992 if (ret)
1993 return ret;
1994
1995 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001996 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001997 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1998
Dan Carpenter6614ee72013-08-21 09:34:20 +01001999 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002000 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2001
2002 return 0;
2003}
2004
2005static void __exit arm_smmu_exit(void)
2006{
2007 return platform_driver_unregister(&arm_smmu_driver);
2008}
2009
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002010subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002011module_exit(arm_smmu_exit);
2012
2013MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2014MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2015MODULE_LICENSE("GPL v2");