blob: 9f7e1d34a32bc8ec75c6470260c66f839419f502 [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
Will Deacon4a1c93c2015-03-04 12:21:03 +0000739 if (smmu->version > ARM_SMMU_V1) {
740 /*
741 * CBA2R.
742 * *Must* be initialised before CBAR thanks to VMID16
743 * architectural oversight affected some implementations.
744 */
745#ifdef CONFIG_64BIT
746 reg = CBA2R_RW64_64BIT;
747#else
748 reg = CBA2R_RW64_32BIT;
749#endif
750 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
751 }
752
Will Deacon45ae7cf2013-06-24 18:31:25 +0100753 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100754 reg = cfg->cbar;
Robin Murphy09360402014-08-28 17:51:59 +0100755 if (smmu->version == ARM_SMMU_V1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700756 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100757
Will Deacon57ca90f2014-02-06 14:59:05 +0000758 /*
759 * Use the weakest shareability/memory types, so they are
760 * overridden by the ttbcr/pte.
761 */
762 if (stage1) {
763 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
764 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
765 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100766 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000767 }
Will Deacon44680ee2014-06-25 11:29:12 +0100768 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100769
Will Deacon518f7132014-11-14 17:17:54 +0000770 /* TTBRs */
771 if (stage1) {
772 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
773 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
774 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32;
Will Deacon44680ee2014-06-25 11:29:12 +0100775 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon518f7132014-11-14 17:17:54 +0000776 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100777
Will Deacon518f7132014-11-14 17:17:54 +0000778 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
779 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO);
780 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32;
781 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
782 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI);
783 } else {
784 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
785 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
786 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32;
787 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
788 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100789
Will Deacon518f7132014-11-14 17:17:54 +0000790 /* TTBCR */
791 if (stage1) {
792 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
793 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
794 if (smmu->version > ARM_SMMU_V1) {
795 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
796 switch (smmu->va_size) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100797 case 32:
Will Deacon518f7132014-11-14 17:17:54 +0000798 reg |= (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100799 break;
800 case 36:
Will Deacon518f7132014-11-14 17:17:54 +0000801 reg |= (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100802 break;
803 case 40:
Will Deacon518f7132014-11-14 17:17:54 +0000804 reg |= (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100805 break;
806 case 42:
Will Deacon518f7132014-11-14 17:17:54 +0000807 reg |= (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100808 break;
809 case 44:
Will Deacon518f7132014-11-14 17:17:54 +0000810 reg |= (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100811 break;
812 case 48:
Will Deacon518f7132014-11-14 17:17:54 +0000813 reg |= (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100814 break;
815 }
Will Deacon518f7132014-11-14 17:17:54 +0000816 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100817 }
818 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000819 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
820 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100821 }
822
Will Deacon518f7132014-11-14 17:17:54 +0000823 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100824 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000825 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100826 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000827 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
828 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100829 }
830
Will Deacon45ae7cf2013-06-24 18:31:25 +0100831 /* SCTLR */
832 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
833 if (stage1)
834 reg |= SCTLR_S1_ASIDPNE;
835#ifdef __BIG_ENDIAN
836 reg |= SCTLR_E;
837#endif
Will Deacon25724842013-08-21 13:49:53 +0100838 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100839}
840
841static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100842 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100843{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100844 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000845 unsigned long ias, oas;
846 struct io_pgtable_ops *pgtbl_ops;
847 struct io_pgtable_cfg pgtbl_cfg;
848 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100849 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100850 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100851
Will Deacon518f7132014-11-14 17:17:54 +0000852 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100853 if (smmu_domain->smmu)
854 goto out_unlock;
855
Will Deaconc752ce42014-06-25 22:46:31 +0100856 /*
857 * Mapping the requested stage onto what we support is surprisingly
858 * complicated, mainly because the spec allows S1+S2 SMMUs without
859 * support for nested translation. That means we end up with the
860 * following table:
861 *
862 * Requested Supported Actual
863 * S1 N S1
864 * S1 S1+S2 S1
865 * S1 S2 S2
866 * S1 S1 S1
867 * N N N
868 * N S1+S2 S2
869 * N S2 S2
870 * N S1 S1
871 *
872 * Note that you can't actually request stage-2 mappings.
873 */
874 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
875 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
876 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
877 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
878
879 switch (smmu_domain->stage) {
880 case ARM_SMMU_DOMAIN_S1:
881 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
882 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000883 ias = smmu->va_size;
884 oas = smmu->ipa_size;
885 if (IS_ENABLED(CONFIG_64BIT))
886 fmt = ARM_64_LPAE_S1;
887 else
888 fmt = ARM_32_LPAE_S1;
Will Deaconc752ce42014-06-25 22:46:31 +0100889 break;
890 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100891 /*
892 * We will likely want to change this if/when KVM gets
893 * involved.
894 */
Will Deaconc752ce42014-06-25 22:46:31 +0100895 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100896 cfg->cbar = CBAR_TYPE_S2_TRANS;
897 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000898 ias = smmu->ipa_size;
899 oas = smmu->pa_size;
900 if (IS_ENABLED(CONFIG_64BIT))
901 fmt = ARM_64_LPAE_S2;
902 else
903 fmt = ARM_32_LPAE_S2;
Will Deaconc752ce42014-06-25 22:46:31 +0100904 break;
905 default:
906 ret = -EINVAL;
907 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100908 }
909
910 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
911 smmu->num_context_banks);
912 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100913 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100914
Will Deacon44680ee2014-06-25 11:29:12 +0100915 cfg->cbndx = ret;
Robin Murphy09360402014-08-28 17:51:59 +0100916 if (smmu->version == ARM_SMMU_V1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100917 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
918 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100919 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100920 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100921 }
922
Will Deacon518f7132014-11-14 17:17:54 +0000923 pgtbl_cfg = (struct io_pgtable_cfg) {
924 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
925 .ias = ias,
926 .oas = oas,
927 .tlb = &arm_smmu_gather_ops,
928 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100929
Will Deacon518f7132014-11-14 17:17:54 +0000930 smmu_domain->smmu = smmu;
931 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
932 if (!pgtbl_ops) {
933 ret = -ENOMEM;
934 goto out_clear_smmu;
935 }
936
937 /* Update our support page sizes to reflect the page table format */
938 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
939
940 /* Initialise the context bank with our page table cfg */
941 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
942
943 /*
944 * Request context fault interrupt. Do this last to avoid the
945 * handler seeing a half-initialised domain state.
946 */
Will Deacon44680ee2014-06-25 11:29:12 +0100947 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100948 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
949 "arm-smmu-context-fault", domain);
950 if (IS_ERR_VALUE(ret)) {
951 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100952 cfg->irptndx, irq);
953 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100954 }
955
Will Deacon518f7132014-11-14 17:17:54 +0000956 mutex_unlock(&smmu_domain->init_mutex);
957
958 /* Publish page table ops for map/unmap */
959 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100960 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100961
Will Deacon518f7132014-11-14 17:17:54 +0000962out_clear_smmu:
963 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100964out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000965 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100966 return ret;
967}
968
969static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
970{
Joerg Roedel1d672632015-03-26 13:43:10 +0100971 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100972 struct arm_smmu_device *smmu = smmu_domain->smmu;
973 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100974 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100975 int irq;
976
977 if (!smmu)
978 return;
979
Will Deacon518f7132014-11-14 17:17:54 +0000980 /*
981 * Disable the context bank and free the page tables before freeing
982 * it.
983 */
Will Deacon44680ee2014-06-25 11:29:12 +0100984 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100985 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +0100986
Will Deacon44680ee2014-06-25 11:29:12 +0100987 if (cfg->irptndx != INVALID_IRPTNDX) {
988 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100989 free_irq(irq, domain);
990 }
991
Will Deacon518f7132014-11-14 17:17:54 +0000992 if (smmu_domain->pgtbl_ops)
993 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
994
Will Deacon44680ee2014-06-25 11:29:12 +0100995 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100996}
997
Joerg Roedel1d672632015-03-26 13:43:10 +0100998static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100999{
1000 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001001
Joerg Roedel1d672632015-03-26 13:43:10 +01001002 if (type != IOMMU_DOMAIN_UNMANAGED)
1003 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001004 /*
1005 * Allocate the domain and initialise some of its data structures.
1006 * We can't really do anything meaningful until we've added a
1007 * master.
1008 */
1009 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1010 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +01001011 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001012
Will Deacon518f7132014-11-14 17:17:54 +00001013 mutex_init(&smmu_domain->init_mutex);
1014 spin_lock_init(&smmu_domain->pgtbl_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +01001015
1016 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001017}
1018
Joerg Roedel1d672632015-03-26 13:43:10 +01001019static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001020{
Joerg Roedel1d672632015-03-26 13:43:10 +01001021 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +01001022
1023 /*
1024 * Free the domain resources. We assume that all devices have
1025 * already been detached.
1026 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001027 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001028 kfree(smmu_domain);
1029}
1030
1031static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001032 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001033{
1034 int i;
1035 struct arm_smmu_smr *smrs;
1036 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1037
1038 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1039 return 0;
1040
Will Deacona9a1b0b2014-05-01 18:05:08 +01001041 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001042 return -EEXIST;
1043
Mitchel Humpherys29073202014-07-08 09:52:18 -07001044 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001045 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001046 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1047 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001048 return -ENOMEM;
1049 }
1050
Will Deacon44680ee2014-06-25 11:29:12 +01001051 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001052 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001053 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1054 smmu->num_mapping_groups);
1055 if (IS_ERR_VALUE(idx)) {
1056 dev_err(smmu->dev, "failed to allocate free SMR\n");
1057 goto err_free_smrs;
1058 }
1059
1060 smrs[i] = (struct arm_smmu_smr) {
1061 .idx = idx,
1062 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001063 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001064 };
1065 }
1066
1067 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001068 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001069 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1070 smrs[i].mask << SMR_MASK_SHIFT;
1071 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1072 }
1073
Will Deacona9a1b0b2014-05-01 18:05:08 +01001074 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001075 return 0;
1076
1077err_free_smrs:
1078 while (--i >= 0)
1079 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1080 kfree(smrs);
1081 return -ENOSPC;
1082}
1083
1084static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001085 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001086{
1087 int i;
1088 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001089 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001090
Will Deacon43b412b2014-07-15 11:22:24 +01001091 if (!smrs)
1092 return;
1093
Will Deacon45ae7cf2013-06-24 18:31:25 +01001094 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001095 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001096 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001097
Will Deacon45ae7cf2013-06-24 18:31:25 +01001098 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1099 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1100 }
1101
Will Deacona9a1b0b2014-05-01 18:05:08 +01001102 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001103 kfree(smrs);
1104}
1105
Will Deacon45ae7cf2013-06-24 18:31:25 +01001106static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001107 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001108{
1109 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001110 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001111 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1112
Will Deacon8f68f8e2014-07-15 11:27:08 +01001113 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001114 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001115 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001116 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001117
Will Deacona9a1b0b2014-05-01 18:05:08 +01001118 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001119 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001120
Will Deacona9a1b0b2014-05-01 18:05:08 +01001121 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001122 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001123 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001124 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1125 }
1126
1127 return 0;
1128}
1129
1130static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001131 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001132{
Will Deacon43b412b2014-07-15 11:22:24 +01001133 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001134 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001135 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001136
Will Deacon8f68f8e2014-07-15 11:27:08 +01001137 /* An IOMMU group is torn down by the first device to be removed */
1138 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1139 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001140
1141 /*
1142 * We *must* clear the S2CR first, because freeing the SMR means
1143 * that it can be re-allocated immediately.
1144 */
Will Deacon43b412b2014-07-15 11:22:24 +01001145 for (i = 0; i < cfg->num_streamids; ++i) {
1146 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1147
1148 writel_relaxed(S2CR_TYPE_BYPASS,
1149 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1150 }
1151
Will Deacona9a1b0b2014-05-01 18:05:08 +01001152 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001153}
1154
1155static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1156{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001157 int ret;
Joerg Roedel1d672632015-03-26 13:43:10 +01001158 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001159 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001160 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001161
Will Deacon8f68f8e2014-07-15 11:27:08 +01001162 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001163 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001164 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1165 return -ENXIO;
1166 }
1167
Will Deacon844e35b2014-07-17 11:23:51 +01001168 if (dev->archdata.iommu) {
1169 dev_err(dev, "already attached to IOMMU domain\n");
1170 return -EEXIST;
1171 }
1172
Will Deacon518f7132014-11-14 17:17:54 +00001173 /* Ensure that the domain is finalised */
1174 ret = arm_smmu_init_domain_context(domain, smmu);
1175 if (IS_ERR_VALUE(ret))
1176 return ret;
1177
Will Deacon45ae7cf2013-06-24 18:31:25 +01001178 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001179 * Sanity check the domain. We don't support domains across
1180 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001181 */
Will Deacon518f7132014-11-14 17:17:54 +00001182 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001183 dev_err(dev,
1184 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001185 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1186 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001187 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001188
1189 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001190 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001191 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001192 return -ENODEV;
1193
Will Deacon844e35b2014-07-17 11:23:51 +01001194 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1195 if (!ret)
1196 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001197 return ret;
1198}
1199
1200static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1201{
Joerg Roedel1d672632015-03-26 13:43:10 +01001202 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001203 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001204
Will Deacon8f68f8e2014-07-15 11:27:08 +01001205 cfg = find_smmu_master_cfg(dev);
Will Deacon844e35b2014-07-17 11:23:51 +01001206 if (!cfg)
1207 return;
1208
1209 dev->archdata.iommu = NULL;
1210 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001211}
1212
Will Deacon45ae7cf2013-06-24 18:31:25 +01001213static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001214 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001215{
Will Deacon518f7132014-11-14 17:17:54 +00001216 int ret;
1217 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001218 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001219 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001220
Will Deacon518f7132014-11-14 17:17:54 +00001221 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001222 return -ENODEV;
1223
Will Deacon518f7132014-11-14 17:17:54 +00001224 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1225 ret = ops->map(ops, iova, paddr, size, prot);
1226 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1227 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001228}
1229
1230static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1231 size_t size)
1232{
Will Deacon518f7132014-11-14 17:17:54 +00001233 size_t ret;
1234 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001235 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001236 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001237
Will Deacon518f7132014-11-14 17:17:54 +00001238 if (!ops)
1239 return 0;
1240
1241 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1242 ret = ops->unmap(ops, iova, size);
1243 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1244 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001245}
1246
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001247static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1248 dma_addr_t iova)
1249{
Joerg Roedel1d672632015-03-26 13:43:10 +01001250 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001251 struct arm_smmu_device *smmu = smmu_domain->smmu;
1252 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1253 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1254 struct device *dev = smmu->dev;
1255 void __iomem *cb_base;
1256 u32 tmp;
1257 u64 phys;
1258
1259 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1260
1261 if (smmu->version == 1) {
1262 u32 reg = iova & ~0xfff;
1263 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
1264 } else {
1265 u32 reg = iova & ~0xfff;
1266 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
Arnd Bergmanna4188be2015-01-30 22:55:55 +01001267 reg = ((u64)iova & ~0xfff) >> 32;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001268 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_HI);
1269 }
1270
1271 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1272 !(tmp & ATSR_ACTIVE), 5, 50)) {
1273 dev_err(dev,
1274 "iova to phys timed out on 0x%pad. Falling back to software table walk.\n",
1275 &iova);
1276 return ops->iova_to_phys(ops, iova);
1277 }
1278
1279 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1280 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1281
1282 if (phys & CB_PAR_F) {
1283 dev_err(dev, "translation fault!\n");
1284 dev_err(dev, "PAR = 0x%llx\n", phys);
1285 return 0;
1286 }
1287
1288 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1289}
1290
Will Deacon45ae7cf2013-06-24 18:31:25 +01001291static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001292 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001293{
Will Deacon518f7132014-11-14 17:17:54 +00001294 phys_addr_t ret;
1295 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001296 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001297 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001298
Will Deacon518f7132014-11-14 17:17:54 +00001299 if (!ops)
Will Deacona44a9792013-11-07 18:47:50 +00001300 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001301
Will Deacon518f7132014-11-14 17:17:54 +00001302 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001303 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1304 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001305 ret = arm_smmu_iova_to_phys_hard(domain, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001306 } else {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001307 ret = ops->iova_to_phys(ops, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001308 }
1309
Will Deacon518f7132014-11-14 17:17:54 +00001310 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001311
Will Deacon518f7132014-11-14 17:17:54 +00001312 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001313}
1314
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001315static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001316{
Will Deacond0948942014-06-24 17:30:10 +01001317 switch (cap) {
1318 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001319 /*
1320 * Return true here as the SMMU can always send out coherent
1321 * requests.
1322 */
1323 return true;
Will Deacond0948942014-06-24 17:30:10 +01001324 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001325 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001326 case IOMMU_CAP_NOEXEC:
1327 return true;
Will Deacond0948942014-06-24 17:30:10 +01001328 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001329 return false;
Will Deacond0948942014-06-24 17:30:10 +01001330 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001331}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001332
Will Deacona9a1b0b2014-05-01 18:05:08 +01001333static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1334{
1335 *((u16 *)data) = alias;
1336 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001337}
1338
Will Deacon8f68f8e2014-07-15 11:27:08 +01001339static void __arm_smmu_release_pci_iommudata(void *data)
1340{
1341 kfree(data);
1342}
1343
Will Deacon03edb222015-01-19 14:27:33 +00001344static int arm_smmu_add_pci_device(struct pci_dev *pdev)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001345{
Will Deacon03edb222015-01-19 14:27:33 +00001346 int i, ret;
1347 u16 sid;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001348 struct iommu_group *group;
Will Deacon03edb222015-01-19 14:27:33 +00001349 struct arm_smmu_master_cfg *cfg;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001350
Will Deacon03edb222015-01-19 14:27:33 +00001351 group = iommu_group_get_for_dev(&pdev->dev);
1352 if (IS_ERR(group))
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001353 return PTR_ERR(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001354
Will Deacon03edb222015-01-19 14:27:33 +00001355 cfg = iommu_group_get_iommudata(group);
1356 if (!cfg) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001357 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1358 if (!cfg) {
1359 ret = -ENOMEM;
1360 goto out_put_group;
1361 }
1362
Will Deacon03edb222015-01-19 14:27:33 +00001363 iommu_group_set_iommudata(group, cfg,
1364 __arm_smmu_release_pci_iommudata);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001365 }
1366
Will Deacon03edb222015-01-19 14:27:33 +00001367 if (cfg->num_streamids >= MAX_MASTER_STREAMIDS) {
1368 ret = -ENOSPC;
1369 goto out_put_group;
1370 }
Will Deacona9a1b0b2014-05-01 18:05:08 +01001371
Will Deacon03edb222015-01-19 14:27:33 +00001372 /*
1373 * Assume Stream ID == Requester ID for now.
1374 * We need a way to describe the ID mappings in FDT.
1375 */
1376 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1377 for (i = 0; i < cfg->num_streamids; ++i)
1378 if (cfg->streamids[i] == sid)
1379 break;
1380
1381 /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
1382 if (i == cfg->num_streamids)
1383 cfg->streamids[cfg->num_streamids++] = sid;
1384
1385 return 0;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001386out_put_group:
1387 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001388 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001389}
1390
Will Deacon03edb222015-01-19 14:27:33 +00001391static int arm_smmu_add_platform_device(struct device *dev)
1392{
1393 struct iommu_group *group;
1394 struct arm_smmu_master *master;
1395 struct arm_smmu_device *smmu = find_smmu_for_device(dev);
1396
1397 if (!smmu)
1398 return -ENODEV;
1399
1400 master = find_smmu_master(smmu, dev->of_node);
1401 if (!master)
1402 return -ENODEV;
1403
1404 /* No automatic group creation for platform devices */
1405 group = iommu_group_alloc();
1406 if (IS_ERR(group))
1407 return PTR_ERR(group);
1408
1409 iommu_group_set_iommudata(group, &master->cfg, NULL);
1410 return iommu_group_add_device(group, dev);
1411}
1412
1413static int arm_smmu_add_device(struct device *dev)
1414{
1415 if (dev_is_pci(dev))
1416 return arm_smmu_add_pci_device(to_pci_dev(dev));
1417
1418 return arm_smmu_add_platform_device(dev);
1419}
1420
Will Deacon45ae7cf2013-06-24 18:31:25 +01001421static void arm_smmu_remove_device(struct device *dev)
1422{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001423 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001424}
1425
Will Deaconc752ce42014-06-25 22:46:31 +01001426static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1427 enum iommu_attr attr, void *data)
1428{
Joerg Roedel1d672632015-03-26 13:43:10 +01001429 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001430
1431 switch (attr) {
1432 case DOMAIN_ATTR_NESTING:
1433 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1434 return 0;
1435 default:
1436 return -ENODEV;
1437 }
1438}
1439
1440static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1441 enum iommu_attr attr, void *data)
1442{
Will Deacon518f7132014-11-14 17:17:54 +00001443 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001444 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001445
Will Deacon518f7132014-11-14 17:17:54 +00001446 mutex_lock(&smmu_domain->init_mutex);
1447
Will Deaconc752ce42014-06-25 22:46:31 +01001448 switch (attr) {
1449 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001450 if (smmu_domain->smmu) {
1451 ret = -EPERM;
1452 goto out_unlock;
1453 }
1454
Will Deaconc752ce42014-06-25 22:46:31 +01001455 if (*(int *)data)
1456 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1457 else
1458 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1459
Will Deacon518f7132014-11-14 17:17:54 +00001460 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001461 default:
Will Deacon518f7132014-11-14 17:17:54 +00001462 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001463 }
Will Deacon518f7132014-11-14 17:17:54 +00001464
1465out_unlock:
1466 mutex_unlock(&smmu_domain->init_mutex);
1467 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001468}
1469
Will Deacon518f7132014-11-14 17:17:54 +00001470static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001471 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001472 .domain_alloc = arm_smmu_domain_alloc,
1473 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001474 .attach_dev = arm_smmu_attach_dev,
1475 .detach_dev = arm_smmu_detach_dev,
1476 .map = arm_smmu_map,
1477 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001478 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001479 .iova_to_phys = arm_smmu_iova_to_phys,
1480 .add_device = arm_smmu_add_device,
1481 .remove_device = arm_smmu_remove_device,
1482 .domain_get_attr = arm_smmu_domain_get_attr,
1483 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001484 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001485};
1486
1487static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1488{
1489 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001490 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001491 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001492 u32 reg;
1493
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001494 /* clear global FSR */
1495 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1496 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001497
1498 /* Mark all SMRn as invalid and all S2CRn as bypass */
1499 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001500 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001501 writel_relaxed(S2CR_TYPE_BYPASS,
1502 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001503 }
1504
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001505 /* Make sure all context banks are disabled and clear CB_FSR */
1506 for (i = 0; i < smmu->num_context_banks; ++i) {
1507 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1508 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1509 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1510 }
Will Deacon1463fe42013-07-31 19:21:27 +01001511
Will Deacon45ae7cf2013-06-24 18:31:25 +01001512 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001513 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1514 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1515
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001516 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001517
Will Deacon45ae7cf2013-06-24 18:31:25 +01001518 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001519 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001520
1521 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001522 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001523
1524 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001525 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001526
1527 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001528 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001529
1530 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001531 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001532
1533 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001534 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001535 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001536}
1537
1538static int arm_smmu_id_size_to_bits(int size)
1539{
1540 switch (size) {
1541 case 0:
1542 return 32;
1543 case 1:
1544 return 36;
1545 case 2:
1546 return 40;
1547 case 3:
1548 return 42;
1549 case 4:
1550 return 44;
1551 case 5:
1552 default:
1553 return 48;
1554 }
1555}
1556
1557static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1558{
1559 unsigned long size;
1560 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1561 u32 id;
1562
1563 dev_notice(smmu->dev, "probing hardware configuration...\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001564 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1565
1566 /* ID0 */
1567 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001568
1569 /* Restrict available stages based on module parameter */
1570 if (force_stage == 1)
1571 id &= ~(ID0_S2TS | ID0_NTS);
1572 else if (force_stage == 2)
1573 id &= ~(ID0_S1TS | ID0_NTS);
1574
Will Deacon45ae7cf2013-06-24 18:31:25 +01001575 if (id & ID0_S1TS) {
1576 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1577 dev_notice(smmu->dev, "\tstage 1 translation\n");
1578 }
1579
1580 if (id & ID0_S2TS) {
1581 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1582 dev_notice(smmu->dev, "\tstage 2 translation\n");
1583 }
1584
1585 if (id & ID0_NTS) {
1586 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1587 dev_notice(smmu->dev, "\tnested translation\n");
1588 }
1589
1590 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001591 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001592 dev_err(smmu->dev, "\tno translation support!\n");
1593 return -ENODEV;
1594 }
1595
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001596 if ((id & ID0_S1TS) && ((smmu->version == 1) || (id & ID0_ATOSNS))) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001597 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1598 dev_notice(smmu->dev, "\taddress translation ops\n");
1599 }
1600
Will Deacon45ae7cf2013-06-24 18:31:25 +01001601 if (id & ID0_CTTW) {
1602 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1603 dev_notice(smmu->dev, "\tcoherent table walk\n");
1604 }
1605
1606 if (id & ID0_SMS) {
1607 u32 smr, sid, mask;
1608
1609 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1610 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1611 ID0_NUMSMRG_MASK;
1612 if (smmu->num_mapping_groups == 0) {
1613 dev_err(smmu->dev,
1614 "stream-matching supported, but no SMRs present!\n");
1615 return -ENODEV;
1616 }
1617
1618 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1619 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1620 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1621 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1622
1623 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1624 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1625 if ((mask & sid) != sid) {
1626 dev_err(smmu->dev,
1627 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1628 mask, sid);
1629 return -ENODEV;
1630 }
1631
1632 dev_notice(smmu->dev,
1633 "\tstream matching with %u register groups, mask 0x%x",
1634 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001635 } else {
1636 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1637 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001638 }
1639
1640 /* ID1 */
1641 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001642 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001643
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001644 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001645 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001646 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001647 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001648 dev_warn(smmu->dev,
1649 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1650 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001651
Will Deacon518f7132014-11-14 17:17:54 +00001652 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001653 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1654 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1655 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1656 return -ENODEV;
1657 }
1658 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1659 smmu->num_context_banks, smmu->num_s2_context_banks);
1660
1661 /* ID2 */
1662 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1663 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001664 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001665
Will Deacon518f7132014-11-14 17:17:54 +00001666 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001667 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001668 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001669
Robin Murphyf1d84542015-03-04 16:41:05 +00001670 /*
1671 * What the page table walker can address actually depends on which
1672 * descriptor format is in use, but since a) we don't know that yet,
1673 * and b) it can vary per context bank, this will have to do...
1674 */
1675 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1676 dev_warn(smmu->dev,
1677 "failed to set DMA mask for table walker\n");
1678
Robin Murphy09360402014-08-28 17:51:59 +01001679 if (smmu->version == ARM_SMMU_V1) {
Will Deacon518f7132014-11-14 17:17:54 +00001680 smmu->va_size = smmu->ipa_size;
1681 size = SZ_4K | SZ_2M | SZ_1G;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001682 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001683 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001684 smmu->va_size = arm_smmu_id_size_to_bits(size);
1685#ifndef CONFIG_64BIT
1686 smmu->va_size = min(32UL, smmu->va_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001687#endif
Will Deacon518f7132014-11-14 17:17:54 +00001688 size = 0;
1689 if (id & ID2_PTFS_4K)
1690 size |= SZ_4K | SZ_2M | SZ_1G;
1691 if (id & ID2_PTFS_16K)
1692 size |= SZ_16K | SZ_32M;
1693 if (id & ID2_PTFS_64K)
1694 size |= SZ_64K | SZ_512M;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001695 }
1696
Will Deacon518f7132014-11-14 17:17:54 +00001697 arm_smmu_ops.pgsize_bitmap &= size;
1698 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1699
Will Deacon28d60072014-09-01 16:24:48 +01001700 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1701 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001702 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001703
1704 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1705 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001706 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001707
Will Deacon45ae7cf2013-06-24 18:31:25 +01001708 return 0;
1709}
1710
Joerg Roedel09b52692014-10-02 12:24:45 +02001711static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy09360402014-08-28 17:51:59 +01001712 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1713 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1714 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
Robin Murphyd3aba042014-08-28 17:52:00 +01001715 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
Robin Murphy09360402014-08-28 17:51:59 +01001716 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1717 { },
1718};
1719MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1720
Will Deacon45ae7cf2013-06-24 18:31:25 +01001721static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1722{
Robin Murphy09360402014-08-28 17:51:59 +01001723 const struct of_device_id *of_id;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001724 struct resource *res;
1725 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001726 struct device *dev = &pdev->dev;
1727 struct rb_node *node;
1728 struct of_phandle_args masterspec;
1729 int num_irqs, i, err;
1730
1731 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1732 if (!smmu) {
1733 dev_err(dev, "failed to allocate arm_smmu_device\n");
1734 return -ENOMEM;
1735 }
1736 smmu->dev = dev;
1737
Robin Murphy09360402014-08-28 17:51:59 +01001738 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1739 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1740
Will Deacon45ae7cf2013-06-24 18:31:25 +01001741 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001742 smmu->base = devm_ioremap_resource(dev, res);
1743 if (IS_ERR(smmu->base))
1744 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001745 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001746
1747 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1748 &smmu->num_global_irqs)) {
1749 dev_err(dev, "missing #global-interrupts property\n");
1750 return -ENODEV;
1751 }
1752
1753 num_irqs = 0;
1754 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1755 num_irqs++;
1756 if (num_irqs > smmu->num_global_irqs)
1757 smmu->num_context_irqs++;
1758 }
1759
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001760 if (!smmu->num_context_irqs) {
1761 dev_err(dev, "found %d interrupts but expected at least %d\n",
1762 num_irqs, smmu->num_global_irqs + 1);
1763 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001764 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001765
1766 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1767 GFP_KERNEL);
1768 if (!smmu->irqs) {
1769 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1770 return -ENOMEM;
1771 }
1772
1773 for (i = 0; i < num_irqs; ++i) {
1774 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001775
Will Deacon45ae7cf2013-06-24 18:31:25 +01001776 if (irq < 0) {
1777 dev_err(dev, "failed to get irq index %d\n", i);
1778 return -ENODEV;
1779 }
1780 smmu->irqs[i] = irq;
1781 }
1782
Olav Haugan3c8766d2014-08-22 17:12:32 -07001783 err = arm_smmu_device_cfg_probe(smmu);
1784 if (err)
1785 return err;
1786
Will Deacon45ae7cf2013-06-24 18:31:25 +01001787 i = 0;
1788 smmu->masters = RB_ROOT;
1789 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1790 "#stream-id-cells", i,
1791 &masterspec)) {
1792 err = register_smmu_master(smmu, dev, &masterspec);
1793 if (err) {
1794 dev_err(dev, "failed to add master %s\n",
1795 masterspec.np->name);
1796 goto out_put_masters;
1797 }
1798
1799 i++;
1800 }
1801 dev_notice(dev, "registered %d master devices\n", i);
1802
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001803 parse_driver_options(smmu);
1804
Robin Murphy09360402014-08-28 17:51:59 +01001805 if (smmu->version > ARM_SMMU_V1 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001806 smmu->num_context_banks != smmu->num_context_irqs) {
1807 dev_err(dev,
1808 "found only %d context interrupt(s) but %d required\n",
1809 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cd2013-11-15 09:42:30 +00001810 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001811 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001812 }
1813
Will Deacon45ae7cf2013-06-24 18:31:25 +01001814 for (i = 0; i < smmu->num_global_irqs; ++i) {
1815 err = request_irq(smmu->irqs[i],
1816 arm_smmu_global_fault,
1817 IRQF_SHARED,
1818 "arm-smmu global fault",
1819 smmu);
1820 if (err) {
1821 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1822 i, smmu->irqs[i]);
1823 goto out_free_irqs;
1824 }
1825 }
1826
1827 INIT_LIST_HEAD(&smmu->list);
1828 spin_lock(&arm_smmu_devices_lock);
1829 list_add(&smmu->list, &arm_smmu_devices);
1830 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001831
1832 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001833 return 0;
1834
1835out_free_irqs:
1836 while (i--)
1837 free_irq(smmu->irqs[i], smmu);
1838
Will Deacon45ae7cf2013-06-24 18:31:25 +01001839out_put_masters:
1840 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001841 struct arm_smmu_master *master
1842 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001843 of_node_put(master->of_node);
1844 }
1845
1846 return err;
1847}
1848
1849static int arm_smmu_device_remove(struct platform_device *pdev)
1850{
1851 int i;
1852 struct device *dev = &pdev->dev;
1853 struct arm_smmu_device *curr, *smmu = NULL;
1854 struct rb_node *node;
1855
1856 spin_lock(&arm_smmu_devices_lock);
1857 list_for_each_entry(curr, &arm_smmu_devices, list) {
1858 if (curr->dev == dev) {
1859 smmu = curr;
1860 list_del(&smmu->list);
1861 break;
1862 }
1863 }
1864 spin_unlock(&arm_smmu_devices_lock);
1865
1866 if (!smmu)
1867 return -ENODEV;
1868
Will Deacon45ae7cf2013-06-24 18:31:25 +01001869 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001870 struct arm_smmu_master *master
1871 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001872 of_node_put(master->of_node);
1873 }
1874
Will Deaconecfadb62013-07-31 19:21:28 +01001875 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001876 dev_err(dev, "removing device with active domains!\n");
1877
1878 for (i = 0; i < smmu->num_global_irqs; ++i)
1879 free_irq(smmu->irqs[i], smmu);
1880
1881 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001882 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001883 return 0;
1884}
1885
Will Deacon45ae7cf2013-06-24 18:31:25 +01001886static struct platform_driver arm_smmu_driver = {
1887 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001888 .name = "arm-smmu",
1889 .of_match_table = of_match_ptr(arm_smmu_of_match),
1890 },
1891 .probe = arm_smmu_device_dt_probe,
1892 .remove = arm_smmu_device_remove,
1893};
1894
1895static int __init arm_smmu_init(void)
1896{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001897 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001898 int ret;
1899
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001900 /*
1901 * Play nice with systems that don't have an ARM SMMU by checking that
1902 * an ARM SMMU exists in the system before proceeding with the driver
1903 * and IOMMU bus operation registration.
1904 */
1905 np = of_find_matching_node(NULL, arm_smmu_of_match);
1906 if (!np)
1907 return 0;
1908
1909 of_node_put(np);
1910
Will Deacon45ae7cf2013-06-24 18:31:25 +01001911 ret = platform_driver_register(&arm_smmu_driver);
1912 if (ret)
1913 return ret;
1914
1915 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001916 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001917 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1918
Will Deacond123cf82014-02-04 22:17:53 +00001919#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01001920 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001921 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00001922#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01001923
Will Deacona9a1b0b2014-05-01 18:05:08 +01001924#ifdef CONFIG_PCI
1925 if (!iommu_present(&pci_bus_type))
1926 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1927#endif
1928
Will Deacon45ae7cf2013-06-24 18:31:25 +01001929 return 0;
1930}
1931
1932static void __exit arm_smmu_exit(void)
1933{
1934 return platform_driver_unregister(&arm_smmu_driver);
1935}
1936
Andreas Herrmannb1950b22013-10-01 13:39:05 +01001937subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001938module_exit(arm_smmu_exit);
1939
1940MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1941MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1942MODULE_LICENSE("GPL v2");