blob: e8e7bcc4540c4093bde79214328bb2b7739a7be3 [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>
Robin Murphy9adb9592016-01-26 18:06:36 +000032#include <linux/dma-iommu.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010033#include <linux/dma-mapping.h>
34#include <linux/err.h>
35#include <linux/interrupt.h>
36#include <linux/io.h>
37#include <linux/iommu.h>
Mitchel Humpherys859a7322014-10-29 21:13:40 +000038#include <linux/iopoll.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010039#include <linux/module.h>
40#include <linux/of.h>
Robin Murphybae2c2d2015-07-29 19:46:05 +010041#include <linux/of_address.h>
Will Deacona9a1b0b2014-05-01 18:05:08 +010042#include <linux/pci.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010043#include <linux/platform_device.h>
44#include <linux/slab.h>
45#include <linux/spinlock.h>
46
47#include <linux/amba/bus.h>
48
Will Deacon518f7132014-11-14 17:17:54 +000049#include "io-pgtable.h"
Will Deacon45ae7cf2013-06-24 18:31:25 +010050
51/* Maximum number of stream IDs assigned to a single device */
Andreas Herrmann636e97b2014-01-30 18:18:08 +000052#define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
Will Deacon45ae7cf2013-06-24 18:31:25 +010053
54/* Maximum number of context banks per SMMU */
55#define ARM_SMMU_MAX_CBS 128
56
57/* Maximum number of mapping groups per SMMU */
58#define ARM_SMMU_MAX_SMRS 128
59
Will Deacon45ae7cf2013-06-24 18:31:25 +010060/* SMMU global address space */
61#define ARM_SMMU_GR0(smmu) ((smmu)->base)
Will Deaconc757e852014-07-30 11:33:25 +010062#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +010063
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +000064/*
65 * SMMU global address space with conditional offset to access secure
66 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
67 * nsGFSYNR0: 0x450)
68 */
69#define ARM_SMMU_GR0_NS(smmu) \
70 ((smmu)->base + \
71 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
72 ? 0x400 : 0))
73
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +010074#ifdef CONFIG_64BIT
75#define smmu_writeq writeq_relaxed
76#else
77#define smmu_writeq(reg64, addr) \
78 do { \
79 u64 __val = (reg64); \
80 void __iomem *__addr = (addr); \
81 writel_relaxed(__val >> 32, __addr + 4); \
82 writel_relaxed(__val, __addr); \
83 } while (0)
84#endif
85
Will Deacon45ae7cf2013-06-24 18:31:25 +010086/* Configuration registers */
87#define ARM_SMMU_GR0_sCR0 0x0
88#define sCR0_CLIENTPD (1 << 0)
89#define sCR0_GFRE (1 << 1)
90#define sCR0_GFIE (1 << 2)
91#define sCR0_GCFGFRE (1 << 4)
92#define sCR0_GCFGFIE (1 << 5)
93#define sCR0_USFCFG (1 << 10)
94#define sCR0_VMIDPNE (1 << 11)
95#define sCR0_PTM (1 << 12)
96#define sCR0_FB (1 << 13)
97#define sCR0_BSU_SHIFT 14
98#define sCR0_BSU_MASK 0x3
99
100/* Identification registers */
101#define ARM_SMMU_GR0_ID0 0x20
102#define ARM_SMMU_GR0_ID1 0x24
103#define ARM_SMMU_GR0_ID2 0x28
104#define ARM_SMMU_GR0_ID3 0x2c
105#define ARM_SMMU_GR0_ID4 0x30
106#define ARM_SMMU_GR0_ID5 0x34
107#define ARM_SMMU_GR0_ID6 0x38
108#define ARM_SMMU_GR0_ID7 0x3c
109#define ARM_SMMU_GR0_sGFSR 0x48
110#define ARM_SMMU_GR0_sGFSYNR0 0x50
111#define ARM_SMMU_GR0_sGFSYNR1 0x54
112#define ARM_SMMU_GR0_sGFSYNR2 0x58
Will Deacon45ae7cf2013-06-24 18:31:25 +0100113
114#define ID0_S1TS (1 << 30)
115#define ID0_S2TS (1 << 29)
116#define ID0_NTS (1 << 28)
117#define ID0_SMS (1 << 27)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000118#define ID0_ATOSNS (1 << 26)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100119#define ID0_CTTW (1 << 14)
120#define ID0_NUMIRPT_SHIFT 16
121#define ID0_NUMIRPT_MASK 0xff
Olav Haugan3c8766d2014-08-22 17:12:32 -0700122#define ID0_NUMSIDB_SHIFT 9
123#define ID0_NUMSIDB_MASK 0xf
Will Deacon45ae7cf2013-06-24 18:31:25 +0100124#define ID0_NUMSMRG_SHIFT 0
125#define ID0_NUMSMRG_MASK 0xff
126
127#define ID1_PAGESIZE (1 << 31)
128#define ID1_NUMPAGENDXB_SHIFT 28
129#define ID1_NUMPAGENDXB_MASK 7
130#define ID1_NUMS2CB_SHIFT 16
131#define ID1_NUMS2CB_MASK 0xff
132#define ID1_NUMCB_SHIFT 0
133#define ID1_NUMCB_MASK 0xff
134
135#define ID2_OAS_SHIFT 4
136#define ID2_OAS_MASK 0xf
137#define ID2_IAS_SHIFT 0
138#define ID2_IAS_MASK 0xf
139#define ID2_UBS_SHIFT 8
140#define ID2_UBS_MASK 0xf
141#define ID2_PTFS_4K (1 << 12)
142#define ID2_PTFS_16K (1 << 13)
143#define ID2_PTFS_64K (1 << 14)
144
Will Deacon45ae7cf2013-06-24 18:31:25 +0100145/* Global TLB invalidation */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100146#define ARM_SMMU_GR0_TLBIVMID 0x64
147#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
148#define ARM_SMMU_GR0_TLBIALLH 0x6c
149#define ARM_SMMU_GR0_sTLBGSYNC 0x70
150#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
151#define sTLBGSTATUS_GSACTIVE (1 << 0)
152#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
153
154/* Stream mapping registers */
155#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
156#define SMR_VALID (1 << 31)
157#define SMR_MASK_SHIFT 16
158#define SMR_MASK_MASK 0x7fff
159#define SMR_ID_SHIFT 0
160#define SMR_ID_MASK 0x7fff
161
162#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
163#define S2CR_CBNDX_SHIFT 0
164#define S2CR_CBNDX_MASK 0xff
165#define S2CR_TYPE_SHIFT 16
166#define S2CR_TYPE_MASK 0x3
167#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
168#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
169#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
170
Robin Murphyd3461802016-01-26 18:06:34 +0000171#define S2CR_PRIVCFG_SHIFT 24
172#define S2CR_PRIVCFG_UNPRIV (2 << S2CR_PRIVCFG_SHIFT)
173
Will Deacon45ae7cf2013-06-24 18:31:25 +0100174/* Context bank attribute registers */
175#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
176#define CBAR_VMID_SHIFT 0
177#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000178#define CBAR_S1_BPSHCFG_SHIFT 8
179#define CBAR_S1_BPSHCFG_MASK 3
180#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100181#define CBAR_S1_MEMATTR_SHIFT 12
182#define CBAR_S1_MEMATTR_MASK 0xf
183#define CBAR_S1_MEMATTR_WB 0xf
184#define CBAR_TYPE_SHIFT 16
185#define CBAR_TYPE_MASK 0x3
186#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
187#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
188#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
189#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
190#define CBAR_IRPTNDX_SHIFT 24
191#define CBAR_IRPTNDX_MASK 0xff
192
193#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
194#define CBA2R_RW64_32BIT (0 << 0)
195#define CBA2R_RW64_64BIT (1 << 0)
196
197/* Translation context bank */
198#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
Will Deaconc757e852014-07-30 11:33:25 +0100199#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +0100200
201#define ARM_SMMU_CB_SCTLR 0x0
202#define ARM_SMMU_CB_RESUME 0x8
203#define ARM_SMMU_CB_TTBCR2 0x10
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100204#define ARM_SMMU_CB_TTBR0 0x20
205#define ARM_SMMU_CB_TTBR1 0x28
Will Deacon45ae7cf2013-06-24 18:31:25 +0100206#define ARM_SMMU_CB_TTBCR 0x30
207#define ARM_SMMU_CB_S1_MAIR0 0x38
Will Deacon518f7132014-11-14 17:17:54 +0000208#define ARM_SMMU_CB_S1_MAIR1 0x3c
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000209#define ARM_SMMU_CB_PAR_LO 0x50
210#define ARM_SMMU_CB_PAR_HI 0x54
Will Deacon45ae7cf2013-06-24 18:31:25 +0100211#define ARM_SMMU_CB_FSR 0x58
212#define ARM_SMMU_CB_FAR_LO 0x60
213#define ARM_SMMU_CB_FAR_HI 0x64
214#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon518f7132014-11-14 17:17:54 +0000215#define ARM_SMMU_CB_S1_TLBIVA 0x600
Will Deacon1463fe42013-07-31 19:21:27 +0100216#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon518f7132014-11-14 17:17:54 +0000217#define ARM_SMMU_CB_S1_TLBIVAL 0x620
218#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
219#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
Robin Murphy661d9622015-05-27 17:09:34 +0100220#define ARM_SMMU_CB_ATS1PR 0x800
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000221#define ARM_SMMU_CB_ATSR 0x8f0
Will Deacon45ae7cf2013-06-24 18:31:25 +0100222
223#define SCTLR_S1_ASIDPNE (1 << 12)
224#define SCTLR_CFCFG (1 << 7)
225#define SCTLR_CFIE (1 << 6)
226#define SCTLR_CFRE (1 << 5)
227#define SCTLR_E (1 << 4)
228#define SCTLR_AFE (1 << 2)
229#define SCTLR_TRE (1 << 1)
230#define SCTLR_M (1 << 0)
231#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
232
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000233#define CB_PAR_F (1 << 0)
234
235#define ATSR_ACTIVE (1 << 0)
236
Will Deacon45ae7cf2013-06-24 18:31:25 +0100237#define RESUME_RETRY (0 << 0)
238#define RESUME_TERMINATE (1 << 0)
239
Will Deacon45ae7cf2013-06-24 18:31:25 +0100240#define TTBCR2_SEP_SHIFT 15
Will Deacon5dc56162015-05-08 17:44:22 +0100241#define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100242
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100243#define TTBRn_ASID_SHIFT 48
Will Deacon45ae7cf2013-06-24 18:31:25 +0100244
245#define FSR_MULTI (1 << 31)
246#define FSR_SS (1 << 30)
247#define FSR_UUT (1 << 8)
248#define FSR_ASF (1 << 7)
249#define FSR_TLBLKF (1 << 6)
250#define FSR_TLBMCF (1 << 5)
251#define FSR_EF (1 << 4)
252#define FSR_PF (1 << 3)
253#define FSR_AFF (1 << 2)
254#define FSR_TF (1 << 1)
255
Mitchel Humpherys29073202014-07-08 09:52:18 -0700256#define FSR_IGN (FSR_AFF | FSR_ASF | \
257 FSR_TLBMCF | FSR_TLBLKF)
258#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100259 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100260
261#define FSYNR0_WNR (1 << 4)
262
Will Deacon4cf740b2014-07-14 19:47:39 +0100263static int force_stage;
Will Deacone3ce0c92015-05-27 17:09:35 +0100264module_param_named(force_stage, force_stage, int, S_IRUGO);
Will Deacon4cf740b2014-07-14 19:47:39 +0100265MODULE_PARM_DESC(force_stage,
266 "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.");
267
Robin Murphy09360402014-08-28 17:51:59 +0100268enum arm_smmu_arch_version {
269 ARM_SMMU_V1 = 1,
270 ARM_SMMU_V2,
271};
272
Will Deacon45ae7cf2013-06-24 18:31:25 +0100273struct arm_smmu_smr {
274 u8 idx;
275 u16 mask;
276 u16 id;
277};
278
Will Deacona9a1b0b2014-05-01 18:05:08 +0100279struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100280 int num_streamids;
281 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100282 struct arm_smmu_smr *smrs;
283};
284
Will Deacona9a1b0b2014-05-01 18:05:08 +0100285struct arm_smmu_master {
286 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100287 struct rb_node node;
288 struct arm_smmu_master_cfg cfg;
289};
290
Will Deacon45ae7cf2013-06-24 18:31:25 +0100291struct arm_smmu_device {
292 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100293
294 void __iomem *base;
295 unsigned long size;
Will Deaconc757e852014-07-30 11:33:25 +0100296 unsigned long pgshift;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100297
298#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
299#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
300#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
301#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
302#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000303#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100304 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000305
306#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
307 u32 options;
Robin Murphy09360402014-08-28 17:51:59 +0100308 enum arm_smmu_arch_version version;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100309
310 u32 num_context_banks;
311 u32 num_s2_context_banks;
312 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
313 atomic_t irptndx;
314
315 u32 num_mapping_groups;
316 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
317
Will Deacon518f7132014-11-14 17:17:54 +0000318 unsigned long va_size;
319 unsigned long ipa_size;
320 unsigned long pa_size;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100321
322 u32 num_global_irqs;
323 u32 num_context_irqs;
324 unsigned int *irqs;
325
Will Deacon45ae7cf2013-06-24 18:31:25 +0100326 struct list_head list;
327 struct rb_root masters;
328};
329
330struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100331 u8 cbndx;
332 u8 irptndx;
333 u32 cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100334};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100335#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100336
Will Deaconecfadb62013-07-31 19:21:28 +0100337#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
338#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
339
Will Deaconc752ce42014-06-25 22:46:31 +0100340enum arm_smmu_domain_stage {
341 ARM_SMMU_DOMAIN_S1 = 0,
342 ARM_SMMU_DOMAIN_S2,
343 ARM_SMMU_DOMAIN_NESTED,
344};
345
Will Deacon45ae7cf2013-06-24 18:31:25 +0100346struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100347 struct arm_smmu_device *smmu;
Will Deacon518f7132014-11-14 17:17:54 +0000348 struct io_pgtable_ops *pgtbl_ops;
349 spinlock_t pgtbl_lock;
Will Deacon44680ee2014-06-25 11:29:12 +0100350 struct arm_smmu_cfg cfg;
Will Deaconc752ce42014-06-25 22:46:31 +0100351 enum arm_smmu_domain_stage stage;
Will Deacon518f7132014-11-14 17:17:54 +0000352 struct mutex init_mutex; /* Protects smmu pointer */
Joerg Roedel1d672632015-03-26 13:43:10 +0100353 struct iommu_domain domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100354};
355
Will Deacon518f7132014-11-14 17:17:54 +0000356static struct iommu_ops arm_smmu_ops;
357
Will Deacon45ae7cf2013-06-24 18:31:25 +0100358static DEFINE_SPINLOCK(arm_smmu_devices_lock);
359static LIST_HEAD(arm_smmu_devices);
360
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000361struct arm_smmu_option_prop {
362 u32 opt;
363 const char *prop;
364};
365
Mitchel Humpherys29073202014-07-08 09:52:18 -0700366static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000367 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
368 { 0, NULL},
369};
370
Joerg Roedel1d672632015-03-26 13:43:10 +0100371static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
372{
373 return container_of(dom, struct arm_smmu_domain, domain);
374}
375
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000376static void parse_driver_options(struct arm_smmu_device *smmu)
377{
378 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700379
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000380 do {
381 if (of_property_read_bool(smmu->dev->of_node,
382 arm_smmu_options[i].prop)) {
383 smmu->options |= arm_smmu_options[i].opt;
384 dev_notice(smmu->dev, "option %s\n",
385 arm_smmu_options[i].prop);
386 }
387 } while (arm_smmu_options[++i].opt);
388}
389
Will Deacon8f68f8e2014-07-15 11:27:08 +0100390static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100391{
392 if (dev_is_pci(dev)) {
393 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700394
Will Deacona9a1b0b2014-05-01 18:05:08 +0100395 while (!pci_is_root_bus(bus))
396 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100397 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100398 }
399
Will Deacon8f68f8e2014-07-15 11:27:08 +0100400 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100401}
402
Will Deacon45ae7cf2013-06-24 18:31:25 +0100403static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
404 struct device_node *dev_node)
405{
406 struct rb_node *node = smmu->masters.rb_node;
407
408 while (node) {
409 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700410
Will Deacon45ae7cf2013-06-24 18:31:25 +0100411 master = container_of(node, struct arm_smmu_master, node);
412
413 if (dev_node < master->of_node)
414 node = node->rb_left;
415 else if (dev_node > master->of_node)
416 node = node->rb_right;
417 else
418 return master;
419 }
420
421 return NULL;
422}
423
Will Deacona9a1b0b2014-05-01 18:05:08 +0100424static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100425find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100426{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100427 struct arm_smmu_master_cfg *cfg = NULL;
428 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100429
Will Deacon8f68f8e2014-07-15 11:27:08 +0100430 if (group) {
431 cfg = iommu_group_get_iommudata(group);
432 iommu_group_put(group);
433 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100434
Will Deacon8f68f8e2014-07-15 11:27:08 +0100435 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100436}
437
Will Deacon45ae7cf2013-06-24 18:31:25 +0100438static int insert_smmu_master(struct arm_smmu_device *smmu,
439 struct arm_smmu_master *master)
440{
441 struct rb_node **new, *parent;
442
443 new = &smmu->masters.rb_node;
444 parent = NULL;
445 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700446 struct arm_smmu_master *this
447 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100448
449 parent = *new;
450 if (master->of_node < this->of_node)
451 new = &((*new)->rb_left);
452 else if (master->of_node > this->of_node)
453 new = &((*new)->rb_right);
454 else
455 return -EEXIST;
456 }
457
458 rb_link_node(&master->node, parent, new);
459 rb_insert_color(&master->node, &smmu->masters);
460 return 0;
461}
462
463static int register_smmu_master(struct arm_smmu_device *smmu,
464 struct device *dev,
465 struct of_phandle_args *masterspec)
466{
467 int i;
468 struct arm_smmu_master *master;
469
470 master = find_smmu_master(smmu, masterspec->np);
471 if (master) {
472 dev_err(dev,
473 "rejecting multiple registrations for master device %s\n",
474 masterspec->np->name);
475 return -EBUSY;
476 }
477
478 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
479 dev_err(dev,
480 "reached maximum number (%d) of stream IDs for master device %s\n",
481 MAX_MASTER_STREAMIDS, masterspec->np->name);
482 return -ENOSPC;
483 }
484
485 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
486 if (!master)
487 return -ENOMEM;
488
Will Deacona9a1b0b2014-05-01 18:05:08 +0100489 master->of_node = masterspec->np;
490 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100491
Olav Haugan3c8766d2014-08-22 17:12:32 -0700492 for (i = 0; i < master->cfg.num_streamids; ++i) {
493 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100494
Olav Haugan3c8766d2014-08-22 17:12:32 -0700495 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
496 (streamid >= smmu->num_mapping_groups)) {
497 dev_err(dev,
498 "stream ID for master device %s greater than maximum allowed (%d)\n",
499 masterspec->np->name, smmu->num_mapping_groups);
500 return -ERANGE;
501 }
502 master->cfg.streamids[i] = streamid;
503 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100504 return insert_smmu_master(smmu, master);
505}
506
Will Deacon44680ee2014-06-25 11:29:12 +0100507static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100508{
Will Deacon44680ee2014-06-25 11:29:12 +0100509 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100510 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100511 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100512
513 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100514 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100515 master = find_smmu_master(smmu, dev_node);
516 if (master)
517 break;
518 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100519 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100520
Will Deacona9a1b0b2014-05-01 18:05:08 +0100521 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100522}
523
524static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
525{
526 int idx;
527
528 do {
529 idx = find_next_zero_bit(map, end, start);
530 if (idx == end)
531 return -ENOSPC;
532 } while (test_and_set_bit(idx, map));
533
534 return idx;
535}
536
537static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
538{
539 clear_bit(idx, map);
540}
541
542/* Wait for any pending TLB invalidations to complete */
Will Deacon518f7132014-11-14 17:17:54 +0000543static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100544{
545 int count = 0;
546 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
547
548 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
549 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
550 & sTLBGSTATUS_GSACTIVE) {
551 cpu_relax();
552 if (++count == TLB_LOOP_TIMEOUT) {
553 dev_err_ratelimited(smmu->dev,
554 "TLB sync timed out -- SMMU may be deadlocked\n");
555 return;
556 }
557 udelay(1);
558 }
559}
560
Will Deacon518f7132014-11-14 17:17:54 +0000561static void arm_smmu_tlb_sync(void *cookie)
Will Deacon1463fe42013-07-31 19:21:27 +0100562{
Will Deacon518f7132014-11-14 17:17:54 +0000563 struct arm_smmu_domain *smmu_domain = cookie;
564 __arm_smmu_tlb_sync(smmu_domain->smmu);
565}
566
567static void arm_smmu_tlb_inv_context(void *cookie)
568{
569 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100570 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
571 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100572 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon518f7132014-11-14 17:17:54 +0000573 void __iomem *base;
Will Deacon1463fe42013-07-31 19:21:27 +0100574
575 if (stage1) {
576 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100577 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
578 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100579 } else {
580 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100581 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
582 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100583 }
584
Will Deacon518f7132014-11-14 17:17:54 +0000585 __arm_smmu_tlb_sync(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100586}
587
Will Deacon518f7132014-11-14 17:17:54 +0000588static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
Robin Murphy06c610e2015-12-07 18:18:53 +0000589 size_t granule, bool leaf, void *cookie)
Will Deacon518f7132014-11-14 17:17:54 +0000590{
591 struct arm_smmu_domain *smmu_domain = cookie;
592 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
593 struct arm_smmu_device *smmu = smmu_domain->smmu;
594 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
595 void __iomem *reg;
596
597 if (stage1) {
598 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
599 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
600
601 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
602 iova &= ~12UL;
603 iova |= ARM_SMMU_CB_ASID(cfg);
Robin Murphy75df1382015-12-07 18:18:52 +0000604 do {
605 writel_relaxed(iova, reg);
606 iova += granule;
607 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000608#ifdef CONFIG_64BIT
609 } else {
610 iova >>= 12;
611 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
Robin Murphy75df1382015-12-07 18:18:52 +0000612 do {
613 writeq_relaxed(iova, reg);
614 iova += granule >> 12;
615 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000616#endif
617 }
618#ifdef CONFIG_64BIT
619 } else if (smmu->version == ARM_SMMU_V2) {
620 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
621 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
622 ARM_SMMU_CB_S2_TLBIIPAS2;
Robin Murphy75df1382015-12-07 18:18:52 +0000623 iova >>= 12;
624 do {
625 writeq_relaxed(iova, reg);
626 iova += granule >> 12;
627 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000628#endif
629 } else {
630 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
631 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
632 }
633}
634
Will Deacon518f7132014-11-14 17:17:54 +0000635static 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,
Will Deacon518f7132014-11-14 17:17:54 +0000639};
640
Will Deacon45ae7cf2013-06-24 18:31:25 +0100641static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
642{
643 int flags, ret;
644 u32 fsr, far, fsynr, resume;
645 unsigned long iova;
646 struct iommu_domain *domain = dev;
Joerg Roedel1d672632015-03-26 13:43:10 +0100647 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100648 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
649 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100650 void __iomem *cb_base;
651
Will Deacon44680ee2014-06-25 11:29:12 +0100652 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100653 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
654
655 if (!(fsr & FSR_FAULT))
656 return IRQ_NONE;
657
658 if (fsr & FSR_IGN)
659 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100660 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100661 fsr);
662
663 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
664 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
665
666 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
667 iova = far;
668#ifdef CONFIG_64BIT
669 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
670 iova |= ((unsigned long)far << 32);
671#endif
672
673 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
674 ret = IRQ_HANDLED;
675 resume = RESUME_RETRY;
676 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100677 dev_err_ratelimited(smmu->dev,
678 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100679 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100680 ret = IRQ_NONE;
681 resume = RESUME_TERMINATE;
682 }
683
684 /* Clear the faulting FSR */
685 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
686
687 /* Retry or terminate any stalled transactions */
688 if (fsr & FSR_SS)
689 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
690
691 return ret;
692}
693
694static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
695{
696 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
697 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000698 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100699
700 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
701 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
702 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
703 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
704
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000705 if (!gfsr)
706 return IRQ_NONE;
707
Will Deacon45ae7cf2013-06-24 18:31:25 +0100708 dev_err_ratelimited(smmu->dev,
709 "Unexpected global fault, this could be serious\n");
710 dev_err_ratelimited(smmu->dev,
711 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
712 gfsr, gfsynr0, gfsynr1, gfsynr2);
713
714 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100715 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100716}
717
Will Deacon518f7132014-11-14 17:17:54 +0000718static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
719 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100720{
721 u32 reg;
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100722 u64 reg64;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100723 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 Deaconc88ae5d2015-10-13 17:53:24 +0100726 void __iomem *cb_base, *gr1_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100727
Will Deacon45ae7cf2013-06-24 18:31:25 +0100728 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100729 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
730 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100731
Will Deacon4a1c93c2015-03-04 12:21:03 +0000732 if (smmu->version > ARM_SMMU_V1) {
733 /*
734 * CBA2R.
735 * *Must* be initialised before CBAR thanks to VMID16
736 * architectural oversight affected some implementations.
737 */
738#ifdef CONFIG_64BIT
739 reg = CBA2R_RW64_64BIT;
740#else
741 reg = CBA2R_RW64_32BIT;
742#endif
743 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
744 }
745
Will Deacon45ae7cf2013-06-24 18:31:25 +0100746 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100747 reg = cfg->cbar;
Robin Murphy09360402014-08-28 17:51:59 +0100748 if (smmu->version == ARM_SMMU_V1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700749 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100750
Will Deacon57ca90f2014-02-06 14:59:05 +0000751 /*
752 * Use the weakest shareability/memory types, so they are
753 * overridden by the ttbcr/pte.
754 */
755 if (stage1) {
756 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
757 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
758 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100759 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000760 }
Will Deacon44680ee2014-06-25 11:29:12 +0100761 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100762
Will Deacon518f7132014-11-14 17:17:54 +0000763 /* TTBRs */
764 if (stage1) {
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100765 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100766
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100767 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT;
768 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0);
769
770 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
771 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT;
772 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR1);
Will Deacon518f7132014-11-14 17:17:54 +0000773 } else {
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100774 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
775 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0);
Will Deacon518f7132014-11-14 17:17:54 +0000776 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100777
Will Deacon518f7132014-11-14 17:17:54 +0000778 /* TTBCR */
779 if (stage1) {
780 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
781 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
782 if (smmu->version > ARM_SMMU_V1) {
783 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
Will Deacon5dc56162015-05-08 17:44:22 +0100784 reg |= TTBCR2_SEP_UPSTREAM;
Will Deacon518f7132014-11-14 17:17:54 +0000785 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100786 }
787 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000788 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
789 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100790 }
791
Will Deacon518f7132014-11-14 17:17:54 +0000792 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100793 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000794 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100795 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000796 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
797 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100798 }
799
Will Deacon45ae7cf2013-06-24 18:31:25 +0100800 /* SCTLR */
801 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
802 if (stage1)
803 reg |= SCTLR_S1_ASIDPNE;
804#ifdef __BIG_ENDIAN
805 reg |= SCTLR_E;
806#endif
Will Deacon25724842013-08-21 13:49:53 +0100807 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100808}
809
810static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100811 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100812{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100813 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000814 unsigned long ias, oas;
815 struct io_pgtable_ops *pgtbl_ops;
816 struct io_pgtable_cfg pgtbl_cfg;
817 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100818 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100819 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100820
Will Deacon518f7132014-11-14 17:17:54 +0000821 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100822 if (smmu_domain->smmu)
823 goto out_unlock;
824
Will Deaconc752ce42014-06-25 22:46:31 +0100825 /*
826 * Mapping the requested stage onto what we support is surprisingly
827 * complicated, mainly because the spec allows S1+S2 SMMUs without
828 * support for nested translation. That means we end up with the
829 * following table:
830 *
831 * Requested Supported Actual
832 * S1 N S1
833 * S1 S1+S2 S1
834 * S1 S2 S2
835 * S1 S1 S1
836 * N N N
837 * N S1+S2 S2
838 * N S2 S2
839 * N S1 S1
840 *
841 * Note that you can't actually request stage-2 mappings.
842 */
843 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
844 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
845 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
846 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
847
848 switch (smmu_domain->stage) {
849 case ARM_SMMU_DOMAIN_S1:
850 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
851 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000852 ias = smmu->va_size;
853 oas = smmu->ipa_size;
854 if (IS_ENABLED(CONFIG_64BIT))
855 fmt = ARM_64_LPAE_S1;
856 else
857 fmt = ARM_32_LPAE_S1;
Will Deaconc752ce42014-06-25 22:46:31 +0100858 break;
859 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100860 /*
861 * We will likely want to change this if/when KVM gets
862 * involved.
863 */
Will Deaconc752ce42014-06-25 22:46:31 +0100864 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100865 cfg->cbar = CBAR_TYPE_S2_TRANS;
866 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000867 ias = smmu->ipa_size;
868 oas = smmu->pa_size;
869 if (IS_ENABLED(CONFIG_64BIT))
870 fmt = ARM_64_LPAE_S2;
871 else
872 fmt = ARM_32_LPAE_S2;
Will Deaconc752ce42014-06-25 22:46:31 +0100873 break;
874 default:
875 ret = -EINVAL;
876 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100877 }
878
879 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
880 smmu->num_context_banks);
881 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100882 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100883
Will Deacon44680ee2014-06-25 11:29:12 +0100884 cfg->cbndx = ret;
Robin Murphy09360402014-08-28 17:51:59 +0100885 if (smmu->version == ARM_SMMU_V1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100886 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
887 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100888 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100889 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100890 }
891
Will Deacon518f7132014-11-14 17:17:54 +0000892 pgtbl_cfg = (struct io_pgtable_cfg) {
893 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
894 .ias = ias,
895 .oas = oas,
896 .tlb = &arm_smmu_gather_ops,
Robin Murphy2df7a252015-07-29 19:46:06 +0100897 .iommu_dev = smmu->dev,
Will Deacon518f7132014-11-14 17:17:54 +0000898 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100899
Will Deacon518f7132014-11-14 17:17:54 +0000900 smmu_domain->smmu = smmu;
901 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
902 if (!pgtbl_ops) {
903 ret = -ENOMEM;
904 goto out_clear_smmu;
905 }
906
907 /* Update our support page sizes to reflect the page table format */
908 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
909
910 /* Initialise the context bank with our page table cfg */
911 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
912
913 /*
914 * Request context fault interrupt. Do this last to avoid the
915 * handler seeing a half-initialised domain state.
916 */
Will Deacon44680ee2014-06-25 11:29:12 +0100917 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100918 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
919 "arm-smmu-context-fault", domain);
920 if (IS_ERR_VALUE(ret)) {
921 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100922 cfg->irptndx, irq);
923 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100924 }
925
Will Deacon518f7132014-11-14 17:17:54 +0000926 mutex_unlock(&smmu_domain->init_mutex);
927
928 /* Publish page table ops for map/unmap */
929 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100930 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100931
Will Deacon518f7132014-11-14 17:17:54 +0000932out_clear_smmu:
933 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100934out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000935 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100936 return ret;
937}
938
939static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
940{
Joerg Roedel1d672632015-03-26 13:43:10 +0100941 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100942 struct arm_smmu_device *smmu = smmu_domain->smmu;
943 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100944 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100945 int irq;
946
947 if (!smmu)
948 return;
949
Will Deacon518f7132014-11-14 17:17:54 +0000950 /*
951 * Disable the context bank and free the page tables before freeing
952 * it.
953 */
Will Deacon44680ee2014-06-25 11:29:12 +0100954 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100955 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +0100956
Will Deacon44680ee2014-06-25 11:29:12 +0100957 if (cfg->irptndx != INVALID_IRPTNDX) {
958 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100959 free_irq(irq, domain);
960 }
961
Markus Elfring44830b02015-11-06 18:32:41 +0100962 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon44680ee2014-06-25 11:29:12 +0100963 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100964}
965
Joerg Roedel1d672632015-03-26 13:43:10 +0100966static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100967{
968 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100969
Robin Murphy9adb9592016-01-26 18:06:36 +0000970 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
Joerg Roedel1d672632015-03-26 13:43:10 +0100971 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100972 /*
973 * Allocate the domain and initialise some of its data structures.
974 * We can't really do anything meaningful until we've added a
975 * master.
976 */
977 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
978 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +0100979 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100980
Robin Murphy9adb9592016-01-26 18:06:36 +0000981 if (type == IOMMU_DOMAIN_DMA &&
982 iommu_get_dma_cookie(&smmu_domain->domain)) {
983 kfree(smmu_domain);
984 return NULL;
985 }
986
Will Deacon518f7132014-11-14 17:17:54 +0000987 mutex_init(&smmu_domain->init_mutex);
988 spin_lock_init(&smmu_domain->pgtbl_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +0100989
990 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100991}
992
Joerg Roedel1d672632015-03-26 13:43:10 +0100993static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100994{
Joerg Roedel1d672632015-03-26 13:43:10 +0100995 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +0100996
997 /*
998 * Free the domain resources. We assume that all devices have
999 * already been detached.
1000 */
Robin Murphy9adb9592016-01-26 18:06:36 +00001001 iommu_put_dma_cookie(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001002 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001003 kfree(smmu_domain);
1004}
1005
1006static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001007 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001008{
1009 int i;
1010 struct arm_smmu_smr *smrs;
1011 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1012
1013 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1014 return 0;
1015
Will Deacona9a1b0b2014-05-01 18:05:08 +01001016 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001017 return -EEXIST;
1018
Mitchel Humpherys29073202014-07-08 09:52:18 -07001019 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001020 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001021 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1022 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001023 return -ENOMEM;
1024 }
1025
Will Deacon44680ee2014-06-25 11:29:12 +01001026 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001027 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001028 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1029 smmu->num_mapping_groups);
1030 if (IS_ERR_VALUE(idx)) {
1031 dev_err(smmu->dev, "failed to allocate free SMR\n");
1032 goto err_free_smrs;
1033 }
1034
1035 smrs[i] = (struct arm_smmu_smr) {
1036 .idx = idx,
1037 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001038 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001039 };
1040 }
1041
1042 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001043 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001044 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1045 smrs[i].mask << SMR_MASK_SHIFT;
1046 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1047 }
1048
Will Deacona9a1b0b2014-05-01 18:05:08 +01001049 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001050 return 0;
1051
1052err_free_smrs:
1053 while (--i >= 0)
1054 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1055 kfree(smrs);
1056 return -ENOSPC;
1057}
1058
1059static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001060 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001061{
1062 int i;
1063 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001064 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001065
Will Deacon43b412b2014-07-15 11:22:24 +01001066 if (!smrs)
1067 return;
1068
Will Deacon45ae7cf2013-06-24 18:31:25 +01001069 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001070 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001071 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001072
Will Deacon45ae7cf2013-06-24 18:31:25 +01001073 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1074 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1075 }
1076
Will Deacona9a1b0b2014-05-01 18:05:08 +01001077 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001078 kfree(smrs);
1079}
1080
Will Deacon45ae7cf2013-06-24 18:31:25 +01001081static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001082 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001083{
1084 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001085 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001086 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1087
Will Deacon8f68f8e2014-07-15 11:27:08 +01001088 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001089 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001090 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001091 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001092
Will Deacona9a1b0b2014-05-01 18:05:08 +01001093 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001094 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001095
Will Deacona9a1b0b2014-05-01 18:05:08 +01001096 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Robin Murphyd3461802016-01-26 18:06:34 +00001097 s2cr = S2CR_TYPE_TRANS | S2CR_PRIVCFG_UNPRIV |
Will Deacon44680ee2014-06-25 11:29:12 +01001098 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001099 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1100 }
1101
1102 return 0;
1103}
1104
1105static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001106 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001107{
Will Deacon43b412b2014-07-15 11:22:24 +01001108 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001109 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001110 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001111
Will Deacon8f68f8e2014-07-15 11:27:08 +01001112 /* An IOMMU group is torn down by the first device to be removed */
1113 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1114 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001115
1116 /*
1117 * We *must* clear the S2CR first, because freeing the SMR means
1118 * that it can be re-allocated immediately.
1119 */
Will Deacon43b412b2014-07-15 11:22:24 +01001120 for (i = 0; i < cfg->num_streamids; ++i) {
1121 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1122
1123 writel_relaxed(S2CR_TYPE_BYPASS,
1124 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1125 }
1126
Will Deacona9a1b0b2014-05-01 18:05:08 +01001127 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001128}
1129
1130static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1131{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001132 int ret;
Joerg Roedel1d672632015-03-26 13:43:10 +01001133 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001134 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001135 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001136
Will Deacon8f68f8e2014-07-15 11:27:08 +01001137 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001138 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001139 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1140 return -ENXIO;
1141 }
1142
Will Deacon844e35b2014-07-17 11:23:51 +01001143 if (dev->archdata.iommu) {
1144 dev_err(dev, "already attached to IOMMU domain\n");
1145 return -EEXIST;
1146 }
1147
Will Deacon518f7132014-11-14 17:17:54 +00001148 /* Ensure that the domain is finalised */
1149 ret = arm_smmu_init_domain_context(domain, smmu);
1150 if (IS_ERR_VALUE(ret))
1151 return ret;
1152
Will Deacon45ae7cf2013-06-24 18:31:25 +01001153 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001154 * Sanity check the domain. We don't support domains across
1155 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001156 */
Will Deacon518f7132014-11-14 17:17:54 +00001157 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001158 dev_err(dev,
1159 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001160 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1161 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001162 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001163
1164 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001165 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001166 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001167 return -ENODEV;
1168
Will Deacon844e35b2014-07-17 11:23:51 +01001169 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1170 if (!ret)
1171 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001172 return ret;
1173}
1174
1175static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1176{
Joerg Roedel1d672632015-03-26 13:43:10 +01001177 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001178 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001179
Will Deacon8f68f8e2014-07-15 11:27:08 +01001180 cfg = find_smmu_master_cfg(dev);
Will Deacon844e35b2014-07-17 11:23:51 +01001181 if (!cfg)
1182 return;
1183
1184 dev->archdata.iommu = NULL;
1185 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001186}
1187
Will Deacon45ae7cf2013-06-24 18:31:25 +01001188static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001189 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001190{
Will Deacon518f7132014-11-14 17:17:54 +00001191 int ret;
1192 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001193 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001194 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001195
Will Deacon518f7132014-11-14 17:17:54 +00001196 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001197 return -ENODEV;
1198
Will Deacon518f7132014-11-14 17:17:54 +00001199 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1200 ret = ops->map(ops, iova, paddr, size, prot);
1201 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1202 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001203}
1204
1205static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1206 size_t size)
1207{
Will Deacon518f7132014-11-14 17:17:54 +00001208 size_t ret;
1209 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001210 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
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)
1214 return 0;
1215
1216 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1217 ret = ops->unmap(ops, iova, size);
1218 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1219 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001220}
1221
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001222static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1223 dma_addr_t iova)
1224{
Joerg Roedel1d672632015-03-26 13:43:10 +01001225 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001226 struct arm_smmu_device *smmu = smmu_domain->smmu;
1227 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1228 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1229 struct device *dev = smmu->dev;
1230 void __iomem *cb_base;
1231 u32 tmp;
1232 u64 phys;
Robin Murphy661d9622015-05-27 17:09:34 +01001233 unsigned long va;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001234
1235 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1236
Robin Murphy661d9622015-05-27 17:09:34 +01001237 /* ATS1 registers can only be written atomically */
1238 va = iova & ~0xfffUL;
Robin Murphy661d9622015-05-27 17:09:34 +01001239 if (smmu->version == ARM_SMMU_V2)
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +01001240 smmu_writeq(va, cb_base + ARM_SMMU_CB_ATS1PR);
Robin Murphy661d9622015-05-27 17:09:34 +01001241 else
Robin Murphy661d9622015-05-27 17:09:34 +01001242 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001243
1244 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1245 !(tmp & ATSR_ACTIVE), 5, 50)) {
1246 dev_err(dev,
Fabio Estevam077124c2015-08-18 17:12:24 +01001247 "iova to phys timed out on %pad. Falling back to software table walk.\n",
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001248 &iova);
1249 return ops->iova_to_phys(ops, iova);
1250 }
1251
1252 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1253 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1254
1255 if (phys & CB_PAR_F) {
1256 dev_err(dev, "translation fault!\n");
1257 dev_err(dev, "PAR = 0x%llx\n", phys);
1258 return 0;
1259 }
1260
1261 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1262}
1263
Will Deacon45ae7cf2013-06-24 18:31:25 +01001264static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001265 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001266{
Will Deacon518f7132014-11-14 17:17:54 +00001267 phys_addr_t ret;
1268 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001269 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001270 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001271
Will Deacon518f7132014-11-14 17:17:54 +00001272 if (!ops)
Will Deacona44a9792013-11-07 18:47:50 +00001273 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001274
Will Deacon518f7132014-11-14 17:17:54 +00001275 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001276 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1277 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001278 ret = arm_smmu_iova_to_phys_hard(domain, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001279 } else {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001280 ret = ops->iova_to_phys(ops, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001281 }
1282
Will Deacon518f7132014-11-14 17:17:54 +00001283 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001284
Will Deacon518f7132014-11-14 17:17:54 +00001285 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001286}
1287
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001288static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001289{
Will Deacond0948942014-06-24 17:30:10 +01001290 switch (cap) {
1291 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001292 /*
1293 * Return true here as the SMMU can always send out coherent
1294 * requests.
1295 */
1296 return true;
Will Deacond0948942014-06-24 17:30:10 +01001297 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001298 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001299 case IOMMU_CAP_NOEXEC:
1300 return true;
Will Deacond0948942014-06-24 17:30:10 +01001301 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001302 return false;
Will Deacond0948942014-06-24 17:30:10 +01001303 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001304}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001305
Will Deacona9a1b0b2014-05-01 18:05:08 +01001306static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1307{
1308 *((u16 *)data) = alias;
1309 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001310}
1311
Will Deacon8f68f8e2014-07-15 11:27:08 +01001312static void __arm_smmu_release_pci_iommudata(void *data)
1313{
1314 kfree(data);
1315}
1316
Joerg Roedelaf659932015-10-21 23:51:41 +02001317static int arm_smmu_init_pci_device(struct pci_dev *pdev,
1318 struct iommu_group *group)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001319{
Will Deacon03edb222015-01-19 14:27:33 +00001320 struct arm_smmu_master_cfg *cfg;
Joerg Roedelaf659932015-10-21 23:51:41 +02001321 u16 sid;
1322 int i;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001323
Will Deacon03edb222015-01-19 14:27:33 +00001324 cfg = iommu_group_get_iommudata(group);
1325 if (!cfg) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001326 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
Joerg Roedelaf659932015-10-21 23:51:41 +02001327 if (!cfg)
1328 return -ENOMEM;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001329
Will Deacon03edb222015-01-19 14:27:33 +00001330 iommu_group_set_iommudata(group, cfg,
1331 __arm_smmu_release_pci_iommudata);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001332 }
1333
Joerg Roedelaf659932015-10-21 23:51:41 +02001334 if (cfg->num_streamids >= MAX_MASTER_STREAMIDS)
1335 return -ENOSPC;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001336
Will Deacon03edb222015-01-19 14:27:33 +00001337 /*
1338 * Assume Stream ID == Requester ID for now.
1339 * We need a way to describe the ID mappings in FDT.
1340 */
1341 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1342 for (i = 0; i < cfg->num_streamids; ++i)
1343 if (cfg->streamids[i] == sid)
1344 break;
1345
1346 /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
1347 if (i == cfg->num_streamids)
1348 cfg->streamids[cfg->num_streamids++] = sid;
1349
1350 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001351}
1352
Joerg Roedelaf659932015-10-21 23:51:41 +02001353static int arm_smmu_init_platform_device(struct device *dev,
1354 struct iommu_group *group)
Will Deacon03edb222015-01-19 14:27:33 +00001355{
Will Deacon03edb222015-01-19 14:27:33 +00001356 struct arm_smmu_device *smmu = find_smmu_for_device(dev);
Joerg Roedelaf659932015-10-21 23:51:41 +02001357 struct arm_smmu_master *master;
Will Deacon03edb222015-01-19 14:27:33 +00001358
1359 if (!smmu)
1360 return -ENODEV;
1361
1362 master = find_smmu_master(smmu, dev->of_node);
1363 if (!master)
1364 return -ENODEV;
1365
Will Deacon03edb222015-01-19 14:27:33 +00001366 iommu_group_set_iommudata(group, &master->cfg, NULL);
Joerg Roedelaf659932015-10-21 23:51:41 +02001367
1368 return 0;
Will Deacon03edb222015-01-19 14:27:33 +00001369}
1370
1371static int arm_smmu_add_device(struct device *dev)
1372{
Joerg Roedelaf659932015-10-21 23:51:41 +02001373 struct iommu_group *group;
Will Deacon03edb222015-01-19 14:27:33 +00001374
Joerg Roedelaf659932015-10-21 23:51:41 +02001375 group = iommu_group_get_for_dev(dev);
1376 if (IS_ERR(group))
1377 return PTR_ERR(group);
1378
Peng Fan9a4a9d82015-11-20 16:56:18 +08001379 iommu_group_put(group);
Joerg Roedelaf659932015-10-21 23:51:41 +02001380 return 0;
Will Deacon03edb222015-01-19 14:27:33 +00001381}
1382
Will Deacon45ae7cf2013-06-24 18:31:25 +01001383static void arm_smmu_remove_device(struct device *dev)
1384{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001385 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001386}
1387
Joerg Roedelaf659932015-10-21 23:51:41 +02001388static struct iommu_group *arm_smmu_device_group(struct device *dev)
1389{
1390 struct iommu_group *group;
1391 int ret;
1392
1393 if (dev_is_pci(dev))
1394 group = pci_device_group(dev);
1395 else
1396 group = generic_device_group(dev);
1397
1398 if (IS_ERR(group))
1399 return group;
1400
1401 if (dev_is_pci(dev))
1402 ret = arm_smmu_init_pci_device(to_pci_dev(dev), group);
1403 else
1404 ret = arm_smmu_init_platform_device(dev, group);
1405
1406 if (ret) {
1407 iommu_group_put(group);
1408 group = ERR_PTR(ret);
1409 }
1410
1411 return group;
1412}
1413
Will Deaconc752ce42014-06-25 22:46:31 +01001414static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1415 enum iommu_attr attr, void *data)
1416{
Joerg Roedel1d672632015-03-26 13:43:10 +01001417 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001418
1419 switch (attr) {
1420 case DOMAIN_ATTR_NESTING:
1421 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1422 return 0;
1423 default:
1424 return -ENODEV;
1425 }
1426}
1427
1428static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1429 enum iommu_attr attr, void *data)
1430{
Will Deacon518f7132014-11-14 17:17:54 +00001431 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001432 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001433
Will Deacon518f7132014-11-14 17:17:54 +00001434 mutex_lock(&smmu_domain->init_mutex);
1435
Will Deaconc752ce42014-06-25 22:46:31 +01001436 switch (attr) {
1437 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001438 if (smmu_domain->smmu) {
1439 ret = -EPERM;
1440 goto out_unlock;
1441 }
1442
Will Deaconc752ce42014-06-25 22:46:31 +01001443 if (*(int *)data)
1444 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1445 else
1446 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1447
Will Deacon518f7132014-11-14 17:17:54 +00001448 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001449 default:
Will Deacon518f7132014-11-14 17:17:54 +00001450 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001451 }
Will Deacon518f7132014-11-14 17:17:54 +00001452
1453out_unlock:
1454 mutex_unlock(&smmu_domain->init_mutex);
1455 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001456}
1457
Will Deacon518f7132014-11-14 17:17:54 +00001458static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001459 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001460 .domain_alloc = arm_smmu_domain_alloc,
1461 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001462 .attach_dev = arm_smmu_attach_dev,
1463 .detach_dev = arm_smmu_detach_dev,
1464 .map = arm_smmu_map,
1465 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001466 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001467 .iova_to_phys = arm_smmu_iova_to_phys,
1468 .add_device = arm_smmu_add_device,
1469 .remove_device = arm_smmu_remove_device,
Joerg Roedelaf659932015-10-21 23:51:41 +02001470 .device_group = arm_smmu_device_group,
Will Deaconc752ce42014-06-25 22:46:31 +01001471 .domain_get_attr = arm_smmu_domain_get_attr,
1472 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001473 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001474};
1475
1476static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1477{
1478 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001479 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001480 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001481 u32 reg;
1482
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001483 /* clear global FSR */
1484 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1485 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001486
1487 /* Mark all SMRn as invalid and all S2CRn as bypass */
1488 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001489 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001490 writel_relaxed(S2CR_TYPE_BYPASS,
1491 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001492 }
1493
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001494 /* Make sure all context banks are disabled and clear CB_FSR */
1495 for (i = 0; i < smmu->num_context_banks; ++i) {
1496 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1497 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1498 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1499 }
Will Deacon1463fe42013-07-31 19:21:27 +01001500
Will Deacon45ae7cf2013-06-24 18:31:25 +01001501 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001502 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1503 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1504
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001505 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001506
Will Deacon45ae7cf2013-06-24 18:31:25 +01001507 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001508 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001509
1510 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001511 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001512
1513 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001514 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001515
1516 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001517 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001518
1519 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001520 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001521
1522 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001523 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001524 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001525}
1526
1527static int arm_smmu_id_size_to_bits(int size)
1528{
1529 switch (size) {
1530 case 0:
1531 return 32;
1532 case 1:
1533 return 36;
1534 case 2:
1535 return 40;
1536 case 3:
1537 return 42;
1538 case 4:
1539 return 44;
1540 case 5:
1541 default:
1542 return 48;
1543 }
1544}
1545
1546static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1547{
1548 unsigned long size;
1549 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1550 u32 id;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001551 bool cttw_dt, cttw_reg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001552
1553 dev_notice(smmu->dev, "probing hardware configuration...\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001554 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1555
1556 /* ID0 */
1557 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001558
1559 /* Restrict available stages based on module parameter */
1560 if (force_stage == 1)
1561 id &= ~(ID0_S2TS | ID0_NTS);
1562 else if (force_stage == 2)
1563 id &= ~(ID0_S1TS | ID0_NTS);
1564
Will Deacon45ae7cf2013-06-24 18:31:25 +01001565 if (id & ID0_S1TS) {
1566 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1567 dev_notice(smmu->dev, "\tstage 1 translation\n");
1568 }
1569
1570 if (id & ID0_S2TS) {
1571 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1572 dev_notice(smmu->dev, "\tstage 2 translation\n");
1573 }
1574
1575 if (id & ID0_NTS) {
1576 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1577 dev_notice(smmu->dev, "\tnested translation\n");
1578 }
1579
1580 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001581 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001582 dev_err(smmu->dev, "\tno translation support!\n");
1583 return -ENODEV;
1584 }
1585
Will Deacond38f0ff2015-06-29 17:47:42 +01001586 if ((id & ID0_S1TS) && ((smmu->version == 1) || !(id & ID0_ATOSNS))) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001587 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1588 dev_notice(smmu->dev, "\taddress translation ops\n");
1589 }
1590
Robin Murphybae2c2d2015-07-29 19:46:05 +01001591 /*
1592 * In order for DMA API calls to work properly, we must defer to what
1593 * the DT says about coherency, regardless of what the hardware claims.
1594 * Fortunately, this also opens up a workaround for systems where the
1595 * ID register value has ended up configured incorrectly.
1596 */
1597 cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
1598 cttw_reg = !!(id & ID0_CTTW);
1599 if (cttw_dt)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001600 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001601 if (cttw_dt || cttw_reg)
1602 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1603 cttw_dt ? "" : "non-");
1604 if (cttw_dt != cttw_reg)
1605 dev_notice(smmu->dev,
1606 "\t(IDR0.CTTW overridden by dma-coherent property)\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001607
1608 if (id & ID0_SMS) {
1609 u32 smr, sid, mask;
1610
1611 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1612 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1613 ID0_NUMSMRG_MASK;
1614 if (smmu->num_mapping_groups == 0) {
1615 dev_err(smmu->dev,
1616 "stream-matching supported, but no SMRs present!\n");
1617 return -ENODEV;
1618 }
1619
1620 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1621 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1622 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1623 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1624
1625 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1626 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1627 if ((mask & sid) != sid) {
1628 dev_err(smmu->dev,
1629 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1630 mask, sid);
1631 return -ENODEV;
1632 }
1633
1634 dev_notice(smmu->dev,
1635 "\tstream matching with %u register groups, mask 0x%x",
1636 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001637 } else {
1638 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1639 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001640 }
1641
1642 /* ID1 */
1643 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001644 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001645
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001646 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001647 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001648 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001649 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001650 dev_warn(smmu->dev,
1651 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1652 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001653
Will Deacon518f7132014-11-14 17:17:54 +00001654 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001655 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1656 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1657 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1658 return -ENODEV;
1659 }
1660 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1661 smmu->num_context_banks, smmu->num_s2_context_banks);
1662
1663 /* ID2 */
1664 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1665 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001666 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001667
Will Deacon518f7132014-11-14 17:17:54 +00001668 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001669 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001670 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001671
Robin Murphyf1d84542015-03-04 16:41:05 +00001672 /*
1673 * What the page table walker can address actually depends on which
1674 * descriptor format is in use, but since a) we don't know that yet,
1675 * and b) it can vary per context bank, this will have to do...
1676 */
1677 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1678 dev_warn(smmu->dev,
1679 "failed to set DMA mask for table walker\n");
1680
Robin Murphy09360402014-08-28 17:51:59 +01001681 if (smmu->version == ARM_SMMU_V1) {
Will Deacon518f7132014-11-14 17:17:54 +00001682 smmu->va_size = smmu->ipa_size;
1683 size = SZ_4K | SZ_2M | SZ_1G;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001684 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001685 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001686 smmu->va_size = arm_smmu_id_size_to_bits(size);
1687#ifndef CONFIG_64BIT
1688 smmu->va_size = min(32UL, smmu->va_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001689#endif
Will Deacon518f7132014-11-14 17:17:54 +00001690 size = 0;
1691 if (id & ID2_PTFS_4K)
1692 size |= SZ_4K | SZ_2M | SZ_1G;
1693 if (id & ID2_PTFS_16K)
1694 size |= SZ_16K | SZ_32M;
1695 if (id & ID2_PTFS_64K)
1696 size |= SZ_64K | SZ_512M;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001697 }
1698
Will Deacon518f7132014-11-14 17:17:54 +00001699 arm_smmu_ops.pgsize_bitmap &= size;
1700 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1701
Will Deacon28d60072014-09-01 16:24:48 +01001702 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1703 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001704 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001705
1706 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1707 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001708 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001709
Will Deacon45ae7cf2013-06-24 18:31:25 +01001710 return 0;
1711}
1712
Joerg Roedel09b52692014-10-02 12:24:45 +02001713static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy09360402014-08-28 17:51:59 +01001714 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1715 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1716 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
Robin Murphyd3aba042014-08-28 17:52:00 +01001717 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
Robin Murphy09360402014-08-28 17:51:59 +01001718 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1719 { },
1720};
1721MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1722
Will Deacon45ae7cf2013-06-24 18:31:25 +01001723static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1724{
Robin Murphy09360402014-08-28 17:51:59 +01001725 const struct of_device_id *of_id;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001726 struct resource *res;
1727 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001728 struct device *dev = &pdev->dev;
1729 struct rb_node *node;
1730 struct of_phandle_args masterspec;
1731 int num_irqs, i, err;
1732
1733 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1734 if (!smmu) {
1735 dev_err(dev, "failed to allocate arm_smmu_device\n");
1736 return -ENOMEM;
1737 }
1738 smmu->dev = dev;
1739
Robin Murphy09360402014-08-28 17:51:59 +01001740 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1741 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1742
Will Deacon45ae7cf2013-06-24 18:31:25 +01001743 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001744 smmu->base = devm_ioremap_resource(dev, res);
1745 if (IS_ERR(smmu->base))
1746 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001747 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001748
1749 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1750 &smmu->num_global_irqs)) {
1751 dev_err(dev, "missing #global-interrupts property\n");
1752 return -ENODEV;
1753 }
1754
1755 num_irqs = 0;
1756 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1757 num_irqs++;
1758 if (num_irqs > smmu->num_global_irqs)
1759 smmu->num_context_irqs++;
1760 }
1761
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001762 if (!smmu->num_context_irqs) {
1763 dev_err(dev, "found %d interrupts but expected at least %d\n",
1764 num_irqs, smmu->num_global_irqs + 1);
1765 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001766 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001767
1768 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1769 GFP_KERNEL);
1770 if (!smmu->irqs) {
1771 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1772 return -ENOMEM;
1773 }
1774
1775 for (i = 0; i < num_irqs; ++i) {
1776 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001777
Will Deacon45ae7cf2013-06-24 18:31:25 +01001778 if (irq < 0) {
1779 dev_err(dev, "failed to get irq index %d\n", i);
1780 return -ENODEV;
1781 }
1782 smmu->irqs[i] = irq;
1783 }
1784
Olav Haugan3c8766d2014-08-22 17:12:32 -07001785 err = arm_smmu_device_cfg_probe(smmu);
1786 if (err)
1787 return err;
1788
Will Deacon45ae7cf2013-06-24 18:31:25 +01001789 i = 0;
1790 smmu->masters = RB_ROOT;
1791 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1792 "#stream-id-cells", i,
1793 &masterspec)) {
1794 err = register_smmu_master(smmu, dev, &masterspec);
1795 if (err) {
1796 dev_err(dev, "failed to add master %s\n",
1797 masterspec.np->name);
1798 goto out_put_masters;
1799 }
1800
1801 i++;
1802 }
1803 dev_notice(dev, "registered %d master devices\n", i);
1804
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001805 parse_driver_options(smmu);
1806
Robin Murphy09360402014-08-28 17:51:59 +01001807 if (smmu->version > ARM_SMMU_V1 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001808 smmu->num_context_banks != smmu->num_context_irqs) {
1809 dev_err(dev,
1810 "found only %d context interrupt(s) but %d required\n",
1811 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cd2013-11-15 09:42:30 +00001812 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001813 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001814 }
1815
Will Deacon45ae7cf2013-06-24 18:31:25 +01001816 for (i = 0; i < smmu->num_global_irqs; ++i) {
1817 err = request_irq(smmu->irqs[i],
1818 arm_smmu_global_fault,
1819 IRQF_SHARED,
1820 "arm-smmu global fault",
1821 smmu);
1822 if (err) {
1823 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1824 i, smmu->irqs[i]);
1825 goto out_free_irqs;
1826 }
1827 }
1828
1829 INIT_LIST_HEAD(&smmu->list);
1830 spin_lock(&arm_smmu_devices_lock);
1831 list_add(&smmu->list, &arm_smmu_devices);
1832 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001833
1834 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001835 return 0;
1836
1837out_free_irqs:
1838 while (i--)
1839 free_irq(smmu->irqs[i], smmu);
1840
Will Deacon45ae7cf2013-06-24 18:31:25 +01001841out_put_masters:
1842 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001843 struct arm_smmu_master *master
1844 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001845 of_node_put(master->of_node);
1846 }
1847
1848 return err;
1849}
1850
1851static int arm_smmu_device_remove(struct platform_device *pdev)
1852{
1853 int i;
1854 struct device *dev = &pdev->dev;
1855 struct arm_smmu_device *curr, *smmu = NULL;
1856 struct rb_node *node;
1857
1858 spin_lock(&arm_smmu_devices_lock);
1859 list_for_each_entry(curr, &arm_smmu_devices, list) {
1860 if (curr->dev == dev) {
1861 smmu = curr;
1862 list_del(&smmu->list);
1863 break;
1864 }
1865 }
1866 spin_unlock(&arm_smmu_devices_lock);
1867
1868 if (!smmu)
1869 return -ENODEV;
1870
Will Deacon45ae7cf2013-06-24 18:31:25 +01001871 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001872 struct arm_smmu_master *master
1873 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001874 of_node_put(master->of_node);
1875 }
1876
Will Deaconecfadb62013-07-31 19:21:28 +01001877 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001878 dev_err(dev, "removing device with active domains!\n");
1879
1880 for (i = 0; i < smmu->num_global_irqs; ++i)
1881 free_irq(smmu->irqs[i], smmu);
1882
1883 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001884 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001885 return 0;
1886}
1887
Will Deacon45ae7cf2013-06-24 18:31:25 +01001888static struct platform_driver arm_smmu_driver = {
1889 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001890 .name = "arm-smmu",
1891 .of_match_table = of_match_ptr(arm_smmu_of_match),
1892 },
1893 .probe = arm_smmu_device_dt_probe,
1894 .remove = arm_smmu_device_remove,
1895};
1896
1897static int __init arm_smmu_init(void)
1898{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001899 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001900 int ret;
1901
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001902 /*
1903 * Play nice with systems that don't have an ARM SMMU by checking that
1904 * an ARM SMMU exists in the system before proceeding with the driver
1905 * and IOMMU bus operation registration.
1906 */
1907 np = of_find_matching_node(NULL, arm_smmu_of_match);
1908 if (!np)
1909 return 0;
1910
1911 of_node_put(np);
1912
Will Deacon45ae7cf2013-06-24 18:31:25 +01001913 ret = platform_driver_register(&arm_smmu_driver);
1914 if (ret)
1915 return ret;
1916
1917 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001918 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001919 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1920
Will Deacond123cf82014-02-04 22:17:53 +00001921#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01001922 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001923 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00001924#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01001925
Will Deacona9a1b0b2014-05-01 18:05:08 +01001926#ifdef CONFIG_PCI
1927 if (!iommu_present(&pci_bus_type))
1928 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1929#endif
1930
Will Deacon45ae7cf2013-06-24 18:31:25 +01001931 return 0;
1932}
1933
1934static void __exit arm_smmu_exit(void)
1935{
1936 return platform_driver_unregister(&arm_smmu_driver);
1937}
1938
Andreas Herrmannb1950b22013-10-01 13:39:05 +01001939subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001940module_exit(arm_smmu_exit);
1941
1942MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1943MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1944MODULE_LICENSE("GPL v2");