blob: 27fd3d7ba88602bd86285263d681865b8e41d29f [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 Deacon28d60072014-09-01 16:24:48 +010027 * - Up to 48-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)
Will Deaconc757e852014-07-30 11:33:25 +010062#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +010063
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))
Will Deaconc757e852014-07-30 11:33:25 +0100227#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +0100228
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;
Will Deaconc757e852014-07-30 11:33:25 +0100357 unsigned long pgshift;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100358
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
Will Deacon28d60072014-09-01 16:24:48 +0100378 unsigned long s1_input_size;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100379 unsigned long s1_output_size;
Will Deacon28d60072014-09-01 16:24:48 +0100380 unsigned long s2_input_size;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100381 unsigned long s2_output_size;
382
383 u32 num_global_irqs;
384 u32 num_context_irqs;
385 unsigned int *irqs;
386
Will Deacon45ae7cf2013-06-24 18:31:25 +0100387 struct list_head list;
388 struct rb_root masters;
389};
390
391struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100392 u8 cbndx;
393 u8 irptndx;
394 u32 cbar;
395 pgd_t *pgd;
396};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100397#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100398
Will Deaconecfadb62013-07-31 19:21:28 +0100399#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
400#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
401
Will Deacon45ae7cf2013-06-24 18:31:25 +0100402struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100403 struct arm_smmu_device *smmu;
404 struct arm_smmu_cfg cfg;
Will Deaconc9d09e22014-02-04 22:12:42 +0000405 spinlock_t lock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100406};
407
408static DEFINE_SPINLOCK(arm_smmu_devices_lock);
409static LIST_HEAD(arm_smmu_devices);
410
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000411struct arm_smmu_option_prop {
412 u32 opt;
413 const char *prop;
414};
415
Mitchel Humpherys29073202014-07-08 09:52:18 -0700416static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000417 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
418 { 0, NULL},
419};
420
421static void parse_driver_options(struct arm_smmu_device *smmu)
422{
423 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700424
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000425 do {
426 if (of_property_read_bool(smmu->dev->of_node,
427 arm_smmu_options[i].prop)) {
428 smmu->options |= arm_smmu_options[i].opt;
429 dev_notice(smmu->dev, "option %s\n",
430 arm_smmu_options[i].prop);
431 }
432 } while (arm_smmu_options[++i].opt);
433}
434
Will Deacon8f68f8e2014-07-15 11:27:08 +0100435static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100436{
437 if (dev_is_pci(dev)) {
438 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700439
Will Deacona9a1b0b2014-05-01 18:05:08 +0100440 while (!pci_is_root_bus(bus))
441 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100442 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100443 }
444
Will Deacon8f68f8e2014-07-15 11:27:08 +0100445 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100446}
447
Will Deacon45ae7cf2013-06-24 18:31:25 +0100448static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
449 struct device_node *dev_node)
450{
451 struct rb_node *node = smmu->masters.rb_node;
452
453 while (node) {
454 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700455
Will Deacon45ae7cf2013-06-24 18:31:25 +0100456 master = container_of(node, struct arm_smmu_master, node);
457
458 if (dev_node < master->of_node)
459 node = node->rb_left;
460 else if (dev_node > master->of_node)
461 node = node->rb_right;
462 else
463 return master;
464 }
465
466 return NULL;
467}
468
Will Deacona9a1b0b2014-05-01 18:05:08 +0100469static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100470find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100471{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100472 struct arm_smmu_master_cfg *cfg = NULL;
473 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100474
Will Deacon8f68f8e2014-07-15 11:27:08 +0100475 if (group) {
476 cfg = iommu_group_get_iommudata(group);
477 iommu_group_put(group);
478 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100479
Will Deacon8f68f8e2014-07-15 11:27:08 +0100480 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100481}
482
Will Deacon45ae7cf2013-06-24 18:31:25 +0100483static int insert_smmu_master(struct arm_smmu_device *smmu,
484 struct arm_smmu_master *master)
485{
486 struct rb_node **new, *parent;
487
488 new = &smmu->masters.rb_node;
489 parent = NULL;
490 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700491 struct arm_smmu_master *this
492 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100493
494 parent = *new;
495 if (master->of_node < this->of_node)
496 new = &((*new)->rb_left);
497 else if (master->of_node > this->of_node)
498 new = &((*new)->rb_right);
499 else
500 return -EEXIST;
501 }
502
503 rb_link_node(&master->node, parent, new);
504 rb_insert_color(&master->node, &smmu->masters);
505 return 0;
506}
507
508static int register_smmu_master(struct arm_smmu_device *smmu,
509 struct device *dev,
510 struct of_phandle_args *masterspec)
511{
512 int i;
513 struct arm_smmu_master *master;
514
515 master = find_smmu_master(smmu, masterspec->np);
516 if (master) {
517 dev_err(dev,
518 "rejecting multiple registrations for master device %s\n",
519 masterspec->np->name);
520 return -EBUSY;
521 }
522
523 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
524 dev_err(dev,
525 "reached maximum number (%d) of stream IDs for master device %s\n",
526 MAX_MASTER_STREAMIDS, masterspec->np->name);
527 return -ENOSPC;
528 }
529
530 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
531 if (!master)
532 return -ENOMEM;
533
Will Deacona9a1b0b2014-05-01 18:05:08 +0100534 master->of_node = masterspec->np;
535 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100536
Olav Haugan3c8766d2014-08-22 17:12:32 -0700537 for (i = 0; i < master->cfg.num_streamids; ++i) {
538 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100539
Olav Haugan3c8766d2014-08-22 17:12:32 -0700540 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
541 (streamid >= smmu->num_mapping_groups)) {
542 dev_err(dev,
543 "stream ID for master device %s greater than maximum allowed (%d)\n",
544 masterspec->np->name, smmu->num_mapping_groups);
545 return -ERANGE;
546 }
547 master->cfg.streamids[i] = streamid;
548 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100549 return insert_smmu_master(smmu, master);
550}
551
Will Deacon44680ee2014-06-25 11:29:12 +0100552static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100553{
Will Deacon44680ee2014-06-25 11:29:12 +0100554 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100555 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100556 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100557
558 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100559 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100560 master = find_smmu_master(smmu, dev_node);
561 if (master)
562 break;
563 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100564 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100565
Will Deacona9a1b0b2014-05-01 18:05:08 +0100566 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100567}
568
569static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
570{
571 int idx;
572
573 do {
574 idx = find_next_zero_bit(map, end, start);
575 if (idx == end)
576 return -ENOSPC;
577 } while (test_and_set_bit(idx, map));
578
579 return idx;
580}
581
582static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
583{
584 clear_bit(idx, map);
585}
586
587/* Wait for any pending TLB invalidations to complete */
588static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
589{
590 int count = 0;
591 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
592
593 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
594 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
595 & sTLBGSTATUS_GSACTIVE) {
596 cpu_relax();
597 if (++count == TLB_LOOP_TIMEOUT) {
598 dev_err_ratelimited(smmu->dev,
599 "TLB sync timed out -- SMMU may be deadlocked\n");
600 return;
601 }
602 udelay(1);
603 }
604}
605
Will Deacon44680ee2014-06-25 11:29:12 +0100606static void arm_smmu_tlb_inv_context(struct arm_smmu_domain *smmu_domain)
Will Deacon1463fe42013-07-31 19:21:27 +0100607{
Will Deacon44680ee2014-06-25 11:29:12 +0100608 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
609 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100610 void __iomem *base = ARM_SMMU_GR0(smmu);
611 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
612
613 if (stage1) {
614 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100615 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
616 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100617 } else {
618 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100619 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
620 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100621 }
622
623 arm_smmu_tlb_sync(smmu);
624}
625
Will Deacon45ae7cf2013-06-24 18:31:25 +0100626static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
627{
628 int flags, ret;
629 u32 fsr, far, fsynr, resume;
630 unsigned long iova;
631 struct iommu_domain *domain = dev;
632 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100633 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
634 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100635 void __iomem *cb_base;
636
Will Deacon44680ee2014-06-25 11:29:12 +0100637 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100638 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
639
640 if (!(fsr & FSR_FAULT))
641 return IRQ_NONE;
642
643 if (fsr & FSR_IGN)
644 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100645 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100646 fsr);
647
648 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
649 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
650
651 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
652 iova = far;
653#ifdef CONFIG_64BIT
654 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
655 iova |= ((unsigned long)far << 32);
656#endif
657
658 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
659 ret = IRQ_HANDLED;
660 resume = RESUME_RETRY;
661 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100662 dev_err_ratelimited(smmu->dev,
663 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100664 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100665 ret = IRQ_NONE;
666 resume = RESUME_TERMINATE;
667 }
668
669 /* Clear the faulting FSR */
670 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
671
672 /* Retry or terminate any stalled transactions */
673 if (fsr & FSR_SS)
674 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
675
676 return ret;
677}
678
679static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
680{
681 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
682 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000683 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100684
685 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
686 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
687 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
688 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
689
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000690 if (!gfsr)
691 return IRQ_NONE;
692
Will Deacon45ae7cf2013-06-24 18:31:25 +0100693 dev_err_ratelimited(smmu->dev,
694 "Unexpected global fault, this could be serious\n");
695 dev_err_ratelimited(smmu->dev,
696 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
697 gfsr, gfsynr0, gfsynr1, gfsynr2);
698
699 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100700 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100701}
702
Will Deacon6dd35f42014-02-05 17:49:34 +0000703static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
704 size_t size)
705{
706 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
707
708
709 /* Ensure new page tables are visible to the hardware walker */
710 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
Will Deacon3aa80ea2014-02-05 23:35:47 +0000711 dsb(ishst);
Will Deacon6dd35f42014-02-05 17:49:34 +0000712 } else {
713 /*
714 * If the SMMU can't walk tables in the CPU caches, treat them
715 * like non-coherent DMA since we need to flush the new entries
716 * all the way out to memory. There's no possibility of
717 * recursion here as the SMMU table walker will not be wired
718 * through another SMMU.
719 */
720 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
721 DMA_TO_DEVICE);
722 }
723}
724
Will Deacon45ae7cf2013-06-24 18:31:25 +0100725static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
726{
727 u32 reg;
728 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100729 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
730 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100731 void __iomem *cb_base, *gr0_base, *gr1_base;
732
733 gr0_base = ARM_SMMU_GR0(smmu);
734 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100735 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
736 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100737
738 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100739 reg = cfg->cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100740 if (smmu->version == 1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700741 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100742
Will Deacon57ca90f2014-02-06 14:59:05 +0000743 /*
744 * Use the weakest shareability/memory types, so they are
745 * overridden by the ttbcr/pte.
746 */
747 if (stage1) {
748 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
749 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
750 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100751 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000752 }
Will Deacon44680ee2014-06-25 11:29:12 +0100753 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100754
755 if (smmu->version > 1) {
756 /* CBA2R */
757#ifdef CONFIG_64BIT
758 reg = CBA2R_RW64_64BIT;
759#else
760 reg = CBA2R_RW64_32BIT;
761#endif
762 writel_relaxed(reg,
Will Deacon44680ee2014-06-25 11:29:12 +0100763 gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100764
765 /* TTBCR2 */
Will Deacon28d60072014-09-01 16:24:48 +0100766 switch (smmu->s1_input_size) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100767 case 32:
768 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
769 break;
770 case 36:
771 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
772 break;
773 case 39:
Will Deacon4d09d992014-07-23 13:20:43 +0100774 case 40:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100775 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
776 break;
777 case 42:
778 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
779 break;
780 case 44:
781 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
782 break;
783 case 48:
784 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
785 break;
786 }
787
788 switch (smmu->s1_output_size) {
789 case 32:
790 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
791 break;
792 case 36:
793 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
794 break;
795 case 39:
Will Deacon4d09d992014-07-23 13:20:43 +0100796 case 40:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100797 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
798 break;
799 case 42:
800 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
801 break;
802 case 44:
803 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
804 break;
805 case 48:
806 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
807 break;
808 }
809
810 if (stage1)
811 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
812 }
813
814 /* TTBR0 */
Will Deacon44680ee2014-06-25 11:29:12 +0100815 arm_smmu_flush_pgtable(smmu, cfg->pgd,
Will Deacon6dd35f42014-02-05 17:49:34 +0000816 PTRS_PER_PGD * sizeof(pgd_t));
Will Deacon44680ee2014-06-25 11:29:12 +0100817 reg = __pa(cfg->pgd);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100818 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
Will Deacon44680ee2014-06-25 11:29:12 +0100819 reg = (phys_addr_t)__pa(cfg->pgd) >> 32;
Will Deacon1463fe42013-07-31 19:21:27 +0100820 if (stage1)
Will Deacon44680ee2014-06-25 11:29:12 +0100821 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100822 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100823
824 /*
825 * TTBCR
826 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
827 */
828 if (smmu->version > 1) {
829 if (PAGE_SIZE == SZ_4K)
830 reg = TTBCR_TG0_4K;
831 else
832 reg = TTBCR_TG0_64K;
833
834 if (!stage1) {
Will Deacon28d60072014-09-01 16:24:48 +0100835 reg |= (64 - smmu->s2_input_size) << TTBCR_T0SZ_SHIFT;
Will Deacona65217a2014-06-24 18:26:26 +0100836
Will Deacon45ae7cf2013-06-24 18:31:25 +0100837 switch (smmu->s2_output_size) {
838 case 32:
839 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
840 break;
841 case 36:
842 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
843 break;
844 case 40:
845 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
846 break;
847 case 42:
848 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
849 break;
850 case 44:
851 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
852 break;
853 case 48:
854 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
855 break;
856 }
857 } else {
Will Deacon28d60072014-09-01 16:24:48 +0100858 reg |= (64 - smmu->s1_input_size) << TTBCR_T0SZ_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100859 }
860 } else {
861 reg = 0;
862 }
863
864 reg |= TTBCR_EAE |
865 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
866 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
Olav Haugan1fc870c2014-08-04 19:01:02 +0100867 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT);
868
869 if (!stage1)
870 reg |= (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
871
Will Deacon45ae7cf2013-06-24 18:31:25 +0100872 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
873
874 /* MAIR0 (stage-1 only) */
875 if (stage1) {
876 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
877 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
878 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
879 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
880 }
881
Will Deacon45ae7cf2013-06-24 18:31:25 +0100882 /* SCTLR */
883 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
884 if (stage1)
885 reg |= SCTLR_S1_ASIDPNE;
886#ifdef __BIG_ENDIAN
887 reg |= SCTLR_E;
888#endif
Will Deacon25724842013-08-21 13:49:53 +0100889 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100890}
891
892static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100893 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100894{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100895 int irq, start, ret = 0;
896 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100897 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100898 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100899
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100900 spin_lock_irqsave(&smmu_domain->lock, flags);
901 if (smmu_domain->smmu)
902 goto out_unlock;
903
Will Deacon45ae7cf2013-06-24 18:31:25 +0100904 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
905 /*
906 * We will likely want to change this if/when KVM gets
907 * involved.
908 */
Will Deacon44680ee2014-06-25 11:29:12 +0100909 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100910 start = smmu->num_s2_context_banks;
Will Deacon9c5c92e2014-06-25 12:12:41 +0100911 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100912 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100913 start = smmu->num_s2_context_banks;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100914 } else {
Will Deacon9c5c92e2014-06-25 12:12:41 +0100915 cfg->cbar = CBAR_TYPE_S2_TRANS;
916 start = 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100917 }
918
919 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
920 smmu->num_context_banks);
921 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100922 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100923
Will Deacon44680ee2014-06-25 11:29:12 +0100924 cfg->cbndx = ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100925 if (smmu->version == 1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100926 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
927 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100928 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100929 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100930 }
931
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100932 ACCESS_ONCE(smmu_domain->smmu) = smmu;
933 arm_smmu_init_context_bank(smmu_domain);
934 spin_unlock_irqrestore(&smmu_domain->lock, flags);
935
Will Deacon44680ee2014-06-25 11:29:12 +0100936 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100937 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
938 "arm-smmu-context-fault", domain);
939 if (IS_ERR_VALUE(ret)) {
940 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100941 cfg->irptndx, irq);
942 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100943 }
944
Will Deacona9a1b0b2014-05-01 18:05:08 +0100945 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100946
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100947out_unlock:
948 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100949 return ret;
950}
951
952static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
953{
954 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100955 struct arm_smmu_device *smmu = smmu_domain->smmu;
956 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100957 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100958 int irq;
959
960 if (!smmu)
961 return;
962
Will Deacon1463fe42013-07-31 19:21:27 +0100963 /* Disable the context bank and nuke the TLB before freeing it. */
Will Deacon44680ee2014-06-25 11:29:12 +0100964 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100965 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon44680ee2014-06-25 11:29:12 +0100966 arm_smmu_tlb_inv_context(smmu_domain);
Will Deacon1463fe42013-07-31 19:21:27 +0100967
Will Deacon44680ee2014-06-25 11:29:12 +0100968 if (cfg->irptndx != INVALID_IRPTNDX) {
969 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100970 free_irq(irq, domain);
971 }
972
Will Deacon44680ee2014-06-25 11:29:12 +0100973 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100974}
975
976static int arm_smmu_domain_init(struct iommu_domain *domain)
977{
978 struct arm_smmu_domain *smmu_domain;
979 pgd_t *pgd;
980
981 /*
982 * Allocate the domain and initialise some of its data structures.
983 * We can't really do anything meaningful until we've added a
984 * master.
985 */
986 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
987 if (!smmu_domain)
988 return -ENOMEM;
989
Mitchel Humpherys29073202014-07-08 09:52:18 -0700990 pgd = kcalloc(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100991 if (!pgd)
992 goto out_free_domain;
Will Deacon44680ee2014-06-25 11:29:12 +0100993 smmu_domain->cfg.pgd = pgd;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100994
Will Deaconc9d09e22014-02-04 22:12:42 +0000995 spin_lock_init(&smmu_domain->lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100996 domain->priv = smmu_domain;
997 return 0;
998
999out_free_domain:
1000 kfree(smmu_domain);
1001 return -ENOMEM;
1002}
1003
1004static void arm_smmu_free_ptes(pmd_t *pmd)
1005{
1006 pgtable_t table = pmd_pgtable(*pmd);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001007
Will Deacon45ae7cf2013-06-24 18:31:25 +01001008 __free_page(table);
1009}
1010
1011static void arm_smmu_free_pmds(pud_t *pud)
1012{
1013 int i;
1014 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
1015
1016 pmd = pmd_base;
1017 for (i = 0; i < PTRS_PER_PMD; ++i) {
1018 if (pmd_none(*pmd))
1019 continue;
1020
1021 arm_smmu_free_ptes(pmd);
1022 pmd++;
1023 }
1024
1025 pmd_free(NULL, pmd_base);
1026}
1027
1028static void arm_smmu_free_puds(pgd_t *pgd)
1029{
1030 int i;
1031 pud_t *pud, *pud_base = pud_offset(pgd, 0);
1032
1033 pud = pud_base;
1034 for (i = 0; i < PTRS_PER_PUD; ++i) {
1035 if (pud_none(*pud))
1036 continue;
1037
1038 arm_smmu_free_pmds(pud);
1039 pud++;
1040 }
1041
1042 pud_free(NULL, pud_base);
1043}
1044
1045static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
1046{
1047 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001048 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1049 pgd_t *pgd, *pgd_base = cfg->pgd;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001050
1051 /*
1052 * Recursively free the page tables for this domain. We don't
Will Deacon34fb4b32014-02-26 11:14:37 +00001053 * care about speculative TLB filling because the tables should
1054 * not be active in any context bank at this point (SCTLR.M is 0).
Will Deacon45ae7cf2013-06-24 18:31:25 +01001055 */
1056 pgd = pgd_base;
1057 for (i = 0; i < PTRS_PER_PGD; ++i) {
1058 if (pgd_none(*pgd))
1059 continue;
1060 arm_smmu_free_puds(pgd);
1061 pgd++;
1062 }
1063
1064 kfree(pgd_base);
1065}
1066
1067static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1068{
1069 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001070
1071 /*
1072 * Free the domain resources. We assume that all devices have
1073 * already been detached.
1074 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001075 arm_smmu_destroy_domain_context(domain);
1076 arm_smmu_free_pgtables(smmu_domain);
1077 kfree(smmu_domain);
1078}
1079
1080static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001081 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001082{
1083 int i;
1084 struct arm_smmu_smr *smrs;
1085 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1086
1087 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1088 return 0;
1089
Will Deacona9a1b0b2014-05-01 18:05:08 +01001090 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001091 return -EEXIST;
1092
Mitchel Humpherys29073202014-07-08 09:52:18 -07001093 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001094 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001095 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1096 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001097 return -ENOMEM;
1098 }
1099
Will Deacon44680ee2014-06-25 11:29:12 +01001100 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001101 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001102 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1103 smmu->num_mapping_groups);
1104 if (IS_ERR_VALUE(idx)) {
1105 dev_err(smmu->dev, "failed to allocate free SMR\n");
1106 goto err_free_smrs;
1107 }
1108
1109 smrs[i] = (struct arm_smmu_smr) {
1110 .idx = idx,
1111 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001112 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001113 };
1114 }
1115
1116 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001117 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001118 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1119 smrs[i].mask << SMR_MASK_SHIFT;
1120 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1121 }
1122
Will Deacona9a1b0b2014-05-01 18:05:08 +01001123 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001124 return 0;
1125
1126err_free_smrs:
1127 while (--i >= 0)
1128 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1129 kfree(smrs);
1130 return -ENOSPC;
1131}
1132
1133static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001134 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001135{
1136 int i;
1137 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001138 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001139
Will Deacon43b412b2014-07-15 11:22:24 +01001140 if (!smrs)
1141 return;
1142
Will Deacon45ae7cf2013-06-24 18:31:25 +01001143 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001144 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001145 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001146
Will Deacon45ae7cf2013-06-24 18:31:25 +01001147 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1148 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1149 }
1150
Will Deacona9a1b0b2014-05-01 18:05:08 +01001151 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001152 kfree(smrs);
1153}
1154
Will Deacon45ae7cf2013-06-24 18:31:25 +01001155static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001156 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001157{
1158 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001159 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001160 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1161
Will Deacon8f68f8e2014-07-15 11:27:08 +01001162 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001163 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001164 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001165 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001166
Will Deacona9a1b0b2014-05-01 18:05:08 +01001167 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001168 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001169
Will Deacona9a1b0b2014-05-01 18:05:08 +01001170 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001171 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001172 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001173 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1174 }
1175
1176 return 0;
1177}
1178
1179static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001180 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001181{
Will Deacon43b412b2014-07-15 11:22:24 +01001182 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001183 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001184 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001185
Will Deacon8f68f8e2014-07-15 11:27:08 +01001186 /* An IOMMU group is torn down by the first device to be removed */
1187 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1188 return;
1189
Will Deacon45ae7cf2013-06-24 18:31:25 +01001190 /*
1191 * We *must* clear the S2CR first, because freeing the SMR means
1192 * that it can be re-allocated immediately.
1193 */
Will Deacon43b412b2014-07-15 11:22:24 +01001194 for (i = 0; i < cfg->num_streamids; ++i) {
1195 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1196
1197 writel_relaxed(S2CR_TYPE_BYPASS,
1198 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1199 }
1200
Will Deacona9a1b0b2014-05-01 18:05:08 +01001201 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001202}
1203
1204static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1205{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001206 int ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001207 struct arm_smmu_domain *smmu_domain = domain->priv;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001208 struct arm_smmu_device *smmu, *dom_smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001209 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001210
Will Deacon8f68f8e2014-07-15 11:27:08 +01001211 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001212 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001213 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1214 return -ENXIO;
1215 }
1216
Will Deacon844e35b2014-07-17 11:23:51 +01001217 if (dev->archdata.iommu) {
1218 dev_err(dev, "already attached to IOMMU domain\n");
1219 return -EEXIST;
1220 }
1221
Will Deacon45ae7cf2013-06-24 18:31:25 +01001222 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001223 * Sanity check the domain. We don't support domains across
1224 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001225 */
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001226 dom_smmu = ACCESS_ONCE(smmu_domain->smmu);
1227 if (!dom_smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001228 /* Now that we have a master, we can finalise the domain */
Will Deacon44680ee2014-06-25 11:29:12 +01001229 ret = arm_smmu_init_domain_context(domain, smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001230 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001231 return ret;
1232
1233 dom_smmu = smmu_domain->smmu;
1234 }
1235
1236 if (dom_smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001237 dev_err(dev,
1238 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001239 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1240 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001241 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001242
1243 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001244 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001245 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001246 return -ENODEV;
1247
Will Deacon844e35b2014-07-17 11:23:51 +01001248 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1249 if (!ret)
1250 dev->archdata.iommu = domain;
1251 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001252}
1253
1254static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1255{
1256 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001257 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001258
Will Deacon8f68f8e2014-07-15 11:27:08 +01001259 cfg = find_smmu_master_cfg(dev);
Will Deacon844e35b2014-07-17 11:23:51 +01001260 if (!cfg)
1261 return;
1262
1263 dev->archdata.iommu = NULL;
1264 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001265}
1266
Will Deacon45ae7cf2013-06-24 18:31:25 +01001267static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1268 unsigned long end)
1269{
1270 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1271 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1272}
1273
1274static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1275 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001276 unsigned long pfn, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001277{
1278 pte_t *pte, *start;
Will Deaconcf2d45b2013-11-05 16:32:00 +00001279 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001280
1281 if (pmd_none(*pmd)) {
1282 /* Allocate a new set of tables */
Will Deaconc9d09e22014-02-04 22:12:42 +00001283 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001284
Will Deacon45ae7cf2013-06-24 18:31:25 +01001285 if (!table)
1286 return -ENOMEM;
1287
Will Deacon6dd35f42014-02-05 17:49:34 +00001288 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001289 pmd_populate(NULL, pmd, table);
1290 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1291 }
1292
1293 if (stage == 1) {
Will Deacon1463fe42013-07-31 19:21:27 +01001294 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
Will Deaconb410aed2014-02-20 16:31:06 +00001295 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001296 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1297
Will Deaconb410aed2014-02-20 16:31:06 +00001298 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001299 pteval |= (MAIR_ATTR_IDX_CACHE <<
1300 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1301 } else {
1302 pteval |= ARM_SMMU_PTE_HAP_FAULT;
Will Deaconb410aed2014-02-20 16:31:06 +00001303 if (prot & IOMMU_READ)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001304 pteval |= ARM_SMMU_PTE_HAP_READ;
Will Deaconb410aed2014-02-20 16:31:06 +00001305 if (prot & IOMMU_WRITE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001306 pteval |= ARM_SMMU_PTE_HAP_WRITE;
Will Deaconb410aed2014-02-20 16:31:06 +00001307 if (prot & IOMMU_CACHE)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001308 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1309 else
1310 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1311 }
1312
1313 /* If no access, create a faulting entry to avoid TLB fills */
Will Deaconb410aed2014-02-20 16:31:06 +00001314 if (prot & IOMMU_EXEC)
Will Deaconcf2d45b2013-11-05 16:32:00 +00001315 pteval &= ~ARM_SMMU_PTE_XN;
Will Deaconb410aed2014-02-20 16:31:06 +00001316 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001317 pteval &= ~ARM_SMMU_PTE_PAGE;
1318
1319 pteval |= ARM_SMMU_PTE_SH_IS;
1320 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1321 pte = start;
1322
1323 /*
1324 * Install the page table entries. This is fairly complicated
1325 * since we attempt to make use of the contiguous hint in the
1326 * ptes where possible. The contiguous hint indicates a series
1327 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1328 * contiguous region with the following constraints:
1329 *
1330 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1331 * - Each pte in the region has the contiguous hint bit set
1332 *
1333 * This complicates unmapping (also handled by this code, when
1334 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1335 * possible, yet highly unlikely, that a client may unmap only
1336 * part of a contiguous range. This requires clearing of the
1337 * contiguous hint bits in the range before installing the new
1338 * faulting entries.
1339 *
1340 * Note that re-mapping an address range without first unmapping
1341 * it is not supported, so TLB invalidation is not required here
1342 * and is instead performed at unmap and domain-init time.
1343 */
1344 do {
1345 int i = 1;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001346
Will Deacon45ae7cf2013-06-24 18:31:25 +01001347 pteval &= ~ARM_SMMU_PTE_CONT;
1348
1349 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1350 i = ARM_SMMU_PTE_CONT_ENTRIES;
1351 pteval |= ARM_SMMU_PTE_CONT;
1352 } else if (pte_val(*pte) &
1353 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1354 int j;
1355 pte_t *cont_start;
1356 unsigned long idx = pte_index(addr);
1357
1358 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1359 cont_start = pmd_page_vaddr(*pmd) + idx;
1360 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001361 pte_val(*(cont_start + j)) &=
1362 ~ARM_SMMU_PTE_CONT;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001363
1364 arm_smmu_flush_pgtable(smmu, cont_start,
1365 sizeof(*pte) *
1366 ARM_SMMU_PTE_CONT_ENTRIES);
1367 }
1368
1369 do {
1370 *pte = pfn_pte(pfn, __pgprot(pteval));
1371 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1372 } while (addr != end);
1373
1374 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1375 return 0;
1376}
1377
1378static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1379 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001380 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001381{
1382 int ret;
1383 pmd_t *pmd;
1384 unsigned long next, pfn = __phys_to_pfn(phys);
1385
1386#ifndef __PAGETABLE_PMD_FOLDED
1387 if (pud_none(*pud)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001388 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001389 if (!pmd)
1390 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001391
Will Deacon6dd35f42014-02-05 17:49:34 +00001392 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001393 pud_populate(NULL, pud, pmd);
1394 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1395
1396 pmd += pmd_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001397 } else
1398#endif
1399 pmd = pmd_offset(pud, addr);
1400
1401 do {
1402 next = pmd_addr_end(addr, end);
Bin Wangaca1bc42014-03-21 10:06:07 +00001403 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn,
Will Deaconb410aed2014-02-20 16:31:06 +00001404 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001405 phys += next - addr;
1406 } while (pmd++, addr = next, addr < end);
1407
1408 return ret;
1409}
1410
1411static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1412 unsigned long addr, unsigned long end,
Will Deaconb410aed2014-02-20 16:31:06 +00001413 phys_addr_t phys, int prot, int stage)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001414{
1415 int ret = 0;
1416 pud_t *pud;
1417 unsigned long next;
1418
1419#ifndef __PAGETABLE_PUD_FOLDED
1420 if (pgd_none(*pgd)) {
Will Deaconc9d09e22014-02-04 22:12:42 +00001421 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001422 if (!pud)
1423 return -ENOMEM;
Yifan Zhang97a64422014-01-03 12:01:26 +00001424
Will Deacon6dd35f42014-02-05 17:49:34 +00001425 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
Yifan Zhang97a64422014-01-03 12:01:26 +00001426 pgd_populate(NULL, pgd, pud);
1427 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1428
1429 pud += pud_index(addr);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001430 } else
1431#endif
1432 pud = pud_offset(pgd, addr);
1433
1434 do {
1435 next = pud_addr_end(addr, end);
1436 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
Will Deaconb410aed2014-02-20 16:31:06 +00001437 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001438 phys += next - addr;
1439 } while (pud++, addr = next, addr < end);
1440
1441 return ret;
1442}
1443
1444static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1445 unsigned long iova, phys_addr_t paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001446 size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001447{
1448 int ret, stage;
1449 unsigned long end;
1450 phys_addr_t input_mask, output_mask;
Will Deacon44680ee2014-06-25 11:29:12 +01001451 struct arm_smmu_device *smmu = smmu_domain->smmu;
1452 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1453 pgd_t *pgd = cfg->pgd;
Will Deaconb410aed2014-02-20 16:31:06 +00001454 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001455
Will Deacon44680ee2014-06-25 11:29:12 +01001456 if (cfg->cbar == CBAR_TYPE_S2_TRANS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001457 stage = 2;
Will Deacon28d60072014-09-01 16:24:48 +01001458 input_mask = (1ULL << smmu->s2_input_size) - 1;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001459 output_mask = (1ULL << smmu->s2_output_size) - 1;
1460 } else {
1461 stage = 1;
Will Deacon28d60072014-09-01 16:24:48 +01001462 input_mask = (1ULL << smmu->s1_input_size) - 1;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001463 output_mask = (1ULL << smmu->s1_output_size) - 1;
1464 }
1465
1466 if (!pgd)
1467 return -EINVAL;
1468
1469 if (size & ~PAGE_MASK)
1470 return -EINVAL;
1471
Will Deacon45ae7cf2013-06-24 18:31:25 +01001472 if ((phys_addr_t)iova & ~input_mask)
1473 return -ERANGE;
1474
1475 if (paddr & ~output_mask)
1476 return -ERANGE;
1477
Will Deaconb410aed2014-02-20 16:31:06 +00001478 spin_lock_irqsave(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001479 pgd += pgd_index(iova);
1480 end = iova + size;
1481 do {
1482 unsigned long next = pgd_addr_end(iova, end);
1483
1484 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
Will Deaconb410aed2014-02-20 16:31:06 +00001485 prot, stage);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001486 if (ret)
1487 goto out_unlock;
1488
1489 paddr += next - iova;
1490 iova = next;
1491 } while (pgd++, iova != end);
1492
1493out_unlock:
Will Deaconb410aed2014-02-20 16:31:06 +00001494 spin_unlock_irqrestore(&smmu_domain->lock, flags);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001495
Will Deacon45ae7cf2013-06-24 18:31:25 +01001496 return ret;
1497}
1498
1499static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001500 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001501{
1502 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001503
Will Deacon5552ecd2013-11-08 15:08:06 +00001504 if (!smmu_domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001505 return -ENODEV;
1506
Will Deaconb410aed2014-02-20 16:31:06 +00001507 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, prot);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001508}
1509
1510static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1511 size_t size)
1512{
1513 int ret;
1514 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001515
1516 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
Will Deacon44680ee2014-06-25 11:29:12 +01001517 arm_smmu_tlb_inv_context(smmu_domain);
Laurent Pinchart16c50dcf2014-02-28 15:37:10 +00001518 return ret ? 0 : size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001519}
1520
1521static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1522 dma_addr_t iova)
1523{
Will Deacona44a97912013-11-07 18:47:50 +00001524 pgd_t *pgdp, pgd;
1525 pud_t pud;
1526 pmd_t pmd;
1527 pte_t pte;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001528 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +01001529 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001530
Will Deacon44680ee2014-06-25 11:29:12 +01001531 pgdp = cfg->pgd;
Will Deacona44a97912013-11-07 18:47:50 +00001532 if (!pgdp)
1533 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001534
Will Deacona44a97912013-11-07 18:47:50 +00001535 pgd = *(pgdp + pgd_index(iova));
1536 if (pgd_none(pgd))
1537 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001538
Will Deacona44a97912013-11-07 18:47:50 +00001539 pud = *pud_offset(&pgd, iova);
1540 if (pud_none(pud))
1541 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001542
Will Deacona44a97912013-11-07 18:47:50 +00001543 pmd = *pmd_offset(&pud, iova);
1544 if (pmd_none(pmd))
1545 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001546
Will Deacona44a97912013-11-07 18:47:50 +00001547 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001548 if (pte_none(pte))
Will Deacona44a97912013-11-07 18:47:50 +00001549 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001550
Will Deacona44a97912013-11-07 18:47:50 +00001551 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001552}
1553
1554static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1555 unsigned long cap)
1556{
Will Deacon45ae7cf2013-06-24 18:31:25 +01001557 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacond3bca162014-07-04 11:06:01 +01001558 struct arm_smmu_device *smmu = smmu_domain->smmu;
1559 u32 features = smmu ? smmu->features : 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001560
Will Deacond0948942014-06-24 17:30:10 +01001561 switch (cap) {
1562 case IOMMU_CAP_CACHE_COHERENCY:
1563 return features & ARM_SMMU_FEAT_COHERENT_WALK;
1564 case IOMMU_CAP_INTR_REMAP:
1565 return 1; /* MSIs are just memory writes */
1566 default:
1567 return 0;
1568 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001569}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001570
Will Deacona9a1b0b2014-05-01 18:05:08 +01001571static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1572{
1573 *((u16 *)data) = alias;
1574 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001575}
1576
Will Deacon8f68f8e2014-07-15 11:27:08 +01001577static void __arm_smmu_release_pci_iommudata(void *data)
1578{
1579 kfree(data);
1580}
1581
Will Deacon45ae7cf2013-06-24 18:31:25 +01001582static int arm_smmu_add_device(struct device *dev)
1583{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001584 struct arm_smmu_device *smmu;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001585 struct arm_smmu_master_cfg *cfg;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001586 struct iommu_group *group;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001587 void (*releasefn)(void *) = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001588 int ret;
1589
Will Deacon44680ee2014-06-25 11:29:12 +01001590 smmu = find_smmu_for_device(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001591 if (!smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001592 return -ENODEV;
1593
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001594 group = iommu_group_alloc();
1595 if (IS_ERR(group)) {
1596 dev_err(dev, "Failed to allocate IOMMU group\n");
1597 return PTR_ERR(group);
1598 }
1599
Will Deacona9a1b0b2014-05-01 18:05:08 +01001600 if (dev_is_pci(dev)) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001601 struct pci_dev *pdev = to_pci_dev(dev);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001602
Will Deacona9a1b0b2014-05-01 18:05:08 +01001603 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1604 if (!cfg) {
1605 ret = -ENOMEM;
1606 goto out_put_group;
1607 }
1608
1609 cfg->num_streamids = 1;
1610 /*
1611 * Assume Stream ID == Requester ID for now.
1612 * We need a way to describe the ID mappings in FDT.
1613 */
1614 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1615 &cfg->streamids[0]);
Will Deacon8f68f8e2014-07-15 11:27:08 +01001616 releasefn = __arm_smmu_release_pci_iommudata;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001617 } else {
Will Deacon8f68f8e2014-07-15 11:27:08 +01001618 struct arm_smmu_master *master;
1619
1620 master = find_smmu_master(smmu, dev->of_node);
1621 if (!master) {
1622 ret = -ENODEV;
1623 goto out_put_group;
1624 }
1625
1626 cfg = &master->cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001627 }
1628
Will Deacon8f68f8e2014-07-15 11:27:08 +01001629 iommu_group_set_iommudata(group, cfg, releasefn);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001630 ret = iommu_group_add_device(group, dev);
1631
1632out_put_group:
1633 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001634 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001635}
1636
1637static void arm_smmu_remove_device(struct device *dev)
1638{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001639 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001640}
1641
Thierry Redingb22f6432014-06-27 09:03:12 +02001642static const struct iommu_ops arm_smmu_ops = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001643 .domain_init = arm_smmu_domain_init,
1644 .domain_destroy = arm_smmu_domain_destroy,
1645 .attach_dev = arm_smmu_attach_dev,
1646 .detach_dev = arm_smmu_detach_dev,
1647 .map = arm_smmu_map,
1648 .unmap = arm_smmu_unmap,
1649 .iova_to_phys = arm_smmu_iova_to_phys,
1650 .domain_has_cap = arm_smmu_domain_has_cap,
1651 .add_device = arm_smmu_add_device,
1652 .remove_device = arm_smmu_remove_device,
1653 .pgsize_bitmap = (SECTION_SIZE |
1654 ARM_SMMU_PTE_CONT_SIZE |
1655 PAGE_SIZE),
1656};
1657
1658static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1659{
1660 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001661 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001662 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001663 u32 reg;
1664
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001665 /* clear global FSR */
1666 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1667 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001668
1669 /* Mark all SMRn as invalid and all S2CRn as bypass */
1670 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001671 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001672 writel_relaxed(S2CR_TYPE_BYPASS,
1673 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001674 }
1675
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001676 /* Make sure all context banks are disabled and clear CB_FSR */
1677 for (i = 0; i < smmu->num_context_banks; ++i) {
1678 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1679 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1680 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1681 }
Will Deacon1463fe42013-07-31 19:21:27 +01001682
Will Deacon45ae7cf2013-06-24 18:31:25 +01001683 /* Invalidate the TLB, just in case */
1684 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1685 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1686 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1687
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001688 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001689
Will Deacon45ae7cf2013-06-24 18:31:25 +01001690 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001691 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001692
1693 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001694 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001695
1696 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001697 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001698
1699 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001700 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001701
1702 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001703 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001704
1705 /* Push the button */
1706 arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001707 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001708}
1709
1710static int arm_smmu_id_size_to_bits(int size)
1711{
1712 switch (size) {
1713 case 0:
1714 return 32;
1715 case 1:
1716 return 36;
1717 case 2:
1718 return 40;
1719 case 3:
1720 return 42;
1721 case 4:
1722 return 44;
1723 case 5:
1724 default:
1725 return 48;
1726 }
1727}
1728
1729static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1730{
1731 unsigned long size;
1732 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1733 u32 id;
1734
1735 dev_notice(smmu->dev, "probing hardware configuration...\n");
1736
1737 /* Primecell ID */
1738 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1739 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1740 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1741
1742 /* ID0 */
1743 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1744#ifndef CONFIG_64BIT
1745 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1746 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1747 return -ENODEV;
1748 }
1749#endif
Will Deacon4cf740b2014-07-14 19:47:39 +01001750
1751 /* Restrict available stages based on module parameter */
1752 if (force_stage == 1)
1753 id &= ~(ID0_S2TS | ID0_NTS);
1754 else if (force_stage == 2)
1755 id &= ~(ID0_S1TS | ID0_NTS);
1756
Will Deacon45ae7cf2013-06-24 18:31:25 +01001757 if (id & ID0_S1TS) {
1758 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1759 dev_notice(smmu->dev, "\tstage 1 translation\n");
1760 }
1761
1762 if (id & ID0_S2TS) {
1763 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1764 dev_notice(smmu->dev, "\tstage 2 translation\n");
1765 }
1766
1767 if (id & ID0_NTS) {
1768 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1769 dev_notice(smmu->dev, "\tnested translation\n");
1770 }
1771
1772 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001773 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001774 dev_err(smmu->dev, "\tno translation support!\n");
1775 return -ENODEV;
1776 }
1777
1778 if (id & ID0_CTTW) {
1779 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1780 dev_notice(smmu->dev, "\tcoherent table walk\n");
1781 }
1782
1783 if (id & ID0_SMS) {
1784 u32 smr, sid, mask;
1785
1786 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1787 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1788 ID0_NUMSMRG_MASK;
1789 if (smmu->num_mapping_groups == 0) {
1790 dev_err(smmu->dev,
1791 "stream-matching supported, but no SMRs present!\n");
1792 return -ENODEV;
1793 }
1794
1795 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1796 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1797 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1798 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1799
1800 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1801 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1802 if ((mask & sid) != sid) {
1803 dev_err(smmu->dev,
1804 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1805 mask, sid);
1806 return -ENODEV;
1807 }
1808
1809 dev_notice(smmu->dev,
1810 "\tstream matching with %u register groups, mask 0x%x",
1811 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001812 } else {
1813 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1814 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001815 }
1816
1817 /* ID1 */
1818 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001819 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001820
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001821 /* Check for size mismatch of SMMU address space from mapped region */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001822 size = 1 <<
1823 (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001824 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001825 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001826 dev_warn(smmu->dev,
1827 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1828 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001829
1830 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1831 ID1_NUMS2CB_MASK;
1832 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1833 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1834 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1835 return -ENODEV;
1836 }
1837 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1838 smmu->num_context_banks, smmu->num_s2_context_banks);
1839
1840 /* ID2 */
1841 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1842 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon28d60072014-09-01 16:24:48 +01001843 smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001844
Will Deacon28d60072014-09-01 16:24:48 +01001845 /* Stage-2 input size limited due to pgd allocation (PTRS_PER_PGD) */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001846#ifdef CONFIG_64BIT
Will Deacon28d60072014-09-01 16:24:48 +01001847 smmu->s2_input_size = min_t(unsigned long, VA_BITS, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001848#else
Will Deacon28d60072014-09-01 16:24:48 +01001849 smmu->s2_input_size = min(32UL, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001850#endif
1851
1852 /* The stage-2 output mask is also applied for bypass */
1853 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001854 smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001855
1856 if (smmu->version == 1) {
Will Deacon28d60072014-09-01 16:24:48 +01001857 smmu->s1_input_size = 32;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001858 } else {
1859#ifdef CONFIG_64BIT
1860 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon06f983d2013-11-05 15:55:04 +00001861 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001862#else
1863 size = 32;
1864#endif
Will Deacon28d60072014-09-01 16:24:48 +01001865 smmu->s1_input_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001866
1867 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1868 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1869 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1870 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1871 PAGE_SIZE);
1872 return -ENODEV;
1873 }
1874 }
1875
Will Deacon28d60072014-09-01 16:24:48 +01001876 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1877 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1878 smmu->s1_input_size, smmu->s1_output_size);
1879
1880 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1881 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1882 smmu->s2_input_size, smmu->s2_output_size);
1883
Will Deacon45ae7cf2013-06-24 18:31:25 +01001884 return 0;
1885}
1886
1887static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1888{
1889 struct resource *res;
1890 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001891 struct device *dev = &pdev->dev;
1892 struct rb_node *node;
1893 struct of_phandle_args masterspec;
1894 int num_irqs, i, err;
1895
1896 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1897 if (!smmu) {
1898 dev_err(dev, "failed to allocate arm_smmu_device\n");
1899 return -ENOMEM;
1900 }
1901 smmu->dev = dev;
1902
1903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001904 smmu->base = devm_ioremap_resource(dev, res);
1905 if (IS_ERR(smmu->base))
1906 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001907 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001908
1909 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1910 &smmu->num_global_irqs)) {
1911 dev_err(dev, "missing #global-interrupts property\n");
1912 return -ENODEV;
1913 }
1914
1915 num_irqs = 0;
1916 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1917 num_irqs++;
1918 if (num_irqs > smmu->num_global_irqs)
1919 smmu->num_context_irqs++;
1920 }
1921
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001922 if (!smmu->num_context_irqs) {
1923 dev_err(dev, "found %d interrupts but expected at least %d\n",
1924 num_irqs, smmu->num_global_irqs + 1);
1925 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001926 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001927
1928 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1929 GFP_KERNEL);
1930 if (!smmu->irqs) {
1931 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1932 return -ENOMEM;
1933 }
1934
1935 for (i = 0; i < num_irqs; ++i) {
1936 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001937
Will Deacon45ae7cf2013-06-24 18:31:25 +01001938 if (irq < 0) {
1939 dev_err(dev, "failed to get irq index %d\n", i);
1940 return -ENODEV;
1941 }
1942 smmu->irqs[i] = irq;
1943 }
1944
Olav Haugan3c8766d2014-08-22 17:12:32 -07001945 err = arm_smmu_device_cfg_probe(smmu);
1946 if (err)
1947 return err;
1948
Will Deacon45ae7cf2013-06-24 18:31:25 +01001949 i = 0;
1950 smmu->masters = RB_ROOT;
1951 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1952 "#stream-id-cells", i,
1953 &masterspec)) {
1954 err = register_smmu_master(smmu, dev, &masterspec);
1955 if (err) {
1956 dev_err(dev, "failed to add master %s\n",
1957 masterspec.np->name);
1958 goto out_put_masters;
1959 }
1960
1961 i++;
1962 }
1963 dev_notice(dev, "registered %d master devices\n", i);
1964
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001965 parse_driver_options(smmu);
1966
Will Deacon45ae7cf2013-06-24 18:31:25 +01001967 if (smmu->version > 1 &&
1968 smmu->num_context_banks != smmu->num_context_irqs) {
1969 dev_err(dev,
1970 "found only %d context interrupt(s) but %d required\n",
1971 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001972 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001973 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001974 }
1975
Will Deacon45ae7cf2013-06-24 18:31:25 +01001976 for (i = 0; i < smmu->num_global_irqs; ++i) {
1977 err = request_irq(smmu->irqs[i],
1978 arm_smmu_global_fault,
1979 IRQF_SHARED,
1980 "arm-smmu global fault",
1981 smmu);
1982 if (err) {
1983 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1984 i, smmu->irqs[i]);
1985 goto out_free_irqs;
1986 }
1987 }
1988
1989 INIT_LIST_HEAD(&smmu->list);
1990 spin_lock(&arm_smmu_devices_lock);
1991 list_add(&smmu->list, &arm_smmu_devices);
1992 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001993
1994 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001995 return 0;
1996
1997out_free_irqs:
1998 while (i--)
1999 free_irq(smmu->irqs[i], smmu);
2000
Will Deacon45ae7cf2013-06-24 18:31:25 +01002001out_put_masters:
2002 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07002003 struct arm_smmu_master *master
2004 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002005 of_node_put(master->of_node);
2006 }
2007
2008 return err;
2009}
2010
2011static int arm_smmu_device_remove(struct platform_device *pdev)
2012{
2013 int i;
2014 struct device *dev = &pdev->dev;
2015 struct arm_smmu_device *curr, *smmu = NULL;
2016 struct rb_node *node;
2017
2018 spin_lock(&arm_smmu_devices_lock);
2019 list_for_each_entry(curr, &arm_smmu_devices, list) {
2020 if (curr->dev == dev) {
2021 smmu = curr;
2022 list_del(&smmu->list);
2023 break;
2024 }
2025 }
2026 spin_unlock(&arm_smmu_devices_lock);
2027
2028 if (!smmu)
2029 return -ENODEV;
2030
Will Deacon45ae7cf2013-06-24 18:31:25 +01002031 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07002032 struct arm_smmu_master *master
2033 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002034 of_node_put(master->of_node);
2035 }
2036
Will Deaconecfadb62013-07-31 19:21:28 +01002037 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002038 dev_err(dev, "removing device with active domains!\n");
2039
2040 for (i = 0; i < smmu->num_global_irqs; ++i)
2041 free_irq(smmu->irqs[i], smmu);
2042
2043 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07002044 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002045 return 0;
2046}
2047
2048#ifdef CONFIG_OF
2049static struct of_device_id arm_smmu_of_match[] = {
2050 { .compatible = "arm,smmu-v1", },
2051 { .compatible = "arm,smmu-v2", },
2052 { .compatible = "arm,mmu-400", },
2053 { .compatible = "arm,mmu-500", },
2054 { },
2055};
2056MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2057#endif
2058
2059static struct platform_driver arm_smmu_driver = {
2060 .driver = {
2061 .owner = THIS_MODULE,
2062 .name = "arm-smmu",
2063 .of_match_table = of_match_ptr(arm_smmu_of_match),
2064 },
2065 .probe = arm_smmu_device_dt_probe,
2066 .remove = arm_smmu_device_remove,
2067};
2068
2069static int __init arm_smmu_init(void)
2070{
2071 int ret;
2072
2073 ret = platform_driver_register(&arm_smmu_driver);
2074 if (ret)
2075 return ret;
2076
2077 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01002078 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002079 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2080
Will Deacond123cf82014-02-04 22:17:53 +00002081#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01002082 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002083 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00002084#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01002085
Will Deacona9a1b0b2014-05-01 18:05:08 +01002086#ifdef CONFIG_PCI
2087 if (!iommu_present(&pci_bus_type))
2088 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2089#endif
2090
Will Deacon45ae7cf2013-06-24 18:31:25 +01002091 return 0;
2092}
2093
2094static void __exit arm_smmu_exit(void)
2095{
2096 return platform_driver_unregister(&arm_smmu_driver);
2097}
2098
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002099subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002100module_exit(arm_smmu_exit);
2101
2102MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2103MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2104MODULE_LICENSE("GPL v2");