blob: 5575c3d485d0c799aeb6965239d308087149763c [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
Will Deacon45ae7cf2013-06-24 18:31:25 +010026 * - Context fault reporting
27 */
28
29#define pr_fmt(fmt) "arm-smmu: " fmt
30
31#include <linux/delay.h>
32#include <linux/dma-mapping.h>
33#include <linux/err.h>
34#include <linux/interrupt.h>
35#include <linux/io.h>
36#include <linux/iommu.h>
Mitchel Humpherys859a7322014-10-29 21:13:40 +000037#include <linux/iopoll.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010038#include <linux/module.h>
39#include <linux/of.h>
Will Deacona9a1b0b2014-05-01 18:05:08 +010040#include <linux/pci.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010041#include <linux/platform_device.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44
45#include <linux/amba/bus.h>
46
Will Deacon518f7132014-11-14 17:17:54 +000047#include "io-pgtable.h"
Will Deacon45ae7cf2013-06-24 18:31:25 +010048
49/* Maximum number of stream IDs assigned to a single device */
Andreas Herrmann636e97b2014-01-30 18:18:08 +000050#define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
Will Deacon45ae7cf2013-06-24 18:31:25 +010051
52/* Maximum number of context banks per SMMU */
53#define ARM_SMMU_MAX_CBS 128
54
55/* Maximum number of mapping groups per SMMU */
56#define ARM_SMMU_MAX_SMRS 128
57
Will Deacon45ae7cf2013-06-24 18:31:25 +010058/* SMMU global address space */
59#define ARM_SMMU_GR0(smmu) ((smmu)->base)
Will Deaconc757e852014-07-30 11:33:25 +010060#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +010061
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +000062/*
63 * SMMU global address space with conditional offset to access secure
64 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
65 * nsGFSYNR0: 0x450)
66 */
67#define ARM_SMMU_GR0_NS(smmu) \
68 ((smmu)->base + \
69 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
70 ? 0x400 : 0))
71
Will Deacon45ae7cf2013-06-24 18:31:25 +010072/* Configuration registers */
73#define ARM_SMMU_GR0_sCR0 0x0
74#define sCR0_CLIENTPD (1 << 0)
75#define sCR0_GFRE (1 << 1)
76#define sCR0_GFIE (1 << 2)
77#define sCR0_GCFGFRE (1 << 4)
78#define sCR0_GCFGFIE (1 << 5)
79#define sCR0_USFCFG (1 << 10)
80#define sCR0_VMIDPNE (1 << 11)
81#define sCR0_PTM (1 << 12)
82#define sCR0_FB (1 << 13)
83#define sCR0_BSU_SHIFT 14
84#define sCR0_BSU_MASK 0x3
85
86/* Identification registers */
87#define ARM_SMMU_GR0_ID0 0x20
88#define ARM_SMMU_GR0_ID1 0x24
89#define ARM_SMMU_GR0_ID2 0x28
90#define ARM_SMMU_GR0_ID3 0x2c
91#define ARM_SMMU_GR0_ID4 0x30
92#define ARM_SMMU_GR0_ID5 0x34
93#define ARM_SMMU_GR0_ID6 0x38
94#define ARM_SMMU_GR0_ID7 0x3c
95#define ARM_SMMU_GR0_sGFSR 0x48
96#define ARM_SMMU_GR0_sGFSYNR0 0x50
97#define ARM_SMMU_GR0_sGFSYNR1 0x54
98#define ARM_SMMU_GR0_sGFSYNR2 0x58
Will Deacon45ae7cf2013-06-24 18:31:25 +010099
100#define ID0_S1TS (1 << 30)
101#define ID0_S2TS (1 << 29)
102#define ID0_NTS (1 << 28)
103#define ID0_SMS (1 << 27)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000104#define ID0_ATOSNS (1 << 26)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100105#define ID0_CTTW (1 << 14)
106#define ID0_NUMIRPT_SHIFT 16
107#define ID0_NUMIRPT_MASK 0xff
Olav Haugan3c8766d2014-08-22 17:12:32 -0700108#define ID0_NUMSIDB_SHIFT 9
109#define ID0_NUMSIDB_MASK 0xf
Will Deacon45ae7cf2013-06-24 18:31:25 +0100110#define ID0_NUMSMRG_SHIFT 0
111#define ID0_NUMSMRG_MASK 0xff
112
113#define ID1_PAGESIZE (1 << 31)
114#define ID1_NUMPAGENDXB_SHIFT 28
115#define ID1_NUMPAGENDXB_MASK 7
116#define ID1_NUMS2CB_SHIFT 16
117#define ID1_NUMS2CB_MASK 0xff
118#define ID1_NUMCB_SHIFT 0
119#define ID1_NUMCB_MASK 0xff
120
121#define ID2_OAS_SHIFT 4
122#define ID2_OAS_MASK 0xf
123#define ID2_IAS_SHIFT 0
124#define ID2_IAS_MASK 0xf
125#define ID2_UBS_SHIFT 8
126#define ID2_UBS_MASK 0xf
127#define ID2_PTFS_4K (1 << 12)
128#define ID2_PTFS_16K (1 << 13)
129#define ID2_PTFS_64K (1 << 14)
130
Will Deacon45ae7cf2013-06-24 18:31:25 +0100131/* Global TLB invalidation */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100132#define ARM_SMMU_GR0_TLBIVMID 0x64
133#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
134#define ARM_SMMU_GR0_TLBIALLH 0x6c
135#define ARM_SMMU_GR0_sTLBGSYNC 0x70
136#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
137#define sTLBGSTATUS_GSACTIVE (1 << 0)
138#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
139
140/* Stream mapping registers */
141#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
142#define SMR_VALID (1 << 31)
143#define SMR_MASK_SHIFT 16
144#define SMR_MASK_MASK 0x7fff
145#define SMR_ID_SHIFT 0
146#define SMR_ID_MASK 0x7fff
147
148#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
149#define S2CR_CBNDX_SHIFT 0
150#define S2CR_CBNDX_MASK 0xff
151#define S2CR_TYPE_SHIFT 16
152#define S2CR_TYPE_MASK 0x3
153#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
154#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
155#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
156
157/* Context bank attribute registers */
158#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
159#define CBAR_VMID_SHIFT 0
160#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000161#define CBAR_S1_BPSHCFG_SHIFT 8
162#define CBAR_S1_BPSHCFG_MASK 3
163#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100164#define CBAR_S1_MEMATTR_SHIFT 12
165#define CBAR_S1_MEMATTR_MASK 0xf
166#define CBAR_S1_MEMATTR_WB 0xf
167#define CBAR_TYPE_SHIFT 16
168#define CBAR_TYPE_MASK 0x3
169#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
170#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
171#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
172#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
173#define CBAR_IRPTNDX_SHIFT 24
174#define CBAR_IRPTNDX_MASK 0xff
175
176#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
177#define CBA2R_RW64_32BIT (0 << 0)
178#define CBA2R_RW64_64BIT (1 << 0)
179
180/* Translation context bank */
181#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
Will Deaconc757e852014-07-30 11:33:25 +0100182#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +0100183
184#define ARM_SMMU_CB_SCTLR 0x0
185#define ARM_SMMU_CB_RESUME 0x8
186#define ARM_SMMU_CB_TTBCR2 0x10
187#define ARM_SMMU_CB_TTBR0_LO 0x20
188#define ARM_SMMU_CB_TTBR0_HI 0x24
Will Deacon518f7132014-11-14 17:17:54 +0000189#define ARM_SMMU_CB_TTBR1_LO 0x28
190#define ARM_SMMU_CB_TTBR1_HI 0x2c
Will Deacon45ae7cf2013-06-24 18:31:25 +0100191#define ARM_SMMU_CB_TTBCR 0x30
192#define ARM_SMMU_CB_S1_MAIR0 0x38
Will Deacon518f7132014-11-14 17:17:54 +0000193#define ARM_SMMU_CB_S1_MAIR1 0x3c
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000194#define ARM_SMMU_CB_PAR_LO 0x50
195#define ARM_SMMU_CB_PAR_HI 0x54
Will Deacon45ae7cf2013-06-24 18:31:25 +0100196#define ARM_SMMU_CB_FSR 0x58
197#define ARM_SMMU_CB_FAR_LO 0x60
198#define ARM_SMMU_CB_FAR_HI 0x64
199#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon518f7132014-11-14 17:17:54 +0000200#define ARM_SMMU_CB_S1_TLBIVA 0x600
Will Deacon1463fe42013-07-31 19:21:27 +0100201#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon518f7132014-11-14 17:17:54 +0000202#define ARM_SMMU_CB_S1_TLBIVAL 0x620
203#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
204#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000205#define ARM_SMMU_CB_ATS1PR_LO 0x800
206#define ARM_SMMU_CB_ATS1PR_HI 0x804
207#define ARM_SMMU_CB_ATSR 0x8f0
Will Deacon45ae7cf2013-06-24 18:31:25 +0100208
209#define SCTLR_S1_ASIDPNE (1 << 12)
210#define SCTLR_CFCFG (1 << 7)
211#define SCTLR_CFIE (1 << 6)
212#define SCTLR_CFRE (1 << 5)
213#define SCTLR_E (1 << 4)
214#define SCTLR_AFE (1 << 2)
215#define SCTLR_TRE (1 << 1)
216#define SCTLR_M (1 << 0)
217#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
218
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000219#define CB_PAR_F (1 << 0)
220
221#define ATSR_ACTIVE (1 << 0)
222
Will Deacon45ae7cf2013-06-24 18:31:25 +0100223#define RESUME_RETRY (0 << 0)
224#define RESUME_TERMINATE (1 << 0)
225
Will Deacon45ae7cf2013-06-24 18:31:25 +0100226#define TTBCR2_SEP_SHIFT 15
227#define TTBCR2_SEP_MASK 0x7
228
Will Deacon45ae7cf2013-06-24 18:31:25 +0100229#define TTBCR2_ADDR_32 0
230#define TTBCR2_ADDR_36 1
231#define TTBCR2_ADDR_40 2
232#define TTBCR2_ADDR_42 3
233#define TTBCR2_ADDR_44 4
234#define TTBCR2_ADDR_48 5
235
Will Deacon518f7132014-11-14 17:17:54 +0000236#define TTBRn_HI_ASID_SHIFT 16
Will Deacon45ae7cf2013-06-24 18:31:25 +0100237
238#define FSR_MULTI (1 << 31)
239#define FSR_SS (1 << 30)
240#define FSR_UUT (1 << 8)
241#define FSR_ASF (1 << 7)
242#define FSR_TLBLKF (1 << 6)
243#define FSR_TLBMCF (1 << 5)
244#define FSR_EF (1 << 4)
245#define FSR_PF (1 << 3)
246#define FSR_AFF (1 << 2)
247#define FSR_TF (1 << 1)
248
Mitchel Humpherys29073202014-07-08 09:52:18 -0700249#define FSR_IGN (FSR_AFF | FSR_ASF | \
250 FSR_TLBMCF | FSR_TLBLKF)
251#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100252 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100253
254#define FSYNR0_WNR (1 << 4)
255
Will Deacon4cf740b2014-07-14 19:47:39 +0100256static int force_stage;
257module_param_named(force_stage, force_stage, int, S_IRUGO | S_IWUSR);
258MODULE_PARM_DESC(force_stage,
259 "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.");
260
Robin Murphy09360402014-08-28 17:51:59 +0100261enum arm_smmu_arch_version {
262 ARM_SMMU_V1 = 1,
263 ARM_SMMU_V2,
264};
265
Will Deacon45ae7cf2013-06-24 18:31:25 +0100266struct arm_smmu_smr {
267 u8 idx;
268 u16 mask;
269 u16 id;
270};
271
Will Deacona9a1b0b2014-05-01 18:05:08 +0100272struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100273 int num_streamids;
274 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100275 struct arm_smmu_smr *smrs;
276};
277
Will Deacona9a1b0b2014-05-01 18:05:08 +0100278struct arm_smmu_master {
279 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100280 struct rb_node node;
281 struct arm_smmu_master_cfg cfg;
282};
283
Will Deacon45ae7cf2013-06-24 18:31:25 +0100284struct arm_smmu_device {
285 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100286
287 void __iomem *base;
288 unsigned long size;
Will Deaconc757e852014-07-30 11:33:25 +0100289 unsigned long pgshift;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100290
291#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
292#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
293#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
294#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
295#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000296#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100297 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000298
299#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
300 u32 options;
Robin Murphy09360402014-08-28 17:51:59 +0100301 enum arm_smmu_arch_version version;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100302
303 u32 num_context_banks;
304 u32 num_s2_context_banks;
305 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
306 atomic_t irptndx;
307
308 u32 num_mapping_groups;
309 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
310
Will Deacon518f7132014-11-14 17:17:54 +0000311 unsigned long va_size;
312 unsigned long ipa_size;
313 unsigned long pa_size;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100314
315 u32 num_global_irqs;
316 u32 num_context_irqs;
317 unsigned int *irqs;
318
Will Deacon45ae7cf2013-06-24 18:31:25 +0100319 struct list_head list;
320 struct rb_root masters;
321};
322
323struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100324 u8 cbndx;
325 u8 irptndx;
326 u32 cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100327};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100328#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100329
Will Deaconecfadb62013-07-31 19:21:28 +0100330#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
331#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
332
Will Deaconc752ce42014-06-25 22:46:31 +0100333enum arm_smmu_domain_stage {
334 ARM_SMMU_DOMAIN_S1 = 0,
335 ARM_SMMU_DOMAIN_S2,
336 ARM_SMMU_DOMAIN_NESTED,
337};
338
Will Deacon45ae7cf2013-06-24 18:31:25 +0100339struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100340 struct arm_smmu_device *smmu;
Will Deacon518f7132014-11-14 17:17:54 +0000341 struct io_pgtable_ops *pgtbl_ops;
342 spinlock_t pgtbl_lock;
Will Deacon44680ee2014-06-25 11:29:12 +0100343 struct arm_smmu_cfg cfg;
Will Deaconc752ce42014-06-25 22:46:31 +0100344 enum arm_smmu_domain_stage stage;
Will Deacon518f7132014-11-14 17:17:54 +0000345 struct mutex init_mutex; /* Protects smmu pointer */
Joerg Roedel1d672632015-03-26 13:43:10 +0100346 struct iommu_domain domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100347};
348
Will Deacon518f7132014-11-14 17:17:54 +0000349static struct iommu_ops arm_smmu_ops;
350
Will Deacon45ae7cf2013-06-24 18:31:25 +0100351static DEFINE_SPINLOCK(arm_smmu_devices_lock);
352static LIST_HEAD(arm_smmu_devices);
353
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000354struct arm_smmu_option_prop {
355 u32 opt;
356 const char *prop;
357};
358
Mitchel Humpherys29073202014-07-08 09:52:18 -0700359static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000360 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
361 { 0, NULL},
362};
363
Joerg Roedel1d672632015-03-26 13:43:10 +0100364static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
365{
366 return container_of(dom, struct arm_smmu_domain, domain);
367}
368
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000369static void parse_driver_options(struct arm_smmu_device *smmu)
370{
371 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700372
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000373 do {
374 if (of_property_read_bool(smmu->dev->of_node,
375 arm_smmu_options[i].prop)) {
376 smmu->options |= arm_smmu_options[i].opt;
377 dev_notice(smmu->dev, "option %s\n",
378 arm_smmu_options[i].prop);
379 }
380 } while (arm_smmu_options[++i].opt);
381}
382
Will Deacon8f68f8e2014-07-15 11:27:08 +0100383static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100384{
385 if (dev_is_pci(dev)) {
386 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700387
Will Deacona9a1b0b2014-05-01 18:05:08 +0100388 while (!pci_is_root_bus(bus))
389 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100390 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100391 }
392
Will Deacon8f68f8e2014-07-15 11:27:08 +0100393 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100394}
395
Will Deacon45ae7cf2013-06-24 18:31:25 +0100396static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
397 struct device_node *dev_node)
398{
399 struct rb_node *node = smmu->masters.rb_node;
400
401 while (node) {
402 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700403
Will Deacon45ae7cf2013-06-24 18:31:25 +0100404 master = container_of(node, struct arm_smmu_master, node);
405
406 if (dev_node < master->of_node)
407 node = node->rb_left;
408 else if (dev_node > master->of_node)
409 node = node->rb_right;
410 else
411 return master;
412 }
413
414 return NULL;
415}
416
Will Deacona9a1b0b2014-05-01 18:05:08 +0100417static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100418find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100419{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100420 struct arm_smmu_master_cfg *cfg = NULL;
421 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100422
Will Deacon8f68f8e2014-07-15 11:27:08 +0100423 if (group) {
424 cfg = iommu_group_get_iommudata(group);
425 iommu_group_put(group);
426 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100427
Will Deacon8f68f8e2014-07-15 11:27:08 +0100428 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100429}
430
Will Deacon45ae7cf2013-06-24 18:31:25 +0100431static int insert_smmu_master(struct arm_smmu_device *smmu,
432 struct arm_smmu_master *master)
433{
434 struct rb_node **new, *parent;
435
436 new = &smmu->masters.rb_node;
437 parent = NULL;
438 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700439 struct arm_smmu_master *this
440 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100441
442 parent = *new;
443 if (master->of_node < this->of_node)
444 new = &((*new)->rb_left);
445 else if (master->of_node > this->of_node)
446 new = &((*new)->rb_right);
447 else
448 return -EEXIST;
449 }
450
451 rb_link_node(&master->node, parent, new);
452 rb_insert_color(&master->node, &smmu->masters);
453 return 0;
454}
455
456static int register_smmu_master(struct arm_smmu_device *smmu,
457 struct device *dev,
458 struct of_phandle_args *masterspec)
459{
460 int i;
461 struct arm_smmu_master *master;
462
463 master = find_smmu_master(smmu, masterspec->np);
464 if (master) {
465 dev_err(dev,
466 "rejecting multiple registrations for master device %s\n",
467 masterspec->np->name);
468 return -EBUSY;
469 }
470
471 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
472 dev_err(dev,
473 "reached maximum number (%d) of stream IDs for master device %s\n",
474 MAX_MASTER_STREAMIDS, masterspec->np->name);
475 return -ENOSPC;
476 }
477
478 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
479 if (!master)
480 return -ENOMEM;
481
Will Deacona9a1b0b2014-05-01 18:05:08 +0100482 master->of_node = masterspec->np;
483 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100484
Olav Haugan3c8766d2014-08-22 17:12:32 -0700485 for (i = 0; i < master->cfg.num_streamids; ++i) {
486 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100487
Olav Haugan3c8766d2014-08-22 17:12:32 -0700488 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
489 (streamid >= smmu->num_mapping_groups)) {
490 dev_err(dev,
491 "stream ID for master device %s greater than maximum allowed (%d)\n",
492 masterspec->np->name, smmu->num_mapping_groups);
493 return -ERANGE;
494 }
495 master->cfg.streamids[i] = streamid;
496 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100497 return insert_smmu_master(smmu, master);
498}
499
Will Deacon44680ee2014-06-25 11:29:12 +0100500static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100501{
Will Deacon44680ee2014-06-25 11:29:12 +0100502 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100503 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100504 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100505
506 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100507 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100508 master = find_smmu_master(smmu, dev_node);
509 if (master)
510 break;
511 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100512 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100513
Will Deacona9a1b0b2014-05-01 18:05:08 +0100514 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100515}
516
517static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
518{
519 int idx;
520
521 do {
522 idx = find_next_zero_bit(map, end, start);
523 if (idx == end)
524 return -ENOSPC;
525 } while (test_and_set_bit(idx, map));
526
527 return idx;
528}
529
530static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
531{
532 clear_bit(idx, map);
533}
534
535/* Wait for any pending TLB invalidations to complete */
Will Deacon518f7132014-11-14 17:17:54 +0000536static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100537{
538 int count = 0;
539 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
540
541 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
542 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
543 & sTLBGSTATUS_GSACTIVE) {
544 cpu_relax();
545 if (++count == TLB_LOOP_TIMEOUT) {
546 dev_err_ratelimited(smmu->dev,
547 "TLB sync timed out -- SMMU may be deadlocked\n");
548 return;
549 }
550 udelay(1);
551 }
552}
553
Will Deacon518f7132014-11-14 17:17:54 +0000554static void arm_smmu_tlb_sync(void *cookie)
Will Deacon1463fe42013-07-31 19:21:27 +0100555{
Will Deacon518f7132014-11-14 17:17:54 +0000556 struct arm_smmu_domain *smmu_domain = cookie;
557 __arm_smmu_tlb_sync(smmu_domain->smmu);
558}
559
560static void arm_smmu_tlb_inv_context(void *cookie)
561{
562 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100563 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
564 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100565 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon518f7132014-11-14 17:17:54 +0000566 void __iomem *base;
Will Deacon1463fe42013-07-31 19:21:27 +0100567
568 if (stage1) {
569 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100570 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
571 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100572 } else {
573 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100574 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
575 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100576 }
577
Will Deacon518f7132014-11-14 17:17:54 +0000578 __arm_smmu_tlb_sync(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100579}
580
Will Deacon518f7132014-11-14 17:17:54 +0000581static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
582 bool leaf, void *cookie)
583{
584 struct arm_smmu_domain *smmu_domain = cookie;
585 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
586 struct arm_smmu_device *smmu = smmu_domain->smmu;
587 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
588 void __iomem *reg;
589
590 if (stage1) {
591 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
592 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
593
594 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
595 iova &= ~12UL;
596 iova |= ARM_SMMU_CB_ASID(cfg);
597 writel_relaxed(iova, reg);
598#ifdef CONFIG_64BIT
599 } else {
600 iova >>= 12;
601 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
602 writeq_relaxed(iova, reg);
603#endif
604 }
605#ifdef CONFIG_64BIT
606 } else if (smmu->version == ARM_SMMU_V2) {
607 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
608 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
609 ARM_SMMU_CB_S2_TLBIIPAS2;
610 writeq_relaxed(iova >> 12, reg);
611#endif
612 } else {
613 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
614 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
615 }
616}
617
618static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)
619{
620 struct arm_smmu_domain *smmu_domain = cookie;
621 struct arm_smmu_device *smmu = smmu_domain->smmu;
622 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
623
624
625 /* Ensure new page tables are visible to the hardware walker */
626 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
627 dsb(ishst);
628 } else {
629 /*
630 * If the SMMU can't walk tables in the CPU caches, treat them
631 * like non-coherent DMA since we need to flush the new entries
632 * all the way out to memory. There's no possibility of
633 * recursion here as the SMMU table walker will not be wired
634 * through another SMMU.
635 */
636 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
637 DMA_TO_DEVICE);
638 }
639}
640
641static struct iommu_gather_ops arm_smmu_gather_ops = {
642 .tlb_flush_all = arm_smmu_tlb_inv_context,
643 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
644 .tlb_sync = arm_smmu_tlb_sync,
645 .flush_pgtable = arm_smmu_flush_pgtable,
646};
647
Will Deacon45ae7cf2013-06-24 18:31:25 +0100648static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
649{
650 int flags, ret;
651 u32 fsr, far, fsynr, resume;
652 unsigned long iova;
653 struct iommu_domain *domain = dev;
Joerg Roedel1d672632015-03-26 13:43:10 +0100654 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100655 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
656 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100657 void __iomem *cb_base;
658
Will Deacon44680ee2014-06-25 11:29:12 +0100659 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100660 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
661
662 if (!(fsr & FSR_FAULT))
663 return IRQ_NONE;
664
665 if (fsr & FSR_IGN)
666 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100667 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100668 fsr);
669
670 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
671 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
672
673 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
674 iova = far;
675#ifdef CONFIG_64BIT
676 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
677 iova |= ((unsigned long)far << 32);
678#endif
679
680 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
681 ret = IRQ_HANDLED;
682 resume = RESUME_RETRY;
683 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100684 dev_err_ratelimited(smmu->dev,
685 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100686 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100687 ret = IRQ_NONE;
688 resume = RESUME_TERMINATE;
689 }
690
691 /* Clear the faulting FSR */
692 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
693
694 /* Retry or terminate any stalled transactions */
695 if (fsr & FSR_SS)
696 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
697
698 return ret;
699}
700
701static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
702{
703 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
704 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000705 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100706
707 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
708 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
709 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
710 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
711
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000712 if (!gfsr)
713 return IRQ_NONE;
714
Will Deacon45ae7cf2013-06-24 18:31:25 +0100715 dev_err_ratelimited(smmu->dev,
716 "Unexpected global fault, this could be serious\n");
717 dev_err_ratelimited(smmu->dev,
718 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
719 gfsr, gfsynr0, gfsynr1, gfsynr2);
720
721 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100722 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100723}
724
Will Deacon518f7132014-11-14 17:17:54 +0000725static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
726 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100727{
728 u32 reg;
729 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100730 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
731 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100732 void __iomem *cb_base, *gr0_base, *gr1_base;
733
734 gr0_base = ARM_SMMU_GR0(smmu);
735 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100736 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
737 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100738
739 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100740 reg = cfg->cbar;
Robin Murphy09360402014-08-28 17:51:59 +0100741 if (smmu->version == ARM_SMMU_V1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700742 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100743
Will Deacon57ca90f2014-02-06 14:59:05 +0000744 /*
745 * Use the weakest shareability/memory types, so they are
746 * overridden by the ttbcr/pte.
747 */
748 if (stage1) {
749 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
750 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
751 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100752 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000753 }
Will Deacon44680ee2014-06-25 11:29:12 +0100754 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100755
Robin Murphy09360402014-08-28 17:51:59 +0100756 if (smmu->version > ARM_SMMU_V1) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100757 /* CBA2R */
758#ifdef CONFIG_64BIT
759 reg = CBA2R_RW64_64BIT;
760#else
761 reg = CBA2R_RW64_32BIT;
762#endif
Will Deacon518f7132014-11-14 17:17:54 +0000763 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100764 }
765
Will Deacon518f7132014-11-14 17:17:54 +0000766 /* TTBRs */
767 if (stage1) {
768 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
769 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
770 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32;
Will Deacon44680ee2014-06-25 11:29:12 +0100771 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon518f7132014-11-14 17:17:54 +0000772 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100773
Will Deacon518f7132014-11-14 17:17:54 +0000774 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
775 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO);
776 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32;
777 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
778 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI);
779 } else {
780 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
781 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
782 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32;
783 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
784 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100785
Will Deacon518f7132014-11-14 17:17:54 +0000786 /* TTBCR */
787 if (stage1) {
788 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
789 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
790 if (smmu->version > ARM_SMMU_V1) {
791 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
792 switch (smmu->va_size) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100793 case 32:
Will Deacon518f7132014-11-14 17:17:54 +0000794 reg |= (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100795 break;
796 case 36:
Will Deacon518f7132014-11-14 17:17:54 +0000797 reg |= (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100798 break;
799 case 40:
Will Deacon518f7132014-11-14 17:17:54 +0000800 reg |= (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100801 break;
802 case 42:
Will Deacon518f7132014-11-14 17:17:54 +0000803 reg |= (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100804 break;
805 case 44:
Will Deacon518f7132014-11-14 17:17:54 +0000806 reg |= (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100807 break;
808 case 48:
Will Deacon518f7132014-11-14 17:17:54 +0000809 reg |= (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100810 break;
811 }
Will Deacon518f7132014-11-14 17:17:54 +0000812 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100813 }
814 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000815 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
816 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100817 }
818
Will Deacon518f7132014-11-14 17:17:54 +0000819 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100820 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000821 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100822 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000823 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
824 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100825 }
826
Will Deacon45ae7cf2013-06-24 18:31:25 +0100827 /* SCTLR */
828 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
829 if (stage1)
830 reg |= SCTLR_S1_ASIDPNE;
831#ifdef __BIG_ENDIAN
832 reg |= SCTLR_E;
833#endif
Will Deacon25724842013-08-21 13:49:53 +0100834 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100835}
836
837static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100838 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100839{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100840 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000841 unsigned long ias, oas;
842 struct io_pgtable_ops *pgtbl_ops;
843 struct io_pgtable_cfg pgtbl_cfg;
844 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100845 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100846 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100847
Will Deacon518f7132014-11-14 17:17:54 +0000848 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100849 if (smmu_domain->smmu)
850 goto out_unlock;
851
Will Deaconc752ce42014-06-25 22:46:31 +0100852 /*
853 * Mapping the requested stage onto what we support is surprisingly
854 * complicated, mainly because the spec allows S1+S2 SMMUs without
855 * support for nested translation. That means we end up with the
856 * following table:
857 *
858 * Requested Supported Actual
859 * S1 N S1
860 * S1 S1+S2 S1
861 * S1 S2 S2
862 * S1 S1 S1
863 * N N N
864 * N S1+S2 S2
865 * N S2 S2
866 * N S1 S1
867 *
868 * Note that you can't actually request stage-2 mappings.
869 */
870 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
871 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
872 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
873 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
874
875 switch (smmu_domain->stage) {
876 case ARM_SMMU_DOMAIN_S1:
877 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
878 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000879 ias = smmu->va_size;
880 oas = smmu->ipa_size;
881 if (IS_ENABLED(CONFIG_64BIT))
882 fmt = ARM_64_LPAE_S1;
883 else
884 fmt = ARM_32_LPAE_S1;
Will Deaconc752ce42014-06-25 22:46:31 +0100885 break;
886 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100887 /*
888 * We will likely want to change this if/when KVM gets
889 * involved.
890 */
Will Deaconc752ce42014-06-25 22:46:31 +0100891 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100892 cfg->cbar = CBAR_TYPE_S2_TRANS;
893 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000894 ias = smmu->ipa_size;
895 oas = smmu->pa_size;
896 if (IS_ENABLED(CONFIG_64BIT))
897 fmt = ARM_64_LPAE_S2;
898 else
899 fmt = ARM_32_LPAE_S2;
Will Deaconc752ce42014-06-25 22:46:31 +0100900 break;
901 default:
902 ret = -EINVAL;
903 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100904 }
905
906 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
907 smmu->num_context_banks);
908 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100909 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100910
Will Deacon44680ee2014-06-25 11:29:12 +0100911 cfg->cbndx = ret;
Robin Murphy09360402014-08-28 17:51:59 +0100912 if (smmu->version == ARM_SMMU_V1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100913 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
914 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100915 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100916 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100917 }
918
Will Deacon518f7132014-11-14 17:17:54 +0000919 pgtbl_cfg = (struct io_pgtable_cfg) {
920 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
921 .ias = ias,
922 .oas = oas,
923 .tlb = &arm_smmu_gather_ops,
924 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100925
Will Deacon518f7132014-11-14 17:17:54 +0000926 smmu_domain->smmu = smmu;
927 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
928 if (!pgtbl_ops) {
929 ret = -ENOMEM;
930 goto out_clear_smmu;
931 }
932
933 /* Update our support page sizes to reflect the page table format */
934 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
935
936 /* Initialise the context bank with our page table cfg */
937 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
938
939 /*
940 * Request context fault interrupt. Do this last to avoid the
941 * handler seeing a half-initialised domain state.
942 */
Will Deacon44680ee2014-06-25 11:29:12 +0100943 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100944 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
945 "arm-smmu-context-fault", domain);
946 if (IS_ERR_VALUE(ret)) {
947 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100948 cfg->irptndx, irq);
949 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100950 }
951
Will Deacon518f7132014-11-14 17:17:54 +0000952 mutex_unlock(&smmu_domain->init_mutex);
953
954 /* Publish page table ops for map/unmap */
955 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100956 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100957
Will Deacon518f7132014-11-14 17:17:54 +0000958out_clear_smmu:
959 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100960out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000961 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100962 return ret;
963}
964
965static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
966{
Joerg Roedel1d672632015-03-26 13:43:10 +0100967 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100968 struct arm_smmu_device *smmu = smmu_domain->smmu;
969 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100970 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100971 int irq;
972
973 if (!smmu)
974 return;
975
Will Deacon518f7132014-11-14 17:17:54 +0000976 /*
977 * Disable the context bank and free the page tables before freeing
978 * it.
979 */
Will Deacon44680ee2014-06-25 11:29:12 +0100980 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100981 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +0100982
Will Deacon44680ee2014-06-25 11:29:12 +0100983 if (cfg->irptndx != INVALID_IRPTNDX) {
984 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100985 free_irq(irq, domain);
986 }
987
Will Deacon518f7132014-11-14 17:17:54 +0000988 if (smmu_domain->pgtbl_ops)
989 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
990
Will Deacon44680ee2014-06-25 11:29:12 +0100991 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100992}
993
Joerg Roedel1d672632015-03-26 13:43:10 +0100994static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100995{
996 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100997
Joerg Roedel1d672632015-03-26 13:43:10 +0100998 if (type != IOMMU_DOMAIN_UNMANAGED)
999 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001000 /*
1001 * Allocate the domain and initialise some of its data structures.
1002 * We can't really do anything meaningful until we've added a
1003 * master.
1004 */
1005 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1006 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +01001007 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001008
Will Deacon518f7132014-11-14 17:17:54 +00001009 mutex_init(&smmu_domain->init_mutex);
1010 spin_lock_init(&smmu_domain->pgtbl_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +01001011
1012 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001013}
1014
Joerg Roedel1d672632015-03-26 13:43:10 +01001015static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001016{
Joerg Roedel1d672632015-03-26 13:43:10 +01001017 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +01001018
1019 /*
1020 * Free the domain resources. We assume that all devices have
1021 * already been detached.
1022 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001023 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001024 kfree(smmu_domain);
1025}
1026
1027static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001028 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001029{
1030 int i;
1031 struct arm_smmu_smr *smrs;
1032 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1033
1034 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1035 return 0;
1036
Will Deacona9a1b0b2014-05-01 18:05:08 +01001037 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001038 return -EEXIST;
1039
Mitchel Humpherys29073202014-07-08 09:52:18 -07001040 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001041 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001042 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1043 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001044 return -ENOMEM;
1045 }
1046
Will Deacon44680ee2014-06-25 11:29:12 +01001047 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001048 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001049 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1050 smmu->num_mapping_groups);
1051 if (IS_ERR_VALUE(idx)) {
1052 dev_err(smmu->dev, "failed to allocate free SMR\n");
1053 goto err_free_smrs;
1054 }
1055
1056 smrs[i] = (struct arm_smmu_smr) {
1057 .idx = idx,
1058 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001059 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001060 };
1061 }
1062
1063 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001064 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001065 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1066 smrs[i].mask << SMR_MASK_SHIFT;
1067 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1068 }
1069
Will Deacona9a1b0b2014-05-01 18:05:08 +01001070 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001071 return 0;
1072
1073err_free_smrs:
1074 while (--i >= 0)
1075 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1076 kfree(smrs);
1077 return -ENOSPC;
1078}
1079
1080static void arm_smmu_master_free_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 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001085 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001086
Will Deacon43b412b2014-07-15 11:22:24 +01001087 if (!smrs)
1088 return;
1089
Will Deacon45ae7cf2013-06-24 18:31:25 +01001090 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001091 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001092 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001093
Will Deacon45ae7cf2013-06-24 18:31:25 +01001094 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1095 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1096 }
1097
Will Deacona9a1b0b2014-05-01 18:05:08 +01001098 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001099 kfree(smrs);
1100}
1101
Will Deacon45ae7cf2013-06-24 18:31:25 +01001102static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001103 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001104{
1105 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001106 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001107 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1108
Will Deacon8f68f8e2014-07-15 11:27:08 +01001109 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001110 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001111 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001112 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001113
Will Deacona9a1b0b2014-05-01 18:05:08 +01001114 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001115 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001116
Will Deacona9a1b0b2014-05-01 18:05:08 +01001117 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001118 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001119 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001120 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1121 }
1122
1123 return 0;
1124}
1125
1126static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001127 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001128{
Will Deacon43b412b2014-07-15 11:22:24 +01001129 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001130 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001131 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001132
Will Deacon8f68f8e2014-07-15 11:27:08 +01001133 /* An IOMMU group is torn down by the first device to be removed */
1134 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1135 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001136
1137 /*
1138 * We *must* clear the S2CR first, because freeing the SMR means
1139 * that it can be re-allocated immediately.
1140 */
Will Deacon43b412b2014-07-15 11:22:24 +01001141 for (i = 0; i < cfg->num_streamids; ++i) {
1142 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1143
1144 writel_relaxed(S2CR_TYPE_BYPASS,
1145 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1146 }
1147
Will Deacona9a1b0b2014-05-01 18:05:08 +01001148 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001149}
1150
1151static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1152{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001153 int ret;
Joerg Roedel1d672632015-03-26 13:43:10 +01001154 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001155 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001156 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001157
Will Deacon8f68f8e2014-07-15 11:27:08 +01001158 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001159 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001160 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1161 return -ENXIO;
1162 }
1163
Will Deacon844e35b2014-07-17 11:23:51 +01001164 if (dev->archdata.iommu) {
1165 dev_err(dev, "already attached to IOMMU domain\n");
1166 return -EEXIST;
1167 }
1168
Will Deacon518f7132014-11-14 17:17:54 +00001169 /* Ensure that the domain is finalised */
1170 ret = arm_smmu_init_domain_context(domain, smmu);
1171 if (IS_ERR_VALUE(ret))
1172 return ret;
1173
Will Deacon45ae7cf2013-06-24 18:31:25 +01001174 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001175 * Sanity check the domain. We don't support domains across
1176 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001177 */
Will Deacon518f7132014-11-14 17:17:54 +00001178 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001179 dev_err(dev,
1180 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001181 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1182 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001183 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001184
1185 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001186 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001187 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001188 return -ENODEV;
1189
Will Deacon844e35b2014-07-17 11:23:51 +01001190 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1191 if (!ret)
1192 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001193 return ret;
1194}
1195
1196static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1197{
Joerg Roedel1d672632015-03-26 13:43:10 +01001198 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001199 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001200
Will Deacon8f68f8e2014-07-15 11:27:08 +01001201 cfg = find_smmu_master_cfg(dev);
Will Deacon844e35b2014-07-17 11:23:51 +01001202 if (!cfg)
1203 return;
1204
1205 dev->archdata.iommu = NULL;
1206 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001207}
1208
Will Deacon45ae7cf2013-06-24 18:31:25 +01001209static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001210 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001211{
Will Deacon518f7132014-11-14 17:17:54 +00001212 int ret;
1213 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001214 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001215 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001216
Will Deacon518f7132014-11-14 17:17:54 +00001217 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001218 return -ENODEV;
1219
Will Deacon518f7132014-11-14 17:17:54 +00001220 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1221 ret = ops->map(ops, iova, paddr, size, prot);
1222 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1223 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001224}
1225
1226static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1227 size_t size)
1228{
Will Deacon518f7132014-11-14 17:17:54 +00001229 size_t ret;
1230 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001231 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001232 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001233
Will Deacon518f7132014-11-14 17:17:54 +00001234 if (!ops)
1235 return 0;
1236
1237 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1238 ret = ops->unmap(ops, iova, size);
1239 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1240 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001241}
1242
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001243static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1244 dma_addr_t iova)
1245{
Joerg Roedel1d672632015-03-26 13:43:10 +01001246 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001247 struct arm_smmu_device *smmu = smmu_domain->smmu;
1248 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1249 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1250 struct device *dev = smmu->dev;
1251 void __iomem *cb_base;
1252 u32 tmp;
1253 u64 phys;
1254
1255 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1256
1257 if (smmu->version == 1) {
1258 u32 reg = iova & ~0xfff;
1259 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
1260 } else {
1261 u32 reg = iova & ~0xfff;
1262 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
Arnd Bergmanna4188be2015-01-30 22:55:55 +01001263 reg = ((u64)iova & ~0xfff) >> 32;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001264 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_HI);
1265 }
1266
1267 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1268 !(tmp & ATSR_ACTIVE), 5, 50)) {
1269 dev_err(dev,
1270 "iova to phys timed out on 0x%pad. Falling back to software table walk.\n",
1271 &iova);
1272 return ops->iova_to_phys(ops, iova);
1273 }
1274
1275 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1276 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1277
1278 if (phys & CB_PAR_F) {
1279 dev_err(dev, "translation fault!\n");
1280 dev_err(dev, "PAR = 0x%llx\n", phys);
1281 return 0;
1282 }
1283
1284 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1285}
1286
Will Deacon45ae7cf2013-06-24 18:31:25 +01001287static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001288 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001289{
Will Deacon518f7132014-11-14 17:17:54 +00001290 phys_addr_t ret;
1291 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001292 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001293 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001294
Will Deacon518f7132014-11-14 17:17:54 +00001295 if (!ops)
Will Deacona44a97912013-11-07 18:47:50 +00001296 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001297
Will Deacon518f7132014-11-14 17:17:54 +00001298 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001299 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS)
1300 ret = arm_smmu_iova_to_phys_hard(domain, iova);
1301 else
1302 ret = ops->iova_to_phys(ops, iova);
Will Deacon518f7132014-11-14 17:17:54 +00001303 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001304
Will Deacon518f7132014-11-14 17:17:54 +00001305 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001306}
1307
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001308static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001309{
Will Deacond0948942014-06-24 17:30:10 +01001310 switch (cap) {
1311 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001312 /*
1313 * Return true here as the SMMU can always send out coherent
1314 * requests.
1315 */
1316 return true;
Will Deacond0948942014-06-24 17:30:10 +01001317 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001318 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001319 case IOMMU_CAP_NOEXEC:
1320 return true;
Will Deacond0948942014-06-24 17:30:10 +01001321 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001322 return false;
Will Deacond0948942014-06-24 17:30:10 +01001323 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001324}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001325
Will Deacona9a1b0b2014-05-01 18:05:08 +01001326static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1327{
1328 *((u16 *)data) = alias;
1329 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001330}
1331
Will Deacon8f68f8e2014-07-15 11:27:08 +01001332static void __arm_smmu_release_pci_iommudata(void *data)
1333{
1334 kfree(data);
1335}
1336
Will Deacon45ae7cf2013-06-24 18:31:25 +01001337static int arm_smmu_add_device(struct device *dev)
1338{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001339 struct arm_smmu_device *smmu;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001340 struct arm_smmu_master_cfg *cfg;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001341 struct iommu_group *group;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001342 void (*releasefn)(void *) = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001343 int ret;
1344
Will Deacon44680ee2014-06-25 11:29:12 +01001345 smmu = find_smmu_for_device(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001346 if (!smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001347 return -ENODEV;
1348
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001349 group = iommu_group_alloc();
1350 if (IS_ERR(group)) {
1351 dev_err(dev, "Failed to allocate IOMMU group\n");
1352 return PTR_ERR(group);
1353 }
1354
Will Deacona9a1b0b2014-05-01 18:05:08 +01001355 if (dev_is_pci(dev)) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001356 struct pci_dev *pdev = to_pci_dev(dev);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001357
Will Deacona9a1b0b2014-05-01 18:05:08 +01001358 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1359 if (!cfg) {
1360 ret = -ENOMEM;
1361 goto out_put_group;
1362 }
1363
1364 cfg->num_streamids = 1;
1365 /*
1366 * Assume Stream ID == Requester ID for now.
1367 * We need a way to describe the ID mappings in FDT.
1368 */
1369 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1370 &cfg->streamids[0]);
Will Deacon8f68f8e2014-07-15 11:27:08 +01001371 releasefn = __arm_smmu_release_pci_iommudata;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001372 } else {
Will Deacon8f68f8e2014-07-15 11:27:08 +01001373 struct arm_smmu_master *master;
1374
1375 master = find_smmu_master(smmu, dev->of_node);
1376 if (!master) {
1377 ret = -ENODEV;
1378 goto out_put_group;
1379 }
1380
1381 cfg = &master->cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001382 }
1383
Will Deacon8f68f8e2014-07-15 11:27:08 +01001384 iommu_group_set_iommudata(group, cfg, releasefn);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001385 ret = iommu_group_add_device(group, dev);
1386
1387out_put_group:
1388 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001389 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001390}
1391
1392static void arm_smmu_remove_device(struct device *dev)
1393{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001394 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001395}
1396
Will Deaconc752ce42014-06-25 22:46:31 +01001397static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1398 enum iommu_attr attr, void *data)
1399{
Joerg Roedel1d672632015-03-26 13:43:10 +01001400 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001401
1402 switch (attr) {
1403 case DOMAIN_ATTR_NESTING:
1404 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1405 return 0;
1406 default:
1407 return -ENODEV;
1408 }
1409}
1410
1411static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1412 enum iommu_attr attr, void *data)
1413{
Will Deacon518f7132014-11-14 17:17:54 +00001414 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001415 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001416
Will Deacon518f7132014-11-14 17:17:54 +00001417 mutex_lock(&smmu_domain->init_mutex);
1418
Will Deaconc752ce42014-06-25 22:46:31 +01001419 switch (attr) {
1420 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001421 if (smmu_domain->smmu) {
1422 ret = -EPERM;
1423 goto out_unlock;
1424 }
1425
Will Deaconc752ce42014-06-25 22:46:31 +01001426 if (*(int *)data)
1427 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1428 else
1429 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1430
Will Deacon518f7132014-11-14 17:17:54 +00001431 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001432 default:
Will Deacon518f7132014-11-14 17:17:54 +00001433 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001434 }
Will Deacon518f7132014-11-14 17:17:54 +00001435
1436out_unlock:
1437 mutex_unlock(&smmu_domain->init_mutex);
1438 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001439}
1440
Will Deacon518f7132014-11-14 17:17:54 +00001441static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001442 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001443 .domain_alloc = arm_smmu_domain_alloc,
1444 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001445 .attach_dev = arm_smmu_attach_dev,
1446 .detach_dev = arm_smmu_detach_dev,
1447 .map = arm_smmu_map,
1448 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001449 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001450 .iova_to_phys = arm_smmu_iova_to_phys,
1451 .add_device = arm_smmu_add_device,
1452 .remove_device = arm_smmu_remove_device,
1453 .domain_get_attr = arm_smmu_domain_get_attr,
1454 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001455 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001456};
1457
1458static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1459{
1460 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001461 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001462 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001463 u32 reg;
1464
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001465 /* clear global FSR */
1466 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1467 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001468
1469 /* Mark all SMRn as invalid and all S2CRn as bypass */
1470 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001471 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001472 writel_relaxed(S2CR_TYPE_BYPASS,
1473 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001474 }
1475
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001476 /* Make sure all context banks are disabled and clear CB_FSR */
1477 for (i = 0; i < smmu->num_context_banks; ++i) {
1478 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1479 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1480 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1481 }
Will Deacon1463fe42013-07-31 19:21:27 +01001482
Will Deacon45ae7cf2013-06-24 18:31:25 +01001483 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001484 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1485 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1486
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001487 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001488
Will Deacon45ae7cf2013-06-24 18:31:25 +01001489 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001490 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001491
1492 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001493 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001494
1495 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001496 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001497
1498 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001499 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001500
1501 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001502 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001503
1504 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001505 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001506 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001507}
1508
1509static int arm_smmu_id_size_to_bits(int size)
1510{
1511 switch (size) {
1512 case 0:
1513 return 32;
1514 case 1:
1515 return 36;
1516 case 2:
1517 return 40;
1518 case 3:
1519 return 42;
1520 case 4:
1521 return 44;
1522 case 5:
1523 default:
1524 return 48;
1525 }
1526}
1527
1528static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1529{
1530 unsigned long size;
1531 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1532 u32 id;
1533
1534 dev_notice(smmu->dev, "probing hardware configuration...\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001535 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1536
1537 /* ID0 */
1538 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001539
1540 /* Restrict available stages based on module parameter */
1541 if (force_stage == 1)
1542 id &= ~(ID0_S2TS | ID0_NTS);
1543 else if (force_stage == 2)
1544 id &= ~(ID0_S1TS | ID0_NTS);
1545
Will Deacon45ae7cf2013-06-24 18:31:25 +01001546 if (id & ID0_S1TS) {
1547 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1548 dev_notice(smmu->dev, "\tstage 1 translation\n");
1549 }
1550
1551 if (id & ID0_S2TS) {
1552 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1553 dev_notice(smmu->dev, "\tstage 2 translation\n");
1554 }
1555
1556 if (id & ID0_NTS) {
1557 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1558 dev_notice(smmu->dev, "\tnested translation\n");
1559 }
1560
1561 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001562 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001563 dev_err(smmu->dev, "\tno translation support!\n");
1564 return -ENODEV;
1565 }
1566
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001567 if (smmu->version == 1 || (!(id & ID0_ATOSNS) && (id & ID0_S1TS))) {
1568 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1569 dev_notice(smmu->dev, "\taddress translation ops\n");
1570 }
1571
Will Deacon45ae7cf2013-06-24 18:31:25 +01001572 if (id & ID0_CTTW) {
1573 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1574 dev_notice(smmu->dev, "\tcoherent table walk\n");
1575 }
1576
1577 if (id & ID0_SMS) {
1578 u32 smr, sid, mask;
1579
1580 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1581 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1582 ID0_NUMSMRG_MASK;
1583 if (smmu->num_mapping_groups == 0) {
1584 dev_err(smmu->dev,
1585 "stream-matching supported, but no SMRs present!\n");
1586 return -ENODEV;
1587 }
1588
1589 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1590 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1591 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1592 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1593
1594 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1595 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1596 if ((mask & sid) != sid) {
1597 dev_err(smmu->dev,
1598 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1599 mask, sid);
1600 return -ENODEV;
1601 }
1602
1603 dev_notice(smmu->dev,
1604 "\tstream matching with %u register groups, mask 0x%x",
1605 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001606 } else {
1607 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1608 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001609 }
1610
1611 /* ID1 */
1612 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001613 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001614
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001615 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001616 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001617 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001618 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001619 dev_warn(smmu->dev,
1620 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1621 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001622
Will Deacon518f7132014-11-14 17:17:54 +00001623 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001624 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1625 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1626 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1627 return -ENODEV;
1628 }
1629 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1630 smmu->num_context_banks, smmu->num_s2_context_banks);
1631
1632 /* ID2 */
1633 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1634 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001635 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001636
Will Deacon518f7132014-11-14 17:17:54 +00001637 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001638 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001639 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001640
Robin Murphy09360402014-08-28 17:51:59 +01001641 if (smmu->version == ARM_SMMU_V1) {
Will Deacon518f7132014-11-14 17:17:54 +00001642 smmu->va_size = smmu->ipa_size;
1643 size = SZ_4K | SZ_2M | SZ_1G;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001644 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001645 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001646 smmu->va_size = arm_smmu_id_size_to_bits(size);
1647#ifndef CONFIG_64BIT
1648 smmu->va_size = min(32UL, smmu->va_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001649#endif
Will Deacon518f7132014-11-14 17:17:54 +00001650 size = 0;
1651 if (id & ID2_PTFS_4K)
1652 size |= SZ_4K | SZ_2M | SZ_1G;
1653 if (id & ID2_PTFS_16K)
1654 size |= SZ_16K | SZ_32M;
1655 if (id & ID2_PTFS_64K)
1656 size |= SZ_64K | SZ_512M;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001657 }
1658
Will Deacon518f7132014-11-14 17:17:54 +00001659 arm_smmu_ops.pgsize_bitmap &= size;
1660 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1661
Will Deacon28d60072014-09-01 16:24:48 +01001662 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1663 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001664 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001665
1666 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1667 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001668 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001669
Will Deacon45ae7cf2013-06-24 18:31:25 +01001670 return 0;
1671}
1672
Joerg Roedel09b52692014-10-02 12:24:45 +02001673static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy09360402014-08-28 17:51:59 +01001674 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1675 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1676 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
Robin Murphyd3aba042014-08-28 17:52:00 +01001677 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
Robin Murphy09360402014-08-28 17:51:59 +01001678 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1679 { },
1680};
1681MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1682
Will Deacon45ae7cf2013-06-24 18:31:25 +01001683static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1684{
Robin Murphy09360402014-08-28 17:51:59 +01001685 const struct of_device_id *of_id;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001686 struct resource *res;
1687 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001688 struct device *dev = &pdev->dev;
1689 struct rb_node *node;
1690 struct of_phandle_args masterspec;
1691 int num_irqs, i, err;
1692
1693 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1694 if (!smmu) {
1695 dev_err(dev, "failed to allocate arm_smmu_device\n");
1696 return -ENOMEM;
1697 }
1698 smmu->dev = dev;
1699
Robin Murphy09360402014-08-28 17:51:59 +01001700 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1701 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1702
Will Deacon45ae7cf2013-06-24 18:31:25 +01001703 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001704 smmu->base = devm_ioremap_resource(dev, res);
1705 if (IS_ERR(smmu->base))
1706 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001707 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001708
1709 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1710 &smmu->num_global_irqs)) {
1711 dev_err(dev, "missing #global-interrupts property\n");
1712 return -ENODEV;
1713 }
1714
1715 num_irqs = 0;
1716 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1717 num_irqs++;
1718 if (num_irqs > smmu->num_global_irqs)
1719 smmu->num_context_irqs++;
1720 }
1721
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001722 if (!smmu->num_context_irqs) {
1723 dev_err(dev, "found %d interrupts but expected at least %d\n",
1724 num_irqs, smmu->num_global_irqs + 1);
1725 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001726 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001727
1728 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1729 GFP_KERNEL);
1730 if (!smmu->irqs) {
1731 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1732 return -ENOMEM;
1733 }
1734
1735 for (i = 0; i < num_irqs; ++i) {
1736 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001737
Will Deacon45ae7cf2013-06-24 18:31:25 +01001738 if (irq < 0) {
1739 dev_err(dev, "failed to get irq index %d\n", i);
1740 return -ENODEV;
1741 }
1742 smmu->irqs[i] = irq;
1743 }
1744
Olav Haugan3c8766d2014-08-22 17:12:32 -07001745 err = arm_smmu_device_cfg_probe(smmu);
1746 if (err)
1747 return err;
1748
Will Deacon45ae7cf2013-06-24 18:31:25 +01001749 i = 0;
1750 smmu->masters = RB_ROOT;
1751 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1752 "#stream-id-cells", i,
1753 &masterspec)) {
1754 err = register_smmu_master(smmu, dev, &masterspec);
1755 if (err) {
1756 dev_err(dev, "failed to add master %s\n",
1757 masterspec.np->name);
1758 goto out_put_masters;
1759 }
1760
1761 i++;
1762 }
1763 dev_notice(dev, "registered %d master devices\n", i);
1764
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001765 parse_driver_options(smmu);
1766
Robin Murphy09360402014-08-28 17:51:59 +01001767 if (smmu->version > ARM_SMMU_V1 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001768 smmu->num_context_banks != smmu->num_context_irqs) {
1769 dev_err(dev,
1770 "found only %d context interrupt(s) but %d required\n",
1771 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001772 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001773 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001774 }
1775
Will Deacon45ae7cf2013-06-24 18:31:25 +01001776 for (i = 0; i < smmu->num_global_irqs; ++i) {
1777 err = request_irq(smmu->irqs[i],
1778 arm_smmu_global_fault,
1779 IRQF_SHARED,
1780 "arm-smmu global fault",
1781 smmu);
1782 if (err) {
1783 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1784 i, smmu->irqs[i]);
1785 goto out_free_irqs;
1786 }
1787 }
1788
1789 INIT_LIST_HEAD(&smmu->list);
1790 spin_lock(&arm_smmu_devices_lock);
1791 list_add(&smmu->list, &arm_smmu_devices);
1792 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001793
1794 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001795 return 0;
1796
1797out_free_irqs:
1798 while (i--)
1799 free_irq(smmu->irqs[i], smmu);
1800
Will Deacon45ae7cf2013-06-24 18:31:25 +01001801out_put_masters:
1802 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001803 struct arm_smmu_master *master
1804 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001805 of_node_put(master->of_node);
1806 }
1807
1808 return err;
1809}
1810
1811static int arm_smmu_device_remove(struct platform_device *pdev)
1812{
1813 int i;
1814 struct device *dev = &pdev->dev;
1815 struct arm_smmu_device *curr, *smmu = NULL;
1816 struct rb_node *node;
1817
1818 spin_lock(&arm_smmu_devices_lock);
1819 list_for_each_entry(curr, &arm_smmu_devices, list) {
1820 if (curr->dev == dev) {
1821 smmu = curr;
1822 list_del(&smmu->list);
1823 break;
1824 }
1825 }
1826 spin_unlock(&arm_smmu_devices_lock);
1827
1828 if (!smmu)
1829 return -ENODEV;
1830
Will Deacon45ae7cf2013-06-24 18:31:25 +01001831 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001832 struct arm_smmu_master *master
1833 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001834 of_node_put(master->of_node);
1835 }
1836
Will Deaconecfadb62013-07-31 19:21:28 +01001837 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001838 dev_err(dev, "removing device with active domains!\n");
1839
1840 for (i = 0; i < smmu->num_global_irqs; ++i)
1841 free_irq(smmu->irqs[i], smmu);
1842
1843 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001844 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001845 return 0;
1846}
1847
Will Deacon45ae7cf2013-06-24 18:31:25 +01001848static struct platform_driver arm_smmu_driver = {
1849 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001850 .name = "arm-smmu",
1851 .of_match_table = of_match_ptr(arm_smmu_of_match),
1852 },
1853 .probe = arm_smmu_device_dt_probe,
1854 .remove = arm_smmu_device_remove,
1855};
1856
1857static int __init arm_smmu_init(void)
1858{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001859 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001860 int ret;
1861
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001862 /*
1863 * Play nice with systems that don't have an ARM SMMU by checking that
1864 * an ARM SMMU exists in the system before proceeding with the driver
1865 * and IOMMU bus operation registration.
1866 */
1867 np = of_find_matching_node(NULL, arm_smmu_of_match);
1868 if (!np)
1869 return 0;
1870
1871 of_node_put(np);
1872
Will Deacon45ae7cf2013-06-24 18:31:25 +01001873 ret = platform_driver_register(&arm_smmu_driver);
1874 if (ret)
1875 return ret;
1876
1877 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001878 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001879 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1880
Will Deacond123cf82014-02-04 22:17:53 +00001881#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01001882 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001883 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00001884#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01001885
Will Deacona9a1b0b2014-05-01 18:05:08 +01001886#ifdef CONFIG_PCI
1887 if (!iommu_present(&pci_bus_type))
1888 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1889#endif
1890
Will Deacon45ae7cf2013-06-24 18:31:25 +01001891 return 0;
1892}
1893
1894static void __exit arm_smmu_exit(void)
1895{
1896 return platform_driver_unregister(&arm_smmu_driver);
1897}
1898
Andreas Herrmannb1950b22013-10-01 13:39:05 +01001899subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001900module_exit(arm_smmu_exit);
1901
1902MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1903MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1904MODULE_LICENSE("GPL v2");