blob: 2409e3bd3df21e973db1231360b7a50fba66efa1 [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;
Robin Murphy25a1c962016-02-10 14:25:33 +0000264module_param(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.");
Robin Murphy25a1c962016-02-10 14:25:33 +0000267static bool disable_bypass;
268module_param(disable_bypass, bool, S_IRUGO);
269MODULE_PARM_DESC(disable_bypass,
270 "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
Will Deacon4cf740b2014-07-14 19:47:39 +0100271
Robin Murphy09360402014-08-28 17:51:59 +0100272enum arm_smmu_arch_version {
273 ARM_SMMU_V1 = 1,
274 ARM_SMMU_V2,
275};
276
Will Deacon45ae7cf2013-06-24 18:31:25 +0100277struct arm_smmu_smr {
278 u8 idx;
279 u16 mask;
280 u16 id;
281};
282
Will Deacona9a1b0b2014-05-01 18:05:08 +0100283struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100284 int num_streamids;
285 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100286 struct arm_smmu_smr *smrs;
287};
288
Will Deacona9a1b0b2014-05-01 18:05:08 +0100289struct arm_smmu_master {
290 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100291 struct rb_node node;
292 struct arm_smmu_master_cfg cfg;
293};
294
Will Deacon45ae7cf2013-06-24 18:31:25 +0100295struct arm_smmu_device {
296 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100297
298 void __iomem *base;
299 unsigned long size;
Will Deaconc757e852014-07-30 11:33:25 +0100300 unsigned long pgshift;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100301
302#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
303#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
304#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
305#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
306#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000307#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100308 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000309
310#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
311 u32 options;
Robin Murphy09360402014-08-28 17:51:59 +0100312 enum arm_smmu_arch_version version;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100313
314 u32 num_context_banks;
315 u32 num_s2_context_banks;
316 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
317 atomic_t irptndx;
318
319 u32 num_mapping_groups;
320 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
321
Will Deacon518f7132014-11-14 17:17:54 +0000322 unsigned long va_size;
323 unsigned long ipa_size;
324 unsigned long pa_size;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100325
326 u32 num_global_irqs;
327 u32 num_context_irqs;
328 unsigned int *irqs;
329
Will Deacon45ae7cf2013-06-24 18:31:25 +0100330 struct list_head list;
331 struct rb_root masters;
332};
333
334struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100335 u8 cbndx;
336 u8 irptndx;
337 u32 cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100338};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100339#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100340
Will Deaconecfadb62013-07-31 19:21:28 +0100341#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
342#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
343
Will Deaconc752ce42014-06-25 22:46:31 +0100344enum arm_smmu_domain_stage {
345 ARM_SMMU_DOMAIN_S1 = 0,
346 ARM_SMMU_DOMAIN_S2,
347 ARM_SMMU_DOMAIN_NESTED,
348};
349
Will Deacon45ae7cf2013-06-24 18:31:25 +0100350struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100351 struct arm_smmu_device *smmu;
Will Deacon518f7132014-11-14 17:17:54 +0000352 struct io_pgtable_ops *pgtbl_ops;
353 spinlock_t pgtbl_lock;
Will Deacon44680ee2014-06-25 11:29:12 +0100354 struct arm_smmu_cfg cfg;
Will Deaconc752ce42014-06-25 22:46:31 +0100355 enum arm_smmu_domain_stage stage;
Will Deacon518f7132014-11-14 17:17:54 +0000356 struct mutex init_mutex; /* Protects smmu pointer */
Joerg Roedel1d672632015-03-26 13:43:10 +0100357 struct iommu_domain domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100358};
359
Will Deacon518f7132014-11-14 17:17:54 +0000360static struct iommu_ops arm_smmu_ops;
361
Will Deacon45ae7cf2013-06-24 18:31:25 +0100362static DEFINE_SPINLOCK(arm_smmu_devices_lock);
363static LIST_HEAD(arm_smmu_devices);
364
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000365struct arm_smmu_option_prop {
366 u32 opt;
367 const char *prop;
368};
369
Mitchel Humpherys29073202014-07-08 09:52:18 -0700370static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000371 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
372 { 0, NULL},
373};
374
Joerg Roedel1d672632015-03-26 13:43:10 +0100375static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
376{
377 return container_of(dom, struct arm_smmu_domain, domain);
378}
379
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000380static void parse_driver_options(struct arm_smmu_device *smmu)
381{
382 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700383
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000384 do {
385 if (of_property_read_bool(smmu->dev->of_node,
386 arm_smmu_options[i].prop)) {
387 smmu->options |= arm_smmu_options[i].opt;
388 dev_notice(smmu->dev, "option %s\n",
389 arm_smmu_options[i].prop);
390 }
391 } while (arm_smmu_options[++i].opt);
392}
393
Will Deacon8f68f8e2014-07-15 11:27:08 +0100394static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100395{
396 if (dev_is_pci(dev)) {
397 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700398
Will Deacona9a1b0b2014-05-01 18:05:08 +0100399 while (!pci_is_root_bus(bus))
400 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100401 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100402 }
403
Will Deacon8f68f8e2014-07-15 11:27:08 +0100404 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100405}
406
Will Deacon45ae7cf2013-06-24 18:31:25 +0100407static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
408 struct device_node *dev_node)
409{
410 struct rb_node *node = smmu->masters.rb_node;
411
412 while (node) {
413 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700414
Will Deacon45ae7cf2013-06-24 18:31:25 +0100415 master = container_of(node, struct arm_smmu_master, node);
416
417 if (dev_node < master->of_node)
418 node = node->rb_left;
419 else if (dev_node > master->of_node)
420 node = node->rb_right;
421 else
422 return master;
423 }
424
425 return NULL;
426}
427
Will Deacona9a1b0b2014-05-01 18:05:08 +0100428static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100429find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100430{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100431 struct arm_smmu_master_cfg *cfg = NULL;
432 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100433
Will Deacon8f68f8e2014-07-15 11:27:08 +0100434 if (group) {
435 cfg = iommu_group_get_iommudata(group);
436 iommu_group_put(group);
437 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100438
Will Deacon8f68f8e2014-07-15 11:27:08 +0100439 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100440}
441
Will Deacon45ae7cf2013-06-24 18:31:25 +0100442static int insert_smmu_master(struct arm_smmu_device *smmu,
443 struct arm_smmu_master *master)
444{
445 struct rb_node **new, *parent;
446
447 new = &smmu->masters.rb_node;
448 parent = NULL;
449 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700450 struct arm_smmu_master *this
451 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100452
453 parent = *new;
454 if (master->of_node < this->of_node)
455 new = &((*new)->rb_left);
456 else if (master->of_node > this->of_node)
457 new = &((*new)->rb_right);
458 else
459 return -EEXIST;
460 }
461
462 rb_link_node(&master->node, parent, new);
463 rb_insert_color(&master->node, &smmu->masters);
464 return 0;
465}
466
467static int register_smmu_master(struct arm_smmu_device *smmu,
468 struct device *dev,
469 struct of_phandle_args *masterspec)
470{
471 int i;
472 struct arm_smmu_master *master;
473
474 master = find_smmu_master(smmu, masterspec->np);
475 if (master) {
476 dev_err(dev,
477 "rejecting multiple registrations for master device %s\n",
478 masterspec->np->name);
479 return -EBUSY;
480 }
481
482 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
483 dev_err(dev,
484 "reached maximum number (%d) of stream IDs for master device %s\n",
485 MAX_MASTER_STREAMIDS, masterspec->np->name);
486 return -ENOSPC;
487 }
488
489 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
490 if (!master)
491 return -ENOMEM;
492
Will Deacona9a1b0b2014-05-01 18:05:08 +0100493 master->of_node = masterspec->np;
494 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100495
Olav Haugan3c8766d2014-08-22 17:12:32 -0700496 for (i = 0; i < master->cfg.num_streamids; ++i) {
497 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100498
Olav Haugan3c8766d2014-08-22 17:12:32 -0700499 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
500 (streamid >= smmu->num_mapping_groups)) {
501 dev_err(dev,
502 "stream ID for master device %s greater than maximum allowed (%d)\n",
503 masterspec->np->name, smmu->num_mapping_groups);
504 return -ERANGE;
505 }
506 master->cfg.streamids[i] = streamid;
507 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100508 return insert_smmu_master(smmu, master);
509}
510
Will Deacon44680ee2014-06-25 11:29:12 +0100511static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100512{
Will Deacon44680ee2014-06-25 11:29:12 +0100513 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100514 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100515 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100516
517 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100518 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100519 master = find_smmu_master(smmu, dev_node);
520 if (master)
521 break;
522 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100523 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100524
Will Deacona9a1b0b2014-05-01 18:05:08 +0100525 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100526}
527
528static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
529{
530 int idx;
531
532 do {
533 idx = find_next_zero_bit(map, end, start);
534 if (idx == end)
535 return -ENOSPC;
536 } while (test_and_set_bit(idx, map));
537
538 return idx;
539}
540
541static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
542{
543 clear_bit(idx, map);
544}
545
546/* Wait for any pending TLB invalidations to complete */
Will Deacon518f7132014-11-14 17:17:54 +0000547static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100548{
549 int count = 0;
550 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
551
552 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
553 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
554 & sTLBGSTATUS_GSACTIVE) {
555 cpu_relax();
556 if (++count == TLB_LOOP_TIMEOUT) {
557 dev_err_ratelimited(smmu->dev,
558 "TLB sync timed out -- SMMU may be deadlocked\n");
559 return;
560 }
561 udelay(1);
562 }
563}
564
Will Deacon518f7132014-11-14 17:17:54 +0000565static void arm_smmu_tlb_sync(void *cookie)
Will Deacon1463fe42013-07-31 19:21:27 +0100566{
Will Deacon518f7132014-11-14 17:17:54 +0000567 struct arm_smmu_domain *smmu_domain = cookie;
568 __arm_smmu_tlb_sync(smmu_domain->smmu);
569}
570
571static void arm_smmu_tlb_inv_context(void *cookie)
572{
573 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100574 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
575 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100576 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon518f7132014-11-14 17:17:54 +0000577 void __iomem *base;
Will Deacon1463fe42013-07-31 19:21:27 +0100578
579 if (stage1) {
580 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100581 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
582 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100583 } else {
584 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100585 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
586 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100587 }
588
Will Deacon518f7132014-11-14 17:17:54 +0000589 __arm_smmu_tlb_sync(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100590}
591
Will Deacon518f7132014-11-14 17:17:54 +0000592static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
Robin Murphy06c610e2015-12-07 18:18:53 +0000593 size_t granule, bool leaf, void *cookie)
Will Deacon518f7132014-11-14 17:17:54 +0000594{
595 struct arm_smmu_domain *smmu_domain = cookie;
596 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
597 struct arm_smmu_device *smmu = smmu_domain->smmu;
598 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
599 void __iomem *reg;
600
601 if (stage1) {
602 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
603 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
604
605 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
606 iova &= ~12UL;
607 iova |= ARM_SMMU_CB_ASID(cfg);
Robin Murphy75df1382015-12-07 18:18:52 +0000608 do {
609 writel_relaxed(iova, reg);
610 iova += granule;
611 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000612#ifdef CONFIG_64BIT
613 } else {
614 iova >>= 12;
615 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
Robin Murphy75df1382015-12-07 18:18:52 +0000616 do {
617 writeq_relaxed(iova, reg);
618 iova += granule >> 12;
619 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000620#endif
621 }
622#ifdef CONFIG_64BIT
623 } else if (smmu->version == ARM_SMMU_V2) {
624 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
625 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
626 ARM_SMMU_CB_S2_TLBIIPAS2;
Robin Murphy75df1382015-12-07 18:18:52 +0000627 iova >>= 12;
628 do {
629 writeq_relaxed(iova, reg);
630 iova += granule >> 12;
631 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000632#endif
633 } else {
634 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
635 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
636 }
637}
638
Will Deacon518f7132014-11-14 17:17:54 +0000639static struct iommu_gather_ops arm_smmu_gather_ops = {
640 .tlb_flush_all = arm_smmu_tlb_inv_context,
641 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
642 .tlb_sync = arm_smmu_tlb_sync,
Will Deacon518f7132014-11-14 17:17:54 +0000643};
644
Will Deacon45ae7cf2013-06-24 18:31:25 +0100645static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
646{
647 int flags, ret;
648 u32 fsr, far, fsynr, resume;
649 unsigned long iova;
650 struct iommu_domain *domain = dev;
Joerg Roedel1d672632015-03-26 13:43:10 +0100651 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100652 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
653 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100654 void __iomem *cb_base;
655
Will Deacon44680ee2014-06-25 11:29:12 +0100656 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100657 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
658
659 if (!(fsr & FSR_FAULT))
660 return IRQ_NONE;
661
662 if (fsr & FSR_IGN)
663 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100664 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100665 fsr);
666
667 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
668 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
669
670 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
671 iova = far;
672#ifdef CONFIG_64BIT
673 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
674 iova |= ((unsigned long)far << 32);
675#endif
676
677 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
678 ret = IRQ_HANDLED;
679 resume = RESUME_RETRY;
680 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100681 dev_err_ratelimited(smmu->dev,
682 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100683 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100684 ret = IRQ_NONE;
685 resume = RESUME_TERMINATE;
686 }
687
688 /* Clear the faulting FSR */
689 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
690
691 /* Retry or terminate any stalled transactions */
692 if (fsr & FSR_SS)
693 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
694
695 return ret;
696}
697
698static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
699{
700 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
701 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000702 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100703
704 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
705 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
706 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
707 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
708
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000709 if (!gfsr)
710 return IRQ_NONE;
711
Will Deacon45ae7cf2013-06-24 18:31:25 +0100712 dev_err_ratelimited(smmu->dev,
713 "Unexpected global fault, this could be serious\n");
714 dev_err_ratelimited(smmu->dev,
715 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
716 gfsr, gfsynr0, gfsynr1, gfsynr2);
717
718 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100719 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100720}
721
Will Deacon518f7132014-11-14 17:17:54 +0000722static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
723 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100724{
725 u32 reg;
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100726 u64 reg64;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100727 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100728 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
729 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deaconc88ae5d2015-10-13 17:53:24 +0100730 void __iomem *cb_base, *gr1_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100731
Will Deacon45ae7cf2013-06-24 18:31:25 +0100732 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100733 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
734 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100735
Will Deacon4a1c93c2015-03-04 12:21:03 +0000736 if (smmu->version > ARM_SMMU_V1) {
737 /*
738 * CBA2R.
739 * *Must* be initialised before CBAR thanks to VMID16
740 * architectural oversight affected some implementations.
741 */
742#ifdef CONFIG_64BIT
743 reg = CBA2R_RW64_64BIT;
744#else
745 reg = CBA2R_RW64_32BIT;
746#endif
747 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
748 }
749
Will Deacon45ae7cf2013-06-24 18:31:25 +0100750 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100751 reg = cfg->cbar;
Robin Murphy09360402014-08-28 17:51:59 +0100752 if (smmu->version == ARM_SMMU_V1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700753 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100754
Will Deacon57ca90f2014-02-06 14:59:05 +0000755 /*
756 * Use the weakest shareability/memory types, so they are
757 * overridden by the ttbcr/pte.
758 */
759 if (stage1) {
760 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
761 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
762 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100763 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000764 }
Will Deacon44680ee2014-06-25 11:29:12 +0100765 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100766
Will Deacon518f7132014-11-14 17:17:54 +0000767 /* TTBRs */
768 if (stage1) {
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100769 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100770
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100771 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT;
772 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0);
773
774 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
775 reg64 |= ((u64)ARM_SMMU_CB_ASID(cfg)) << TTBRn_ASID_SHIFT;
776 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR1);
Will Deacon518f7132014-11-14 17:17:54 +0000777 } else {
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100778 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
779 smmu_writeq(reg64, cb_base + ARM_SMMU_CB_TTBR0);
Will Deacon518f7132014-11-14 17:17:54 +0000780 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100781
Will Deacon518f7132014-11-14 17:17:54 +0000782 /* TTBCR */
783 if (stage1) {
784 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
785 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
786 if (smmu->version > ARM_SMMU_V1) {
787 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
Will Deacon5dc56162015-05-08 17:44:22 +0100788 reg |= TTBCR2_SEP_UPSTREAM;
Will Deacon518f7132014-11-14 17:17:54 +0000789 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100790 }
791 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000792 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
793 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100794 }
795
Will Deacon518f7132014-11-14 17:17:54 +0000796 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100797 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000798 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100799 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000800 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
801 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100802 }
803
Will Deacon45ae7cf2013-06-24 18:31:25 +0100804 /* SCTLR */
805 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
806 if (stage1)
807 reg |= SCTLR_S1_ASIDPNE;
808#ifdef __BIG_ENDIAN
809 reg |= SCTLR_E;
810#endif
Will Deacon25724842013-08-21 13:49:53 +0100811 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100812}
813
814static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100815 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100816{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100817 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000818 unsigned long ias, oas;
819 struct io_pgtable_ops *pgtbl_ops;
820 struct io_pgtable_cfg pgtbl_cfg;
821 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100822 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100823 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100824
Will Deacon518f7132014-11-14 17:17:54 +0000825 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100826 if (smmu_domain->smmu)
827 goto out_unlock;
828
Will Deaconc752ce42014-06-25 22:46:31 +0100829 /*
830 * Mapping the requested stage onto what we support is surprisingly
831 * complicated, mainly because the spec allows S1+S2 SMMUs without
832 * support for nested translation. That means we end up with the
833 * following table:
834 *
835 * Requested Supported Actual
836 * S1 N S1
837 * S1 S1+S2 S1
838 * S1 S2 S2
839 * S1 S1 S1
840 * N N N
841 * N S1+S2 S2
842 * N S2 S2
843 * N S1 S1
844 *
845 * Note that you can't actually request stage-2 mappings.
846 */
847 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
848 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
849 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
850 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
851
852 switch (smmu_domain->stage) {
853 case ARM_SMMU_DOMAIN_S1:
854 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
855 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000856 ias = smmu->va_size;
857 oas = smmu->ipa_size;
858 if (IS_ENABLED(CONFIG_64BIT))
859 fmt = ARM_64_LPAE_S1;
860 else
861 fmt = ARM_32_LPAE_S1;
Will Deaconc752ce42014-06-25 22:46:31 +0100862 break;
863 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100864 /*
865 * We will likely want to change this if/when KVM gets
866 * involved.
867 */
Will Deaconc752ce42014-06-25 22:46:31 +0100868 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100869 cfg->cbar = CBAR_TYPE_S2_TRANS;
870 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000871 ias = smmu->ipa_size;
872 oas = smmu->pa_size;
873 if (IS_ENABLED(CONFIG_64BIT))
874 fmt = ARM_64_LPAE_S2;
875 else
876 fmt = ARM_32_LPAE_S2;
Will Deaconc752ce42014-06-25 22:46:31 +0100877 break;
878 default:
879 ret = -EINVAL;
880 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100881 }
882
883 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
884 smmu->num_context_banks);
885 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100886 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100887
Will Deacon44680ee2014-06-25 11:29:12 +0100888 cfg->cbndx = ret;
Robin Murphy09360402014-08-28 17:51:59 +0100889 if (smmu->version == ARM_SMMU_V1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100890 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
891 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100892 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100893 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100894 }
895
Will Deacon518f7132014-11-14 17:17:54 +0000896 pgtbl_cfg = (struct io_pgtable_cfg) {
897 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
898 .ias = ias,
899 .oas = oas,
900 .tlb = &arm_smmu_gather_ops,
Robin Murphy2df7a252015-07-29 19:46:06 +0100901 .iommu_dev = smmu->dev,
Will Deacon518f7132014-11-14 17:17:54 +0000902 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100903
Will Deacon518f7132014-11-14 17:17:54 +0000904 smmu_domain->smmu = smmu;
905 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
906 if (!pgtbl_ops) {
907 ret = -ENOMEM;
908 goto out_clear_smmu;
909 }
910
911 /* Update our support page sizes to reflect the page table format */
912 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
913
914 /* Initialise the context bank with our page table cfg */
915 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
916
917 /*
918 * Request context fault interrupt. Do this last to avoid the
919 * handler seeing a half-initialised domain state.
920 */
Will Deacon44680ee2014-06-25 11:29:12 +0100921 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100922 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
923 "arm-smmu-context-fault", domain);
924 if (IS_ERR_VALUE(ret)) {
925 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100926 cfg->irptndx, irq);
927 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100928 }
929
Will Deacon518f7132014-11-14 17:17:54 +0000930 mutex_unlock(&smmu_domain->init_mutex);
931
932 /* Publish page table ops for map/unmap */
933 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100934 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100935
Will Deacon518f7132014-11-14 17:17:54 +0000936out_clear_smmu:
937 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100938out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000939 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100940 return ret;
941}
942
943static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
944{
Joerg Roedel1d672632015-03-26 13:43:10 +0100945 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100946 struct arm_smmu_device *smmu = smmu_domain->smmu;
947 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100948 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100949 int irq;
950
951 if (!smmu)
952 return;
953
Will Deacon518f7132014-11-14 17:17:54 +0000954 /*
955 * Disable the context bank and free the page tables before freeing
956 * it.
957 */
Will Deacon44680ee2014-06-25 11:29:12 +0100958 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100959 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +0100960
Will Deacon44680ee2014-06-25 11:29:12 +0100961 if (cfg->irptndx != INVALID_IRPTNDX) {
962 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100963 free_irq(irq, domain);
964 }
965
Markus Elfring44830b02015-11-06 18:32:41 +0100966 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon44680ee2014-06-25 11:29:12 +0100967 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100968}
969
Joerg Roedel1d672632015-03-26 13:43:10 +0100970static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100971{
972 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100973
Robin Murphy9adb9592016-01-26 18:06:36 +0000974 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
Joerg Roedel1d672632015-03-26 13:43:10 +0100975 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100976 /*
977 * Allocate the domain and initialise some of its data structures.
978 * We can't really do anything meaningful until we've added a
979 * master.
980 */
981 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
982 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +0100983 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100984
Robin Murphy9adb9592016-01-26 18:06:36 +0000985 if (type == IOMMU_DOMAIN_DMA &&
986 iommu_get_dma_cookie(&smmu_domain->domain)) {
987 kfree(smmu_domain);
988 return NULL;
989 }
990
Will Deacon518f7132014-11-14 17:17:54 +0000991 mutex_init(&smmu_domain->init_mutex);
992 spin_lock_init(&smmu_domain->pgtbl_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +0100993
994 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100995}
996
Joerg Roedel1d672632015-03-26 13:43:10 +0100997static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100998{
Joerg Roedel1d672632015-03-26 13:43:10 +0100999 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +01001000
1001 /*
1002 * Free the domain resources. We assume that all devices have
1003 * already been detached.
1004 */
Robin Murphy9adb9592016-01-26 18:06:36 +00001005 iommu_put_dma_cookie(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001006 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001007 kfree(smmu_domain);
1008}
1009
1010static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001011 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001012{
1013 int i;
1014 struct arm_smmu_smr *smrs;
1015 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1016
1017 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1018 return 0;
1019
Will Deacona9a1b0b2014-05-01 18:05:08 +01001020 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001021 return -EEXIST;
1022
Mitchel Humpherys29073202014-07-08 09:52:18 -07001023 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001024 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001025 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1026 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001027 return -ENOMEM;
1028 }
1029
Will Deacon44680ee2014-06-25 11:29:12 +01001030 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001031 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001032 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1033 smmu->num_mapping_groups);
1034 if (IS_ERR_VALUE(idx)) {
1035 dev_err(smmu->dev, "failed to allocate free SMR\n");
1036 goto err_free_smrs;
1037 }
1038
1039 smrs[i] = (struct arm_smmu_smr) {
1040 .idx = idx,
1041 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001042 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001043 };
1044 }
1045
1046 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001047 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001048 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1049 smrs[i].mask << SMR_MASK_SHIFT;
1050 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1051 }
1052
Will Deacona9a1b0b2014-05-01 18:05:08 +01001053 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001054 return 0;
1055
1056err_free_smrs:
1057 while (--i >= 0)
1058 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1059 kfree(smrs);
1060 return -ENOSPC;
1061}
1062
1063static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001064 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001065{
1066 int i;
1067 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001068 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001069
Will Deacon43b412b2014-07-15 11:22:24 +01001070 if (!smrs)
1071 return;
1072
Will Deacon45ae7cf2013-06-24 18:31:25 +01001073 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001074 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001075 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001076
Will Deacon45ae7cf2013-06-24 18:31:25 +01001077 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1078 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1079 }
1080
Will Deacona9a1b0b2014-05-01 18:05:08 +01001081 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001082 kfree(smrs);
1083}
1084
Will Deacon45ae7cf2013-06-24 18:31:25 +01001085static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001086 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001087{
1088 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001089 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001090 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1091
Will Deacon8f68f8e2014-07-15 11:27:08 +01001092 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001093 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001094 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001095 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001096
Will Deaconcbf82772016-02-18 12:05:57 +00001097 /*
1098 * FIXME: This won't be needed once we have IOMMU-backed DMA ops
1099 * for all devices behind the SMMU.
1100 */
1101 if (smmu_domain->domain.type == IOMMU_DOMAIN_DMA)
1102 return 0;
1103
Will Deacona9a1b0b2014-05-01 18:05:08 +01001104 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001105 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001106
Will Deacona9a1b0b2014-05-01 18:05:08 +01001107 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Robin Murphyd3461802016-01-26 18:06:34 +00001108 s2cr = S2CR_TYPE_TRANS | S2CR_PRIVCFG_UNPRIV |
Will Deacon44680ee2014-06-25 11:29:12 +01001109 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001110 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1111 }
1112
1113 return 0;
1114}
1115
1116static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001117 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001118{
Will Deacon43b412b2014-07-15 11:22:24 +01001119 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001120 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001121 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001122
Will Deacon8f68f8e2014-07-15 11:27:08 +01001123 /* An IOMMU group is torn down by the first device to be removed */
1124 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1125 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001126
1127 /*
1128 * We *must* clear the S2CR first, because freeing the SMR means
1129 * that it can be re-allocated immediately.
1130 */
Will Deacon43b412b2014-07-15 11:22:24 +01001131 for (i = 0; i < cfg->num_streamids; ++i) {
1132 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Robin Murphy25a1c962016-02-10 14:25:33 +00001133 u32 reg = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS;
Will Deacon43b412b2014-07-15 11:22:24 +01001134
Robin Murphy25a1c962016-02-10 14:25:33 +00001135 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(idx));
Will Deacon43b412b2014-07-15 11:22:24 +01001136 }
1137
Will Deacona9a1b0b2014-05-01 18:05:08 +01001138 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001139}
1140
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001141static void arm_smmu_detach_dev(struct device *dev,
1142 struct arm_smmu_master_cfg *cfg)
1143{
1144 struct iommu_domain *domain = dev->archdata.iommu;
1145 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1146
1147 dev->archdata.iommu = NULL;
1148 arm_smmu_domain_remove_master(smmu_domain, cfg);
1149}
1150
Will Deacon45ae7cf2013-06-24 18:31:25 +01001151static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1152{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001153 int ret;
Joerg Roedel1d672632015-03-26 13:43:10 +01001154 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001155 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001156 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001157
Will Deacon8f68f8e2014-07-15 11:27:08 +01001158 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001159 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001160 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1161 return -ENXIO;
1162 }
1163
Will Deacon518f7132014-11-14 17:17:54 +00001164 /* Ensure that the domain is finalised */
1165 ret = arm_smmu_init_domain_context(domain, smmu);
1166 if (IS_ERR_VALUE(ret))
1167 return ret;
1168
Will Deacon45ae7cf2013-06-24 18:31:25 +01001169 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001170 * Sanity check the domain. We don't support domains across
1171 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001172 */
Will Deacon518f7132014-11-14 17:17:54 +00001173 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001174 dev_err(dev,
1175 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001176 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1177 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001178 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001179
1180 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001181 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001182 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001183 return -ENODEV;
1184
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001185 /* Detach the dev from its current domain */
1186 if (dev->archdata.iommu)
1187 arm_smmu_detach_dev(dev, cfg);
1188
Will Deacon844e35b2014-07-17 11:23:51 +01001189 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1190 if (!ret)
1191 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001192 return ret;
1193}
1194
Will Deacon45ae7cf2013-06-24 18:31:25 +01001195static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001196 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001197{
Will Deacon518f7132014-11-14 17:17:54 +00001198 int ret;
1199 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001200 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001201 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001202
Will Deacon518f7132014-11-14 17:17:54 +00001203 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001204 return -ENODEV;
1205
Will Deacon518f7132014-11-14 17:17:54 +00001206 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1207 ret = ops->map(ops, iova, paddr, size, prot);
1208 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1209 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001210}
1211
1212static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1213 size_t size)
1214{
Will Deacon518f7132014-11-14 17:17:54 +00001215 size_t ret;
1216 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001217 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001218 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001219
Will Deacon518f7132014-11-14 17:17:54 +00001220 if (!ops)
1221 return 0;
1222
1223 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1224 ret = ops->unmap(ops, iova, size);
1225 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1226 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001227}
1228
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001229static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1230 dma_addr_t iova)
1231{
Joerg Roedel1d672632015-03-26 13:43:10 +01001232 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001233 struct arm_smmu_device *smmu = smmu_domain->smmu;
1234 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1235 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1236 struct device *dev = smmu->dev;
1237 void __iomem *cb_base;
1238 u32 tmp;
1239 u64 phys;
Robin Murphy661d9622015-05-27 17:09:34 +01001240 unsigned long va;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001241
1242 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1243
Robin Murphy661d9622015-05-27 17:09:34 +01001244 /* ATS1 registers can only be written atomically */
1245 va = iova & ~0xfffUL;
Robin Murphy661d9622015-05-27 17:09:34 +01001246 if (smmu->version == ARM_SMMU_V2)
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +01001247 smmu_writeq(va, cb_base + ARM_SMMU_CB_ATS1PR);
Robin Murphy661d9622015-05-27 17:09:34 +01001248 else
Robin Murphy661d9622015-05-27 17:09:34 +01001249 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001250
1251 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1252 !(tmp & ATSR_ACTIVE), 5, 50)) {
1253 dev_err(dev,
Fabio Estevam077124c2015-08-18 17:12:24 +01001254 "iova to phys timed out on %pad. Falling back to software table walk.\n",
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001255 &iova);
1256 return ops->iova_to_phys(ops, iova);
1257 }
1258
1259 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1260 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1261
1262 if (phys & CB_PAR_F) {
1263 dev_err(dev, "translation fault!\n");
1264 dev_err(dev, "PAR = 0x%llx\n", phys);
1265 return 0;
1266 }
1267
1268 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1269}
1270
Will Deacon45ae7cf2013-06-24 18:31:25 +01001271static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001272 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001273{
Will Deacon518f7132014-11-14 17:17:54 +00001274 phys_addr_t ret;
1275 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001276 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001277 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001278
Will Deacon518f7132014-11-14 17:17:54 +00001279 if (!ops)
Will Deacona44a97912013-11-07 18:47:50 +00001280 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001281
Will Deacon518f7132014-11-14 17:17:54 +00001282 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001283 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1284 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001285 ret = arm_smmu_iova_to_phys_hard(domain, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001286 } else {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001287 ret = ops->iova_to_phys(ops, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001288 }
1289
Will Deacon518f7132014-11-14 17:17:54 +00001290 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001291
Will Deacon518f7132014-11-14 17:17:54 +00001292 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001293}
1294
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001295static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001296{
Will Deacond0948942014-06-24 17:30:10 +01001297 switch (cap) {
1298 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001299 /*
1300 * Return true here as the SMMU can always send out coherent
1301 * requests.
1302 */
1303 return true;
Will Deacond0948942014-06-24 17:30:10 +01001304 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001305 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001306 case IOMMU_CAP_NOEXEC:
1307 return true;
Will Deacond0948942014-06-24 17:30:10 +01001308 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001309 return false;
Will Deacond0948942014-06-24 17:30:10 +01001310 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001311}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001312
Will Deacona9a1b0b2014-05-01 18:05:08 +01001313static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1314{
1315 *((u16 *)data) = alias;
1316 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001317}
1318
Will Deacon8f68f8e2014-07-15 11:27:08 +01001319static void __arm_smmu_release_pci_iommudata(void *data)
1320{
1321 kfree(data);
1322}
1323
Joerg Roedelaf659932015-10-21 23:51:41 +02001324static int arm_smmu_init_pci_device(struct pci_dev *pdev,
1325 struct iommu_group *group)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001326{
Will Deacon03edb222015-01-19 14:27:33 +00001327 struct arm_smmu_master_cfg *cfg;
Joerg Roedelaf659932015-10-21 23:51:41 +02001328 u16 sid;
1329 int i;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001330
Will Deacon03edb222015-01-19 14:27:33 +00001331 cfg = iommu_group_get_iommudata(group);
1332 if (!cfg) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001333 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
Joerg Roedelaf659932015-10-21 23:51:41 +02001334 if (!cfg)
1335 return -ENOMEM;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001336
Will Deacon03edb222015-01-19 14:27:33 +00001337 iommu_group_set_iommudata(group, cfg,
1338 __arm_smmu_release_pci_iommudata);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001339 }
1340
Joerg Roedelaf659932015-10-21 23:51:41 +02001341 if (cfg->num_streamids >= MAX_MASTER_STREAMIDS)
1342 return -ENOSPC;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001343
Will Deacon03edb222015-01-19 14:27:33 +00001344 /*
1345 * Assume Stream ID == Requester ID for now.
1346 * We need a way to describe the ID mappings in FDT.
1347 */
1348 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1349 for (i = 0; i < cfg->num_streamids; ++i)
1350 if (cfg->streamids[i] == sid)
1351 break;
1352
1353 /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
1354 if (i == cfg->num_streamids)
1355 cfg->streamids[cfg->num_streamids++] = sid;
1356
1357 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001358}
1359
Joerg Roedelaf659932015-10-21 23:51:41 +02001360static int arm_smmu_init_platform_device(struct device *dev,
1361 struct iommu_group *group)
Will Deacon03edb222015-01-19 14:27:33 +00001362{
Will Deacon03edb222015-01-19 14:27:33 +00001363 struct arm_smmu_device *smmu = find_smmu_for_device(dev);
Joerg Roedelaf659932015-10-21 23:51:41 +02001364 struct arm_smmu_master *master;
Will Deacon03edb222015-01-19 14:27:33 +00001365
1366 if (!smmu)
1367 return -ENODEV;
1368
1369 master = find_smmu_master(smmu, dev->of_node);
1370 if (!master)
1371 return -ENODEV;
1372
Will Deacon03edb222015-01-19 14:27:33 +00001373 iommu_group_set_iommudata(group, &master->cfg, NULL);
Joerg Roedelaf659932015-10-21 23:51:41 +02001374
1375 return 0;
Will Deacon03edb222015-01-19 14:27:33 +00001376}
1377
1378static int arm_smmu_add_device(struct device *dev)
1379{
Joerg Roedelaf659932015-10-21 23:51:41 +02001380 struct iommu_group *group;
Will Deacon03edb222015-01-19 14:27:33 +00001381
Joerg Roedelaf659932015-10-21 23:51:41 +02001382 group = iommu_group_get_for_dev(dev);
1383 if (IS_ERR(group))
1384 return PTR_ERR(group);
1385
Peng Fan9a4a9d82015-11-20 16:56:18 +08001386 iommu_group_put(group);
Joerg Roedelaf659932015-10-21 23:51:41 +02001387 return 0;
Will Deacon03edb222015-01-19 14:27:33 +00001388}
1389
Will Deacon45ae7cf2013-06-24 18:31:25 +01001390static void arm_smmu_remove_device(struct device *dev)
1391{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001392 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001393}
1394
Joerg Roedelaf659932015-10-21 23:51:41 +02001395static struct iommu_group *arm_smmu_device_group(struct device *dev)
1396{
1397 struct iommu_group *group;
1398 int ret;
1399
1400 if (dev_is_pci(dev))
1401 group = pci_device_group(dev);
1402 else
1403 group = generic_device_group(dev);
1404
1405 if (IS_ERR(group))
1406 return group;
1407
1408 if (dev_is_pci(dev))
1409 ret = arm_smmu_init_pci_device(to_pci_dev(dev), group);
1410 else
1411 ret = arm_smmu_init_platform_device(dev, group);
1412
1413 if (ret) {
1414 iommu_group_put(group);
1415 group = ERR_PTR(ret);
1416 }
1417
1418 return group;
1419}
1420
Will Deaconc752ce42014-06-25 22:46:31 +01001421static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1422 enum iommu_attr attr, void *data)
1423{
Joerg Roedel1d672632015-03-26 13:43:10 +01001424 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001425
1426 switch (attr) {
1427 case DOMAIN_ATTR_NESTING:
1428 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1429 return 0;
1430 default:
1431 return -ENODEV;
1432 }
1433}
1434
1435static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1436 enum iommu_attr attr, void *data)
1437{
Will Deacon518f7132014-11-14 17:17:54 +00001438 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001439 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001440
Will Deacon518f7132014-11-14 17:17:54 +00001441 mutex_lock(&smmu_domain->init_mutex);
1442
Will Deaconc752ce42014-06-25 22:46:31 +01001443 switch (attr) {
1444 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001445 if (smmu_domain->smmu) {
1446 ret = -EPERM;
1447 goto out_unlock;
1448 }
1449
Will Deaconc752ce42014-06-25 22:46:31 +01001450 if (*(int *)data)
1451 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1452 else
1453 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1454
Will Deacon518f7132014-11-14 17:17:54 +00001455 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001456 default:
Will Deacon518f7132014-11-14 17:17:54 +00001457 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001458 }
Will Deacon518f7132014-11-14 17:17:54 +00001459
1460out_unlock:
1461 mutex_unlock(&smmu_domain->init_mutex);
1462 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001463}
1464
Will Deacon518f7132014-11-14 17:17:54 +00001465static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001466 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001467 .domain_alloc = arm_smmu_domain_alloc,
1468 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001469 .attach_dev = arm_smmu_attach_dev,
Will Deaconc752ce42014-06-25 22:46:31 +01001470 .map = arm_smmu_map,
1471 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001472 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001473 .iova_to_phys = arm_smmu_iova_to_phys,
1474 .add_device = arm_smmu_add_device,
1475 .remove_device = arm_smmu_remove_device,
Joerg Roedelaf659932015-10-21 23:51:41 +02001476 .device_group = arm_smmu_device_group,
Will Deaconc752ce42014-06-25 22:46:31 +01001477 .domain_get_attr = arm_smmu_domain_get_attr,
1478 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001479 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001480};
1481
1482static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1483{
1484 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001485 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001486 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001487 u32 reg;
1488
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001489 /* clear global FSR */
1490 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1491 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001492
Robin Murphy25a1c962016-02-10 14:25:33 +00001493 /* Mark all SMRn as invalid and all S2CRn as bypass unless overridden */
1494 reg = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001495 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001496 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Robin Murphy25a1c962016-02-10 14:25:33 +00001497 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001498 }
1499
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001500 /* Make sure all context banks are disabled and clear CB_FSR */
1501 for (i = 0; i < smmu->num_context_banks; ++i) {
1502 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1503 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1504 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1505 }
Will Deacon1463fe42013-07-31 19:21:27 +01001506
Will Deacon45ae7cf2013-06-24 18:31:25 +01001507 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001508 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1509 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1510
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001511 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001512
Will Deacon45ae7cf2013-06-24 18:31:25 +01001513 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001514 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001515
1516 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001517 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001518
Robin Murphy25a1c962016-02-10 14:25:33 +00001519 /* Enable client access, handling unmatched streams as appropriate */
1520 reg &= ~sCR0_CLIENTPD;
1521 if (disable_bypass)
1522 reg |= sCR0_USFCFG;
1523 else
1524 reg &= ~sCR0_USFCFG;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001525
1526 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001527 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001528
1529 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001530 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001531
1532 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001533 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001534 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001535}
1536
1537static int arm_smmu_id_size_to_bits(int size)
1538{
1539 switch (size) {
1540 case 0:
1541 return 32;
1542 case 1:
1543 return 36;
1544 case 2:
1545 return 40;
1546 case 3:
1547 return 42;
1548 case 4:
1549 return 44;
1550 case 5:
1551 default:
1552 return 48;
1553 }
1554}
1555
1556static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1557{
1558 unsigned long size;
1559 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1560 u32 id;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001561 bool cttw_dt, cttw_reg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001562
1563 dev_notice(smmu->dev, "probing hardware configuration...\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001564 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1565
1566 /* ID0 */
1567 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001568
1569 /* Restrict available stages based on module parameter */
1570 if (force_stage == 1)
1571 id &= ~(ID0_S2TS | ID0_NTS);
1572 else if (force_stage == 2)
1573 id &= ~(ID0_S1TS | ID0_NTS);
1574
Will Deacon45ae7cf2013-06-24 18:31:25 +01001575 if (id & ID0_S1TS) {
1576 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1577 dev_notice(smmu->dev, "\tstage 1 translation\n");
1578 }
1579
1580 if (id & ID0_S2TS) {
1581 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1582 dev_notice(smmu->dev, "\tstage 2 translation\n");
1583 }
1584
1585 if (id & ID0_NTS) {
1586 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1587 dev_notice(smmu->dev, "\tnested translation\n");
1588 }
1589
1590 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001591 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001592 dev_err(smmu->dev, "\tno translation support!\n");
1593 return -ENODEV;
1594 }
1595
Will Deacond38f0ff2015-06-29 17:47:42 +01001596 if ((id & ID0_S1TS) && ((smmu->version == 1) || !(id & ID0_ATOSNS))) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001597 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1598 dev_notice(smmu->dev, "\taddress translation ops\n");
1599 }
1600
Robin Murphybae2c2d2015-07-29 19:46:05 +01001601 /*
1602 * In order for DMA API calls to work properly, we must defer to what
1603 * the DT says about coherency, regardless of what the hardware claims.
1604 * Fortunately, this also opens up a workaround for systems where the
1605 * ID register value has ended up configured incorrectly.
1606 */
1607 cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
1608 cttw_reg = !!(id & ID0_CTTW);
1609 if (cttw_dt)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001610 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001611 if (cttw_dt || cttw_reg)
1612 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1613 cttw_dt ? "" : "non-");
1614 if (cttw_dt != cttw_reg)
1615 dev_notice(smmu->dev,
1616 "\t(IDR0.CTTW overridden by dma-coherent property)\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001617
1618 if (id & ID0_SMS) {
1619 u32 smr, sid, mask;
1620
1621 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1622 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1623 ID0_NUMSMRG_MASK;
1624 if (smmu->num_mapping_groups == 0) {
1625 dev_err(smmu->dev,
1626 "stream-matching supported, but no SMRs present!\n");
1627 return -ENODEV;
1628 }
1629
1630 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1631 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1632 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1633 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1634
1635 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1636 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1637 if ((mask & sid) != sid) {
1638 dev_err(smmu->dev,
1639 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1640 mask, sid);
1641 return -ENODEV;
1642 }
1643
1644 dev_notice(smmu->dev,
1645 "\tstream matching with %u register groups, mask 0x%x",
1646 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001647 } else {
1648 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1649 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001650 }
1651
1652 /* ID1 */
1653 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001654 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001655
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001656 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001657 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001658 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001659 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001660 dev_warn(smmu->dev,
1661 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1662 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001663
Will Deacon518f7132014-11-14 17:17:54 +00001664 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001665 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1666 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1667 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1668 return -ENODEV;
1669 }
1670 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1671 smmu->num_context_banks, smmu->num_s2_context_banks);
1672
1673 /* ID2 */
1674 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1675 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001676 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001677
Will Deacon518f7132014-11-14 17:17:54 +00001678 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001679 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001680 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001681
Robin Murphyf1d84542015-03-04 16:41:05 +00001682 /*
1683 * What the page table walker can address actually depends on which
1684 * descriptor format is in use, but since a) we don't know that yet,
1685 * and b) it can vary per context bank, this will have to do...
1686 */
1687 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1688 dev_warn(smmu->dev,
1689 "failed to set DMA mask for table walker\n");
1690
Robin Murphy09360402014-08-28 17:51:59 +01001691 if (smmu->version == ARM_SMMU_V1) {
Will Deacon518f7132014-11-14 17:17:54 +00001692 smmu->va_size = smmu->ipa_size;
1693 size = SZ_4K | SZ_2M | SZ_1G;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001694 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001695 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001696 smmu->va_size = arm_smmu_id_size_to_bits(size);
1697#ifndef CONFIG_64BIT
1698 smmu->va_size = min(32UL, smmu->va_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001699#endif
Will Deacon518f7132014-11-14 17:17:54 +00001700 size = 0;
1701 if (id & ID2_PTFS_4K)
1702 size |= SZ_4K | SZ_2M | SZ_1G;
1703 if (id & ID2_PTFS_16K)
1704 size |= SZ_16K | SZ_32M;
1705 if (id & ID2_PTFS_64K)
1706 size |= SZ_64K | SZ_512M;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001707 }
1708
Will Deacon518f7132014-11-14 17:17:54 +00001709 arm_smmu_ops.pgsize_bitmap &= size;
1710 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1711
Will Deacon28d60072014-09-01 16:24:48 +01001712 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1713 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001714 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001715
1716 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1717 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001718 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001719
Will Deacon45ae7cf2013-06-24 18:31:25 +01001720 return 0;
1721}
1722
Joerg Roedel09b52692014-10-02 12:24:45 +02001723static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy09360402014-08-28 17:51:59 +01001724 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1725 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1726 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
Robin Murphyd3aba042014-08-28 17:52:00 +01001727 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
Robin Murphy09360402014-08-28 17:51:59 +01001728 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1729 { },
1730};
1731MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1732
Will Deacon45ae7cf2013-06-24 18:31:25 +01001733static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1734{
Robin Murphy09360402014-08-28 17:51:59 +01001735 const struct of_device_id *of_id;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001736 struct resource *res;
1737 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001738 struct device *dev = &pdev->dev;
1739 struct rb_node *node;
1740 struct of_phandle_args masterspec;
1741 int num_irqs, i, err;
1742
1743 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1744 if (!smmu) {
1745 dev_err(dev, "failed to allocate arm_smmu_device\n");
1746 return -ENOMEM;
1747 }
1748 smmu->dev = dev;
1749
Robin Murphy09360402014-08-28 17:51:59 +01001750 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1751 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1752
Will Deacon45ae7cf2013-06-24 18:31:25 +01001753 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001754 smmu->base = devm_ioremap_resource(dev, res);
1755 if (IS_ERR(smmu->base))
1756 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001757 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001758
1759 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1760 &smmu->num_global_irqs)) {
1761 dev_err(dev, "missing #global-interrupts property\n");
1762 return -ENODEV;
1763 }
1764
1765 num_irqs = 0;
1766 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1767 num_irqs++;
1768 if (num_irqs > smmu->num_global_irqs)
1769 smmu->num_context_irqs++;
1770 }
1771
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001772 if (!smmu->num_context_irqs) {
1773 dev_err(dev, "found %d interrupts but expected at least %d\n",
1774 num_irqs, smmu->num_global_irqs + 1);
1775 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001776 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001777
1778 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1779 GFP_KERNEL);
1780 if (!smmu->irqs) {
1781 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1782 return -ENOMEM;
1783 }
1784
1785 for (i = 0; i < num_irqs; ++i) {
1786 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001787
Will Deacon45ae7cf2013-06-24 18:31:25 +01001788 if (irq < 0) {
1789 dev_err(dev, "failed to get irq index %d\n", i);
1790 return -ENODEV;
1791 }
1792 smmu->irqs[i] = irq;
1793 }
1794
Olav Haugan3c8766d2014-08-22 17:12:32 -07001795 err = arm_smmu_device_cfg_probe(smmu);
1796 if (err)
1797 return err;
1798
Will Deacon45ae7cf2013-06-24 18:31:25 +01001799 i = 0;
1800 smmu->masters = RB_ROOT;
1801 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1802 "#stream-id-cells", i,
1803 &masterspec)) {
1804 err = register_smmu_master(smmu, dev, &masterspec);
1805 if (err) {
1806 dev_err(dev, "failed to add master %s\n",
1807 masterspec.np->name);
1808 goto out_put_masters;
1809 }
1810
1811 i++;
1812 }
1813 dev_notice(dev, "registered %d master devices\n", i);
1814
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001815 parse_driver_options(smmu);
1816
Robin Murphy09360402014-08-28 17:51:59 +01001817 if (smmu->version > ARM_SMMU_V1 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001818 smmu->num_context_banks != smmu->num_context_irqs) {
1819 dev_err(dev,
1820 "found only %d context interrupt(s) but %d required\n",
1821 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001822 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001823 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001824 }
1825
Will Deacon45ae7cf2013-06-24 18:31:25 +01001826 for (i = 0; i < smmu->num_global_irqs; ++i) {
1827 err = request_irq(smmu->irqs[i],
1828 arm_smmu_global_fault,
1829 IRQF_SHARED,
1830 "arm-smmu global fault",
1831 smmu);
1832 if (err) {
1833 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1834 i, smmu->irqs[i]);
1835 goto out_free_irqs;
1836 }
1837 }
1838
1839 INIT_LIST_HEAD(&smmu->list);
1840 spin_lock(&arm_smmu_devices_lock);
1841 list_add(&smmu->list, &arm_smmu_devices);
1842 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001843
1844 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001845 return 0;
1846
1847out_free_irqs:
1848 while (i--)
1849 free_irq(smmu->irqs[i], smmu);
1850
Will Deacon45ae7cf2013-06-24 18:31:25 +01001851out_put_masters:
1852 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001853 struct arm_smmu_master *master
1854 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001855 of_node_put(master->of_node);
1856 }
1857
1858 return err;
1859}
1860
1861static int arm_smmu_device_remove(struct platform_device *pdev)
1862{
1863 int i;
1864 struct device *dev = &pdev->dev;
1865 struct arm_smmu_device *curr, *smmu = NULL;
1866 struct rb_node *node;
1867
1868 spin_lock(&arm_smmu_devices_lock);
1869 list_for_each_entry(curr, &arm_smmu_devices, list) {
1870 if (curr->dev == dev) {
1871 smmu = curr;
1872 list_del(&smmu->list);
1873 break;
1874 }
1875 }
1876 spin_unlock(&arm_smmu_devices_lock);
1877
1878 if (!smmu)
1879 return -ENODEV;
1880
Will Deacon45ae7cf2013-06-24 18:31:25 +01001881 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001882 struct arm_smmu_master *master
1883 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001884 of_node_put(master->of_node);
1885 }
1886
Will Deaconecfadb62013-07-31 19:21:28 +01001887 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001888 dev_err(dev, "removing device with active domains!\n");
1889
1890 for (i = 0; i < smmu->num_global_irqs; ++i)
1891 free_irq(smmu->irqs[i], smmu);
1892
1893 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001894 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001895 return 0;
1896}
1897
Will Deacon45ae7cf2013-06-24 18:31:25 +01001898static struct platform_driver arm_smmu_driver = {
1899 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001900 .name = "arm-smmu",
1901 .of_match_table = of_match_ptr(arm_smmu_of_match),
1902 },
1903 .probe = arm_smmu_device_dt_probe,
1904 .remove = arm_smmu_device_remove,
1905};
1906
1907static int __init arm_smmu_init(void)
1908{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001909 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001910 int ret;
1911
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001912 /*
1913 * Play nice with systems that don't have an ARM SMMU by checking that
1914 * an ARM SMMU exists in the system before proceeding with the driver
1915 * and IOMMU bus operation registration.
1916 */
1917 np = of_find_matching_node(NULL, arm_smmu_of_match);
1918 if (!np)
1919 return 0;
1920
1921 of_node_put(np);
1922
Will Deacon45ae7cf2013-06-24 18:31:25 +01001923 ret = platform_driver_register(&arm_smmu_driver);
1924 if (ret)
1925 return ret;
1926
1927 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001928 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001929 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1930
Will Deacond123cf82014-02-04 22:17:53 +00001931#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01001932 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001933 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00001934#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01001935
Will Deacona9a1b0b2014-05-01 18:05:08 +01001936#ifdef CONFIG_PCI
1937 if (!iommu_present(&pci_bus_type))
1938 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1939#endif
1940
Will Deacon45ae7cf2013-06-24 18:31:25 +01001941 return 0;
1942}
1943
1944static void __exit arm_smmu_exit(void)
1945{
1946 return platform_driver_unregister(&arm_smmu_driver);
1947}
1948
Andreas Herrmannb1950b22013-10-01 13:39:05 +01001949subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001950module_exit(arm_smmu_exit);
1951
1952MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1953MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1954MODULE_LICENSE("GPL v2");