blob: 4abb831e24f0fdde69529db0f37527a73fbc4d19 [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 */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100346};
347
Will Deacon518f7132014-11-14 17:17:54 +0000348static struct iommu_ops arm_smmu_ops;
349
Will Deacon45ae7cf2013-06-24 18:31:25 +0100350static DEFINE_SPINLOCK(arm_smmu_devices_lock);
351static LIST_HEAD(arm_smmu_devices);
352
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000353struct arm_smmu_option_prop {
354 u32 opt;
355 const char *prop;
356};
357
Mitchel Humpherys29073202014-07-08 09:52:18 -0700358static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000359 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
360 { 0, NULL},
361};
362
363static void parse_driver_options(struct arm_smmu_device *smmu)
364{
365 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700366
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000367 do {
368 if (of_property_read_bool(smmu->dev->of_node,
369 arm_smmu_options[i].prop)) {
370 smmu->options |= arm_smmu_options[i].opt;
371 dev_notice(smmu->dev, "option %s\n",
372 arm_smmu_options[i].prop);
373 }
374 } while (arm_smmu_options[++i].opt);
375}
376
Will Deacon8f68f8e2014-07-15 11:27:08 +0100377static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100378{
379 if (dev_is_pci(dev)) {
380 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700381
Will Deacona9a1b0b2014-05-01 18:05:08 +0100382 while (!pci_is_root_bus(bus))
383 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100384 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100385 }
386
Will Deacon8f68f8e2014-07-15 11:27:08 +0100387 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100388}
389
Will Deacon45ae7cf2013-06-24 18:31:25 +0100390static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
391 struct device_node *dev_node)
392{
393 struct rb_node *node = smmu->masters.rb_node;
394
395 while (node) {
396 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700397
Will Deacon45ae7cf2013-06-24 18:31:25 +0100398 master = container_of(node, struct arm_smmu_master, node);
399
400 if (dev_node < master->of_node)
401 node = node->rb_left;
402 else if (dev_node > master->of_node)
403 node = node->rb_right;
404 else
405 return master;
406 }
407
408 return NULL;
409}
410
Will Deacona9a1b0b2014-05-01 18:05:08 +0100411static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100412find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100413{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100414 struct arm_smmu_master_cfg *cfg = NULL;
415 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100416
Will Deacon8f68f8e2014-07-15 11:27:08 +0100417 if (group) {
418 cfg = iommu_group_get_iommudata(group);
419 iommu_group_put(group);
420 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100421
Will Deacon8f68f8e2014-07-15 11:27:08 +0100422 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100423}
424
Will Deacon45ae7cf2013-06-24 18:31:25 +0100425static int insert_smmu_master(struct arm_smmu_device *smmu,
426 struct arm_smmu_master *master)
427{
428 struct rb_node **new, *parent;
429
430 new = &smmu->masters.rb_node;
431 parent = NULL;
432 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700433 struct arm_smmu_master *this
434 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100435
436 parent = *new;
437 if (master->of_node < this->of_node)
438 new = &((*new)->rb_left);
439 else if (master->of_node > this->of_node)
440 new = &((*new)->rb_right);
441 else
442 return -EEXIST;
443 }
444
445 rb_link_node(&master->node, parent, new);
446 rb_insert_color(&master->node, &smmu->masters);
447 return 0;
448}
449
450static int register_smmu_master(struct arm_smmu_device *smmu,
451 struct device *dev,
452 struct of_phandle_args *masterspec)
453{
454 int i;
455 struct arm_smmu_master *master;
456
457 master = find_smmu_master(smmu, masterspec->np);
458 if (master) {
459 dev_err(dev,
460 "rejecting multiple registrations for master device %s\n",
461 masterspec->np->name);
462 return -EBUSY;
463 }
464
465 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
466 dev_err(dev,
467 "reached maximum number (%d) of stream IDs for master device %s\n",
468 MAX_MASTER_STREAMIDS, masterspec->np->name);
469 return -ENOSPC;
470 }
471
472 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
473 if (!master)
474 return -ENOMEM;
475
Will Deacona9a1b0b2014-05-01 18:05:08 +0100476 master->of_node = masterspec->np;
477 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100478
Olav Haugan3c8766d2014-08-22 17:12:32 -0700479 for (i = 0; i < master->cfg.num_streamids; ++i) {
480 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100481
Olav Haugan3c8766d2014-08-22 17:12:32 -0700482 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
483 (streamid >= smmu->num_mapping_groups)) {
484 dev_err(dev,
485 "stream ID for master device %s greater than maximum allowed (%d)\n",
486 masterspec->np->name, smmu->num_mapping_groups);
487 return -ERANGE;
488 }
489 master->cfg.streamids[i] = streamid;
490 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100491 return insert_smmu_master(smmu, master);
492}
493
Will Deacon44680ee2014-06-25 11:29:12 +0100494static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100495{
Will Deacon44680ee2014-06-25 11:29:12 +0100496 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100497 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100498 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100499
500 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100501 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100502 master = find_smmu_master(smmu, dev_node);
503 if (master)
504 break;
505 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100506 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100507
Will Deacona9a1b0b2014-05-01 18:05:08 +0100508 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100509}
510
511static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
512{
513 int idx;
514
515 do {
516 idx = find_next_zero_bit(map, end, start);
517 if (idx == end)
518 return -ENOSPC;
519 } while (test_and_set_bit(idx, map));
520
521 return idx;
522}
523
524static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
525{
526 clear_bit(idx, map);
527}
528
529/* Wait for any pending TLB invalidations to complete */
Will Deacon518f7132014-11-14 17:17:54 +0000530static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100531{
532 int count = 0;
533 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
534
535 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
536 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
537 & sTLBGSTATUS_GSACTIVE) {
538 cpu_relax();
539 if (++count == TLB_LOOP_TIMEOUT) {
540 dev_err_ratelimited(smmu->dev,
541 "TLB sync timed out -- SMMU may be deadlocked\n");
542 return;
543 }
544 udelay(1);
545 }
546}
547
Will Deacon518f7132014-11-14 17:17:54 +0000548static void arm_smmu_tlb_sync(void *cookie)
Will Deacon1463fe42013-07-31 19:21:27 +0100549{
Will Deacon518f7132014-11-14 17:17:54 +0000550 struct arm_smmu_domain *smmu_domain = cookie;
551 __arm_smmu_tlb_sync(smmu_domain->smmu);
552}
553
554static void arm_smmu_tlb_inv_context(void *cookie)
555{
556 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100557 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
558 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100559 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon518f7132014-11-14 17:17:54 +0000560 void __iomem *base;
Will Deacon1463fe42013-07-31 19:21:27 +0100561
562 if (stage1) {
563 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100564 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
565 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100566 } else {
567 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100568 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
569 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100570 }
571
Will Deacon518f7132014-11-14 17:17:54 +0000572 __arm_smmu_tlb_sync(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100573}
574
Will Deacon518f7132014-11-14 17:17:54 +0000575static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
576 bool leaf, void *cookie)
577{
578 struct arm_smmu_domain *smmu_domain = cookie;
579 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
580 struct arm_smmu_device *smmu = smmu_domain->smmu;
581 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
582 void __iomem *reg;
583
584 if (stage1) {
585 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
586 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
587
588 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
589 iova &= ~12UL;
590 iova |= ARM_SMMU_CB_ASID(cfg);
591 writel_relaxed(iova, reg);
592#ifdef CONFIG_64BIT
593 } else {
594 iova >>= 12;
595 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
596 writeq_relaxed(iova, reg);
597#endif
598 }
599#ifdef CONFIG_64BIT
600 } else if (smmu->version == ARM_SMMU_V2) {
601 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
602 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
603 ARM_SMMU_CB_S2_TLBIIPAS2;
604 writeq_relaxed(iova >> 12, reg);
605#endif
606 } else {
607 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
608 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
609 }
610}
611
612static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)
613{
614 struct arm_smmu_domain *smmu_domain = cookie;
615 struct arm_smmu_device *smmu = smmu_domain->smmu;
616 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
617
618
619 /* Ensure new page tables are visible to the hardware walker */
620 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
621 dsb(ishst);
622 } else {
623 /*
624 * If the SMMU can't walk tables in the CPU caches, treat them
625 * like non-coherent DMA since we need to flush the new entries
626 * all the way out to memory. There's no possibility of
627 * recursion here as the SMMU table walker will not be wired
628 * through another SMMU.
629 */
630 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
631 DMA_TO_DEVICE);
632 }
633}
634
635static struct iommu_gather_ops arm_smmu_gather_ops = {
636 .tlb_flush_all = arm_smmu_tlb_inv_context,
637 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
638 .tlb_sync = arm_smmu_tlb_sync,
639 .flush_pgtable = arm_smmu_flush_pgtable,
640};
641
Will Deacon45ae7cf2013-06-24 18:31:25 +0100642static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
643{
644 int flags, ret;
645 u32 fsr, far, fsynr, resume;
646 unsigned long iova;
647 struct iommu_domain *domain = dev;
648 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100649 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
650 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100651 void __iomem *cb_base;
652
Will Deacon44680ee2014-06-25 11:29:12 +0100653 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100654 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
655
656 if (!(fsr & FSR_FAULT))
657 return IRQ_NONE;
658
659 if (fsr & FSR_IGN)
660 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100661 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100662 fsr);
663
664 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
665 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
666
667 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
668 iova = far;
669#ifdef CONFIG_64BIT
670 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
671 iova |= ((unsigned long)far << 32);
672#endif
673
674 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
675 ret = IRQ_HANDLED;
676 resume = RESUME_RETRY;
677 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100678 dev_err_ratelimited(smmu->dev,
679 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100680 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100681 ret = IRQ_NONE;
682 resume = RESUME_TERMINATE;
683 }
684
685 /* Clear the faulting FSR */
686 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
687
688 /* Retry or terminate any stalled transactions */
689 if (fsr & FSR_SS)
690 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
691
692 return ret;
693}
694
695static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
696{
697 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
698 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000699 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100700
701 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
702 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
703 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
704 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
705
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000706 if (!gfsr)
707 return IRQ_NONE;
708
Will Deacon45ae7cf2013-06-24 18:31:25 +0100709 dev_err_ratelimited(smmu->dev,
710 "Unexpected global fault, this could be serious\n");
711 dev_err_ratelimited(smmu->dev,
712 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
713 gfsr, gfsynr0, gfsynr1, gfsynr2);
714
715 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100716 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100717}
718
Will Deacon518f7132014-11-14 17:17:54 +0000719static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
720 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100721{
722 u32 reg;
723 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100724 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
725 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100726 void __iomem *cb_base, *gr0_base, *gr1_base;
727
728 gr0_base = ARM_SMMU_GR0(smmu);
729 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100730 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
731 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100732
Will Deacon4a1c93c2015-03-04 12:21:03 +0000733 if (smmu->version > ARM_SMMU_V1) {
734 /*
735 * CBA2R.
736 * *Must* be initialised before CBAR thanks to VMID16
737 * architectural oversight affected some implementations.
738 */
739#ifdef CONFIG_64BIT
740 reg = CBA2R_RW64_64BIT;
741#else
742 reg = CBA2R_RW64_32BIT;
743#endif
744 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
745 }
746
Will Deacon45ae7cf2013-06-24 18:31:25 +0100747 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100748 reg = cfg->cbar;
Robin Murphy09360402014-08-28 17:51:59 +0100749 if (smmu->version == ARM_SMMU_V1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700750 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100751
Will Deacon57ca90f2014-02-06 14:59:05 +0000752 /*
753 * Use the weakest shareability/memory types, so they are
754 * overridden by the ttbcr/pte.
755 */
756 if (stage1) {
757 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
758 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
759 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100760 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000761 }
Will Deacon44680ee2014-06-25 11:29:12 +0100762 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100763
Will Deacon518f7132014-11-14 17:17:54 +0000764 /* TTBRs */
765 if (stage1) {
766 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
767 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
768 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32;
Will Deacon44680ee2014-06-25 11:29:12 +0100769 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon518f7132014-11-14 17:17:54 +0000770 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100771
Will Deacon518f7132014-11-14 17:17:54 +0000772 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
773 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO);
774 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32;
775 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
776 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI);
777 } else {
778 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
779 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
780 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32;
781 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
782 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100783
Will Deacon518f7132014-11-14 17:17:54 +0000784 /* TTBCR */
785 if (stage1) {
786 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
787 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
788 if (smmu->version > ARM_SMMU_V1) {
789 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
790 switch (smmu->va_size) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100791 case 32:
Will Deacon518f7132014-11-14 17:17:54 +0000792 reg |= (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100793 break;
794 case 36:
Will Deacon518f7132014-11-14 17:17:54 +0000795 reg |= (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100796 break;
797 case 40:
Will Deacon518f7132014-11-14 17:17:54 +0000798 reg |= (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100799 break;
800 case 42:
Will Deacon518f7132014-11-14 17:17:54 +0000801 reg |= (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100802 break;
803 case 44:
Will Deacon518f7132014-11-14 17:17:54 +0000804 reg |= (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100805 break;
806 case 48:
Will Deacon518f7132014-11-14 17:17:54 +0000807 reg |= (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100808 break;
809 }
Will Deacon518f7132014-11-14 17:17:54 +0000810 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100811 }
812 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000813 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
814 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100815 }
816
Will Deacon518f7132014-11-14 17:17:54 +0000817 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100818 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000819 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100820 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000821 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
822 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100823 }
824
Will Deacon45ae7cf2013-06-24 18:31:25 +0100825 /* SCTLR */
826 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
827 if (stage1)
828 reg |= SCTLR_S1_ASIDPNE;
829#ifdef __BIG_ENDIAN
830 reg |= SCTLR_E;
831#endif
Will Deacon25724842013-08-21 13:49:53 +0100832 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100833}
834
835static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100836 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100837{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100838 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000839 unsigned long ias, oas;
840 struct io_pgtable_ops *pgtbl_ops;
841 struct io_pgtable_cfg pgtbl_cfg;
842 enum io_pgtable_fmt fmt;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100843 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100844 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100845
Will Deacon518f7132014-11-14 17:17:54 +0000846 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100847 if (smmu_domain->smmu)
848 goto out_unlock;
849
Will Deaconc752ce42014-06-25 22:46:31 +0100850 /*
851 * Mapping the requested stage onto what we support is surprisingly
852 * complicated, mainly because the spec allows S1+S2 SMMUs without
853 * support for nested translation. That means we end up with the
854 * following table:
855 *
856 * Requested Supported Actual
857 * S1 N S1
858 * S1 S1+S2 S1
859 * S1 S2 S2
860 * S1 S1 S1
861 * N N N
862 * N S1+S2 S2
863 * N S2 S2
864 * N S1 S1
865 *
866 * Note that you can't actually request stage-2 mappings.
867 */
868 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
869 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
870 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
871 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
872
873 switch (smmu_domain->stage) {
874 case ARM_SMMU_DOMAIN_S1:
875 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
876 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000877 ias = smmu->va_size;
878 oas = smmu->ipa_size;
879 if (IS_ENABLED(CONFIG_64BIT))
880 fmt = ARM_64_LPAE_S1;
881 else
882 fmt = ARM_32_LPAE_S1;
Will Deaconc752ce42014-06-25 22:46:31 +0100883 break;
884 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100885 /*
886 * We will likely want to change this if/when KVM gets
887 * involved.
888 */
Will Deaconc752ce42014-06-25 22:46:31 +0100889 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100890 cfg->cbar = CBAR_TYPE_S2_TRANS;
891 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000892 ias = smmu->ipa_size;
893 oas = smmu->pa_size;
894 if (IS_ENABLED(CONFIG_64BIT))
895 fmt = ARM_64_LPAE_S2;
896 else
897 fmt = ARM_32_LPAE_S2;
Will Deaconc752ce42014-06-25 22:46:31 +0100898 break;
899 default:
900 ret = -EINVAL;
901 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100902 }
903
904 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
905 smmu->num_context_banks);
906 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100907 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100908
Will Deacon44680ee2014-06-25 11:29:12 +0100909 cfg->cbndx = ret;
Robin Murphy09360402014-08-28 17:51:59 +0100910 if (smmu->version == ARM_SMMU_V1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100911 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
912 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100913 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100914 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100915 }
916
Will Deacon518f7132014-11-14 17:17:54 +0000917 pgtbl_cfg = (struct io_pgtable_cfg) {
918 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
919 .ias = ias,
920 .oas = oas,
921 .tlb = &arm_smmu_gather_ops,
922 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100923
Will Deacon518f7132014-11-14 17:17:54 +0000924 smmu_domain->smmu = smmu;
925 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
926 if (!pgtbl_ops) {
927 ret = -ENOMEM;
928 goto out_clear_smmu;
929 }
930
931 /* Update our support page sizes to reflect the page table format */
932 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
933
934 /* Initialise the context bank with our page table cfg */
935 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
936
937 /*
938 * Request context fault interrupt. Do this last to avoid the
939 * handler seeing a half-initialised domain state.
940 */
Will Deacon44680ee2014-06-25 11:29:12 +0100941 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100942 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
943 "arm-smmu-context-fault", domain);
944 if (IS_ERR_VALUE(ret)) {
945 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100946 cfg->irptndx, irq);
947 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100948 }
949
Will Deacon518f7132014-11-14 17:17:54 +0000950 mutex_unlock(&smmu_domain->init_mutex);
951
952 /* Publish page table ops for map/unmap */
953 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100954 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100955
Will Deacon518f7132014-11-14 17:17:54 +0000956out_clear_smmu:
957 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100958out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000959 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100960 return ret;
961}
962
963static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
964{
965 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon44680ee2014-06-25 11:29:12 +0100966 struct arm_smmu_device *smmu = smmu_domain->smmu;
967 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100968 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100969 int irq;
970
971 if (!smmu)
972 return;
973
Will Deacon518f7132014-11-14 17:17:54 +0000974 /*
975 * Disable the context bank and free the page tables before freeing
976 * it.
977 */
Will Deacon44680ee2014-06-25 11:29:12 +0100978 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100979 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +0100980
Will Deacon44680ee2014-06-25 11:29:12 +0100981 if (cfg->irptndx != INVALID_IRPTNDX) {
982 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100983 free_irq(irq, domain);
984 }
985
Will Deacon518f7132014-11-14 17:17:54 +0000986 if (smmu_domain->pgtbl_ops)
987 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
988
Will Deacon44680ee2014-06-25 11:29:12 +0100989 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100990}
991
992static int arm_smmu_domain_init(struct iommu_domain *domain)
993{
994 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100995
996 /*
997 * Allocate the domain and initialise some of its data structures.
998 * We can't really do anything meaningful until we've added a
999 * master.
1000 */
1001 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1002 if (!smmu_domain)
1003 return -ENOMEM;
1004
Will Deacon518f7132014-11-14 17:17:54 +00001005 mutex_init(&smmu_domain->init_mutex);
1006 spin_lock_init(&smmu_domain->pgtbl_lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001007 domain->priv = smmu_domain;
1008 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001009}
1010
1011static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1012{
1013 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon1463fe42013-07-31 19:21:27 +01001014
1015 /*
1016 * Free the domain resources. We assume that all devices have
1017 * already been detached.
1018 */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001019 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001020 kfree(smmu_domain);
1021}
1022
1023static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001024 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001025{
1026 int i;
1027 struct arm_smmu_smr *smrs;
1028 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1029
1030 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1031 return 0;
1032
Will Deacona9a1b0b2014-05-01 18:05:08 +01001033 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001034 return -EEXIST;
1035
Mitchel Humpherys29073202014-07-08 09:52:18 -07001036 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001037 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001038 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1039 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001040 return -ENOMEM;
1041 }
1042
Will Deacon44680ee2014-06-25 11:29:12 +01001043 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001044 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001045 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1046 smmu->num_mapping_groups);
1047 if (IS_ERR_VALUE(idx)) {
1048 dev_err(smmu->dev, "failed to allocate free SMR\n");
1049 goto err_free_smrs;
1050 }
1051
1052 smrs[i] = (struct arm_smmu_smr) {
1053 .idx = idx,
1054 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001055 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001056 };
1057 }
1058
1059 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001060 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001061 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1062 smrs[i].mask << SMR_MASK_SHIFT;
1063 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1064 }
1065
Will Deacona9a1b0b2014-05-01 18:05:08 +01001066 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001067 return 0;
1068
1069err_free_smrs:
1070 while (--i >= 0)
1071 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1072 kfree(smrs);
1073 return -ENOSPC;
1074}
1075
1076static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001077 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001078{
1079 int i;
1080 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001081 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001082
Will Deacon43b412b2014-07-15 11:22:24 +01001083 if (!smrs)
1084 return;
1085
Will Deacon45ae7cf2013-06-24 18:31:25 +01001086 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001087 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001088 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001089
Will Deacon45ae7cf2013-06-24 18:31:25 +01001090 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1091 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1092 }
1093
Will Deacona9a1b0b2014-05-01 18:05:08 +01001094 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001095 kfree(smrs);
1096}
1097
Will Deacon45ae7cf2013-06-24 18:31:25 +01001098static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001099 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001100{
1101 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001102 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001103 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1104
Will Deacon8f68f8e2014-07-15 11:27:08 +01001105 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001106 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001107 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001108 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001109
Will Deacona9a1b0b2014-05-01 18:05:08 +01001110 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001111 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001112
Will Deacona9a1b0b2014-05-01 18:05:08 +01001113 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001114 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001115 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001116 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1117 }
1118
1119 return 0;
1120}
1121
1122static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001123 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001124{
Will Deacon43b412b2014-07-15 11:22:24 +01001125 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001126 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001127 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001128
Will Deacon8f68f8e2014-07-15 11:27:08 +01001129 /* An IOMMU group is torn down by the first device to be removed */
1130 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1131 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001132
1133 /*
1134 * We *must* clear the S2CR first, because freeing the SMR means
1135 * that it can be re-allocated immediately.
1136 */
Will Deacon43b412b2014-07-15 11:22:24 +01001137 for (i = 0; i < cfg->num_streamids; ++i) {
1138 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1139
1140 writel_relaxed(S2CR_TYPE_BYPASS,
1141 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1142 }
1143
Will Deacona9a1b0b2014-05-01 18:05:08 +01001144 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001145}
1146
1147static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1148{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001149 int ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001150 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon518f7132014-11-14 17:17:54 +00001151 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001152 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001153
Will Deacon8f68f8e2014-07-15 11:27:08 +01001154 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001155 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001156 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1157 return -ENXIO;
1158 }
1159
Will Deacon844e35b2014-07-17 11:23:51 +01001160 if (dev->archdata.iommu) {
1161 dev_err(dev, "already attached to IOMMU domain\n");
1162 return -EEXIST;
1163 }
1164
Will Deacon518f7132014-11-14 17:17:54 +00001165 /* Ensure that the domain is finalised */
1166 ret = arm_smmu_init_domain_context(domain, smmu);
1167 if (IS_ERR_VALUE(ret))
1168 return ret;
1169
Will Deacon45ae7cf2013-06-24 18:31:25 +01001170 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001171 * Sanity check the domain. We don't support domains across
1172 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001173 */
Will Deacon518f7132014-11-14 17:17:54 +00001174 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001175 dev_err(dev,
1176 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001177 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1178 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001179 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001180
1181 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001182 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001183 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001184 return -ENODEV;
1185
Will Deacon844e35b2014-07-17 11:23:51 +01001186 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1187 if (!ret)
1188 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001189 return ret;
1190}
1191
1192static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1193{
1194 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001195 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001196
Will Deacon8f68f8e2014-07-15 11:27:08 +01001197 cfg = find_smmu_master_cfg(dev);
Will Deacon844e35b2014-07-17 11:23:51 +01001198 if (!cfg)
1199 return;
1200
1201 dev->archdata.iommu = NULL;
1202 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001203}
1204
Will Deacon45ae7cf2013-06-24 18:31:25 +01001205static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001206 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001207{
Will Deacon518f7132014-11-14 17:17:54 +00001208 int ret;
1209 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001210 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon518f7132014-11-14 17:17:54 +00001211 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001212
Will Deacon518f7132014-11-14 17:17:54 +00001213 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001214 return -ENODEV;
1215
Will Deacon518f7132014-11-14 17:17:54 +00001216 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1217 ret = ops->map(ops, iova, paddr, size, prot);
1218 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1219 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001220}
1221
1222static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1223 size_t size)
1224{
Will Deacon518f7132014-11-14 17:17:54 +00001225 size_t ret;
1226 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001227 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon518f7132014-11-14 17:17:54 +00001228 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001229
Will Deacon518f7132014-11-14 17:17:54 +00001230 if (!ops)
1231 return 0;
1232
1233 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1234 ret = ops->unmap(ops, iova, size);
1235 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1236 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001237}
1238
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001239static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1240 dma_addr_t iova)
1241{
1242 struct arm_smmu_domain *smmu_domain = domain->priv;
1243 struct arm_smmu_device *smmu = smmu_domain->smmu;
1244 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1245 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1246 struct device *dev = smmu->dev;
1247 void __iomem *cb_base;
1248 u32 tmp;
1249 u64 phys;
1250
1251 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1252
1253 if (smmu->version == 1) {
1254 u32 reg = iova & ~0xfff;
1255 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
1256 } else {
1257 u32 reg = iova & ~0xfff;
1258 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
Arnd Bergmanna4188be2015-01-30 22:55:55 +01001259 reg = ((u64)iova & ~0xfff) >> 32;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001260 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_HI);
1261 }
1262
1263 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1264 !(tmp & ATSR_ACTIVE), 5, 50)) {
1265 dev_err(dev,
1266 "iova to phys timed out on 0x%pad. Falling back to software table walk.\n",
1267 &iova);
1268 return ops->iova_to_phys(ops, iova);
1269 }
1270
1271 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1272 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1273
1274 if (phys & CB_PAR_F) {
1275 dev_err(dev, "translation fault!\n");
1276 dev_err(dev, "PAR = 0x%llx\n", phys);
1277 return 0;
1278 }
1279
1280 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1281}
1282
Will Deacon45ae7cf2013-06-24 18:31:25 +01001283static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001284 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001285{
Will Deacon518f7132014-11-14 17:17:54 +00001286 phys_addr_t ret;
1287 unsigned long flags;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001288 struct arm_smmu_domain *smmu_domain = domain->priv;
Will Deacon518f7132014-11-14 17:17:54 +00001289 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001290
Will Deacon518f7132014-11-14 17:17:54 +00001291 if (!ops)
Will Deacona44a97912013-11-07 18:47:50 +00001292 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001293
Will Deacon518f7132014-11-14 17:17:54 +00001294 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001295 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS)
1296 ret = arm_smmu_iova_to_phys_hard(domain, iova);
1297 else
1298 ret = ops->iova_to_phys(ops, iova);
Will Deacon518f7132014-11-14 17:17:54 +00001299 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001300
Will Deacon518f7132014-11-14 17:17:54 +00001301 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001302}
1303
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001304static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001305{
Will Deacond0948942014-06-24 17:30:10 +01001306 switch (cap) {
1307 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001308 /*
1309 * Return true here as the SMMU can always send out coherent
1310 * requests.
1311 */
1312 return true;
Will Deacond0948942014-06-24 17:30:10 +01001313 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001314 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001315 case IOMMU_CAP_NOEXEC:
1316 return true;
Will Deacond0948942014-06-24 17:30:10 +01001317 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001318 return false;
Will Deacond0948942014-06-24 17:30:10 +01001319 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001320}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001321
Will Deacona9a1b0b2014-05-01 18:05:08 +01001322static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1323{
1324 *((u16 *)data) = alias;
1325 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001326}
1327
Will Deacon8f68f8e2014-07-15 11:27:08 +01001328static void __arm_smmu_release_pci_iommudata(void *data)
1329{
1330 kfree(data);
1331}
1332
Will Deacon45ae7cf2013-06-24 18:31:25 +01001333static int arm_smmu_add_device(struct device *dev)
1334{
Will Deacona9a1b0b2014-05-01 18:05:08 +01001335 struct arm_smmu_device *smmu;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001336 struct arm_smmu_master_cfg *cfg;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001337 struct iommu_group *group;
Will Deacon8f68f8e2014-07-15 11:27:08 +01001338 void (*releasefn)(void *) = NULL;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001339 int ret;
1340
Will Deacon44680ee2014-06-25 11:29:12 +01001341 smmu = find_smmu_for_device(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001342 if (!smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001343 return -ENODEV;
1344
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001345 group = iommu_group_alloc();
1346 if (IS_ERR(group)) {
1347 dev_err(dev, "Failed to allocate IOMMU group\n");
1348 return PTR_ERR(group);
1349 }
1350
Will Deacona9a1b0b2014-05-01 18:05:08 +01001351 if (dev_is_pci(dev)) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001352 struct pci_dev *pdev = to_pci_dev(dev);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001353
Will Deacona9a1b0b2014-05-01 18:05:08 +01001354 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1355 if (!cfg) {
1356 ret = -ENOMEM;
1357 goto out_put_group;
1358 }
1359
1360 cfg->num_streamids = 1;
1361 /*
1362 * Assume Stream ID == Requester ID for now.
1363 * We need a way to describe the ID mappings in FDT.
1364 */
1365 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1366 &cfg->streamids[0]);
Will Deacon8f68f8e2014-07-15 11:27:08 +01001367 releasefn = __arm_smmu_release_pci_iommudata;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001368 } else {
Will Deacon8f68f8e2014-07-15 11:27:08 +01001369 struct arm_smmu_master *master;
1370
1371 master = find_smmu_master(smmu, dev->of_node);
1372 if (!master) {
1373 ret = -ENODEV;
1374 goto out_put_group;
1375 }
1376
1377 cfg = &master->cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001378 }
1379
Will Deacon8f68f8e2014-07-15 11:27:08 +01001380 iommu_group_set_iommudata(group, cfg, releasefn);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001381 ret = iommu_group_add_device(group, dev);
1382
1383out_put_group:
1384 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001385 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001386}
1387
1388static void arm_smmu_remove_device(struct device *dev)
1389{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001390 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001391}
1392
Will Deaconc752ce42014-06-25 22:46:31 +01001393static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1394 enum iommu_attr attr, void *data)
1395{
1396 struct arm_smmu_domain *smmu_domain = domain->priv;
1397
1398 switch (attr) {
1399 case DOMAIN_ATTR_NESTING:
1400 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1401 return 0;
1402 default:
1403 return -ENODEV;
1404 }
1405}
1406
1407static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1408 enum iommu_attr attr, void *data)
1409{
Will Deacon518f7132014-11-14 17:17:54 +00001410 int ret = 0;
Will Deaconc752ce42014-06-25 22:46:31 +01001411 struct arm_smmu_domain *smmu_domain = domain->priv;
1412
Will Deacon518f7132014-11-14 17:17:54 +00001413 mutex_lock(&smmu_domain->init_mutex);
1414
Will Deaconc752ce42014-06-25 22:46:31 +01001415 switch (attr) {
1416 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001417 if (smmu_domain->smmu) {
1418 ret = -EPERM;
1419 goto out_unlock;
1420 }
1421
Will Deaconc752ce42014-06-25 22:46:31 +01001422 if (*(int *)data)
1423 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1424 else
1425 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1426
Will Deacon518f7132014-11-14 17:17:54 +00001427 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001428 default:
Will Deacon518f7132014-11-14 17:17:54 +00001429 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001430 }
Will Deacon518f7132014-11-14 17:17:54 +00001431
1432out_unlock:
1433 mutex_unlock(&smmu_domain->init_mutex);
1434 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001435}
1436
Will Deacon518f7132014-11-14 17:17:54 +00001437static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001438 .capable = arm_smmu_capable,
1439 .domain_init = arm_smmu_domain_init,
1440 .domain_destroy = arm_smmu_domain_destroy,
1441 .attach_dev = arm_smmu_attach_dev,
1442 .detach_dev = arm_smmu_detach_dev,
1443 .map = arm_smmu_map,
1444 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001445 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001446 .iova_to_phys = arm_smmu_iova_to_phys,
1447 .add_device = arm_smmu_add_device,
1448 .remove_device = arm_smmu_remove_device,
1449 .domain_get_attr = arm_smmu_domain_get_attr,
1450 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001451 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001452};
1453
1454static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1455{
1456 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001457 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001458 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001459 u32 reg;
1460
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001461 /* clear global FSR */
1462 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1463 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001464
1465 /* Mark all SMRn as invalid and all S2CRn as bypass */
1466 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001467 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001468 writel_relaxed(S2CR_TYPE_BYPASS,
1469 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001470 }
1471
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001472 /* Make sure all context banks are disabled and clear CB_FSR */
1473 for (i = 0; i < smmu->num_context_banks; ++i) {
1474 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1475 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1476 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1477 }
Will Deacon1463fe42013-07-31 19:21:27 +01001478
Will Deacon45ae7cf2013-06-24 18:31:25 +01001479 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001480 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1481 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1482
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001483 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001484
Will Deacon45ae7cf2013-06-24 18:31:25 +01001485 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001486 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001487
1488 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001489 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001490
1491 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001492 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001493
1494 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001495 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001496
1497 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001498 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001499
1500 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001501 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001502 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001503}
1504
1505static int arm_smmu_id_size_to_bits(int size)
1506{
1507 switch (size) {
1508 case 0:
1509 return 32;
1510 case 1:
1511 return 36;
1512 case 2:
1513 return 40;
1514 case 3:
1515 return 42;
1516 case 4:
1517 return 44;
1518 case 5:
1519 default:
1520 return 48;
1521 }
1522}
1523
1524static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1525{
1526 unsigned long size;
1527 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1528 u32 id;
1529
1530 dev_notice(smmu->dev, "probing hardware configuration...\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001531 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1532
1533 /* ID0 */
1534 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001535
1536 /* Restrict available stages based on module parameter */
1537 if (force_stage == 1)
1538 id &= ~(ID0_S2TS | ID0_NTS);
1539 else if (force_stage == 2)
1540 id &= ~(ID0_S1TS | ID0_NTS);
1541
Will Deacon45ae7cf2013-06-24 18:31:25 +01001542 if (id & ID0_S1TS) {
1543 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1544 dev_notice(smmu->dev, "\tstage 1 translation\n");
1545 }
1546
1547 if (id & ID0_S2TS) {
1548 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1549 dev_notice(smmu->dev, "\tstage 2 translation\n");
1550 }
1551
1552 if (id & ID0_NTS) {
1553 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1554 dev_notice(smmu->dev, "\tnested translation\n");
1555 }
1556
1557 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001558 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001559 dev_err(smmu->dev, "\tno translation support!\n");
1560 return -ENODEV;
1561 }
1562
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001563 if (smmu->version == 1 || (!(id & ID0_ATOSNS) && (id & ID0_S1TS))) {
1564 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1565 dev_notice(smmu->dev, "\taddress translation ops\n");
1566 }
1567
Will Deacon45ae7cf2013-06-24 18:31:25 +01001568 if (id & ID0_CTTW) {
1569 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1570 dev_notice(smmu->dev, "\tcoherent table walk\n");
1571 }
1572
1573 if (id & ID0_SMS) {
1574 u32 smr, sid, mask;
1575
1576 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1577 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1578 ID0_NUMSMRG_MASK;
1579 if (smmu->num_mapping_groups == 0) {
1580 dev_err(smmu->dev,
1581 "stream-matching supported, but no SMRs present!\n");
1582 return -ENODEV;
1583 }
1584
1585 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1586 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1587 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1588 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1589
1590 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1591 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1592 if ((mask & sid) != sid) {
1593 dev_err(smmu->dev,
1594 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1595 mask, sid);
1596 return -ENODEV;
1597 }
1598
1599 dev_notice(smmu->dev,
1600 "\tstream matching with %u register groups, mask 0x%x",
1601 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001602 } else {
1603 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1604 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001605 }
1606
1607 /* ID1 */
1608 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001609 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001610
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001611 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001612 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001613 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001614 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001615 dev_warn(smmu->dev,
1616 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1617 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001618
Will Deacon518f7132014-11-14 17:17:54 +00001619 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001620 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1621 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1622 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1623 return -ENODEV;
1624 }
1625 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1626 smmu->num_context_banks, smmu->num_s2_context_banks);
1627
1628 /* ID2 */
1629 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1630 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001631 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001632
Will Deacon518f7132014-11-14 17:17:54 +00001633 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001634 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001635 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001636
Robin Murphy09360402014-08-28 17:51:59 +01001637 if (smmu->version == ARM_SMMU_V1) {
Will Deacon518f7132014-11-14 17:17:54 +00001638 smmu->va_size = smmu->ipa_size;
1639 size = SZ_4K | SZ_2M | SZ_1G;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001640 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001641 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001642 smmu->va_size = arm_smmu_id_size_to_bits(size);
1643#ifndef CONFIG_64BIT
1644 smmu->va_size = min(32UL, smmu->va_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001645#endif
Will Deacon518f7132014-11-14 17:17:54 +00001646 size = 0;
1647 if (id & ID2_PTFS_4K)
1648 size |= SZ_4K | SZ_2M | SZ_1G;
1649 if (id & ID2_PTFS_16K)
1650 size |= SZ_16K | SZ_32M;
1651 if (id & ID2_PTFS_64K)
1652 size |= SZ_64K | SZ_512M;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001653 }
1654
Will Deacon518f7132014-11-14 17:17:54 +00001655 arm_smmu_ops.pgsize_bitmap &= size;
1656 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1657
Will Deacon28d60072014-09-01 16:24:48 +01001658 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1659 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001660 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001661
1662 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1663 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001664 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001665
Will Deacon45ae7cf2013-06-24 18:31:25 +01001666 return 0;
1667}
1668
Joerg Roedel09b52692014-10-02 12:24:45 +02001669static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy09360402014-08-28 17:51:59 +01001670 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1671 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1672 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
Robin Murphyd3aba042014-08-28 17:52:00 +01001673 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
Robin Murphy09360402014-08-28 17:51:59 +01001674 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1675 { },
1676};
1677MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1678
Will Deacon45ae7cf2013-06-24 18:31:25 +01001679static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1680{
Robin Murphy09360402014-08-28 17:51:59 +01001681 const struct of_device_id *of_id;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001682 struct resource *res;
1683 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001684 struct device *dev = &pdev->dev;
1685 struct rb_node *node;
1686 struct of_phandle_args masterspec;
1687 int num_irqs, i, err;
1688
1689 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1690 if (!smmu) {
1691 dev_err(dev, "failed to allocate arm_smmu_device\n");
1692 return -ENOMEM;
1693 }
1694 smmu->dev = dev;
1695
Robin Murphy09360402014-08-28 17:51:59 +01001696 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1697 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1698
Will Deacon45ae7cf2013-06-24 18:31:25 +01001699 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001700 smmu->base = devm_ioremap_resource(dev, res);
1701 if (IS_ERR(smmu->base))
1702 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001703 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001704
1705 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1706 &smmu->num_global_irqs)) {
1707 dev_err(dev, "missing #global-interrupts property\n");
1708 return -ENODEV;
1709 }
1710
1711 num_irqs = 0;
1712 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1713 num_irqs++;
1714 if (num_irqs > smmu->num_global_irqs)
1715 smmu->num_context_irqs++;
1716 }
1717
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001718 if (!smmu->num_context_irqs) {
1719 dev_err(dev, "found %d interrupts but expected at least %d\n",
1720 num_irqs, smmu->num_global_irqs + 1);
1721 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001722 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001723
1724 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1725 GFP_KERNEL);
1726 if (!smmu->irqs) {
1727 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1728 return -ENOMEM;
1729 }
1730
1731 for (i = 0; i < num_irqs; ++i) {
1732 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001733
Will Deacon45ae7cf2013-06-24 18:31:25 +01001734 if (irq < 0) {
1735 dev_err(dev, "failed to get irq index %d\n", i);
1736 return -ENODEV;
1737 }
1738 smmu->irqs[i] = irq;
1739 }
1740
Olav Haugan3c8766d2014-08-22 17:12:32 -07001741 err = arm_smmu_device_cfg_probe(smmu);
1742 if (err)
1743 return err;
1744
Will Deacon45ae7cf2013-06-24 18:31:25 +01001745 i = 0;
1746 smmu->masters = RB_ROOT;
1747 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1748 "#stream-id-cells", i,
1749 &masterspec)) {
1750 err = register_smmu_master(smmu, dev, &masterspec);
1751 if (err) {
1752 dev_err(dev, "failed to add master %s\n",
1753 masterspec.np->name);
1754 goto out_put_masters;
1755 }
1756
1757 i++;
1758 }
1759 dev_notice(dev, "registered %d master devices\n", i);
1760
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001761 parse_driver_options(smmu);
1762
Robin Murphy09360402014-08-28 17:51:59 +01001763 if (smmu->version > ARM_SMMU_V1 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001764 smmu->num_context_banks != smmu->num_context_irqs) {
1765 dev_err(dev,
1766 "found only %d context interrupt(s) but %d required\n",
1767 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001768 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001769 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001770 }
1771
Will Deacon45ae7cf2013-06-24 18:31:25 +01001772 for (i = 0; i < smmu->num_global_irqs; ++i) {
1773 err = request_irq(smmu->irqs[i],
1774 arm_smmu_global_fault,
1775 IRQF_SHARED,
1776 "arm-smmu global fault",
1777 smmu);
1778 if (err) {
1779 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1780 i, smmu->irqs[i]);
1781 goto out_free_irqs;
1782 }
1783 }
1784
1785 INIT_LIST_HEAD(&smmu->list);
1786 spin_lock(&arm_smmu_devices_lock);
1787 list_add(&smmu->list, &arm_smmu_devices);
1788 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001789
1790 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001791 return 0;
1792
1793out_free_irqs:
1794 while (i--)
1795 free_irq(smmu->irqs[i], smmu);
1796
Will Deacon45ae7cf2013-06-24 18:31:25 +01001797out_put_masters:
1798 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001799 struct arm_smmu_master *master
1800 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001801 of_node_put(master->of_node);
1802 }
1803
1804 return err;
1805}
1806
1807static int arm_smmu_device_remove(struct platform_device *pdev)
1808{
1809 int i;
1810 struct device *dev = &pdev->dev;
1811 struct arm_smmu_device *curr, *smmu = NULL;
1812 struct rb_node *node;
1813
1814 spin_lock(&arm_smmu_devices_lock);
1815 list_for_each_entry(curr, &arm_smmu_devices, list) {
1816 if (curr->dev == dev) {
1817 smmu = curr;
1818 list_del(&smmu->list);
1819 break;
1820 }
1821 }
1822 spin_unlock(&arm_smmu_devices_lock);
1823
1824 if (!smmu)
1825 return -ENODEV;
1826
Will Deacon45ae7cf2013-06-24 18:31:25 +01001827 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001828 struct arm_smmu_master *master
1829 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001830 of_node_put(master->of_node);
1831 }
1832
Will Deaconecfadb62013-07-31 19:21:28 +01001833 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001834 dev_err(dev, "removing device with active domains!\n");
1835
1836 for (i = 0; i < smmu->num_global_irqs; ++i)
1837 free_irq(smmu->irqs[i], smmu);
1838
1839 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001840 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001841 return 0;
1842}
1843
Will Deacon45ae7cf2013-06-24 18:31:25 +01001844static struct platform_driver arm_smmu_driver = {
1845 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001846 .name = "arm-smmu",
1847 .of_match_table = of_match_ptr(arm_smmu_of_match),
1848 },
1849 .probe = arm_smmu_device_dt_probe,
1850 .remove = arm_smmu_device_remove,
1851};
1852
1853static int __init arm_smmu_init(void)
1854{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001855 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001856 int ret;
1857
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001858 /*
1859 * Play nice with systems that don't have an ARM SMMU by checking that
1860 * an ARM SMMU exists in the system before proceeding with the driver
1861 * and IOMMU bus operation registration.
1862 */
1863 np = of_find_matching_node(NULL, arm_smmu_of_match);
1864 if (!np)
1865 return 0;
1866
1867 of_node_put(np);
1868
Will Deacon45ae7cf2013-06-24 18:31:25 +01001869 ret = platform_driver_register(&arm_smmu_driver);
1870 if (ret)
1871 return ret;
1872
1873 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001874 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001875 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1876
Will Deacond123cf82014-02-04 22:17:53 +00001877#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01001878 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001879 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00001880#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01001881
Will Deacona9a1b0b2014-05-01 18:05:08 +01001882#ifdef CONFIG_PCI
1883 if (!iommu_present(&pci_bus_type))
1884 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1885#endif
1886
Will Deacon45ae7cf2013-06-24 18:31:25 +01001887 return 0;
1888}
1889
1890static void __exit arm_smmu_exit(void)
1891{
1892 return platform_driver_unregister(&arm_smmu_driver);
1893}
1894
Andreas Herrmannb1950b22013-10-01 13:39:05 +01001895subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001896module_exit(arm_smmu_exit);
1897
1898MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1899MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1900MODULE_LICENSE("GPL v2");