blob: a83cc2a2a2cab5464bd7b4f85d514b00afe580f2 [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
Olav Haugan3c8766d2014-08-22 17:12:32 -0700149#define ID0_NUMSIDB_SHIFT 9
150#define ID0_NUMSIDB_MASK 0xf
Will Deacon45ae7cf2013-06-24 18:31:25 +0100151#define ID0_NUMSMRG_SHIFT 0
152#define ID0_NUMSMRG_MASK 0xff
153
154#define ID1_PAGESIZE (1 << 31)
155#define ID1_NUMPAGENDXB_SHIFT 28
156#define ID1_NUMPAGENDXB_MASK 7
157#define ID1_NUMS2CB_SHIFT 16
158#define ID1_NUMS2CB_MASK 0xff
159#define ID1_NUMCB_SHIFT 0
160#define ID1_NUMCB_MASK 0xff
161
162#define ID2_OAS_SHIFT 4
163#define ID2_OAS_MASK 0xf
164#define ID2_IAS_SHIFT 0
165#define ID2_IAS_MASK 0xf
166#define ID2_UBS_SHIFT 8
167#define ID2_UBS_MASK 0xf
168#define ID2_PTFS_4K (1 << 12)
169#define ID2_PTFS_16K (1 << 13)
170#define ID2_PTFS_64K (1 << 14)
171
172#define PIDR2_ARCH_SHIFT 4
173#define PIDR2_ARCH_MASK 0xf
174
175/* Global TLB invalidation */
176#define ARM_SMMU_GR0_STLBIALL 0x60
177#define ARM_SMMU_GR0_TLBIVMID 0x64
178#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
179#define ARM_SMMU_GR0_TLBIALLH 0x6c
180#define ARM_SMMU_GR0_sTLBGSYNC 0x70
181#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
182#define sTLBGSTATUS_GSACTIVE (1 << 0)
183#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
184
185/* Stream mapping registers */
186#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
187#define SMR_VALID (1 << 31)
188#define SMR_MASK_SHIFT 16
189#define SMR_MASK_MASK 0x7fff
190#define SMR_ID_SHIFT 0
191#define SMR_ID_MASK 0x7fff
192
193#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
194#define S2CR_CBNDX_SHIFT 0
195#define S2CR_CBNDX_MASK 0xff
196#define S2CR_TYPE_SHIFT 16
197#define S2CR_TYPE_MASK 0x3
198#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
199#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
200#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
201
202/* Context bank attribute registers */
203#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
204#define CBAR_VMID_SHIFT 0
205#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000206#define CBAR_S1_BPSHCFG_SHIFT 8
207#define CBAR_S1_BPSHCFG_MASK 3
208#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100209#define CBAR_S1_MEMATTR_SHIFT 12
210#define CBAR_S1_MEMATTR_MASK 0xf
211#define CBAR_S1_MEMATTR_WB 0xf
212#define CBAR_TYPE_SHIFT 16
213#define CBAR_TYPE_MASK 0x3
214#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
215#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
216#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
217#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
218#define CBAR_IRPTNDX_SHIFT 24
219#define CBAR_IRPTNDX_MASK 0xff
220
221#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
222#define CBA2R_RW64_32BIT (0 << 0)
223#define CBA2R_RW64_64BIT (1 << 0)
224
225/* Translation context bank */
226#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
227#define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize)
228
229#define ARM_SMMU_CB_SCTLR 0x0
230#define ARM_SMMU_CB_RESUME 0x8
231#define ARM_SMMU_CB_TTBCR2 0x10
232#define ARM_SMMU_CB_TTBR0_LO 0x20
233#define ARM_SMMU_CB_TTBR0_HI 0x24
234#define ARM_SMMU_CB_TTBCR 0x30
235#define ARM_SMMU_CB_S1_MAIR0 0x38
236#define ARM_SMMU_CB_FSR 0x58
237#define ARM_SMMU_CB_FAR_LO 0x60
238#define ARM_SMMU_CB_FAR_HI 0x64
239#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon1463fe42013-07-31 19:21:27 +0100240#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon45ae7cf2013-06-24 18:31:25 +0100241
242#define SCTLR_S1_ASIDPNE (1 << 12)
243#define SCTLR_CFCFG (1 << 7)
244#define SCTLR_CFIE (1 << 6)
245#define SCTLR_CFRE (1 << 5)
246#define SCTLR_E (1 << 4)
247#define SCTLR_AFE (1 << 2)
248#define SCTLR_TRE (1 << 1)
249#define SCTLR_M (1 << 0)
250#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
251
252#define RESUME_RETRY (0 << 0)
253#define RESUME_TERMINATE (1 << 0)
254
255#define TTBCR_EAE (1 << 31)
256
257#define TTBCR_PASIZE_SHIFT 16
258#define TTBCR_PASIZE_MASK 0x7
259
260#define TTBCR_TG0_4K (0 << 14)
261#define TTBCR_TG0_64K (1 << 14)
262
263#define TTBCR_SH0_SHIFT 12
264#define TTBCR_SH0_MASK 0x3
265#define TTBCR_SH_NS 0
266#define TTBCR_SH_OS 2
267#define TTBCR_SH_IS 3
268
269#define TTBCR_ORGN0_SHIFT 10
270#define TTBCR_IRGN0_SHIFT 8
271#define TTBCR_RGN_MASK 0x3
272#define TTBCR_RGN_NC 0
273#define TTBCR_RGN_WBWA 1
274#define TTBCR_RGN_WT 2
275#define TTBCR_RGN_WB 3
276
277#define TTBCR_SL0_SHIFT 6
278#define TTBCR_SL0_MASK 0x3
279#define TTBCR_SL0_LVL_2 0
280#define TTBCR_SL0_LVL_1 1
281
282#define TTBCR_T1SZ_SHIFT 16
283#define TTBCR_T0SZ_SHIFT 0
284#define TTBCR_SZ_MASK 0xf
285
286#define TTBCR2_SEP_SHIFT 15
287#define TTBCR2_SEP_MASK 0x7
288
289#define TTBCR2_PASIZE_SHIFT 0
290#define TTBCR2_PASIZE_MASK 0x7
291
292/* Common definitions for PASize and SEP fields */
293#define TTBCR2_ADDR_32 0
294#define TTBCR2_ADDR_36 1
295#define TTBCR2_ADDR_40 2
296#define TTBCR2_ADDR_42 3
297#define TTBCR2_ADDR_44 4
298#define TTBCR2_ADDR_48 5
299
Will Deacon1463fe42013-07-31 19:21:27 +0100300#define TTBRn_HI_ASID_SHIFT 16
301
Will Deacon45ae7cf2013-06-24 18:31:25 +0100302#define MAIR_ATTR_SHIFT(n) ((n) << 3)
303#define MAIR_ATTR_MASK 0xff
304#define MAIR_ATTR_DEVICE 0x04
305#define MAIR_ATTR_NC 0x44
306#define MAIR_ATTR_WBRWA 0xff
307#define MAIR_ATTR_IDX_NC 0
308#define MAIR_ATTR_IDX_CACHE 1
309#define MAIR_ATTR_IDX_DEV 2
310
311#define FSR_MULTI (1 << 31)
312#define FSR_SS (1 << 30)
313#define FSR_UUT (1 << 8)
314#define FSR_ASF (1 << 7)
315#define FSR_TLBLKF (1 << 6)
316#define FSR_TLBMCF (1 << 5)
317#define FSR_EF (1 << 4)
318#define FSR_PF (1 << 3)
319#define FSR_AFF (1 << 2)
320#define FSR_TF (1 << 1)
321
Mitchel Humpherys29073202014-07-08 09:52:18 -0700322#define FSR_IGN (FSR_AFF | FSR_ASF | \
323 FSR_TLBMCF | FSR_TLBLKF)
324#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100325 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100326
327#define FSYNR0_WNR (1 << 4)
328
329struct arm_smmu_smr {
330 u8 idx;
331 u16 mask;
332 u16 id;
333};
334
Will Deacona9a1b0b2014-05-01 18:05:08 +0100335struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100336 int num_streamids;
337 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100338 struct arm_smmu_smr *smrs;
339};
340
Will Deacona9a1b0b2014-05-01 18:05:08 +0100341struct arm_smmu_master {
342 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100343 struct rb_node node;
344 struct arm_smmu_master_cfg cfg;
345};
346
Will Deacon45ae7cf2013-06-24 18:31:25 +0100347struct arm_smmu_device {
348 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100349
350 void __iomem *base;
351 unsigned long size;
352 unsigned long pagesize;
353
354#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
355#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
356#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
357#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
358#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
359 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000360
361#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
362 u32 options;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100363 int version;
364
365 u32 num_context_banks;
366 u32 num_s2_context_banks;
367 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
368 atomic_t irptndx;
369
370 u32 num_mapping_groups;
371 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
372
373 unsigned long input_size;
374 unsigned long s1_output_size;
375 unsigned long s2_output_size;
376
377 u32 num_global_irqs;
378 u32 num_context_irqs;
379 unsigned int *irqs;
380
Will Deacon45ae7cf2013-06-24 18:31:25 +0100381 struct list_head list;
382 struct rb_root masters;
383};
384
385struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100386 u8 cbndx;
387 u8 irptndx;
388 u32 cbar;
389 pgd_t *pgd;
390};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100391#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100392
Will Deaconecfadb62013-07-31 19:21:28 +0100393#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
394#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
395
Will Deacon45ae7cf2013-06-24 18:31:25 +0100396struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100397 struct arm_smmu_device *smmu;
398 struct arm_smmu_cfg cfg;
Will Deaconc9d09e22014-02-04 22:12:42 +0000399 spinlock_t lock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100400};
401
402static DEFINE_SPINLOCK(arm_smmu_devices_lock);
403static LIST_HEAD(arm_smmu_devices);
404
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000405struct arm_smmu_option_prop {
406 u32 opt;
407 const char *prop;
408};
409
Mitchel Humpherys29073202014-07-08 09:52:18 -0700410static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000411 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
412 { 0, NULL},
413};
414
415static void parse_driver_options(struct arm_smmu_device *smmu)
416{
417 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700418
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000419 do {
420 if (of_property_read_bool(smmu->dev->of_node,
421 arm_smmu_options[i].prop)) {
422 smmu->options |= arm_smmu_options[i].opt;
423 dev_notice(smmu->dev, "option %s\n",
424 arm_smmu_options[i].prop);
425 }
426 } while (arm_smmu_options[++i].opt);
427}
428
Will Deacona9a1b0b2014-05-01 18:05:08 +0100429static struct device *dev_get_master_dev(struct device *dev)
430{
431 if (dev_is_pci(dev)) {
432 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700433
Will Deacona9a1b0b2014-05-01 18:05:08 +0100434 while (!pci_is_root_bus(bus))
435 bus = bus->parent;
436 return bus->bridge->parent;
437 }
438
439 return dev;
440}
441
Will Deacon45ae7cf2013-06-24 18:31:25 +0100442static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
443 struct device_node *dev_node)
444{
445 struct rb_node *node = smmu->masters.rb_node;
446
447 while (node) {
448 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700449
Will Deacon45ae7cf2013-06-24 18:31:25 +0100450 master = container_of(node, struct arm_smmu_master, node);
451
452 if (dev_node < master->of_node)
453 node = node->rb_left;
454 else if (dev_node > master->of_node)
455 node = node->rb_right;
456 else
457 return master;
458 }
459
460 return NULL;
461}
462
Will Deacona9a1b0b2014-05-01 18:05:08 +0100463static struct arm_smmu_master_cfg *
464find_smmu_master_cfg(struct arm_smmu_device *smmu, struct device *dev)
465{
466 struct arm_smmu_master *master;
467
468 if (dev_is_pci(dev))
469 return dev->archdata.iommu;
470
471 master = find_smmu_master(smmu, dev->of_node);
472 return master ? &master->cfg : NULL;
473}
474
Will Deacon45ae7cf2013-06-24 18:31:25 +0100475static int insert_smmu_master(struct arm_smmu_device *smmu,
476 struct arm_smmu_master *master)
477{
478 struct rb_node **new, *parent;
479
480 new = &smmu->masters.rb_node;
481 parent = NULL;
482 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700483 struct arm_smmu_master *this
484 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100485
486 parent = *new;
487 if (master->of_node < this->of_node)
488 new = &((*new)->rb_left);
489 else if (master->of_node > this->of_node)
490 new = &((*new)->rb_right);
491 else
492 return -EEXIST;
493 }
494
495 rb_link_node(&master->node, parent, new);
496 rb_insert_color(&master->node, &smmu->masters);
497 return 0;
498}
499
500static int register_smmu_master(struct arm_smmu_device *smmu,
501 struct device *dev,
502 struct of_phandle_args *masterspec)
503{
504 int i;
505 struct arm_smmu_master *master;
506
507 master = find_smmu_master(smmu, masterspec->np);
508 if (master) {
509 dev_err(dev,
510 "rejecting multiple registrations for master device %s\n",
511 masterspec->np->name);
512 return -EBUSY;
513 }
514
515 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
516 dev_err(dev,
517 "reached maximum number (%d) of stream IDs for master device %s\n",
518 MAX_MASTER_STREAMIDS, masterspec->np->name);
519 return -ENOSPC;
520 }
521
522 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
523 if (!master)
524 return -ENOMEM;
525
Will Deacona9a1b0b2014-05-01 18:05:08 +0100526 master->of_node = masterspec->np;
527 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100528
Olav Haugan3c8766d2014-08-22 17:12:32 -0700529 for (i = 0; i < master->cfg.num_streamids; ++i) {
530 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100531
Olav Haugan3c8766d2014-08-22 17:12:32 -0700532 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
533 (streamid >= smmu->num_mapping_groups)) {
534 dev_err(dev,
535 "stream ID for master device %s greater than maximum allowed (%d)\n",
536 masterspec->np->name, smmu->num_mapping_groups);
537 return -ERANGE;
538 }
539 master->cfg.streamids[i] = streamid;
540 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100541 return insert_smmu_master(smmu, master);
542}
543
Will Deacon44680ee2014-06-25 11:29:12 +0100544static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100545{
Will Deacon44680ee2014-06-25 11:29:12 +0100546 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100547 struct arm_smmu_master *master = NULL;
548 struct device_node *dev_node = dev_get_master_dev(dev)->of_node;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100549
550 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100551 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100552 master = find_smmu_master(smmu, dev_node);
553 if (master)
554 break;
555 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100556 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100557
Will Deacona9a1b0b2014-05-01 18:05:08 +0100558 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100559}
560
561static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
562{
563 int idx;
564
565 do {
566 idx = find_next_zero_bit(map, end, start);
567 if (idx == end)
568 return -ENOSPC;
569 } while (test_and_set_bit(idx, map));
570
571 return idx;
572}
573
574static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
575{
576 clear_bit(idx, map);
577}
578
579/* Wait for any pending TLB invalidations to complete */
580static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
581{
582 int count = 0;
583 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
584
585 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
586 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
587 & sTLBGSTATUS_GSACTIVE) {
588 cpu_relax();
589 if (++count == TLB_LOOP_TIMEOUT) {
590 dev_err_ratelimited(smmu->dev,
591 "TLB sync timed out -- SMMU may be deadlocked\n");
592 return;
593 }
594 udelay(1);
595 }
596}
597
Will Deacon44680ee2014-06-25 11:29:12 +0100598static void arm_smmu_tlb_inv_context(struct arm_smmu_domain *smmu_domain)
Will Deacon1463fe42013-07-31 19:21:27 +0100599{
Will Deacon44680ee2014-06-25 11:29:12 +0100600 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
601 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100602 void __iomem *base = ARM_SMMU_GR0(smmu);
603 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
604
605 if (stage1) {
606 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100607 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
608 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100609 } else {
610 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100611 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
612 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100613 }
614
615 arm_smmu_tlb_sync(smmu);
616}
617
Will Deacon45ae7cf2013-06-24 18:31:25 +0100618static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
619{
620 int flags, ret;
621 u32 fsr, far, fsynr, resume;
622 unsigned long iova;
623 struct iommu_domain *domain = dev;
624 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100625 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
626 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100627 void __iomem *cb_base;
628
Will Deacon44680ee2014-06-25 11:29:12 +0100629 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100630 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
631
632 if (!(fsr & FSR_FAULT))
633 return IRQ_NONE;
634
635 if (fsr & FSR_IGN)
636 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100637 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100638 fsr);
639
640 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
641 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
642
643 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
644 iova = far;
645#ifdef CONFIG_64BIT
646 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
647 iova |= ((unsigned long)far << 32);
648#endif
649
650 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
651 ret = IRQ_HANDLED;
652 resume = RESUME_RETRY;
653 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100654 dev_err_ratelimited(smmu->dev,
655 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100656 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100657 ret = IRQ_NONE;
658 resume = RESUME_TERMINATE;
659 }
660
661 /* Clear the faulting FSR */
662 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
663
664 /* Retry or terminate any stalled transactions */
665 if (fsr & FSR_SS)
666 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
667
668 return ret;
669}
670
671static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
672{
673 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
674 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000675 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100676
677 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
678 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
679 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
680 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
681
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000682 if (!gfsr)
683 return IRQ_NONE;
684
Will Deacon45ae7cf2013-06-24 18:31:25 +0100685 dev_err_ratelimited(smmu->dev,
686 "Unexpected global fault, this could be serious\n");
687 dev_err_ratelimited(smmu->dev,
688 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
689 gfsr, gfsynr0, gfsynr1, gfsynr2);
690
691 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100692 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100693}
694
Will Deacon6dd35f42014-02-05 17:49:34 +0000695static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
696 size_t size)
697{
698 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
699
700
701 /* Ensure new page tables are visible to the hardware walker */
702 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
Will Deacon3aa80ea2014-02-05 23:35:47 +0000703 dsb(ishst);
Will Deacon6dd35f42014-02-05 17:49:34 +0000704 } else {
705 /*
706 * If the SMMU can't walk tables in the CPU caches, treat them
707 * like non-coherent DMA since we need to flush the new entries
708 * all the way out to memory. There's no possibility of
709 * recursion here as the SMMU table walker will not be wired
710 * through another SMMU.
711 */
712 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
713 DMA_TO_DEVICE);
714 }
715}
716
Will Deacon45ae7cf2013-06-24 18:31:25 +0100717static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
718{
719 u32 reg;
720 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100721 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
722 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100723 void __iomem *cb_base, *gr0_base, *gr1_base;
724
725 gr0_base = ARM_SMMU_GR0(smmu);
726 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100727 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
728 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100729
730 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100731 reg = cfg->cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100732 if (smmu->version == 1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700733 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100734
Will Deacon57ca90f2014-02-06 14:59:05 +0000735 /*
736 * Use the weakest shareability/memory types, so they are
737 * overridden by the ttbcr/pte.
738 */
739 if (stage1) {
740 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
741 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
742 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100743 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000744 }
Will Deacon44680ee2014-06-25 11:29:12 +0100745 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100746
747 if (smmu->version > 1) {
748 /* CBA2R */
749#ifdef CONFIG_64BIT
750 reg = CBA2R_RW64_64BIT;
751#else
752 reg = CBA2R_RW64_32BIT;
753#endif
754 writel_relaxed(reg,
Will Deacon44680ee2014-06-25 11:29:12 +0100755 gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100756
757 /* TTBCR2 */
758 switch (smmu->input_size) {
759 case 32:
760 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
761 break;
762 case 36:
763 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
764 break;
765 case 39:
Will Deacon4d09d992014-07-23 13:20:43 +0100766 case 40:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100767 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
768 break;
769 case 42:
770 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
771 break;
772 case 44:
773 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
774 break;
775 case 48:
776 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
777 break;
778 }
779
780 switch (smmu->s1_output_size) {
781 case 32:
782 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
783 break;
784 case 36:
785 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
786 break;
787 case 39:
Will Deacon4d09d992014-07-23 13:20:43 +0100788 case 40:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100789 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
790 break;
791 case 42:
792 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
793 break;
794 case 44:
795 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
796 break;
797 case 48:
798 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
799 break;
800 }
801
802 if (stage1)
803 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
804 }
805
806 /* TTBR0 */
Will Deacon44680ee2014-06-25 11:29:12 +0100807 arm_smmu_flush_pgtable(smmu, cfg->pgd,
Will Deacon6dd35f42014-02-05 17:49:34 +0000808 PTRS_PER_PGD * sizeof(pgd_t));
Will Deacon44680ee2014-06-25 11:29:12 +0100809 reg = __pa(cfg->pgd);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100810 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
Will Deacon44680ee2014-06-25 11:29:12 +0100811 reg = (phys_addr_t)__pa(cfg->pgd) >> 32;
Will Deacon1463fe42013-07-31 19:21:27 +0100812 if (stage1)
Will Deacon44680ee2014-06-25 11:29:12 +0100813 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100814 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100815
816 /*
817 * TTBCR
818 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
819 */
820 if (smmu->version > 1) {
821 if (PAGE_SIZE == SZ_4K)
822 reg = TTBCR_TG0_4K;
823 else
824 reg = TTBCR_TG0_64K;
825
826 if (!stage1) {
Will Deacona65217a2014-06-24 18:26:26 +0100827 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
828
Will Deacon45ae7cf2013-06-24 18:31:25 +0100829 switch (smmu->s2_output_size) {
830 case 32:
831 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
832 break;
833 case 36:
834 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
835 break;
836 case 40:
837 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
838 break;
839 case 42:
840 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
841 break;
842 case 44:
843 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
844 break;
845 case 48:
846 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
847 break;
848 }
849 } else {
Will Deacona65217a2014-06-24 18:26:26 +0100850 reg |= (64 - smmu->input_size) << TTBCR_T0SZ_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100851 }
852 } else {
853 reg = 0;
854 }
855
856 reg |= TTBCR_EAE |
857 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
858 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
Olav Haugan1fc870c2014-08-04 19:01:02 +0100859 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT);
860
861 if (!stage1)
862 reg |= (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
863
Will Deacon45ae7cf2013-06-24 18:31:25 +0100864 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
865
866 /* MAIR0 (stage-1 only) */
867 if (stage1) {
868 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
869 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
870 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
871 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
872 }
873
Will Deacon45ae7cf2013-06-24 18:31:25 +0100874 /* SCTLR */
875 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
876 if (stage1)
877 reg |= SCTLR_S1_ASIDPNE;
878#ifdef __BIG_ENDIAN
879 reg |= SCTLR_E;
880#endif
Will Deacon25724842013-08-21 13:49:53 +0100881 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100882}
883
884static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100885 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100886{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100887 int irq, start, ret = 0;
888 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100889 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100890 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100891
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100892 spin_lock_irqsave(&smmu_domain->lock, flags);
893 if (smmu_domain->smmu)
894 goto out_unlock;
895
Will Deacon45ae7cf2013-06-24 18:31:25 +0100896 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
897 /*
898 * We will likely want to change this if/when KVM gets
899 * involved.
900 */
Will Deacon44680ee2014-06-25 11:29:12 +0100901 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100902 start = smmu->num_s2_context_banks;
Will Deacon9c5c92e2014-06-25 12:12:41 +0100903 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100904 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100905 start = smmu->num_s2_context_banks;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100906 } else {
Will Deacon9c5c92e2014-06-25 12:12:41 +0100907 cfg->cbar = CBAR_TYPE_S2_TRANS;
908 start = 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100909 }
910
911 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
912 smmu->num_context_banks);
913 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100914 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100915
Will Deacon44680ee2014-06-25 11:29:12 +0100916 cfg->cbndx = ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100917 if (smmu->version == 1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100918 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
919 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100920 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100921 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100922 }
923
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100924 ACCESS_ONCE(smmu_domain->smmu) = smmu;
925 arm_smmu_init_context_bank(smmu_domain);
926 spin_unlock_irqrestore(&smmu_domain->lock, flags);
927
Will Deacon44680ee2014-06-25 11:29:12 +0100928 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100929 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
930 "arm-smmu-context-fault", domain);
931 if (IS_ERR_VALUE(ret)) {
932 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100933 cfg->irptndx, irq);
934 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100935 }
936
Will Deacona9a1b0b2014-05-01 18:05:08 +0100937 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100938
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100939out_unlock:
940 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100941 return ret;
942}
943
944static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
945{
946 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100947 struct arm_smmu_device *smmu = smmu_domain->smmu;
948 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100949 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100950 int irq;
951
952 if (!smmu)
953 return;
954
Will Deacon1463fe42013-07-31 19:21:27 +0100955 /* Disable the context bank and nuke the TLB before freeing it. */
Will Deacon44680ee2014-06-25 11:29:12 +0100956 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100957 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon44680ee2014-06-25 11:29:12 +0100958 arm_smmu_tlb_inv_context(smmu_domain);
Will Deacon1463fe42013-07-31 19:21:27 +0100959
Will Deacon44680ee2014-06-25 11:29:12 +0100960 if (cfg->irptndx != INVALID_IRPTNDX) {
961 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100962 free_irq(irq, domain);
963 }
964
Will Deacon44680ee2014-06-25 11:29:12 +0100965 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100966}
967
968static int arm_smmu_domain_init(struct iommu_domain *domain)
969{
970 struct arm_smmu_domain *smmu_domain;
971 pgd_t *pgd;
972
973 /*
974 * Allocate the domain and initialise some of its data structures.
975 * We can't really do anything meaningful until we've added a
976 * master.
977 */
978 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
979 if (!smmu_domain)
980 return -ENOMEM;
981
Mitchel Humpherys29073202014-07-08 09:52:18 -0700982 pgd = kcalloc(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100983 if (!pgd)
984 goto out_free_domain;
Will Deacon44680ee2014-06-25 11:29:12 +0100985 smmu_domain->cfg.pgd = pgd;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100986
Will Deaconc9d09e22014-02-04 22:12:42 +0000987 spin_lock_init(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100988 domain->priv = smmu_domain;
989 return 0;
990
991out_free_domain:
992 kfree(smmu_domain);
993 return -ENOMEM;
994}
995
996static void arm_smmu_free_ptes(pmd_t *pmd)
997{
998 pgtable_t table = pmd_pgtable(*pmd);
Mitchel Humpherys29073202014-07-08 09:52:18 -0700999
Will Deacon45ae7cf2013-06-24 18:31:25 +01001000 __free_page(table);
1001}
1002
1003static void arm_smmu_free_pmds(pud_t *pud)
1004{
1005 int i;
1006 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
1007
1008 pmd = pmd_base;
1009 for (i = 0; i < PTRS_PER_PMD; ++i) {
1010 if (pmd_none(*pmd))
1011 continue;
1012
1013 arm_smmu_free_ptes(pmd);
1014 pmd++;
1015 }
1016
1017 pmd_free(NULL, pmd_base);
1018}
1019
1020static void arm_smmu_free_puds(pgd_t *pgd)
1021{
1022 int i;
1023 pud_t *pud, *pud_base = pud_offset(pgd, 0);
1024
1025 pud = pud_base;
1026 for (i = 0; i < PTRS_PER_PUD; ++i) {
1027 if (pud_none(*pud))
1028 continue;
1029
1030 arm_smmu_free_pmds(pud);
1031 pud++;
1032 }
1033
1034 pud_free(NULL, pud_base);
1035}
1036
1037static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
1038{
1039 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001040 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1041 pgd_t *pgd, *pgd_base = cfg->pgd;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001042
1043 /*
1044 * Recursively free the page tables for this domain. We don't
Will Deacon34fb4b32014-02-26 11:14:37 +00001045 * care about speculative TLB filling because the tables should
1046 * not be active in any context bank at this point (SCTLR.M is 0).
Will Deacon45ae7cf2013-06-24 18:31:25 +01001047 */
1048 pgd = pgd_base;
1049 for (i = 0; i < PTRS_PER_PGD; ++i) {
1050 if (pgd_none(*pgd))
1051 continue;
1052 arm_smmu_free_puds(pgd);
1053 pgd++;
1054 }
1055
1056 kfree(pgd_base);
1057}
1058
1059static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1060{
1061 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001062
1063 /*
1064 * Free the domain resources. We assume that all devices have
1065 * already been detached.
1066 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001067 arm_smmu_destroy_domain_context(domain);
1068 arm_smmu_free_pgtables(smmu_domain);
1069 kfree(smmu_domain);
1070}
1071
1072static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001073 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001074{
1075 int i;
1076 struct arm_smmu_smr *smrs;
1077 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1078
1079 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1080 return 0;
1081
Will Deacona9a1b0b2014-05-01 18:05:08 +01001082 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001083 return -EEXIST;
1084
Mitchel Humpherys29073202014-07-08 09:52:18 -07001085 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001086 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001087 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1088 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001089 return -ENOMEM;
1090 }
1091
Will Deacon44680ee2014-06-25 11:29:12 +01001092 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001093 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001094 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1095 smmu->num_mapping_groups);
1096 if (IS_ERR_VALUE(idx)) {
1097 dev_err(smmu->dev, "failed to allocate free SMR\n");
1098 goto err_free_smrs;
1099 }
1100
1101 smrs[i] = (struct arm_smmu_smr) {
1102 .idx = idx,
1103 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001104 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001105 };
1106 }
1107
1108 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001109 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001110 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1111 smrs[i].mask << SMR_MASK_SHIFT;
1112 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1113 }
1114
Will Deacona9a1b0b2014-05-01 18:05:08 +01001115 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001116 return 0;
1117
1118err_free_smrs:
1119 while (--i >= 0)
1120 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1121 kfree(smrs);
1122 return -ENOSPC;
1123}
1124
1125static void arm_smmu_master_free_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 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001130 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001131
Will Deacon43b412b2014-07-15 11:22:24 +01001132 if (!smrs)
1133 return;
1134
Will Deacon45ae7cf2013-06-24 18:31:25 +01001135 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001136 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001137 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001138
Will Deacon45ae7cf2013-06-24 18:31:25 +01001139 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1140 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1141 }
1142
Will Deacona9a1b0b2014-05-01 18:05:08 +01001143 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001144 kfree(smrs);
1145}
1146
Will Deacon45ae7cf2013-06-24 18:31:25 +01001147static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001148 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001149{
1150 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001151 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001152 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1153
Will Deacona9a1b0b2014-05-01 18:05:08 +01001154 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001155 if (ret)
1156 return ret;
1157
Will Deacona9a1b0b2014-05-01 18:05:08 +01001158 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001159 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001160
Will Deacona9a1b0b2014-05-01 18:05:08 +01001161 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001162 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001163 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001164 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1165 }
1166
1167 return 0;
1168}
1169
1170static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001171 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001172{
Will Deacon43b412b2014-07-15 11:22:24 +01001173 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001174 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001175 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001176
1177 /*
1178 * We *must* clear the S2CR first, because freeing the SMR means
1179 * that it can be re-allocated immediately.
1180 */
Will Deacon43b412b2014-07-15 11:22:24 +01001181 for (i = 0; i < cfg->num_streamids; ++i) {
1182 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1183
1184 writel_relaxed(S2CR_TYPE_BYPASS,
1185 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1186 }
1187
Will Deacona9a1b0b2014-05-01 18:05:08 +01001188 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001189}
1190
1191static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1192{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001193 int ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001194 struct arm_smmu_domain *smmu_domain = domain->priv;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001195 struct arm_smmu_device *smmu, *dom_smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001196 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001197
Will Deacon44680ee2014-06-25 11:29:12 +01001198 smmu = dev_get_master_dev(dev)->archdata.iommu;
1199 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001200 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1201 return -ENXIO;
1202 }
1203
1204 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001205 * Sanity check the domain. We don't support domains across
1206 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001207 */
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001208 dom_smmu = ACCESS_ONCE(smmu_domain->smmu);
1209 if (!dom_smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001210 /* Now that we have a master, we can finalise the domain */
Will Deacon44680ee2014-06-25 11:29:12 +01001211 ret = arm_smmu_init_domain_context(domain, smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001212 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001213 return ret;
1214
1215 dom_smmu = smmu_domain->smmu;
1216 }
1217
1218 if (dom_smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001219 dev_err(dev,
1220 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001221 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1222 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001223 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001224
1225 /* Looks ok, so add the device to the domain */
Will Deacon44680ee2014-06-25 11:29:12 +01001226 cfg = find_smmu_master_cfg(smmu_domain->smmu, dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001227 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001228 return -ENODEV;
1229
Will Deacona9a1b0b2014-05-01 18:05:08 +01001230 return arm_smmu_domain_add_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001231}
1232
1233static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1234{
1235 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001236 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001237
Will Deacon44680ee2014-06-25 11:29:12 +01001238 cfg = find_smmu_master_cfg(smmu_domain->smmu, dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001239 if (cfg)
1240 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001241}
1242
Will Deacon45ae7cf2013-06-24 18:31:25 +01001243static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1244 unsigned long end)
1245{
1246 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1247 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1248}
1249
1250static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1251 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001252 unsigned long pfn, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001253{
1254 pte_t *pte, *start;
Will Deaconcf2d45b2013-11-05 16:32:00 +00001255 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001256
1257 if (pmd_none(*pmd)) {
1258 /* Allocate a new set of tables */
Will Deaconc9d09e22014-02-04 22:12:42 +00001259 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001260
Will Deacon45ae7cf2013-06-24 18:31:25 +01001261 if (!table)
1262 return -ENOMEM;
1263
Will Deacon6dd35f42014-02-05 17:49:34 +00001264 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001265 pmd_populate(NULL, pmd, table);
1266 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1267 }
1268
1269 if (stage == 1) {
Will Deacon1463fe42013-07-31 19:21:27 +01001270 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
Will Deaconb410aed2014-02-20 16:31:06 +00001271 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001272 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1273
Will Deaconb410aed2014-02-20 16:31:06 +00001274 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001275 pteval |= (MAIR_ATTR_IDX_CACHE <<
1276 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1277 } else {
1278 pteval |= ARM_SMMU_PTE_HAP_FAULT;
Will Deaconb410aed2014-02-20 16:31:06 +00001279 if (prot & IOMMU_READ)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001280 pteval |= ARM_SMMU_PTE_HAP_READ;
Will Deaconb410aed2014-02-20 16:31:06 +00001281 if (prot & IOMMU_WRITE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001282 pteval |= ARM_SMMU_PTE_HAP_WRITE;
Will Deaconb410aed2014-02-20 16:31:06 +00001283 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001284 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1285 else
1286 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1287 }
1288
1289 /* If no access, create a faulting entry to avoid TLB fills */
Will Deaconb410aed2014-02-20 16:31:06 +00001290 if (prot & IOMMU_EXEC)
Will Deaconcf2d45b2013-11-05 16:32:00 +00001291 pteval &= ~ARM_SMMU_PTE_XN;
Will Deaconb410aed2014-02-20 16:31:06 +00001292 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001293 pteval &= ~ARM_SMMU_PTE_PAGE;
1294
1295 pteval |= ARM_SMMU_PTE_SH_IS;
1296 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1297 pte = start;
1298
1299 /*
1300 * Install the page table entries. This is fairly complicated
1301 * since we attempt to make use of the contiguous hint in the
1302 * ptes where possible. The contiguous hint indicates a series
1303 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1304 * contiguous region with the following constraints:
1305 *
1306 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1307 * - Each pte in the region has the contiguous hint bit set
1308 *
1309 * This complicates unmapping (also handled by this code, when
1310 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1311 * possible, yet highly unlikely, that a client may unmap only
1312 * part of a contiguous range. This requires clearing of the
1313 * contiguous hint bits in the range before installing the new
1314 * faulting entries.
1315 *
1316 * Note that re-mapping an address range without first unmapping
1317 * it is not supported, so TLB invalidation is not required here
1318 * and is instead performed at unmap and domain-init time.
1319 */
1320 do {
1321 int i = 1;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001322
Will Deacon45ae7cf2013-06-24 18:31:25 +01001323 pteval &= ~ARM_SMMU_PTE_CONT;
1324
1325 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1326 i = ARM_SMMU_PTE_CONT_ENTRIES;
1327 pteval |= ARM_SMMU_PTE_CONT;
1328 } else if (pte_val(*pte) &
1329 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1330 int j;
1331 pte_t *cont_start;
1332 unsigned long idx = pte_index(addr);
1333
1334 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1335 cont_start = pmd_page_vaddr(*pmd) + idx;
1336 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001337 pte_val(*(cont_start + j)) &=
1338 ~ARM_SMMU_PTE_CONT;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001339
1340 arm_smmu_flush_pgtable(smmu, cont_start,
1341 sizeof(*pte) *
1342 ARM_SMMU_PTE_CONT_ENTRIES);
1343 }
1344
1345 do {
1346 *pte = pfn_pte(pfn, __pgprot(pteval));
1347 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1348 } while (addr != end);
1349
1350 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1351 return 0;
1352}
1353
1354static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1355 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001356 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001357{
1358 int ret;
1359 pmd_t *pmd;
1360 unsigned long next, pfn = __phys_to_pfn(phys);
1361
1362#ifndef __PAGETABLE_PMD_FOLDED
1363 if (pud_none(*pud)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001364 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001365 if (!pmd)
1366 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001367
Will Deacon6dd35f42014-02-05 17:49:34 +00001368 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001369 pud_populate(NULL, pud, pmd);
1370 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1371
1372 pmd += pmd_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001373 } else
1374#endif
1375 pmd = pmd_offset(pud, addr);
1376
1377 do {
1378 next = pmd_addr_end(addr, end);
Bin Wangaca1bc42014-03-21 10:06:07 +00001379 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn,
Will Deaconb410aed2014-02-20 16:31:06 +00001380 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001381 phys += next - addr;
1382 } while (pmd++, addr = next, addr < end);
1383
1384 return ret;
1385}
1386
1387static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1388 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001389 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001390{
1391 int ret = 0;
1392 pud_t *pud;
1393 unsigned long next;
1394
1395#ifndef __PAGETABLE_PUD_FOLDED
1396 if (pgd_none(*pgd)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001397 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001398 if (!pud)
1399 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001400
Will Deacon6dd35f42014-02-05 17:49:34 +00001401 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001402 pgd_populate(NULL, pgd, pud);
1403 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1404
1405 pud += pud_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001406 } else
1407#endif
1408 pud = pud_offset(pgd, addr);
1409
1410 do {
1411 next = pud_addr_end(addr, end);
1412 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
Will Deaconb410aed2014-02-20 16:31:06 +00001413 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001414 phys += next - addr;
1415 } while (pud++, addr = next, addr < end);
1416
1417 return ret;
1418}
1419
1420static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1421 unsigned long iova, phys_addr_t paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001422 size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001423{
1424 int ret, stage;
1425 unsigned long end;
1426 phys_addr_t input_mask, output_mask;
Will Deacon44680ee2014-06-25 11:29:12 +01001427 struct arm_smmu_device *smmu = smmu_domain->smmu;
1428 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1429 pgd_t *pgd = cfg->pgd;
Will Deaconb410aed2014-02-20 16:31:06 +00001430 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001431
Will Deacon44680ee2014-06-25 11:29:12 +01001432 if (cfg->cbar == CBAR_TYPE_S2_TRANS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001433 stage = 2;
1434 output_mask = (1ULL << smmu->s2_output_size) - 1;
1435 } else {
1436 stage = 1;
1437 output_mask = (1ULL << smmu->s1_output_size) - 1;
1438 }
1439
1440 if (!pgd)
1441 return -EINVAL;
1442
1443 if (size & ~PAGE_MASK)
1444 return -EINVAL;
1445
1446 input_mask = (1ULL << smmu->input_size) - 1;
1447 if ((phys_addr_t)iova & ~input_mask)
1448 return -ERANGE;
1449
1450 if (paddr & ~output_mask)
1451 return -ERANGE;
1452
Will Deaconb410aed2014-02-20 16:31:06 +00001453 spin_lock_irqsave(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001454 pgd += pgd_index(iova);
1455 end = iova + size;
1456 do {
1457 unsigned long next = pgd_addr_end(iova, end);
1458
1459 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001460 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001461 if (ret)
1462 goto out_unlock;
1463
1464 paddr += next - iova;
1465 iova = next;
1466 } while (pgd++, iova != end);
1467
1468out_unlock:
Will Deaconb410aed2014-02-20 16:31:06 +00001469 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001470
Will Deacon45ae7cf2013-06-24 18:31:25 +01001471 return ret;
1472}
1473
1474static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001475 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001476{
1477 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001478
Will Deacon5552ecd2013-11-08 15:08:06 +00001479 if (!smmu_domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001480 return -ENODEV;
1481
Will Deaconb410aed2014-02-20 16:31:06 +00001482 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, prot);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001483}
1484
1485static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1486 size_t size)
1487{
1488 int ret;
1489 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001490
1491 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
Will Deacon44680ee2014-06-25 11:29:12 +01001492 arm_smmu_tlb_inv_context(smmu_domain);
Laurent Pinchart16c50dcf2014-02-28 15:37:10 +00001493 return ret ? 0 : size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001494}
1495
1496static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1497 dma_addr_t iova)
1498{
Will Deacona44a9792013-11-07 18:47:50 +00001499 pgd_t *pgdp, pgd;
1500 pud_t pud;
1501 pmd_t pmd;
1502 pte_t pte;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001503 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +01001504 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001505
Will Deacon44680ee2014-06-25 11:29:12 +01001506 pgdp = cfg->pgd;
Will Deacona44a9792013-11-07 18:47:50 +00001507 if (!pgdp)
1508 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001509
Will Deacona44a9792013-11-07 18:47:50 +00001510 pgd = *(pgdp + pgd_index(iova));
1511 if (pgd_none(pgd))
1512 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001513
Will Deacona44a9792013-11-07 18:47:50 +00001514 pud = *pud_offset(&pgd, iova);
1515 if (pud_none(pud))
1516 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001517
Will Deacona44a9792013-11-07 18:47:50 +00001518 pmd = *pmd_offset(&pud, iova);
1519 if (pmd_none(pmd))
1520 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001521
Will Deacona44a9792013-11-07 18:47:50 +00001522 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001523 if (pte_none(pte))
Will Deacona44a9792013-11-07 18:47:50 +00001524 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001525
Will Deacona44a9792013-11-07 18:47:50 +00001526 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001527}
1528
1529static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1530 unsigned long cap)
1531{
Will Deacon45ae7cf2013-06-24 18:31:25 +01001532 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacond3bca162014-07-04 11:06:01 +01001533 struct arm_smmu_device *smmu = smmu_domain->smmu;
1534 u32 features = smmu ? smmu->features : 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001535
Will Deacond0948942014-06-24 17:30:10 +01001536 switch (cap) {
1537 case IOMMU_CAP_CACHE_COHERENCY:
1538 return features & ARM_SMMU_FEAT_COHERENT_WALK;
1539 case IOMMU_CAP_INTR_REMAP:
1540 return 1; /* MSIs are just memory writes */
1541 default:
1542 return 0;
1543 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001544}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001545
Will Deacona9a1b0b2014-05-01 18:05:08 +01001546static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1547{
1548 *((u16 *)data) = alias;
1549 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001550}
1551
1552static int arm_smmu_add_device(struct device *dev)
1553{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001554 struct arm_smmu_device *smmu;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001555 struct iommu_group *group;
1556 int ret;
1557
1558 if (dev->archdata.iommu) {
1559 dev_warn(dev, "IOMMU driver already assigned to device\n");
1560 return -EINVAL;
1561 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001562
Will Deacon44680ee2014-06-25 11:29:12 +01001563 smmu = find_smmu_for_device(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001564 if (!smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001565 return -ENODEV;
1566
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001567 group = iommu_group_alloc();
1568 if (IS_ERR(group)) {
1569 dev_err(dev, "Failed to allocate IOMMU group\n");
1570 return PTR_ERR(group);
1571 }
1572
Will Deacona9a1b0b2014-05-01 18:05:08 +01001573 if (dev_is_pci(dev)) {
1574 struct arm_smmu_master_cfg *cfg;
1575 struct pci_dev *pdev = to_pci_dev(dev);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001576
Will Deacona9a1b0b2014-05-01 18:05:08 +01001577 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1578 if (!cfg) {
1579 ret = -ENOMEM;
1580 goto out_put_group;
1581 }
1582
1583 cfg->num_streamids = 1;
1584 /*
1585 * Assume Stream ID == Requester ID for now.
1586 * We need a way to describe the ID mappings in FDT.
1587 */
1588 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1589 &cfg->streamids[0]);
1590 dev->archdata.iommu = cfg;
1591 } else {
1592 dev->archdata.iommu = smmu;
1593 }
1594
1595 ret = iommu_group_add_device(group, dev);
1596
1597out_put_group:
1598 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001599 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001600}
1601
1602static void arm_smmu_remove_device(struct device *dev)
1603{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001604 if (dev_is_pci(dev))
1605 kfree(dev->archdata.iommu);
1606
Will Deacon45ae7cf2013-06-24 18:31:25 +01001607 dev->archdata.iommu = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001608 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001609}
1610
Thierry Redingb22f6432014-06-27 09:03:12 +02001611static const struct iommu_ops arm_smmu_ops = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001612 .domain_init = arm_smmu_domain_init,
1613 .domain_destroy = arm_smmu_domain_destroy,
1614 .attach_dev = arm_smmu_attach_dev,
1615 .detach_dev = arm_smmu_detach_dev,
1616 .map = arm_smmu_map,
1617 .unmap = arm_smmu_unmap,
1618 .iova_to_phys = arm_smmu_iova_to_phys,
1619 .domain_has_cap = arm_smmu_domain_has_cap,
1620 .add_device = arm_smmu_add_device,
1621 .remove_device = arm_smmu_remove_device,
1622 .pgsize_bitmap = (SECTION_SIZE |
1623 ARM_SMMU_PTE_CONT_SIZE |
1624 PAGE_SIZE),
1625};
1626
1627static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1628{
1629 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001630 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001631 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001632 u32 reg;
1633
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001634 /* clear global FSR */
1635 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1636 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001637
1638 /* Mark all SMRn as invalid and all S2CRn as bypass */
1639 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001640 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001641 writel_relaxed(S2CR_TYPE_BYPASS,
1642 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001643 }
1644
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001645 /* Make sure all context banks are disabled and clear CB_FSR */
1646 for (i = 0; i < smmu->num_context_banks; ++i) {
1647 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1648 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1649 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1650 }
Will Deacon1463fe42013-07-31 19:21:27 +01001651
Will Deacon45ae7cf2013-06-24 18:31:25 +01001652 /* Invalidate the TLB, just in case */
1653 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1654 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1655 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1656
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001657 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001658
Will Deacon45ae7cf2013-06-24 18:31:25 +01001659 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001660 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001661
1662 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001663 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001664
1665 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001666 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001667
1668 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001669 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001670
1671 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001672 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001673
1674 /* Push the button */
1675 arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001676 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001677}
1678
1679static int arm_smmu_id_size_to_bits(int size)
1680{
1681 switch (size) {
1682 case 0:
1683 return 32;
1684 case 1:
1685 return 36;
1686 case 2:
1687 return 40;
1688 case 3:
1689 return 42;
1690 case 4:
1691 return 44;
1692 case 5:
1693 default:
1694 return 48;
1695 }
1696}
1697
1698static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1699{
1700 unsigned long size;
1701 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1702 u32 id;
1703
1704 dev_notice(smmu->dev, "probing hardware configuration...\n");
1705
1706 /* Primecell ID */
1707 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1708 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1709 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1710
1711 /* ID0 */
1712 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1713#ifndef CONFIG_64BIT
1714 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1715 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1716 return -ENODEV;
1717 }
1718#endif
1719 if (id & ID0_S1TS) {
1720 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1721 dev_notice(smmu->dev, "\tstage 1 translation\n");
1722 }
1723
1724 if (id & ID0_S2TS) {
1725 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1726 dev_notice(smmu->dev, "\tstage 2 translation\n");
1727 }
1728
1729 if (id & ID0_NTS) {
1730 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1731 dev_notice(smmu->dev, "\tnested translation\n");
1732 }
1733
1734 if (!(smmu->features &
1735 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 |
1736 ARM_SMMU_FEAT_TRANS_NESTED))) {
1737 dev_err(smmu->dev, "\tno translation support!\n");
1738 return -ENODEV;
1739 }
1740
1741 if (id & ID0_CTTW) {
1742 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1743 dev_notice(smmu->dev, "\tcoherent table walk\n");
1744 }
1745
1746 if (id & ID0_SMS) {
1747 u32 smr, sid, mask;
1748
1749 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1750 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1751 ID0_NUMSMRG_MASK;
1752 if (smmu->num_mapping_groups == 0) {
1753 dev_err(smmu->dev,
1754 "stream-matching supported, but no SMRs present!\n");
1755 return -ENODEV;
1756 }
1757
1758 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1759 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1760 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1761 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1762
1763 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1764 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1765 if ((mask & sid) != sid) {
1766 dev_err(smmu->dev,
1767 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1768 mask, sid);
1769 return -ENODEV;
1770 }
1771
1772 dev_notice(smmu->dev,
1773 "\tstream matching with %u register groups, mask 0x%x",
1774 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001775 } else {
1776 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1777 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001778 }
1779
1780 /* ID1 */
1781 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1782 smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
1783
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001784 /* Check for size mismatch of SMMU address space from mapped region */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001785 size = 1 <<
1786 (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001787 size *= (smmu->pagesize << 1);
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001788 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001789 dev_warn(smmu->dev,
1790 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1791 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001792
1793 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1794 ID1_NUMS2CB_MASK;
1795 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1796 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1797 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1798 return -ENODEV;
1799 }
1800 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1801 smmu->num_context_banks, smmu->num_s2_context_banks);
1802
1803 /* ID2 */
1804 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1805 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1806
1807 /*
1808 * Stage-1 output limited by stage-2 input size due to pgd
1809 * allocation (PTRS_PER_PGD).
1810 */
Will Deacon4d09d992014-07-23 13:20:43 +01001811 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001812#ifdef CONFIG_64BIT
Will Deacon4d09d992014-07-23 13:20:43 +01001813 smmu->s1_output_size = min_t(unsigned long, VA_BITS, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001814#else
Will Deacon4d09d992014-07-23 13:20:43 +01001815 smmu->s1_output_size = min(32UL, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001816#endif
Will Deacon4d09d992014-07-23 13:20:43 +01001817 } else {
1818 smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT,
1819 size);
1820 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001821
1822 /* The stage-2 output mask is also applied for bypass */
1823 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001824 smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001825
1826 if (smmu->version == 1) {
1827 smmu->input_size = 32;
1828 } else {
1829#ifdef CONFIG_64BIT
1830 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon06f983d2013-11-05 15:55:04 +00001831 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001832#else
1833 size = 32;
1834#endif
1835 smmu->input_size = size;
1836
1837 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1838 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1839 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1840 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1841 PAGE_SIZE);
1842 return -ENODEV;
1843 }
1844 }
1845
1846 dev_notice(smmu->dev,
1847 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
Mitchel Humpherys29073202014-07-08 09:52:18 -07001848 smmu->input_size, smmu->s1_output_size,
1849 smmu->s2_output_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001850 return 0;
1851}
1852
1853static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1854{
1855 struct resource *res;
1856 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001857 struct device *dev = &pdev->dev;
1858 struct rb_node *node;
1859 struct of_phandle_args masterspec;
1860 int num_irqs, i, err;
1861
1862 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1863 if (!smmu) {
1864 dev_err(dev, "failed to allocate arm_smmu_device\n");
1865 return -ENOMEM;
1866 }
1867 smmu->dev = dev;
1868
1869 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001870 smmu->base = devm_ioremap_resource(dev, res);
1871 if (IS_ERR(smmu->base))
1872 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001873 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001874
1875 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1876 &smmu->num_global_irqs)) {
1877 dev_err(dev, "missing #global-interrupts property\n");
1878 return -ENODEV;
1879 }
1880
1881 num_irqs = 0;
1882 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1883 num_irqs++;
1884 if (num_irqs > smmu->num_global_irqs)
1885 smmu->num_context_irqs++;
1886 }
1887
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001888 if (!smmu->num_context_irqs) {
1889 dev_err(dev, "found %d interrupts but expected at least %d\n",
1890 num_irqs, smmu->num_global_irqs + 1);
1891 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001892 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001893
1894 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1895 GFP_KERNEL);
1896 if (!smmu->irqs) {
1897 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1898 return -ENOMEM;
1899 }
1900
1901 for (i = 0; i < num_irqs; ++i) {
1902 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001903
Will Deacon45ae7cf2013-06-24 18:31:25 +01001904 if (irq < 0) {
1905 dev_err(dev, "failed to get irq index %d\n", i);
1906 return -ENODEV;
1907 }
1908 smmu->irqs[i] = irq;
1909 }
1910
Olav Haugan3c8766d2014-08-22 17:12:32 -07001911 err = arm_smmu_device_cfg_probe(smmu);
1912 if (err)
1913 return err;
1914
Will Deacon45ae7cf2013-06-24 18:31:25 +01001915 i = 0;
1916 smmu->masters = RB_ROOT;
1917 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1918 "#stream-id-cells", i,
1919 &masterspec)) {
1920 err = register_smmu_master(smmu, dev, &masterspec);
1921 if (err) {
1922 dev_err(dev, "failed to add master %s\n",
1923 masterspec.np->name);
1924 goto out_put_masters;
1925 }
1926
1927 i++;
1928 }
1929 dev_notice(dev, "registered %d master devices\n", i);
1930
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001931 parse_driver_options(smmu);
1932
Will Deacon45ae7cf2013-06-24 18:31:25 +01001933 if (smmu->version > 1 &&
1934 smmu->num_context_banks != smmu->num_context_irqs) {
1935 dev_err(dev,
1936 "found only %d context interrupt(s) but %d required\n",
1937 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001938 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001939 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001940 }
1941
Will Deacon45ae7cf2013-06-24 18:31:25 +01001942 for (i = 0; i < smmu->num_global_irqs; ++i) {
1943 err = request_irq(smmu->irqs[i],
1944 arm_smmu_global_fault,
1945 IRQF_SHARED,
1946 "arm-smmu global fault",
1947 smmu);
1948 if (err) {
1949 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1950 i, smmu->irqs[i]);
1951 goto out_free_irqs;
1952 }
1953 }
1954
1955 INIT_LIST_HEAD(&smmu->list);
1956 spin_lock(&arm_smmu_devices_lock);
1957 list_add(&smmu->list, &arm_smmu_devices);
1958 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001959
1960 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001961 return 0;
1962
1963out_free_irqs:
1964 while (i--)
1965 free_irq(smmu->irqs[i], smmu);
1966
Will Deacon45ae7cf2013-06-24 18:31:25 +01001967out_put_masters:
1968 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001969 struct arm_smmu_master *master
1970 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001971 of_node_put(master->of_node);
1972 }
1973
1974 return err;
1975}
1976
1977static int arm_smmu_device_remove(struct platform_device *pdev)
1978{
1979 int i;
1980 struct device *dev = &pdev->dev;
1981 struct arm_smmu_device *curr, *smmu = NULL;
1982 struct rb_node *node;
1983
1984 spin_lock(&arm_smmu_devices_lock);
1985 list_for_each_entry(curr, &arm_smmu_devices, list) {
1986 if (curr->dev == dev) {
1987 smmu = curr;
1988 list_del(&smmu->list);
1989 break;
1990 }
1991 }
1992 spin_unlock(&arm_smmu_devices_lock);
1993
1994 if (!smmu)
1995 return -ENODEV;
1996
Will Deacon45ae7cf2013-06-24 18:31:25 +01001997 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001998 struct arm_smmu_master *master
1999 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002000 of_node_put(master->of_node);
2001 }
2002
Will Deaconecfadb62013-07-31 19:21:28 +01002003 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002004 dev_err(dev, "removing device with active domains!\n");
2005
2006 for (i = 0; i < smmu->num_global_irqs; ++i)
2007 free_irq(smmu->irqs[i], smmu);
2008
2009 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07002010 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002011 return 0;
2012}
2013
2014#ifdef CONFIG_OF
2015static struct of_device_id arm_smmu_of_match[] = {
2016 { .compatible = "arm,smmu-v1", },
2017 { .compatible = "arm,smmu-v2", },
2018 { .compatible = "arm,mmu-400", },
2019 { .compatible = "arm,mmu-500", },
2020 { },
2021};
2022MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2023#endif
2024
2025static struct platform_driver arm_smmu_driver = {
2026 .driver = {
2027 .owner = THIS_MODULE,
2028 .name = "arm-smmu",
2029 .of_match_table = of_match_ptr(arm_smmu_of_match),
2030 },
2031 .probe = arm_smmu_device_dt_probe,
2032 .remove = arm_smmu_device_remove,
2033};
2034
2035static int __init arm_smmu_init(void)
2036{
2037 int ret;
2038
2039 ret = platform_driver_register(&arm_smmu_driver);
2040 if (ret)
2041 return ret;
2042
2043 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01002044 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002045 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2046
Will Deacond123cf82014-02-04 22:17:53 +00002047#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01002048 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002049 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00002050#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01002051
Will Deacona9a1b0b2014-05-01 18:05:08 +01002052#ifdef CONFIG_PCI
2053 if (!iommu_present(&pci_bus_type))
2054 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2055#endif
2056
Will Deacon45ae7cf2013-06-24 18:31:25 +01002057 return 0;
2058}
2059
2060static void __exit arm_smmu_exit(void)
2061{
2062 return platform_driver_unregister(&arm_smmu_driver);
2063}
2064
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002065subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002066module_exit(arm_smmu_exit);
2067
2068MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2069MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2070MODULE_LICENSE("GPL v2");