blob: 38f8d670afb3e4fc01d77ec7cf74b805ee18fee6 [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
Will Deacon4cf740b2014-07-14 19:47:39 +0100329static int force_stage;
330module_param_named(force_stage, force_stage, int, S_IRUGO | S_IWUSR);
331MODULE_PARM_DESC(force_stage,
332 "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
333
Will Deacon45ae7cf2013-06-24 18:31:25 +0100334struct arm_smmu_smr {
335 u8 idx;
336 u16 mask;
337 u16 id;
338};
339
Will Deacona9a1b0b2014-05-01 18:05:08 +0100340struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100341 int num_streamids;
342 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100343 struct arm_smmu_smr *smrs;
344};
345
Will Deacona9a1b0b2014-05-01 18:05:08 +0100346struct arm_smmu_master {
347 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100348 struct rb_node node;
349 struct arm_smmu_master_cfg cfg;
350};
351
Will Deacon45ae7cf2013-06-24 18:31:25 +0100352struct arm_smmu_device {
353 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100354
355 void __iomem *base;
356 unsigned long size;
357 unsigned long pagesize;
358
359#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
360#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
361#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
362#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
363#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
364 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000365
366#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
367 u32 options;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100368 int version;
369
370 u32 num_context_banks;
371 u32 num_s2_context_banks;
372 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
373 atomic_t irptndx;
374
375 u32 num_mapping_groups;
376 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
377
378 unsigned long input_size;
379 unsigned long s1_output_size;
380 unsigned long s2_output_size;
381
382 u32 num_global_irqs;
383 u32 num_context_irqs;
384 unsigned int *irqs;
385
Will Deacon45ae7cf2013-06-24 18:31:25 +0100386 struct list_head list;
387 struct rb_root masters;
388};
389
390struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100391 u8 cbndx;
392 u8 irptndx;
393 u32 cbar;
394 pgd_t *pgd;
395};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100396#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100397
Will Deaconecfadb62013-07-31 19:21:28 +0100398#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
399#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
400
Will Deacon45ae7cf2013-06-24 18:31:25 +0100401struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100402 struct arm_smmu_device *smmu;
403 struct arm_smmu_cfg cfg;
Will Deaconc9d09e22014-02-04 22:12:42 +0000404 spinlock_t lock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100405};
406
407static DEFINE_SPINLOCK(arm_smmu_devices_lock);
408static LIST_HEAD(arm_smmu_devices);
409
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000410struct arm_smmu_option_prop {
411 u32 opt;
412 const char *prop;
413};
414
Mitchel Humpherys29073202014-07-08 09:52:18 -0700415static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000416 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
417 { 0, NULL},
418};
419
420static void parse_driver_options(struct arm_smmu_device *smmu)
421{
422 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700423
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000424 do {
425 if (of_property_read_bool(smmu->dev->of_node,
426 arm_smmu_options[i].prop)) {
427 smmu->options |= arm_smmu_options[i].opt;
428 dev_notice(smmu->dev, "option %s\n",
429 arm_smmu_options[i].prop);
430 }
431 } while (arm_smmu_options[++i].opt);
432}
433
Will Deacon8f68f8e2014-07-15 11:27:08 +0100434static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100435{
436 if (dev_is_pci(dev)) {
437 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700438
Will Deacona9a1b0b2014-05-01 18:05:08 +0100439 while (!pci_is_root_bus(bus))
440 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100441 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100442 }
443
Will Deacon8f68f8e2014-07-15 11:27:08 +0100444 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100445}
446
Will Deacon45ae7cf2013-06-24 18:31:25 +0100447static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
448 struct device_node *dev_node)
449{
450 struct rb_node *node = smmu->masters.rb_node;
451
452 while (node) {
453 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700454
Will Deacon45ae7cf2013-06-24 18:31:25 +0100455 master = container_of(node, struct arm_smmu_master, node);
456
457 if (dev_node < master->of_node)
458 node = node->rb_left;
459 else if (dev_node > master->of_node)
460 node = node->rb_right;
461 else
462 return master;
463 }
464
465 return NULL;
466}
467
Will Deacona9a1b0b2014-05-01 18:05:08 +0100468static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100469find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100470{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100471 struct arm_smmu_master_cfg *cfg = NULL;
472 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100473
Will Deacon8f68f8e2014-07-15 11:27:08 +0100474 if (group) {
475 cfg = iommu_group_get_iommudata(group);
476 iommu_group_put(group);
477 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100478
Will Deacon8f68f8e2014-07-15 11:27:08 +0100479 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100480}
481
Will Deacon45ae7cf2013-06-24 18:31:25 +0100482static int insert_smmu_master(struct arm_smmu_device *smmu,
483 struct arm_smmu_master *master)
484{
485 struct rb_node **new, *parent;
486
487 new = &smmu->masters.rb_node;
488 parent = NULL;
489 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700490 struct arm_smmu_master *this
491 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100492
493 parent = *new;
494 if (master->of_node < this->of_node)
495 new = &((*new)->rb_left);
496 else if (master->of_node > this->of_node)
497 new = &((*new)->rb_right);
498 else
499 return -EEXIST;
500 }
501
502 rb_link_node(&master->node, parent, new);
503 rb_insert_color(&master->node, &smmu->masters);
504 return 0;
505}
506
507static int register_smmu_master(struct arm_smmu_device *smmu,
508 struct device *dev,
509 struct of_phandle_args *masterspec)
510{
511 int i;
512 struct arm_smmu_master *master;
513
514 master = find_smmu_master(smmu, masterspec->np);
515 if (master) {
516 dev_err(dev,
517 "rejecting multiple registrations for master device %s\n",
518 masterspec->np->name);
519 return -EBUSY;
520 }
521
522 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
523 dev_err(dev,
524 "reached maximum number (%d) of stream IDs for master device %s\n",
525 MAX_MASTER_STREAMIDS, masterspec->np->name);
526 return -ENOSPC;
527 }
528
529 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
530 if (!master)
531 return -ENOMEM;
532
Will Deacona9a1b0b2014-05-01 18:05:08 +0100533 master->of_node = masterspec->np;
534 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100535
Olav Haugan3c8766d2014-08-22 17:12:32 -0700536 for (i = 0; i < master->cfg.num_streamids; ++i) {
537 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100538
Olav Haugan3c8766d2014-08-22 17:12:32 -0700539 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
540 (streamid >= smmu->num_mapping_groups)) {
541 dev_err(dev,
542 "stream ID for master device %s greater than maximum allowed (%d)\n",
543 masterspec->np->name, smmu->num_mapping_groups);
544 return -ERANGE;
545 }
546 master->cfg.streamids[i] = streamid;
547 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100548 return insert_smmu_master(smmu, master);
549}
550
Will Deacon44680ee2014-06-25 11:29:12 +0100551static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100552{
Will Deacon44680ee2014-06-25 11:29:12 +0100553 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100554 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100555 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100556
557 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100558 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100559 master = find_smmu_master(smmu, dev_node);
560 if (master)
561 break;
562 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100563 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100564
Will Deacona9a1b0b2014-05-01 18:05:08 +0100565 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100566}
567
568static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
569{
570 int idx;
571
572 do {
573 idx = find_next_zero_bit(map, end, start);
574 if (idx == end)
575 return -ENOSPC;
576 } while (test_and_set_bit(idx, map));
577
578 return idx;
579}
580
581static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
582{
583 clear_bit(idx, map);
584}
585
586/* Wait for any pending TLB invalidations to complete */
587static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
588{
589 int count = 0;
590 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
591
592 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
593 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
594 & sTLBGSTATUS_GSACTIVE) {
595 cpu_relax();
596 if (++count == TLB_LOOP_TIMEOUT) {
597 dev_err_ratelimited(smmu->dev,
598 "TLB sync timed out -- SMMU may be deadlocked\n");
599 return;
600 }
601 udelay(1);
602 }
603}
604
Will Deacon44680ee2014-06-25 11:29:12 +0100605static void arm_smmu_tlb_inv_context(struct arm_smmu_domain *smmu_domain)
Will Deacon1463fe42013-07-31 19:21:27 +0100606{
Will Deacon44680ee2014-06-25 11:29:12 +0100607 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
608 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100609 void __iomem *base = ARM_SMMU_GR0(smmu);
610 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
611
612 if (stage1) {
613 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100614 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
615 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100616 } else {
617 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100618 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
619 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100620 }
621
622 arm_smmu_tlb_sync(smmu);
623}
624
Will Deacon45ae7cf2013-06-24 18:31:25 +0100625static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
626{
627 int flags, ret;
628 u32 fsr, far, fsynr, resume;
629 unsigned long iova;
630 struct iommu_domain *domain = dev;
631 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100632 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
633 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100634 void __iomem *cb_base;
635
Will Deacon44680ee2014-06-25 11:29:12 +0100636 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100637 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
638
639 if (!(fsr & FSR_FAULT))
640 return IRQ_NONE;
641
642 if (fsr & FSR_IGN)
643 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100644 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100645 fsr);
646
647 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
648 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
649
650 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
651 iova = far;
652#ifdef CONFIG_64BIT
653 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
654 iova |= ((unsigned long)far << 32);
655#endif
656
657 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
658 ret = IRQ_HANDLED;
659 resume = RESUME_RETRY;
660 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100661 dev_err_ratelimited(smmu->dev,
662 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100663 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100664 ret = IRQ_NONE;
665 resume = RESUME_TERMINATE;
666 }
667
668 /* Clear the faulting FSR */
669 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
670
671 /* Retry or terminate any stalled transactions */
672 if (fsr & FSR_SS)
673 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
674
675 return ret;
676}
677
678static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
679{
680 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
681 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000682 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100683
684 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
685 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
686 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
687 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
688
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000689 if (!gfsr)
690 return IRQ_NONE;
691
Will Deacon45ae7cf2013-06-24 18:31:25 +0100692 dev_err_ratelimited(smmu->dev,
693 "Unexpected global fault, this could be serious\n");
694 dev_err_ratelimited(smmu->dev,
695 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
696 gfsr, gfsynr0, gfsynr1, gfsynr2);
697
698 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100699 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100700}
701
Will Deacon6dd35f42014-02-05 17:49:34 +0000702static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
703 size_t size)
704{
705 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
706
707
708 /* Ensure new page tables are visible to the hardware walker */
709 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
Will Deacon3aa80ea2014-02-05 23:35:47 +0000710 dsb(ishst);
Will Deacon6dd35f42014-02-05 17:49:34 +0000711 } else {
712 /*
713 * If the SMMU can't walk tables in the CPU caches, treat them
714 * like non-coherent DMA since we need to flush the new entries
715 * all the way out to memory. There's no possibility of
716 * recursion here as the SMMU table walker will not be wired
717 * through another SMMU.
718 */
719 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
720 DMA_TO_DEVICE);
721 }
722}
723
Will Deacon45ae7cf2013-06-24 18:31:25 +0100724static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
725{
726 u32 reg;
727 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100728 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
729 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100730 void __iomem *cb_base, *gr0_base, *gr1_base;
731
732 gr0_base = ARM_SMMU_GR0(smmu);
733 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100734 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
735 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100736
737 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100738 reg = cfg->cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100739 if (smmu->version == 1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700740 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100741
Will Deacon57ca90f2014-02-06 14:59:05 +0000742 /*
743 * Use the weakest shareability/memory types, so they are
744 * overridden by the ttbcr/pte.
745 */
746 if (stage1) {
747 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
748 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
749 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100750 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000751 }
Will Deacon44680ee2014-06-25 11:29:12 +0100752 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100753
754 if (smmu->version > 1) {
755 /* CBA2R */
756#ifdef CONFIG_64BIT
757 reg = CBA2R_RW64_64BIT;
758#else
759 reg = CBA2R_RW64_32BIT;
760#endif
761 writel_relaxed(reg,
Will Deacon44680ee2014-06-25 11:29:12 +0100762 gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100763
764 /* TTBCR2 */
765 switch (smmu->input_size) {
766 case 32:
767 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
768 break;
769 case 36:
770 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
771 break;
772 case 39:
Will Deacon4d09d992014-07-23 13:20:43 +0100773 case 40:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100774 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
775 break;
776 case 42:
777 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
778 break;
779 case 44:
780 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
781 break;
782 case 48:
783 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
784 break;
785 }
786
787 switch (smmu->s1_output_size) {
788 case 32:
789 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
790 break;
791 case 36:
792 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
793 break;
794 case 39:
Will Deacon4d09d992014-07-23 13:20:43 +0100795 case 40:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100796 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
797 break;
798 case 42:
799 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
800 break;
801 case 44:
802 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
803 break;
804 case 48:
805 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
806 break;
807 }
808
809 if (stage1)
810 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
811 }
812
813 /* TTBR0 */
Will Deacon44680ee2014-06-25 11:29:12 +0100814 arm_smmu_flush_pgtable(smmu, cfg->pgd,
Will Deacon6dd35f42014-02-05 17:49:34 +0000815 PTRS_PER_PGD * sizeof(pgd_t));
Will Deacon44680ee2014-06-25 11:29:12 +0100816 reg = __pa(cfg->pgd);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100817 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
Will Deacon44680ee2014-06-25 11:29:12 +0100818 reg = (phys_addr_t)__pa(cfg->pgd) >> 32;
Will Deacon1463fe42013-07-31 19:21:27 +0100819 if (stage1)
Will Deacon44680ee2014-06-25 11:29:12 +0100820 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100821 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100822
823 /*
824 * TTBCR
825 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
826 */
827 if (smmu->version > 1) {
828 if (PAGE_SIZE == SZ_4K)
829 reg = TTBCR_TG0_4K;
830 else
831 reg = TTBCR_TG0_64K;
832
833 if (!stage1) {
Will Deacona65217a2014-06-24 18:26:26 +0100834 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
835
Will Deacon45ae7cf2013-06-24 18:31:25 +0100836 switch (smmu->s2_output_size) {
837 case 32:
838 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
839 break;
840 case 36:
841 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
842 break;
843 case 40:
844 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
845 break;
846 case 42:
847 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
848 break;
849 case 44:
850 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
851 break;
852 case 48:
853 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
854 break;
855 }
856 } else {
Will Deacona65217a2014-06-24 18:26:26 +0100857 reg |= (64 - smmu->input_size) << TTBCR_T0SZ_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100858 }
859 } else {
860 reg = 0;
861 }
862
863 reg |= TTBCR_EAE |
864 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
865 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
Olav Haugan1fc870c2014-08-04 19:01:02 +0100866 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT);
867
868 if (!stage1)
869 reg |= (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
870
Will Deacon45ae7cf2013-06-24 18:31:25 +0100871 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
872
873 /* MAIR0 (stage-1 only) */
874 if (stage1) {
875 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
876 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
877 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
878 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
879 }
880
Will Deacon45ae7cf2013-06-24 18:31:25 +0100881 /* SCTLR */
882 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
883 if (stage1)
884 reg |= SCTLR_S1_ASIDPNE;
885#ifdef __BIG_ENDIAN
886 reg |= SCTLR_E;
887#endif
Will Deacon25724842013-08-21 13:49:53 +0100888 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100889}
890
891static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100892 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100893{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100894 int irq, start, ret = 0;
895 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100896 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100897 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100898
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100899 spin_lock_irqsave(&smmu_domain->lock, flags);
900 if (smmu_domain->smmu)
901 goto out_unlock;
902
Will Deacon45ae7cf2013-06-24 18:31:25 +0100903 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
904 /*
905 * We will likely want to change this if/when KVM gets
906 * involved.
907 */
Will Deacon44680ee2014-06-25 11:29:12 +0100908 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100909 start = smmu->num_s2_context_banks;
Will Deacon9c5c92e2014-06-25 12:12:41 +0100910 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100911 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100912 start = smmu->num_s2_context_banks;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100913 } else {
Will Deacon9c5c92e2014-06-25 12:12:41 +0100914 cfg->cbar = CBAR_TYPE_S2_TRANS;
915 start = 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100916 }
917
918 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
919 smmu->num_context_banks);
920 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100921 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100922
Will Deacon44680ee2014-06-25 11:29:12 +0100923 cfg->cbndx = ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100924 if (smmu->version == 1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100925 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
926 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100927 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100928 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100929 }
930
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100931 ACCESS_ONCE(smmu_domain->smmu) = smmu;
932 arm_smmu_init_context_bank(smmu_domain);
933 spin_unlock_irqrestore(&smmu_domain->lock, flags);
934
Will Deacon44680ee2014-06-25 11:29:12 +0100935 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100936 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
937 "arm-smmu-context-fault", domain);
938 if (IS_ERR_VALUE(ret)) {
939 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100940 cfg->irptndx, irq);
941 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100942 }
943
Will Deacona9a1b0b2014-05-01 18:05:08 +0100944 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100945
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100946out_unlock:
947 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100948 return ret;
949}
950
951static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
952{
953 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100954 struct arm_smmu_device *smmu = smmu_domain->smmu;
955 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100956 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100957 int irq;
958
959 if (!smmu)
960 return;
961
Will Deacon1463fe42013-07-31 19:21:27 +0100962 /* Disable the context bank and nuke the TLB before freeing it. */
Will Deacon44680ee2014-06-25 11:29:12 +0100963 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100964 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon44680ee2014-06-25 11:29:12 +0100965 arm_smmu_tlb_inv_context(smmu_domain);
Will Deacon1463fe42013-07-31 19:21:27 +0100966
Will Deacon44680ee2014-06-25 11:29:12 +0100967 if (cfg->irptndx != INVALID_IRPTNDX) {
968 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100969 free_irq(irq, domain);
970 }
971
Will Deacon44680ee2014-06-25 11:29:12 +0100972 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100973}
974
975static int arm_smmu_domain_init(struct iommu_domain *domain)
976{
977 struct arm_smmu_domain *smmu_domain;
978 pgd_t *pgd;
979
980 /*
981 * Allocate the domain and initialise some of its data structures.
982 * We can't really do anything meaningful until we've added a
983 * master.
984 */
985 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
986 if (!smmu_domain)
987 return -ENOMEM;
988
Mitchel Humpherys29073202014-07-08 09:52:18 -0700989 pgd = kcalloc(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100990 if (!pgd)
991 goto out_free_domain;
Will Deacon44680ee2014-06-25 11:29:12 +0100992 smmu_domain->cfg.pgd = pgd;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100993
Will Deaconc9d09e22014-02-04 22:12:42 +0000994 spin_lock_init(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100995 domain->priv = smmu_domain;
996 return 0;
997
998out_free_domain:
999 kfree(smmu_domain);
1000 return -ENOMEM;
1001}
1002
1003static void arm_smmu_free_ptes(pmd_t *pmd)
1004{
1005 pgtable_t table = pmd_pgtable(*pmd);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001006
Will Deacon45ae7cf2013-06-24 18:31:25 +01001007 __free_page(table);
1008}
1009
1010static void arm_smmu_free_pmds(pud_t *pud)
1011{
1012 int i;
1013 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
1014
1015 pmd = pmd_base;
1016 for (i = 0; i < PTRS_PER_PMD; ++i) {
1017 if (pmd_none(*pmd))
1018 continue;
1019
1020 arm_smmu_free_ptes(pmd);
1021 pmd++;
1022 }
1023
1024 pmd_free(NULL, pmd_base);
1025}
1026
1027static void arm_smmu_free_puds(pgd_t *pgd)
1028{
1029 int i;
1030 pud_t *pud, *pud_base = pud_offset(pgd, 0);
1031
1032 pud = pud_base;
1033 for (i = 0; i < PTRS_PER_PUD; ++i) {
1034 if (pud_none(*pud))
1035 continue;
1036
1037 arm_smmu_free_pmds(pud);
1038 pud++;
1039 }
1040
1041 pud_free(NULL, pud_base);
1042}
1043
1044static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
1045{
1046 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001047 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1048 pgd_t *pgd, *pgd_base = cfg->pgd;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001049
1050 /*
1051 * Recursively free the page tables for this domain. We don't
Will Deacon34fb4b32014-02-26 11:14:37 +00001052 * care about speculative TLB filling because the tables should
1053 * not be active in any context bank at this point (SCTLR.M is 0).
Will Deacon45ae7cf2013-06-24 18:31:25 +01001054 */
1055 pgd = pgd_base;
1056 for (i = 0; i < PTRS_PER_PGD; ++i) {
1057 if (pgd_none(*pgd))
1058 continue;
1059 arm_smmu_free_puds(pgd);
1060 pgd++;
1061 }
1062
1063 kfree(pgd_base);
1064}
1065
1066static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1067{
1068 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001069
1070 /*
1071 * Free the domain resources. We assume that all devices have
1072 * already been detached.
1073 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001074 arm_smmu_destroy_domain_context(domain);
1075 arm_smmu_free_pgtables(smmu_domain);
1076 kfree(smmu_domain);
1077}
1078
1079static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001080 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001081{
1082 int i;
1083 struct arm_smmu_smr *smrs;
1084 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1085
1086 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1087 return 0;
1088
Will Deacona9a1b0b2014-05-01 18:05:08 +01001089 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001090 return -EEXIST;
1091
Mitchel Humpherys29073202014-07-08 09:52:18 -07001092 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001093 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001094 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1095 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001096 return -ENOMEM;
1097 }
1098
Will Deacon44680ee2014-06-25 11:29:12 +01001099 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001100 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001101 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1102 smmu->num_mapping_groups);
1103 if (IS_ERR_VALUE(idx)) {
1104 dev_err(smmu->dev, "failed to allocate free SMR\n");
1105 goto err_free_smrs;
1106 }
1107
1108 smrs[i] = (struct arm_smmu_smr) {
1109 .idx = idx,
1110 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001111 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001112 };
1113 }
1114
1115 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001116 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001117 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1118 smrs[i].mask << SMR_MASK_SHIFT;
1119 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1120 }
1121
Will Deacona9a1b0b2014-05-01 18:05:08 +01001122 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001123 return 0;
1124
1125err_free_smrs:
1126 while (--i >= 0)
1127 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1128 kfree(smrs);
1129 return -ENOSPC;
1130}
1131
1132static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001133 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001134{
1135 int i;
1136 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001137 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001138
Will Deacon43b412b2014-07-15 11:22:24 +01001139 if (!smrs)
1140 return;
1141
Will Deacon45ae7cf2013-06-24 18:31:25 +01001142 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001143 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001144 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001145
Will Deacon45ae7cf2013-06-24 18:31:25 +01001146 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1147 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1148 }
1149
Will Deacona9a1b0b2014-05-01 18:05:08 +01001150 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001151 kfree(smrs);
1152}
1153
Will Deacon45ae7cf2013-06-24 18:31:25 +01001154static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001155 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001156{
1157 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001158 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001159 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1160
Will Deacon8f68f8e2014-07-15 11:27:08 +01001161 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001162 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001163 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001164 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001165
Will Deacona9a1b0b2014-05-01 18:05:08 +01001166 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001167 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001168
Will Deacona9a1b0b2014-05-01 18:05:08 +01001169 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001170 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001171 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001172 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1173 }
1174
1175 return 0;
1176}
1177
1178static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001179 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001180{
Will Deacon43b412b2014-07-15 11:22:24 +01001181 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001182 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001183 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001184
Will Deacon8f68f8e2014-07-15 11:27:08 +01001185 /* An IOMMU group is torn down by the first device to be removed */
1186 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1187 return;
1188
Will Deacon45ae7cf2013-06-24 18:31:25 +01001189 /*
1190 * We *must* clear the S2CR first, because freeing the SMR means
1191 * that it can be re-allocated immediately.
1192 */
Will Deacon43b412b2014-07-15 11:22:24 +01001193 for (i = 0; i < cfg->num_streamids; ++i) {
1194 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1195
1196 writel_relaxed(S2CR_TYPE_BYPASS,
1197 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1198 }
1199
Will Deacona9a1b0b2014-05-01 18:05:08 +01001200 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001201}
1202
1203static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1204{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001205 int ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001206 struct arm_smmu_domain *smmu_domain = domain->priv;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001207 struct arm_smmu_device *smmu, *dom_smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001208 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001209
Will Deacon8f68f8e2014-07-15 11:27:08 +01001210 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001211 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001212 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1213 return -ENXIO;
1214 }
1215
1216 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001217 * Sanity check the domain. We don't support domains across
1218 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001219 */
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001220 dom_smmu = ACCESS_ONCE(smmu_domain->smmu);
1221 if (!dom_smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001222 /* Now that we have a master, we can finalise the domain */
Will Deacon44680ee2014-06-25 11:29:12 +01001223 ret = arm_smmu_init_domain_context(domain, smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001224 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001225 return ret;
1226
1227 dom_smmu = smmu_domain->smmu;
1228 }
1229
1230 if (dom_smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001231 dev_err(dev,
1232 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001233 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1234 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001235 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001236
1237 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001238 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001239 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001240 return -ENODEV;
1241
Will Deacona9a1b0b2014-05-01 18:05:08 +01001242 return arm_smmu_domain_add_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001243}
1244
1245static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1246{
1247 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001248 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001249
Will Deacon8f68f8e2014-07-15 11:27:08 +01001250 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001251 if (cfg)
1252 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001253}
1254
Will Deacon45ae7cf2013-06-24 18:31:25 +01001255static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1256 unsigned long end)
1257{
1258 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1259 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1260}
1261
1262static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1263 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001264 unsigned long pfn, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001265{
1266 pte_t *pte, *start;
Will Deaconcf2d45b2013-11-05 16:32:00 +00001267 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001268
1269 if (pmd_none(*pmd)) {
1270 /* Allocate a new set of tables */
Will Deaconc9d09e22014-02-04 22:12:42 +00001271 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001272
Will Deacon45ae7cf2013-06-24 18:31:25 +01001273 if (!table)
1274 return -ENOMEM;
1275
Will Deacon6dd35f42014-02-05 17:49:34 +00001276 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001277 pmd_populate(NULL, pmd, table);
1278 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1279 }
1280
1281 if (stage == 1) {
Will Deacon1463fe42013-07-31 19:21:27 +01001282 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
Will Deaconb410aed2014-02-20 16:31:06 +00001283 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001284 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1285
Will Deaconb410aed2014-02-20 16:31:06 +00001286 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001287 pteval |= (MAIR_ATTR_IDX_CACHE <<
1288 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1289 } else {
1290 pteval |= ARM_SMMU_PTE_HAP_FAULT;
Will Deaconb410aed2014-02-20 16:31:06 +00001291 if (prot & IOMMU_READ)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001292 pteval |= ARM_SMMU_PTE_HAP_READ;
Will Deaconb410aed2014-02-20 16:31:06 +00001293 if (prot & IOMMU_WRITE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001294 pteval |= ARM_SMMU_PTE_HAP_WRITE;
Will Deaconb410aed2014-02-20 16:31:06 +00001295 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001296 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1297 else
1298 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1299 }
1300
1301 /* If no access, create a faulting entry to avoid TLB fills */
Will Deaconb410aed2014-02-20 16:31:06 +00001302 if (prot & IOMMU_EXEC)
Will Deaconcf2d45b2013-11-05 16:32:00 +00001303 pteval &= ~ARM_SMMU_PTE_XN;
Will Deaconb410aed2014-02-20 16:31:06 +00001304 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001305 pteval &= ~ARM_SMMU_PTE_PAGE;
1306
1307 pteval |= ARM_SMMU_PTE_SH_IS;
1308 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1309 pte = start;
1310
1311 /*
1312 * Install the page table entries. This is fairly complicated
1313 * since we attempt to make use of the contiguous hint in the
1314 * ptes where possible. The contiguous hint indicates a series
1315 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1316 * contiguous region with the following constraints:
1317 *
1318 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1319 * - Each pte in the region has the contiguous hint bit set
1320 *
1321 * This complicates unmapping (also handled by this code, when
1322 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1323 * possible, yet highly unlikely, that a client may unmap only
1324 * part of a contiguous range. This requires clearing of the
1325 * contiguous hint bits in the range before installing the new
1326 * faulting entries.
1327 *
1328 * Note that re-mapping an address range without first unmapping
1329 * it is not supported, so TLB invalidation is not required here
1330 * and is instead performed at unmap and domain-init time.
1331 */
1332 do {
1333 int i = 1;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001334
Will Deacon45ae7cf2013-06-24 18:31:25 +01001335 pteval &= ~ARM_SMMU_PTE_CONT;
1336
1337 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1338 i = ARM_SMMU_PTE_CONT_ENTRIES;
1339 pteval |= ARM_SMMU_PTE_CONT;
1340 } else if (pte_val(*pte) &
1341 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1342 int j;
1343 pte_t *cont_start;
1344 unsigned long idx = pte_index(addr);
1345
1346 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1347 cont_start = pmd_page_vaddr(*pmd) + idx;
1348 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001349 pte_val(*(cont_start + j)) &=
1350 ~ARM_SMMU_PTE_CONT;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001351
1352 arm_smmu_flush_pgtable(smmu, cont_start,
1353 sizeof(*pte) *
1354 ARM_SMMU_PTE_CONT_ENTRIES);
1355 }
1356
1357 do {
1358 *pte = pfn_pte(pfn, __pgprot(pteval));
1359 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1360 } while (addr != end);
1361
1362 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1363 return 0;
1364}
1365
1366static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1367 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001368 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001369{
1370 int ret;
1371 pmd_t *pmd;
1372 unsigned long next, pfn = __phys_to_pfn(phys);
1373
1374#ifndef __PAGETABLE_PMD_FOLDED
1375 if (pud_none(*pud)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001376 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001377 if (!pmd)
1378 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001379
Will Deacon6dd35f42014-02-05 17:49:34 +00001380 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001381 pud_populate(NULL, pud, pmd);
1382 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1383
1384 pmd += pmd_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001385 } else
1386#endif
1387 pmd = pmd_offset(pud, addr);
1388
1389 do {
1390 next = pmd_addr_end(addr, end);
Bin Wangaca1bc42014-03-21 10:06:07 +00001391 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn,
Will Deaconb410aed2014-02-20 16:31:06 +00001392 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001393 phys += next - addr;
1394 } while (pmd++, addr = next, addr < end);
1395
1396 return ret;
1397}
1398
1399static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1400 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001401 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001402{
1403 int ret = 0;
1404 pud_t *pud;
1405 unsigned long next;
1406
1407#ifndef __PAGETABLE_PUD_FOLDED
1408 if (pgd_none(*pgd)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001409 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001410 if (!pud)
1411 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001412
Will Deacon6dd35f42014-02-05 17:49:34 +00001413 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001414 pgd_populate(NULL, pgd, pud);
1415 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1416
1417 pud += pud_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001418 } else
1419#endif
1420 pud = pud_offset(pgd, addr);
1421
1422 do {
1423 next = pud_addr_end(addr, end);
1424 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
Will Deaconb410aed2014-02-20 16:31:06 +00001425 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001426 phys += next - addr;
1427 } while (pud++, addr = next, addr < end);
1428
1429 return ret;
1430}
1431
1432static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1433 unsigned long iova, phys_addr_t paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001434 size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001435{
1436 int ret, stage;
1437 unsigned long end;
1438 phys_addr_t input_mask, output_mask;
Will Deacon44680ee2014-06-25 11:29:12 +01001439 struct arm_smmu_device *smmu = smmu_domain->smmu;
1440 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1441 pgd_t *pgd = cfg->pgd;
Will Deaconb410aed2014-02-20 16:31:06 +00001442 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001443
Will Deacon44680ee2014-06-25 11:29:12 +01001444 if (cfg->cbar == CBAR_TYPE_S2_TRANS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001445 stage = 2;
1446 output_mask = (1ULL << smmu->s2_output_size) - 1;
1447 } else {
1448 stage = 1;
1449 output_mask = (1ULL << smmu->s1_output_size) - 1;
1450 }
1451
1452 if (!pgd)
1453 return -EINVAL;
1454
1455 if (size & ~PAGE_MASK)
1456 return -EINVAL;
1457
1458 input_mask = (1ULL << smmu->input_size) - 1;
1459 if ((phys_addr_t)iova & ~input_mask)
1460 return -ERANGE;
1461
1462 if (paddr & ~output_mask)
1463 return -ERANGE;
1464
Will Deaconb410aed2014-02-20 16:31:06 +00001465 spin_lock_irqsave(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001466 pgd += pgd_index(iova);
1467 end = iova + size;
1468 do {
1469 unsigned long next = pgd_addr_end(iova, end);
1470
1471 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001472 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001473 if (ret)
1474 goto out_unlock;
1475
1476 paddr += next - iova;
1477 iova = next;
1478 } while (pgd++, iova != end);
1479
1480out_unlock:
Will Deaconb410aed2014-02-20 16:31:06 +00001481 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001482
Will Deacon45ae7cf2013-06-24 18:31:25 +01001483 return ret;
1484}
1485
1486static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001487 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001488{
1489 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001490
Will Deacon5552ecd2013-11-08 15:08:06 +00001491 if (!smmu_domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001492 return -ENODEV;
1493
Will Deaconb410aed2014-02-20 16:31:06 +00001494 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, prot);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001495}
1496
1497static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1498 size_t size)
1499{
1500 int ret;
1501 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001502
1503 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
Will Deacon44680ee2014-06-25 11:29:12 +01001504 arm_smmu_tlb_inv_context(smmu_domain);
Laurent Pinchart16c50dc2014-02-28 15:37:10 +00001505 return ret ? 0 : size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001506}
1507
1508static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1509 dma_addr_t iova)
1510{
Will Deacona44a9792013-11-07 18:47:50 +00001511 pgd_t *pgdp, pgd;
1512 pud_t pud;
1513 pmd_t pmd;
1514 pte_t pte;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001515 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +01001516 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001517
Will Deacon44680ee2014-06-25 11:29:12 +01001518 pgdp = cfg->pgd;
Will Deacona44a9792013-11-07 18:47:50 +00001519 if (!pgdp)
1520 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001521
Will Deacona44a9792013-11-07 18:47:50 +00001522 pgd = *(pgdp + pgd_index(iova));
1523 if (pgd_none(pgd))
1524 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001525
Will Deacona44a9792013-11-07 18:47:50 +00001526 pud = *pud_offset(&pgd, iova);
1527 if (pud_none(pud))
1528 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001529
Will Deacona44a9792013-11-07 18:47:50 +00001530 pmd = *pmd_offset(&pud, iova);
1531 if (pmd_none(pmd))
1532 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001533
Will Deacona44a9792013-11-07 18:47:50 +00001534 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001535 if (pte_none(pte))
Will Deacona44a9792013-11-07 18:47:50 +00001536 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001537
Will Deacona44a9792013-11-07 18:47:50 +00001538 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001539}
1540
1541static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1542 unsigned long cap)
1543{
Will Deacon45ae7cf2013-06-24 18:31:25 +01001544 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacond3bca162014-07-04 11:06:01 +01001545 struct arm_smmu_device *smmu = smmu_domain->smmu;
1546 u32 features = smmu ? smmu->features : 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001547
Will Deacond0948942014-06-24 17:30:10 +01001548 switch (cap) {
1549 case IOMMU_CAP_CACHE_COHERENCY:
1550 return features & ARM_SMMU_FEAT_COHERENT_WALK;
1551 case IOMMU_CAP_INTR_REMAP:
1552 return 1; /* MSIs are just memory writes */
1553 default:
1554 return 0;
1555 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001556}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001557
Will Deacona9a1b0b2014-05-01 18:05:08 +01001558static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1559{
1560 *((u16 *)data) = alias;
1561 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001562}
1563
Will Deacon8f68f8e2014-07-15 11:27:08 +01001564static void __arm_smmu_release_pci_iommudata(void *data)
1565{
1566 kfree(data);
1567}
1568
Will Deacon45ae7cf2013-06-24 18:31:25 +01001569static int arm_smmu_add_device(struct device *dev)
1570{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001571 struct arm_smmu_device *smmu;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001572 struct arm_smmu_master_cfg *cfg;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001573 struct iommu_group *group;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001574 void (*releasefn)(void *) = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001575 int ret;
1576
Will Deacon44680ee2014-06-25 11:29:12 +01001577 smmu = find_smmu_for_device(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001578 if (!smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001579 return -ENODEV;
1580
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001581 group = iommu_group_alloc();
1582 if (IS_ERR(group)) {
1583 dev_err(dev, "Failed to allocate IOMMU group\n");
1584 return PTR_ERR(group);
1585 }
1586
Will Deacona9a1b0b2014-05-01 18:05:08 +01001587 if (dev_is_pci(dev)) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001588 struct pci_dev *pdev = to_pci_dev(dev);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001589
Will Deacona9a1b0b2014-05-01 18:05:08 +01001590 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1591 if (!cfg) {
1592 ret = -ENOMEM;
1593 goto out_put_group;
1594 }
1595
1596 cfg->num_streamids = 1;
1597 /*
1598 * Assume Stream ID == Requester ID for now.
1599 * We need a way to describe the ID mappings in FDT.
1600 */
1601 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1602 &cfg->streamids[0]);
Will Deacon8f68f8e2014-07-15 11:27:08 +01001603 releasefn = __arm_smmu_release_pci_iommudata;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001604 } else {
Will Deacon8f68f8e2014-07-15 11:27:08 +01001605 struct arm_smmu_master *master;
1606
1607 master = find_smmu_master(smmu, dev->of_node);
1608 if (!master) {
1609 ret = -ENODEV;
1610 goto out_put_group;
1611 }
1612
1613 cfg = &master->cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001614 }
1615
Will Deacon8f68f8e2014-07-15 11:27:08 +01001616 iommu_group_set_iommudata(group, cfg, releasefn);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001617 ret = iommu_group_add_device(group, dev);
1618
1619out_put_group:
1620 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001621 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001622}
1623
1624static void arm_smmu_remove_device(struct device *dev)
1625{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001626 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001627}
1628
Thierry Redingb22f6432014-06-27 09:03:12 +02001629static const struct iommu_ops arm_smmu_ops = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001630 .domain_init = arm_smmu_domain_init,
1631 .domain_destroy = arm_smmu_domain_destroy,
1632 .attach_dev = arm_smmu_attach_dev,
1633 .detach_dev = arm_smmu_detach_dev,
1634 .map = arm_smmu_map,
1635 .unmap = arm_smmu_unmap,
1636 .iova_to_phys = arm_smmu_iova_to_phys,
1637 .domain_has_cap = arm_smmu_domain_has_cap,
1638 .add_device = arm_smmu_add_device,
1639 .remove_device = arm_smmu_remove_device,
1640 .pgsize_bitmap = (SECTION_SIZE |
1641 ARM_SMMU_PTE_CONT_SIZE |
1642 PAGE_SIZE),
1643};
1644
1645static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1646{
1647 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001648 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001649 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001650 u32 reg;
1651
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001652 /* clear global FSR */
1653 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1654 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001655
1656 /* Mark all SMRn as invalid and all S2CRn as bypass */
1657 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001658 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001659 writel_relaxed(S2CR_TYPE_BYPASS,
1660 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001661 }
1662
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001663 /* Make sure all context banks are disabled and clear CB_FSR */
1664 for (i = 0; i < smmu->num_context_banks; ++i) {
1665 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1666 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1667 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1668 }
Will Deacon1463fe42013-07-31 19:21:27 +01001669
Will Deacon45ae7cf2013-06-24 18:31:25 +01001670 /* Invalidate the TLB, just in case */
1671 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1672 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1673 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1674
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001675 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001676
Will Deacon45ae7cf2013-06-24 18:31:25 +01001677 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001678 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001679
1680 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001681 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001682
1683 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001684 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001685
1686 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001687 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001688
1689 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001690 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001691
1692 /* Push the button */
1693 arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001694 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001695}
1696
1697static int arm_smmu_id_size_to_bits(int size)
1698{
1699 switch (size) {
1700 case 0:
1701 return 32;
1702 case 1:
1703 return 36;
1704 case 2:
1705 return 40;
1706 case 3:
1707 return 42;
1708 case 4:
1709 return 44;
1710 case 5:
1711 default:
1712 return 48;
1713 }
1714}
1715
1716static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1717{
1718 unsigned long size;
1719 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1720 u32 id;
1721
1722 dev_notice(smmu->dev, "probing hardware configuration...\n");
1723
1724 /* Primecell ID */
1725 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1726 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1727 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1728
1729 /* ID0 */
1730 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1731#ifndef CONFIG_64BIT
1732 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1733 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1734 return -ENODEV;
1735 }
1736#endif
Will Deacon4cf740b2014-07-14 19:47:39 +01001737
1738 /* Restrict available stages based on module parameter */
1739 if (force_stage == 1)
1740 id &= ~(ID0_S2TS | ID0_NTS);
1741 else if (force_stage == 2)
1742 id &= ~(ID0_S1TS | ID0_NTS);
1743
Will Deacon45ae7cf2013-06-24 18:31:25 +01001744 if (id & ID0_S1TS) {
1745 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1746 dev_notice(smmu->dev, "\tstage 1 translation\n");
1747 }
1748
1749 if (id & ID0_S2TS) {
1750 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1751 dev_notice(smmu->dev, "\tstage 2 translation\n");
1752 }
1753
1754 if (id & ID0_NTS) {
1755 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1756 dev_notice(smmu->dev, "\tnested translation\n");
1757 }
1758
1759 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001760 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001761 dev_err(smmu->dev, "\tno translation support!\n");
1762 return -ENODEV;
1763 }
1764
1765 if (id & ID0_CTTW) {
1766 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1767 dev_notice(smmu->dev, "\tcoherent table walk\n");
1768 }
1769
1770 if (id & ID0_SMS) {
1771 u32 smr, sid, mask;
1772
1773 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1774 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1775 ID0_NUMSMRG_MASK;
1776 if (smmu->num_mapping_groups == 0) {
1777 dev_err(smmu->dev,
1778 "stream-matching supported, but no SMRs present!\n");
1779 return -ENODEV;
1780 }
1781
1782 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1783 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1784 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1785 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1786
1787 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1788 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1789 if ((mask & sid) != sid) {
1790 dev_err(smmu->dev,
1791 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1792 mask, sid);
1793 return -ENODEV;
1794 }
1795
1796 dev_notice(smmu->dev,
1797 "\tstream matching with %u register groups, mask 0x%x",
1798 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001799 } else {
1800 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1801 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001802 }
1803
1804 /* ID1 */
1805 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1806 smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
1807
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001808 /* Check for size mismatch of SMMU address space from mapped region */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001809 size = 1 <<
1810 (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001811 size *= (smmu->pagesize << 1);
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001812 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001813 dev_warn(smmu->dev,
1814 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1815 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001816
1817 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1818 ID1_NUMS2CB_MASK;
1819 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1820 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1821 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1822 return -ENODEV;
1823 }
1824 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1825 smmu->num_context_banks, smmu->num_s2_context_banks);
1826
1827 /* ID2 */
1828 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1829 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1830
1831 /*
1832 * Stage-1 output limited by stage-2 input size due to pgd
1833 * allocation (PTRS_PER_PGD).
1834 */
Will Deacon4d09d992014-07-23 13:20:43 +01001835 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001836#ifdef CONFIG_64BIT
Will Deacon4d09d992014-07-23 13:20:43 +01001837 smmu->s1_output_size = min_t(unsigned long, VA_BITS, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001838#else
Will Deacon4d09d992014-07-23 13:20:43 +01001839 smmu->s1_output_size = min(32UL, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001840#endif
Will Deacon4d09d992014-07-23 13:20:43 +01001841 } else {
1842 smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT,
1843 size);
1844 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001845
1846 /* The stage-2 output mask is also applied for bypass */
1847 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001848 smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001849
1850 if (smmu->version == 1) {
1851 smmu->input_size = 32;
1852 } else {
1853#ifdef CONFIG_64BIT
1854 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon06f983d2013-11-05 15:55:04 +00001855 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001856#else
1857 size = 32;
1858#endif
1859 smmu->input_size = size;
1860
1861 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1862 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1863 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1864 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1865 PAGE_SIZE);
1866 return -ENODEV;
1867 }
1868 }
1869
1870 dev_notice(smmu->dev,
1871 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
Mitchel Humpherys29073202014-07-08 09:52:18 -07001872 smmu->input_size, smmu->s1_output_size,
1873 smmu->s2_output_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001874 return 0;
1875}
1876
1877static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1878{
1879 struct resource *res;
1880 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001881 struct device *dev = &pdev->dev;
1882 struct rb_node *node;
1883 struct of_phandle_args masterspec;
1884 int num_irqs, i, err;
1885
1886 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1887 if (!smmu) {
1888 dev_err(dev, "failed to allocate arm_smmu_device\n");
1889 return -ENOMEM;
1890 }
1891 smmu->dev = dev;
1892
1893 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001894 smmu->base = devm_ioremap_resource(dev, res);
1895 if (IS_ERR(smmu->base))
1896 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001897 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001898
1899 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1900 &smmu->num_global_irqs)) {
1901 dev_err(dev, "missing #global-interrupts property\n");
1902 return -ENODEV;
1903 }
1904
1905 num_irqs = 0;
1906 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1907 num_irqs++;
1908 if (num_irqs > smmu->num_global_irqs)
1909 smmu->num_context_irqs++;
1910 }
1911
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001912 if (!smmu->num_context_irqs) {
1913 dev_err(dev, "found %d interrupts but expected at least %d\n",
1914 num_irqs, smmu->num_global_irqs + 1);
1915 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001916 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001917
1918 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1919 GFP_KERNEL);
1920 if (!smmu->irqs) {
1921 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1922 return -ENOMEM;
1923 }
1924
1925 for (i = 0; i < num_irqs; ++i) {
1926 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001927
Will Deacon45ae7cf2013-06-24 18:31:25 +01001928 if (irq < 0) {
1929 dev_err(dev, "failed to get irq index %d\n", i);
1930 return -ENODEV;
1931 }
1932 smmu->irqs[i] = irq;
1933 }
1934
Olav Haugan3c8766d2014-08-22 17:12:32 -07001935 err = arm_smmu_device_cfg_probe(smmu);
1936 if (err)
1937 return err;
1938
Will Deacon45ae7cf2013-06-24 18:31:25 +01001939 i = 0;
1940 smmu->masters = RB_ROOT;
1941 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1942 "#stream-id-cells", i,
1943 &masterspec)) {
1944 err = register_smmu_master(smmu, dev, &masterspec);
1945 if (err) {
1946 dev_err(dev, "failed to add master %s\n",
1947 masterspec.np->name);
1948 goto out_put_masters;
1949 }
1950
1951 i++;
1952 }
1953 dev_notice(dev, "registered %d master devices\n", i);
1954
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001955 parse_driver_options(smmu);
1956
Will Deacon45ae7cf2013-06-24 18:31:25 +01001957 if (smmu->version > 1 &&
1958 smmu->num_context_banks != smmu->num_context_irqs) {
1959 dev_err(dev,
1960 "found only %d context interrupt(s) but %d required\n",
1961 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cd2013-11-15 09:42:30 +00001962 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001963 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001964 }
1965
Will Deacon45ae7cf2013-06-24 18:31:25 +01001966 for (i = 0; i < smmu->num_global_irqs; ++i) {
1967 err = request_irq(smmu->irqs[i],
1968 arm_smmu_global_fault,
1969 IRQF_SHARED,
1970 "arm-smmu global fault",
1971 smmu);
1972 if (err) {
1973 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1974 i, smmu->irqs[i]);
1975 goto out_free_irqs;
1976 }
1977 }
1978
1979 INIT_LIST_HEAD(&smmu->list);
1980 spin_lock(&arm_smmu_devices_lock);
1981 list_add(&smmu->list, &arm_smmu_devices);
1982 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001983
1984 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001985 return 0;
1986
1987out_free_irqs:
1988 while (i--)
1989 free_irq(smmu->irqs[i], smmu);
1990
Will Deacon45ae7cf2013-06-24 18:31:25 +01001991out_put_masters:
1992 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001993 struct arm_smmu_master *master
1994 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001995 of_node_put(master->of_node);
1996 }
1997
1998 return err;
1999}
2000
2001static int arm_smmu_device_remove(struct platform_device *pdev)
2002{
2003 int i;
2004 struct device *dev = &pdev->dev;
2005 struct arm_smmu_device *curr, *smmu = NULL;
2006 struct rb_node *node;
2007
2008 spin_lock(&arm_smmu_devices_lock);
2009 list_for_each_entry(curr, &arm_smmu_devices, list) {
2010 if (curr->dev == dev) {
2011 smmu = curr;
2012 list_del(&smmu->list);
2013 break;
2014 }
2015 }
2016 spin_unlock(&arm_smmu_devices_lock);
2017
2018 if (!smmu)
2019 return -ENODEV;
2020
Will Deacon45ae7cf2013-06-24 18:31:25 +01002021 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07002022 struct arm_smmu_master *master
2023 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002024 of_node_put(master->of_node);
2025 }
2026
Will Deaconecfadb62013-07-31 19:21:28 +01002027 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002028 dev_err(dev, "removing device with active domains!\n");
2029
2030 for (i = 0; i < smmu->num_global_irqs; ++i)
2031 free_irq(smmu->irqs[i], smmu);
2032
2033 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07002034 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002035 return 0;
2036}
2037
2038#ifdef CONFIG_OF
2039static struct of_device_id arm_smmu_of_match[] = {
2040 { .compatible = "arm,smmu-v1", },
2041 { .compatible = "arm,smmu-v2", },
2042 { .compatible = "arm,mmu-400", },
2043 { .compatible = "arm,mmu-500", },
2044 { },
2045};
2046MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2047#endif
2048
2049static struct platform_driver arm_smmu_driver = {
2050 .driver = {
2051 .owner = THIS_MODULE,
2052 .name = "arm-smmu",
2053 .of_match_table = of_match_ptr(arm_smmu_of_match),
2054 },
2055 .probe = arm_smmu_device_dt_probe,
2056 .remove = arm_smmu_device_remove,
2057};
2058
2059static int __init arm_smmu_init(void)
2060{
2061 int ret;
2062
2063 ret = platform_driver_register(&arm_smmu_driver);
2064 if (ret)
2065 return ret;
2066
2067 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01002068 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002069 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2070
Will Deacond123cf82014-02-04 22:17:53 +00002071#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01002072 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002073 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00002074#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01002075
Will Deacona9a1b0b2014-05-01 18:05:08 +01002076#ifdef CONFIG_PCI
2077 if (!iommu_present(&pci_bus_type))
2078 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2079#endif
2080
Will Deacon45ae7cf2013-06-24 18:31:25 +01002081 return 0;
2082}
2083
2084static void __exit arm_smmu_exit(void)
2085{
2086 return platform_driver_unregister(&arm_smmu_driver);
2087}
2088
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002089subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002090module_exit(arm_smmu_exit);
2091
2092MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2093MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2094MODULE_LICENSE("GPL v2");