blob: 3ae50be49269d8daa77f0565ce59fd97cf7659ce [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>
Will Deacona9a1b0b2014-05-01 18:05:08 +010042#include <linux/pci.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010043#include <linux/platform_device.h>
44#include <linux/slab.h>
45#include <linux/spinlock.h>
46
47#include <linux/amba/bus.h>
48
49#include <asm/pgalloc.h>
50
51/* Maximum number of stream IDs assigned to a single device */
Andreas Herrmann636e97b2014-01-30 18:18:08 +000052#define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
Will Deacon45ae7cf2013-06-24 18:31:25 +010053
54/* Maximum number of context banks per SMMU */
55#define ARM_SMMU_MAX_CBS 128
56
57/* Maximum number of mapping groups per SMMU */
58#define ARM_SMMU_MAX_SMRS 128
59
Will Deacon45ae7cf2013-06-24 18:31:25 +010060/* SMMU global address space */
61#define ARM_SMMU_GR0(smmu) ((smmu)->base)
62#define ARM_SMMU_GR1(smmu) ((smmu)->base + (smmu)->pagesize)
63
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +000064/*
65 * SMMU global address space with conditional offset to access secure
66 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
67 * nsGFSYNR0: 0x450)
68 */
69#define ARM_SMMU_GR0_NS(smmu) \
70 ((smmu)->base + \
71 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
72 ? 0x400 : 0))
73
Will Deacon45ae7cf2013-06-24 18:31:25 +010074/* Page table bits */
Will Deaconcf2d45b2013-11-05 16:32:00 +000075#define ARM_SMMU_PTE_XN (((pteval_t)3) << 53)
Will Deacon45ae7cf2013-06-24 18:31:25 +010076#define ARM_SMMU_PTE_CONT (((pteval_t)1) << 52)
77#define ARM_SMMU_PTE_AF (((pteval_t)1) << 10)
78#define ARM_SMMU_PTE_SH_NS (((pteval_t)0) << 8)
79#define ARM_SMMU_PTE_SH_OS (((pteval_t)2) << 8)
80#define ARM_SMMU_PTE_SH_IS (((pteval_t)3) << 8)
Will Deaconcf2d45b2013-11-05 16:32:00 +000081#define ARM_SMMU_PTE_PAGE (((pteval_t)3) << 0)
Will Deacon45ae7cf2013-06-24 18:31:25 +010082
83#if PAGE_SIZE == SZ_4K
84#define ARM_SMMU_PTE_CONT_ENTRIES 16
85#elif PAGE_SIZE == SZ_64K
86#define ARM_SMMU_PTE_CONT_ENTRIES 32
87#else
88#define ARM_SMMU_PTE_CONT_ENTRIES 1
89#endif
90
91#define ARM_SMMU_PTE_CONT_SIZE (PAGE_SIZE * ARM_SMMU_PTE_CONT_ENTRIES)
92#define ARM_SMMU_PTE_CONT_MASK (~(ARM_SMMU_PTE_CONT_SIZE - 1))
Will Deacon45ae7cf2013-06-24 18:31:25 +010093
94/* Stage-1 PTE */
95#define ARM_SMMU_PTE_AP_UNPRIV (((pteval_t)1) << 6)
96#define ARM_SMMU_PTE_AP_RDONLY (((pteval_t)2) << 6)
97#define ARM_SMMU_PTE_ATTRINDX_SHIFT 2
Will Deacon1463fe42013-07-31 19:21:27 +010098#define ARM_SMMU_PTE_nG (((pteval_t)1) << 11)
Will Deacon45ae7cf2013-06-24 18:31:25 +010099
100/* Stage-2 PTE */
101#define ARM_SMMU_PTE_HAP_FAULT (((pteval_t)0) << 6)
102#define ARM_SMMU_PTE_HAP_READ (((pteval_t)1) << 6)
103#define ARM_SMMU_PTE_HAP_WRITE (((pteval_t)2) << 6)
104#define ARM_SMMU_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2)
105#define ARM_SMMU_PTE_MEMATTR_NC (((pteval_t)0x5) << 2)
106#define ARM_SMMU_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2)
107
108/* Configuration registers */
109#define ARM_SMMU_GR0_sCR0 0x0
110#define sCR0_CLIENTPD (1 << 0)
111#define sCR0_GFRE (1 << 1)
112#define sCR0_GFIE (1 << 2)
113#define sCR0_GCFGFRE (1 << 4)
114#define sCR0_GCFGFIE (1 << 5)
115#define sCR0_USFCFG (1 << 10)
116#define sCR0_VMIDPNE (1 << 11)
117#define sCR0_PTM (1 << 12)
118#define sCR0_FB (1 << 13)
119#define sCR0_BSU_SHIFT 14
120#define sCR0_BSU_MASK 0x3
121
122/* Identification registers */
123#define ARM_SMMU_GR0_ID0 0x20
124#define ARM_SMMU_GR0_ID1 0x24
125#define ARM_SMMU_GR0_ID2 0x28
126#define ARM_SMMU_GR0_ID3 0x2c
127#define ARM_SMMU_GR0_ID4 0x30
128#define ARM_SMMU_GR0_ID5 0x34
129#define ARM_SMMU_GR0_ID6 0x38
130#define ARM_SMMU_GR0_ID7 0x3c
131#define ARM_SMMU_GR0_sGFSR 0x48
132#define ARM_SMMU_GR0_sGFSYNR0 0x50
133#define ARM_SMMU_GR0_sGFSYNR1 0x54
134#define ARM_SMMU_GR0_sGFSYNR2 0x58
135#define ARM_SMMU_GR0_PIDR0 0xfe0
136#define ARM_SMMU_GR0_PIDR1 0xfe4
137#define ARM_SMMU_GR0_PIDR2 0xfe8
138
139#define ID0_S1TS (1 << 30)
140#define ID0_S2TS (1 << 29)
141#define ID0_NTS (1 << 28)
142#define ID0_SMS (1 << 27)
143#define ID0_PTFS_SHIFT 24
144#define ID0_PTFS_MASK 0x2
145#define ID0_PTFS_V8_ONLY 0x2
146#define ID0_CTTW (1 << 14)
147#define ID0_NUMIRPT_SHIFT 16
148#define ID0_NUMIRPT_MASK 0xff
149#define ID0_NUMSMRG_SHIFT 0
150#define ID0_NUMSMRG_MASK 0xff
151
152#define ID1_PAGESIZE (1 << 31)
153#define ID1_NUMPAGENDXB_SHIFT 28
154#define ID1_NUMPAGENDXB_MASK 7
155#define ID1_NUMS2CB_SHIFT 16
156#define ID1_NUMS2CB_MASK 0xff
157#define ID1_NUMCB_SHIFT 0
158#define ID1_NUMCB_MASK 0xff
159
160#define ID2_OAS_SHIFT 4
161#define ID2_OAS_MASK 0xf
162#define ID2_IAS_SHIFT 0
163#define ID2_IAS_MASK 0xf
164#define ID2_UBS_SHIFT 8
165#define ID2_UBS_MASK 0xf
166#define ID2_PTFS_4K (1 << 12)
167#define ID2_PTFS_16K (1 << 13)
168#define ID2_PTFS_64K (1 << 14)
169
170#define PIDR2_ARCH_SHIFT 4
171#define PIDR2_ARCH_MASK 0xf
172
173/* Global TLB invalidation */
174#define ARM_SMMU_GR0_STLBIALL 0x60
175#define ARM_SMMU_GR0_TLBIVMID 0x64
176#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
177#define ARM_SMMU_GR0_TLBIALLH 0x6c
178#define ARM_SMMU_GR0_sTLBGSYNC 0x70
179#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
180#define sTLBGSTATUS_GSACTIVE (1 << 0)
181#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
182
183/* Stream mapping registers */
184#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
185#define SMR_VALID (1 << 31)
186#define SMR_MASK_SHIFT 16
187#define SMR_MASK_MASK 0x7fff
188#define SMR_ID_SHIFT 0
189#define SMR_ID_MASK 0x7fff
190
191#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
192#define S2CR_CBNDX_SHIFT 0
193#define S2CR_CBNDX_MASK 0xff
194#define S2CR_TYPE_SHIFT 16
195#define S2CR_TYPE_MASK 0x3
196#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
197#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
198#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
199
200/* Context bank attribute registers */
201#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
202#define CBAR_VMID_SHIFT 0
203#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000204#define CBAR_S1_BPSHCFG_SHIFT 8
205#define CBAR_S1_BPSHCFG_MASK 3
206#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100207#define CBAR_S1_MEMATTR_SHIFT 12
208#define CBAR_S1_MEMATTR_MASK 0xf
209#define CBAR_S1_MEMATTR_WB 0xf
210#define CBAR_TYPE_SHIFT 16
211#define CBAR_TYPE_MASK 0x3
212#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
213#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
214#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
215#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
216#define CBAR_IRPTNDX_SHIFT 24
217#define CBAR_IRPTNDX_MASK 0xff
218
219#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
220#define CBA2R_RW64_32BIT (0 << 0)
221#define CBA2R_RW64_64BIT (1 << 0)
222
223/* Translation context bank */
224#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
225#define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize)
226
227#define ARM_SMMU_CB_SCTLR 0x0
228#define ARM_SMMU_CB_RESUME 0x8
229#define ARM_SMMU_CB_TTBCR2 0x10
230#define ARM_SMMU_CB_TTBR0_LO 0x20
231#define ARM_SMMU_CB_TTBR0_HI 0x24
232#define ARM_SMMU_CB_TTBCR 0x30
233#define ARM_SMMU_CB_S1_MAIR0 0x38
234#define ARM_SMMU_CB_FSR 0x58
235#define ARM_SMMU_CB_FAR_LO 0x60
236#define ARM_SMMU_CB_FAR_HI 0x64
237#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon1463fe42013-07-31 19:21:27 +0100238#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon45ae7cf2013-06-24 18:31:25 +0100239
240#define SCTLR_S1_ASIDPNE (1 << 12)
241#define SCTLR_CFCFG (1 << 7)
242#define SCTLR_CFIE (1 << 6)
243#define SCTLR_CFRE (1 << 5)
244#define SCTLR_E (1 << 4)
245#define SCTLR_AFE (1 << 2)
246#define SCTLR_TRE (1 << 1)
247#define SCTLR_M (1 << 0)
248#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
249
250#define RESUME_RETRY (0 << 0)
251#define RESUME_TERMINATE (1 << 0)
252
253#define TTBCR_EAE (1 << 31)
254
255#define TTBCR_PASIZE_SHIFT 16
256#define TTBCR_PASIZE_MASK 0x7
257
258#define TTBCR_TG0_4K (0 << 14)
259#define TTBCR_TG0_64K (1 << 14)
260
261#define TTBCR_SH0_SHIFT 12
262#define TTBCR_SH0_MASK 0x3
263#define TTBCR_SH_NS 0
264#define TTBCR_SH_OS 2
265#define TTBCR_SH_IS 3
266
267#define TTBCR_ORGN0_SHIFT 10
268#define TTBCR_IRGN0_SHIFT 8
269#define TTBCR_RGN_MASK 0x3
270#define TTBCR_RGN_NC 0
271#define TTBCR_RGN_WBWA 1
272#define TTBCR_RGN_WT 2
273#define TTBCR_RGN_WB 3
274
275#define TTBCR_SL0_SHIFT 6
276#define TTBCR_SL0_MASK 0x3
277#define TTBCR_SL0_LVL_2 0
278#define TTBCR_SL0_LVL_1 1
279
280#define TTBCR_T1SZ_SHIFT 16
281#define TTBCR_T0SZ_SHIFT 0
282#define TTBCR_SZ_MASK 0xf
283
284#define TTBCR2_SEP_SHIFT 15
285#define TTBCR2_SEP_MASK 0x7
286
287#define TTBCR2_PASIZE_SHIFT 0
288#define TTBCR2_PASIZE_MASK 0x7
289
290/* Common definitions for PASize and SEP fields */
291#define TTBCR2_ADDR_32 0
292#define TTBCR2_ADDR_36 1
293#define TTBCR2_ADDR_40 2
294#define TTBCR2_ADDR_42 3
295#define TTBCR2_ADDR_44 4
296#define TTBCR2_ADDR_48 5
297
Will Deacon1463fe42013-07-31 19:21:27 +0100298#define TTBRn_HI_ASID_SHIFT 16
299
Will Deacon45ae7cf2013-06-24 18:31:25 +0100300#define MAIR_ATTR_SHIFT(n) ((n) << 3)
301#define MAIR_ATTR_MASK 0xff
302#define MAIR_ATTR_DEVICE 0x04
303#define MAIR_ATTR_NC 0x44
304#define MAIR_ATTR_WBRWA 0xff
305#define MAIR_ATTR_IDX_NC 0
306#define MAIR_ATTR_IDX_CACHE 1
307#define MAIR_ATTR_IDX_DEV 2
308
309#define FSR_MULTI (1 << 31)
310#define FSR_SS (1 << 30)
311#define FSR_UUT (1 << 8)
312#define FSR_ASF (1 << 7)
313#define FSR_TLBLKF (1 << 6)
314#define FSR_TLBMCF (1 << 5)
315#define FSR_EF (1 << 4)
316#define FSR_PF (1 << 3)
317#define FSR_AFF (1 << 2)
318#define FSR_TF (1 << 1)
319
320#define FSR_IGN (FSR_AFF | FSR_ASF | FSR_TLBMCF | \
321 FSR_TLBLKF)
322#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100323 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100324
325#define FSYNR0_WNR (1 << 4)
326
327struct arm_smmu_smr {
328 u8 idx;
329 u16 mask;
330 u16 id;
331};
332
Will Deacona9a1b0b2014-05-01 18:05:08 +0100333struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100334 int num_streamids;
335 u16 streamids[MAX_MASTER_STREAMIDS];
336
337 /*
338 * We only need to allocate these on the root SMMU, as we
339 * configure unmatched streams to bypass translation.
340 */
341 struct arm_smmu_smr *smrs;
342};
343
Will Deacona9a1b0b2014-05-01 18:05:08 +0100344struct arm_smmu_master {
345 struct device_node *of_node;
346
347 /*
348 * The following is specific to the master's position in the
349 * SMMU chain.
350 */
351 struct rb_node node;
352 struct arm_smmu_master_cfg cfg;
353};
354
Will Deacon45ae7cf2013-06-24 18:31:25 +0100355struct arm_smmu_device {
356 struct device *dev;
357 struct device_node *parent_of_node;
358
359 void __iomem *base;
360 unsigned long size;
361 unsigned long pagesize;
362
363#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
364#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
365#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
366#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
367#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
368 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000369
370#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
371 u32 options;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100372 int version;
373
374 u32 num_context_banks;
375 u32 num_s2_context_banks;
376 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
377 atomic_t irptndx;
378
379 u32 num_mapping_groups;
380 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
381
382 unsigned long input_size;
383 unsigned long s1_output_size;
384 unsigned long s2_output_size;
385
386 u32 num_global_irqs;
387 u32 num_context_irqs;
388 unsigned int *irqs;
389
Will Deacon45ae7cf2013-06-24 18:31:25 +0100390 struct list_head list;
391 struct rb_root masters;
392};
393
394struct arm_smmu_cfg {
395 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100396 u8 cbndx;
397 u8 irptndx;
398 u32 cbar;
399 pgd_t *pgd;
400};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100401#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100402
Will Deaconecfadb62013-07-31 19:21:28 +0100403#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
404#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
405
Will Deacon45ae7cf2013-06-24 18:31:25 +0100406struct arm_smmu_domain {
407 /*
408 * A domain can span across multiple, chained SMMUs and requires
409 * all devices within the domain to follow the same translation
410 * path.
411 */
412 struct arm_smmu_device *leaf_smmu;
413 struct arm_smmu_cfg root_cfg;
414 phys_addr_t output_mask;
415
Will Deaconc9d09e22014-02-04 22:12:42 +0000416 spinlock_t lock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100417};
418
419static DEFINE_SPINLOCK(arm_smmu_devices_lock);
420static LIST_HEAD(arm_smmu_devices);
421
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000422struct arm_smmu_option_prop {
423 u32 opt;
424 const char *prop;
425};
426
427static struct arm_smmu_option_prop arm_smmu_options [] = {
428 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
429 { 0, NULL},
430};
431
432static void parse_driver_options(struct arm_smmu_device *smmu)
433{
434 int i = 0;
435 do {
436 if (of_property_read_bool(smmu->dev->of_node,
437 arm_smmu_options[i].prop)) {
438 smmu->options |= arm_smmu_options[i].opt;
439 dev_notice(smmu->dev, "option %s\n",
440 arm_smmu_options[i].prop);
441 }
442 } while (arm_smmu_options[++i].opt);
443}
444
Will Deacona9a1b0b2014-05-01 18:05:08 +0100445static struct device *dev_get_master_dev(struct device *dev)
446{
447 if (dev_is_pci(dev)) {
448 struct pci_bus *bus = to_pci_dev(dev)->bus;
449 while (!pci_is_root_bus(bus))
450 bus = bus->parent;
451 return bus->bridge->parent;
452 }
453
454 return dev;
455}
456
Will Deacon45ae7cf2013-06-24 18:31:25 +0100457static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
458 struct device_node *dev_node)
459{
460 struct rb_node *node = smmu->masters.rb_node;
461
462 while (node) {
463 struct arm_smmu_master *master;
464 master = container_of(node, struct arm_smmu_master, node);
465
466 if (dev_node < master->of_node)
467 node = node->rb_left;
468 else if (dev_node > master->of_node)
469 node = node->rb_right;
470 else
471 return master;
472 }
473
474 return NULL;
475}
476
Will Deacona9a1b0b2014-05-01 18:05:08 +0100477static struct arm_smmu_master_cfg *
478find_smmu_master_cfg(struct arm_smmu_device *smmu, struct device *dev)
479{
480 struct arm_smmu_master *master;
481
482 if (dev_is_pci(dev))
483 return dev->archdata.iommu;
484
485 master = find_smmu_master(smmu, dev->of_node);
486 return master ? &master->cfg : NULL;
487}
488
Will Deacon45ae7cf2013-06-24 18:31:25 +0100489static int insert_smmu_master(struct arm_smmu_device *smmu,
490 struct arm_smmu_master *master)
491{
492 struct rb_node **new, *parent;
493
494 new = &smmu->masters.rb_node;
495 parent = NULL;
496 while (*new) {
497 struct arm_smmu_master *this;
498 this = container_of(*new, struct arm_smmu_master, node);
499
500 parent = *new;
501 if (master->of_node < this->of_node)
502 new = &((*new)->rb_left);
503 else if (master->of_node > this->of_node)
504 new = &((*new)->rb_right);
505 else
506 return -EEXIST;
507 }
508
509 rb_link_node(&master->node, parent, new);
510 rb_insert_color(&master->node, &smmu->masters);
511 return 0;
512}
513
514static int register_smmu_master(struct arm_smmu_device *smmu,
515 struct device *dev,
516 struct of_phandle_args *masterspec)
517{
518 int i;
519 struct arm_smmu_master *master;
520
521 master = find_smmu_master(smmu, masterspec->np);
522 if (master) {
523 dev_err(dev,
524 "rejecting multiple registrations for master device %s\n",
525 masterspec->np->name);
526 return -EBUSY;
527 }
528
529 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
530 dev_err(dev,
531 "reached maximum number (%d) of stream IDs for master device %s\n",
532 MAX_MASTER_STREAMIDS, masterspec->np->name);
533 return -ENOSPC;
534 }
535
536 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
537 if (!master)
538 return -ENOMEM;
539
Will Deacona9a1b0b2014-05-01 18:05:08 +0100540 master->of_node = masterspec->np;
541 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100542
Will Deacona9a1b0b2014-05-01 18:05:08 +0100543 for (i = 0; i < master->cfg.num_streamids; ++i)
544 master->cfg.streamids[i] = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100545
546 return insert_smmu_master(smmu, master);
547}
548
549static struct arm_smmu_device *find_parent_smmu(struct arm_smmu_device *smmu)
550{
551 struct arm_smmu_device *parent;
552
553 if (!smmu->parent_of_node)
554 return NULL;
555
556 spin_lock(&arm_smmu_devices_lock);
557 list_for_each_entry(parent, &arm_smmu_devices, list)
558 if (parent->dev->of_node == smmu->parent_of_node)
559 goto out_unlock;
560
561 parent = NULL;
562 dev_warn(smmu->dev,
563 "Failed to find SMMU parent despite parent in DT\n");
564out_unlock:
565 spin_unlock(&arm_smmu_devices_lock);
566 return parent;
567}
568
Will Deacona9a1b0b2014-05-01 18:05:08 +0100569static struct arm_smmu_device *find_parent_smmu_for_device(struct device *dev)
570{
571 struct arm_smmu_device *child, *parent, *smmu;
572 struct arm_smmu_master *master = NULL;
573 struct device_node *dev_node = dev_get_master_dev(dev)->of_node;
574
575 spin_lock(&arm_smmu_devices_lock);
576 list_for_each_entry(parent, &arm_smmu_devices, list) {
577 smmu = parent;
578
579 /* Try to find a child of the current SMMU. */
580 list_for_each_entry(child, &arm_smmu_devices, list) {
581 if (child->parent_of_node == parent->dev->of_node) {
582 /* Does the child sit above our master? */
583 master = find_smmu_master(child, dev_node);
584 if (master) {
585 smmu = NULL;
586 break;
587 }
588 }
589 }
590
591 /* We found some children, so keep searching. */
592 if (!smmu) {
593 master = NULL;
594 continue;
595 }
596
597 master = find_smmu_master(smmu, dev_node);
598 if (master)
599 break;
600 }
601 spin_unlock(&arm_smmu_devices_lock);
602 return master ? smmu : NULL;
603}
604
Will Deacon45ae7cf2013-06-24 18:31:25 +0100605static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
606{
607 int idx;
608
609 do {
610 idx = find_next_zero_bit(map, end, start);
611 if (idx == end)
612 return -ENOSPC;
613 } while (test_and_set_bit(idx, map));
614
615 return idx;
616}
617
618static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
619{
620 clear_bit(idx, map);
621}
622
623/* Wait for any pending TLB invalidations to complete */
624static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
625{
626 int count = 0;
627 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
628
629 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
630 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
631 & sTLBGSTATUS_GSACTIVE) {
632 cpu_relax();
633 if (++count == TLB_LOOP_TIMEOUT) {
634 dev_err_ratelimited(smmu->dev,
635 "TLB sync timed out -- SMMU may be deadlocked\n");
636 return;
637 }
638 udelay(1);
639 }
640}
641
Will Deacon1463fe42013-07-31 19:21:27 +0100642static void arm_smmu_tlb_inv_context(struct arm_smmu_cfg *cfg)
643{
644 struct arm_smmu_device *smmu = cfg->smmu;
645 void __iomem *base = ARM_SMMU_GR0(smmu);
646 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
647
648 if (stage1) {
649 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100650 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
651 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100652 } else {
653 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100654 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
655 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100656 }
657
658 arm_smmu_tlb_sync(smmu);
659}
660
Will Deacon45ae7cf2013-06-24 18:31:25 +0100661static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
662{
663 int flags, ret;
664 u32 fsr, far, fsynr, resume;
665 unsigned long iova;
666 struct iommu_domain *domain = dev;
667 struct arm_smmu_domain *smmu_domain = domain->priv;
668 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
669 struct arm_smmu_device *smmu = root_cfg->smmu;
670 void __iomem *cb_base;
671
672 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
673 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
674
675 if (!(fsr & FSR_FAULT))
676 return IRQ_NONE;
677
678 if (fsr & FSR_IGN)
679 dev_err_ratelimited(smmu->dev,
680 "Unexpected context fault (fsr 0x%u)\n",
681 fsr);
682
683 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
684 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
685
686 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
687 iova = far;
688#ifdef CONFIG_64BIT
689 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
690 iova |= ((unsigned long)far << 32);
691#endif
692
693 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
694 ret = IRQ_HANDLED;
695 resume = RESUME_RETRY;
696 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100697 dev_err_ratelimited(smmu->dev,
698 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
699 iova, fsynr, root_cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100700 ret = IRQ_NONE;
701 resume = RESUME_TERMINATE;
702 }
703
704 /* Clear the faulting FSR */
705 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
706
707 /* Retry or terminate any stalled transactions */
708 if (fsr & FSR_SS)
709 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
710
711 return ret;
712}
713
714static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
715{
716 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
717 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000718 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100719
720 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
721 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
722 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
723 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
724
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000725 if (!gfsr)
726 return IRQ_NONE;
727
Will Deacon45ae7cf2013-06-24 18:31:25 +0100728 dev_err_ratelimited(smmu->dev,
729 "Unexpected global fault, this could be serious\n");
730 dev_err_ratelimited(smmu->dev,
731 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
732 gfsr, gfsynr0, gfsynr1, gfsynr2);
733
734 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100735 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100736}
737
Will Deacon6dd35f42014-02-05 17:49:34 +0000738static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
739 size_t size)
740{
741 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
742
743
744 /* Ensure new page tables are visible to the hardware walker */
745 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
Will Deacon3aa80ea2014-02-05 23:35:47 +0000746 dsb(ishst);
Will Deacon6dd35f42014-02-05 17:49:34 +0000747 } else {
748 /*
749 * If the SMMU can't walk tables in the CPU caches, treat them
750 * like non-coherent DMA since we need to flush the new entries
751 * all the way out to memory. There's no possibility of
752 * recursion here as the SMMU table walker will not be wired
753 * through another SMMU.
754 */
755 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
756 DMA_TO_DEVICE);
757 }
758}
759
Will Deacon45ae7cf2013-06-24 18:31:25 +0100760static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
761{
762 u32 reg;
763 bool stage1;
764 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
765 struct arm_smmu_device *smmu = root_cfg->smmu;
766 void __iomem *cb_base, *gr0_base, *gr1_base;
767
768 gr0_base = ARM_SMMU_GR0(smmu);
769 gr1_base = ARM_SMMU_GR1(smmu);
770 stage1 = root_cfg->cbar != CBAR_TYPE_S2_TRANS;
771 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
772
773 /* CBAR */
Will Deacon1463fe42013-07-31 19:21:27 +0100774 reg = root_cfg->cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100775 if (smmu->version == 1)
776 reg |= root_cfg->irptndx << CBAR_IRPTNDX_SHIFT;
777
Will Deacon57ca90f2014-02-06 14:59:05 +0000778 /*
779 * Use the weakest shareability/memory types, so they are
780 * overridden by the ttbcr/pte.
781 */
782 if (stage1) {
783 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
784 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
785 } else {
Will Deaconecfadb62013-07-31 19:21:28 +0100786 reg |= ARM_SMMU_CB_VMID(root_cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000787 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100788 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(root_cfg->cbndx));
789
790 if (smmu->version > 1) {
791 /* CBA2R */
792#ifdef CONFIG_64BIT
793 reg = CBA2R_RW64_64BIT;
794#else
795 reg = CBA2R_RW64_32BIT;
796#endif
797 writel_relaxed(reg,
798 gr1_base + ARM_SMMU_GR1_CBA2R(root_cfg->cbndx));
799
800 /* TTBCR2 */
801 switch (smmu->input_size) {
802 case 32:
803 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
804 break;
805 case 36:
806 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
807 break;
808 case 39:
809 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
810 break;
811 case 42:
812 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
813 break;
814 case 44:
815 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
816 break;
817 case 48:
818 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
819 break;
820 }
821
822 switch (smmu->s1_output_size) {
823 case 32:
824 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
825 break;
826 case 36:
827 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
828 break;
829 case 39:
830 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
831 break;
832 case 42:
833 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
834 break;
835 case 44:
836 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
837 break;
838 case 48:
839 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
840 break;
841 }
842
843 if (stage1)
844 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
845 }
846
847 /* TTBR0 */
Will Deacon6dd35f42014-02-05 17:49:34 +0000848 arm_smmu_flush_pgtable(smmu, root_cfg->pgd,
849 PTRS_PER_PGD * sizeof(pgd_t));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100850 reg = __pa(root_cfg->pgd);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100851 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
852 reg = (phys_addr_t)__pa(root_cfg->pgd) >> 32;
Will Deacon1463fe42013-07-31 19:21:27 +0100853 if (stage1)
Will Deaconecfadb62013-07-31 19:21:28 +0100854 reg |= ARM_SMMU_CB_ASID(root_cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100855 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100856
857 /*
858 * TTBCR
859 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
860 */
861 if (smmu->version > 1) {
862 if (PAGE_SIZE == SZ_4K)
863 reg = TTBCR_TG0_4K;
864 else
865 reg = TTBCR_TG0_64K;
866
867 if (!stage1) {
Will Deacona65217a2014-06-24 18:26:26 +0100868 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
869
Will Deacon45ae7cf2013-06-24 18:31:25 +0100870 switch (smmu->s2_output_size) {
871 case 32:
872 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
873 break;
874 case 36:
875 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
876 break;
877 case 40:
878 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
879 break;
880 case 42:
881 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
882 break;
883 case 44:
884 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
885 break;
886 case 48:
887 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
888 break;
889 }
890 } else {
Will Deacona65217a2014-06-24 18:26:26 +0100891 reg |= (64 - smmu->input_size) << TTBCR_T0SZ_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100892 }
893 } else {
894 reg = 0;
895 }
896
897 reg |= TTBCR_EAE |
898 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
899 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
900 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT) |
901 (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
902 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
903
904 /* MAIR0 (stage-1 only) */
905 if (stage1) {
906 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
907 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
908 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
909 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
910 }
911
Will Deacon45ae7cf2013-06-24 18:31:25 +0100912 /* SCTLR */
913 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
914 if (stage1)
915 reg |= SCTLR_S1_ASIDPNE;
916#ifdef __BIG_ENDIAN
917 reg |= SCTLR_E;
918#endif
Will Deacon25724842013-08-21 13:49:53 +0100919 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100920}
921
922static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +0100923 struct device *dev,
924 struct arm_smmu_device *device_smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100925{
926 int irq, ret, start;
927 struct arm_smmu_domain *smmu_domain = domain->priv;
928 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
929 struct arm_smmu_device *smmu, *parent;
930
931 /*
932 * Walk the SMMU chain to find the root device for this chain.
933 * We assume that no masters have translations which terminate
934 * early, and therefore check that the root SMMU does indeed have
935 * a StreamID for the master in question.
936 */
Will Deacona9a1b0b2014-05-01 18:05:08 +0100937 parent = device_smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100938 smmu_domain->output_mask = -1;
939 do {
940 smmu = parent;
941 smmu_domain->output_mask &= (1ULL << smmu->s2_output_size) - 1;
942 } while ((parent = find_parent_smmu(smmu)));
943
Will Deacona9a1b0b2014-05-01 18:05:08 +0100944 if (!find_smmu_master_cfg(smmu, dev)) {
945 dev_err(dev, "unable to find root SMMU config for device\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +0100946 return -ENODEV;
947 }
948
Will Deacon45ae7cf2013-06-24 18:31:25 +0100949 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
950 /*
951 * We will likely want to change this if/when KVM gets
952 * involved.
953 */
954 root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
955 start = smmu->num_s2_context_banks;
956 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) {
957 root_cfg->cbar = CBAR_TYPE_S2_TRANS;
958 start = 0;
959 } else {
960 root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
961 start = smmu->num_s2_context_banks;
962 }
963
964 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
965 smmu->num_context_banks);
966 if (IS_ERR_VALUE(ret))
Will Deaconecfadb62013-07-31 19:21:28 +0100967 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100968
969 root_cfg->cbndx = ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100970 if (smmu->version == 1) {
971 root_cfg->irptndx = atomic_inc_return(&smmu->irptndx);
972 root_cfg->irptndx %= smmu->num_context_irqs;
973 } else {
974 root_cfg->irptndx = root_cfg->cbndx;
975 }
976
977 irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
978 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
979 "arm-smmu-context-fault", domain);
980 if (IS_ERR_VALUE(ret)) {
981 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
982 root_cfg->irptndx, irq);
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100983 root_cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100984 goto out_free_context;
985 }
986
987 root_cfg->smmu = smmu;
988 arm_smmu_init_context_bank(smmu_domain);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100989 smmu_domain->leaf_smmu = device_smmu;
990 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100991
992out_free_context:
993 __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100994 return ret;
995}
996
997static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
998{
999 struct arm_smmu_domain *smmu_domain = domain->priv;
1000 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
1001 struct arm_smmu_device *smmu = root_cfg->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +01001002 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001003 int irq;
1004
1005 if (!smmu)
1006 return;
1007
Will Deacon1463fe42013-07-31 19:21:27 +01001008 /* Disable the context bank and nuke the TLB before freeing it. */
1009 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
1010 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1011 arm_smmu_tlb_inv_context(root_cfg);
1012
Dan Carpenterfaea13b72013-08-21 09:33:30 +01001013 if (root_cfg->irptndx != INVALID_IRPTNDX) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001014 irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
1015 free_irq(irq, domain);
1016 }
1017
Will Deacon45ae7cf2013-06-24 18:31:25 +01001018 __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
1019}
1020
1021static int arm_smmu_domain_init(struct iommu_domain *domain)
1022{
1023 struct arm_smmu_domain *smmu_domain;
1024 pgd_t *pgd;
1025
1026 /*
1027 * Allocate the domain and initialise some of its data structures.
1028 * We can't really do anything meaningful until we've added a
1029 * master.
1030 */
1031 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1032 if (!smmu_domain)
1033 return -ENOMEM;
1034
1035 pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
1036 if (!pgd)
1037 goto out_free_domain;
1038 smmu_domain->root_cfg.pgd = pgd;
1039
Will Deaconc9d09e22014-02-04 22:12:42 +00001040 spin_lock_init(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001041 domain->priv = smmu_domain;
1042 return 0;
1043
1044out_free_domain:
1045 kfree(smmu_domain);
1046 return -ENOMEM;
1047}
1048
1049static void arm_smmu_free_ptes(pmd_t *pmd)
1050{
1051 pgtable_t table = pmd_pgtable(*pmd);
1052 pgtable_page_dtor(table);
1053 __free_page(table);
1054}
1055
1056static void arm_smmu_free_pmds(pud_t *pud)
1057{
1058 int i;
1059 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
1060
1061 pmd = pmd_base;
1062 for (i = 0; i < PTRS_PER_PMD; ++i) {
1063 if (pmd_none(*pmd))
1064 continue;
1065
1066 arm_smmu_free_ptes(pmd);
1067 pmd++;
1068 }
1069
1070 pmd_free(NULL, pmd_base);
1071}
1072
1073static void arm_smmu_free_puds(pgd_t *pgd)
1074{
1075 int i;
1076 pud_t *pud, *pud_base = pud_offset(pgd, 0);
1077
1078 pud = pud_base;
1079 for (i = 0; i < PTRS_PER_PUD; ++i) {
1080 if (pud_none(*pud))
1081 continue;
1082
1083 arm_smmu_free_pmds(pud);
1084 pud++;
1085 }
1086
1087 pud_free(NULL, pud_base);
1088}
1089
1090static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
1091{
1092 int i;
1093 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
1094 pgd_t *pgd, *pgd_base = root_cfg->pgd;
1095
1096 /*
1097 * Recursively free the page tables for this domain. We don't
Will Deacon34fb4b32014-02-26 11:14:37 +00001098 * care about speculative TLB filling because the tables should
1099 * not be active in any context bank at this point (SCTLR.M is 0).
Will Deacon45ae7cf2013-06-24 18:31:25 +01001100 */
1101 pgd = pgd_base;
1102 for (i = 0; i < PTRS_PER_PGD; ++i) {
1103 if (pgd_none(*pgd))
1104 continue;
1105 arm_smmu_free_puds(pgd);
1106 pgd++;
1107 }
1108
1109 kfree(pgd_base);
1110}
1111
1112static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1113{
1114 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001115
1116 /*
1117 * Free the domain resources. We assume that all devices have
1118 * already been detached.
1119 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001120 arm_smmu_destroy_domain_context(domain);
1121 arm_smmu_free_pgtables(smmu_domain);
1122 kfree(smmu_domain);
1123}
1124
1125static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001126 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001127{
1128 int i;
1129 struct arm_smmu_smr *smrs;
1130 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1131
1132 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1133 return 0;
1134
Will Deacona9a1b0b2014-05-01 18:05:08 +01001135 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001136 return -EEXIST;
1137
Will Deacona9a1b0b2014-05-01 18:05:08 +01001138 smrs = kmalloc(sizeof(*smrs) * cfg->num_streamids, GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001139 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001140 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1141 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001142 return -ENOMEM;
1143 }
1144
1145 /* Allocate the SMRs on the root SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001146 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001147 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1148 smmu->num_mapping_groups);
1149 if (IS_ERR_VALUE(idx)) {
1150 dev_err(smmu->dev, "failed to allocate free SMR\n");
1151 goto err_free_smrs;
1152 }
1153
1154 smrs[i] = (struct arm_smmu_smr) {
1155 .idx = idx,
1156 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001157 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001158 };
1159 }
1160
1161 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001162 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001163 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1164 smrs[i].mask << SMR_MASK_SHIFT;
1165 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1166 }
1167
Will Deacona9a1b0b2014-05-01 18:05:08 +01001168 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001169 return 0;
1170
1171err_free_smrs:
1172 while (--i >= 0)
1173 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1174 kfree(smrs);
1175 return -ENOSPC;
1176}
1177
1178static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001179 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001180{
1181 int i;
1182 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001183 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001184
1185 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001186 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001187 u8 idx = smrs[i].idx;
1188 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1189 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1190 }
1191
Will Deacona9a1b0b2014-05-01 18:05:08 +01001192 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001193 kfree(smrs);
1194}
1195
1196static void arm_smmu_bypass_stream_mapping(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001197 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001198{
1199 int i;
1200 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1201
Will Deacona9a1b0b2014-05-01 18:05:08 +01001202 for (i = 0; i < cfg->num_streamids; ++i) {
1203 u16 sid = cfg->streamids[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +01001204 writel_relaxed(S2CR_TYPE_BYPASS,
1205 gr0_base + ARM_SMMU_GR0_S2CR(sid));
1206 }
1207}
1208
1209static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001210 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001211{
1212 int i, ret;
1213 struct arm_smmu_device *parent, *smmu = smmu_domain->root_cfg.smmu;
1214 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1215
Will Deacona9a1b0b2014-05-01 18:05:08 +01001216 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001217 if (ret)
1218 return ret;
1219
1220 /* Bypass the leaves */
1221 smmu = smmu_domain->leaf_smmu;
1222 while ((parent = find_parent_smmu(smmu))) {
1223 /*
1224 * We won't have a StreamID match for anything but the root
1225 * smmu, so we only need to worry about StreamID indexing,
1226 * where we must install bypass entries in the S2CRs.
1227 */
1228 if (smmu->features & ARM_SMMU_FEAT_STREAM_MATCH)
1229 continue;
1230
Will Deacona9a1b0b2014-05-01 18:05:08 +01001231 arm_smmu_bypass_stream_mapping(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001232 smmu = parent;
1233 }
1234
1235 /* Now we're at the root, time to point at our context bank */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001236 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001237 u32 idx, s2cr;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001238 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001239 s2cr = S2CR_TYPE_TRANS |
Will Deacon45ae7cf2013-06-24 18:31:25 +01001240 (smmu_domain->root_cfg.cbndx << S2CR_CBNDX_SHIFT);
1241 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1242 }
1243
1244 return 0;
1245}
1246
1247static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001248 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001249{
1250 struct arm_smmu_device *smmu = smmu_domain->root_cfg.smmu;
1251
1252 /*
1253 * We *must* clear the S2CR first, because freeing the SMR means
1254 * that it can be re-allocated immediately.
1255 */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001256 arm_smmu_bypass_stream_mapping(smmu, cfg);
1257 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001258}
1259
1260static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1261{
1262 int ret = -EINVAL;
1263 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001264 struct arm_smmu_device *device_smmu;
1265 struct arm_smmu_master_cfg *cfg;
Joerg Roedel972157c2014-02-20 12:19:08 +01001266 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001267
Will Deacona9a1b0b2014-05-01 18:05:08 +01001268 device_smmu = dev_get_master_dev(dev)->archdata.iommu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001269 if (!device_smmu) {
1270 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1271 return -ENXIO;
1272 }
1273
1274 /*
1275 * Sanity check the domain. We don't currently support domains
1276 * that cross between different SMMU chains.
1277 */
Joerg Roedel972157c2014-02-20 12:19:08 +01001278 spin_lock_irqsave(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001279 if (!smmu_domain->leaf_smmu) {
1280 /* Now that we have a master, we can finalise the domain */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001281 ret = arm_smmu_init_domain_context(domain, dev, device_smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001282 if (IS_ERR_VALUE(ret))
1283 goto err_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001284 } else if (smmu_domain->leaf_smmu != device_smmu) {
1285 dev_err(dev,
1286 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1287 dev_name(smmu_domain->leaf_smmu->dev),
1288 dev_name(device_smmu->dev));
1289 goto err_unlock;
1290 }
Joerg Roedel972157c2014-02-20 12:19:08 +01001291 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001292
1293 /* Looks ok, so add the device to the domain */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001294 cfg = find_smmu_master_cfg(smmu_domain->leaf_smmu, dev);
1295 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001296 return -ENODEV;
1297
Will Deacona9a1b0b2014-05-01 18:05:08 +01001298 return arm_smmu_domain_add_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001299
1300err_unlock:
Joerg Roedel972157c2014-02-20 12:19:08 +01001301 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001302 return ret;
1303}
1304
1305static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1306{
1307 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001308 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001309
Will Deacona9a1b0b2014-05-01 18:05:08 +01001310 cfg = find_smmu_master_cfg(smmu_domain->leaf_smmu, dev);
1311 if (cfg)
1312 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001313}
1314
Will Deacon45ae7cf2013-06-24 18:31:25 +01001315static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1316 unsigned long end)
1317{
1318 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1319 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1320}
1321
1322static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1323 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001324 unsigned long pfn, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001325{
1326 pte_t *pte, *start;
Will Deaconcf2d45b2013-11-05 16:32:00 +00001327 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001328
1329 if (pmd_none(*pmd)) {
1330 /* Allocate a new set of tables */
Will Deaconc9d09e22014-02-04 22:12:42 +00001331 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001332 if (!table)
1333 return -ENOMEM;
1334
Will Deacon6dd35f42014-02-05 17:49:34 +00001335 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
Kirill A. Shutemov01058e72013-11-14 14:31:49 -08001336 if (!pgtable_page_ctor(table)) {
1337 __free_page(table);
1338 return -ENOMEM;
1339 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001340 pmd_populate(NULL, pmd, table);
1341 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1342 }
1343
1344 if (stage == 1) {
Will Deacon1463fe42013-07-31 19:21:27 +01001345 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
Will Deaconb410aed2014-02-20 16:31:06 +00001346 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001347 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1348
Will Deaconb410aed2014-02-20 16:31:06 +00001349 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001350 pteval |= (MAIR_ATTR_IDX_CACHE <<
1351 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1352 } else {
1353 pteval |= ARM_SMMU_PTE_HAP_FAULT;
Will Deaconb410aed2014-02-20 16:31:06 +00001354 if (prot & IOMMU_READ)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001355 pteval |= ARM_SMMU_PTE_HAP_READ;
Will Deaconb410aed2014-02-20 16:31:06 +00001356 if (prot & IOMMU_WRITE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001357 pteval |= ARM_SMMU_PTE_HAP_WRITE;
Will Deaconb410aed2014-02-20 16:31:06 +00001358 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001359 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1360 else
1361 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1362 }
1363
1364 /* If no access, create a faulting entry to avoid TLB fills */
Will Deaconb410aed2014-02-20 16:31:06 +00001365 if (prot & IOMMU_EXEC)
Will Deaconcf2d45b2013-11-05 16:32:00 +00001366 pteval &= ~ARM_SMMU_PTE_XN;
Will Deaconb410aed2014-02-20 16:31:06 +00001367 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001368 pteval &= ~ARM_SMMU_PTE_PAGE;
1369
1370 pteval |= ARM_SMMU_PTE_SH_IS;
1371 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1372 pte = start;
1373
1374 /*
1375 * Install the page table entries. This is fairly complicated
1376 * since we attempt to make use of the contiguous hint in the
1377 * ptes where possible. The contiguous hint indicates a series
1378 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1379 * contiguous region with the following constraints:
1380 *
1381 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1382 * - Each pte in the region has the contiguous hint bit set
1383 *
1384 * This complicates unmapping (also handled by this code, when
1385 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1386 * possible, yet highly unlikely, that a client may unmap only
1387 * part of a contiguous range. This requires clearing of the
1388 * contiguous hint bits in the range before installing the new
1389 * faulting entries.
1390 *
1391 * Note that re-mapping an address range without first unmapping
1392 * it is not supported, so TLB invalidation is not required here
1393 * and is instead performed at unmap and domain-init time.
1394 */
1395 do {
1396 int i = 1;
1397 pteval &= ~ARM_SMMU_PTE_CONT;
1398
1399 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1400 i = ARM_SMMU_PTE_CONT_ENTRIES;
1401 pteval |= ARM_SMMU_PTE_CONT;
1402 } else if (pte_val(*pte) &
1403 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1404 int j;
1405 pte_t *cont_start;
1406 unsigned long idx = pte_index(addr);
1407
1408 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1409 cont_start = pmd_page_vaddr(*pmd) + idx;
1410 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
1411 pte_val(*(cont_start + j)) &= ~ARM_SMMU_PTE_CONT;
1412
1413 arm_smmu_flush_pgtable(smmu, cont_start,
1414 sizeof(*pte) *
1415 ARM_SMMU_PTE_CONT_ENTRIES);
1416 }
1417
1418 do {
1419 *pte = pfn_pte(pfn, __pgprot(pteval));
1420 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1421 } while (addr != end);
1422
1423 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1424 return 0;
1425}
1426
1427static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1428 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001429 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001430{
1431 int ret;
1432 pmd_t *pmd;
1433 unsigned long next, pfn = __phys_to_pfn(phys);
1434
1435#ifndef __PAGETABLE_PMD_FOLDED
1436 if (pud_none(*pud)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001437 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001438 if (!pmd)
1439 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001440
Will Deacon6dd35f42014-02-05 17:49:34 +00001441 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001442 pud_populate(NULL, pud, pmd);
1443 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1444
1445 pmd += pmd_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001446 } else
1447#endif
1448 pmd = pmd_offset(pud, addr);
1449
1450 do {
1451 next = pmd_addr_end(addr, end);
Bin Wangaca1bc42014-03-21 10:06:07 +00001452 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn,
Will Deaconb410aed2014-02-20 16:31:06 +00001453 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001454 phys += next - addr;
1455 } while (pmd++, addr = next, addr < end);
1456
1457 return ret;
1458}
1459
1460static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1461 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001462 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001463{
1464 int ret = 0;
1465 pud_t *pud;
1466 unsigned long next;
1467
1468#ifndef __PAGETABLE_PUD_FOLDED
1469 if (pgd_none(*pgd)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001470 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001471 if (!pud)
1472 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001473
Will Deacon6dd35f42014-02-05 17:49:34 +00001474 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001475 pgd_populate(NULL, pgd, pud);
1476 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1477
1478 pud += pud_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001479 } else
1480#endif
1481 pud = pud_offset(pgd, addr);
1482
1483 do {
1484 next = pud_addr_end(addr, end);
1485 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
Will Deaconb410aed2014-02-20 16:31:06 +00001486 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001487 phys += next - addr;
1488 } while (pud++, addr = next, addr < end);
1489
1490 return ret;
1491}
1492
1493static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1494 unsigned long iova, phys_addr_t paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001495 size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001496{
1497 int ret, stage;
1498 unsigned long end;
1499 phys_addr_t input_mask, output_mask;
1500 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
1501 pgd_t *pgd = root_cfg->pgd;
1502 struct arm_smmu_device *smmu = root_cfg->smmu;
Will Deaconb410aed2014-02-20 16:31:06 +00001503 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001504
1505 if (root_cfg->cbar == CBAR_TYPE_S2_TRANS) {
1506 stage = 2;
1507 output_mask = (1ULL << smmu->s2_output_size) - 1;
1508 } else {
1509 stage = 1;
1510 output_mask = (1ULL << smmu->s1_output_size) - 1;
1511 }
1512
1513 if (!pgd)
1514 return -EINVAL;
1515
1516 if (size & ~PAGE_MASK)
1517 return -EINVAL;
1518
1519 input_mask = (1ULL << smmu->input_size) - 1;
1520 if ((phys_addr_t)iova & ~input_mask)
1521 return -ERANGE;
1522
1523 if (paddr & ~output_mask)
1524 return -ERANGE;
1525
Will Deaconb410aed2014-02-20 16:31:06 +00001526 spin_lock_irqsave(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001527 pgd += pgd_index(iova);
1528 end = iova + size;
1529 do {
1530 unsigned long next = pgd_addr_end(iova, end);
1531
1532 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001533 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001534 if (ret)
1535 goto out_unlock;
1536
1537 paddr += next - iova;
1538 iova = next;
1539 } while (pgd++, iova != end);
1540
1541out_unlock:
Will Deaconb410aed2014-02-20 16:31:06 +00001542 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001543
Will Deacon45ae7cf2013-06-24 18:31:25 +01001544 return ret;
1545}
1546
1547static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001548 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001549{
1550 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001551
Will Deacon5552ecd2013-11-08 15:08:06 +00001552 if (!smmu_domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001553 return -ENODEV;
1554
1555 /* Check for silent address truncation up the SMMU chain. */
1556 if ((phys_addr_t)iova & ~smmu_domain->output_mask)
1557 return -ERANGE;
1558
Will Deaconb410aed2014-02-20 16:31:06 +00001559 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, prot);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001560}
1561
1562static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1563 size_t size)
1564{
1565 int ret;
1566 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001567
1568 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
Will Deacon1463fe42013-07-31 19:21:27 +01001569 arm_smmu_tlb_inv_context(&smmu_domain->root_cfg);
Laurent Pinchart16c50dcf2014-02-28 15:37:10 +00001570 return ret ? 0 : size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001571}
1572
1573static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1574 dma_addr_t iova)
1575{
Will Deacona44a9792013-11-07 18:47:50 +00001576 pgd_t *pgdp, pgd;
1577 pud_t pud;
1578 pmd_t pmd;
1579 pte_t pte;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001580 struct arm_smmu_domain *smmu_domain = domain->priv;
1581 struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001582
Will Deacona44a9792013-11-07 18:47:50 +00001583 pgdp = root_cfg->pgd;
1584 if (!pgdp)
1585 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001586
Will Deacona44a9792013-11-07 18:47:50 +00001587 pgd = *(pgdp + pgd_index(iova));
1588 if (pgd_none(pgd))
1589 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001590
Will Deacona44a9792013-11-07 18:47:50 +00001591 pud = *pud_offset(&pgd, iova);
1592 if (pud_none(pud))
1593 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001594
Will Deacona44a9792013-11-07 18:47:50 +00001595 pmd = *pmd_offset(&pud, iova);
1596 if (pmd_none(pmd))
1597 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001598
Will Deacona44a9792013-11-07 18:47:50 +00001599 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001600 if (pte_none(pte))
Will Deacona44a9792013-11-07 18:47:50 +00001601 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001602
Will Deacona44a9792013-11-07 18:47:50 +00001603 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001604}
1605
1606static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1607 unsigned long cap)
1608{
Will Deacon45ae7cf2013-06-24 18:31:25 +01001609 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacond0948942014-06-24 17:30:10 +01001610 u32 features = smmu_domain->root_cfg.smmu->features;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001611
Will Deacond0948942014-06-24 17:30:10 +01001612 switch (cap) {
1613 case IOMMU_CAP_CACHE_COHERENCY:
1614 return features & ARM_SMMU_FEAT_COHERENT_WALK;
1615 case IOMMU_CAP_INTR_REMAP:
1616 return 1; /* MSIs are just memory writes */
1617 default:
1618 return 0;
1619 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001620}
1621
Will Deacona9a1b0b2014-05-01 18:05:08 +01001622static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1623{
1624 *((u16 *)data) = alias;
1625 return 0; /* Continue walking */
1626}
1627
Will Deacon45ae7cf2013-06-24 18:31:25 +01001628static int arm_smmu_add_device(struct device *dev)
1629{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001630 struct arm_smmu_device *smmu;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001631 struct iommu_group *group;
1632 int ret;
1633
1634 if (dev->archdata.iommu) {
1635 dev_warn(dev, "IOMMU driver already assigned to device\n");
1636 return -EINVAL;
1637 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001638
Will Deacona9a1b0b2014-05-01 18:05:08 +01001639 smmu = find_parent_smmu_for_device(dev);
1640 if (!smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001641 return -ENODEV;
1642
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001643 group = iommu_group_alloc();
1644 if (IS_ERR(group)) {
1645 dev_err(dev, "Failed to allocate IOMMU group\n");
1646 return PTR_ERR(group);
1647 }
1648
Will Deacona9a1b0b2014-05-01 18:05:08 +01001649 if (dev_is_pci(dev)) {
1650 struct arm_smmu_master_cfg *cfg;
1651 struct pci_dev *pdev = to_pci_dev(dev);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001652
Will Deacona9a1b0b2014-05-01 18:05:08 +01001653 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1654 if (!cfg) {
1655 ret = -ENOMEM;
1656 goto out_put_group;
1657 }
1658
1659 cfg->num_streamids = 1;
1660 /*
1661 * Assume Stream ID == Requester ID for now.
1662 * We need a way to describe the ID mappings in FDT.
1663 */
1664 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1665 &cfg->streamids[0]);
1666 dev->archdata.iommu = cfg;
1667 } else {
1668 dev->archdata.iommu = smmu;
1669 }
1670
1671 ret = iommu_group_add_device(group, dev);
1672
1673out_put_group:
1674 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001675 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001676}
1677
1678static void arm_smmu_remove_device(struct device *dev)
1679{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001680 if (dev_is_pci(dev))
1681 kfree(dev->archdata.iommu);
1682
Will Deacon45ae7cf2013-06-24 18:31:25 +01001683 dev->archdata.iommu = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001684 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001685}
1686
1687static struct iommu_ops arm_smmu_ops = {
1688 .domain_init = arm_smmu_domain_init,
1689 .domain_destroy = arm_smmu_domain_destroy,
1690 .attach_dev = arm_smmu_attach_dev,
1691 .detach_dev = arm_smmu_detach_dev,
1692 .map = arm_smmu_map,
1693 .unmap = arm_smmu_unmap,
1694 .iova_to_phys = arm_smmu_iova_to_phys,
1695 .domain_has_cap = arm_smmu_domain_has_cap,
1696 .add_device = arm_smmu_add_device,
1697 .remove_device = arm_smmu_remove_device,
1698 .pgsize_bitmap = (SECTION_SIZE |
1699 ARM_SMMU_PTE_CONT_SIZE |
1700 PAGE_SIZE),
1701};
1702
1703static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1704{
1705 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001706 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001707 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001708 u32 reg;
1709
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001710 /* clear global FSR */
1711 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1712 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001713
1714 /* Mark all SMRn as invalid and all S2CRn as bypass */
1715 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1716 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(i));
1717 writel_relaxed(S2CR_TYPE_BYPASS, gr0_base + ARM_SMMU_GR0_S2CR(i));
1718 }
1719
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001720 /* Make sure all context banks are disabled and clear CB_FSR */
1721 for (i = 0; i < smmu->num_context_banks; ++i) {
1722 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1723 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1724 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1725 }
Will Deacon1463fe42013-07-31 19:21:27 +01001726
Will Deacon45ae7cf2013-06-24 18:31:25 +01001727 /* Invalidate the TLB, just in case */
1728 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1729 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1730 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1731
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001732 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001733
Will Deacon45ae7cf2013-06-24 18:31:25 +01001734 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001735 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001736
1737 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001738 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001739
1740 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001741 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001742
1743 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001744 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001745
1746 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001747 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001748
1749 /* Push the button */
1750 arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001751 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001752}
1753
1754static int arm_smmu_id_size_to_bits(int size)
1755{
1756 switch (size) {
1757 case 0:
1758 return 32;
1759 case 1:
1760 return 36;
1761 case 2:
1762 return 40;
1763 case 3:
1764 return 42;
1765 case 4:
1766 return 44;
1767 case 5:
1768 default:
1769 return 48;
1770 }
1771}
1772
1773static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1774{
1775 unsigned long size;
1776 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1777 u32 id;
1778
1779 dev_notice(smmu->dev, "probing hardware configuration...\n");
1780
1781 /* Primecell ID */
1782 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1783 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1784 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1785
1786 /* ID0 */
1787 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1788#ifndef CONFIG_64BIT
1789 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1790 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1791 return -ENODEV;
1792 }
1793#endif
1794 if (id & ID0_S1TS) {
1795 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1796 dev_notice(smmu->dev, "\tstage 1 translation\n");
1797 }
1798
1799 if (id & ID0_S2TS) {
1800 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1801 dev_notice(smmu->dev, "\tstage 2 translation\n");
1802 }
1803
1804 if (id & ID0_NTS) {
1805 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1806 dev_notice(smmu->dev, "\tnested translation\n");
1807 }
1808
1809 if (!(smmu->features &
1810 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 |
1811 ARM_SMMU_FEAT_TRANS_NESTED))) {
1812 dev_err(smmu->dev, "\tno translation support!\n");
1813 return -ENODEV;
1814 }
1815
1816 if (id & ID0_CTTW) {
1817 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1818 dev_notice(smmu->dev, "\tcoherent table walk\n");
1819 }
1820
1821 if (id & ID0_SMS) {
1822 u32 smr, sid, mask;
1823
1824 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1825 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1826 ID0_NUMSMRG_MASK;
1827 if (smmu->num_mapping_groups == 0) {
1828 dev_err(smmu->dev,
1829 "stream-matching supported, but no SMRs present!\n");
1830 return -ENODEV;
1831 }
1832
1833 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1834 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1835 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1836 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1837
1838 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1839 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1840 if ((mask & sid) != sid) {
1841 dev_err(smmu->dev,
1842 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1843 mask, sid);
1844 return -ENODEV;
1845 }
1846
1847 dev_notice(smmu->dev,
1848 "\tstream matching with %u register groups, mask 0x%x",
1849 smmu->num_mapping_groups, mask);
1850 }
1851
1852 /* ID1 */
1853 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1854 smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
1855
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001856 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001857 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
1858 size *= (smmu->pagesize << 1);
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001859 if (smmu->size != size)
1860 dev_warn(smmu->dev, "SMMU address space size (0x%lx) differs "
1861 "from mapped region size (0x%lx)!\n", size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001862
1863 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1864 ID1_NUMS2CB_MASK;
1865 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1866 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1867 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1868 return -ENODEV;
1869 }
1870 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1871 smmu->num_context_banks, smmu->num_s2_context_banks);
1872
1873 /* ID2 */
1874 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1875 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1876
1877 /*
1878 * Stage-1 output limited by stage-2 input size due to pgd
1879 * allocation (PTRS_PER_PGD).
1880 */
1881#ifdef CONFIG_64BIT
Will Deaconc4430842014-03-13 11:46:57 +00001882 smmu->s1_output_size = min((unsigned long)VA_BITS, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001883#else
1884 smmu->s1_output_size = min(32UL, size);
1885#endif
1886
1887 /* The stage-2 output mask is also applied for bypass */
1888 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
1889 smmu->s2_output_size = min((unsigned long)PHYS_MASK_SHIFT, size);
1890
1891 if (smmu->version == 1) {
1892 smmu->input_size = 32;
1893 } else {
1894#ifdef CONFIG_64BIT
1895 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon06f983d2013-11-05 15:55:04 +00001896 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001897#else
1898 size = 32;
1899#endif
1900 smmu->input_size = size;
1901
1902 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1903 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1904 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1905 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1906 PAGE_SIZE);
1907 return -ENODEV;
1908 }
1909 }
1910
1911 dev_notice(smmu->dev,
1912 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
1913 smmu->input_size, smmu->s1_output_size, smmu->s2_output_size);
1914 return 0;
1915}
1916
1917static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1918{
1919 struct resource *res;
1920 struct arm_smmu_device *smmu;
1921 struct device_node *dev_node;
1922 struct device *dev = &pdev->dev;
1923 struct rb_node *node;
1924 struct of_phandle_args masterspec;
1925 int num_irqs, i, err;
1926
1927 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1928 if (!smmu) {
1929 dev_err(dev, "failed to allocate arm_smmu_device\n");
1930 return -ENOMEM;
1931 }
1932 smmu->dev = dev;
1933
1934 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001935 smmu->base = devm_ioremap_resource(dev, res);
1936 if (IS_ERR(smmu->base))
1937 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001938 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001939
1940 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1941 &smmu->num_global_irqs)) {
1942 dev_err(dev, "missing #global-interrupts property\n");
1943 return -ENODEV;
1944 }
1945
1946 num_irqs = 0;
1947 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1948 num_irqs++;
1949 if (num_irqs > smmu->num_global_irqs)
1950 smmu->num_context_irqs++;
1951 }
1952
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001953 if (!smmu->num_context_irqs) {
1954 dev_err(dev, "found %d interrupts but expected at least %d\n",
1955 num_irqs, smmu->num_global_irqs + 1);
1956 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001957 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001958
1959 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1960 GFP_KERNEL);
1961 if (!smmu->irqs) {
1962 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1963 return -ENOMEM;
1964 }
1965
1966 for (i = 0; i < num_irqs; ++i) {
1967 int irq = platform_get_irq(pdev, i);
1968 if (irq < 0) {
1969 dev_err(dev, "failed to get irq index %d\n", i);
1970 return -ENODEV;
1971 }
1972 smmu->irqs[i] = irq;
1973 }
1974
1975 i = 0;
1976 smmu->masters = RB_ROOT;
1977 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1978 "#stream-id-cells", i,
1979 &masterspec)) {
1980 err = register_smmu_master(smmu, dev, &masterspec);
1981 if (err) {
1982 dev_err(dev, "failed to add master %s\n",
1983 masterspec.np->name);
1984 goto out_put_masters;
1985 }
1986
1987 i++;
1988 }
1989 dev_notice(dev, "registered %d master devices\n", i);
1990
1991 if ((dev_node = of_parse_phandle(dev->of_node, "smmu-parent", 0)))
1992 smmu->parent_of_node = dev_node;
1993
1994 err = arm_smmu_device_cfg_probe(smmu);
1995 if (err)
1996 goto out_put_parent;
1997
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001998 parse_driver_options(smmu);
1999
Will Deacon45ae7cf2013-06-24 18:31:25 +01002000 if (smmu->version > 1 &&
2001 smmu->num_context_banks != smmu->num_context_irqs) {
2002 dev_err(dev,
2003 "found only %d context interrupt(s) but %d required\n",
2004 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00002005 err = -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002006 goto out_put_parent;
2007 }
2008
Will Deacon45ae7cf2013-06-24 18:31:25 +01002009 for (i = 0; i < smmu->num_global_irqs; ++i) {
2010 err = request_irq(smmu->irqs[i],
2011 arm_smmu_global_fault,
2012 IRQF_SHARED,
2013 "arm-smmu global fault",
2014 smmu);
2015 if (err) {
2016 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2017 i, smmu->irqs[i]);
2018 goto out_free_irqs;
2019 }
2020 }
2021
2022 INIT_LIST_HEAD(&smmu->list);
2023 spin_lock(&arm_smmu_devices_lock);
2024 list_add(&smmu->list, &arm_smmu_devices);
2025 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01002026
2027 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002028 return 0;
2029
2030out_free_irqs:
2031 while (i--)
2032 free_irq(smmu->irqs[i], smmu);
2033
2034out_put_parent:
2035 if (smmu->parent_of_node)
2036 of_node_put(smmu->parent_of_node);
2037
2038out_put_masters:
2039 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
2040 struct arm_smmu_master *master;
2041 master = container_of(node, struct arm_smmu_master, node);
2042 of_node_put(master->of_node);
2043 }
2044
2045 return err;
2046}
2047
2048static int arm_smmu_device_remove(struct platform_device *pdev)
2049{
2050 int i;
2051 struct device *dev = &pdev->dev;
2052 struct arm_smmu_device *curr, *smmu = NULL;
2053 struct rb_node *node;
2054
2055 spin_lock(&arm_smmu_devices_lock);
2056 list_for_each_entry(curr, &arm_smmu_devices, list) {
2057 if (curr->dev == dev) {
2058 smmu = curr;
2059 list_del(&smmu->list);
2060 break;
2061 }
2062 }
2063 spin_unlock(&arm_smmu_devices_lock);
2064
2065 if (!smmu)
2066 return -ENODEV;
2067
2068 if (smmu->parent_of_node)
2069 of_node_put(smmu->parent_of_node);
2070
2071 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
2072 struct arm_smmu_master *master;
2073 master = container_of(node, struct arm_smmu_master, node);
2074 of_node_put(master->of_node);
2075 }
2076
Will Deaconecfadb62013-07-31 19:21:28 +01002077 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002078 dev_err(dev, "removing device with active domains!\n");
2079
2080 for (i = 0; i < smmu->num_global_irqs; ++i)
2081 free_irq(smmu->irqs[i], smmu);
2082
2083 /* Turn the thing off */
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00002084 writel(sCR0_CLIENTPD,ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002085 return 0;
2086}
2087
2088#ifdef CONFIG_OF
2089static struct of_device_id arm_smmu_of_match[] = {
2090 { .compatible = "arm,smmu-v1", },
2091 { .compatible = "arm,smmu-v2", },
2092 { .compatible = "arm,mmu-400", },
2093 { .compatible = "arm,mmu-500", },
2094 { },
2095};
2096MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2097#endif
2098
2099static struct platform_driver arm_smmu_driver = {
2100 .driver = {
2101 .owner = THIS_MODULE,
2102 .name = "arm-smmu",
2103 .of_match_table = of_match_ptr(arm_smmu_of_match),
2104 },
2105 .probe = arm_smmu_device_dt_probe,
2106 .remove = arm_smmu_device_remove,
2107};
2108
2109static int __init arm_smmu_init(void)
2110{
2111 int ret;
2112
2113 ret = platform_driver_register(&arm_smmu_driver);
2114 if (ret)
2115 return ret;
2116
2117 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01002118 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002119 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2120
Will Deacond123cf82014-02-04 22:17:53 +00002121#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01002122 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002123 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00002124#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01002125
Will Deacona9a1b0b2014-05-01 18:05:08 +01002126#ifdef CONFIG_PCI
2127 if (!iommu_present(&pci_bus_type))
2128 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2129#endif
2130
Will Deacon45ae7cf2013-06-24 18:31:25 +01002131 return 0;
2132}
2133
2134static void __exit arm_smmu_exit(void)
2135{
2136 return platform_driver_unregister(&arm_smmu_driver);
2137}
2138
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002139subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002140module_exit(arm_smmu_exit);
2141
2142MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2143MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2144MODULE_LICENSE("GPL v2");